@editframe/elements 0.20.4-beta.0 → 0.21.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 (92) hide show
  1. package/dist/DelayedLoadingState.js +0 -27
  2. package/dist/EF_FRAMEGEN.d.ts +5 -3
  3. package/dist/EF_FRAMEGEN.js +50 -11
  4. package/dist/_virtual/_@oxc-project_runtime@0.93.0/helpers/decorate.js +7 -0
  5. package/dist/elements/ContextProxiesController.js +2 -22
  6. package/dist/elements/EFAudio.js +4 -8
  7. package/dist/elements/EFCaptions.js +59 -84
  8. package/dist/elements/EFImage.js +5 -6
  9. package/dist/elements/EFMedia/AssetIdMediaEngine.js +2 -4
  10. package/dist/elements/EFMedia/AssetMediaEngine.js +35 -30
  11. package/dist/elements/EFMedia/BaseMediaEngine.js +57 -73
  12. package/dist/elements/EFMedia/BufferedSeekingInput.js +134 -76
  13. package/dist/elements/EFMedia/JitMediaEngine.js +9 -19
  14. package/dist/elements/EFMedia/audioTasks/makeAudioBufferTask.js +3 -6
  15. package/dist/elements/EFMedia/audioTasks/makeAudioFrequencyAnalysisTask.js +1 -1
  16. package/dist/elements/EFMedia/audioTasks/makeAudioInitSegmentFetchTask.js +1 -1
  17. package/dist/elements/EFMedia/audioTasks/makeAudioInputTask.js +6 -5
  18. package/dist/elements/EFMedia/audioTasks/makeAudioSeekTask.js +1 -3
  19. package/dist/elements/EFMedia/audioTasks/makeAudioSegmentFetchTask.js +1 -1
  20. package/dist/elements/EFMedia/audioTasks/makeAudioSegmentIdTask.js +1 -1
  21. package/dist/elements/EFMedia/audioTasks/makeAudioTimeDomainAnalysisTask.js +1 -1
  22. package/dist/elements/EFMedia/shared/AudioSpanUtils.js +4 -16
  23. package/dist/elements/EFMedia/shared/BufferUtils.js +2 -15
  24. package/dist/elements/EFMedia/shared/GlobalInputCache.js +0 -24
  25. package/dist/elements/EFMedia/shared/PrecisionUtils.js +0 -21
  26. package/dist/elements/EFMedia/shared/ThumbnailExtractor.js +0 -17
  27. package/dist/elements/EFMedia/tasks/makeMediaEngineTask.js +1 -10
  28. package/dist/elements/EFMedia/videoTasks/MainVideoInputCache.d.ts +29 -0
  29. package/dist/elements/EFMedia/videoTasks/MainVideoInputCache.js +32 -0
  30. package/dist/elements/EFMedia/videoTasks/ScrubInputCache.js +1 -15
  31. package/dist/elements/EFMedia/videoTasks/makeScrubVideoBufferTask.js +1 -7
  32. package/dist/elements/EFMedia/videoTasks/makeScrubVideoInputTask.js +8 -5
  33. package/dist/elements/EFMedia/videoTasks/makeScrubVideoSeekTask.js +12 -13
  34. package/dist/elements/EFMedia/videoTasks/makeScrubVideoSegmentIdTask.js +1 -1
  35. package/dist/elements/EFMedia/videoTasks/makeUnifiedVideoSeekTask.js +134 -70
  36. package/dist/elements/EFMedia/videoTasks/makeVideoBufferTask.js +7 -11
  37. package/dist/elements/EFMedia.js +26 -24
  38. package/dist/elements/EFSourceMixin.js +5 -7
  39. package/dist/elements/EFSurface.js +6 -9
  40. package/dist/elements/EFTemporal.js +19 -37
  41. package/dist/elements/EFThumbnailStrip.js +16 -59
  42. package/dist/elements/EFTimegroup.js +95 -90
  43. package/dist/elements/EFVideo.d.ts +6 -2
  44. package/dist/elements/EFVideo.js +142 -107
  45. package/dist/elements/EFWaveform.js +18 -27
  46. package/dist/elements/SampleBuffer.js +2 -5
  47. package/dist/elements/TargetController.js +3 -3
  48. package/dist/elements/durationConverter.js +4 -4
  49. package/dist/elements/updateAnimations.js +14 -35
  50. package/dist/gui/ContextMixin.js +23 -52
  51. package/dist/gui/EFConfiguration.js +7 -7
  52. package/dist/gui/EFControls.js +5 -5
  53. package/dist/gui/EFFilmstrip.js +77 -98
  54. package/dist/gui/EFFitScale.js +5 -6
  55. package/dist/gui/EFFocusOverlay.js +4 -4
  56. package/dist/gui/EFPreview.js +4 -4
  57. package/dist/gui/EFScrubber.js +9 -9
  58. package/dist/gui/EFTimeDisplay.js +5 -5
  59. package/dist/gui/EFToggleLoop.js +4 -4
  60. package/dist/gui/EFTogglePlay.js +5 -5
  61. package/dist/gui/EFWorkbench.js +5 -5
  62. package/dist/gui/TWMixin2.js +1 -1
  63. package/dist/index.d.ts +1 -0
  64. package/dist/otel/BridgeSpanExporter.d.ts +13 -0
  65. package/dist/otel/BridgeSpanExporter.js +87 -0
  66. package/dist/otel/setupBrowserTracing.d.ts +12 -0
  67. package/dist/otel/setupBrowserTracing.js +30 -0
  68. package/dist/otel/tracingHelpers.d.ts +34 -0
  69. package/dist/otel/tracingHelpers.js +113 -0
  70. package/dist/transcoding/cache/RequestDeduplicator.js +0 -21
  71. package/dist/transcoding/cache/URLTokenDeduplicator.js +1 -21
  72. package/dist/transcoding/utils/UrlGenerator.js +2 -19
  73. package/dist/utils/LRUCache.js +6 -53
  74. package/package.json +10 -2
  75. package/src/elements/EFCaptions.browsertest.ts +2 -0
  76. package/src/elements/EFMedia/AssetMediaEngine.ts +65 -37
  77. package/src/elements/EFMedia/BaseMediaEngine.ts +110 -52
  78. package/src/elements/EFMedia/BufferedSeekingInput.ts +218 -101
  79. package/src/elements/EFMedia/audioTasks/makeAudioInputTask.ts +7 -3
  80. package/src/elements/EFMedia/videoTasks/MainVideoInputCache.ts +76 -0
  81. package/src/elements/EFMedia/videoTasks/makeScrubVideoInputTask.ts +16 -10
  82. package/src/elements/EFMedia/videoTasks/makeScrubVideoSeekTask.ts +7 -1
  83. package/src/elements/EFMedia/videoTasks/makeUnifiedVideoSeekTask.ts +222 -116
  84. package/src/elements/EFMedia.ts +16 -1
  85. package/src/elements/EFTimegroup.browsertest.ts +10 -8
  86. package/src/elements/EFTimegroup.ts +164 -76
  87. package/src/elements/EFVideo.browsertest.ts +19 -27
  88. package/src/elements/EFVideo.ts +203 -101
  89. package/src/otel/BridgeSpanExporter.ts +150 -0
  90. package/src/otel/setupBrowserTracing.ts +68 -0
  91. package/src/otel/tracingHelpers.ts +251 -0
  92. package/types.json +1 -1
@@ -1,13 +1,13 @@
1
1
  import { EF_INTERACTIVE } from "../EF_INTERACTIVE.js";
2
+ import { __decorate } from "../_virtual/_@oxc-project_runtime@0.93.0/helpers/decorate.js";
2
3
  import { EFSourceMixin } from "./EFSourceMixin.js";
3
4
  import { EFTemporal } from "./EFTemporal.js";
4
5
  import { FetchMixin } from "./FetchMixin.js";
5
6
  import { Task } from "@lit/task";
6
7
  import { LitElement, css, html } from "lit";
7
8
  import { customElement, property } from "lit/decorators.js";
8
- import _decorate from "@oxc-project/runtime/helpers/decorate";
9
9
  import { createRef, ref } from "lit/directives/ref.js";
10
- let EFImage = class EFImage$1 extends EFTemporal(EFSourceMixin(FetchMixin(LitElement), { assetType: "image_files" })) {
10
+ var EFImage = class EFImage$1 extends EFTemporal(EFSourceMixin(FetchMixin(LitElement), { assetType: "image_files" })) {
11
11
  constructor(..._args) {
12
12
  super(..._args);
13
13
  this.imageRef = createRef();
@@ -65,8 +65,7 @@ let EFImage = class EFImage$1 extends EFTemporal(EFSourceMixin(FetchMixin(LitEle
65
65
  }
66
66
  render() {
67
67
  const assetPath = this.assetPath();
68
- const isDirectUrl = this.isDirectUrl(assetPath);
69
- return isDirectUrl ? html`<img ${ref(this.imageRef)} src=${assetPath} />` : html`<canvas ${ref(this.canvasRef)}></canvas>`;
68
+ return this.isDirectUrl(assetPath) ? html`<img ${ref(this.imageRef)} src=${assetPath} />` : html`<canvas ${ref(this.canvasRef)}></canvas>`;
70
69
  }
71
70
  isDirectUrl(src) {
72
71
  return src.startsWith("http://") || src.startsWith("https://");
@@ -80,10 +79,10 @@ let EFImage = class EFImage$1 extends EFTemporal(EFSourceMixin(FetchMixin(LitEle
80
79
  return this.hasExplicitDuration;
81
80
  }
82
81
  };
83
- _decorate([property({
82
+ __decorate([property({
84
83
  type: String,
85
84
  attribute: "asset-id",
86
85
  reflect: true
87
86
  })], EFImage.prototype, "assetId", null);
88
- EFImage = _decorate([customElement("ef-image")], EFImage);
87
+ EFImage = __decorate([customElement("ef-image")], EFImage);
89
88
  export { EFImage };
@@ -2,8 +2,7 @@ import { AssetMediaEngine } from "./AssetMediaEngine.js";
2
2
  var AssetIdMediaEngine = class AssetIdMediaEngine extends AssetMediaEngine {
3
3
  static async fetchByAssetId(host, _urlGenerator, assetId, apiHost) {
4
4
  const url = `${apiHost}/api/v1/isobmff_files/${assetId}/index`;
5
- const response = await host.fetch(url);
6
- const data = await response.json();
5
+ const data = await (await host.fetch(url)).json();
7
6
  return new AssetIdMediaEngine(host, assetId, data, apiHost);
8
7
  }
9
8
  constructor(host, assetId, data, apiHost) {
@@ -11,8 +10,7 @@ var AssetIdMediaEngine = class AssetIdMediaEngine extends AssetMediaEngine {
11
10
  this.assetId = assetId;
12
11
  this.apiHost = apiHost;
13
12
  this.data = data;
14
- const longestFragment = Object.values(this.data).reduce((max, fragment) => Math.max(max, fragment.duration / fragment.timescale), 0);
15
- this.durationMs = longestFragment * 1e3;
13
+ this.durationMs = Object.values(this.data).reduce((max, fragment) => Math.max(max, fragment.duration / fragment.timescale), 0) * 1e3;
16
14
  }
17
15
  get initSegmentPaths() {
18
16
  const paths = {};
@@ -1,3 +1,4 @@
1
+ import { withSpan } from "../../otel/tracingHelpers.js";
1
2
  import { BaseMediaEngine } from "./BaseMediaEngine.js";
2
3
  import { convertToScaledTime, roundToMilliseconds } from "./shared/PrecisionUtils.js";
3
4
  var AssetMediaEngine = class AssetMediaEngine extends BaseMediaEngine {
@@ -10,10 +11,8 @@ var AssetMediaEngine = class AssetMediaEngine extends BaseMediaEngine {
10
11
  static async fetch(host, urlGenerator, src) {
11
12
  const engine = new AssetMediaEngine(host, src);
12
13
  const url = urlGenerator.generateTrackFragmentIndexUrl(src);
13
- const data = await engine.fetchManifest(url);
14
- engine.data = data;
15
- const longestFragment = Object.values(engine.data).reduce((max, fragment) => Math.max(max, fragment.duration / fragment.timescale), 0);
16
- engine.durationMs = longestFragment * 1e3;
14
+ engine.data = await engine.fetchManifest(url);
15
+ engine.durationMs = Object.values(engine.data).reduce((max, fragment) => Math.max(max, fragment.duration / fragment.timescale), 0) * 1e3;
17
16
  if (src.startsWith("/")) engine.src = src.slice(1);
18
17
  return engine;
19
18
  }
@@ -25,7 +24,7 @@ var AssetMediaEngine = class AssetMediaEngine extends BaseMediaEngine {
25
24
  }
26
25
  get videoRendition() {
27
26
  const videoTrack = this.videoTrackIndex;
28
- if (!videoTrack || videoTrack.track === void 0) return void 0;
27
+ if (!videoTrack || videoTrack.track === void 0) return;
29
28
  return {
30
29
  trackId: videoTrack.track,
31
30
  src: this.src,
@@ -34,7 +33,7 @@ var AssetMediaEngine = class AssetMediaEngine extends BaseMediaEngine {
34
33
  }
35
34
  get audioRendition() {
36
35
  const audioTrack = this.audioTrackIndex;
37
- if (!audioTrack || audioTrack.track === void 0) return void 0;
36
+ if (!audioTrack || audioTrack.track === void 0) return;
38
37
  return {
39
38
  trackId: audioTrack.track,
40
39
  src: this.src
@@ -67,25 +66,37 @@ var AssetMediaEngine = class AssetMediaEngine extends BaseMediaEngine {
67
66
  return `/@ef-track/${this.src}?trackId=${trackId}&segmentId=${segmentId}`;
68
67
  }
69
68
  async fetchInitSegment(rendition, signal) {
70
- if (!rendition.trackId) throw new Error("[fetchInitSegment] Track ID is required for asset metadata");
71
- const url = this.buildInitSegmentUrl(rendition.trackId);
72
- const initSegment = this.data[rendition.trackId]?.initSegment;
73
- if (!initSegment) throw new Error("Init segment not found");
74
- const headers = { Range: `bytes=${initSegment.offset}-${initSegment.offset + initSegment.size - 1}` };
75
- return this.fetchMediaWithHeaders(url, headers, signal);
69
+ return withSpan("assetEngine.fetchInitSegment", {
70
+ trackId: rendition.trackId || -1,
71
+ src: rendition.src
72
+ }, void 0, async (span) => {
73
+ if (!rendition.trackId) throw new Error("[fetchInitSegment] Track ID is required for asset metadata");
74
+ const url = this.buildInitSegmentUrl(rendition.trackId);
75
+ const initSegment = this.data[rendition.trackId]?.initSegment;
76
+ if (!initSegment) throw new Error("Init segment not found");
77
+ span.setAttribute("offset", initSegment.offset);
78
+ span.setAttribute("size", initSegment.size);
79
+ const headers = { Range: `bytes=${initSegment.offset}-${initSegment.offset + initSegment.size - 1}` };
80
+ return this.fetchMediaWithHeaders(url, headers, signal);
81
+ });
76
82
  }
77
83
  async fetchMediaSegment(segmentId, rendition, signal) {
78
- if (!rendition.trackId) throw new Error("[fetchMediaSegment] Track ID is required for asset metadata");
79
- if (segmentId === void 0) throw new Error("Segment ID is not available");
80
- const url = this.buildMediaSegmentUrl(rendition.trackId, segmentId);
81
- const mediaSegment = this.data[rendition.trackId]?.segments[segmentId];
82
- if (!mediaSegment) throw new Error("Media segment not found");
83
- const headers = { Range: `bytes=${mediaSegment.offset}-${mediaSegment.offset + mediaSegment.size - 1}` };
84
- return this.fetchMediaWithHeaders(url, headers, signal);
85
- }
86
- /**
87
- * Calculate audio segments for variable-duration segments using track fragment index
88
- */
84
+ return withSpan("assetEngine.fetchMediaSegment", {
85
+ segmentId,
86
+ trackId: rendition.trackId || -1,
87
+ src: rendition.src
88
+ }, void 0, async (span) => {
89
+ if (!rendition.trackId) throw new Error("[fetchMediaSegment] Track ID is required for asset metadata");
90
+ if (segmentId === void 0) throw new Error("Segment ID is not available");
91
+ const url = this.buildMediaSegmentUrl(rendition.trackId, segmentId);
92
+ const mediaSegment = this.data[rendition.trackId]?.segments[segmentId];
93
+ if (!mediaSegment) throw new Error("Media segment not found");
94
+ span.setAttribute("offset", mediaSegment.offset);
95
+ span.setAttribute("size", mediaSegment.size);
96
+ const headers = { Range: `bytes=${mediaSegment.offset}-${mediaSegment.offset + mediaSegment.size - 1}` };
97
+ return this.fetchMediaWithHeaders(url, headers, signal);
98
+ });
99
+ }
89
100
  calculateAudioSegmentRange(fromMs, toMs, rendition, _durationMs) {
90
101
  if (fromMs >= toMs || !rendition.trackId) {
91
102
  console.warn(`calculateAudioSegmentRange: invalid fromMs ${fromMs} toMs ${toMs} rendition ${JSON.stringify(rendition)}`);
@@ -149,13 +160,7 @@ var AssetMediaEngine = class AssetMediaEngine extends BaseMediaEngine {
149
160
  }
150
161
  return nearestSegmentIndex;
151
162
  }
152
- getScrubVideoRendition() {
153
- return void 0;
154
- }
155
- /**
156
- * Get preferred buffer configuration for this media engine
157
- * AssetMediaEngine uses lower buffering since segments are already optimized
158
- */
163
+ getScrubVideoRendition() {}
159
164
  getBufferConfig() {
160
165
  return {
161
166
  videoBufferDurationMs: 2e3,
@@ -1,3 +1,4 @@
1
+ import { withSpan } from "../../otel/tracingHelpers.js";
1
2
  import { RequestDeduplicator } from "../../transcoding/cache/RequestDeduplicator.js";
2
3
  import { SizeAwareLRUCache } from "../../utils/LRUCache.js";
3
4
  const mediaCache = new SizeAwareLRUCache(100 * 1024 * 1024);
@@ -6,60 +7,73 @@ var BaseMediaEngine = class {
6
7
  constructor(host) {
7
8
  this.host = host;
8
9
  }
9
- /**
10
- * Get video rendition if available. Returns undefined for audio-only assets.
11
- * Callers should handle undefined gracefully.
12
- */
13
10
  getVideoRendition() {
14
11
  return this.videoRendition;
15
12
  }
16
- /**
17
- * Get audio rendition if available. Returns undefined for video-only assets.
18
- * Callers should handle undefined gracefully.
19
- */
20
13
  getAudioRendition() {
21
14
  return this.audioRendition;
22
15
  }
23
- /**
24
- * Generate cache key for segment requests
25
- */
26
16
  getSegmentCacheKey(segmentId, rendition) {
27
17
  return `${rendition.src}-${rendition.id}-${segmentId}-${rendition.trackId}`;
28
18
  }
29
- /**
30
- * Unified fetch method with caching and global deduplication
31
- * All requests (media, manifest, init segments) go through this method
32
- */
33
19
  async fetchWithCache(url, options) {
34
- const { responseType, headers, signal } = options;
35
- const cacheKey = headers ? `${url}:${JSON.stringify(headers)}` : url;
36
- const cached = mediaCache.get(cacheKey);
37
- if (cached) {
38
- if (signal) return this.handleAbortForCachedRequest(cached, signal);
39
- return cached;
40
- }
41
- const promise = globalRequestDeduplicator.executeRequest(cacheKey, async () => {
42
- try {
43
- const response = await this.host.fetch(url, { headers });
44
- if (responseType === "json") return response.json();
45
- return response.arrayBuffer();
46
- } catch (error) {
20
+ return withSpan("mediaEngine.fetchWithCache", {
21
+ url: url.length > 100 ? `${url.substring(0, 100)}...` : url,
22
+ responseType: options.responseType,
23
+ hasHeaders: !!options.headers
24
+ }, void 0, async (span) => {
25
+ const t0 = performance.now();
26
+ const { responseType, headers, signal } = options;
27
+ const cacheKey = headers ? `${url}:${JSON.stringify(headers)}` : url;
28
+ const t1 = performance.now();
29
+ const cached = mediaCache.get(cacheKey);
30
+ const t2 = performance.now();
31
+ span.setAttribute("cacheLookupMs", Math.round((t2 - t1) * 1e3) / 1e3);
32
+ if (cached) {
33
+ span.setAttribute("cacheHit", true);
34
+ if (signal) {
35
+ const t3 = performance.now();
36
+ const result$1 = await this.handleAbortForCachedRequest(cached, signal);
37
+ const t4 = performance.now();
38
+ span.setAttribute("handleAbortMs", Math.round((t4 - t3) * 100) / 100);
39
+ span.setAttribute("totalCacheHitMs", Math.round((t4 - t0) * 100) / 100);
40
+ return result$1;
41
+ }
42
+ span.setAttribute("totalCacheHitMs", Math.round((t2 - t0) * 100) / 100);
43
+ return cached;
44
+ }
45
+ span.setAttribute("cacheHit", false);
46
+ const promise = globalRequestDeduplicator.executeRequest(cacheKey, async () => {
47
+ const fetchStart = performance.now();
48
+ try {
49
+ const response = await this.host.fetch(url, { headers });
50
+ const fetchEnd = performance.now();
51
+ span.setAttribute("fetchMs", fetchEnd - fetchStart);
52
+ if (responseType === "json") return response.json();
53
+ const buffer = await response.arrayBuffer();
54
+ span.setAttribute("sizeBytes", buffer.byteLength);
55
+ return buffer;
56
+ } catch (error) {
57
+ if (error instanceof DOMException && error.name === "AbortError") mediaCache.delete(cacheKey);
58
+ throw error;
59
+ }
60
+ });
61
+ mediaCache.set(cacheKey, promise);
62
+ promise.catch((error) => {
47
63
  if (error instanceof DOMException && error.name === "AbortError") mediaCache.delete(cacheKey);
48
- throw error;
64
+ });
65
+ if (signal) {
66
+ const result$1 = await this.handleAbortForCachedRequest(promise, signal);
67
+ const tEnd$1 = performance.now();
68
+ span.setAttribute("totalFetchMs", Math.round((tEnd$1 - t0) * 100) / 100);
69
+ return result$1;
49
70
  }
71
+ const result = await promise;
72
+ const tEnd = performance.now();
73
+ span.setAttribute("totalFetchMs", Math.round((tEnd - t0) * 100) / 100);
74
+ return result;
50
75
  });
51
- mediaCache.set(cacheKey, promise);
52
- promise.catch((error) => {
53
- if (error instanceof DOMException && error.name === "AbortError") mediaCache.delete(cacheKey);
54
- });
55
- if (signal) return this.handleAbortForCachedRequest(promise, signal);
56
- return promise;
57
- }
58
- /**
59
- * Handles abort logic for a cached request without affecting the underlying fetch
60
- * This allows multiple instances to share the same cached request while each
61
- * manages their own abort behavior
62
- */
76
+ }
63
77
  handleAbortForCachedRequest(promise, signal) {
64
78
  if (signal.aborted) throw new DOMException("Aborted", "AbortError");
65
79
  return Promise.race([promise, new Promise((_, reject) => {
@@ -99,39 +113,22 @@ var BaseMediaEngine = class {
99
113
  async fetchMediaCacheWithHeaders(url, headers, signal) {
100
114
  return this.fetchMediaWithHeaders(url, headers, signal);
101
115
  }
102
- /**
103
- * Fetch media segment with built-in deduplication
104
- * Now uses global deduplication for all requests
105
- */
106
116
  async fetchMediaSegmentWithDeduplication(segmentId, rendition, _signal) {
107
117
  const cacheKey = this.getSegmentCacheKey(segmentId, rendition);
108
118
  return globalRequestDeduplicator.executeRequest(cacheKey, async () => {
109
119
  return this.fetchMediaSegment(segmentId, rendition);
110
120
  });
111
121
  }
112
- /**
113
- * Check if a segment is currently being fetched
114
- */
115
122
  isSegmentBeingFetched(segmentId, rendition) {
116
123
  const cacheKey = this.getSegmentCacheKey(segmentId, rendition);
117
124
  return globalRequestDeduplicator.isPending(cacheKey);
118
125
  }
119
- /**
120
- * Get count of active segment requests (for debugging/monitoring)
121
- */
122
126
  getActiveSegmentRequestCount() {
123
127
  return globalRequestDeduplicator.getPendingCount();
124
128
  }
125
- /**
126
- * Cancel all active segment requests (for cleanup)
127
- */
128
129
  cancelAllSegmentRequests() {
129
130
  globalRequestDeduplicator.clear();
130
131
  }
131
- /**
132
- * Calculate audio segments needed for a time range
133
- * Each media engine implements this based on their segment structure
134
- */
135
132
  calculateAudioSegmentRange(fromMs, toMs, rendition, durationMs) {
136
133
  if (fromMs >= toMs) return [];
137
134
  const segments = [];
@@ -169,37 +166,24 @@ var BaseMediaEngine = class {
169
166
  }
170
167
  return segments;
171
168
  }
172
- /**
173
- * Check if a segment is cached for a given rendition
174
- * This needs to check the URL-based cache since that's where segments are actually stored
175
- */
176
169
  isSegmentCached(segmentId, rendition) {
177
170
  try {
178
171
  const maybeJitEngine = this;
179
172
  if (maybeJitEngine.urlGenerator && typeof maybeJitEngine.urlGenerator.generateSegmentUrl === "function") {
180
173
  if (!rendition.id) return false;
181
174
  const segmentUrl = maybeJitEngine.urlGenerator.generateSegmentUrl(segmentId, rendition.id, maybeJitEngine);
182
- const urlIsCached = mediaCache.has(segmentUrl);
183
- return urlIsCached;
175
+ return mediaCache.has(segmentUrl);
184
176
  }
185
177
  const cacheKey = `${rendition.src}-${rendition.id || "default"}-${segmentId}-${rendition.trackId}`;
186
- const isCached = mediaCache.has(cacheKey);
187
- return isCached;
178
+ return mediaCache.has(cacheKey);
188
179
  } catch (error) {
189
180
  console.warn(`🎬 BaseMediaEngine: Error checking if segment ${segmentId} is cached:`, error);
190
181
  return false;
191
182
  }
192
183
  }
193
- /**
194
- * Get cached segment IDs from a list for a given rendition
195
- */
196
184
  getCachedSegments(segmentIds, rendition) {
197
185
  return new Set(segmentIds.filter((id) => this.isSegmentCached(id, rendition)));
198
186
  }
199
- /**
200
- * Extract thumbnail canvases at multiple timestamps efficiently
201
- * Default implementation provides helpful error information
202
- */
203
187
  async extractThumbnails(timestamps) {
204
188
  const engineName = this.constructor.name;
205
189
  console.warn(`${engineName}: extractThumbnails not properly implemented. This MediaEngine type does not support thumbnail generation. Supported engines: JitMediaEngine. Requested ${timestamps.length} thumbnail${timestamps.length === 1 ? "" : "s"}.`);
@@ -1,7 +1,8 @@
1
+ import { withSpan } from "../../otel/tracingHelpers.js";
1
2
  import { roundToMilliseconds } from "./shared/PrecisionUtils.js";
2
3
  import { SampleBuffer } from "../SampleBuffer.js";
3
4
  import { AudioSampleSink, BufferSource, Input, InputAudioTrack, InputVideoTrack, MP4, VideoSampleSink } from "mediabunny";
4
- const defaultOptions = {
5
+ var defaultOptions = {
5
6
  videoBufferSize: 30,
6
7
  audioBufferSize: 100,
7
8
  startTimeOffsetMs: 0
@@ -14,11 +15,10 @@ var BufferedSeekingInput = class {
14
15
  this.trackIteratorCreationPromises = /* @__PURE__ */ new Map();
15
16
  this.trackSeekPromises = /* @__PURE__ */ new Map();
16
17
  const bufferSource = new BufferSource(arrayBuffer);
17
- const input = new Input({
18
+ this.input = new Input({
18
19
  source: bufferSource,
19
20
  formats: [MP4]
20
21
  });
21
- this.input = input;
22
22
  this.options = {
23
23
  ...defaultOptions,
24
24
  ...options
@@ -34,8 +34,7 @@ var BufferedSeekingInput = class {
34
34
  return buffer ? Object.freeze([...buffer.getContents()]) : [];
35
35
  }
36
36
  getBufferTimestamps(trackId) {
37
- const contents = this.getBufferContents(trackId);
38
- return contents.map((sample) => sample.timestamp || 0);
37
+ return this.getBufferContents(trackId).map((sample) => sample.timestamp || 0);
39
38
  }
40
39
  clearBuffer(trackId) {
41
40
  const buffer = this.trackBuffers.get(trackId);
@@ -45,30 +44,25 @@ var BufferedSeekingInput = class {
45
44
  return this.input.computeDuration();
46
45
  }
47
46
  async getTrack(trackId) {
48
- const tracks = await this.input.getTracks();
49
- const track = tracks.find((track$1) => track$1.id === trackId);
47
+ const track = (await this.input.getTracks()).find((track$1) => track$1.id === trackId);
50
48
  if (!track) throw new Error(`Track ${trackId} not found`);
51
49
  return track;
52
50
  }
53
51
  async getAudioTrack(trackId) {
54
- const tracks = await this.input.getAudioTracks();
55
- const track = tracks.find((track$1) => track$1.id === trackId && track$1.type === "audio");
52
+ const track = (await this.input.getAudioTracks()).find((track$1) => track$1.id === trackId && track$1.type === "audio");
56
53
  if (!track) throw new Error(`Track ${trackId} not found`);
57
54
  return track;
58
55
  }
59
56
  async getVideoTrack(trackId) {
60
- const tracks = await this.input.getVideoTracks();
61
- const track = tracks.find((track$1) => track$1.id === trackId && track$1.type === "video");
57
+ const track = (await this.input.getVideoTracks()).find((track$1) => track$1.id === trackId && track$1.type === "video");
62
58
  if (!track) throw new Error(`Track ${trackId} not found`);
63
59
  return track;
64
60
  }
65
61
  async getFirstVideoTrack() {
66
- const tracks = await this.input.getVideoTracks();
67
- return tracks[0];
62
+ return (await this.input.getVideoTracks())[0];
68
63
  }
69
64
  async getFirstAudioTrack() {
70
- const tracks = await this.input.getAudioTracks();
71
- return tracks[0];
65
+ return (await this.input.getAudioTracks())[0];
72
66
  }
73
67
  getTrackIterator(track) {
74
68
  if (this.trackIterators.has(track.id)) return this.trackIterators.get(track.id);
@@ -82,18 +76,15 @@ var BufferedSeekingInput = class {
82
76
  throw new Error(`Unsupported track type ${track.type}`);
83
77
  }
84
78
  createTrackIterator(track) {
85
- const sampleSink = this.createTrackSampleSink(track);
86
- return sampleSink.samples();
79
+ return this.createTrackSampleSink(track).samples();
87
80
  }
88
81
  createTrackBuffer(track) {
89
82
  if (track.type === "audio") {
90
83
  const bufferSize$1 = this.options.audioBufferSize;
91
- const sampleBuffer$1 = new SampleBuffer(bufferSize$1);
92
- return sampleBuffer$1;
84
+ return new SampleBuffer(bufferSize$1);
93
85
  }
94
86
  const bufferSize = this.options.videoBufferSize;
95
- const sampleBuffer = new SampleBuffer(bufferSize);
96
- return sampleBuffer;
87
+ return new SampleBuffer(bufferSize);
97
88
  }
98
89
  getTrackBuffer(track) {
99
90
  const maybeTrackBuffer = this.trackBuffers.get(track.id);
@@ -103,21 +94,30 @@ var BufferedSeekingInput = class {
103
94
  return trackBuffer;
104
95
  }
105
96
  async seek(trackId, timeMs) {
106
- const mediaTimeMs = timeMs + this.startTimeOffsetMs;
107
- const roundedMediaTimeMs = roundToMilliseconds(mediaTimeMs);
108
- const existingSeek = this.trackSeekPromises.get(trackId);
109
- if (existingSeek) await existingSeek;
110
- const seekPromise = this.seekSafe(trackId, roundedMediaTimeMs);
111
- this.trackSeekPromises.set(trackId, seekPromise);
112
- try {
113
- return await seekPromise;
114
- } finally {
115
- this.trackSeekPromises.delete(trackId);
116
- }
97
+ return withSpan("bufferedInput.seek", {
98
+ trackId,
99
+ timeMs,
100
+ startTimeOffsetMs: this.startTimeOffsetMs
101
+ }, void 0, async (span) => {
102
+ const mediaTimeMs = timeMs + this.startTimeOffsetMs;
103
+ const roundedMediaTimeMs = roundToMilliseconds(mediaTimeMs);
104
+ span.setAttribute("roundedMediaTimeMs", roundedMediaTimeMs);
105
+ const existingSeek = this.trackSeekPromises.get(trackId);
106
+ if (existingSeek) {
107
+ span.setAttribute("waitedForExistingSeek", true);
108
+ await existingSeek;
109
+ }
110
+ const seekPromise = this.seekSafe(trackId, roundedMediaTimeMs);
111
+ this.trackSeekPromises.set(trackId, seekPromise);
112
+ try {
113
+ return await seekPromise;
114
+ } finally {
115
+ this.trackSeekPromises.delete(trackId);
116
+ }
117
+ });
117
118
  }
118
119
  async resetIterator(track) {
119
- const trackBuffer = this.trackBuffers.get(track.id);
120
- trackBuffer?.clear();
120
+ this.trackBuffers.get(track.id)?.clear();
121
121
  const ongoingIteratorCreation = this.trackIteratorCreationPromises.get(track.id);
122
122
  if (ongoingIteratorCreation) await ongoingIteratorCreation;
123
123
  const iterator = this.trackIterators.get(track.id);
@@ -128,50 +128,108 @@ var BufferedSeekingInput = class {
128
128
  }
129
129
  #seekLock;
130
130
  async seekSafe(trackId, timeMs) {
131
- if (this.#seekLock) await this.#seekLock.promise;
132
- const seekLock = Promise.withResolvers();
133
- this.#seekLock = seekLock;
134
- try {
135
- const track = await this.getTrack(trackId);
136
- const trackBuffer = this.getTrackBuffer(track);
137
- const roundedTimeMs = roundToMilliseconds(timeMs);
138
- const firstTimestampMs = roundToMilliseconds(await track.getFirstTimestamp() * 1e3);
139
- if (roundedTimeMs < firstTimestampMs) {
140
- console.error("Seeking outside bounds of input", {
141
- roundedTimeMs,
142
- firstTimestampMs
143
- });
144
- throw new NoSample(`Seeking outside bounds of input ${roundedTimeMs} < ${firstTimestampMs}`);
131
+ return withSpan("bufferedInput.seekSafe", {
132
+ trackId,
133
+ timeMs
134
+ }, void 0, async (span) => {
135
+ if (this.#seekLock) {
136
+ span.setAttribute("waitedForSeekLock", true);
137
+ await this.#seekLock.promise;
145
138
  }
146
- const bufferContents = trackBuffer.getContents();
147
- if (bufferContents.length > 0) {
148
- const bufferStartMs = roundToMilliseconds(trackBuffer.firstTimestamp * 1e3);
149
- if (roundedTimeMs < bufferStartMs) await this.resetIterator(track);
139
+ const seekLock = Promise.withResolvers();
140
+ this.#seekLock = seekLock;
141
+ try {
142
+ const track = await this.getTrack(trackId);
143
+ span.setAttribute("trackType", track.type);
144
+ const trackBuffer = this.getTrackBuffer(track);
145
+ const roundedTimeMs = roundToMilliseconds(timeMs);
146
+ const firstTimestampMs = roundToMilliseconds(await track.getFirstTimestamp() * 1e3);
147
+ span.setAttribute("firstTimestampMs", firstTimestampMs);
148
+ if (roundedTimeMs < firstTimestampMs) {
149
+ console.error("Seeking outside bounds of input", {
150
+ roundedTimeMs,
151
+ firstTimestampMs
152
+ });
153
+ throw new NoSample(`Seeking outside bounds of input ${roundedTimeMs} < ${firstTimestampMs}`);
154
+ }
155
+ const bufferContents = trackBuffer.getContents();
156
+ span.setAttribute("bufferContentsLength", bufferContents.length);
157
+ if (bufferContents.length > 0) {
158
+ const bufferStartMs = roundToMilliseconds(trackBuffer.firstTimestamp * 1e3);
159
+ span.setAttribute("bufferStartMs", bufferStartMs);
160
+ if (roundedTimeMs < bufferStartMs) {
161
+ span.setAttribute("resetIterator", true);
162
+ await this.resetIterator(track);
163
+ }
164
+ }
165
+ const alreadyInBuffer = trackBuffer.find(timeMs);
166
+ if (alreadyInBuffer) {
167
+ span.setAttribute("foundInBuffer", true);
168
+ span.setAttribute("bufferSize", trackBuffer.length);
169
+ const contents$1 = trackBuffer.getContents();
170
+ if (contents$1.length > 0) span.setAttribute("bufferTimestamps", contents$1.map((s) => Math.round((s.timestamp || 0) * 1e3)).slice(0, 10).join(","));
171
+ return alreadyInBuffer;
172
+ }
173
+ span.setAttribute("foundInBuffer", false);
174
+ span.setAttribute("bufferSize", trackBuffer.length);
175
+ span.setAttribute("requestedTimeMs", Math.round(timeMs));
176
+ const contents = trackBuffer.getContents();
177
+ if (contents.length > 0) {
178
+ const firstSample = contents[0];
179
+ const lastSample = contents[contents.length - 1];
180
+ if (firstSample && lastSample) {
181
+ const bufferStartMs = Math.round((firstSample.timestamp || 0) * 1e3);
182
+ const bufferEndMs = Math.round(((lastSample.timestamp || 0) + (lastSample.duration || 0)) * 1e3);
183
+ span.setAttribute("bufferStartMs", bufferStartMs);
184
+ span.setAttribute("bufferEndMs", bufferEndMs);
185
+ span.setAttribute("bufferRangeMs", `${bufferStartMs}-${bufferEndMs}`);
186
+ }
187
+ }
188
+ const iterator = this.getTrackIterator(track);
189
+ let iterationCount = 0;
190
+ const decodeStart = performance.now();
191
+ while (true) {
192
+ iterationCount++;
193
+ const iterStart = performance.now();
194
+ const { done, value: decodedSample } = await iterator.next();
195
+ const iterEnd = performance.now();
196
+ if (iterationCount <= 5) span.setAttribute(`iter${iterationCount}Ms`, Math.round((iterEnd - iterStart) * 100) / 100);
197
+ if (decodedSample) {
198
+ trackBuffer.push(decodedSample);
199
+ if (iterationCount <= 5) span.setAttribute(`iter${iterationCount}Timestamp`, Math.round((decodedSample.timestamp || 0) * 1e3));
200
+ }
201
+ const foundSample = trackBuffer.find(roundedTimeMs);
202
+ if (foundSample) {
203
+ const decodeEnd = performance.now();
204
+ span.setAttribute("iterationCount", iterationCount);
205
+ span.setAttribute("decodeMs", Math.round((decodeEnd - decodeStart) * 100) / 100);
206
+ span.setAttribute("avgIterMs", Math.round((decodeEnd - decodeStart) / iterationCount * 100) / 100);
207
+ span.setAttribute("foundSample", true);
208
+ span.setAttribute("foundTimestamp", Math.round((foundSample.timestamp || 0) * 1e3));
209
+ return foundSample;
210
+ }
211
+ if (done) break;
212
+ }
213
+ span.setAttribute("iterationCount", iterationCount);
214
+ span.setAttribute("reachedEnd", true);
215
+ const finalBufferContents = trackBuffer.getContents();
216
+ if (finalBufferContents.length > 0) {
217
+ const lastSample = finalBufferContents[finalBufferContents.length - 1];
218
+ const lastSampleEndMs = roundToMilliseconds(((lastSample?.timestamp || 0) + (lastSample?.duration || 0)) * 1e3);
219
+ const trackDurationMs = await track.computeDuration() * 1e3;
220
+ const isSeekingToTrackEnd = roundToMilliseconds(timeMs) === roundToMilliseconds(trackDurationMs);
221
+ const isAtEndOfTrack = roundToMilliseconds(timeMs) >= lastSampleEndMs;
222
+ if (isSeekingToTrackEnd && isAtEndOfTrack) {
223
+ span.setAttribute("returnedLastSample", true);
224
+ return lastSample;
225
+ }
226
+ }
227
+ throw new NoSample(`Sample not found for time ${timeMs} in ${track.type} track ${trackId}`);
228
+ } finally {
229
+ this.#seekLock = void 0;
230
+ seekLock.resolve();
150
231
  }
151
- const alreadyInBuffer = trackBuffer.find(timeMs);
152
- if (alreadyInBuffer) return alreadyInBuffer;
153
- const iterator = this.getTrackIterator(track);
154
- while (true) {
155
- const { done, value: decodedSample } = await iterator.next();
156
- if (decodedSample) trackBuffer.push(decodedSample);
157
- const foundSample = trackBuffer.find(roundedTimeMs);
158
- if (foundSample) return foundSample;
159
- if (done) break;
160
- }
161
- const finalBufferContents = trackBuffer.getContents();
162
- if (finalBufferContents.length > 0) {
163
- const lastSample = finalBufferContents[finalBufferContents.length - 1];
164
- const lastSampleEndMs = roundToMilliseconds(((lastSample?.timestamp || 0) + (lastSample?.duration || 0)) * 1e3);
165
- const trackDurationMs = await track.computeDuration() * 1e3;
166
- const isSeekingToTrackEnd = roundToMilliseconds(timeMs) === roundToMilliseconds(trackDurationMs);
167
- const isAtEndOfTrack = roundToMilliseconds(timeMs) >= lastSampleEndMs;
168
- if (isSeekingToTrackEnd && isAtEndOfTrack) return lastSample;
169
- }
170
- throw new NoSample(`Sample not found for time ${timeMs} in ${track.type} track ${trackId}`);
171
- } finally {
172
- this.#seekLock = void 0;
173
- seekLock.resolve();
174
- }
232
+ });
175
233
  }
176
234
  };
177
235
  export { BufferedSeekingInput };