mattebox 0.13.0 → 0.14.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 (54) hide show
  1. package/dist/cdn/mattebox.dash-drm.min.js +3 -3
  2. package/dist/cdn/mattebox.dash.min.js +3 -3
  3. package/dist/cdn/mattebox.dual-drm.min.js +3 -3
  4. package/dist/cdn/mattebox.dual-ts-drm.min.js +3 -3
  5. package/dist/cdn/mattebox.dual-ts.min.js +3 -3
  6. package/dist/cdn/mattebox.dual.min.js +3 -3
  7. package/dist/cdn/mattebox.hls-drm.min.js +3 -3
  8. package/dist/cdn/mattebox.hls-ts-drm.min.js +3 -3
  9. package/dist/cdn/mattebox.hls-ts.min.js +3 -3
  10. package/dist/cdn/mattebox.hls.min.js +3 -3
  11. package/dist/cdn/mattebox.kernel.min.js +1 -1
  12. package/dist/cdn/mattebox.min.js +5 -5
  13. package/dist/es2015/index.js +37 -5
  14. package/dist/es2015/kernel/dry-run.js +179 -0
  15. package/dist/es2015/kernel/lifecycle.js +6 -2
  16. package/dist/es2015/kernel/reducer.js +15 -2
  17. package/dist/es2015/kernel/transport.js +88 -34
  18. package/dist/es2015/presets/full/index.js +3 -5
  19. package/dist/es2015/presets/tiers.js +1 -2
  20. package/dist/es2015/protocols/dash-cmaf/periods-runtime.js +1 -1
  21. package/dist/es2015/stages/aes-128/index.js +1 -1
  22. package/dist/es2015/stages/chapters/index.js +1 -1
  23. package/dist/es2015/stages/thumbnails/index.js +1 -1
  24. package/dist/es2015/stages/trick-play/index.js +1 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +27 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/kernel/dry-run.d.ts +27 -0
  29. package/dist/kernel/dry-run.d.ts.map +1 -0
  30. package/dist/kernel/dry-run.js +198 -0
  31. package/dist/kernel/dry-run.js.map +1 -0
  32. package/dist/kernel/lifecycle.d.ts +3 -1
  33. package/dist/kernel/lifecycle.d.ts.map +1 -1
  34. package/dist/kernel/lifecycle.js +4 -0
  35. package/dist/kernel/lifecycle.js.map +1 -1
  36. package/dist/kernel/reducer.d.ts.map +1 -1
  37. package/dist/kernel/reducer.js +22 -2
  38. package/dist/kernel/reducer.js.map +1 -1
  39. package/dist/kernel/transport.d.ts +34 -5
  40. package/dist/kernel/transport.d.ts.map +1 -1
  41. package/dist/kernel/transport.js +88 -30
  42. package/dist/kernel/transport.js.map +1 -1
  43. package/dist/presets/full/index.d.ts.map +1 -1
  44. package/dist/presets/full/index.js +3 -5
  45. package/dist/presets/full/index.js.map +1 -1
  46. package/dist/presets/tiers.d.ts +1 -2
  47. package/dist/presets/tiers.d.ts.map +1 -1
  48. package/dist/presets/tiers.js +1 -2
  49. package/dist/presets/tiers.js.map +1 -1
  50. package/dist/types/facade.d.ts +15 -3
  51. package/dist/types/facade.d.ts.map +1 -1
  52. package/dist/types/messages.d.ts +15 -0
  53. package/dist/types/messages.d.ts.map +1 -1
  54. package/package.json +1 -1
@@ -2,16 +2,17 @@ import _objectSpread2 from "./_virtual/_@oxc-project_runtime@0.152.0/helpers/esm
2
2
  import { exportTrace, replay } from "./kernel/trace.js";
3
3
  import { createBus } from "./kernel/bus.js";
4
4
  import { createEffectRunner } from "./kernel/effects.js";
5
+ import _asyncToGenerator from "./_virtual/_@oxc-project_runtime@0.152.0/helpers/esm/asyncToGenerator.js";
6
+ import { createSegmentPreparer } from "./kernel/prepare.js";
5
7
  import { cueFormat, normalizeMimeType } from "./kernel/mime.js";
6
8
  import { findTrackSite, isTrick } from "./kernel/presentation.js";
7
9
  import { activeAudio, availableGroups, canSwitchTo, createArbiter, exclusionsOf } from "./kernel/rendition-select.js";
8
10
  import { createMseSink } from "./kernel/sinks/mse-sink.js";
9
11
  import { createReducer, initialState, resolveConfig } from "./kernel/reducer.js";
12
+ import { dryRun } from "./kernel/dry-run.js";
10
13
  import { createLifecycle } from "./kernel/lifecycle.js";
11
14
  import { compose } from "./kernel/loader.js";
12
15
  import { createMseController, decodable } from "./kernel/mse.js";
13
- import _asyncToGenerator from "./_virtual/_@oxc-project_runtime@0.152.0/helpers/esm/asyncToGenerator.js";
14
- import { createSegmentPreparer } from "./kernel/prepare.js";
15
16
  import { createTrackRegistry } from "./kernel/track-registry.js";
16
17
  import { createTransport } from "./kernel/transport.js";
17
18
  import { isAudioDescription, isEnhancedSpeech, isOriginal, isSdh } from "./kernel/characteristics.js";
@@ -159,6 +160,8 @@ function mattebox(options) {
159
160
  decodable
160
161
  };
161
162
  let lastError = null;
163
+ /** What each deep preload started from, by URL, for the load that follows it. */
164
+ const seeds = /* @__PURE__ */ new Map();
162
165
  function accepts(mimeType) {
163
166
  return composition.manifestTypes.has(normalizeMimeType(mimeType));
164
167
  }
@@ -311,25 +314,54 @@ function mattebox(options) {
311
314
  lastError = null;
312
315
  const { phase } = bus.getState().lifecycle;
313
316
  if (phase !== "idle" && phase !== "attaching") bus.dispatch({ type: "UNLOAD" });
314
- bus.dispatch(_objectSpread2({
317
+ const kept = seeds.get(absolute);
318
+ seeds.delete(absolute);
319
+ bus.dispatch(_objectSpread2(_objectSpread2({
315
320
  type: "LOAD",
316
321
  url: absolute
317
- }, (loadOptions === null || loadOptions === void 0 ? void 0 : loadOptions.mimeType) !== void 0 ? { mimeType: loadOptions.mimeType } : {}));
322
+ }, (loadOptions === null || loadOptions === void 0 ? void 0 : loadOptions.mimeType) !== void 0 ? { mimeType: loadOptions.mimeType } : {}), kept !== void 0 && performance.now() < kept.expires ? { seed: kept.seed } : {}));
318
323
  },
319
324
  preload(url, loadOptions) {
320
325
  return _asyncToGenerator(function* () {
326
+ var _loadOptions$keepSeco, _loadOptions$segments;
321
327
  const absolute = absolutize(url);
322
328
  if ((loadOptions === null || loadOptions === void 0 ? void 0 : loadOptions.mimeType) !== void 0 && !accepts(loadOptions.mimeType)) throw Object.assign(/* @__PURE__ */ new Error(`no protocol reads ${loadOptions.mimeType}`), {
323
329
  category: "manifest",
324
330
  code: "MANIFEST_UNSUPPORTED"
325
331
  });
332
+ const keepMs = ((_loadOptions$keepSeco = loadOptions === null || loadOptions === void 0 ? void 0 : loadOptions.keepSeconds) !== null && _loadOptions$keepSeco !== void 0 ? _loadOptions$keepSeco : 30) * 1e3;
333
+ const element = lifecycle.element();
334
+ const segments = (_loadOptions$segments = loadOptions === null || loadOptions === void 0 ? void 0 : loadOptions.segments) !== null && _loadOptions$segments !== void 0 ? _loadOptions$segments : 0;
335
+ if (segments > 0 && element !== null) {
336
+ const state = bus.getState();
337
+ const seed = {
338
+ throughputEwma: state.stats.throughputEwma,
339
+ throughputFastEwma: state.stats.throughputFastEwma,
340
+ constraints: [...state.quality.constraints].filter(([, constraint]) => constraint.excludeIds === void 0 && constraint.filter === void 0)
341
+ };
342
+ seeds.set(absolute, {
343
+ seed,
344
+ expires: performance.now() + keepMs
345
+ });
346
+ return dryRun({
347
+ slices: lifecycle.slices(),
348
+ config,
349
+ hooks,
350
+ transport,
351
+ element,
352
+ seed
353
+ }, absolute, _objectSpread2(_objectSpread2({}, (loadOptions === null || loadOptions === void 0 ? void 0 : loadOptions.mimeType) !== void 0 ? { mimeType: loadOptions.mimeType } : {}), {}, {
354
+ segments,
355
+ keepMs
356
+ }));
357
+ }
326
358
  const response = yield transport.request(absolute, { method: "GET" });
327
359
  if (!response.ok) throw Object.assign(/* @__PURE__ */ new Error(`preload ${absolute}: HTTP ${response.status}`), {
328
360
  category: "network",
329
361
  code: "NETWORK_HTTP_STATUS",
330
362
  status: response.status
331
363
  });
332
- transport.prime(absolute, yield response.arrayBuffer(), response.headers);
364
+ transport.prime(absolute, yield response.arrayBuffer(), response.headers, { keepMs });
333
365
  })();
334
366
  },
335
367
  unload() {
@@ -0,0 +1,179 @@
1
+ import _objectSpread2 from "../_virtual/_@oxc-project_runtime@0.152.0/helpers/esm/objectSpread2.js";
2
+ import { createBus } from "./bus.js";
3
+ import { createEffectRunner } from "./effects.js";
4
+ import { createSegmentPreparer } from "./prepare.js";
5
+ import { createReducer, initialState } from "./reducer.js";
6
+ //#region src/kernel/dry-run.ts
7
+ /** The whole run ends here, whatever it has fetched: a preload must not outlive its use. */
8
+ const LIMIT_MS = 1e4;
9
+ /** Ranges this close are one range, as a SourceBuffer reports them. */
10
+ const TOUCHING = .1;
11
+ function merge(ranges, added) {
12
+ const sorted = [...ranges, added].sort((a, b) => a.start - b.start);
13
+ const out = [];
14
+ for (const range of sorted) {
15
+ const last = out[out.length - 1];
16
+ if (last !== void 0 && range.start <= last.end + TOUCHING) out[out.length - 1] = {
17
+ start: last.start,
18
+ end: Math.max(last.end, range.end)
19
+ };
20
+ else out.push(range);
21
+ }
22
+ return out;
23
+ }
24
+ /**
25
+ * Resolves when each media track has its segments, when the loop has
26
+ * nothing left to fetch, or at the time limit. Rejects when the manifest
27
+ * fails.
28
+ */
29
+ function dryRun(deps, url, options) {
30
+ return new Promise((resolve, reject) => {
31
+ const bus = createBus({
32
+ reducer: createReducer(deps.slices, deps.config, deps.hooks),
33
+ initial: initialState(deps.config),
34
+ traceCapacity: 0
35
+ });
36
+ const runner = createEffectRunner();
37
+ bus.setEffectSink((effects) => runner.run(effects));
38
+ let stopped = false;
39
+ let failure = null;
40
+ const timers = /* @__PURE__ */ new Set();
41
+ /** Per buffer: the segments loaded and not yet appended, null for an init. */
42
+ const loaded = /* @__PURE__ */ new Map();
43
+ const ranges = /* @__PURE__ */ new Map();
44
+ /** Media segments loaded, per buffer. */
45
+ const counts = /* @__PURE__ */ new Map();
46
+ /**
47
+ * The requests still out, by token. The reducer's own record does not
48
+ * hold a slice's requests, a media playlist among them.
49
+ */
50
+ const open = /* @__PURE__ */ new Set();
51
+ const inflight = (token) => bus.getState().scheduling.inflight.get(token);
52
+ function finish(error) {
53
+ if (stopped) return;
54
+ stopped = true;
55
+ clearTimeout(limit);
56
+ for (const id of timers) clearTimeout(id);
57
+ channel.close();
58
+ if (error === void 0) resolve();
59
+ else reject(error);
60
+ }
61
+ /** Whether the run has what it came for, or nothing left to do. */
62
+ function check() {
63
+ if (stopped) return;
64
+ const state = bus.getState();
65
+ if (state.lifecycle.phase === "error") {
66
+ var _failure;
67
+ finish((_failure = failure) !== null && _failure !== void 0 ? _failure : /* @__PURE__ */ new Error(`preload ${url} failed`));
68
+ return;
69
+ }
70
+ if (state.presentation === null) return;
71
+ const tracks = ["video", "audio"].filter((type) => state.tracks.active.has(type));
72
+ const full = tracks.length > 0 && tracks.every((type) => {
73
+ var _counts$get;
74
+ return ((_counts$get = counts.get(`sb:${type}`)) !== null && _counts$get !== void 0 ? _counts$get : 0) >= options.segments;
75
+ });
76
+ const idle = open.size === 0 && timers.size === 0;
77
+ if (full || idle) finish();
78
+ }
79
+ /** A message from outside the loop: a response, a timer. The loop is at rest when it returns. */
80
+ function feed(msg) {
81
+ if (stopped) return;
82
+ bus.route(msg);
83
+ check();
84
+ }
85
+ const probe = deps.hooks.timeProbe;
86
+ const prepare = createSegmentPreparer({
87
+ transforms: () => [],
88
+ timeProbe: () => probe == null ? null : (data, meta) => probe(data, _objectSpread2(_objectSpread2({}, meta), {}, { renditionId: `preload:${meta.renditionId}` })),
89
+ inflight,
90
+ absorb: (fact) => {
91
+ if (stopped) return;
92
+ if (fact.type === "SEGMENT_LOADED" && fact.token !== void 0) {
93
+ open.delete(fact.token);
94
+ const request = inflight(fact.token);
95
+ if ((request === null || request === void 0 ? void 0 : request.sbId) !== void 0) {
96
+ var _request$segmentStart, _loaded$get, _request$segmentDurat, _counts$get2;
97
+ const media = request.seq >= 0;
98
+ const start = (_request$segmentStart = request.segmentStart) !== null && _request$segmentStart !== void 0 ? _request$segmentStart : 0;
99
+ const queue = (_loaded$get = loaded.get(request.sbId)) !== null && _loaded$get !== void 0 ? _loaded$get : [];
100
+ queue.push(media ? {
101
+ start,
102
+ end: start + ((_request$segmentDurat = request.segmentDuration) !== null && _request$segmentDurat !== void 0 ? _request$segmentDurat : 0)
103
+ } : null);
104
+ loaded.set(request.sbId, queue);
105
+ if (media) counts.set(request.sbId, ((_counts$get2 = counts.get(request.sbId)) !== null && _counts$get2 !== void 0 ? _counts$get2 : 0) + 1);
106
+ }
107
+ }
108
+ feed(fact);
109
+ }
110
+ });
111
+ const channel = deps.transport.channel({
112
+ absorb: prepare,
113
+ inflight,
114
+ keepMs: options.keepMs,
115
+ failed: (token) => open.delete(token)
116
+ });
117
+ runner.register("fetch", (effect) => {
118
+ var _bus$getState$present, _counts$get3;
119
+ if (stopped) return void 0;
120
+ const request = inflight(effect.token);
121
+ if (((_bus$getState$present = bus.getState().presentation) === null || _bus$getState$present === void 0 ? void 0 : _bus$getState$present.isLive) === true && (request === null || request === void 0 ? void 0 : request.sbId) !== void 0 && request.seq >= 0) {
122
+ finish();
123
+ return;
124
+ }
125
+ if ((request === null || request === void 0 ? void 0 : request.sbId) !== void 0 && request.seq >= 0 && ((_counts$get3 = counts.get(request.sbId)) !== null && _counts$get3 !== void 0 ? _counts$get3 : 0) >= options.segments) return;
126
+ open.add(effect.token);
127
+ const cancel = channel.fetch(effect);
128
+ return () => {
129
+ open.delete(effect.token);
130
+ cancel();
131
+ };
132
+ });
133
+ runner.register("schedule", (effect) => {
134
+ if (stopped) return void 0;
135
+ const id = setTimeout(() => {
136
+ timers.delete(id);
137
+ feed(effect.then.type === "TICK" ? _objectSpread2(_objectSpread2({}, effect.then), {}, { wallClock: Date.now() / 1e3 }) : effect.then);
138
+ }, effect.delayMs);
139
+ timers.add(id);
140
+ return () => {
141
+ clearTimeout(id);
142
+ timers.delete(id);
143
+ };
144
+ });
145
+ runner.register("createSourceBuffer", (effect) => {
146
+ bus.absorb({
147
+ type: "SOURCEBUFFER_CREATED",
148
+ sbId: effect.sbId,
149
+ codecs: effect.codecs
150
+ });
151
+ });
152
+ runner.register("append", (effect) => {
153
+ var _loaded$get2, _ranges$get, _ranges$get2;
154
+ const segment = (_loaded$get2 = loaded.get(effect.sbId)) === null || _loaded$get2 === void 0 ? void 0 : _loaded$get2.shift();
155
+ if (segment != null) ranges.set(effect.sbId, merge((_ranges$get = ranges.get(effect.sbId)) !== null && _ranges$get !== void 0 ? _ranges$get : [], segment));
156
+ bus.absorb({
157
+ type: "SOURCEBUFFER_UPDATEEND",
158
+ sbId: effect.sbId,
159
+ ranges: (_ranges$get2 = ranges.get(effect.sbId)) !== null && _ranges$get2 !== void 0 ? _ranges$get2 : []
160
+ });
161
+ });
162
+ runner.register("emit", (effect) => {
163
+ const payload = effect.payload;
164
+ if (effect.event === "error" && (payload === null || payload === void 0 ? void 0 : payload.fatal) === true) failure = payload;
165
+ });
166
+ const limit = setTimeout(finish, LIMIT_MS);
167
+ bus.dispatch({
168
+ type: "ATTACH",
169
+ element: deps.element
170
+ });
171
+ feed(_objectSpread2({
172
+ type: "LOAD",
173
+ url,
174
+ seed: deps.seed
175
+ }, options.mimeType !== void 0 ? { mimeType: options.mimeType } : {}));
176
+ });
177
+ }
178
+ //#endregion
179
+ export { dryRun };
@@ -1,5 +1,5 @@
1
- import { createStageContext } from "./context.js";
2
1
  import { createReducer } from "./reducer.js";
2
+ import { createStageContext } from "./context.js";
3
3
  import { createPlaybackWatchdog } from "./watchdog.js";
4
4
  //#region src/kernel/lifecycle.ts
5
5
  function snapshot(ranges) {
@@ -13,6 +13,7 @@ function snapshot(ranges) {
13
13
  function createLifecycle(deps) {
14
14
  let element = null;
15
15
  let teardowns = [];
16
+ let installed = [];
16
17
  let stopWatchdog = null;
17
18
  const elementListeners = [];
18
19
  function listen(el, type, fn) {
@@ -46,6 +47,7 @@ function createLifecycle(deps) {
46
47
  }
47
48
  deps.bus.setReducer(createReducer(slices, deps.config, deps.hooks));
48
49
  teardowns = stageTeardowns;
50
+ installed = slices;
49
51
  deps.bus.dispatch({
50
52
  type: "ATTACH",
51
53
  element: el
@@ -108,6 +110,7 @@ function createLifecycle(deps) {
108
110
  stopWatchdog = null;
109
111
  for (const fn of [...teardowns].reverse()) fn();
110
112
  teardowns = [];
113
+ installed = [];
111
114
  deps.mse.detach();
112
115
  for (const { type, fn } of elementListeners) el.removeEventListener(type, fn);
113
116
  elementListeners.length = 0;
@@ -117,7 +120,8 @@ function createLifecycle(deps) {
117
120
  return {
118
121
  attach,
119
122
  detach,
120
- element: () => element
123
+ element: () => element,
124
+ slices: () => installed
121
125
  };
122
126
  }
123
127
  //#endregion
@@ -263,7 +263,12 @@ function reduceCommand(state, msg, cfg, hooks) {
263
263
  return [_objectSpread2(_objectSpread2({}, state), {}, { lifecycle: { phase: "attaching" } }), []];
264
264
  case "DETACH": {
265
265
  const [, aborts] = abortInflight(state);
266
- return [initialState(cfg), aborts];
266
+ const fresh = initialState(cfg);
267
+ const { throughputEwma, throughputFastEwma } = state.stats;
268
+ return [_objectSpread2(_objectSpread2({}, fresh), {}, { stats: _objectSpread2(_objectSpread2({}, fresh.stats), {}, {
269
+ throughputEwma,
270
+ throughputFastEwma
271
+ }) }), aborts];
267
272
  }
268
273
  case "LOAD": {
269
274
  if (state.lifecycle.phase === "idle") return reject(state, msg.type, "not attached");
@@ -286,7 +291,15 @@ function reduceCommand(state, msg, cfg, hooks) {
286
291
  seq: 0,
287
292
  url: msg.url
288
293
  });
289
- return [_objectSpread2(_objectSpread2({}, state), {}, {
294
+ const seed = msg.seed;
295
+ const seeded = seed === void 0 ? state : _objectSpread2(_objectSpread2({}, state), {}, {
296
+ stats: _objectSpread2(_objectSpread2({}, state.stats), {}, {
297
+ throughputEwma: seed.throughputEwma,
298
+ throughputFastEwma: seed.throughputFastEwma
299
+ }),
300
+ quality: _objectSpread2(_objectSpread2({}, state.quality), {}, { constraints: new Map(seed.constraints) })
301
+ });
302
+ return [_objectSpread2(_objectSpread2({}, seeded), {}, {
290
303
  lifecycle: { phase: "loading" },
291
304
  scheduling: _objectSpread2(_objectSpread2({}, state.scheduling), {}, {
292
305
  inflight,
@@ -19,8 +19,13 @@ const DEFAULT_RETRY_POLICY = {
19
19
  function backoffDelayMs(policy, attempt) {
20
20
  return Math.min(policy.maxDelayMs, policy.baseDelayMs * Math.pow(policy.factor, attempt - 1));
21
21
  }
22
- const PRIMED_LIMIT = 3;
22
+ const PRIMED_LIMIT = 32;
23
+ const PRIMED_BYTES = 48e6;
23
24
  const PRIMED_TTL_MS = 3e4;
25
+ /** A primed response answers one URL and one byte range. */
26
+ function primedKey(url, range) {
27
+ return range === void 0 ? url : `${url} ${range.start}-${range.end}`;
28
+ }
24
29
  function createTransport(options) {
25
30
  var _options$fetchImpl, _options$now;
26
31
  const policy = _objectSpread2(_objectSpread2({}, DEFAULT_RETRY_POLICY), options.retry);
@@ -30,21 +35,43 @@ function createTransport(options) {
30
35
  const requestHooks = /* @__PURE__ */ new Set();
31
36
  const responseHooks = /* @__PURE__ */ new Set();
32
37
  const live = /* @__PURE__ */ new Map();
38
+ let channels = 0;
33
39
  const primed = /* @__PURE__ */ new Map();
34
- /** The primed response for a URL, once; null when none or expired. */
35
- function takePrimed(url) {
36
- const entry = primed.get(url);
40
+ const main = {
41
+ id: "",
42
+ absorb: options.absorb
43
+ };
44
+ /** The primed entry for a URL and range, once; null when none or expired. */
45
+ function takePrimed(url, range) {
46
+ const key = primedKey(url, range);
47
+ const entry = primed.get(key);
37
48
  if (entry === void 0) return null;
38
- primed.delete(url);
39
- if (now() - entry.at > PRIMED_TTL_MS) return null;
40
- return new Response(entry.bytes, {
41
- status: 200,
42
- headers: entry.headers
49
+ primed.delete(key);
50
+ return now() > entry.expires ? null : entry;
51
+ }
52
+ function prime(url, bytes, headers, kept = {}) {
53
+ var _kept$keepMs, _kept$rtt;
54
+ const key = primedKey(url, kept.range);
55
+ primed.delete(key);
56
+ primed.set(key, {
57
+ bytes,
58
+ headers,
59
+ expires: now() + ((_kept$keepMs = kept.keepMs) !== null && _kept$keepMs !== void 0 ? _kept$keepMs : PRIMED_TTL_MS),
60
+ rtt: (_kept$rtt = kept.rtt) !== null && _kept$rtt !== void 0 ? _kept$rtt : 0
43
61
  });
62
+ let total = 0;
63
+ for (const entry of primed.values()) total += entry.bytes.byteLength;
64
+ while (primed.size > 1 && (primed.size > PRIMED_LIMIT || total > PRIMED_BYTES)) {
65
+ const oldest = primed.keys().next().value;
66
+ total -= primed.get(oldest).bytes.byteLength;
67
+ primed.delete(oldest);
68
+ }
44
69
  }
45
- function fail(token, request, code, status) {
46
- live.delete(token);
47
- options.absorb(_objectSpread2(_objectSpread2(_objectSpread2({
70
+ function fail(sink, token, request, code, status) {
71
+ var _sink$failed;
72
+ live.delete(sink.id + token);
73
+ (_sink$failed = sink.failed) === null || _sink$failed === void 0 || _sink$failed.call(sink, token);
74
+ sink.absorb(_objectSpread2(_objectSpread2(_objectSpread2({
48
75
  type: "SEGMENT_FAILED",
49
76
  trackId: request.trackId,
50
77
  seq: request.seq
@@ -64,7 +91,7 @@ function createTransport(options) {
64
91
  hook(info);
65
92
  } catch (_unused) {}
66
93
  }
67
- function attempt(token, request, effectUrl, range, timeoutFromEffect, attemptNo) {
94
+ function attempt(sink, token, request, effectUrl, range, timeoutFromEffect, attemptNo) {
68
95
  const state = {
69
96
  controller: new AbortController(),
70
97
  timedOut: false,
@@ -72,7 +99,7 @@ function createTransport(options) {
72
99
  timeoutId: null,
73
100
  retryId: null
74
101
  };
75
- live.set(token, state);
102
+ live.set(sink.id + token, state);
76
103
  const draft = {
77
104
  url: effectUrl,
78
105
  headers: {},
@@ -94,16 +121,19 @@ function createTransport(options) {
94
121
  const startedAt = now();
95
122
  const retryOrFail = (code, status) => {
96
123
  if (!(attemptNo < policy.maxAttempts && (code !== "NETWORK_HTTP_STATUS" || status !== void 0 && policy.retryStatuses.includes(status)))) {
97
- fail(token, request, code, status);
124
+ fail(sink, token, request, code, status);
98
125
  return;
99
126
  }
100
127
  state.retryId = setTimeout(() => {
101
128
  state.retryId = null;
102
- attempt(token, request, effectUrl, range, timeoutFromEffect, attemptNo + 1);
129
+ attempt(sink, token, request, effectUrl, range, timeoutFromEffect, attemptNo + 1);
103
130
  }, backoffDelayMs(policy, attemptNo));
104
131
  };
105
- const answer = takePrimed(effectUrl);
106
- (answer !== null ? Promise.resolve(answer) : fetchImpl(draft.url, {
132
+ const answer = takePrimed(effectUrl, range);
133
+ (answer !== null ? Promise.resolve(new Response(answer.bytes, {
134
+ status: 200,
135
+ headers: answer.headers
136
+ })) : fetchImpl(draft.url, {
107
137
  headers: draft.headers,
108
138
  signal: state.controller.signal
109
139
  })).then(function() {
@@ -129,10 +159,10 @@ function createTransport(options) {
129
159
  if (refused) yield (_response$body = response.body) === null || _response$body === void 0 ? void 0 : _response$body.cancel();
130
160
  const bytes = refused ? /* @__PURE__ */ new ArrayBuffer(0) : yield response.arrayBuffer();
131
161
  if (state.userAborted) {
132
- live.delete(token);
162
+ live.delete(sink.id + token);
133
163
  return;
134
164
  }
135
- const rtt = now() - startedAt;
165
+ const rtt = answer !== null ? answer.rtt : now() - startedAt;
136
166
  notifyResponse({
137
167
  token,
138
168
  url: draft.url,
@@ -143,9 +173,11 @@ function createTransport(options) {
143
173
  attempt: attemptNo,
144
174
  headers: response.headers
145
175
  });
146
- live.delete(token);
176
+ live.delete(sink.id + token);
147
177
  if (refused) {
148
- options.absorb({
178
+ var _sink$failed2;
179
+ (_sink$failed2 = sink.failed) === null || _sink$failed2 === void 0 || _sink$failed2.call(sink, token);
180
+ sink.absorb({
149
181
  type: "SEGMENT_FAILED",
150
182
  trackId: request.trackId,
151
183
  seq: request.seq,
@@ -164,7 +196,11 @@ function createTransport(options) {
164
196
  });
165
197
  return;
166
198
  }
167
- options.absorb({
199
+ if (sink.keepMs !== void 0) prime(effectUrl, bytes, response.headers, _objectSpread2(_objectSpread2({}, range !== void 0 ? { range } : {}), {}, {
200
+ keepMs: sink.keepMs,
201
+ rtt
202
+ }));
203
+ sink.absorb({
168
204
  type: "SEGMENT_LOADED",
169
205
  trackId: request.trackId,
170
206
  seq: request.seq,
@@ -181,7 +217,7 @@ function createTransport(options) {
181
217
  }()).catch(() => {
182
218
  if (state.timeoutId !== null) clearTimeout(state.timeoutId);
183
219
  if (state.userAborted) {
184
- live.delete(token);
220
+ live.delete(sink.id + token);
185
221
  return;
186
222
  }
187
223
  const outcome = state.timedOut ? "timeout" : "failure";
@@ -220,10 +256,36 @@ function createTransport(options) {
220
256
  seq: -1,
221
257
  url: effect.url
222
258
  }, effect.renditionId !== void 0 ? { renditionId: effect.renditionId } : {});
223
- attempt(effect.token, request, effect.url, effect.range, effect.timeout, 1);
259
+ attempt(main, effect.token, request, effect.url, effect.range, effect.timeout, 1);
224
260
  return () => cancel(effect.token);
225
261
  });
226
262
  },
263
+ channel(deps) {
264
+ channels += 1;
265
+ const sink = _objectSpread2({
266
+ id: `c${channels}:`,
267
+ absorb: deps.absorb,
268
+ keepMs: deps.keepMs
269
+ }, deps.failed !== void 0 ? { failed: deps.failed } : {});
270
+ const tokens = /* @__PURE__ */ new Set();
271
+ return {
272
+ fetch(effect) {
273
+ var _deps$inflight;
274
+ const request = (_deps$inflight = deps.inflight(effect.token)) !== null && _deps$inflight !== void 0 ? _deps$inflight : _objectSpread2({
275
+ token: effect.token,
276
+ trackId: effect.token,
277
+ seq: -1,
278
+ url: effect.url
279
+ }, effect.renditionId !== void 0 ? { renditionId: effect.renditionId } : {});
280
+ tokens.add(effect.token);
281
+ attempt(sink, effect.token, request, effect.url, effect.range, effect.timeout, 1);
282
+ return () => cancel(sink.id + effect.token);
283
+ },
284
+ close() {
285
+ for (const token of tokens) cancel(sink.id + token);
286
+ }
287
+ };
288
+ },
227
289
  request(url, init) {
228
290
  return _asyncToGenerator(function* () {
229
291
  var _init$headers, _init$method;
@@ -254,15 +316,7 @@ function createTransport(options) {
254
316
  pending() {
255
317
  return [...live.keys()];
256
318
  },
257
- prime(url, bytes, headers) {
258
- primed.delete(url);
259
- primed.set(url, {
260
- bytes,
261
- headers,
262
- at: now()
263
- });
264
- while (primed.size > PRIMED_LIMIT) primed.delete(primed.keys().next().value);
265
- }
319
+ prime
266
320
  };
267
321
  }
268
322
  //#endregion
@@ -7,15 +7,12 @@ import renditionNames from "../../stages/rendition-names/index.js";
7
7
  import qoe from "../../stages/qoe/index.js";
8
8
  import hdr from "../../stages/hdr/index.js";
9
9
  import cmsd from "../../stages/cmsd/index.js";
10
- import cmcd from "../../stages/cmcd/index.js";
11
10
  import chapters from "../../stages/chapters/index.js";
12
11
  import { definePreset } from "../define.js";
13
12
  import { base, dashLine, drmTier, hlsLine, tsTier } from "../tiers.js";
14
13
  //#region src/presets/full/index.ts
15
14
  /**
16
- * The `full` preset: every stage the catalogue ships. `dual-ts-drm` plus
17
- * the accessories: CMCD (changes every request, so opt-in elsewhere),
18
- * thumbnails (fetches image playlists, so opt-in elsewhere), chapters
15
+ * The `full` preset: `dual-ts-drm` plus the accessories: thumbnails (fetches image playlists, so opt-in elsewhere), chapters
19
16
  * (loads a file the app names), trick play (a UI control drives it),
20
17
  * timed metadata (a page reads it), CEA-708 captions (US broadcast
21
18
  * streams carry 608 beside them, which the base reads), TTML subtitles
@@ -23,6 +20,8 @@ import { base, dashLine, drmTier, hlsLine, tsTier } from "../tiers.js";
23
20
  * where nothing is known, which a page may not want by default), local
24
21
  * QoE metrics, CMSD (it acts on response headers a CDN must expose), and
25
22
  * localized rendition names (a menu reads them).
23
+ * CMCD is in no preset: it changes every request, which costs a page that
24
+ * never asked for it. A page adds the stage.
26
25
  * Feature parity with
27
26
  * videojs-http-streaming; the modularity claim is measured against it, and
28
27
  * the main CDN bundle exposes it.
@@ -33,7 +32,6 @@ const preset = definePreset("full", () => [
33
32
  ...base(),
34
33
  ...tsTier(),
35
34
  ...drmTier(),
36
- cmcd(),
37
35
  thumbnails(),
38
36
  chapters(),
39
37
  trickPlay(),
@@ -47,8 +47,7 @@ function dashLine() {
47
47
  return [dashCmaf(), dashLive()];
48
48
  }
49
49
  /**
50
- * What every preset carries: adaptive quality with a size cap and bandwidth
51
- * memory, recovery, content steering (inert until a manifest asks for it),
50
+ * What every preset carries: adaptive quality with a size cap, recovery, content steering (inert until a manifest asks for it),
52
51
  * alternate audio with codec switching, WebVTT subtitles, CMAF timing (reads
53
52
  * each segment's decode time so the kernel lands it at its playlist time,
54
53
  * whatever clock the packager wrote), program date time from either
@@ -1,6 +1,6 @@
1
+ import _asyncToGenerator from "../../_virtual/_@oxc-project_runtime@0.152.0/helpers/esm/asyncToGenerator.js";
1
2
  import { findRendition } from "../../kernel/presentation.js";
2
3
  import { segmentAt } from "../../kernel/timeline.js";
3
- import _asyncToGenerator from "../../_virtual/_@oxc-project_runtime@0.152.0/helpers/esm/asyncToGenerator.js";
4
4
  //#region src/protocols/dash-cmaf/periods-runtime.ts
5
5
  /**
6
6
  * What a flattened multi-period presentation needs at playback time, kept
@@ -1,5 +1,5 @@
1
- import { findRendition } from "../../kernel/presentation.js";
2
1
  import _asyncToGenerator from "../../_virtual/_@oxc-project_runtime@0.152.0/helpers/esm/asyncToGenerator.js";
2
+ import { findRendition } from "../../kernel/presentation.js";
3
3
  //#region src/stages/aes-128/index.ts
4
4
  /**
5
5
  * aes-128 as a stage: full-segment AES-CBC decryption for HLS EXT-X-KEY
@@ -1,6 +1,6 @@
1
1
  import _objectSpread2 from "../../_virtual/_@oxc-project_runtime@0.152.0/helpers/esm/objectSpread2.js";
2
- import { findTrackSite } from "../../kernel/presentation.js";
3
2
  import _asyncToGenerator from "../../_virtual/_@oxc-project_runtime@0.152.0/helpers/esm/asyncToGenerator.js";
3
+ import { findTrackSite } from "../../kernel/presentation.js";
4
4
  import { chaptersFromInput, parseAppleChapters, parseChapterTrack } from "./parse.js";
5
5
  //#region src/stages/chapters/index.ts
6
6
  /**
@@ -1,6 +1,6 @@
1
1
  import { scheduled } from "../../kernel/effects.js";
2
- import { isIndexed, segmentAt, segmentAtTime } from "../../kernel/timeline.js";
3
2
  import _asyncToGenerator from "../../_virtual/_@oxc-project_runtime@0.152.0/helpers/esm/asyncToGenerator.js";
3
+ import { isIndexed, segmentAt, segmentAtTime } from "../../kernel/timeline.js";
4
4
  import { parseVtt } from "../../containers/webvtt.js";
5
5
  import { resolveUrl } from "../../kernel/url.js";
6
6
  //#region src/stages/thumbnails/index.ts
@@ -1,6 +1,6 @@
1
+ import _asyncToGenerator from "../../_virtual/_@oxc-project_runtime@0.152.0/helpers/esm/asyncToGenerator.js";
1
2
  import { findRendition, findTrackSite, isTrick } from "../../kernel/presentation.js";
2
3
  import { isUnresolved, segmentAtTime } from "../../kernel/timeline.js";
3
- import _asyncToGenerator from "../../_virtual/_@oxc-project_runtime@0.152.0/helpers/esm/asyncToGenerator.js";
4
4
  import { decoderConfigBox, findBox, fragmentSamples, trackTimescales, trexDefaults } from "../../containers/mp4-box/index.js";
5
5
  import { concat } from "../../containers/fmp4/writer.js";
6
6
  import { fitFragment } from "../../containers/fmp4/fit.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAsBH,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAKvE,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,UAAU,EACV,KAAK,GACN,MAAM,6BAA6B,CAAC;AAGrC,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEjF,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAExD,mBAAmB,kBAAkB,CAAC;AACtC,mBAAmB,mBAAmB,CAAC;AACvC,mBAAmB,eAAe,CAAC;AACnC,mBAAmB,mBAAmB,CAAC;AACvC,mBAAmB,qBAAqB,CAAC;AACzC,mBAAmB,qBAAqB,CAAC;AACzC,mBAAmB,oBAAoB,CAAC;AACxC,mBAAmB,iBAAiB,CAAC;AACrC,mBAAmB,kBAAkB,CAAC;AAEtC;;;;;;GAMG;AAEH,MAAM,WAAW,kBAAkB;CAAG;AAEtC;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG,YAAY,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;AAkBlE,wBAAgB,QAAQ,CAAC,OAAO,EAAE,eAAe,GAAG,QAAQ,CA+a3D;yBA/ae,QAAQ;mBAqbH,gBAAgB,KAAG,QAAQ,GAAG,IAAI"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAuBH,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAMvE,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,UAAU,EACV,KAAK,GACN,MAAM,6BAA6B,CAAC;AAGrC,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEjF,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAExD,mBAAmB,kBAAkB,CAAC;AACtC,mBAAmB,mBAAmB,CAAC;AACvC,mBAAmB,eAAe,CAAC;AACnC,mBAAmB,mBAAmB,CAAC;AACvC,mBAAmB,qBAAqB,CAAC;AACzC,mBAAmB,qBAAqB,CAAC;AACzC,mBAAmB,oBAAoB,CAAC;AACxC,mBAAmB,iBAAiB,CAAC;AACrC,mBAAmB,kBAAkB,CAAC;AAEtC;;;;;;GAMG;AAEH,MAAM,WAAW,kBAAkB;CAAG;AAEtC;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG,YAAY,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;AAkBlE,wBAAgB,QAAQ,CAAC,OAAO,EAAE,eAAe,GAAG,QAAQ,CA+c3D;yBA/ce,QAAQ;mBAqdH,gBAAgB,KAAG,QAAQ,GAAG,IAAI"}