@meframe/core 0.7.2 → 0.8.1-preview-frame-gap.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 (83) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/cache/index.d.ts +0 -3
  3. package/dist/cache/index.d.ts.map +1 -1
  4. package/dist/cache/resource/AudioSampleCache.d.ts +3 -3
  5. package/dist/cache/resource/AudioSampleCache.d.ts.map +1 -1
  6. package/dist/cache/resource/AudioSampleCache.js +4 -3
  7. package/dist/cache/resource/AudioSampleCache.js.map +1 -1
  8. package/dist/cache/resource/MP4IndexCache.d.ts.map +1 -1
  9. package/dist/cache/resource/MP4IndexCache.js +23 -13
  10. package/dist/cache/resource/MP4IndexCache.js.map +1 -1
  11. package/dist/index.d.ts +1 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +1 -1
  14. package/dist/index.js.map +1 -1
  15. package/dist/orchestrator/AudioWindowPreparer.d.ts +4 -0
  16. package/dist/orchestrator/AudioWindowPreparer.d.ts.map +1 -1
  17. package/dist/orchestrator/AudioWindowPreparer.js +253 -13
  18. package/dist/orchestrator/AudioWindowPreparer.js.map +1 -1
  19. package/dist/orchestrator/Orchestrator.d.ts.map +1 -1
  20. package/dist/orchestrator/Orchestrator.js +20 -9
  21. package/dist/orchestrator/Orchestrator.js.map +1 -1
  22. package/dist/orchestrator/VideoWindowDecodeSession.d.ts.map +1 -1
  23. package/dist/orchestrator/VideoWindowDecodeSession.js +59 -47
  24. package/dist/orchestrator/VideoWindowDecodeSession.js.map +1 -1
  25. package/dist/orchestrator/aac-window-timeline.d.ts +7 -1
  26. package/dist/orchestrator/aac-window-timeline.d.ts.map +1 -1
  27. package/dist/orchestrator/aac-window-timeline.js +22 -22
  28. package/dist/orchestrator/aac-window-timeline.js.map +1 -1
  29. package/dist/stages/decode/preview-audio-decoder.d.ts +57 -0
  30. package/dist/stages/decode/preview-audio-decoder.d.ts.map +1 -0
  31. package/dist/stages/decode/preview-audio-decoder.js +281 -0
  32. package/dist/stages/decode/preview-audio-decoder.js.map +1 -0
  33. package/dist/stages/demux/MP3FrameParser.d.ts +2 -2
  34. package/dist/stages/demux/MP3FrameParser.d.ts.map +1 -1
  35. package/dist/stages/demux/MP3FrameParser.js +12 -11
  36. package/dist/stages/demux/MP3FrameParser.js.map +1 -1
  37. package/dist/stages/demux/MP4IndexParser.d.ts +2 -2
  38. package/dist/stages/demux/MP4IndexParser.d.ts.map +1 -1
  39. package/dist/stages/demux/MP4IndexParser.js +42 -34
  40. package/dist/stages/demux/MP4IndexParser.js.map +1 -1
  41. package/dist/stages/demux/MP4IndexWorkerClient.d.ts +15 -0
  42. package/dist/stages/demux/MP4IndexWorkerClient.d.ts.map +1 -0
  43. package/dist/stages/demux/MP4IndexWorkerClient.js +257 -0
  44. package/dist/stages/demux/MP4IndexWorkerClient.js.map +1 -0
  45. package/dist/stages/demux/SampleTable.d.ts +85 -0
  46. package/dist/stages/demux/SampleTable.d.ts.map +1 -0
  47. package/dist/stages/demux/SampleTable.js +273 -0
  48. package/dist/stages/demux/SampleTable.js.map +1 -0
  49. package/dist/stages/demux/mp4-index-protocol.d.ts +141 -0
  50. package/dist/stages/demux/mp4-index-protocol.d.ts.map +1 -0
  51. package/dist/stages/demux/mp4-index-protocol.js +108 -0
  52. package/dist/stages/demux/mp4-index-protocol.js.map +1 -0
  53. package/dist/stages/demux/types.d.ts +3 -2
  54. package/dist/stages/demux/types.d.ts.map +1 -1
  55. package/dist/stages/load/ResourceLoader.d.ts +1 -0
  56. package/dist/stages/load/ResourceLoader.d.ts.map +1 -1
  57. package/dist/stages/load/ResourceLoader.js +6 -5
  58. package/dist/stages/load/ResourceLoader.js.map +1 -1
  59. package/dist/stages/load/types.d.ts +2 -0
  60. package/dist/stages/load/types.d.ts.map +1 -1
  61. package/dist/worker/WorkerPool.d.ts +4 -0
  62. package/dist/worker/WorkerPool.d.ts.map +1 -1
  63. package/dist/worker/WorkerPool.js +10 -2
  64. package/dist/worker/WorkerPool.js.map +1 -1
  65. package/dist/worker/types.d.ts +1 -1
  66. package/dist/worker/types.d.ts.map +1 -1
  67. package/dist/worker/types.js.map +1 -1
  68. package/dist/worker/worker-event-whitelist.d.ts.map +1 -1
  69. package/dist/workers/stages/demux/mp4-index.worker.CFcc5GBh.js +8460 -0
  70. package/dist/workers/stages/demux/mp4-index.worker.CFcc5GBh.js.map +1 -0
  71. package/dist/workers/stages/export/export.worker.BG8HA7dY.js.map +1 -1
  72. package/dist/workers/worker-manifest.json +1 -0
  73. package/package.json +1 -1
  74. package/dist/cache/l2/IndexedDBStore.d.ts +0 -73
  75. package/dist/cache/l2/IndexedDBStore.d.ts.map +0 -1
  76. package/dist/cache/l2/L2Cache.d.ts +0 -79
  77. package/dist/cache/l2/L2Cache.d.ts.map +0 -1
  78. package/dist/cache/l2/L2OPFSStore.d.ts +0 -37
  79. package/dist/cache/l2/L2OPFSStore.d.ts.map +0 -1
  80. package/dist/cache/l2/OPFSStore.d.ts +0 -45
  81. package/dist/cache/l2/OPFSStore.d.ts.map +0 -1
  82. package/dist/cache/storage/indexeddb/ChunkRecordStore.d.ts +0 -75
  83. package/dist/cache/storage/indexeddb/ChunkRecordStore.d.ts.map +0 -1
@@ -1,5 +1,6 @@
1
1
  import { MeframeEvent } from "../event/events.js";
2
2
  import { AudioChunkDecoder } from "../stages/decode/AudioChunkDecoder.js";
3
+ import { PersistentPreviewAudioDecoder } from "../stages/decode/preview-audio-decoder.js";
3
4
  import { decodeWavPCM } from "../stages/demux/WAVIndexParser.js";
4
5
  import { hasResourceId, isAudioClip, clipPlaybackRate } from "../model/types.js";
5
6
  import { buildLoopedResourceSegments } from "../utils/loop-utils.js";
@@ -7,7 +8,8 @@ import { extractPlanesFromAudioData } from "../utils/audio-data.js";
7
8
  import { isTerminalMediaResourceError, markReportedToHost } from "../utils/errors.js";
8
9
  import { untilAborted } from "../utils/until-aborted.js";
9
10
  import { binarySearchFirst } from "../utils/binary-search.js";
10
- import { prepareAACTimeline, readIndexedAACWindow, readSampleByteSlice, getAACWindow } from "./aac-window-timeline.js";
11
+ import { sampleDurationAt, sampleTimestampAt, binarySearchFirstIndex, sampleIsKeyframeAt } from "../stages/demux/SampleTable.js";
12
+ import { prepareAACTimeline, readIndexedAACWindow, readSampleByteSlice, getAACWindow, findTimelineBounds } from "./aac-window-timeline.js";
11
13
  class AudioWindowPreparer {
12
14
  activeClips = /* @__PURE__ */ new Set();
13
15
  /** 解码确定失败的 clip(坏包等):本会话不再重试,避免重试风暴;reset 清空 */
@@ -20,7 +22,24 @@ class AudioWindowPreparer {
20
22
  recordIds = /* @__PURE__ */ new WeakMap();
21
23
  nextRecordId = 0;
22
24
  aacTimelines = /* @__PURE__ */ new WeakMap();
25
+ previewDecoders = /* @__PURE__ */ new Map();
26
+ clipDecoderKeys = /* @__PURE__ */ new Map();
23
27
  lifecycle = new AbortController();
28
+ closePreviewDecoders(onlyKey) {
29
+ if (onlyKey !== void 0) {
30
+ const decoder = this.previewDecoders.get(onlyKey);
31
+ if (decoder) {
32
+ this.previewDecoders.delete(onlyKey);
33
+ decoder.close();
34
+ }
35
+ return;
36
+ }
37
+ for (const decoder of this.previewDecoders.values()) {
38
+ decoder.close();
39
+ }
40
+ this.previewDecoders.clear();
41
+ this.clipDecoderKeys.clear();
42
+ }
24
43
  get model() {
25
44
  return this.deps.getCompositionModel();
26
45
  }
@@ -36,6 +55,7 @@ class AudioWindowPreparer {
36
55
  this.lifecycle.abort();
37
56
  this.lifecycle = new AbortController();
38
57
  }
58
+ this.closePreviewDecoders();
39
59
  this.failedDecodeClips.clear();
40
60
  this.activeClips.clear();
41
61
  }
@@ -138,7 +158,7 @@ class AudioWindowPreparer {
138
158
  const run = async () => {
139
159
  signal?.throwIfAborted();
140
160
  this.checkCurrent(source);
141
- const firstSampleTimestamp = source.record.indexed ? source.record.indexed.samples[0]?.timestamp : source.record.samples[0]?.timestamp;
161
+ const firstSampleTimestamp = source.record.indexed ? source.record.indexed.samples.length > 0 ? sampleTimestampAt(source.record.indexed.samples, 0) : void 0 : source.record.samples[0]?.timestamp;
142
162
  const firstUs = source.record.gapless ? 0 : firstSampleTimestamp ?? 0;
143
163
  const readStartUs = Math.max(firstUs, startUs);
144
164
  const readEndUs = Math.min(endUs, source.record.durationUs);
@@ -290,6 +310,20 @@ class AudioWindowPreparer {
290
310
  return;
291
311
  }
292
312
  this.activeClips.delete(clipId);
313
+ const cacheKey = this.clipDecoderKeys.get(clipId);
314
+ if (cacheKey !== void 0) {
315
+ this.clipDecoderKeys.delete(clipId);
316
+ let stillInUse = false;
317
+ for (const otherKey of this.clipDecoderKeys.values()) {
318
+ if (otherKey === cacheKey) {
319
+ stillInUse = true;
320
+ break;
321
+ }
322
+ }
323
+ if (!stillInUse) {
324
+ this.closePreviewDecoders(cacheKey);
325
+ }
326
+ }
293
327
  this.deps.cacheManager.clearClipAudioData(clipId);
294
328
  }
295
329
  /**
@@ -453,6 +487,11 @@ class AudioWindowPreparer {
453
487
  }
454
488
  return;
455
489
  }
490
+ if (!strictMode && source) {
491
+ const owner = signal ? AbortSignal.any([this.lifecycle.signal, signal]) : this.lifecycle.signal;
492
+ await this.decodePreviewAudioWindow(clipId, clip, audioRecord, source, startUs, endUs, owner);
493
+ return;
494
+ }
456
495
  if (audioRecord.indexed) {
457
496
  const owner = signal ? AbortSignal.any([this.lifecycle.signal, signal]) : this.lifecycle.signal;
458
497
  let windowChunks2;
@@ -501,11 +540,10 @@ class AudioWindowPreparer {
501
540
  decodeStart,
502
541
  last,
503
542
  (idx, bytes) => {
504
- const s = idxSamples[idx];
505
543
  built[idx - decodeStart] = new EncodedAudioChunk({
506
544
  type: "key",
507
- timestamp: Math.round(s.timestamp),
508
- duration: Math.max(0, Math.round(s.duration)),
545
+ timestamp: Math.round(sampleTimestampAt(idxSamples, idx)),
546
+ duration: Math.max(0, Math.round(sampleDurationAt(idxSamples, idx))),
509
547
  data: bytes
510
548
  });
511
549
  },
@@ -524,9 +562,12 @@ class AudioWindowPreparer {
524
562
  };
525
563
  } else {
526
564
  const idxSamples = audioRecord.indexed.samples;
527
- const first = binarySearchFirst(idxSamples, (s) => s.timestamp + s.duration > startUs);
565
+ const first = binarySearchFirstIndex(
566
+ idxSamples.length,
567
+ (i) => sampleTimestampAt(idxSamples, i) + sampleDurationAt(idxSamples, i) > startUs
568
+ );
528
569
  let last = first;
529
- while (last < idxSamples.length && idxSamples[last].timestamp < endUs) last++;
570
+ while (last < idxSamples.length && sampleTimestampAt(idxSamples, last) < endUs) last++;
530
571
  if (first >= last) {
531
572
  windowChunks2 = [];
532
573
  } else {
@@ -539,11 +580,10 @@ class AudioWindowPreparer {
539
580
  decodeStart,
540
581
  last,
541
582
  (idx, bytes) => {
542
- const s = idxSamples[idx];
543
583
  built[idx - decodeStart] = new EncodedAudioChunk({
544
- type: s.isKeyframe ? "key" : "delta",
545
- timestamp: Math.round(s.timestamp),
546
- duration: Math.max(0, Math.round(s.duration)),
584
+ type: sampleIsKeyframeAt(idxSamples, idx) ? "key" : "delta",
585
+ timestamp: Math.round(sampleTimestampAt(idxSamples, idx)),
586
+ duration: Math.max(0, Math.round(sampleDurationAt(idxSamples, idx))),
547
587
  data: bytes
548
588
  });
549
589
  },
@@ -556,8 +596,8 @@ class AudioWindowPreparer {
556
596
  trim2 = {
557
597
  skipSamples: 0,
558
598
  sampleRate,
559
- physicalStart: toFrames(idxSamples[decodeStart].timestamp),
560
- validStart: toFrames(idxSamples[first].timestamp),
599
+ physicalStart: toFrames(sampleTimestampAt(idxSamples, decodeStart)),
600
+ validStart: toFrames(sampleTimestampAt(idxSamples, first)),
561
601
  validEnd: Number.POSITIVE_INFINITY
562
602
  };
563
603
  }
@@ -674,6 +714,206 @@ class AudioWindowPreparer {
674
714
  signal
675
715
  );
676
716
  }
717
+ async decodePreviewAudioWindow(clipId, clip, audioRecord, source, startUs, endUs, owner) {
718
+ const indexed = audioRecord.indexed;
719
+ const totalPackets = indexed ? indexed.samples.length : audioRecord.samples.length;
720
+ if (totalPackets === 0) return;
721
+ const timestampAt = indexed ? (i) => sampleTimestampAt(indexed.samples, i) : (i) => audioRecord.samples[i].timestamp;
722
+ const durationAt = indexed ? (i) => sampleDurationAt(indexed.samples, i) : (i) => audioRecord.samples[i].duration ?? 0;
723
+ let first;
724
+ let last;
725
+ let prerollStart;
726
+ let trim;
727
+ let shiftUs = 0;
728
+ const isAAC = audioRecord.metadata.codec.startsWith("mp4a");
729
+ if (isAAC) {
730
+ const bounds = findTimelineBounds(
731
+ totalPackets,
732
+ timestampAt,
733
+ durationAt,
734
+ source.aacTimeline,
735
+ startUs,
736
+ endUs
737
+ );
738
+ first = bounds.first;
739
+ last = bounds.last;
740
+ shiftUs = bounds.shiftUs;
741
+ if (first === -1) return;
742
+ prerollStart = Math.max(0, first - 2);
743
+ const { sampleRate } = audioRecord.metadata;
744
+ const toFrames = (us) => Math.round(us * sampleRate / 1e6);
745
+ const firstTs = Math.round(timestampAt(first) - (first === 0 ? 0 : shiftUs));
746
+ const prerollTs = Math.round(timestampAt(prerollStart) - (prerollStart === 0 ? 0 : shiftUs));
747
+ trim = {
748
+ skipSamples: 0,
749
+ sampleRate,
750
+ physicalStartFrame: toFrames(prerollTs),
751
+ validStartFrame: toFrames(firstTs),
752
+ validEndFrame: Number.POSITIVE_INFINITY,
753
+ rebaseTimestamps: false
754
+ };
755
+ } else if (audioRecord.gapless) {
756
+ const { skipSamples, trimEndSamples, samplesPerFrame } = audioRecord.gapless;
757
+ const sampleRate = audioRecord.metadata.sampleRate;
758
+ const validStart = Math.max(
759
+ skipSamples,
760
+ Math.ceil(startUs * sampleRate / 1e6) + skipSamples
761
+ );
762
+ const validEnd = Math.min(
763
+ totalPackets * samplesPerFrame - trimEndSamples,
764
+ Math.ceil(endUs * sampleRate / 1e6) + skipSamples
765
+ );
766
+ if (validEnd <= validStart) return;
767
+ first = Math.floor(validStart / samplesPerFrame);
768
+ last = Math.min(totalPackets, Math.ceil(validEnd / samplesPerFrame));
769
+ prerollStart = indexed ? this.indexedMp3PrerollStart(indexed, first) : this.mp3PrerollStart(audioRecord, first);
770
+ trim = {
771
+ skipSamples,
772
+ sampleRate,
773
+ physicalStartFrame: prerollStart * samplesPerFrame,
774
+ validStartFrame: Math.max(skipSamples, first * samplesPerFrame),
775
+ // Keep lookahead packets emitted & cached cleanly; only trim true trailing encoder padding.
776
+ validEndFrame: Math.max(0, totalPackets * samplesPerFrame - trimEndSamples),
777
+ rebaseTimestamps: true
778
+ };
779
+ } else {
780
+ first = binarySearchFirstIndex(totalPackets, (i) => timestampAt(i) + durationAt(i) > startUs);
781
+ last = first;
782
+ while (last < totalPackets && timestampAt(last) < endUs) last++;
783
+ if (first >= last) return;
784
+ const isMp3 = audioRecord.metadata.codec === "mp3";
785
+ prerollStart = isMp3 && first > 0 ? indexed ? this.indexedMp3PrerollStart(indexed, first) : this.mp3PrerollStart(audioRecord, first) : first;
786
+ const { sampleRate } = audioRecord.metadata;
787
+ const toFrames = (us) => Math.round(us * sampleRate / 1e6);
788
+ trim = {
789
+ skipSamples: 0,
790
+ sampleRate,
791
+ physicalStartFrame: toFrames(timestampAt(prerollStart)),
792
+ validStartFrame: toFrames(timestampAt(first)),
793
+ validEndFrame: Number.POSITIVE_INFINITY,
794
+ rebaseTimestamps: isMp3 && prerollStart < first
795
+ };
796
+ }
797
+ let byteReadFailed = false;
798
+ const timeline = isAAC ? source.aacTimeline : void 0;
799
+ const packetSource = {
800
+ totalPackets,
801
+ loadChunks: async (from, to, signal) => {
802
+ if (!indexed) {
803
+ const out = [];
804
+ for (let i = from; i < to; i++) {
805
+ const sample = audioRecord.samples[i];
806
+ if (!isAAC && !audioRecord.gapless && i >= first && sample.timestamp + (sample.duration ?? 0) <= startUs) {
807
+ continue;
808
+ }
809
+ const timestamp = isAAC ? Math.round(sample.timestamp - (i === 0 ? 0 : shiftUs)) : sample.timestamp;
810
+ const duration = isAAC && i === 0 && timeline ? timeline.firstDurationUs : sample.duration ?? 0;
811
+ const data = new Uint8Array(sample.byteLength);
812
+ sample.copyTo(data);
813
+ out.push(
814
+ new EncodedAudioChunk({
815
+ type: sample.type,
816
+ timestamp,
817
+ duration,
818
+ data
819
+ })
820
+ );
821
+ }
822
+ return out;
823
+ }
824
+ const idxSamples = indexed.samples;
825
+ const built = new Array(to - from);
826
+ try {
827
+ await readSampleByteSlice(
828
+ idxSamples,
829
+ indexed.readRange,
830
+ from,
831
+ to,
832
+ (idx, bytes) => {
833
+ const rawTs = sampleTimestampAt(idxSamples, idx);
834
+ const timestamp = isAAC ? Math.round(rawTs - (idx === 0 ? 0 : shiftUs)) : Math.round(rawTs);
835
+ const duration = Math.max(
836
+ 0,
837
+ Math.round(
838
+ isAAC && idx === 0 && timeline ? timeline.firstDurationUs : sampleDurationAt(idxSamples, idx)
839
+ )
840
+ );
841
+ built[idx - from] = new EncodedAudioChunk({
842
+ type: audioRecord.gapless ? "key" : sampleIsKeyframeAt(idxSamples, idx) ? "key" : "delta",
843
+ timestamp,
844
+ duration,
845
+ data: bytes
846
+ });
847
+ },
848
+ signal ?? owner
849
+ );
850
+ } catch (error) {
851
+ if (owner.aborted) {
852
+ throw owner.reason ?? new DOMException("Audio decode cancelled", "AbortError");
853
+ }
854
+ byteReadFailed = true;
855
+ this.deps.resourceLoader.reportByteFailure(
856
+ audioRecord.resourceId,
857
+ error,
858
+ () => !owner.aborted && this.deps.cacheManager.audioSampleCache.get(audioRecord.resourceId) === audioRecord
859
+ );
860
+ throw error;
861
+ }
862
+ return built.filter(Boolean);
863
+ }
864
+ };
865
+ let decoder = this.previewDecoders.get(source.cacheKey);
866
+ if (!decoder) {
867
+ decoder = new PersistentPreviewAudioDecoder(audioRecord.metadata);
868
+ this.previewDecoders.set(source.cacheKey, decoder);
869
+ }
870
+ this.clipDecoderKeys.set(clipId, source.cacheKey);
871
+ try {
872
+ await decoder.decodeWindow({
873
+ firstPacket: first,
874
+ lastPacket: last,
875
+ prerollStart,
876
+ startUs,
877
+ endUs,
878
+ trim,
879
+ packetSource,
880
+ isRangeCovered: (fromUs, toUs) => {
881
+ const threshold = Math.max(
882
+ 0.5,
883
+ 1 - 1e6 / audioRecord.metadata.sampleRate / Math.max(1, toUs - fromUs)
884
+ );
885
+ return this.deps.cacheManager.getAudioRangeCoverage(
886
+ source.cacheKey,
887
+ fromUs,
888
+ toUs,
889
+ threshold
890
+ ).covered;
891
+ },
892
+ onDecodedAudioData: (trimmed) => {
893
+ this.checkCurrent(source);
894
+ this.deps.cacheManager.putClipAudioData(
895
+ source.cacheKey,
896
+ trimmed,
897
+ clip.durationUs,
898
+ clip.startUs + trimmed.timestamp
899
+ );
900
+ },
901
+ sliceAudioData,
902
+ signal: owner
903
+ });
904
+ } catch (error) {
905
+ decoder.close();
906
+ if (this.previewDecoders.get(source.cacheKey) === decoder) {
907
+ this.previewDecoders.delete(source.cacheKey);
908
+ }
909
+ if (owner.aborted) {
910
+ throw owner.reason ?? new DOMException("Audio decode cancelled", "AbortError");
911
+ }
912
+ this.checkCurrent(source);
913
+ if (byteReadFailed || error instanceof RangeError) throw error;
914
+ await this.handleDecodeFailure(clipId, error, false);
915
+ }
916
+ }
677
917
  async decodeWavWindow(clipId, record, startUs, endUs, clipDurationUs, clipStartUs, source, signal) {
678
918
  const index = record.pcm;
679
919
  const first = Math.max(0, Math.floor(startUs * index.sampleRate / 1e6));