mattebox 0.5.0 → 0.6.1

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 (163) 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 +4 -4
  13. package/dist/containers/mp4-box/index.d.ts +9 -7
  14. package/dist/containers/mp4-box/index.d.ts.map +1 -1
  15. package/dist/containers/mp4-box/index.js +20 -28
  16. package/dist/containers/mp4-box/index.js.map +1 -1
  17. package/dist/containers/packed-audio/index.d.ts.map +1 -1
  18. package/dist/containers/packed-audio/index.js +5 -1
  19. package/dist/containers/packed-audio/index.js.map +1 -1
  20. package/dist/containers/ts-transmux/index.d.ts.map +1 -1
  21. package/dist/containers/ts-transmux/index.js +2 -1
  22. package/dist/containers/ts-transmux/index.js.map +1 -1
  23. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/asyncToGenerator.js +1 -1
  24. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/defineProperty.js +1 -1
  25. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/objectSpread2.js +1 -1
  26. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/objectWithoutProperties.js +1 -1
  27. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/objectWithoutPropertiesLoose.js +1 -1
  28. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/toPrimitive.js +1 -1
  29. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/toPropertyKey.js +1 -1
  30. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/typeof.js +1 -1
  31. package/dist/es2015/containers/id3.js +1 -1
  32. package/dist/es2015/containers/mp4-box/index.js +19 -22
  33. package/dist/es2015/containers/packed-audio/index.js +2 -5
  34. package/dist/es2015/containers/ts-transmux/h264.js +1 -1
  35. package/dist/es2015/containers/ts-transmux/index.js +3 -6
  36. package/dist/es2015/index.js +40 -27
  37. package/dist/es2015/kernel/append-queue.js +1 -1
  38. package/dist/es2015/kernel/context.js +7 -0
  39. package/dist/es2015/kernel/loader.js +5 -4
  40. package/dist/es2015/kernel/mse.js +48 -74
  41. package/dist/es2015/kernel/prepare.js +77 -0
  42. package/dist/es2015/kernel/reducer.js +284 -73
  43. package/dist/es2015/kernel/refresh.js +35 -0
  44. package/dist/es2015/kernel/rendition-select.js +62 -2
  45. package/dist/es2015/kernel/scheduler.js +23 -5
  46. package/dist/es2015/kernel/timeline.js +22 -1
  47. package/dist/es2015/kernel/trace.js +1 -1
  48. package/dist/es2015/kernel/transport.js +4 -4
  49. package/dist/es2015/presets/define.js +2 -2
  50. package/dist/es2015/presets/tiers.js +7 -6
  51. package/dist/es2015/protocols/dash-cmaf/index.js +136 -55
  52. package/dist/es2015/protocols/dash-cmaf/parse.js +2 -10
  53. package/dist/es2015/protocols/dash-live/index.js +88 -16
  54. package/dist/es2015/protocols/hls-cmaf/index.js +119 -71
  55. package/dist/es2015/protocols/hls-cmaf/parse.js +35 -25
  56. package/dist/es2015/protocols/hls-cmaf/unavailable.js +42 -0
  57. package/dist/es2015/protocols/hls-live/index.js +221 -78
  58. package/dist/es2015/stages/abr/index.js +1 -1
  59. package/dist/es2015/stages/abr-persist/index.js +1 -1
  60. package/dist/es2015/stages/aes-128/index.js +1 -1
  61. package/dist/es2015/stages/alt-audio/index.js +1 -1
  62. package/dist/es2015/stages/cmaf-timing/index.js +24 -39
  63. package/dist/es2015/stages/cmcd/index.js +15 -2
  64. package/dist/es2015/stages/content-steering/index.js +30 -3
  65. package/dist/es2015/stages/eme-core/index.js +2 -2
  66. package/dist/es2015/stages/eme-fairplay/index.js +1 -1
  67. package/dist/es2015/stages/recovery/index.js +1 -1
  68. package/dist/es2015/stages/text-cea608/decode.js +1 -1
  69. package/dist/es2015/stages/text-webvtt/parse.js +1 -1
  70. package/dist/es2015/stages/thumbnails/index.js +1 -1
  71. package/dist/index.d.ts.map +1 -1
  72. package/dist/index.js +37 -32
  73. package/dist/index.js.map +1 -1
  74. package/dist/kernel/context.d.ts +5 -1
  75. package/dist/kernel/context.d.ts.map +1 -1
  76. package/dist/kernel/context.js +10 -0
  77. package/dist/kernel/context.js.map +1 -1
  78. package/dist/kernel/loader.d.ts.map +1 -1
  79. package/dist/kernel/loader.js +5 -7
  80. package/dist/kernel/loader.js.map +1 -1
  81. package/dist/kernel/mse.d.ts +6 -10
  82. package/dist/kernel/mse.d.ts.map +1 -1
  83. package/dist/kernel/mse.js +45 -76
  84. package/dist/kernel/mse.js.map +1 -1
  85. package/dist/kernel/prepare.d.ts +28 -0
  86. package/dist/kernel/prepare.d.ts.map +1 -0
  87. package/dist/kernel/prepare.js +89 -0
  88. package/dist/kernel/prepare.js.map +1 -0
  89. package/dist/kernel/reducer.d.ts +15 -0
  90. package/dist/kernel/reducer.d.ts.map +1 -1
  91. package/dist/kernel/reducer.js +399 -88
  92. package/dist/kernel/reducer.js.map +1 -1
  93. package/dist/kernel/refresh.d.ts +14 -0
  94. package/dist/kernel/refresh.d.ts.map +1 -0
  95. package/dist/kernel/refresh.js +50 -0
  96. package/dist/kernel/refresh.js.map +1 -0
  97. package/dist/kernel/rendition-select.d.ts +19 -1
  98. package/dist/kernel/rendition-select.d.ts.map +1 -1
  99. package/dist/kernel/rendition-select.js +69 -0
  100. package/dist/kernel/rendition-select.js.map +1 -1
  101. package/dist/kernel/scheduler.d.ts +18 -3
  102. package/dist/kernel/scheduler.d.ts.map +1 -1
  103. package/dist/kernel/scheduler.js +32 -7
  104. package/dist/kernel/scheduler.js.map +1 -1
  105. package/dist/kernel/timeline.d.ts +18 -6
  106. package/dist/kernel/timeline.d.ts.map +1 -1
  107. package/dist/kernel/timeline.js +19 -5
  108. package/dist/kernel/timeline.js.map +1 -1
  109. package/dist/kernel/transport.d.ts.map +1 -1
  110. package/dist/kernel/transport.js +1 -0
  111. package/dist/kernel/transport.js.map +1 -1
  112. package/dist/presets/tiers.d.ts +7 -6
  113. package/dist/presets/tiers.d.ts.map +1 -1
  114. package/dist/presets/tiers.js +7 -6
  115. package/dist/presets/tiers.js.map +1 -1
  116. package/dist/protocols/dash-cmaf/index.d.ts +2 -2
  117. package/dist/protocols/dash-cmaf/index.d.ts.map +1 -1
  118. package/dist/protocols/dash-cmaf/index.js +166 -55
  119. package/dist/protocols/dash-cmaf/index.js.map +1 -1
  120. package/dist/protocols/dash-live/index.d.ts.map +1 -1
  121. package/dist/protocols/dash-live/index.js +83 -11
  122. package/dist/protocols/dash-live/index.js.map +1 -1
  123. package/dist/protocols/hls-cmaf/index.d.ts +3 -2
  124. package/dist/protocols/hls-cmaf/index.d.ts.map +1 -1
  125. package/dist/protocols/hls-cmaf/index.js +140 -82
  126. package/dist/protocols/hls-cmaf/index.js.map +1 -1
  127. package/dist/protocols/hls-cmaf/parse.d.ts +9 -5
  128. package/dist/protocols/hls-cmaf/parse.d.ts.map +1 -1
  129. package/dist/protocols/hls-cmaf/parse.js +50 -47
  130. package/dist/protocols/hls-cmaf/parse.js.map +1 -1
  131. package/dist/protocols/hls-cmaf/unavailable.d.ts +17 -0
  132. package/dist/protocols/hls-cmaf/unavailable.d.ts.map +1 -0
  133. package/dist/protocols/hls-cmaf/unavailable.js +36 -0
  134. package/dist/protocols/hls-cmaf/unavailable.js.map +1 -0
  135. package/dist/protocols/hls-live/index.d.ts +17 -0
  136. package/dist/protocols/hls-live/index.d.ts.map +1 -1
  137. package/dist/protocols/hls-live/index.js +239 -76
  138. package/dist/protocols/hls-live/index.js.map +1 -1
  139. package/dist/stages/aes-128/index.js +2 -2
  140. package/dist/stages/aes-128/index.js.map +1 -1
  141. package/dist/stages/alt-audio/index.d.ts.map +1 -1
  142. package/dist/stages/alt-audio/index.js +5 -3
  143. package/dist/stages/alt-audio/index.js.map +1 -1
  144. package/dist/stages/cmaf-timing/index.d.ts.map +1 -1
  145. package/dist/stages/cmaf-timing/index.js +33 -42
  146. package/dist/stages/cmaf-timing/index.js.map +1 -1
  147. package/dist/stages/cmcd/index.d.ts.map +1 -1
  148. package/dist/stages/cmcd/index.js +15 -3
  149. package/dist/stages/cmcd/index.js.map +1 -1
  150. package/dist/stages/content-steering/index.d.ts.map +1 -1
  151. package/dist/stages/content-steering/index.js +23 -1
  152. package/dist/stages/content-steering/index.js.map +1 -1
  153. package/dist/types/facade.d.ts +12 -0
  154. package/dist/types/facade.d.ts.map +1 -1
  155. package/dist/types/kernel.d.ts +54 -15
  156. package/dist/types/kernel.d.ts.map +1 -1
  157. package/dist/types/kernel.js +2 -1
  158. package/dist/types/kernel.js.map +1 -1
  159. package/dist/types/messages.d.ts +58 -10
  160. package/dist/types/messages.d.ts.map +1 -1
  161. package/dist/types/stage.d.ts +18 -0
  162. package/dist/types/stage.d.ts.map +1 -1
  163. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- import _objectSpread2 from "../../_virtual/_@oxc-project_runtime@0.149.0/helpers/esm/objectSpread2.js";
1
+ import _objectSpread2 from "../../_virtual/_@oxc-project_runtime@0.150.0/helpers/esm/objectSpread2.js";
2
2
  import { registerLiveNamespace } from "../live-shared.js";
3
3
  import { parse } from "../dash-cmaf/parse.js";
4
4
  //#region src/protocols/dash-live/index.ts
@@ -12,6 +12,14 @@ const DEFAULT_HOLD_BACK = 10;
12
12
  const DEFAULT_TIME_SHIFT = 60;
13
13
  /** Window movement below this is not worth a fact, in seconds. */
14
14
  const WINDOW_QUANTUM = .5;
15
+ /**
16
+ * Failed MPD reloads in a row before the load fails. Each reload already
17
+ * carries the transport's own retries, so this is several update periods
18
+ * of an MPD that does not answer. Without a limit the stream stops moving
19
+ * and nothing says why; retrying forever keeps the network busy for a
20
+ * stream that cannot play.
21
+ */
22
+ const FAILURE_LIMIT = 4;
15
23
  const INITIAL = {
16
24
  manifestUrl: null,
17
25
  skew: null,
@@ -19,7 +27,9 @@ const INITIAL = {
19
27
  tickPending: false,
20
28
  clockPending: false,
21
29
  lastWindowEnd: -1,
22
- lastWallClock: null
30
+ lastWallClock: null,
31
+ suspended: false,
32
+ failures: 0
23
33
  };
24
34
  /** The window arithmetic; null when it cannot move or is not worth a fact. */
25
35
  function windowUpdate(state, live) {
@@ -82,11 +92,69 @@ function clockTick() {
82
92
  }
83
93
  };
84
94
  }
95
+ /**
96
+ * An MPD reload that failed or did not parse. The next reload waits one
97
+ * update period, as a good one would; after FAILURE_LIMIT in a row the
98
+ * load fails.
99
+ */
100
+ function reloadFailed(state, presentation, cause) {
101
+ var _presentation$live$up;
102
+ const failures = state.failures + 1;
103
+ if (failures >= FAILURE_LIMIT) return [_objectSpread2(_objectSpread2({}, state), {}, { failures }), [feed({
104
+ type: "MANIFEST_FAILED",
105
+ error: {
106
+ category: "manifest",
107
+ code: "MANIFEST_REFRESH_FAILED",
108
+ fatal: true,
109
+ recoverable: false,
110
+ context: {
111
+ cause,
112
+ attempts: failures
113
+ }
114
+ }
115
+ })]];
116
+ if (state.tickPending || state.suspended || !drivesWindow(presentation)) return [_objectSpread2(_objectSpread2({}, state), {}, { failures }), []];
117
+ return [_objectSpread2(_objectSpread2({}, state), {}, {
118
+ failures,
119
+ tickPending: true
120
+ }), [tick((_presentation$live$up = presentation.live.updatePeriod) !== null && _presentation$live$up !== void 0 ? _presentation$live$up : 4)]];
121
+ }
85
122
  const reduceDashLive = (slice, msg, kernel) => {
86
123
  let state = slice !== null && slice !== void 0 ? slice : INITIAL;
87
124
  if ((msg.type === "TIME_UPDATE" || msg.type === "SEGMENT_LOADED") && msg.wallClock !== void 0) state = _objectSpread2(_objectSpread2({}, state), {}, { lastWallClock: msg.wallClock });
88
125
  if (msg.type === "LOAD") return [_objectSpread2(_objectSpread2({}, INITIAL), {}, { manifestUrl: msg.url }), []];
89
126
  if (msg.type === "UNLOAD" || msg.type === "DETACH") return [INITIAL, []];
127
+ if (msg.type === "SUSPEND") {
128
+ const effects = [];
129
+ if (state.tickPending) effects.push({
130
+ kind: "abort",
131
+ token: TICK_TOKEN
132
+ });
133
+ if (state.clockPending) effects.push({
134
+ kind: "abort",
135
+ token: CLOCK_TOKEN
136
+ });
137
+ effects.push({
138
+ kind: "abort",
139
+ token: MPD_TOKEN
140
+ });
141
+ return [_objectSpread2(_objectSpread2({}, state), {}, {
142
+ tickPending: false,
143
+ clockPending: false,
144
+ lastWindowEnd: -1,
145
+ suspended: true
146
+ }), effects];
147
+ }
148
+ if (msg.type === "RESUME") {
149
+ if (!state.suspended) return [state, []];
150
+ const resumed = _objectSpread2(_objectSpread2({}, state), {}, { suspended: false });
151
+ if (state.manifestUrl === null || !drivesWindow(kernel.presentation)) return [resumed, []];
152
+ return [resumed, [{
153
+ kind: "fetch",
154
+ token: MPD_TOKEN,
155
+ url: state.manifestUrl
156
+ }]];
157
+ }
90
158
  if (msg.type === "MANIFEST_LOADED") {
91
159
  if (!drivesWindow(msg.presentation)) return [state, []];
92
160
  const effects = [];
@@ -120,7 +188,7 @@ const reduceDashLive = (slice, msg, kernel) => {
120
188
  return [next, effects];
121
189
  }
122
190
  if (msg.type === "TICK" && msg.token === CLOCK_TOKEN) {
123
- if (!drivesWindow(kernel.presentation)) return [_objectSpread2(_objectSpread2({}, state), {}, { clockPending: false }), []];
191
+ if (state.suspended || !drivesWindow(kernel.presentation)) return [_objectSpread2(_objectSpread2({}, state), {}, { clockPending: false }), []];
124
192
  let next = _objectSpread2(_objectSpread2({}, state), {}, { clockPending: true });
125
193
  if (msg.wallClock !== void 0) next = _objectSpread2(_objectSpread2({}, next), {}, { lastWallClock: msg.wallClock });
126
194
  const effects = [clockTick()];
@@ -135,13 +203,15 @@ const reduceDashLive = (slice, msg, kernel) => {
135
203
  return [next, effects];
136
204
  }
137
205
  if (msg.type === "TICK" && msg.token === TICK_TOKEN) {
138
- if (state.manifestUrl === null || !drivesWindow(kernel.presentation)) return [_objectSpread2(_objectSpread2({}, state), {}, { tickPending: false }), []];
206
+ if (state.suspended || state.manifestUrl === null || !drivesWindow(kernel.presentation)) return [_objectSpread2(_objectSpread2({}, state), {}, { tickPending: false }), []];
139
207
  return [_objectSpread2(_objectSpread2({}, state), {}, { tickPending: false }), [{
140
208
  kind: "fetch",
141
209
  token: MPD_TOKEN,
142
210
  url: state.manifestUrl
143
211
  }]];
144
212
  }
213
+ if (msg.type === "SEGMENT_FAILED" && msg.trackId === UTC_TOKEN) return [_objectSpread2(_objectSpread2({}, state), {}, { utcPending: false }), []];
214
+ if (msg.type === "SEGMENT_FAILED" && msg.trackId === MPD_TOKEN) return reloadFailed(state, kernel.presentation, msg.error.code);
145
215
  if (msg.type === "SEGMENT_LOADED" && msg.trackId === UTC_TOKEN) {
146
216
  const serverTime = Date.parse(new TextDecoder().decode(msg.bytes).trim());
147
217
  if (!Number.isFinite(serverTime) || msg.wallClock === void 0) return [_objectSpread2(_objectSpread2({}, state), {}, { utcPending: false }), []];
@@ -153,19 +223,21 @@ const reduceDashLive = (slice, msg, kernel) => {
153
223
  if (msg.type === "SEGMENT_LOADED" && msg.trackId === MPD_TOKEN) {
154
224
  if (state.manifestUrl === null) return [state, []];
155
225
  const result = parse(new TextDecoder().decode(msg.bytes), state.manifestUrl);
226
+ if (result.presentation === null) {
227
+ var _result$error$code, _result$error;
228
+ return reloadFailed(state, kernel.presentation, (_result$error$code = (_result$error = result.error) === null || _result$error === void 0 ? void 0 : _result$error.code) !== null && _result$error$code !== void 0 ? _result$error$code : "MANIFEST_PARSE_FAILED");
229
+ }
156
230
  const effects = [];
157
- let next = state;
158
- if (result.presentation !== null) {
159
- effects.push(feed({
160
- type: "MANIFEST_LOADED",
161
- presentation: result.presentation
162
- }));
163
- if (result.presentation.isLive && result.presentation.live !== void 0) {
164
- const window = windowUpdate(next, result.presentation.live);
165
- if (window !== null) {
166
- next = window.next;
167
- effects.push(window.fact);
168
- }
231
+ let next = _objectSpread2(_objectSpread2({}, state), {}, { failures: 0 });
232
+ effects.push(feed({
233
+ type: "MANIFEST_LOADED",
234
+ presentation: result.presentation
235
+ }));
236
+ if (result.presentation.isLive && result.presentation.live !== void 0) {
237
+ const window = windowUpdate(next, result.presentation.live);
238
+ if (window !== null) {
239
+ next = window.next;
240
+ effects.push(window.fact);
169
241
  }
170
242
  }
171
243
  return [next, effects];
@@ -1,6 +1,8 @@
1
- import _objectSpread2 from "../../_virtual/_@oxc-project_runtime@0.149.0/helpers/esm/objectSpread2.js";
1
+ import _objectSpread2 from "../../_virtual/_@oxc-project_runtime@0.150.0/helpers/esm/objectSpread2.js";
2
2
  import { normalizeMimeType } from "../../kernel/mime.js";
3
- import { mergePlaylist, parse, parseMediaPlaylist } from "./parse.js";
3
+ import { ladderNeighbours } from "../../kernel/rendition-select.js";
4
+ import { parse, parseMediaPlaylist, refreshFor } from "./parse.js";
5
+ import { LOAD_FAILED, unavailableMessages } from "./unavailable.js";
4
6
  //#region src/protocols/hls-cmaf/index.ts
5
7
  /**
6
8
  * The hls-cmaf protocol adapter as a stage. Inert module: exports a
@@ -9,8 +11,9 @@ import { mergePlaylist, parse, parseMediaPlaylist } from "./parse.js";
9
11
  * The choreography is a slice reducer, so the whole protocol lives inside
10
12
  * the message loop and the trace: manifest bytes arrive as SEGMENT_LOADED
11
13
  * facts, parsing is pure, and results loop back through zero-delay
12
- * schedule effects carrying MANIFEST_LOADED. Media playlists fetch under
13
- * `hls:pl:` tokens the transport correlates back by token.
14
+ * schedule effects: MANIFEST_LOADED for the multivariant playlist, one
15
+ * PLAYLIST_REFRESHED per rendition for a media playlist. Media playlists
16
+ * fetch under `hls:pl:` tokens the transport correlates back by token.
14
17
  */
15
18
  const PLAYLIST_TOKEN = "hls:pl:";
16
19
  /** The playlist MIME types, RFC 8216 §4: the registered type and the three in common use. */
@@ -23,7 +26,9 @@ const MANIFEST_TYPES = [
23
26
  const INITIAL = {
24
27
  manifestUrl: null,
25
28
  mimeType: null,
26
- pending: {}
29
+ pending: {},
30
+ answered: {},
31
+ seen: ""
27
32
  };
28
33
  /**
29
34
  * Whether this adapter owns the manifest bytes. An explicit mimeType
@@ -36,37 +41,44 @@ function claims(state, text) {
36
41
  if (state.mimeType !== null) return MANIFEST_TYPES.includes(state.mimeType);
37
42
  return text.trimStart().startsWith("#EXTM3U");
38
43
  }
39
- function findRendition(presentation, renditionId) {
40
- for (const period of presentation.periods) for (const track of period.tracks) for (const rendition of track.renditions) if (rendition.id === renditionId) return {
41
- rendition,
42
- track
43
- };
44
- return null;
45
- }
46
44
  /**
47
- * The renditions whose media playlists still need fetching: every rendition of
48
- * every active track. Fetching only the "chosen" one is not enough, because ABR
49
- * (or any switch policy) selects independently and can pick a rendition this
50
- * slice would not have; its segments would then stay empty and the scheduler
51
- * would stall the moment ABR ramped onto it, which looks like playback dying
52
- * once the buffer drains. Fetching all of an active track's playlists means the
53
- * rendition ABR lands on is already loaded, and a switch never stalls.
45
+ * The playlist URLs the selection needs and this slice has not asked for:
46
+ * the playing video rendition and its ladder neighbours, and every
47
+ * rendition of the active audio and text tracks (one each in practice; an
48
+ * audio-only ladder is short). Fetching the whole ladder up front costs one
49
+ * request per variant, hundreds on a large multivariant playlist.
54
50
  */
55
- function neededPlaylists(kernel) {
51
+ function neededPlaylists(state, kernel) {
56
52
  const presentation = kernel.presentation;
57
53
  if (presentation === null) return [];
58
- const activeIds = new Set([
54
+ const asked = new Set(Object.values(state.pending));
55
+ const needed = [];
56
+ for (const contentType of [
59
57
  "video",
60
58
  "audio",
61
59
  "text"
62
- ].map((contentType) => kernel.tracks.active.get(contentType)).filter((id) => id !== void 0));
63
- const needed = [];
64
- for (const period of presentation.periods) for (const track of period.tracks) {
65
- if (!activeIds.has(track.id)) continue;
66
- for (const rendition of track.renditions) if (rendition.playlistUrl !== void 0 && Array.isArray(rendition.segments) && rendition.segments.length === 0) needed.push(rendition);
60
+ ]) {
61
+ const trackId = kernel.tracks.active.get(contentType);
62
+ if (trackId === void 0) continue;
63
+ for (const period of presentation.periods) for (const track of period.tracks) {
64
+ if (track.id !== trackId) continue;
65
+ const candidates = contentType === "video" ? ladderNeighbours(track.renditions, kernel.quality.active, kernel.quality.constraints) : track.renditions;
66
+ for (const rendition of candidates) {
67
+ const url = rendition.playlistUrl;
68
+ if (url === void 0 || url in state.answered || asked.has(url)) continue;
69
+ if (needed.some((r) => r.playlistUrl === url)) continue;
70
+ needed.push(rendition);
71
+ }
72
+ }
67
73
  }
68
74
  return needed;
69
75
  }
76
+ /** Every rendition that reads the playlist at `url`. */
77
+ function renditionsAt(presentation, url) {
78
+ const out = [];
79
+ for (const period of presentation.periods) for (const track of period.tracks) for (const rendition of track.renditions) if (rendition.playlistUrl === url) out.push(rendition);
80
+ return out;
81
+ }
70
82
  /** Loops a message back into the bus through a zero-delay schedule effect. */
71
83
  function feed(message) {
72
84
  return {
@@ -76,19 +88,27 @@ function feed(message) {
76
88
  then: message
77
89
  };
78
90
  }
79
- function loopBack(presentation) {
80
- return feed({
81
- type: "MANIFEST_LOADED",
82
- presentation
83
- });
91
+ /**
92
+ * A playlist that failed to load or parse. Its renditions are excluded,
93
+ * so arbitration moves off them and nothing asks for the playlist again.
94
+ * When that leaves a media track with no rendition at all, the load
95
+ * fails: the stream cannot play, and waiting would only stall.
96
+ */
97
+ function unavailable(state, kernel, url, error) {
98
+ const answered = _objectSpread2(_objectSpread2({}, state.answered), {}, { [url]: false });
99
+ const next = _objectSpread2(_objectSpread2({}, state), {}, { answered });
100
+ const presentation = kernel.presentation;
101
+ if (presentation === null) return [next, []];
102
+ const failed = [];
103
+ for (const [failedUrl, ok] of Object.entries(answered)) if (!ok) for (const rendition of renditionsAt(presentation, failedUrl)) failed.push(rendition.id);
104
+ return [next, unavailableMessages(kernel, LOAD_FAILED, failed, error).map(feed)];
84
105
  }
85
106
  const reduceHls = (slice, msg, kernel) => {
86
107
  const state = slice !== null && slice !== void 0 ? slice : INITIAL;
87
- if (msg.type === "LOAD") return [{
108
+ if (msg.type === "LOAD") return [_objectSpread2(_objectSpread2({}, INITIAL), {}, {
88
109
  manifestUrl: msg.url,
89
- mimeType: msg.mimeType === void 0 ? null : normalizeMimeType(msg.mimeType),
90
- pending: {}
91
- }, []];
110
+ mimeType: msg.mimeType === void 0 ? null : normalizeMimeType(msg.mimeType)
111
+ }), []];
92
112
  if (msg.type === "UNLOAD" || msg.type === "DETACH") return [INITIAL, []];
93
113
  if (msg.type === "SEGMENT_LOADED" && msg.trackId === "manifest" && state.manifestUrl !== null) {
94
114
  const text = new TextDecoder().decode(msg.bytes);
@@ -106,50 +126,78 @@ const reduceHls = (slice, msg, kernel) => {
106
126
  }
107
127
  })]];
108
128
  }
109
- return [state, [loopBack(result.presentation)]];
129
+ return [state, [feed({
130
+ type: "MANIFEST_LOADED",
131
+ presentation: result.presentation
132
+ })]];
110
133
  }
111
- if (msg.type === "SEGMENT_LOADED" && msg.trackId.startsWith(PLAYLIST_TOKEN)) {
112
- const renditionId = state.pending[msg.trackId];
113
- if (renditionId === void 0 || kernel.presentation === null) return [state, []];
114
- const site = findRendition(kernel.presentation, renditionId);
134
+ let next = state;
135
+ const effects = [];
136
+ if (msg.type === "SEGMENT_LOADED" && msg.trackId in state.pending) {
137
+ var _media$error;
138
+ const url = state.pending[msg.trackId];
115
139
  const pending = _objectSpread2({}, state.pending);
116
140
  delete pending[msg.trackId];
117
- if (site === null || site.rendition.playlistUrl === void 0) return [_objectSpread2(_objectSpread2({}, state), {}, { pending }), []];
118
- const text = new TextDecoder().decode(msg.bytes);
119
- const media = parseMediaPlaylist(text, site.rendition.playlistUrl);
120
- if (media.playlist === null) {
121
- var _media$error;
122
- return [_objectSpread2(_objectSpread2({}, state), {}, { pending }), [feed({
123
- type: "MANIFEST_FAILED",
124
- error: (_media$error = media.error) !== null && _media$error !== void 0 ? _media$error : {
125
- category: "manifest",
126
- code: "MANIFEST_PARSE_FAILED",
127
- fatal: true,
128
- recoverable: false
141
+ next = _objectSpread2(_objectSpread2({}, next), {}, { pending });
142
+ const media = parseMediaPlaylist(new TextDecoder().decode(msg.bytes), url);
143
+ const playlist = media.playlist;
144
+ const error = playlist === null ? (_media$error = media.error) !== null && _media$error !== void 0 ? _media$error : {
145
+ category: "manifest",
146
+ code: "MANIFEST_PARSE_FAILED",
147
+ fatal: false,
148
+ recoverable: false
149
+ } : playlist.endlist && playlist.segments.length === 0 ? {
150
+ category: "manifest",
151
+ code: "MANIFEST_EMPTY",
152
+ fatal: false,
153
+ recoverable: false
154
+ } : null;
155
+ if (error !== null || playlist === null || kernel.presentation === null) {
156
+ if (error === null) return [next, effects];
157
+ effects.push({
158
+ kind: "emit",
159
+ event: "error",
160
+ payload: {
161
+ category: error.category,
162
+ code: error.code,
163
+ fatal: false,
164
+ recoverable: false,
165
+ url
129
166
  }
130
- })]];
167
+ });
168
+ const [failed, failEffects] = unavailable(next, kernel, url, error);
169
+ return [failed, [...effects, ...failEffects]];
131
170
  }
132
- const merged = mergePlaylist(kernel.presentation, renditionId, media.playlist);
133
- return [_objectSpread2(_objectSpread2({}, state), {}, { pending }), [loopBack(merged)]];
171
+ next = _objectSpread2(_objectSpread2({}, next), {}, { answered: _objectSpread2(_objectSpread2({}, next.answered), {}, { [url]: true }) });
172
+ for (const rendition of renditionsAt(kernel.presentation, url)) {
173
+ const refresh = refreshFor(kernel.presentation, rendition.id, playlist);
174
+ if (refresh !== null) effects.push(feed(refresh));
175
+ }
176
+ return [next, effects];
134
177
  }
135
- if (msg.type === "MANIFEST_LOADED" || msg.type === "SELECT_TRACK" || msg.type === "TIME_UPDATE") {
136
- const needed = neededPlaylists(kernel).filter((rendition) => !Object.values(state.pending).includes(rendition.id));
137
- if (needed.length === 0) return [state, []];
138
- const effects = [];
178
+ if (msg.type === "SEGMENT_FAILED" && msg.trackId in state.pending) {
179
+ const url = state.pending[msg.trackId];
139
180
  const pending = _objectSpread2({}, state.pending);
140
- for (const rendition of needed) {
141
- const token = `${PLAYLIST_TOKEN}${rendition.id}`;
142
- if (pending[token] !== void 0) continue;
143
- pending[token] = rendition.id;
144
- effects.push({
145
- kind: "fetch",
146
- token,
147
- url: rendition.playlistUrl
148
- });
149
- }
150
- return [_objectSpread2(_objectSpread2({}, state), {}, { pending }), effects];
181
+ delete pending[msg.trackId];
182
+ return unavailable(_objectSpread2(_objectSpread2({}, next), {}, { pending }), kernel, url, msg.error);
183
+ }
184
+ if (kernel.lifecycle.phase !== "ready" || kernel.presentation === null) return [next, effects];
185
+ const key = `${kernel.quality.version}:${kernel.quality.active}`;
186
+ if (key === state.seen && msg.type !== "RESUME") return [next, effects];
187
+ next = _objectSpread2(_objectSpread2({}, next), {}, { seen: key });
188
+ const pending = _objectSpread2({}, next.pending);
189
+ for (const rendition of neededPlaylists(next, kernel)) {
190
+ const url = rendition.playlistUrl;
191
+ const token = `${PLAYLIST_TOKEN}${url}`;
192
+ pending[token] = url;
193
+ effects.push({
194
+ kind: "fetch",
195
+ token,
196
+ url,
197
+ renditionId: rendition.id
198
+ });
151
199
  }
152
- return [state, []];
200
+ return [_objectSpread2(_objectSpread2({}, next), {}, { pending }), effects];
153
201
  };
154
202
  /**
155
203
  * The stage factory. `mattebox({ stages: [hlsCmaf()] })` is all a consumer
@@ -1,4 +1,4 @@
1
- import _objectSpread2 from "../../_virtual/_@oxc-project_runtime@0.149.0/helpers/esm/objectSpread2.js";
1
+ import _objectSpread2 from "../../_virtual/_@oxc-project_runtime@0.150.0/helpers/esm/objectSpread2.js";
2
2
  import { lex } from "./lexer.js";
3
3
  //#region src/protocols/hls-cmaf/parse.ts
4
4
  function manifestError(reason) {
@@ -422,36 +422,46 @@ function timelineShift(previous, next) {
422
422
  return last.start + (first.seq - last.seq) * average - first.start;
423
423
  }
424
424
  /**
425
- * Immutably merges a fetched media playlist into the rendition that
426
- * referenced it. Duration, liveness, init, and protection all come from
427
- * the media playlist; the presentation learns them here. On a live refresh
428
- * the new window is rebased onto the running timeline first, so segment start
429
- * times stay absolute across reloads.
425
+ * The PLAYLIST_REFRESHED fact that merges a fetched media playlist into the
426
+ * rendition that referenced it, or null when the playlist is older than
427
+ * what the rendition holds. On a live refresh the new window is rebased
428
+ * onto the running timeline first, so segment start times stay absolute
429
+ * across reloads. The kernel merges the fact into the presentation it
430
+ * holds when the fact lands (applyRefresh).
430
431
  */
431
- function mergePlaylist(presentation, renditionId, playlist) {
432
- var _presentation$duratio;
432
+ function refreshFor(presentation, renditionId, playlist) {
433
433
  let previous = [];
434
- for (const period of presentation.periods) for (const track of period.tracks) for (const rendition of track.renditions) if (rendition.id === renditionId) previous = rendition.segments;
434
+ let sibling = [];
435
+ for (const period of presentation.periods) for (const track of period.tracks) {
436
+ if (!track.renditions.some((r) => r.id === renditionId)) continue;
437
+ for (const rendition of track.renditions) if (rendition.id === renditionId) previous = rendition.segments;
438
+ else if (Array.isArray(rendition.segments) && rendition.segments.length > 0) {
439
+ const candidate = rendition.segments;
440
+ const newest = (list) => {
441
+ var _list$seq, _list;
442
+ return (_list$seq = (_list = list[list.length - 1]) === null || _list === void 0 ? void 0 : _list.seq) !== null && _list$seq !== void 0 ? _list$seq : -1;
443
+ };
444
+ if (newest(candidate) > newest(sibling)) sibling = candidate;
445
+ }
446
+ }
435
447
  const knownLast = Array.isArray(previous) ? previous[previous.length - 1] : void 0;
436
448
  const incomingLast = playlist.segments[playlist.segments.length - 1];
437
- if (knownLast !== void 0 && incomingLast !== void 0 && !playlist.endlist && incomingLast.seq < knownLast.seq) return presentation;
438
- const shift = timelineShift(previous, playlist.segments);
449
+ if (knownLast !== void 0 && incomingLast !== void 0 && !playlist.endlist && incomingLast.seq < knownLast.seq) return null;
450
+ const shift = timelineShift(!playlist.endlist && knownLast === void 0 && sibling.length > 0 ? sibling : previous, playlist.segments);
439
451
  const segments = shift === 0 ? playlist.segments : playlist.segments.map((s) => _objectSpread2(_objectSpread2({}, s), {}, { start: s.start + shift }));
440
- const duration = segments.reduce((sum, s) => sum + s.duration, 0);
441
- const dateAnchor = playlist.dateAnchor !== void 0 ? {
452
+ return _objectSpread2(_objectSpread2(_objectSpread2({
453
+ type: "PLAYLIST_REFRESHED",
454
+ trackId: renditionId,
455
+ renditionId,
456
+ mediaSequence: playlist.mediaSequence,
457
+ segments
458
+ }, playlist.init !== null ? { init: playlist.init } : {}), {}, {
459
+ protection: playlist.protection,
460
+ endlist: playlist.endlist
461
+ }, playlist.endlist ? {} : { updatePeriod: playlist.targetDuration || 4 }), playlist.dateAnchor !== void 0 ? { dateAnchor: {
442
462
  wallClock: playlist.dateAnchor.wallClock,
443
463
  presentationTime: playlist.dateAnchor.presentationTime + shift
444
- } : void 0;
445
- const periods = presentation.periods.map((period) => _objectSpread2(_objectSpread2({}, period), {}, { tracks: period.tracks.map((track) => {
446
- var _track$protection;
447
- if (!track.renditions.some((r) => r.id === renditionId)) return track;
448
- return _objectSpread2(_objectSpread2({}, track), {}, {
449
- protection: (_track$protection = track.protection) !== null && _track$protection !== void 0 ? _track$protection : playlist.protection,
450
- renditions: track.renditions.map((rendition) => rendition.id === renditionId ? _objectSpread2(_objectSpread2({}, rendition), {}, { segments }, playlist.init !== null ? { init: playlist.init } : {}) : rendition)
451
- });
452
- }) }));
453
- const knownDuration = (_presentation$duratio = presentation.duration) !== null && _presentation$duratio !== void 0 ? _presentation$duratio : 0;
454
- return _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, presentation), {}, { isLive: !playlist.endlist }, playlist.endlist ? { duration: Math.max(knownDuration, duration) } : {}), playlist.endlist ? {} : { live: _objectSpread2(_objectSpread2({}, presentation.live), {}, { updatePeriod: playlist.targetDuration || 4 }, dateAnchor !== void 0 ? { dateAnchor } : {}) }), {}, { periods });
464
+ } } : {});
455
465
  }
456
466
  //#endregion
457
- export { mergePlaylist, normalizeAvcCodec, parse, parseMediaPlaylist };
467
+ export { normalizeAvcCodec, parse, parseMediaPlaylist, refreshFor };
@@ -0,0 +1,42 @@
1
+ import _objectSpread2 from "../../_virtual/_@oxc-project_runtime@0.150.0/helpers/esm/objectSpread2.js";
2
+ import { withDeadGroups } from "../../kernel/rendition-select.js";
3
+ //#region src/protocols/hls-cmaf/unavailable.ts
4
+ /**
5
+ * What a playlist that cannot be used means for playback. Pure; shared by
6
+ * hls-cmaf (a playlist that fails to load) and hls-live (a playlist that
7
+ * keeps failing to reload). Each keeps its own constraint source, because
8
+ * CONSTRAIN replaces a source's constraint and two slices writing one source
9
+ * in the same turn would erase each other's exclusions.
10
+ */
11
+ /** Renditions whose playlist failed to load (hls-cmaf). */
12
+ const LOAD_FAILED = "hls:unavailable";
13
+ /** Renditions whose playlist kept failing to reload (hls-live). */
14
+ const RELOAD_FAILED = "hls-live:unavailable";
15
+ /**
16
+ * The messages that stop relying on the renditions in `ids`, the whole list
17
+ * `source` excludes. A failed audio group takes the variants that require
18
+ * it, so arbitration moves to a variant on a working group and alt-audio
19
+ * follows. A CONSTRAIN when playback can go on; a fatal MANIFEST_FAILED
20
+ * when a video track has no rendition left, or when the active audio track
21
+ * has none and no variant names an audio group to move to.
22
+ */
23
+ function unavailableMessages(kernel, source, ids, error) {
24
+ var _kernel$quality$const, _kernel$quality$const2;
25
+ const presentation = kernel.presentation;
26
+ if (presentation === null) return [];
27
+ const other = source === "hls:unavailable" ? RELOAD_FAILED : LOAD_FAILED;
28
+ const excluded = withDeadGroups(presentation, /* @__PURE__ */ new Set([...ids, ...(_kernel$quality$const = (_kernel$quality$const2 = kernel.quality.constraints.get(other)) === null || _kernel$quality$const2 === void 0 ? void 0 : _kernel$quality$const2.excludeIds) !== null && _kernel$quality$const !== void 0 ? _kernel$quality$const : []]));
29
+ const grouped = presentation.couplings.some((c) => c.requires.audio !== void 0);
30
+ const activeAudio = kernel.tracks.active.get("audio");
31
+ if (presentation.periods.some((period) => period.tracks.some((track) => track.renditions.length > 0 && track.renditions.every((r) => excluded.has(r.id)) && (track.contentType === "video" || track.id === activeAudio && !grouped)))) return [{
32
+ type: "MANIFEST_FAILED",
33
+ error: _objectSpread2(_objectSpread2({}, error), {}, { fatal: true })
34
+ }];
35
+ return [{
36
+ type: "CONSTRAIN",
37
+ source,
38
+ constraint: { excludeIds: [...excluded] }
39
+ }];
40
+ }
41
+ //#endregion
42
+ export { LOAD_FAILED, RELOAD_FAILED, unavailableMessages };