@meframe/core 0.6.13 → 0.7.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.
- package/CHANGELOG.md +47 -0
- package/README.md +17 -5
- package/dist/Meframe.d.ts +14 -3
- package/dist/Meframe.d.ts.map +1 -1
- package/dist/Meframe.js +22 -12
- package/dist/Meframe.js.map +1 -1
- package/dist/cache/CacheManager.d.ts +2 -0
- package/dist/cache/CacheManager.d.ts.map +1 -1
- package/dist/cache/CacheManager.js +5 -0
- package/dist/cache/CacheManager.js.map +1 -1
- package/dist/cache/l1/VideoL1Cache.d.ts +2 -0
- package/dist/cache/l1/VideoL1Cache.d.ts.map +1 -1
- package/dist/cache/l1/VideoL1Cache.js +13 -7
- package/dist/cache/l1/VideoL1Cache.js.map +1 -1
- package/dist/cache/resource/ResourceCache.d.ts +0 -12
- package/dist/cache/resource/ResourceCache.d.ts.map +1 -1
- package/dist/cache/resource/ResourceCache.js +0 -24
- package/dist/cache/resource/ResourceCache.js.map +1 -1
- package/dist/config/defaults.d.ts.map +1 -1
- package/dist/config/defaults.js +4 -1
- package/dist/config/defaults.js.map +1 -1
- package/dist/config/index.d.ts +1 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/types.d.ts +29 -0
- package/dist/config/types.d.ts.map +1 -1
- package/dist/config/validation.d.ts.map +1 -1
- package/dist/config/validation.js +3 -0
- package/dist/config/validation.js.map +1 -1
- package/dist/controllers/PlaybackController.d.ts +38 -9
- package/dist/controllers/PlaybackController.d.ts.map +1 -1
- package/dist/controllers/PlaybackController.js +269 -154
- package/dist/controllers/PlaybackController.js.map +1 -1
- package/dist/controllers/PlaybackStateMachine.d.ts.map +1 -1
- package/dist/controllers/PlaybackStateMachine.js +2 -1
- package/dist/controllers/PlaybackStateMachine.js.map +1 -1
- package/dist/controllers/types.d.ts +10 -2
- package/dist/controllers/types.d.ts.map +1 -1
- package/dist/controllers/types.js.map +1 -1
- package/dist/event/events.d.ts +2 -0
- package/dist/event/events.d.ts.map +1 -1
- package/dist/event/events.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/model/CompositionModel.d.ts +0 -1
- package/dist/model/CompositionModel.d.ts.map +1 -1
- package/dist/model/CompositionModel.js +0 -6
- package/dist/model/CompositionModel.js.map +1 -1
- package/dist/model/patch.js +12 -12
- package/dist/model/patch.js.map +1 -1
- package/dist/node_modules/.pnpm/{mp4-muxer@5.2.2 → mp4-muxer@5.2.2_patch_hash_452653ac44da4ef32d244694e500e34d084eaa3c4232515ddec962eacc5f30ea}/node_modules/mp4-muxer/build/mp4-muxer.js +17 -3
- package/dist/node_modules/.pnpm/mp4-muxer@5.2.2_patch_hash_452653ac44da4ef32d244694e500e34d084eaa3c4232515ddec962eacc5f30ea/node_modules/mp4-muxer/build/mp4-muxer.js.map +1 -0
- package/dist/orchestrator/AudioExportSession.d.ts +28 -3
- package/dist/orchestrator/AudioExportSession.d.ts.map +1 -1
- package/dist/orchestrator/AudioExportSession.js +131 -15
- package/dist/orchestrator/AudioExportSession.js.map +1 -1
- package/dist/orchestrator/AudioPreviewSession.d.ts +6 -1
- package/dist/orchestrator/AudioPreviewSession.d.ts.map +1 -1
- package/dist/orchestrator/AudioPreviewSession.js +57 -18
- package/dist/orchestrator/AudioPreviewSession.js.map +1 -1
- package/dist/orchestrator/AudioWindowPreparer.d.ts +5 -4
- package/dist/orchestrator/AudioWindowPreparer.d.ts.map +1 -1
- package/dist/orchestrator/AudioWindowPreparer.js +76 -28
- package/dist/orchestrator/AudioWindowPreparer.js.map +1 -1
- package/dist/orchestrator/CompositionPlanner.d.ts +5 -2
- package/dist/orchestrator/CompositionPlanner.d.ts.map +1 -1
- package/dist/orchestrator/CompositionPlanner.js +8 -3
- package/dist/orchestrator/CompositionPlanner.js.map +1 -1
- package/dist/orchestrator/ExportScheduler.d.ts +1 -0
- package/dist/orchestrator/ExportScheduler.d.ts.map +1 -1
- package/dist/orchestrator/ExportScheduler.js +177 -106
- package/dist/orchestrator/ExportScheduler.js.map +1 -1
- package/dist/orchestrator/Orchestrator.d.ts +30 -4
- package/dist/orchestrator/Orchestrator.d.ts.map +1 -1
- package/dist/orchestrator/Orchestrator.js +264 -111
- package/dist/orchestrator/Orchestrator.js.map +1 -1
- package/dist/orchestrator/PreviewFrameScheduler.d.ts +21 -1
- package/dist/orchestrator/PreviewFrameScheduler.d.ts.map +1 -1
- package/dist/orchestrator/PreviewFrameScheduler.js +129 -30
- package/dist/orchestrator/PreviewFrameScheduler.js.map +1 -1
- package/dist/orchestrator/VideoWindowDecodeSession.d.ts +22 -1
- package/dist/orchestrator/VideoWindowDecodeSession.d.ts.map +1 -1
- package/dist/orchestrator/VideoWindowDecodeSession.js +426 -198
- package/dist/orchestrator/VideoWindowDecodeSession.js.map +1 -1
- package/dist/orchestrator/aac-window-timeline.d.ts +1 -1
- package/dist/orchestrator/aac-window-timeline.d.ts.map +1 -1
- package/dist/orchestrator/aac-window-timeline.js +10 -2
- package/dist/orchestrator/aac-window-timeline.js.map +1 -1
- package/dist/orchestrator/types.d.ts +5 -1
- package/dist/orchestrator/types.d.ts.map +1 -1
- package/dist/stages/compose/FrameRateConverter.d.ts +2 -1
- package/dist/stages/compose/FrameRateConverter.d.ts.map +1 -1
- package/dist/stages/compose/LayerRenderPolicy.d.ts +14 -0
- package/dist/stages/compose/LayerRenderPolicy.d.ts.map +1 -0
- package/dist/stages/compose/LayerRenderPolicy.js +18 -0
- package/dist/stages/compose/LayerRenderPolicy.js.map +1 -0
- package/dist/stages/compose/OfflineAudioMixer.d.ts +2 -0
- package/dist/stages/compose/OfflineAudioMixer.d.ts.map +1 -1
- package/dist/stages/compose/OfflineAudioMixer.js +14 -2
- package/dist/stages/compose/OfflineAudioMixer.js.map +1 -1
- package/dist/stages/compose/OwnedFrameTransform.d.ts +52 -0
- package/dist/stages/compose/OwnedFrameTransform.d.ts.map +1 -0
- package/dist/stages/compose/OwnedFrameTransform.js +154 -0
- package/dist/stages/compose/OwnedFrameTransform.js.map +1 -0
- package/dist/stages/compose/TransitionProcessor.js +0 -1
- package/dist/stages/compose/TransitionProcessor.js.map +1 -1
- package/dist/stages/compose/VideoComposer.d.ts +12 -2
- package/dist/stages/compose/VideoComposer.d.ts.map +1 -1
- package/dist/stages/compose/VideoComposer.js +100 -92
- package/dist/stages/compose/VideoComposer.js.map +1 -1
- package/dist/stages/compose/text-renderers/basic-text-renderer.d.ts.map +1 -1
- package/dist/stages/compose/text-renderers/basic-text-renderer.js +24 -21
- package/dist/stages/compose/text-renderers/basic-text-renderer.js.map +1 -1
- package/dist/stages/compose/text-renderers/caption-stagger-entrance-renderer.d.ts.map +1 -1
- package/dist/stages/compose/text-renderers/caption-stagger-entrance-renderer.js +110 -99
- package/dist/stages/compose/text-renderers/caption-stagger-entrance-renderer.js.map +1 -1
- package/dist/stages/compose/text-renderers/character-ktv-renderer.d.ts.map +1 -1
- package/dist/stages/compose/text-renderers/character-ktv-renderer.js +41 -38
- package/dist/stages/compose/text-renderers/character-ktv-renderer.js.map +1 -1
- package/dist/stages/compose/text-renderers/word-by-word-renderer.d.ts.map +1 -1
- package/dist/stages/compose/text-renderers/word-by-word-renderer.js +47 -39
- package/dist/stages/compose/text-renderers/word-by-word-renderer.js.map +1 -1
- package/dist/stages/compose/text-renderers/word-fancy-renderer.d.ts.map +1 -1
- package/dist/stages/compose/text-renderers/word-fancy-renderer.js +54 -48
- package/dist/stages/compose/text-renderers/word-fancy-renderer.js.map +1 -1
- package/dist/stages/compose/text-utils/text-metrics.d.ts.map +1 -1
- package/dist/stages/compose/text-utils/text-metrics.js +6 -4
- package/dist/stages/compose/text-utils/text-metrics.js.map +1 -1
- package/dist/stages/compose/types.d.ts +12 -0
- package/dist/stages/compose/types.d.ts.map +1 -1
- package/dist/stages/decode/BaseDecoder.d.ts +5 -0
- package/dist/stages/decode/BaseDecoder.d.ts.map +1 -1
- package/dist/stages/decode/BaseDecoder.js +52 -10
- package/dist/stages/decode/BaseDecoder.js.map +1 -1
- package/dist/stages/decode/preview-video-decoder.d.ts +48 -2
- package/dist/stages/decode/preview-video-decoder.d.ts.map +1 -1
- package/dist/stages/decode/preview-video-decoder.js +241 -71
- package/dist/stages/decode/preview-video-decoder.js.map +1 -1
- package/dist/stages/decode/video-decoder.d.ts +2 -11
- package/dist/stages/decode/video-decoder.d.ts.map +1 -1
- package/dist/stages/decode/video-decoder.js +60 -61
- package/dist/stages/decode/video-decoder.js.map +1 -1
- package/dist/stages/encode/BaseEncoder.d.ts +9 -2
- package/dist/stages/encode/BaseEncoder.d.ts.map +1 -1
- package/dist/stages/encode/BaseEncoder.js +90 -59
- package/dist/stages/encode/BaseEncoder.js.map +1 -1
- package/dist/stages/load/ResourceLoader.d.ts +69 -8
- package/dist/stages/load/ResourceLoader.d.ts.map +1 -1
- package/dist/stages/load/ResourceLoader.js +248 -67
- package/dist/stages/load/ResourceLoader.js.map +1 -1
- package/dist/stages/load/TaskManager.d.ts.map +1 -1
- package/dist/stages/load/TaskManager.js +9 -3
- package/dist/stages/load/TaskManager.js.map +1 -1
- package/dist/stages/load/types.d.ts +2 -2
- package/dist/stages/load/types.d.ts.map +1 -1
- package/dist/stages/mux/MP4Muxer.d.ts +7 -1
- package/dist/stages/mux/MP4Muxer.d.ts.map +1 -1
- package/dist/stages/mux/MP4Muxer.js +8 -10
- package/dist/stages/mux/MP4Muxer.js.map +1 -1
- package/dist/stages/mux/MuxManager.d.ts +7 -0
- package/dist/stages/mux/MuxManager.d.ts.map +1 -1
- package/dist/stages/mux/MuxManager.js +9 -0
- package/dist/stages/mux/MuxManager.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/errors.d.ts +2 -0
- package/dist/utils/errors.d.ts.map +1 -1
- package/dist/utils/errors.js +10 -1
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/until-aborted.d.ts +8 -0
- package/dist/utils/until-aborted.d.ts.map +1 -0
- package/dist/utils/until-aborted.js +33 -0
- package/dist/utils/until-aborted.js.map +1 -0
- package/dist/worker/WorkerChannel.d.ts.map +1 -1
- package/dist/worker/WorkerChannel.js +45 -10
- package/dist/worker/WorkerChannel.js.map +1 -1
- package/dist/worker/worker-manifest.js +1 -1
- package/dist/worker/worker-manifest.js.map +1 -1
- package/dist/workers/stages/export/{export.worker.C8jKSbVj.js → export.worker.BG8HA7dY.js} +745 -450
- package/dist/workers/stages/export/export.worker.BG8HA7dY.js.map +1 -0
- package/dist/workers/worker-manifest.json +1 -1
- package/package.json +1 -1
- package/dist/node_modules/.pnpm/mp4-muxer@5.2.2/node_modules/mp4-muxer/build/mp4-muxer.js.map +0 -1
- package/dist/workers/stages/export/export.worker.C8jKSbVj.js.map +0 -1
|
@@ -5,19 +5,32 @@ import { createImageBitmapFromBlob } from "../../utils/image-utils.js";
|
|
|
5
5
|
import { MP4IndexParser } from "../demux/MP4IndexParser.js";
|
|
6
6
|
import { MP3FrameParser } from "../demux/MP3FrameParser.js";
|
|
7
7
|
import { WAVIndexParser } from "../demux/WAVIndexParser.js";
|
|
8
|
-
import { ResourceNotCachedError, NotMP4Error, MP4MoovNotFoundError, isTerminalMediaResourceError, ResourceCorruptedError, EmptyStreamError, isDOMException, toResourceErrorInfo,
|
|
8
|
+
import { ResourceNotCachedError, NotMP4Error, MP4MoovNotFoundError, isTerminalMediaResourceError, ResourceCorruptedError, EmptyStreamError, isDOMException, UnsupportedAudioContainerError, toResourceErrorInfo, markReportedToHost, ResourceSupersededError } from "../../utils/errors.js";
|
|
9
|
+
import { untilAborted } from "../../utils/until-aborted.js";
|
|
9
10
|
class ResourceLoader {
|
|
10
11
|
cacheManager;
|
|
11
|
-
|
|
12
|
+
getCompositionModel;
|
|
13
|
+
get model() {
|
|
14
|
+
return this.getCompositionModel();
|
|
15
|
+
}
|
|
12
16
|
taskManager;
|
|
13
17
|
eventBus;
|
|
14
|
-
onStateChange;
|
|
15
18
|
onResourceInvalidated;
|
|
16
19
|
blobCache = /* @__PURE__ */ new Map();
|
|
17
|
-
parsingIndexes = /* @__PURE__ */ new
|
|
20
|
+
parsingIndexes = /* @__PURE__ */ new Map();
|
|
18
21
|
disposed = false;
|
|
22
|
+
running = /* @__PURE__ */ new Set();
|
|
23
|
+
imageControllers = /* @__PURE__ */ new Set();
|
|
24
|
+
loadOwners = /* @__PURE__ */ new Map();
|
|
25
|
+
readOwners = /* @__PURE__ */ new Set();
|
|
26
|
+
disposePromise = null;
|
|
27
|
+
// null is the tombstone of a removed resource; only a string is a live identity.
|
|
19
28
|
sourceIdentities = /* @__PURE__ */ new Map();
|
|
20
29
|
sourceRevisions = /* @__PURE__ */ new Map();
|
|
30
|
+
// The last live identity of each id, kept across removal so an id added back can be compared with what its bytes were.
|
|
31
|
+
lastLiveIdentities = /* @__PURE__ */ new Map();
|
|
32
|
+
// Ids whose next load must ask the host (ensureCached) even when bytes for the id are already cached.
|
|
33
|
+
unconfirmedSources = /* @__PURE__ */ new Set();
|
|
21
34
|
consumers = /* @__PURE__ */ new Map();
|
|
22
35
|
ensureCached;
|
|
23
36
|
// Preloading state
|
|
@@ -33,11 +46,45 @@ class ResourceLoader {
|
|
|
33
46
|
const preloadConcurrency = config.preloadConcurrency ?? 1;
|
|
34
47
|
this.taskManager = new TaskManager(maxConcurrent, preloadConcurrency);
|
|
35
48
|
this.eventBus = options.eventBus;
|
|
36
|
-
this.onStateChange = options.onStateChange;
|
|
37
49
|
this.onResourceInvalidated = options.onResourceInvalidated;
|
|
38
50
|
this.cacheManager = options.cacheManager;
|
|
51
|
+
this.getCompositionModel = options.getCompositionModel;
|
|
39
52
|
this.ensureCached = options.ensureCached;
|
|
40
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Settled work is removed from `running`; drain and dispose wait on what is still in flight. An owner signal ends the operation
|
|
56
|
+
* even when the host never answers the read it awaits. `discard` releases a native result that arrives after the owner aborted;
|
|
57
|
+
* the result has no caller then, so it is released there, once.
|
|
58
|
+
*/
|
|
59
|
+
track(promise, signal, discard) {
|
|
60
|
+
const tracked = untilAborted(promise, signal, discard);
|
|
61
|
+
this.running.add(tracked);
|
|
62
|
+
const forget = () => this.running.delete(tracked);
|
|
63
|
+
tracked.then(forget, forget);
|
|
64
|
+
return tracked;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Waits for `promise` on behalf of an operation that owns `signal`. The wait ends with that signal, with drain or dispose (which
|
|
68
|
+
* abort every owner in `readOwners`), or when the promise settles. The promise is not cancelled, so a host read that never
|
|
69
|
+
* answers cannot hold drain, dispose or a replacement.
|
|
70
|
+
*/
|
|
71
|
+
awaitOwned(promise, signal) {
|
|
72
|
+
const owner = new AbortController();
|
|
73
|
+
const abortWithCaller = () => owner.abort(signal?.reason);
|
|
74
|
+
this.readOwners.add(owner);
|
|
75
|
+
if (signal?.aborted) abortWithCaller();
|
|
76
|
+
else signal?.addEventListener("abort", abortWithCaller, { once: true });
|
|
77
|
+
return this.track(promise, owner.signal).finally(() => {
|
|
78
|
+
this.readOwners.delete(owner);
|
|
79
|
+
signal?.removeEventListener("abort", abortWithCaller);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/** Aborts every owner that awaitOwned registered; drain and dispose call it. */
|
|
83
|
+
abortReads() {
|
|
84
|
+
for (const owner of this.readOwners) {
|
|
85
|
+
owner.abort(new DOMException("Resource loader disposed", "AbortError"));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
41
88
|
assertTaskCurrent(task) {
|
|
42
89
|
const resource = this.model?.getResource(task.resourceId);
|
|
43
90
|
if (this.disposed || task.controller?.signal.aborted || !resource || resource.uri !== task.resource.uri || resource.type !== task.resource.type || task.sourceRevision !== void 0 && task.sourceRevision !== this.sourceRevisions.get(task.resourceId)) {
|
|
@@ -68,29 +115,52 @@ class ResourceLoader {
|
|
|
68
115
|
else signal?.addEventListener("abort", abort, { once: true });
|
|
69
116
|
});
|
|
70
117
|
}
|
|
71
|
-
|
|
72
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Records each resource's source identity (type and URI) and retires in-memory data that belongs to an older source.
|
|
120
|
+
*
|
|
121
|
+
* A live id that arrives with a new type or URI, or one added back after it was removed, is re-identified: its load task is
|
|
122
|
+
* cancelled, its index and parsed data are dropped, its revision moves on (so work for the old source publishes nothing), it
|
|
123
|
+
* becomes `pending`, and its next load calls the host's `ensureCached` with the new resource, even when bytes for the id are
|
|
124
|
+
* already cached. The host decides whether those bytes match; the engine never deletes them. Returns the re-identified ids;
|
|
125
|
+
* the caller starts their loads (`startPreloading(ids)`) once the model is in place.
|
|
126
|
+
*/
|
|
127
|
+
refreshResourceIdentities(model) {
|
|
73
128
|
for (const [id, identity] of this.sourceIdentities) {
|
|
74
129
|
if (identity && !model.resources.has(id)) {
|
|
75
|
-
this.sourceIdentities.set(id,
|
|
130
|
+
this.sourceIdentities.set(id, null);
|
|
76
131
|
this.sourceRevisions.set(id, (this.sourceRevisions.get(id) ?? 0) + 1);
|
|
77
132
|
this.taskManager.cancelTask(id);
|
|
78
133
|
}
|
|
79
134
|
}
|
|
135
|
+
const reidentified = [];
|
|
80
136
|
for (const [id, resource] of model.resources) {
|
|
81
137
|
const identity = JSON.stringify([resource.type, resource.uri]);
|
|
82
138
|
const old = this.sourceIdentities.get(id);
|
|
83
139
|
this.sourceIdentities.set(id, identity);
|
|
84
|
-
if (old
|
|
85
|
-
if (old
|
|
86
|
-
this.
|
|
87
|
-
|
|
88
|
-
resource.state = "pending";
|
|
89
|
-
deletes.push(this.cacheManager.resourceCache.deleteResource(id));
|
|
90
|
-
deletes.push(this.cacheManager.resourceCache.deleteAudioResource(id));
|
|
140
|
+
if (old === identity) continue;
|
|
141
|
+
if (old === void 0) {
|
|
142
|
+
this.sourceRevisions.set(id, (this.sourceRevisions.get(id) ?? 0) + 1);
|
|
143
|
+
continue;
|
|
91
144
|
}
|
|
145
|
+
this.taskManager.cancelTask(id);
|
|
146
|
+
this.invalidateResourceData(id);
|
|
147
|
+
if (this.lastLiveIdentities.get(id) !== identity) this.unconfirmedSources.add(id);
|
|
148
|
+
this.markPending(resource);
|
|
149
|
+
reidentified.push(id);
|
|
150
|
+
}
|
|
151
|
+
for (const [id, identity] of this.sourceIdentities) {
|
|
152
|
+
if (identity) this.lastLiveIdentities.set(id, identity);
|
|
92
153
|
}
|
|
93
|
-
|
|
154
|
+
return reidentified;
|
|
155
|
+
}
|
|
156
|
+
/** Resets a resource to `pending` and reports the change when it is the live model's resource. */
|
|
157
|
+
markPending(resource) {
|
|
158
|
+
if (this.model?.resources.get(resource.id) === resource) {
|
|
159
|
+
this.updateResourceState(resource.id, "pending");
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
resource.state = "pending";
|
|
163
|
+
resource.error = void 0;
|
|
94
164
|
}
|
|
95
165
|
invalidateResourceData(resourceId, task) {
|
|
96
166
|
const hadData = this.cacheManager.mp4IndexCache.has(resourceId) || this.cacheManager.audioSampleCache.has(resourceId) || this.blobCache.has(resourceId);
|
|
@@ -144,17 +214,19 @@ class ResourceLoader {
|
|
|
144
214
|
throwIfBytesMissing(resourceId) {
|
|
145
215
|
throw new ResourceNotCachedError(resourceId);
|
|
146
216
|
}
|
|
147
|
-
async setModel(model) {
|
|
148
|
-
this.model
|
|
149
|
-
|
|
217
|
+
async setModel(model, options) {
|
|
218
|
+
this.refreshResourceIdentities(model);
|
|
219
|
+
const timeUs = Math.max(0, options?.startUs ?? 0);
|
|
220
|
+
const active = (clip) => clip.startUs <= timeUs && timeUs < clip.startUs + clip.durationUs;
|
|
150
221
|
const mainTrack = model.tracks.find(
|
|
151
222
|
(track) => track.kind === "video" && track.id === (model.mainTrackId || "main")
|
|
152
223
|
);
|
|
153
|
-
|
|
224
|
+
const initialClip = mainTrack?.clips?.find(active);
|
|
225
|
+
if (mainTrack && initialClip && hasResourceId(initialClip)) {
|
|
154
226
|
try {
|
|
155
|
-
await this.load(
|
|
227
|
+
await this.load(initialClip.resourceId, {
|
|
156
228
|
isPreload: false,
|
|
157
|
-
clipId:
|
|
229
|
+
clipId: initialClip.id,
|
|
158
230
|
trackId: mainTrack.id
|
|
159
231
|
});
|
|
160
232
|
} catch (error) {
|
|
@@ -166,7 +238,7 @@ class ResourceLoader {
|
|
|
166
238
|
}
|
|
167
239
|
const audioTracksSpeech = model.tracks.filter((t) => t.kind === "audio" && t.id === "speech");
|
|
168
240
|
for (const track of audioTracksSpeech) {
|
|
169
|
-
const speechClip = track.clips
|
|
241
|
+
const speechClip = track.clips.find(active);
|
|
170
242
|
if (!speechClip || !hasResourceId(speechClip)) {
|
|
171
243
|
continue;
|
|
172
244
|
}
|
|
@@ -188,7 +260,7 @@ class ResourceLoader {
|
|
|
188
260
|
}
|
|
189
261
|
const audioTracksBgm = model.tracks.filter((t) => t.kind === "audio" && t.id === "bgm");
|
|
190
262
|
for (const track of audioTracksBgm) {
|
|
191
|
-
const bgmClip = track.clips
|
|
263
|
+
const bgmClip = track.clips.find(active);
|
|
192
264
|
if (!bgmClip || !hasResourceId(bgmClip)) {
|
|
193
265
|
continue;
|
|
194
266
|
}
|
|
@@ -218,7 +290,8 @@ class ResourceLoader {
|
|
|
218
290
|
this.startPreloading();
|
|
219
291
|
}
|
|
220
292
|
}
|
|
221
|
-
|
|
293
|
+
/** Preloads the pending resources that clips use, in timeline order; with `only`, just those ids. */
|
|
294
|
+
startPreloading(only) {
|
|
222
295
|
if (!this.model || !this.isPreloadingEnabled) return;
|
|
223
296
|
const tracks = this.model.tracks.filter(
|
|
224
297
|
(track) => track.kind === "video" || track.kind === "audio"
|
|
@@ -229,6 +302,7 @@ class ResourceLoader {
|
|
|
229
302
|
for (let clipIndex = 0; clipIndex < track.clips.length; clipIndex++) {
|
|
230
303
|
const clip = track.clips[clipIndex];
|
|
231
304
|
if (!clip || !hasResourceId(clip)) continue;
|
|
305
|
+
if (only && !only.has(clip.resourceId)) continue;
|
|
232
306
|
preloadItems.push({
|
|
233
307
|
track,
|
|
234
308
|
clip,
|
|
@@ -264,10 +338,11 @@ class ResourceLoader {
|
|
|
264
338
|
return task;
|
|
265
339
|
}
|
|
266
340
|
processQueue() {
|
|
341
|
+
if (this.disposed) return;
|
|
267
342
|
while (this.taskManager.canProcess) {
|
|
268
343
|
const task = this.taskManager.getNextTask();
|
|
269
344
|
if (!task) break;
|
|
270
|
-
this.startLoad(task);
|
|
345
|
+
this.track(this.startLoad(task), task.controller?.signal);
|
|
271
346
|
}
|
|
272
347
|
}
|
|
273
348
|
/**
|
|
@@ -275,24 +350,19 @@ class ResourceLoader {
|
|
|
275
350
|
*/
|
|
276
351
|
async loadVideoResource(task) {
|
|
277
352
|
const indexReady = this.cacheManager.mp4IndexCache.has(task.resourceId);
|
|
278
|
-
const cached = await this.cacheManager.hasResourceInCache(task.resourceId);
|
|
353
|
+
const cached = !this.unconfirmedSources.has(task.resourceId) && await this.cacheManager.hasResourceInCache(task.resourceId);
|
|
279
354
|
this.assertTaskCurrent(task);
|
|
280
355
|
if (indexReady && cached) {
|
|
281
356
|
return;
|
|
282
357
|
}
|
|
283
358
|
if (cached) {
|
|
284
359
|
try {
|
|
285
|
-
await this.ensureIndexParsed(task.resourceId);
|
|
360
|
+
await this.ensureIndexParsed(task.resourceId, task.controller?.signal);
|
|
286
361
|
this.assertTaskCurrent(task);
|
|
287
362
|
return;
|
|
288
363
|
} catch (error) {
|
|
289
364
|
this.assertTaskCurrent(task);
|
|
290
|
-
|
|
291
|
-
await this.cacheManager.resourceCache.deleteResource(task.resourceId);
|
|
292
|
-
this.assertTaskCurrent(task);
|
|
293
|
-
} else {
|
|
294
|
-
throw error;
|
|
295
|
-
}
|
|
365
|
+
throw error;
|
|
296
366
|
}
|
|
297
367
|
}
|
|
298
368
|
this.invalidateResourceData(task.resourceId, task);
|
|
@@ -306,7 +376,7 @@ class ResourceLoader {
|
|
|
306
376
|
const bytesReady = await this.cacheManager.hasResourceInCache(task.resourceId);
|
|
307
377
|
this.assertTaskCurrent(task);
|
|
308
378
|
if (bytesReady) {
|
|
309
|
-
await this.ensureIndexParsed(task.resourceId);
|
|
379
|
+
await this.ensureIndexParsed(task.resourceId, task.controller?.signal);
|
|
310
380
|
return;
|
|
311
381
|
}
|
|
312
382
|
this.throwIfBytesMissing(task.resourceId);
|
|
@@ -351,10 +421,7 @@ class ResourceLoader {
|
|
|
351
421
|
return;
|
|
352
422
|
} catch (error) {
|
|
353
423
|
this.assertTaskCurrent(task);
|
|
354
|
-
|
|
355
|
-
throw error;
|
|
356
|
-
console.warn(`[ResourceLoader][audio-opfs] ${resourceId} cached file unusable:`, error);
|
|
357
|
-
await this.cacheManager.resourceCache.deleteAudioResource(resourceId);
|
|
424
|
+
throw error;
|
|
358
425
|
}
|
|
359
426
|
}
|
|
360
427
|
this.throwIfBytesMissing(resourceId);
|
|
@@ -387,11 +454,11 @@ class ResourceLoader {
|
|
|
387
454
|
*/
|
|
388
455
|
async startLoad(task) {
|
|
389
456
|
this.taskManager.activateTask(task);
|
|
457
|
+
this.loadOwners.set(task.resourceId, task);
|
|
390
458
|
let loadError;
|
|
391
459
|
try {
|
|
392
460
|
this.updateResourceState(task.resourceId, "loading");
|
|
393
461
|
this.updateLoadStage(task.resourceId, "loading");
|
|
394
|
-
task.controller = new AbortController();
|
|
395
462
|
switch (task.resource.type) {
|
|
396
463
|
case "image":
|
|
397
464
|
await this.ensureImageBlob(task);
|
|
@@ -408,16 +475,15 @@ class ResourceLoader {
|
|
|
408
475
|
break;
|
|
409
476
|
}
|
|
410
477
|
this.assertTaskCurrent(task);
|
|
478
|
+
this.unconfirmedSources.delete(task.resourceId);
|
|
411
479
|
this.updateResourceState(task.resourceId, "ready");
|
|
412
480
|
this.updateLoadStage(task.resourceId, "ready");
|
|
413
481
|
} catch (error) {
|
|
414
482
|
task.error = error;
|
|
415
483
|
loadError = error;
|
|
416
|
-
|
|
417
|
-
this.updateResourceState(task.resourceId, "error", toResourceErrorInfo(error));
|
|
418
|
-
this.updateLoadStage(task.resourceId, "error", toResourceErrorInfo(error));
|
|
419
|
-
}
|
|
484
|
+
this.failLoad(task.resourceId, task.resource.uri, task, error);
|
|
420
485
|
} finally {
|
|
486
|
+
if (this.loadOwners.get(task.resourceId) === task) this.loadOwners.delete(task.resourceId);
|
|
421
487
|
this.taskManager.completeTask(task.resourceId, loadError, task);
|
|
422
488
|
this.processQueue();
|
|
423
489
|
}
|
|
@@ -425,12 +491,12 @@ class ResourceLoader {
|
|
|
425
491
|
/**
|
|
426
492
|
* Ensure MP4 index is parsed for a cached resource
|
|
427
493
|
*/
|
|
428
|
-
async ensureIndexParsed(resourceId) {
|
|
494
|
+
async ensureIndexParsed(resourceId, signal) {
|
|
429
495
|
if (this.cacheManager.mp4IndexCache.has(resourceId)) {
|
|
430
496
|
return;
|
|
431
497
|
}
|
|
432
498
|
while (this.parsingIndexes.has(resourceId)) {
|
|
433
|
-
await
|
|
499
|
+
await this.awaitOwned(this.parsingIndexes.get(resourceId), signal);
|
|
434
500
|
}
|
|
435
501
|
if (this.cacheManager.mp4IndexCache.has(resourceId)) {
|
|
436
502
|
return;
|
|
@@ -439,16 +505,29 @@ class ResourceLoader {
|
|
|
439
505
|
if (!resource) throw new Error(`Resource ${resourceId} not found`);
|
|
440
506
|
const source = { ...resource };
|
|
441
507
|
const sourceRevision = this.sourceRevisions.get(resourceId);
|
|
508
|
+
const parseOwner = new AbortController();
|
|
442
509
|
const parseTask = {
|
|
443
510
|
resourceId,
|
|
444
511
|
resource: source,
|
|
445
512
|
sourceRevision,
|
|
513
|
+
controller: parseOwner,
|
|
446
514
|
bytesLoaded: 0,
|
|
447
515
|
totalBytes: 0,
|
|
448
516
|
startTime: Date.now(),
|
|
449
517
|
isPreload: false
|
|
450
518
|
};
|
|
451
|
-
|
|
519
|
+
let release;
|
|
520
|
+
const settled = new Promise((resolve) => {
|
|
521
|
+
release = resolve;
|
|
522
|
+
});
|
|
523
|
+
const releaseWithOwner = () => {
|
|
524
|
+
parseOwner.abort(signal?.reason);
|
|
525
|
+
if (this.parsingIndexes.get(resourceId) === settled) this.parsingIndexes.delete(resourceId);
|
|
526
|
+
release();
|
|
527
|
+
};
|
|
528
|
+
this.parsingIndexes.set(resourceId, settled);
|
|
529
|
+
signal?.addEventListener("abort", releaseWithOwner, { once: true });
|
|
530
|
+
if (signal?.aborted) releaseWithOwner();
|
|
452
531
|
try {
|
|
453
532
|
const cached = await this.cacheManager.hasResourceInCache(resourceId);
|
|
454
533
|
if (!cached) {
|
|
@@ -459,18 +538,17 @@ class ResourceLoader {
|
|
|
459
538
|
} catch (error) {
|
|
460
539
|
this.assertTaskCurrent(parseTask);
|
|
461
540
|
if (error instanceof EmptyStreamError || error instanceof ResourceCorruptedError || error instanceof MP4MoovNotFoundError) {
|
|
462
|
-
console.warn(
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
} catch (deleteError) {
|
|
468
|
-
console.error(`[ResourceLoader] Failed to clear corrupted cache:`, deleteError);
|
|
469
|
-
}
|
|
541
|
+
console.warn(
|
|
542
|
+
`[ResourceLoader] Corrupted cache detected for ${resourceId}; the parent-owned bytes are kept`
|
|
543
|
+
);
|
|
544
|
+
this.assertTaskCurrent(parseTask);
|
|
545
|
+
this.cacheManager.mp4IndexCache.delete(resourceId);
|
|
470
546
|
}
|
|
471
547
|
throw error;
|
|
472
548
|
} finally {
|
|
473
|
-
|
|
549
|
+
signal?.removeEventListener("abort", releaseWithOwner);
|
|
550
|
+
if (this.parsingIndexes.get(resourceId) === settled) this.parsingIndexes.delete(resourceId);
|
|
551
|
+
release();
|
|
474
552
|
}
|
|
475
553
|
}
|
|
476
554
|
/**
|
|
@@ -590,7 +668,15 @@ class ResourceLoader {
|
|
|
590
668
|
throw error;
|
|
591
669
|
}
|
|
592
670
|
}
|
|
593
|
-
|
|
671
|
+
loadImage(resource, signal) {
|
|
672
|
+
const owner = new AbortController();
|
|
673
|
+
return this.track(
|
|
674
|
+
this.loadImageInternal(resource, signal, owner),
|
|
675
|
+
owner.signal,
|
|
676
|
+
(image) => image.close()
|
|
677
|
+
);
|
|
678
|
+
}
|
|
679
|
+
async loadImageInternal(resource, signal, owner) {
|
|
594
680
|
const existingBlob = this.blobCache.get(resource.id);
|
|
595
681
|
if (existingBlob) {
|
|
596
682
|
const imageBitmap = await createImageBitmapFromBlob(existingBlob);
|
|
@@ -599,12 +685,18 @@ class ResourceLoader {
|
|
|
599
685
|
const task = {
|
|
600
686
|
resourceId: resource.id,
|
|
601
687
|
resource,
|
|
688
|
+
sourceRevision: this.sourceRevisions.get(resource.id),
|
|
602
689
|
bytesLoaded: 0,
|
|
603
690
|
totalBytes: 0,
|
|
604
691
|
startTime: Date.now(),
|
|
605
692
|
isPreload: false,
|
|
606
|
-
controller:
|
|
693
|
+
controller: owner
|
|
607
694
|
};
|
|
695
|
+
const abortWithCaller = () => owner.abort(signal?.reason);
|
|
696
|
+
this.imageControllers.add(owner);
|
|
697
|
+
this.loadOwners.set(resource.id, task);
|
|
698
|
+
if (signal?.aborted) abortWithCaller();
|
|
699
|
+
else signal?.addEventListener("abort", abortWithCaller, { once: true });
|
|
608
700
|
let loadError;
|
|
609
701
|
try {
|
|
610
702
|
this.updateResourceState(resource.id, "loading");
|
|
@@ -612,13 +704,72 @@ class ResourceLoader {
|
|
|
612
704
|
const blob = this.blobCache.get(resource.id);
|
|
613
705
|
if (!blob) throw new Error(`Failed to load image ${resource.id}`);
|
|
614
706
|
const imageBitmap = await createImageBitmapFromBlob(blob);
|
|
707
|
+
this.unconfirmedSources.delete(resource.id);
|
|
615
708
|
this.updateResourceState(resource.id, "ready");
|
|
616
709
|
return imageBitmap;
|
|
617
710
|
} catch (error) {
|
|
618
711
|
loadError = error;
|
|
619
|
-
this.
|
|
712
|
+
this.failLoad(resource.id, resource.uri, task, loadError);
|
|
620
713
|
throw loadError;
|
|
714
|
+
} finally {
|
|
715
|
+
if (this.loadOwners.get(resource.id) === task) this.loadOwners.delete(resource.id);
|
|
716
|
+
this.imageControllers.delete(owner);
|
|
717
|
+
signal?.removeEventListener("abort", abortWithCaller);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
failLoad(resourceId, resourceUri, task, error) {
|
|
721
|
+
if (this.model?.getResource(resourceId)?.uri !== resourceUri) return;
|
|
722
|
+
if (this.loadOwners.get(resourceId) !== task) return;
|
|
723
|
+
if (task.sourceRevision !== void 0 && task.sourceRevision !== this.sourceRevisions.get(resourceId))
|
|
724
|
+
return;
|
|
725
|
+
if (this.disposed || task.controller?.signal.aborted) {
|
|
726
|
+
this.updateResourceState(resourceId, "pending");
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
this.markFailed(resourceId, error);
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* Puts a resource whose bytes could not be read or parsed into `error`. Its index and parsed data are dropped, so nothing reads
|
|
733
|
+
* through them, and its next load (a consumer's retry or `reloadResource`) asks the host through ensureCached before using
|
|
734
|
+
* cached bytes again. The bytes themselves are the host's and are never deleted.
|
|
735
|
+
*/
|
|
736
|
+
markFailed(resourceId, error) {
|
|
737
|
+
this.invalidateResourceData(resourceId);
|
|
738
|
+
this.unconfirmedSources.add(resourceId);
|
|
739
|
+
const info = toResourceErrorInfo(error);
|
|
740
|
+
this.updateResourceState(resourceId, "error", info);
|
|
741
|
+
markReportedToHost(error);
|
|
742
|
+
this.updateLoadStage(resourceId, "error", info);
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Attributes a failed read of a resource's bytes outside a load (e.g. a range read while decoding) to that resource.
|
|
746
|
+
* `isCurrent` says whether the reader still works on the resource's current source; a failure for an older source, a
|
|
747
|
+
* cancellation, or a failure while a load of the resource is running (that load decides the state) changes nothing.
|
|
748
|
+
* Returns true when the resource was put into `error`; the caller still rethrows the error.
|
|
749
|
+
*/
|
|
750
|
+
reportByteFailure(resourceId, error, isCurrent) {
|
|
751
|
+
if (this.disposed || isAbortLike(error) || !isCurrent()) return false;
|
|
752
|
+
if (!this.model?.getResource(resourceId)) return false;
|
|
753
|
+
if (this.taskManager.hasActiveTask(resourceId) || this.loadOwners.has(resourceId)) return false;
|
|
754
|
+
this.markFailed(resourceId, error);
|
|
755
|
+
return true;
|
|
756
|
+
}
|
|
757
|
+
/**
|
|
758
|
+
* Loads a resource again, typically after the host repaired its bytes in response to `resource:state` = `error`. The
|
|
759
|
+
* in-memory index and data are dropped and the host's ensureCached is called before cached bytes are used. A load already in
|
|
760
|
+
* flight for the resource is awaited instead of being replaced. Rejects with the load's error when it fails again.
|
|
761
|
+
*/
|
|
762
|
+
async reloadResource(resourceId, options) {
|
|
763
|
+
if (this.disposed || options?.signal?.aborted)
|
|
764
|
+
throw new DOMException("Resource load aborted", "AbortError");
|
|
765
|
+
const resource = this.model?.getResource(resourceId);
|
|
766
|
+
if (!resource) throw new Error(`Resource ${resourceId} not found`);
|
|
767
|
+
if (!this.taskManager.hasActiveTask(resourceId)) {
|
|
768
|
+
this.invalidateResourceData(resourceId);
|
|
769
|
+
this.unconfirmedSources.add(resourceId);
|
|
770
|
+
this.updateResourceState(resourceId, "pending");
|
|
621
771
|
}
|
|
772
|
+
await this.load(resourceId, { isPreload: false, signal: options?.signal });
|
|
622
773
|
}
|
|
623
774
|
updateResourceState(resourceId, state, error) {
|
|
624
775
|
const resource = this.model?.resources.get(resourceId);
|
|
@@ -637,7 +788,6 @@ class ResourceLoader {
|
|
|
637
788
|
newState: state
|
|
638
789
|
});
|
|
639
790
|
}
|
|
640
|
-
this.onStateChange?.(resourceId, state);
|
|
641
791
|
}
|
|
642
792
|
updateLoadStage(resourceId, state, error) {
|
|
643
793
|
const resource = this.model?.resources.get(resourceId);
|
|
@@ -718,7 +868,10 @@ class ResourceLoader {
|
|
|
718
868
|
const hasIndex = this.cacheManager.mp4IndexCache.has(resourceId);
|
|
719
869
|
if (hasIndex) {
|
|
720
870
|
const sourceRevision = this.sourceRevisions.get(resourceId);
|
|
721
|
-
const cached = await this.
|
|
871
|
+
const cached = await this.awaitOwned(
|
|
872
|
+
this.cacheManager.hasResourceInCache(resourceId),
|
|
873
|
+
options?.signal
|
|
874
|
+
);
|
|
722
875
|
if (this.disposed || options?.signal?.aborted || sourceRevision !== this.sourceRevisions.get(resourceId))
|
|
723
876
|
throw new DOMException("Resource load superseded", "AbortError");
|
|
724
877
|
if (cached) {
|
|
@@ -728,7 +881,10 @@ class ResourceLoader {
|
|
|
728
881
|
this.updateResourceState(resourceId, "pending");
|
|
729
882
|
} else if (resource.type === "audio") {
|
|
730
883
|
const sourceRevision = this.sourceRevisions.get(resourceId);
|
|
731
|
-
const cached = await this.
|
|
884
|
+
const cached = await this.awaitOwned(
|
|
885
|
+
this.cacheManager.resourceCache.hasAudioResource(resourceId),
|
|
886
|
+
options?.signal
|
|
887
|
+
);
|
|
732
888
|
if (this.disposed || options?.signal?.aborted || sourceRevision !== this.sourceRevisions.get(resourceId))
|
|
733
889
|
throw new DOMException("Resource load superseded", "AbortError");
|
|
734
890
|
if (cached && this.cacheManager.audioSampleCache.has(resourceId)) return;
|
|
@@ -812,14 +968,36 @@ class ResourceLoader {
|
|
|
812
968
|
get taskQueue() {
|
|
813
969
|
return this.taskManager.taskQueue;
|
|
814
970
|
}
|
|
815
|
-
dispose
|
|
971
|
+
/** Aborts the image downloads that run outside TaskManager, so drain and dispose do not wait on a download nobody cancels. */
|
|
972
|
+
abortImageLoads() {
|
|
973
|
+
for (const controller of this.imageControllers) {
|
|
974
|
+
controller.abort(new DOMException("Resource loader disposed", "AbortError"));
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
async drain() {
|
|
978
|
+
this.taskManager.clear();
|
|
979
|
+
this.abortImageLoads();
|
|
980
|
+
this.abortReads();
|
|
981
|
+
this.parsingIndexes.clear();
|
|
982
|
+
await Promise.allSettled([...this.running]);
|
|
983
|
+
}
|
|
984
|
+
async dispose() {
|
|
985
|
+
if (this.disposed) return this.disposePromise;
|
|
816
986
|
this.disposed = true;
|
|
817
987
|
this.taskManager.clear();
|
|
988
|
+
this.abortImageLoads();
|
|
989
|
+
this.abortReads();
|
|
818
990
|
this.consumers.clear();
|
|
819
|
-
this.
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
991
|
+
this.disposePromise = (async () => {
|
|
992
|
+
await Promise.allSettled([...this.running]);
|
|
993
|
+
this.blobCache.clear();
|
|
994
|
+
this.parsingIndexes.clear();
|
|
995
|
+
this.sourceIdentities.clear();
|
|
996
|
+
this.sourceRevisions.clear();
|
|
997
|
+
this.lastLiveIdentities.clear();
|
|
998
|
+
this.unconfirmedSources.clear();
|
|
999
|
+
})();
|
|
1000
|
+
return this.disposePromise;
|
|
823
1001
|
}
|
|
824
1002
|
/**
|
|
825
1003
|
* Check if a clip needs cover extraction (first GOP decode)
|
|
@@ -836,6 +1014,9 @@ function isMp4FamilyContainer(bytes) {
|
|
|
836
1014
|
const tag = String.fromCharCode(bytes[4] ?? 0, bytes[5] ?? 0, bytes[6] ?? 0, bytes[7] ?? 0);
|
|
837
1015
|
return tag === "ftyp";
|
|
838
1016
|
}
|
|
1017
|
+
function isAbortLike(error) {
|
|
1018
|
+
return error?.name === "AbortError" || error instanceof ResourceSupersededError;
|
|
1019
|
+
}
|
|
839
1020
|
export {
|
|
840
1021
|
ResourceLoader
|
|
841
1022
|
};
|