@moqtap/codec 0.8.1 → 0.9.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 (127) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/README.md +16 -4
  3. package/dist/chunk-BMJMY6FX.cjs +868 -0
  4. package/dist/chunk-MAXEHYVB.cjs +2362 -0
  5. package/dist/{chunk-3YAR3I3S.js → chunk-Q7NSUXXB.js} +3 -1
  6. package/dist/{chunk-VWXUT5R2.cjs → chunk-TVSFDQYE.cjs} +3 -1
  7. package/dist/chunk-WX5PU7HF.js +868 -0
  8. package/dist/chunk-YJGOD3Y4.js +2362 -0
  9. package/dist/{codec-BLYAy2OP.d.ts → codec--CVeqGQv.d.ts} +1 -1
  10. package/dist/{codec-DujHf55Z.d.ts → codec-BJ7BHuiZ.d.ts} +1 -1
  11. package/dist/{codec-D47r_-Rl.d.ts → codec-BKsVu3Fr.d.ts} +1 -1
  12. package/dist/{codec-BNS3gS77.d.ts → codec-BMwgZIgG.d.ts} +1 -1
  13. package/dist/{codec-CI0BjmHc.d.ts → codec-BNy5syCU.d.ts} +1 -1
  14. package/dist/{codec-v6Vloc7H.d.ts → codec-BQa6Qsav.d.ts} +1 -1
  15. package/dist/codec-Bym-K2q2.d.cts +49 -0
  16. package/dist/{codec-D2JbI2tp.d.ts → codec-C006F75E.d.ts} +1 -1
  17. package/dist/codec-C5evFHL-.d.ts +49 -0
  18. package/dist/{codec-DQWQSam3.d.cts → codec-C6Mx4bwz.d.cts} +1 -1
  19. package/dist/{codec-DL0njGkL.d.ts → codec-CA6nbbEk.d.ts} +1 -1
  20. package/dist/{codec-D89DWXbv.d.cts → codec-CSBFdPgP.d.cts} +1 -1
  21. package/dist/{codec-DvcJgYKq.d.cts → codec-CXCjya_G.d.cts} +1 -1
  22. package/dist/{codec-hvkJfCF5.d.cts → codec-C_xxsbKy.d.cts} +1 -1
  23. package/dist/{codec-BOyrk_C9.d.cts → codec-Cfo_z5ET.d.cts} +1 -1
  24. package/dist/{codec-xeuGZY77.d.cts → codec-CgZdadPt.d.cts} +2 -2
  25. package/dist/{codec-WwwUipaU.d.ts → codec-Cl73EfRW.d.ts} +1 -1
  26. package/dist/{codec-BOtv3R5j.d.cts → codec-CtvFK4aV.d.cts} +1 -1
  27. package/dist/{codec-DAePx2NL.d.ts → codec-CviAa4lp.d.ts} +2 -2
  28. package/dist/{codec-DJmV04i0.d.ts → codec-D4RvFQmu.d.ts} +1 -1
  29. package/dist/{codec-cou9XBMF.d.cts → codec-DAyiF5sq.d.cts} +1 -1
  30. package/dist/{codec-CyApDKwf.d.cts → codec-DJmqoIsp.d.cts} +1 -1
  31. package/dist/{codec-NMsTK0GX.d.cts → codec-DchsPfr3.d.cts} +1 -1
  32. package/dist/{codec-DplRgIKq.d.ts → codec-Dl_QIXMz.d.ts} +1 -1
  33. package/dist/{codec-N3g1lxQQ.d.cts → codec-DzLvG8Wb.d.cts} +1 -1
  34. package/dist/{codec-D95gT98n.d.cts → codec-DzkdW4kO.d.cts} +1 -1
  35. package/dist/{codec-CWj5iTxA.d.ts → codec-O-1Js7h4.d.ts} +1 -1
  36. package/dist/{codec-BqqoYdh_.d.cts → codec-vddqy0KY.d.cts} +1 -1
  37. package/dist/draft07-session.d.cts +3 -3
  38. package/dist/draft07-session.d.ts +3 -3
  39. package/dist/draft07.d.cts +3 -3
  40. package/dist/draft07.d.ts +3 -3
  41. package/dist/draft08-session.d.cts +3 -3
  42. package/dist/draft08-session.d.ts +3 -3
  43. package/dist/draft08.d.cts +4 -4
  44. package/dist/draft08.d.ts +4 -4
  45. package/dist/draft09-session.d.cts +3 -3
  46. package/dist/draft09-session.d.ts +3 -3
  47. package/dist/draft09.d.cts +4 -4
  48. package/dist/draft09.d.ts +4 -4
  49. package/dist/draft10-session.d.cts +3 -3
  50. package/dist/draft10-session.d.ts +3 -3
  51. package/dist/draft10.d.cts +4 -4
  52. package/dist/draft10.d.ts +4 -4
  53. package/dist/draft11-session.d.cts +3 -3
  54. package/dist/draft11-session.d.ts +3 -3
  55. package/dist/draft11.d.cts +4 -4
  56. package/dist/draft11.d.ts +4 -4
  57. package/dist/draft12-session.d.cts +3 -3
  58. package/dist/draft12-session.d.ts +3 -3
  59. package/dist/draft12.d.cts +4 -4
  60. package/dist/draft12.d.ts +4 -4
  61. package/dist/draft13-session.d.cts +3 -3
  62. package/dist/draft13-session.d.ts +3 -3
  63. package/dist/draft13.d.cts +4 -4
  64. package/dist/draft13.d.ts +4 -4
  65. package/dist/draft14-session.d.cts +3 -3
  66. package/dist/draft14-session.d.ts +3 -3
  67. package/dist/draft14.d.cts +4 -4
  68. package/dist/draft14.d.ts +4 -4
  69. package/dist/draft15-session.d.cts +3 -3
  70. package/dist/draft15-session.d.ts +3 -3
  71. package/dist/draft15.d.cts +4 -4
  72. package/dist/draft15.d.ts +4 -4
  73. package/dist/draft16-session.d.cts +3 -3
  74. package/dist/draft16-session.d.ts +3 -3
  75. package/dist/draft16.d.cts +4 -4
  76. package/dist/draft16.d.ts +4 -4
  77. package/dist/draft17-session.d.cts +3 -3
  78. package/dist/draft17-session.d.ts +3 -3
  79. package/dist/draft17.d.cts +3 -3
  80. package/dist/draft17.d.ts +3 -3
  81. package/dist/draft18-session.d.cts +3 -3
  82. package/dist/draft18-session.d.ts +3 -3
  83. package/dist/draft18.d.cts +3 -3
  84. package/dist/draft18.d.ts +3 -3
  85. package/dist/draft19-session.d.cts +3 -3
  86. package/dist/draft19-session.d.ts +3 -3
  87. package/dist/draft19.cjs +2 -2
  88. package/dist/draft19.d.cts +3 -3
  89. package/dist/draft19.d.ts +3 -3
  90. package/dist/draft19.js +1 -1
  91. package/dist/draft20-session.cjs +8 -0
  92. package/dist/draft20-session.d.cts +195 -0
  93. package/dist/draft20-session.d.ts +195 -0
  94. package/dist/draft20-session.js +8 -0
  95. package/dist/draft20.cjs +217 -0
  96. package/dist/draft20.d.cts +175 -0
  97. package/dist/draft20.d.ts +175 -0
  98. package/dist/draft20.js +217 -0
  99. package/dist/index.cjs +9 -3
  100. package/dist/index.d.cts +32 -17
  101. package/dist/index.d.ts +32 -17
  102. package/dist/index.js +8 -2
  103. package/dist/{session-types-KsqyVp6y.d.cts → session-types-B9idsih-.d.cts} +13 -1
  104. package/dist/{session-types-C0l_-Ups.d.ts → session-types-Dn7b3RZM.d.ts} +13 -1
  105. package/dist/session.cjs +5 -1
  106. package/dist/session.d.cts +5 -3
  107. package/dist/session.d.ts +5 -3
  108. package/dist/session.js +5 -1
  109. package/dist/types-CWCQwuvy.d.cts +359 -0
  110. package/dist/types-CWCQwuvy.d.ts +359 -0
  111. package/dist/{types-CdCSIfvI.d.cts → types-DG5_iKuv.d.cts} +1 -1
  112. package/dist/{types-CdCSIfvI.d.ts → types-DG5_iKuv.d.ts} +1 -1
  113. package/package.json +16 -3
  114. package/src/core/session-types.ts +7 -0
  115. package/src/core/types.ts +1 -0
  116. package/src/drafts/draft19/codec.ts +4 -2
  117. package/src/drafts/draft20/codec.ts +1737 -0
  118. package/src/drafts/draft20/data-streams.ts +1291 -0
  119. package/src/drafts/draft20/error-codes.ts +135 -0
  120. package/src/drafts/draft20/index.ts +165 -0
  121. package/src/drafts/draft20/messages.ts +68 -0
  122. package/src/drafts/draft20/rules.ts +107 -0
  123. package/src/drafts/draft20/session-fsm.ts +1063 -0
  124. package/src/drafts/draft20/session.ts +21 -0
  125. package/src/drafts/draft20/types.ts +455 -0
  126. package/src/index.ts +20 -2
  127. package/src/session.ts +4 -0
@@ -0,0 +1,2362 @@
1
+ import {
2
+ DecodeError,
3
+ MoqtBufferReader,
4
+ MoqtBufferWriter,
5
+ bytesToHex,
6
+ hexToBytes
7
+ } from "./chunk-KLPWJU6W.js";
8
+
9
+ // src/drafts/draft20/messages.ts
10
+ var MSG_REQUEST_UPDATE = 0x02n;
11
+ var MSG_SUBSCRIBE = 0x03n;
12
+ var MSG_SUBSCRIBE_OK = 0x04n;
13
+ var MSG_REQUEST_ERROR = 0x05n;
14
+ var MSG_PUBLISH_NAMESPACE = 0x06n;
15
+ var MSG_REQUEST_OK = 0x07n;
16
+ var MSG_NAMESPACE = 0x08n;
17
+ var MSG_PUBLISH_DONE = 0x0bn;
18
+ var MSG_TRACK_STATUS = 0x0dn;
19
+ var MSG_NAMESPACE_DONE = 0x0en;
20
+ var MSG_PUBLISH_SKIPPED = 0x0fn;
21
+ var MSG_GOAWAY = 0x10n;
22
+ var MSG_FETCH = 0x16n;
23
+ var MSG_FETCH_OK = 0x18n;
24
+ var MSG_PUBLISH = 0x1dn;
25
+ var MSG_PUBLISH_STATE_NOTIFY = 0x22n;
26
+ var MSG_PUBLISH_OK = MSG_REQUEST_OK;
27
+ var MSG_SUBSCRIBE_NAMESPACE = 0x50n;
28
+ var MSG_SUBSCRIBE_TRACKS = 0x51n;
29
+ var MSG_SETUP = 0x2f00n;
30
+ var SETUP_OPT_PATH = 0x01n;
31
+ var SETUP_OPT_AUTHORIZATION_TOKEN = 0x03n;
32
+ var SETUP_OPT_MAX_AUTH_TOKEN_CACHE_SIZE = 0x04n;
33
+ var SETUP_OPT_AUTHORITY = 0x05n;
34
+ var SETUP_OPT_MAX_FILTER_RANGES = 0x06n;
35
+ var SETUP_OPT_MOQT_IMPLEMENTATION = 0x07n;
36
+ var SETUP_OPT_MAX_REQUEST_UPDATES = 0x08n;
37
+ var MESSAGE_TYPE_MAP = /* @__PURE__ */ new Map([
38
+ [MSG_SETUP, "setup"],
39
+ [MSG_SUBSCRIBE, "subscribe"],
40
+ [MSG_SUBSCRIBE_OK, "subscribe_ok"],
41
+ [MSG_REQUEST_UPDATE, "request_update"],
42
+ [MSG_PUBLISH_STATE_NOTIFY, "publish_state_notify"],
43
+ [MSG_PUBLISH, "publish"],
44
+ [MSG_PUBLISH_DONE, "publish_done"],
45
+ [MSG_PUBLISH_NAMESPACE, "publish_namespace"],
46
+ [MSG_NAMESPACE, "namespace"],
47
+ [MSG_NAMESPACE_DONE, "namespace_done"],
48
+ [MSG_SUBSCRIBE_NAMESPACE, "subscribe_namespace"],
49
+ [MSG_SUBSCRIBE_TRACKS, "subscribe_tracks"],
50
+ [MSG_PUBLISH_SKIPPED, "publish_skipped"],
51
+ [MSG_FETCH, "fetch"],
52
+ [MSG_FETCH_OK, "fetch_ok"],
53
+ [MSG_TRACK_STATUS, "track_status"],
54
+ [MSG_REQUEST_OK, "request_ok"],
55
+ [MSG_REQUEST_ERROR, "request_error"],
56
+ [MSG_GOAWAY, "goaway"]
57
+ ]);
58
+ var MESSAGE_ID_MAP = new Map(
59
+ [...MESSAGE_TYPE_MAP.entries()].map(([id, name]) => [name, id])
60
+ );
61
+
62
+ // src/drafts/draft20/data-streams.ts
63
+ var FETCH_STREAM_TYPE = 0x05n;
64
+ var OBJPROP_PRIOR_GROUP_ID_GAP = 0x3cn;
65
+ var OBJPROP_PRIOR_OBJECT_ID_GAP = 0x3en;
66
+ var KNOWN_OBJ_PROPS = /* @__PURE__ */ new Map([
67
+ [OBJPROP_PRIOR_OBJECT_ID_GAP, "prior_object_id_gap"],
68
+ [OBJPROP_PRIOR_GROUP_ID_GAP, "prior_group_id_gap"]
69
+ ]);
70
+ var FETCH_MARKER_NON_EXISTENT = 140;
71
+ var FETCH_MARKER_UNKNOWN = 268;
72
+ var FETCH_MARKER_TIMED_OUT = 524;
73
+ var FETCH_END_OF_RANGE_MARKERS = /* @__PURE__ */ new Set([
74
+ FETCH_MARKER_NON_EXISTENT,
75
+ FETCH_MARKER_UNKNOWN,
76
+ FETCH_MARKER_TIMED_OUT
77
+ ]);
78
+ function decodeObjectProperties(r, propsLength) {
79
+ const endOff = r.offset + propsLength;
80
+ const props = {};
81
+ let prevType = 0n;
82
+ while (r.offset < endOff) {
83
+ const delta = r.readVarInt();
84
+ const propType = prevType + delta;
85
+ prevType = propType;
86
+ if (propType % 2n === 0n) {
87
+ const value = r.readVarInt();
88
+ const name = KNOWN_OBJ_PROPS.get(propType) ?? `0x${propType.toString(16)}`;
89
+ props[name] = value;
90
+ } else {
91
+ const length = Number(r.readVarInt());
92
+ r.readBytes(length);
93
+ }
94
+ }
95
+ return props;
96
+ }
97
+ function encodeObjectProperties(props, w) {
98
+ const reverseMap = /* @__PURE__ */ new Map();
99
+ for (const [id, name] of KNOWN_OBJ_PROPS) {
100
+ reverseMap.set(name, id);
101
+ }
102
+ const entries = [];
103
+ for (const [name, value] of Object.entries(props)) {
104
+ const typeId = reverseMap.get(name);
105
+ if (typeId !== void 0) {
106
+ entries.push({ type: typeId, value });
107
+ }
108
+ }
109
+ entries.sort((a, b) => a.type < b.type ? -1 : a.type > b.type ? 1 : 0);
110
+ let prevType = 0n;
111
+ for (const entry of entries) {
112
+ w.writeVarInt(entry.type - prevType);
113
+ w.writeVarInt(entry.value);
114
+ prevType = entry.type;
115
+ }
116
+ }
117
+ function subgroupTypeFlagsError(typeFlags) {
118
+ if (typeFlags >= 128n) {
119
+ return `SUBGROUP_HEADER Type Flags of 128 or greater are invalid (got ${typeFlags})`;
120
+ }
121
+ const v = Number(typeFlags);
122
+ if ((v & 16) === 0) {
123
+ return `SUBGROUP_HEADER Type Flags 0x${v.toString(16)} must have bit 4 (0x10) set`;
124
+ }
125
+ if ((v & 6) === 6) {
126
+ return `SUBGROUP_HEADER Type Flags 0x${v.toString(16)} sets SUBGROUP_ID_MODE to the reserved 0b11`;
127
+ }
128
+ return null;
129
+ }
130
+ function isValidSubgroupType(streamType) {
131
+ return subgroupTypeFlagsError(BigInt(streamType)) === null;
132
+ }
133
+ var DATAGRAM_ALLOWED_BITS = 0x2fn;
134
+ function datagramTypeFlagsError(typeFlags) {
135
+ if ((typeFlags & 0x10n) !== 0n) {
136
+ return `datagram Type Flags bit 4 (0x10) is reserved and MUST be zero (got 0x${typeFlags.toString(16)})`;
137
+ }
138
+ if ((typeFlags & ~DATAGRAM_ALLOWED_BITS) !== 0n) {
139
+ return `datagram Type Flags 0x${typeFlags.toString(16)} sets a bit whose meaning is not specified`;
140
+ }
141
+ const v = Number(typeFlags);
142
+ if ((v & 32) !== 0 && (v & 2) !== 0) {
143
+ return `datagram Type Flags 0x${v.toString(16)} sets both the STATUS and END_OF_GROUP bits`;
144
+ }
145
+ return null;
146
+ }
147
+ var OBJECT_STATUS_NORMAL = 0n;
148
+ var MAX_U64 = 0xffffffffffffffffn;
149
+ function encodeSubgroupStream(stream) {
150
+ const w = new MoqtBufferWriter();
151
+ const streamType = stream.headerType;
152
+ w.writeVarInt(BigInt(streamType));
153
+ const propertiesPresent = (streamType & 1) !== 0;
154
+ const subgroupMode = (streamType & 6) >> 1;
155
+ const hasSubgroupField = subgroupMode === 2;
156
+ const hasPriority = (streamType & 32) === 0;
157
+ w.writeVarInt(stream.trackAlias);
158
+ w.writeVarInt(stream.groupId);
159
+ if (hasSubgroupField) {
160
+ w.writeVarInt(stream.subgroupId);
161
+ }
162
+ if (hasPriority) {
163
+ w.writeUint8(stream.publisherPriority);
164
+ }
165
+ let prevObjectId = -1n;
166
+ for (const obj of stream.objects) {
167
+ const delta = prevObjectId < 0n ? obj.objectId : obj.objectId - prevObjectId - 1n;
168
+ w.writeVarInt(delta);
169
+ if (propertiesPresent) {
170
+ if (obj.objectProperties && Object.keys(obj.objectProperties).length > 0) {
171
+ const tmpW = new MoqtBufferWriter(32);
172
+ encodeObjectProperties(obj.objectProperties, tmpW);
173
+ const raw = tmpW.finish();
174
+ w.writeVarInt(BigInt(raw.byteLength));
175
+ w.writeBytes(raw);
176
+ } else {
177
+ w.writeVarInt(0n);
178
+ }
179
+ }
180
+ w.writeVarInt(BigInt(obj.payloadLength));
181
+ if (obj.payloadLength === 0) {
182
+ w.writeVarInt(obj.status ?? OBJECT_STATUS_NORMAL);
183
+ } else {
184
+ w.writeBytes(obj.payload);
185
+ }
186
+ prevObjectId = obj.objectId;
187
+ }
188
+ return w.finish();
189
+ }
190
+ function encodeDatagram(dg) {
191
+ const w = new MoqtBufferWriter();
192
+ const dgType = dg.datagramType;
193
+ w.writeVarInt(BigInt(dgType));
194
+ w.writeVarInt(dg.trackAlias);
195
+ w.writeVarInt(dg.groupId);
196
+ const objectIdAbsent = (dgType & 4) !== 0;
197
+ const isStatus = (dgType & 32) !== 0;
198
+ const defaultPriority = (dgType & 8) !== 0;
199
+ const propertiesPresent = (dgType & 1) !== 0;
200
+ if (!objectIdAbsent) {
201
+ w.writeVarInt(dg.objectId);
202
+ }
203
+ if (!defaultPriority) {
204
+ w.writeUint8(dg.publisherPriority);
205
+ }
206
+ if (propertiesPresent) {
207
+ if (dg.objectProperties && Object.keys(dg.objectProperties).length > 0) {
208
+ const tmpW = new MoqtBufferWriter(32);
209
+ encodeObjectProperties(dg.objectProperties, tmpW);
210
+ const raw = tmpW.finish();
211
+ w.writeVarInt(BigInt(raw.byteLength));
212
+ w.writeBytes(raw);
213
+ } else {
214
+ w.writeVarInt(0n);
215
+ }
216
+ }
217
+ if (isStatus) {
218
+ w.writeVarInt(dg.objectStatus ?? OBJECT_STATUS_NORMAL);
219
+ } else {
220
+ w.writeBytes(dg.payload);
221
+ }
222
+ return w.finish();
223
+ }
224
+ function encodeFetchStream(stream) {
225
+ const w = new MoqtBufferWriter();
226
+ w.writeVarInt(FETCH_STREAM_TYPE);
227
+ w.writeVarInt(stream.requestId);
228
+ let prevGroupId = 0n;
229
+ let prevObjectId = 0n;
230
+ let first = true;
231
+ for (const obj of stream.objects) {
232
+ w.writeVarInt(BigInt(obj.serializationFlags));
233
+ const flags = obj.serializationFlags;
234
+ if (flags >= 128) {
235
+ if (first) w.writeVarInt(obj.groupId);
236
+ else w.writeVarInt(obj.groupId - prevGroupId - 1n);
237
+ w.writeVarInt(obj.objectId);
238
+ w.writeVarInt(BigInt(obj.payloadLength));
239
+ } else if (flags & 64) {
240
+ if (flags & 8) {
241
+ if (first) w.writeVarInt(obj.groupId);
242
+ else w.writeVarInt(obj.groupId - prevGroupId - 1n);
243
+ }
244
+ if (flags & 4) w.writeVarInt(objectIdDeltaFor(flags, obj.objectId, prevObjectId));
245
+ if (flags & 16) w.writeUint8(obj.publisherPriority);
246
+ if (flags & 32) {
247
+ if (obj.objectProperties && Object.keys(obj.objectProperties).length > 0) {
248
+ const tmpW = new MoqtBufferWriter(32);
249
+ encodeObjectProperties(obj.objectProperties, tmpW);
250
+ const raw = tmpW.finish();
251
+ w.writeVarInt(BigInt(raw.byteLength));
252
+ w.writeBytes(raw);
253
+ } else {
254
+ w.writeVarInt(0n);
255
+ }
256
+ }
257
+ w.writeVarInt(BigInt(obj.payloadLength));
258
+ if (obj.payloadLength > 0) {
259
+ w.writeBytes(obj.payload);
260
+ }
261
+ } else {
262
+ if (flags & 8) {
263
+ if (first) w.writeVarInt(obj.groupId);
264
+ else w.writeVarInt(obj.groupId - prevGroupId - 1n);
265
+ }
266
+ const subgroupEncoding = flags & 3;
267
+ if (subgroupEncoding === 3) w.writeVarInt(obj.subgroupId);
268
+ if (flags & 4) w.writeVarInt(objectIdDeltaFor(flags, obj.objectId, prevObjectId));
269
+ if (flags & 16) w.writeUint8(obj.publisherPriority);
270
+ if (flags & 32) {
271
+ if (obj.objectProperties && Object.keys(obj.objectProperties).length > 0) {
272
+ const tmpW = new MoqtBufferWriter(32);
273
+ encodeObjectProperties(obj.objectProperties, tmpW);
274
+ const raw = tmpW.finish();
275
+ w.writeVarInt(BigInt(raw.byteLength));
276
+ w.writeBytes(raw);
277
+ } else {
278
+ w.writeVarInt(0n);
279
+ }
280
+ }
281
+ w.writeVarInt(BigInt(obj.payloadLength));
282
+ if (obj.payloadLength > 0) {
283
+ w.writeBytes(obj.payload);
284
+ }
285
+ }
286
+ prevGroupId = obj.groupId;
287
+ prevObjectId = obj.objectId;
288
+ first = false;
289
+ }
290
+ return w.finish();
291
+ }
292
+ function objectIdDeltaFor(flags, objectId, prevObjectId) {
293
+ return (flags & 8) !== 0 ? objectId : objectId - prevObjectId;
294
+ }
295
+ function resolveObjectId(groupIdPresent, delta, prevObjectId) {
296
+ return groupIdPresent ? delta : prevObjectId + delta;
297
+ }
298
+ function resolveGroupId(first, delta, prevGroupId, offset) {
299
+ const groupId = first ? delta : prevGroupId + delta + 1n;
300
+ if (groupId > MAX_U64) {
301
+ throw new DecodeError("CONSTRAINT_VIOLATION", "computed Group ID exceeds 2^64 - 1", offset);
302
+ }
303
+ return groupId;
304
+ }
305
+ function decodeSubgroupStream(bytes) {
306
+ try {
307
+ const r = new MoqtBufferReader(bytes);
308
+ const typeFlags = r.readVarInt();
309
+ const flagsError = subgroupTypeFlagsError(typeFlags);
310
+ if (flagsError !== null) {
311
+ return { ok: false, error: new DecodeError("CONSTRAINT_VIOLATION", flagsError, 0) };
312
+ }
313
+ const streamType = Number(typeFlags);
314
+ const propertiesPresent = (streamType & 1) !== 0;
315
+ const subgroupMode = (streamType & 6) >> 1;
316
+ const hasSubgroupField = subgroupMode === 2;
317
+ const subgroupIsFirstObjId = subgroupMode === 1;
318
+ const endOfGroup = (streamType & 8) !== 0;
319
+ const hasPriority = (streamType & 32) === 0;
320
+ const firstObject = (streamType & 64) !== 0;
321
+ const trackAlias = r.readVarInt();
322
+ const groupId = r.readVarInt();
323
+ let subgroupId = 0n;
324
+ if (hasSubgroupField) {
325
+ subgroupId = r.readVarInt();
326
+ }
327
+ let publisherPriority = 128;
328
+ if (hasPriority) {
329
+ publisherPriority = r.readUint8();
330
+ }
331
+ const objects = [];
332
+ let prevObjectId = -1n;
333
+ let isFirst = true;
334
+ while (r.remaining > 0) {
335
+ const byteOffset = r.offset;
336
+ const delta = r.readVarInt();
337
+ let objectId;
338
+ if (isFirst) {
339
+ objectId = delta;
340
+ if (subgroupIsFirstObjId) {
341
+ subgroupId = objectId;
342
+ }
343
+ isFirst = false;
344
+ } else {
345
+ objectId = prevObjectId + 1n + delta;
346
+ }
347
+ const extensionData = new Uint8Array(0);
348
+ let objectProperties;
349
+ if (propertiesPresent) {
350
+ const propsLen = Number(r.readVarInt());
351
+ if (propsLen > 0) {
352
+ objectProperties = decodeObjectProperties(r, propsLen);
353
+ }
354
+ }
355
+ const payloadLength = Number(r.readVarInt());
356
+ let payload;
357
+ let status;
358
+ let payloadByteOffset;
359
+ if (payloadLength === 0) {
360
+ status = r.readVarInt();
361
+ payloadByteOffset = r.offset;
362
+ payload = new Uint8Array(0);
363
+ } else {
364
+ payloadByteOffset = r.offset;
365
+ payload = r.readBytesView(payloadLength);
366
+ }
367
+ const obj = {
368
+ type: "object",
369
+ byteOffset,
370
+ payloadByteOffset,
371
+ objectId,
372
+ objectIdDelta: delta,
373
+ payloadLength,
374
+ payload,
375
+ extensionData
376
+ };
377
+ if (status !== void 0) obj.status = status;
378
+ if (objectProperties !== void 0)
379
+ obj.objectProperties = objectProperties;
380
+ objects.push(obj);
381
+ prevObjectId = objectId;
382
+ }
383
+ const result = {
384
+ type: "subgroup",
385
+ headerType: streamType,
386
+ trackAlias,
387
+ groupId,
388
+ subgroupId,
389
+ publisherPriority,
390
+ objects
391
+ };
392
+ if (endOfGroup) result.endOfGroup = true;
393
+ if (firstObject) result.firstObject = true;
394
+ return {
395
+ ok: true,
396
+ value: result,
397
+ bytesRead: r.offset
398
+ };
399
+ } catch (e) {
400
+ if (e instanceof DecodeError) return { ok: false, error: e };
401
+ throw e;
402
+ }
403
+ }
404
+ function decodeDatagram(bytes) {
405
+ try {
406
+ const r = new MoqtBufferReader(bytes);
407
+ const typeFlags = r.readVarInt();
408
+ const flagsError = datagramTypeFlagsError(typeFlags);
409
+ if (flagsError !== null) {
410
+ return { ok: false, error: new DecodeError("CONSTRAINT_VIOLATION", flagsError, 0) };
411
+ }
412
+ const dgType = Number(typeFlags);
413
+ const objectIdAbsent = (dgType & 4) !== 0;
414
+ const endOfGroup = (dgType & 2) !== 0;
415
+ const isStatus = (dgType & 32) !== 0;
416
+ const defaultPriority = (dgType & 8) !== 0;
417
+ const propertiesPresent = (dgType & 1) !== 0;
418
+ const trackAlias = r.readVarInt();
419
+ const groupId = r.readVarInt();
420
+ let objectId = 0n;
421
+ if (!objectIdAbsent) {
422
+ objectId = r.readVarInt();
423
+ }
424
+ let publisherPriority = 128;
425
+ if (!defaultPriority) {
426
+ publisherPriority = r.readUint8();
427
+ }
428
+ let objectProperties;
429
+ if (propertiesPresent) {
430
+ const propsLen = Number(r.readVarInt());
431
+ if (propsLen === 0) {
432
+ return {
433
+ ok: false,
434
+ error: new DecodeError(
435
+ "CONSTRAINT_VIOLATION",
436
+ "a datagram with the PROPERTIES bit set must carry a non-empty Properties structure",
437
+ r.offset
438
+ )
439
+ };
440
+ }
441
+ objectProperties = decodeObjectProperties(r, propsLen);
442
+ }
443
+ let objectStatus;
444
+ let payload;
445
+ if (isStatus) {
446
+ objectStatus = r.readVarInt();
447
+ if (propertiesPresent && objectStatus !== OBJECT_STATUS_NORMAL) {
448
+ return {
449
+ ok: false,
450
+ error: new DecodeError(
451
+ "CONSTRAINT_VIOLATION",
452
+ `only a Normal (0x0) Object may carry Properties, got status 0x${objectStatus.toString(16)}`,
453
+ r.offset
454
+ )
455
+ };
456
+ }
457
+ if (r.remaining > 0) {
458
+ return {
459
+ ok: false,
460
+ error: new DecodeError(
461
+ "CONSTRAINT_VIOLATION",
462
+ `a status datagram must not carry a payload (${r.remaining} trailing byte(s))`,
463
+ r.offset
464
+ )
465
+ };
466
+ }
467
+ payload = new Uint8Array(0);
468
+ } else {
469
+ payload = r.readBytesView(r.remaining);
470
+ }
471
+ const payloadLength = payload.byteLength;
472
+ const result = {
473
+ type: "datagram",
474
+ datagramType: dgType,
475
+ trackAlias,
476
+ groupId,
477
+ objectId,
478
+ publisherPriority,
479
+ payloadLength,
480
+ payload
481
+ };
482
+ if (endOfGroup) result.endOfGroup = true;
483
+ if (objectStatus !== void 0)
484
+ result.objectStatus = objectStatus;
485
+ if (objectProperties !== void 0)
486
+ result.objectProperties = objectProperties;
487
+ return { ok: true, value: result, bytesRead: r.offset };
488
+ } catch (e) {
489
+ if (e instanceof DecodeError) return { ok: false, error: e };
490
+ throw e;
491
+ }
492
+ }
493
+ function decodeFetchStream(bytes) {
494
+ try {
495
+ const r = new MoqtBufferReader(bytes);
496
+ const streamType = r.readVarInt();
497
+ if (streamType !== FETCH_STREAM_TYPE) {
498
+ return {
499
+ ok: false,
500
+ error: new DecodeError(
501
+ "CONSTRAINT_VIOLATION",
502
+ `Expected fetch stream type 0x05, got 0x${streamType.toString(16)}`,
503
+ 0
504
+ )
505
+ };
506
+ }
507
+ const requestId = r.readVarInt();
508
+ const objects = [];
509
+ let prevGroupId = 0n;
510
+ let prevSubgroupId = 0n;
511
+ let prevObjectId = 0n;
512
+ let prevPriority = 128;
513
+ let first = true;
514
+ while (r.remaining > 0) {
515
+ const byteOffset = r.offset;
516
+ const flags = Number(r.readVarInt());
517
+ let groupId = prevGroupId;
518
+ let subgroupId = prevSubgroupId;
519
+ let objectId = prevObjectId + 1n;
520
+ let payloadLength;
521
+ let payload;
522
+ let payloadByteOffset;
523
+ const extensionData = new Uint8Array(0);
524
+ if (flags >= 128) {
525
+ if (!FETCH_END_OF_RANGE_MARKERS.has(flags)) {
526
+ return {
527
+ ok: false,
528
+ error: new DecodeError(
529
+ "CONSTRAINT_VIOLATION",
530
+ `0x${flags.toString(16)} is neither a flag combination below 128 nor one of the three End of Range markers`,
531
+ r.offset
532
+ )
533
+ };
534
+ }
535
+ const groupDelta2 = r.readVarInt();
536
+ groupId = resolveGroupId(first, groupDelta2, prevGroupId, r.offset);
537
+ const objectDelta2 = r.readVarInt();
538
+ objectId = objectDelta2;
539
+ payloadLength = Number(r.readVarInt());
540
+ payloadByteOffset = r.offset;
541
+ payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
542
+ const marker = {
543
+ type: "object",
544
+ byteOffset,
545
+ payloadByteOffset,
546
+ serializationFlags: flags,
547
+ groupId,
548
+ groupIdDelta: groupDelta2,
549
+ // A marker has no Subgroup ID or Priority of its own — Section
550
+ // 11.4.4.2 says neither field is present. The interface requires
551
+ // both, so they carry the last actual Object's values, which is also
552
+ // what a following Object that references "the prior Object" gets.
553
+ subgroupId: prevSubgroupId,
554
+ objectId,
555
+ objectIdDelta: objectDelta2,
556
+ publisherPriority: prevPriority,
557
+ payloadLength,
558
+ payload,
559
+ extensionData
560
+ };
561
+ objects.push(marker);
562
+ prevGroupId = groupId;
563
+ prevObjectId = objectId;
564
+ first = false;
565
+ continue;
566
+ }
567
+ if (flags & 64) {
568
+ const objectIdPresent2 = (flags & 4) !== 0;
569
+ const groupIdPresent2 = (flags & 8) !== 0;
570
+ const priorityPresent2 = (flags & 16) !== 0;
571
+ const propsPresent2 = (flags & 32) !== 0;
572
+ let groupDelta2;
573
+ if (groupIdPresent2) {
574
+ groupDelta2 = r.readVarInt();
575
+ groupId = resolveGroupId(first, groupDelta2, prevGroupId, r.offset);
576
+ }
577
+ let objectDelta2;
578
+ if (objectIdPresent2) {
579
+ objectDelta2 = r.readVarInt();
580
+ objectId = resolveObjectId(groupIdPresent2, objectDelta2, prevObjectId);
581
+ }
582
+ if (objectId > MAX_U64) {
583
+ return {
584
+ ok: false,
585
+ error: new DecodeError(
586
+ "CONSTRAINT_VIOLATION",
587
+ "computed Object ID exceeds 2^64 - 1",
588
+ r.offset
589
+ )
590
+ };
591
+ }
592
+ if (priorityPresent2) {
593
+ prevPriority = r.readUint8();
594
+ }
595
+ let objectProperties2;
596
+ if (propsPresent2) {
597
+ const propsLen = Number(r.readVarInt());
598
+ if (propsLen > 0) {
599
+ objectProperties2 = decodeObjectProperties(r, propsLen);
600
+ }
601
+ }
602
+ payloadLength = Number(r.readVarInt());
603
+ payloadByteOffset = r.offset;
604
+ payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
605
+ const obj2 = {
606
+ type: "object",
607
+ byteOffset,
608
+ payloadByteOffset,
609
+ serializationFlags: flags,
610
+ groupId,
611
+ subgroupId: 0n,
612
+ objectId,
613
+ publisherPriority: prevPriority,
614
+ payloadLength,
615
+ payload,
616
+ extensionData
617
+ };
618
+ if (groupDelta2 !== void 0)
619
+ obj2.groupIdDelta = groupDelta2;
620
+ if (objectDelta2 !== void 0)
621
+ obj2.objectIdDelta = objectDelta2;
622
+ if (objectProperties2)
623
+ obj2.objectProperties = objectProperties2;
624
+ objects.push(obj2);
625
+ prevGroupId = groupId;
626
+ prevObjectId = objectId;
627
+ first = false;
628
+ continue;
629
+ }
630
+ const subgroupEncoding = flags & 3;
631
+ const objectIdPresent = (flags & 4) !== 0;
632
+ const groupIdPresent = (flags & 8) !== 0;
633
+ const priorityPresent = (flags & 16) !== 0;
634
+ const propsPresent = (flags & 32) !== 0;
635
+ let groupDelta;
636
+ if (groupIdPresent) {
637
+ groupDelta = r.readVarInt();
638
+ groupId = resolveGroupId(first, groupDelta, prevGroupId, r.offset);
639
+ } else if (first) {
640
+ return {
641
+ ok: false,
642
+ error: new DecodeError(
643
+ "CONSTRAINT_VIOLATION",
644
+ "First fetch object must include groupId",
645
+ r.offset
646
+ )
647
+ };
648
+ }
649
+ if (subgroupEncoding === 0) {
650
+ subgroupId = 0n;
651
+ } else if (subgroupEncoding === 1 || subgroupEncoding === 2) {
652
+ if (first) {
653
+ return {
654
+ ok: false,
655
+ error: new DecodeError(
656
+ "CONSTRAINT_VIOLATION",
657
+ "First fetch object cannot reference prior subgroupId",
658
+ r.offset
659
+ )
660
+ };
661
+ }
662
+ subgroupId = subgroupEncoding === 1 ? prevSubgroupId : prevSubgroupId + 1n;
663
+ } else {
664
+ subgroupId = r.readVarInt();
665
+ }
666
+ let objectDelta;
667
+ if (objectIdPresent) {
668
+ objectDelta = r.readVarInt();
669
+ objectId = resolveObjectId(groupIdPresent, objectDelta, prevObjectId);
670
+ } else if (first) {
671
+ return {
672
+ ok: false,
673
+ error: new DecodeError(
674
+ "CONSTRAINT_VIOLATION",
675
+ "First fetch object must include objectId",
676
+ r.offset
677
+ )
678
+ };
679
+ }
680
+ if (objectId > MAX_U64) {
681
+ return {
682
+ ok: false,
683
+ error: new DecodeError(
684
+ "CONSTRAINT_VIOLATION",
685
+ "computed Object ID exceeds 2^64 - 1",
686
+ r.offset
687
+ )
688
+ };
689
+ }
690
+ if (priorityPresent) {
691
+ prevPriority = r.readUint8();
692
+ }
693
+ let objectProperties;
694
+ if (propsPresent) {
695
+ const propsLen = Number(r.readVarInt());
696
+ if (propsLen > 0) {
697
+ objectProperties = decodeObjectProperties(r, propsLen);
698
+ }
699
+ }
700
+ payloadLength = Number(r.readVarInt());
701
+ payloadByteOffset = r.offset;
702
+ payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
703
+ const obj = {
704
+ type: "object",
705
+ byteOffset,
706
+ payloadByteOffset,
707
+ serializationFlags: flags,
708
+ groupId,
709
+ subgroupId,
710
+ objectId,
711
+ publisherPriority: prevPriority,
712
+ payloadLength,
713
+ payload,
714
+ extensionData
715
+ };
716
+ if (groupDelta !== void 0)
717
+ obj.groupIdDelta = groupDelta;
718
+ if (objectDelta !== void 0)
719
+ obj.objectIdDelta = objectDelta;
720
+ if (objectProperties)
721
+ obj.objectProperties = objectProperties;
722
+ objects.push(obj);
723
+ prevGroupId = groupId;
724
+ prevSubgroupId = subgroupId;
725
+ prevObjectId = objectId;
726
+ first = false;
727
+ }
728
+ return {
729
+ ok: true,
730
+ value: { type: "fetch", requestId, objects },
731
+ bytesRead: r.offset
732
+ };
733
+ } catch (e) {
734
+ if (e instanceof DecodeError) return { ok: false, error: e };
735
+ throw e;
736
+ }
737
+ }
738
+ function decodeDataStream(streamType, bytes) {
739
+ switch (streamType) {
740
+ case "subgroup":
741
+ return decodeSubgroupStream(bytes);
742
+ case "datagram":
743
+ return decodeDatagram(bytes);
744
+ case "fetch":
745
+ return decodeFetchStream(bytes);
746
+ default: {
747
+ const _exhaustive = streamType;
748
+ throw new Error(`Unknown stream type: ${_exhaustive}`);
749
+ }
750
+ }
751
+ }
752
+ function createSubgroupStreamDecoder() {
753
+ let buffer = new Uint8Array(0);
754
+ let offset = 0;
755
+ let headerEmitted = false;
756
+ let prevObjectId = -1n;
757
+ let firstObject = true;
758
+ let _propertiesPresent = false;
759
+ return new TransformStream({
760
+ transform(chunk, controller) {
761
+ if (offset > 0) {
762
+ buffer = buffer.subarray(offset);
763
+ offset = 0;
764
+ }
765
+ const newBuffer = new Uint8Array(buffer.length + chunk.length);
766
+ newBuffer.set(buffer, 0);
767
+ newBuffer.set(chunk, buffer.length);
768
+ buffer = newBuffer;
769
+ if (!headerEmitted) {
770
+ try {
771
+ const r = new MoqtBufferReader(buffer.subarray(offset));
772
+ const typeFlags = r.readVarInt();
773
+ const flagsError = subgroupTypeFlagsError(typeFlags);
774
+ if (flagsError !== null) {
775
+ controller.error(new DecodeError("CONSTRAINT_VIOLATION", flagsError, 0));
776
+ return;
777
+ }
778
+ const streamType = Number(typeFlags);
779
+ _propertiesPresent = (streamType & 1) !== 0;
780
+ const subgroupMode = (streamType & 6) >> 1;
781
+ const hasSubgroupField = subgroupMode === 2;
782
+ const hasPriority = (streamType & 32) === 0;
783
+ const trackAlias = r.readVarInt();
784
+ const groupId = r.readVarInt();
785
+ let subgroupId = 0n;
786
+ if (hasSubgroupField) {
787
+ subgroupId = r.readVarInt();
788
+ }
789
+ let publisherPriority = 128;
790
+ if (hasPriority) {
791
+ publisherPriority = r.readUint8();
792
+ }
793
+ controller.enqueue({
794
+ type: "subgroup_header",
795
+ trackAlias,
796
+ groupId,
797
+ subgroupId,
798
+ publisherPriority
799
+ });
800
+ headerEmitted = true;
801
+ offset += r.offset;
802
+ } catch (e) {
803
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
804
+ return;
805
+ }
806
+ controller.error(e);
807
+ return;
808
+ }
809
+ }
810
+ while (offset < buffer.length) {
811
+ try {
812
+ const r = new MoqtBufferReader(buffer.subarray(offset));
813
+ const delta = r.readVarInt();
814
+ let objectId;
815
+ if (firstObject) {
816
+ objectId = delta;
817
+ firstObject = false;
818
+ } else {
819
+ objectId = prevObjectId + 1n + delta;
820
+ }
821
+ let extensionData = new Uint8Array(0);
822
+ if (_propertiesPresent) {
823
+ const extLen = Number(r.readVarInt());
824
+ extensionData = extLen > 0 ? r.readBytesView(extLen) : new Uint8Array(0);
825
+ }
826
+ const payloadLength = Number(r.readVarInt());
827
+ const payloadByteOffset = r.offset;
828
+ const payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
829
+ controller.enqueue({
830
+ type: "object",
831
+ objectId,
832
+ objectIdDelta: delta,
833
+ payloadLength,
834
+ payload,
835
+ extensionData,
836
+ byteOffset: 0,
837
+ payloadByteOffset
838
+ });
839
+ offset += r.offset;
840
+ prevObjectId = objectId;
841
+ } catch (e) {
842
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
843
+ break;
844
+ }
845
+ controller.error(e);
846
+ return;
847
+ }
848
+ }
849
+ },
850
+ flush(controller) {
851
+ if (offset < buffer.length) {
852
+ controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
853
+ }
854
+ }
855
+ });
856
+ }
857
+ function createFetchStreamDecoder() {
858
+ let buffer = new Uint8Array(0);
859
+ let offset = 0;
860
+ let headerEmitted = false;
861
+ return new TransformStream({
862
+ transform(chunk, controller) {
863
+ if (offset > 0) {
864
+ buffer = buffer.subarray(offset);
865
+ offset = 0;
866
+ }
867
+ const newBuffer = new Uint8Array(buffer.length + chunk.length);
868
+ newBuffer.set(buffer, 0);
869
+ newBuffer.set(chunk, buffer.length);
870
+ buffer = newBuffer;
871
+ if (!headerEmitted) {
872
+ try {
873
+ const r = new MoqtBufferReader(buffer.subarray(offset));
874
+ const streamType = r.readVarInt();
875
+ if (streamType !== FETCH_STREAM_TYPE) {
876
+ controller.error(
877
+ new DecodeError(
878
+ "CONSTRAINT_VIOLATION",
879
+ `Expected fetch stream type 0x05, got 0x${streamType.toString(16)}`,
880
+ 0
881
+ )
882
+ );
883
+ return;
884
+ }
885
+ const requestId = r.readVarInt();
886
+ controller.enqueue({ type: "fetch_header", requestId });
887
+ headerEmitted = true;
888
+ offset += r.offset;
889
+ } catch (e) {
890
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
891
+ return;
892
+ }
893
+ controller.error(e);
894
+ return;
895
+ }
896
+ }
897
+ while (offset < buffer.length) {
898
+ try {
899
+ const r = new MoqtBufferReader(buffer.subarray(offset));
900
+ const flags = Number(r.readVarInt());
901
+ if (flags >= 128) {
902
+ if (!FETCH_END_OF_RANGE_MARKERS.has(flags)) {
903
+ controller.error(
904
+ new DecodeError(
905
+ "CONSTRAINT_VIOLATION",
906
+ `0x${flags.toString(16)} is neither a flag combination below 128 nor one of the three End of Range markers`,
907
+ 0
908
+ )
909
+ );
910
+ return;
911
+ }
912
+ r.readVarInt();
913
+ const objectDelta = r.readVarInt();
914
+ const markerPayloadLength = Number(r.readVarInt());
915
+ const markerPayloadByteOffset = r.offset;
916
+ controller.enqueue({
917
+ type: "object",
918
+ objectId: objectDelta,
919
+ objectIdDelta: objectDelta,
920
+ payloadLength: markerPayloadLength,
921
+ payload: new Uint8Array(0),
922
+ extensionData: new Uint8Array(0),
923
+ byteOffset: 0,
924
+ payloadByteOffset: markerPayloadByteOffset
925
+ });
926
+ offset += r.offset;
927
+ continue;
928
+ }
929
+ const objectIdPresent = (flags & 4) !== 0;
930
+ const groupIdPresent = (flags & 8) !== 0;
931
+ const priorityPresent = (flags & 16) !== 0;
932
+ const extensionsPresent = (flags & 32) !== 0;
933
+ const subgroupEncoding = flags & 3;
934
+ const datagramMode = (flags & 64) !== 0;
935
+ if (groupIdPresent) r.readVarInt();
936
+ if (!datagramMode && subgroupEncoding === 3) r.readVarInt();
937
+ let objectId = 0n;
938
+ let objectIdDelta;
939
+ if (objectIdPresent) {
940
+ objectIdDelta = r.readVarInt();
941
+ objectId = objectIdDelta;
942
+ }
943
+ if (priorityPresent) r.readUint8();
944
+ let extensionData = new Uint8Array(0);
945
+ if (extensionsPresent) {
946
+ const extLen = Number(r.readVarInt());
947
+ extensionData = extLen > 0 ? r.readBytesView(extLen) : new Uint8Array(0);
948
+ }
949
+ const payloadLength = Number(r.readVarInt());
950
+ const payloadByteOffset = r.offset;
951
+ const payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
952
+ const event = {
953
+ type: "object",
954
+ objectId,
955
+ payloadLength,
956
+ payload,
957
+ extensionData,
958
+ byteOffset: 0,
959
+ payloadByteOffset
960
+ };
961
+ if (objectIdDelta !== void 0)
962
+ event.objectIdDelta = objectIdDelta;
963
+ controller.enqueue(event);
964
+ offset += r.offset;
965
+ } catch (e) {
966
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
967
+ break;
968
+ }
969
+ controller.error(e);
970
+ return;
971
+ }
972
+ }
973
+ },
974
+ flush(controller) {
975
+ if (offset < buffer.length) {
976
+ controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
977
+ }
978
+ }
979
+ });
980
+ }
981
+ function createDataStreamDecoder() {
982
+ let buffer = new Uint8Array(0);
983
+ let offset = 0;
984
+ let inner = null;
985
+ return new TransformStream({
986
+ transform(chunk, controller) {
987
+ if (offset > 0) {
988
+ buffer = buffer.subarray(offset);
989
+ offset = 0;
990
+ }
991
+ const newBuffer = new Uint8Array(buffer.length + chunk.length);
992
+ newBuffer.set(buffer, 0);
993
+ newBuffer.set(chunk, buffer.length);
994
+ buffer = newBuffer;
995
+ if (inner === null) {
996
+ if (offset >= buffer.length) return;
997
+ const firstByte = buffer[offset];
998
+ if (isValidSubgroupType(firstByte)) {
999
+ const decoder = createSubgroupStreamDecoder();
1000
+ inner = decoder;
1001
+ } else if (firstByte === 5) {
1002
+ const decoder = createFetchStreamDecoder();
1003
+ inner = decoder;
1004
+ } else {
1005
+ controller.error(
1006
+ new DecodeError(
1007
+ "CONSTRAINT_VIOLATION",
1008
+ `Unknown data stream type: 0x${firstByte.toString(16)}`,
1009
+ 0
1010
+ )
1011
+ );
1012
+ return;
1013
+ }
1014
+ }
1015
+ },
1016
+ flush(controller) {
1017
+ if (offset >= buffer.length) return;
1018
+ const view = buffer.subarray(offset);
1019
+ const firstByte = view[0];
1020
+ let result;
1021
+ if (isValidSubgroupType(firstByte)) {
1022
+ result = decodeSubgroupStream(view);
1023
+ } else if (firstByte === 5) {
1024
+ result = decodeFetchStream(view);
1025
+ } else {
1026
+ controller.error(
1027
+ new DecodeError(
1028
+ "CONSTRAINT_VIOLATION",
1029
+ `Unknown data stream type: 0x${firstByte.toString(16)}`,
1030
+ 0
1031
+ )
1032
+ );
1033
+ return;
1034
+ }
1035
+ if (!result.ok) {
1036
+ controller.error(result.error);
1037
+ return;
1038
+ }
1039
+ const stream = result.value;
1040
+ if (stream.type === "subgroup") {
1041
+ controller.enqueue({
1042
+ type: "subgroup_header",
1043
+ trackAlias: stream.trackAlias,
1044
+ groupId: stream.groupId,
1045
+ subgroupId: stream.subgroupId,
1046
+ publisherPriority: stream.publisherPriority
1047
+ });
1048
+ for (const obj of stream.objects) {
1049
+ controller.enqueue(obj);
1050
+ }
1051
+ } else if (stream.type === "fetch") {
1052
+ controller.enqueue({
1053
+ type: "fetch_header",
1054
+ requestId: stream.requestId
1055
+ });
1056
+ for (const obj of stream.objects) {
1057
+ controller.enqueue(obj);
1058
+ }
1059
+ }
1060
+ }
1061
+ });
1062
+ }
1063
+
1064
+ // src/drafts/draft20/codec.ts
1065
+ var textEncoder = /* @__PURE__ */ new TextEncoder();
1066
+ var textDecoder = /* @__PURE__ */ new TextDecoder();
1067
+ var REQUEST_ERROR_REDIRECT = 0x34n;
1068
+ var MAX_U642 = 0xffffffffffffffffn;
1069
+ var UNKNOWN_STREAM_COUNT = 0xffffffffffffffffn;
1070
+ function encodeSetupOptions(opts, writer) {
1071
+ const entries = [];
1072
+ if (opts.path !== void 0) {
1073
+ entries.push({
1074
+ type: SETUP_OPT_PATH,
1075
+ encode: (w) => {
1076
+ const encoded = textEncoder.encode(opts.path);
1077
+ w.writeVarInt(BigInt(encoded.byteLength));
1078
+ w.writeBytes(encoded);
1079
+ }
1080
+ });
1081
+ }
1082
+ if (opts.authorization_token !== void 0) {
1083
+ entries.push({
1084
+ type: SETUP_OPT_AUTHORIZATION_TOKEN,
1085
+ encode: (w) => {
1086
+ const tmpW = new MoqtBufferWriter(64);
1087
+ encodeAuthorizationToken(opts.authorization_token, tmpW);
1088
+ const raw = tmpW.finish();
1089
+ w.writeVarInt(BigInt(raw.byteLength));
1090
+ w.writeBytes(raw);
1091
+ }
1092
+ });
1093
+ }
1094
+ if (opts.max_auth_token_cache_size !== void 0) {
1095
+ entries.push({
1096
+ type: SETUP_OPT_MAX_AUTH_TOKEN_CACHE_SIZE,
1097
+ encode: (w) => w.writeVarInt(opts.max_auth_token_cache_size)
1098
+ });
1099
+ }
1100
+ if (opts.authority !== void 0) {
1101
+ entries.push({
1102
+ type: SETUP_OPT_AUTHORITY,
1103
+ encode: (w) => {
1104
+ const encoded = textEncoder.encode(opts.authority);
1105
+ w.writeVarInt(BigInt(encoded.byteLength));
1106
+ w.writeBytes(encoded);
1107
+ }
1108
+ });
1109
+ }
1110
+ if (opts.max_filter_ranges !== void 0) {
1111
+ entries.push({
1112
+ type: SETUP_OPT_MAX_FILTER_RANGES,
1113
+ encode: (w) => w.writeVarInt(opts.max_filter_ranges)
1114
+ });
1115
+ }
1116
+ if (opts.moqt_implementation !== void 0) {
1117
+ entries.push({
1118
+ type: SETUP_OPT_MOQT_IMPLEMENTATION,
1119
+ encode: (w) => {
1120
+ const encoded = textEncoder.encode(opts.moqt_implementation);
1121
+ w.writeVarInt(BigInt(encoded.byteLength));
1122
+ w.writeBytes(encoded);
1123
+ }
1124
+ });
1125
+ }
1126
+ if (opts.max_request_updates !== void 0) {
1127
+ entries.push({
1128
+ type: SETUP_OPT_MAX_REQUEST_UPDATES,
1129
+ encode: (w) => w.writeVarInt(opts.max_request_updates)
1130
+ });
1131
+ }
1132
+ if (opts.unknown) {
1133
+ for (const u of opts.unknown) {
1134
+ const id = BigInt(u.id);
1135
+ entries.push({
1136
+ type: id,
1137
+ encode: (w) => {
1138
+ if (id % 2n === 0n) {
1139
+ const raw = hexToBytes(u.raw_hex);
1140
+ const tmpReader = new MoqtBufferReader(raw);
1141
+ w.writeVarInt(tmpReader.readVarInt());
1142
+ } else {
1143
+ const raw = hexToBytes(u.raw_hex);
1144
+ w.writeVarInt(BigInt(raw.byteLength));
1145
+ w.writeBytes(raw);
1146
+ }
1147
+ }
1148
+ });
1149
+ }
1150
+ }
1151
+ entries.sort((a, b) => a.type < b.type ? -1 : a.type > b.type ? 1 : 0);
1152
+ let prevType = 0n;
1153
+ for (const entry of entries) {
1154
+ writer.writeVarInt(entry.type - prevType);
1155
+ entry.encode(writer);
1156
+ prevType = entry.type;
1157
+ }
1158
+ }
1159
+ function decodeSetupOptions(reader, payloadEnd) {
1160
+ const result = {};
1161
+ const unknown = [];
1162
+ let prevType = 0n;
1163
+ while (reader.offset < payloadEnd) {
1164
+ const delta = reader.readVarInt();
1165
+ const optType = prevType + delta;
1166
+ prevType = optType;
1167
+ if (optType % 2n === 0n) {
1168
+ const value = reader.readVarInt();
1169
+ if (optType === SETUP_OPT_MAX_AUTH_TOKEN_CACHE_SIZE) {
1170
+ result.max_auth_token_cache_size = value;
1171
+ } else if (optType === SETUP_OPT_MAX_FILTER_RANGES) {
1172
+ result.max_filter_ranges = value;
1173
+ } else if (optType === SETUP_OPT_MAX_REQUEST_UPDATES) {
1174
+ result.max_request_updates = value;
1175
+ } else {
1176
+ const tmpWriter = new MoqtBufferWriter(16);
1177
+ tmpWriter.writeVarInt(value);
1178
+ const raw = tmpWriter.finish();
1179
+ unknown.push({
1180
+ id: `0x${optType.toString(16)}`,
1181
+ length: raw.byteLength,
1182
+ raw_hex: bytesToHex(raw)
1183
+ });
1184
+ }
1185
+ } else {
1186
+ const length = Number(reader.readVarInt());
1187
+ const bytes = reader.readBytes(length);
1188
+ if (optType === SETUP_OPT_PATH) {
1189
+ result.path = textDecoder.decode(bytes);
1190
+ } else if (optType === SETUP_OPT_AUTHORIZATION_TOKEN) {
1191
+ result.authorization_token = decodeAuthorizationToken(new MoqtBufferReader(bytes));
1192
+ } else if (optType === SETUP_OPT_AUTHORITY) {
1193
+ result.authority = textDecoder.decode(bytes);
1194
+ } else if (optType === SETUP_OPT_MOQT_IMPLEMENTATION) {
1195
+ result.moqt_implementation = textDecoder.decode(bytes);
1196
+ } else {
1197
+ unknown.push({
1198
+ id: `0x${optType.toString(16)}`,
1199
+ length,
1200
+ raw_hex: bytesToHex(bytes)
1201
+ });
1202
+ }
1203
+ }
1204
+ }
1205
+ if (unknown.length > 0) result.unknown = unknown;
1206
+ return result;
1207
+ }
1208
+ function encodeAuthorizationToken(token, w) {
1209
+ const aliasType = Number(token.alias_type);
1210
+ w.writeVarInt(token.alias_type);
1211
+ if (aliasType === 0 || aliasType === 1 || aliasType === 2) {
1212
+ w.writeVarInt(token.token_alias ?? 0n);
1213
+ }
1214
+ if (aliasType === 1 || aliasType === 3) {
1215
+ w.writeVarInt(token.token_type ?? 0n);
1216
+ const tv = token.token_value ?? new Uint8Array(0);
1217
+ w.writeBytes(tv);
1218
+ }
1219
+ }
1220
+ function decodeAuthorizationToken(r) {
1221
+ const alias_type = r.readVarInt();
1222
+ const aliasType = Number(alias_type);
1223
+ const result = { alias_type };
1224
+ if (aliasType === 0 || aliasType === 1 || aliasType === 2) {
1225
+ ;
1226
+ result.token_alias = r.readVarInt();
1227
+ }
1228
+ if (aliasType === 1 || aliasType === 3) {
1229
+ ;
1230
+ result.token_type = r.readVarInt();
1231
+ const bytes = r.readBytes(r.remaining);
1232
+ result.token_value = bytes;
1233
+ }
1234
+ return result;
1235
+ }
1236
+ function encodeNamespaceTuple(ns, w) {
1237
+ w.writeVarInt(BigInt(ns.length));
1238
+ for (const part of ns) {
1239
+ const encoded = textEncoder.encode(part);
1240
+ w.writeVarInt(BigInt(encoded.byteLength));
1241
+ w.writeBytes(encoded);
1242
+ }
1243
+ }
1244
+ function decodeNamespaceTuple(r) {
1245
+ const count = Number(r.readVarInt());
1246
+ const parts = [];
1247
+ for (let i = 0; i < count; i++) {
1248
+ const len = Number(r.readVarInt());
1249
+ parts.push(textDecoder.decode(r.readBytes(len)));
1250
+ }
1251
+ return parts;
1252
+ }
1253
+ var PARAM_OBJECT_DELIVERY_TIMEOUT = 0x02n;
1254
+ var PARAM_AUTHORIZATION_TOKEN = 0x03n;
1255
+ var PARAM_RENDEZVOUS_TIMEOUT = 0x04n;
1256
+ var PARAM_SUBGROUP_DELIVERY_TIMEOUT = 0x06n;
1257
+ var PARAM_EXPIRES = 0x08n;
1258
+ var PARAM_LARGEST_OBJECT = 0x09n;
1259
+ var PARAM_FILL_TIMEOUT = 0x0an;
1260
+ var PARAM_FORWARD = 0x10n;
1261
+ var PARAM_SUBSCRIBER_PRIORITY = 0x20n;
1262
+ var PARAM_LOCATION_FILTER = 0x21n;
1263
+ var PARAM_GROUP_ORDER = 0x22n;
1264
+ var PARAM_FILL_PARAMETERS = 0x23n;
1265
+ var PARAM_SUBGROUP_FILTER = 0x25n;
1266
+ var PARAM_OBJECTID_FILTER = 0x26n;
1267
+ var PARAM_PRIORITY_FILTER = 0x27n;
1268
+ var PARAM_OBJECT_PROPERTY_FILTER = 0x28n;
1269
+ var PARAM_TRACK_PROPERTY_FILTER = 0x29n;
1270
+ var PARAM_NEW_GROUP_REQUEST = 0x32n;
1271
+ var PARAM_TRACK_NAMESPACE_PREFIX = 0x34n;
1272
+ var PARAM_INCLUDE_PROPERTIES = 0x35n;
1273
+ var FILL_SCOPE = "fill_parameters";
1274
+ function encodeRangeFilter(filter, hasProperty, w) {
1275
+ if (filter.removed) {
1276
+ w.writeVarInt(0n);
1277
+ return;
1278
+ }
1279
+ const tmpW = new MoqtBufferWriter(32);
1280
+ tmpW.writeUint8(Number(filter.set_id ?? 0n));
1281
+ if (hasProperty) {
1282
+ tmpW.writeVarInt(filter.property_type ?? 0n);
1283
+ }
1284
+ let prevEnd = 0n;
1285
+ for (const range of filter.ranges ?? []) {
1286
+ tmpW.writeVarInt(range.start - prevEnd);
1287
+ if (range.end !== void 0) {
1288
+ tmpW.writeVarInt(range.end - range.start);
1289
+ prevEnd = range.end;
1290
+ }
1291
+ }
1292
+ const raw = tmpW.finish();
1293
+ w.writeVarInt(BigInt(raw.byteLength));
1294
+ w.writeBytes(raw);
1295
+ }
1296
+ function decodeRangeFilter(reader, hasProperty) {
1297
+ const length = Number(reader.readVarInt());
1298
+ if (length === 0) {
1299
+ return { removed: true };
1300
+ }
1301
+ const end = reader.offset + length;
1302
+ const set_id = BigInt(reader.readUint8());
1303
+ let property_type;
1304
+ if (hasProperty) {
1305
+ property_type = reader.readVarInt();
1306
+ }
1307
+ const ranges = [];
1308
+ let prevEnd = 0n;
1309
+ while (reader.offset < end) {
1310
+ const start = reader.readVarInt() + prevEnd;
1311
+ if (reader.offset < end) {
1312
+ const rangeEnd = reader.readVarInt() + start;
1313
+ ranges.push({ start, end: rangeEnd });
1314
+ prevEnd = rangeEnd;
1315
+ } else {
1316
+ ranges.push({ start });
1317
+ }
1318
+ }
1319
+ return property_type !== void 0 ? { set_id, property_type, ranges } : { set_id, ranges };
1320
+ }
1321
+ function encodeLocationFilter(f, w) {
1322
+ if (f.removed) {
1323
+ w.writeVarInt(0n);
1324
+ return;
1325
+ }
1326
+ const tmpW = new MoqtBufferWriter(48);
1327
+ if (f.start_group !== void 0) tmpW.writeVarInt(f.start_group);
1328
+ if (f.start_object !== void 0) tmpW.writeVarInt(f.start_object);
1329
+ if (f.end_group_delta !== void 0) tmpW.writeVarInt(f.end_group_delta);
1330
+ if (f.end_object !== void 0) tmpW.writeVarInt(f.end_object);
1331
+ const raw = tmpW.finish();
1332
+ w.writeVarInt(BigInt(raw.byteLength));
1333
+ w.writeBytes(raw);
1334
+ }
1335
+ function decodeLocationFilter(reader) {
1336
+ const length = Number(reader.readVarInt());
1337
+ const valueBytes = reader.readBytes(length);
1338
+ if (length === 0) {
1339
+ return { removed: true };
1340
+ }
1341
+ const r = new MoqtBufferReader(valueBytes);
1342
+ const fields = [];
1343
+ while (r.remaining > 0) {
1344
+ let value;
1345
+ try {
1346
+ value = r.readVarInt();
1347
+ } catch {
1348
+ throw new DecodeError(
1349
+ "CONSTRAINT_VIOLATION",
1350
+ "a LOCATION_FILTER field must not extend past the parameter's Length",
1351
+ reader.offset
1352
+ );
1353
+ }
1354
+ fields.push(value);
1355
+ if (fields.length > 4) {
1356
+ throw new DecodeError(
1357
+ "CONSTRAINT_VIOLATION",
1358
+ "a LOCATION_FILTER value may hold at most four vi64 fields",
1359
+ reader.offset
1360
+ );
1361
+ }
1362
+ }
1363
+ const filter = {};
1364
+ if (fields.length >= 1) filter.start_group = fields[0];
1365
+ if (fields.length >= 2) filter.start_object = fields[1];
1366
+ if (fields.length >= 3) filter.end_group_delta = fields[2];
1367
+ if (fields.length >= 4) filter.end_object = fields[3];
1368
+ if (filter.start_group !== void 0 && filter.end_group_delta !== void 0) {
1369
+ if (filter.start_group + filter.end_group_delta > MAX_U642) {
1370
+ throw new DecodeError(
1371
+ "CONSTRAINT_VIOLATION",
1372
+ "StartGroup + EndGroupDelta exceeds 2^64 - 1",
1373
+ reader.offset
1374
+ );
1375
+ }
1376
+ }
1377
+ return filter;
1378
+ }
1379
+ var PARAMETER_SCOPE = /* @__PURE__ */ new Map([
1380
+ // 0x02 OBJECT_DELIVERY_TIMEOUT (10.2.4) — d19 also listed PUBLISH_OK; d20 added PUBLISH.
1381
+ [PARAM_OBJECT_DELIVERY_TIMEOUT, /* @__PURE__ */ new Set(["subscribe", "publish", "request_update"])],
1382
+ // 0x03 AUTHORIZATION_TOKEN (10.2.2) — any request or response that needs authorization.
1383
+ [
1384
+ PARAM_AUTHORIZATION_TOKEN,
1385
+ /* @__PURE__ */ new Set([
1386
+ "publish",
1387
+ "subscribe",
1388
+ "request_update",
1389
+ "subscribe_namespace",
1390
+ "subscribe_tracks",
1391
+ "publish_namespace",
1392
+ "track_status",
1393
+ "fetch"
1394
+ ])
1395
+ ],
1396
+ // 0x04 RENDEZVOUS_TIMEOUT (10.2.6) — "MAY appear in a SUBSCRIBE message", nothing else.
1397
+ [PARAM_RENDEZVOUS_TIMEOUT, /* @__PURE__ */ new Set(["subscribe"])],
1398
+ // 0x06 SUBGROUP_DELIVERY_TIMEOUT (10.2.3) — d19 also listed PUBLISH_OK; d20 added PUBLISH.
1399
+ [PARAM_SUBGROUP_DELIVERY_TIMEOUT, /* @__PURE__ */ new Set(["subscribe", "publish", "request_update"])],
1400
+ // 0x08 EXPIRES (10.2.16) — the one parameter that still names PUBLISH_OK.
1401
+ [PARAM_EXPIRES, /* @__PURE__ */ new Set(["subscribe_ok", "publish", "request_ok"])],
1402
+ // 0x09 LARGEST_OBJECT (10.2.17) — gained PUBLISH_STATE_NOTIFY in draft-20.
1403
+ [
1404
+ PARAM_LARGEST_OBJECT,
1405
+ /* @__PURE__ */ new Set(["subscribe_ok", "publish", "request_ok", "publish_state_notify"])
1406
+ ],
1407
+ // 0x0A FILL_TIMEOUT (10.2.5) — FETCH, or nested inside FILL_PARAMETERS.
1408
+ [PARAM_FILL_TIMEOUT, /* @__PURE__ */ new Set(["fetch", FILL_SCOPE])],
1409
+ // 0x10 FORWARD (10.2.18) — lost PUBLISH_OK, gained PUBLISH_STATE_NOTIFY.
1410
+ [
1411
+ PARAM_FORWARD,
1412
+ /* @__PURE__ */ new Set(["subscribe", "request_update", "publish", "subscribe_tracks", "publish_state_notify"])
1413
+ ],
1414
+ // 0x20 SUBSCRIBER_PRIORITY (10.2.7) — lost PUBLISH_OK, gained PUBLISH.
1415
+ [
1416
+ PARAM_SUBSCRIBER_PRIORITY,
1417
+ /* @__PURE__ */ new Set(["subscribe", "publish", "fetch", "request_update", FILL_SCOPE])
1418
+ ],
1419
+ // 0x21 LOCATION_FILTER (10.2.9) — lost PUBLISH_OK, gained PUBLISH and PUBLISH_STATE_NOTIFY.
1420
+ [
1421
+ PARAM_LOCATION_FILTER,
1422
+ /* @__PURE__ */ new Set([
1423
+ "fetch",
1424
+ "subscribe",
1425
+ "publish",
1426
+ "request_update",
1427
+ "publish_state_notify",
1428
+ FILL_SCOPE
1429
+ ])
1430
+ ],
1431
+ // 0x22 GROUP_ORDER (10.2.8) — gained PUBLISH and FILL_PARAMETERS nesting.
1432
+ [PARAM_GROUP_ORDER, /* @__PURE__ */ new Set(["subscribe", "publish", "subscribe_tracks", "fetch", FILL_SCOPE])],
1433
+ // 0x23 FILL_PARAMETERS (10.2.15) — NEW. Subscriptions only, and never nested
1434
+ // in itself: a FETCH already is a fetch, so it has nothing to fill.
1435
+ [PARAM_FILL_PARAMETERS, /* @__PURE__ */ new Set(["subscribe", "request_update"])],
1436
+ // 0x25-0x28 range filters (5.1.4) — lost PUBLISH_OK, gained FILL_PARAMETERS nesting.
1437
+ [
1438
+ PARAM_SUBGROUP_FILTER,
1439
+ /* @__PURE__ */ new Set(["fetch", "subscribe", "subscribe_tracks", "request_update", FILL_SCOPE])
1440
+ ],
1441
+ [
1442
+ PARAM_OBJECTID_FILTER,
1443
+ /* @__PURE__ */ new Set(["fetch", "subscribe", "subscribe_tracks", "request_update", FILL_SCOPE])
1444
+ ],
1445
+ [
1446
+ PARAM_PRIORITY_FILTER,
1447
+ /* @__PURE__ */ new Set(["fetch", "subscribe", "subscribe_tracks", "request_update", FILL_SCOPE])
1448
+ ],
1449
+ [
1450
+ PARAM_OBJECT_PROPERTY_FILTER,
1451
+ /* @__PURE__ */ new Set(["fetch", "subscribe", "subscribe_tracks", "request_update", FILL_SCOPE])
1452
+ ],
1453
+ // 0x29 TRACK_PROPERTY_FILTER (10.2.14) — lost PUBLISH_OK; NOT permitted
1454
+ // inside FILL_PARAMETERS. A relay forwarding a downstream FILL_PARAMETERS
1455
+ // upstream has to strip it rather than trip this rule.
1456
+ [PARAM_TRACK_PROPERTY_FILTER, /* @__PURE__ */ new Set(["subscribe_tracks", "request_update"])],
1457
+ // 0x32 NEW_GROUP_REQUEST (10.2.19) — lost PUBLISH_OK.
1458
+ [PARAM_NEW_GROUP_REQUEST, /* @__PURE__ */ new Set(["subscribe", "request_update"])],
1459
+ // 0x34 TRACK_NAMESPACE_PREFIX (10.2.20) — REQUEST_UPDATE for a namespace subscription.
1460
+ [PARAM_TRACK_NAMESPACE_PREFIX, /* @__PURE__ */ new Set(["request_update"])],
1461
+ // 0x35 INCLUDE_PROPERTIES (10.2.21) — NEW in draft-20.
1462
+ [PARAM_INCLUDE_PROPERTIES, /* @__PURE__ */ new Set(["subscribe", "track_status", "fetch", "subscribe_tracks"])]
1463
+ ]);
1464
+ function encodeParams(params, writer) {
1465
+ const entries = [];
1466
+ if (params.object_delivery_timeout !== void 0) {
1467
+ entries.push({
1468
+ type: PARAM_OBJECT_DELIVERY_TIMEOUT,
1469
+ encode: (w) => w.writeVarInt(params.object_delivery_timeout)
1470
+ });
1471
+ }
1472
+ if (params.authorization_token !== void 0) {
1473
+ entries.push({
1474
+ type: PARAM_AUTHORIZATION_TOKEN,
1475
+ encode: (w) => {
1476
+ const tmpW = new MoqtBufferWriter(64);
1477
+ encodeAuthorizationToken(params.authorization_token, tmpW);
1478
+ const raw = tmpW.finish();
1479
+ w.writeVarInt(BigInt(raw.byteLength));
1480
+ w.writeBytes(raw);
1481
+ }
1482
+ });
1483
+ }
1484
+ if (params.rendezvous_timeout !== void 0) {
1485
+ entries.push({
1486
+ type: PARAM_RENDEZVOUS_TIMEOUT,
1487
+ encode: (w) => w.writeVarInt(params.rendezvous_timeout)
1488
+ });
1489
+ }
1490
+ if (params.subgroup_delivery_timeout !== void 0) {
1491
+ entries.push({
1492
+ type: PARAM_SUBGROUP_DELIVERY_TIMEOUT,
1493
+ encode: (w) => w.writeVarInt(params.subgroup_delivery_timeout)
1494
+ });
1495
+ }
1496
+ if (params.expires !== void 0) {
1497
+ entries.push({
1498
+ type: PARAM_EXPIRES,
1499
+ encode: (w) => w.writeVarInt(params.expires)
1500
+ });
1501
+ }
1502
+ if (params.largest_object !== void 0) {
1503
+ entries.push({
1504
+ type: PARAM_LARGEST_OBJECT,
1505
+ // A Location is "Two consecutive varints (Group, Object)", which is a
1506
+ // different value encoding from "Length-prefixed". Nothing states the
1507
+ // length, so nothing writes one — even though 0x09 is odd, because the
1508
+ // Key-Value-Pair odd/even rule does not apply to Message Parameters.
1509
+ encode: (w) => {
1510
+ w.writeVarInt(params.largest_object.group);
1511
+ w.writeVarInt(params.largest_object.object);
1512
+ }
1513
+ });
1514
+ }
1515
+ if (params.fill_timeout !== void 0) {
1516
+ entries.push({
1517
+ type: PARAM_FILL_TIMEOUT,
1518
+ encode: (w) => w.writeVarInt(params.fill_timeout)
1519
+ });
1520
+ }
1521
+ if (params.forward !== void 0) {
1522
+ entries.push({
1523
+ type: PARAM_FORWARD,
1524
+ encode: (w) => w.writeUint8(Number(params.forward))
1525
+ });
1526
+ }
1527
+ if (params.subscriber_priority !== void 0) {
1528
+ entries.push({
1529
+ type: PARAM_SUBSCRIBER_PRIORITY,
1530
+ encode: (w) => w.writeUint8(Number(params.subscriber_priority))
1531
+ });
1532
+ }
1533
+ if (params.location_filter !== void 0) {
1534
+ entries.push({
1535
+ type: PARAM_LOCATION_FILTER,
1536
+ encode: (w) => encodeLocationFilter(params.location_filter, w)
1537
+ });
1538
+ }
1539
+ if (params.group_order !== void 0) {
1540
+ entries.push({
1541
+ type: PARAM_GROUP_ORDER,
1542
+ encode: (w) => w.writeUint8(Number(params.group_order))
1543
+ });
1544
+ }
1545
+ if (params.fill_parameters !== void 0) {
1546
+ entries.push({
1547
+ type: PARAM_FILL_PARAMETERS,
1548
+ encode: (w) => {
1549
+ const tmpW = new MoqtBufferWriter(64);
1550
+ encodeParams(params.fill_parameters, tmpW);
1551
+ const raw = tmpW.finish();
1552
+ w.writeVarInt(BigInt(raw.byteLength));
1553
+ w.writeBytes(raw);
1554
+ }
1555
+ });
1556
+ }
1557
+ if (params.subgroup_filter !== void 0) {
1558
+ entries.push({
1559
+ type: PARAM_SUBGROUP_FILTER,
1560
+ encode: (w) => encodeRangeFilter(params.subgroup_filter, false, w)
1561
+ });
1562
+ }
1563
+ if (params.objectid_filter !== void 0) {
1564
+ entries.push({
1565
+ type: PARAM_OBJECTID_FILTER,
1566
+ encode: (w) => encodeRangeFilter(params.objectid_filter, false, w)
1567
+ });
1568
+ }
1569
+ if (params.priority_filter !== void 0) {
1570
+ entries.push({
1571
+ type: PARAM_PRIORITY_FILTER,
1572
+ encode: (w) => encodeRangeFilter(params.priority_filter, false, w)
1573
+ });
1574
+ }
1575
+ if (params.object_property_filter !== void 0) {
1576
+ entries.push({
1577
+ type: PARAM_OBJECT_PROPERTY_FILTER,
1578
+ encode: (w) => encodeRangeFilter(params.object_property_filter, true, w)
1579
+ });
1580
+ }
1581
+ if (params.track_property_filter !== void 0) {
1582
+ entries.push({
1583
+ type: PARAM_TRACK_PROPERTY_FILTER,
1584
+ encode: (w) => encodeRangeFilter(params.track_property_filter, true, w)
1585
+ });
1586
+ }
1587
+ if (params.new_group_request !== void 0) {
1588
+ entries.push({
1589
+ type: PARAM_NEW_GROUP_REQUEST,
1590
+ encode: (w) => w.writeVarInt(params.new_group_request)
1591
+ });
1592
+ }
1593
+ if (params.track_namespace_prefix !== void 0) {
1594
+ entries.push({
1595
+ type: PARAM_TRACK_NAMESPACE_PREFIX,
1596
+ // The parameter "uses the Track Namespace encoding", which is a tuple
1597
+ // that states its own field count. That is not the Length-prefixed
1598
+ // encoding, so there is no byte length ahead of it.
1599
+ encode: (w) => {
1600
+ encodeNamespaceTuple(params.track_namespace_prefix, w);
1601
+ }
1602
+ });
1603
+ }
1604
+ if (params.include_properties !== void 0) {
1605
+ entries.push({
1606
+ type: PARAM_INCLUDE_PROPERTIES,
1607
+ // uint8: one raw byte, 0 or 1 (draft-20 Section 10.2.21).
1608
+ encode: (w) => w.writeUint8(Number(params.include_properties))
1609
+ });
1610
+ }
1611
+ if (params.unknown) {
1612
+ for (const u of params.unknown) {
1613
+ const id = BigInt(u.id);
1614
+ entries.push({
1615
+ type: id,
1616
+ encode: (w) => {
1617
+ const raw = hexToBytes(u.raw_hex);
1618
+ w.writeBytes(raw);
1619
+ }
1620
+ });
1621
+ }
1622
+ }
1623
+ entries.sort((a, b) => a.type < b.type ? -1 : a.type > b.type ? 1 : 0);
1624
+ writer.writeVarInt(BigInt(entries.length));
1625
+ let prevType = 0n;
1626
+ for (const entry of entries) {
1627
+ writer.writeVarInt(entry.type - prevType);
1628
+ entry.encode(writer);
1629
+ prevType = entry.type;
1630
+ }
1631
+ }
1632
+ function decodeParams(reader, messageType) {
1633
+ const count = Number(reader.readVarInt());
1634
+ const result = {};
1635
+ let prevType = 0n;
1636
+ for (let i = 0; i < count; i++) {
1637
+ const delta = reader.readVarInt();
1638
+ const paramType = prevType + delta;
1639
+ prevType = paramType;
1640
+ const scope = PARAMETER_SCOPE.get(paramType);
1641
+ if (scope !== void 0 && !scope.has(messageType)) {
1642
+ throw new DecodeError(
1643
+ "CONSTRAINT_VIOLATION",
1644
+ `Message Parameter 0x${paramType.toString(16)} may not appear in ${messageType}`,
1645
+ reader.offset
1646
+ );
1647
+ }
1648
+ if (paramType === PARAM_OBJECT_DELIVERY_TIMEOUT) {
1649
+ result.object_delivery_timeout = reader.readVarInt();
1650
+ } else if (paramType === PARAM_AUTHORIZATION_TOKEN) {
1651
+ const length = Number(reader.readVarInt());
1652
+ const bytes = reader.readBytes(length);
1653
+ result.authorization_token = decodeAuthorizationToken(new MoqtBufferReader(bytes));
1654
+ } else if (paramType === PARAM_RENDEZVOUS_TIMEOUT) {
1655
+ result.rendezvous_timeout = reader.readVarInt();
1656
+ } else if (paramType === PARAM_SUBGROUP_DELIVERY_TIMEOUT) {
1657
+ result.subgroup_delivery_timeout = reader.readVarInt();
1658
+ } else if (paramType === PARAM_EXPIRES) {
1659
+ result.expires = reader.readVarInt();
1660
+ } else if (paramType === PARAM_LARGEST_OBJECT) {
1661
+ const group = reader.readVarInt();
1662
+ const object = reader.readVarInt();
1663
+ result.largest_object = { group, object };
1664
+ } else if (paramType === PARAM_FILL_TIMEOUT) {
1665
+ result.fill_timeout = reader.readVarInt();
1666
+ } else if (paramType === PARAM_FORWARD) {
1667
+ result.forward = BigInt(reader.readUint8());
1668
+ } else if (paramType === PARAM_SUBSCRIBER_PRIORITY) {
1669
+ result.subscriber_priority = BigInt(reader.readUint8());
1670
+ } else if (paramType === PARAM_LOCATION_FILTER) {
1671
+ result.location_filter = decodeLocationFilter(reader);
1672
+ } else if (paramType === PARAM_GROUP_ORDER) {
1673
+ result.group_order = BigInt(reader.readUint8());
1674
+ } else if (paramType === PARAM_FILL_PARAMETERS) {
1675
+ result.fill_parameters = decodeFillParameters(reader);
1676
+ } else if (paramType === PARAM_SUBGROUP_FILTER) {
1677
+ result.subgroup_filter = decodeRangeFilter(reader, false);
1678
+ } else if (paramType === PARAM_OBJECTID_FILTER) {
1679
+ result.objectid_filter = decodeRangeFilter(reader, false);
1680
+ } else if (paramType === PARAM_PRIORITY_FILTER) {
1681
+ result.priority_filter = decodeRangeFilter(reader, false);
1682
+ } else if (paramType === PARAM_OBJECT_PROPERTY_FILTER) {
1683
+ result.object_property_filter = decodeRangeFilter(reader, true);
1684
+ } else if (paramType === PARAM_TRACK_PROPERTY_FILTER) {
1685
+ result.track_property_filter = decodeRangeFilter(reader, true);
1686
+ } else if (paramType === PARAM_NEW_GROUP_REQUEST) {
1687
+ result.new_group_request = reader.readVarInt();
1688
+ } else if (paramType === PARAM_TRACK_NAMESPACE_PREFIX) {
1689
+ result.track_namespace_prefix = decodeNamespaceTuple(reader);
1690
+ } else if (paramType === PARAM_INCLUDE_PROPERTIES) {
1691
+ const value = BigInt(reader.readUint8());
1692
+ if (value !== 0n && value !== 1n) {
1693
+ throw new DecodeError(
1694
+ "CONSTRAINT_VIOLATION",
1695
+ `INCLUDE_PROPERTIES accepts only 0 or 1, got ${value}`,
1696
+ reader.offset
1697
+ );
1698
+ }
1699
+ result.include_properties = value;
1700
+ } else {
1701
+ throw new DecodeError(
1702
+ "INVALID_PARAMETER",
1703
+ `Unknown Message Parameter type 0x${paramType.toString(16)}`,
1704
+ reader.offset
1705
+ );
1706
+ }
1707
+ }
1708
+ return result;
1709
+ }
1710
+ function decodeFillParameters(reader) {
1711
+ const length = Number(reader.readVarInt());
1712
+ const valueBytes = reader.readBytes(length);
1713
+ const nested = new MoqtBufferReader(valueBytes);
1714
+ const params = decodeParams(nested, FILL_SCOPE);
1715
+ if (nested.remaining > 0) {
1716
+ throw new DecodeError(
1717
+ "CONSTRAINT_VIOLATION",
1718
+ `FILL_PARAMETERS has ${nested.remaining} trailing byte(s) after its parameter block`,
1719
+ reader.offset
1720
+ );
1721
+ }
1722
+ return params;
1723
+ }
1724
+ var TPROP_OBJECT_DELIVERY_TIMEOUT = 0x02n;
1725
+ var TPROP_MAX_CACHE_DURATION = 0x04n;
1726
+ var TPROP_SUBGROUP_DELIVERY_TIMEOUT = 0x06n;
1727
+ var TPROP_IMMUTABLE_PROPERTIES = 0x0bn;
1728
+ var TPROP_DEFAULT_PUBLISHER_PRIORITY = 0x0en;
1729
+ var TPROP_DEFAULT_PUBLISHER_GROUP_ORDER = 0x22n;
1730
+ var TPROP_DYNAMIC_GROUPS = 0x30n;
1731
+ function encodeTrackProperties(props, writer) {
1732
+ const entries = [];
1733
+ if (props.object_delivery_timeout !== void 0) {
1734
+ entries.push({
1735
+ type: TPROP_OBJECT_DELIVERY_TIMEOUT,
1736
+ encode: (w) => w.writeVarInt(props.object_delivery_timeout)
1737
+ });
1738
+ }
1739
+ if (props.max_cache_duration !== void 0) {
1740
+ entries.push({
1741
+ type: TPROP_MAX_CACHE_DURATION,
1742
+ encode: (w) => w.writeVarInt(props.max_cache_duration)
1743
+ });
1744
+ }
1745
+ if (props.subgroup_delivery_timeout !== void 0) {
1746
+ entries.push({
1747
+ type: TPROP_SUBGROUP_DELIVERY_TIMEOUT,
1748
+ encode: (w) => w.writeVarInt(props.subgroup_delivery_timeout)
1749
+ });
1750
+ }
1751
+ if (props.immutable_properties !== void 0) {
1752
+ entries.push({
1753
+ type: TPROP_IMMUTABLE_PROPERTIES,
1754
+ encode: (w) => {
1755
+ const raw = props.immutable_properties;
1756
+ w.writeVarInt(BigInt(raw.byteLength));
1757
+ w.writeBytes(raw);
1758
+ }
1759
+ });
1760
+ }
1761
+ if (props.default_publisher_priority !== void 0) {
1762
+ entries.push({
1763
+ type: TPROP_DEFAULT_PUBLISHER_PRIORITY,
1764
+ encode: (w) => w.writeVarInt(props.default_publisher_priority)
1765
+ });
1766
+ }
1767
+ if (props.default_publisher_group_order !== void 0) {
1768
+ entries.push({
1769
+ type: TPROP_DEFAULT_PUBLISHER_GROUP_ORDER,
1770
+ encode: (w) => w.writeVarInt(props.default_publisher_group_order)
1771
+ });
1772
+ }
1773
+ if (props.dynamic_groups !== void 0) {
1774
+ entries.push({
1775
+ type: TPROP_DYNAMIC_GROUPS,
1776
+ encode: (w) => w.writeVarInt(props.dynamic_groups)
1777
+ });
1778
+ }
1779
+ if (props.unknown) {
1780
+ for (const u of props.unknown) {
1781
+ const id = BigInt(u.id);
1782
+ entries.push({
1783
+ type: id,
1784
+ encode: (w) => {
1785
+ if (id % 2n === 0n) {
1786
+ const raw = hexToBytes(u.raw_hex);
1787
+ const tmpReader = new MoqtBufferReader(raw);
1788
+ w.writeVarInt(tmpReader.readVarInt());
1789
+ } else {
1790
+ const raw = hexToBytes(u.raw_hex);
1791
+ w.writeVarInt(BigInt(raw.byteLength));
1792
+ w.writeBytes(raw);
1793
+ }
1794
+ }
1795
+ });
1796
+ }
1797
+ }
1798
+ entries.sort((a, b) => a.type < b.type ? -1 : a.type > b.type ? 1 : 0);
1799
+ let prevType = 0n;
1800
+ for (const entry of entries) {
1801
+ writer.writeVarInt(entry.type - prevType);
1802
+ entry.encode(writer);
1803
+ prevType = entry.type;
1804
+ }
1805
+ }
1806
+ function decodeTrackProperties(reader, payloadEnd) {
1807
+ const result = {};
1808
+ const unknown = [];
1809
+ let prevType = 0n;
1810
+ while (reader.offset < payloadEnd) {
1811
+ const delta = reader.readVarInt();
1812
+ const propType = prevType + delta;
1813
+ prevType = propType;
1814
+ if (propType % 2n === 0n) {
1815
+ const value = reader.readVarInt();
1816
+ if (propType === TPROP_OBJECT_DELIVERY_TIMEOUT) {
1817
+ result.object_delivery_timeout = value;
1818
+ } else if (propType === TPROP_MAX_CACHE_DURATION) {
1819
+ result.max_cache_duration = value;
1820
+ } else if (propType === TPROP_SUBGROUP_DELIVERY_TIMEOUT) {
1821
+ result.subgroup_delivery_timeout = value;
1822
+ } else if (propType === TPROP_DEFAULT_PUBLISHER_PRIORITY) {
1823
+ result.default_publisher_priority = value;
1824
+ } else if (propType === TPROP_DEFAULT_PUBLISHER_GROUP_ORDER) {
1825
+ result.default_publisher_group_order = value;
1826
+ } else if (propType === TPROP_DYNAMIC_GROUPS) {
1827
+ result.dynamic_groups = value;
1828
+ } else {
1829
+ const tmpWriter = new MoqtBufferWriter(16);
1830
+ tmpWriter.writeVarInt(value);
1831
+ const raw = tmpWriter.finish();
1832
+ unknown.push({
1833
+ id: `0x${propType.toString(16)}`,
1834
+ length: raw.byteLength,
1835
+ raw_hex: bytesToHex(raw)
1836
+ });
1837
+ }
1838
+ } else {
1839
+ const length = Number(reader.readVarInt());
1840
+ const bytes = reader.readBytes(length);
1841
+ if (propType === TPROP_IMMUTABLE_PROPERTIES) {
1842
+ result.immutable_properties = bytes;
1843
+ } else {
1844
+ unknown.push({
1845
+ id: `0x${propType.toString(16)}`,
1846
+ length,
1847
+ raw_hex: bytesToHex(bytes)
1848
+ });
1849
+ }
1850
+ }
1851
+ }
1852
+ if (unknown.length > 0) result.unknown = unknown;
1853
+ return result;
1854
+ }
1855
+ function encodeRedirect(redirect, w) {
1856
+ const uri = textEncoder.encode(redirect.connect_uri);
1857
+ w.writeVarInt(BigInt(uri.byteLength));
1858
+ w.writeBytes(uri);
1859
+ w.writeTuple(redirect.track_namespace);
1860
+ w.writeString(redirect.track_name);
1861
+ }
1862
+ function decodeRedirect(r) {
1863
+ const uriLen = Number(r.readVarInt());
1864
+ const uriBytes = r.readBytes(uriLen);
1865
+ const connect_uri = textDecoder.decode(uriBytes);
1866
+ const track_namespace = r.readTuple();
1867
+ const track_name = r.readString();
1868
+ return { connect_uri, track_namespace, track_name };
1869
+ }
1870
+ function encodeSetupPayload(msg, w) {
1871
+ encodeSetupOptions(msg.options, w);
1872
+ }
1873
+ function encodeSubscribePayload(msg, w) {
1874
+ w.writeVarInt(msg.request_id);
1875
+ w.writeTuple(msg.track_namespace);
1876
+ w.writeString(msg.track_name);
1877
+ encodeParams(msg.parameters, w);
1878
+ }
1879
+ function encodeSubscribeOkPayload(msg, w) {
1880
+ w.writeVarInt(msg.track_alias);
1881
+ encodeParams(msg.parameters, w);
1882
+ encodeTrackProperties(msg.track_properties, w);
1883
+ }
1884
+ function encodeRequestUpdatePayload(msg, w) {
1885
+ w.writeVarInt(msg.request_id);
1886
+ encodeParams(msg.parameters, w);
1887
+ }
1888
+ function encodePublishStateNotifyPayload(msg, w) {
1889
+ encodeParams(msg.parameters, w);
1890
+ }
1891
+ function encodePublishPayload(msg, w) {
1892
+ w.writeVarInt(msg.request_id);
1893
+ w.writeTuple(msg.track_namespace);
1894
+ w.writeString(msg.track_name);
1895
+ w.writeVarInt(msg.track_alias);
1896
+ encodeParams(msg.parameters, w);
1897
+ encodeTrackProperties(msg.track_properties, w);
1898
+ }
1899
+ function encodePublishDonePayload(msg, w) {
1900
+ w.writeVarInt(msg.status_code);
1901
+ w.writeVarInt(msg.stream_count);
1902
+ w.writeString(msg.reason_phrase);
1903
+ }
1904
+ function encodePublishNamespacePayload(msg, w) {
1905
+ w.writeVarInt(msg.request_id);
1906
+ w.writeTuple(msg.track_namespace);
1907
+ encodeParams(msg.parameters, w);
1908
+ }
1909
+ function encodeNamespacePayload(msg, w) {
1910
+ w.writeTuple(msg.namespace_suffix);
1911
+ }
1912
+ function encodeNamespaceDonePayload(msg, w) {
1913
+ w.writeTuple(msg.namespace_suffix);
1914
+ }
1915
+ function encodeSubscribeNamespacePayload(msg, w) {
1916
+ w.writeVarInt(msg.request_id);
1917
+ w.writeTuple(msg.namespace_prefix);
1918
+ encodeParams(msg.parameters, w);
1919
+ }
1920
+ function encodeSubscribeTracksPayload(msg, w) {
1921
+ w.writeVarInt(msg.request_id);
1922
+ w.writeTuple(msg.namespace_prefix);
1923
+ encodeParams(msg.parameters, w);
1924
+ }
1925
+ function encodePublishSkippedPayload(msg, w) {
1926
+ w.writeTuple(msg.namespace_suffix);
1927
+ w.writeString(msg.track_name);
1928
+ }
1929
+ function encodeFetchPayload(msg, w) {
1930
+ w.writeVarInt(msg.request_id);
1931
+ w.writeTuple(msg.track_namespace);
1932
+ w.writeString(msg.track_name);
1933
+ encodeParams(msg.parameters, w);
1934
+ }
1935
+ function encodeFetchOkPayload(msg, w) {
1936
+ w.writeUint8(msg.end_of_track);
1937
+ w.writeVarInt(msg.end_group);
1938
+ w.writeVarInt(msg.end_object);
1939
+ encodeParams(msg.parameters, w);
1940
+ encodeTrackProperties(msg.track_properties, w);
1941
+ }
1942
+ function encodeTrackStatusPayload(msg, w) {
1943
+ w.writeVarInt(msg.request_id);
1944
+ w.writeTuple(msg.track_namespace);
1945
+ w.writeString(msg.track_name);
1946
+ encodeParams(msg.parameters, w);
1947
+ }
1948
+ function encodeRequestOkPayload(msg, w) {
1949
+ encodeParams(msg.parameters, w);
1950
+ encodeTrackProperties(msg.track_properties, w);
1951
+ }
1952
+ function encodeRequestErrorPayload(msg, w) {
1953
+ w.writeVarInt(msg.error_code);
1954
+ w.writeVarInt(msg.retry_interval);
1955
+ w.writeString(msg.reason_phrase);
1956
+ if (msg.error_code === REQUEST_ERROR_REDIRECT && msg.redirect) {
1957
+ encodeRedirect(msg.redirect, w);
1958
+ }
1959
+ }
1960
+ function encodeGoAwayPayload(msg, w) {
1961
+ w.writeString(msg.new_session_uri);
1962
+ w.writeVarInt(msg.timeout);
1963
+ }
1964
+ function decodeSetupPayload(r, payloadEnd) {
1965
+ const options = decodeSetupOptions(r, payloadEnd);
1966
+ return { type: "setup", options };
1967
+ }
1968
+ function decodeSubscribePayload(r) {
1969
+ const request_id = r.readVarInt();
1970
+ const track_namespace = r.readTuple();
1971
+ const track_name = r.readString();
1972
+ const parameters = decodeParams(r, "subscribe");
1973
+ return {
1974
+ type: "subscribe",
1975
+ request_id,
1976
+ track_namespace,
1977
+ track_name,
1978
+ parameters
1979
+ };
1980
+ }
1981
+ function decodeSubscribeOkPayload(r, payloadEnd) {
1982
+ const track_alias = r.readVarInt();
1983
+ const parameters = decodeParams(r, "subscribe_ok");
1984
+ const track_properties = decodeTrackProperties(r, payloadEnd);
1985
+ return { type: "subscribe_ok", track_alias, parameters, track_properties };
1986
+ }
1987
+ function decodeRequestUpdatePayload(r) {
1988
+ const request_id = r.readVarInt();
1989
+ const parameters = decodeParams(r, "request_update");
1990
+ return {
1991
+ type: "request_update",
1992
+ request_id,
1993
+ parameters
1994
+ };
1995
+ }
1996
+ function decodePublishStateNotifyPayload(r) {
1997
+ const parameters = decodeParams(r, "publish_state_notify");
1998
+ return { type: "publish_state_notify", parameters };
1999
+ }
2000
+ function decodePublishPayload(r, payloadEnd) {
2001
+ const request_id = r.readVarInt();
2002
+ const track_namespace = r.readTuple();
2003
+ const track_name = r.readString();
2004
+ const track_alias = r.readVarInt();
2005
+ const parameters = decodeParams(r, "publish");
2006
+ const track_properties = decodeTrackProperties(r, payloadEnd);
2007
+ return {
2008
+ type: "publish",
2009
+ request_id,
2010
+ track_namespace,
2011
+ track_name,
2012
+ track_alias,
2013
+ parameters,
2014
+ track_properties
2015
+ };
2016
+ }
2017
+ function decodePublishDonePayload(r) {
2018
+ const status_code = r.readVarInt();
2019
+ const stream_count = r.readVarInt();
2020
+ const reason_phrase = r.readString();
2021
+ return { type: "publish_done", status_code, stream_count, reason_phrase };
2022
+ }
2023
+ function decodePublishNamespacePayload(r) {
2024
+ const request_id = r.readVarInt();
2025
+ const track_namespace = r.readTuple();
2026
+ const parameters = decodeParams(r, "publish_namespace");
2027
+ return {
2028
+ type: "publish_namespace",
2029
+ request_id,
2030
+ track_namespace,
2031
+ parameters
2032
+ };
2033
+ }
2034
+ function decodeNamespacePayload(r) {
2035
+ const namespace_suffix = r.readTuple();
2036
+ return { type: "namespace", namespace_suffix };
2037
+ }
2038
+ function decodeNamespaceDonePayload(r) {
2039
+ const namespace_suffix = r.readTuple();
2040
+ return { type: "namespace_done", namespace_suffix };
2041
+ }
2042
+ function decodeSubscribeNamespacePayload(r) {
2043
+ const request_id = r.readVarInt();
2044
+ const namespace_prefix = r.readTuple();
2045
+ const parameters = decodeParams(r, "subscribe_namespace");
2046
+ return {
2047
+ type: "subscribe_namespace",
2048
+ request_id,
2049
+ namespace_prefix,
2050
+ parameters
2051
+ };
2052
+ }
2053
+ function decodeSubscribeTracksPayload(r) {
2054
+ const request_id = r.readVarInt();
2055
+ const namespace_prefix = r.readTuple();
2056
+ const parameters = decodeParams(r, "subscribe_tracks");
2057
+ return {
2058
+ type: "subscribe_tracks",
2059
+ request_id,
2060
+ namespace_prefix,
2061
+ parameters
2062
+ };
2063
+ }
2064
+ function decodePublishSkippedPayload(r) {
2065
+ const namespace_suffix = r.readTuple();
2066
+ const track_name = r.readString();
2067
+ return { type: "publish_skipped", namespace_suffix, track_name };
2068
+ }
2069
+ function decodeFetchPayload(r) {
2070
+ const request_id = r.readVarInt();
2071
+ const track_namespace = r.readTuple();
2072
+ const track_name = r.readString();
2073
+ const parameters = decodeParams(r, "fetch");
2074
+ return {
2075
+ type: "fetch",
2076
+ request_id,
2077
+ track_namespace,
2078
+ track_name,
2079
+ parameters
2080
+ };
2081
+ }
2082
+ function decodeFetchOkPayload(r, payloadEnd) {
2083
+ const end_of_track = r.readUint8();
2084
+ const end_group = r.readVarInt();
2085
+ const end_object = r.readVarInt();
2086
+ const parameters = decodeParams(r, "fetch_ok");
2087
+ const track_properties = decodeTrackProperties(r, payloadEnd);
2088
+ return {
2089
+ type: "fetch_ok",
2090
+ end_of_track,
2091
+ end_group,
2092
+ end_object,
2093
+ parameters,
2094
+ track_properties
2095
+ };
2096
+ }
2097
+ function decodeTrackStatusPayload(r) {
2098
+ const request_id = r.readVarInt();
2099
+ const track_namespace = r.readTuple();
2100
+ const track_name = r.readString();
2101
+ const parameters = decodeParams(r, "track_status");
2102
+ return {
2103
+ type: "track_status",
2104
+ request_id,
2105
+ track_namespace,
2106
+ track_name,
2107
+ parameters
2108
+ };
2109
+ }
2110
+ function decodeRequestOkPayload(r, payloadEnd) {
2111
+ const parameters = decodeParams(r, "request_ok");
2112
+ const track_properties = decodeTrackProperties(r, payloadEnd);
2113
+ return { type: "request_ok", parameters, track_properties };
2114
+ }
2115
+ function decodeRequestErrorPayload(r, payloadEnd) {
2116
+ const error_code = r.readVarInt();
2117
+ const retry_interval = r.readVarInt();
2118
+ const reason_phrase = r.readString();
2119
+ let redirect;
2120
+ if (error_code === REQUEST_ERROR_REDIRECT && r.offset < payloadEnd) {
2121
+ redirect = decodeRedirect(r);
2122
+ }
2123
+ if (redirect !== void 0) {
2124
+ return { type: "request_error", error_code, retry_interval, reason_phrase, redirect };
2125
+ }
2126
+ return { type: "request_error", error_code, retry_interval, reason_phrase };
2127
+ }
2128
+ function decodeGoAwayPayload(r) {
2129
+ const new_session_uri = r.readString();
2130
+ const timeout = r.readVarInt();
2131
+ return { type: "goaway", new_session_uri, timeout };
2132
+ }
2133
+ function encodeMessage(message) {
2134
+ const typeId = MESSAGE_ID_MAP.get(message.type);
2135
+ if (typeId === void 0) {
2136
+ throw new Error(`Unknown message type: ${message.type}`);
2137
+ }
2138
+ const payloadWriter = new MoqtBufferWriter();
2139
+ encodePayload(message, payloadWriter);
2140
+ const payload = payloadWriter.finishView();
2141
+ if (payload.byteLength > 65535) {
2142
+ throw new Error(`Payload too large for 16-bit length: ${payload.byteLength}`);
2143
+ }
2144
+ const writer = new MoqtBufferWriter(payload.byteLength + 16);
2145
+ writer.writeVarInt(typeId);
2146
+ writer.writeUint8(payload.byteLength >> 8 & 255);
2147
+ writer.writeUint8(payload.byteLength & 255);
2148
+ writer.writeBytes(payload);
2149
+ return writer.finish();
2150
+ }
2151
+ function encodePayload(msg, w) {
2152
+ switch (msg.type) {
2153
+ case "setup":
2154
+ return encodeSetupPayload(msg, w);
2155
+ case "subscribe":
2156
+ return encodeSubscribePayload(msg, w);
2157
+ case "subscribe_ok":
2158
+ return encodeSubscribeOkPayload(msg, w);
2159
+ case "request_update":
2160
+ return encodeRequestUpdatePayload(msg, w);
2161
+ case "publish_state_notify":
2162
+ return encodePublishStateNotifyPayload(msg, w);
2163
+ case "publish":
2164
+ return encodePublishPayload(msg, w);
2165
+ case "publish_done":
2166
+ return encodePublishDonePayload(msg, w);
2167
+ case "publish_namespace":
2168
+ return encodePublishNamespacePayload(msg, w);
2169
+ case "namespace":
2170
+ return encodeNamespacePayload(msg, w);
2171
+ case "namespace_done":
2172
+ return encodeNamespaceDonePayload(msg, w);
2173
+ case "subscribe_namespace":
2174
+ return encodeSubscribeNamespacePayload(msg, w);
2175
+ case "subscribe_tracks":
2176
+ return encodeSubscribeTracksPayload(msg, w);
2177
+ case "publish_skipped":
2178
+ return encodePublishSkippedPayload(msg, w);
2179
+ case "fetch":
2180
+ return encodeFetchPayload(msg, w);
2181
+ case "fetch_ok":
2182
+ return encodeFetchOkPayload(msg, w);
2183
+ case "track_status":
2184
+ return encodeTrackStatusPayload(msg, w);
2185
+ case "request_ok":
2186
+ return encodeRequestOkPayload(msg, w);
2187
+ case "request_error":
2188
+ return encodeRequestErrorPayload(msg, w);
2189
+ case "goaway":
2190
+ return encodeGoAwayPayload(msg, w);
2191
+ default: {
2192
+ const _exhaustive = msg;
2193
+ throw new Error(`Unhandled message type: ${_exhaustive.type}`);
2194
+ }
2195
+ }
2196
+ }
2197
+ function decodeMessage(bytes) {
2198
+ try {
2199
+ const reader = new MoqtBufferReader(bytes);
2200
+ const typeId = reader.readVarInt();
2201
+ const lenHi = reader.readUint8();
2202
+ const lenLo = reader.readUint8();
2203
+ const payloadLength = lenHi << 8 | lenLo;
2204
+ const payloadBytes = reader.readBytes(payloadLength);
2205
+ const payloadReader = new MoqtBufferReader(payloadBytes);
2206
+ let message;
2207
+ if (typeId === MSG_SETUP) {
2208
+ message = decodeSetupPayload(payloadReader, payloadLength);
2209
+ } else if (typeId === MSG_SUBSCRIBE) {
2210
+ message = decodeSubscribePayload(payloadReader);
2211
+ } else if (typeId === MSG_SUBSCRIBE_OK) {
2212
+ message = decodeSubscribeOkPayload(payloadReader, payloadLength);
2213
+ } else if (typeId === MSG_REQUEST_UPDATE) {
2214
+ message = decodeRequestUpdatePayload(payloadReader);
2215
+ } else if (typeId === MSG_PUBLISH_STATE_NOTIFY) {
2216
+ message = decodePublishStateNotifyPayload(payloadReader);
2217
+ } else if (typeId === MSG_PUBLISH) {
2218
+ message = decodePublishPayload(payloadReader, payloadLength);
2219
+ } else if (typeId === MSG_PUBLISH_DONE) {
2220
+ message = decodePublishDonePayload(payloadReader);
2221
+ } else if (typeId === MSG_PUBLISH_NAMESPACE) {
2222
+ message = decodePublishNamespacePayload(payloadReader);
2223
+ } else if (typeId === MSG_NAMESPACE) {
2224
+ message = decodeNamespacePayload(payloadReader);
2225
+ } else if (typeId === MSG_NAMESPACE_DONE) {
2226
+ message = decodeNamespaceDonePayload(payloadReader);
2227
+ } else if (typeId === MSG_SUBSCRIBE_NAMESPACE) {
2228
+ message = decodeSubscribeNamespacePayload(payloadReader);
2229
+ } else if (typeId === MSG_SUBSCRIBE_TRACKS) {
2230
+ message = decodeSubscribeTracksPayload(payloadReader);
2231
+ } else if (typeId === MSG_PUBLISH_SKIPPED) {
2232
+ message = decodePublishSkippedPayload(payloadReader);
2233
+ } else if (typeId === MSG_FETCH) {
2234
+ message = decodeFetchPayload(payloadReader);
2235
+ } else if (typeId === MSG_FETCH_OK) {
2236
+ message = decodeFetchOkPayload(payloadReader, payloadLength);
2237
+ } else if (typeId === MSG_TRACK_STATUS) {
2238
+ message = decodeTrackStatusPayload(payloadReader);
2239
+ } else if (typeId === MSG_REQUEST_OK) {
2240
+ message = decodeRequestOkPayload(payloadReader, payloadLength);
2241
+ } else if (typeId === MSG_REQUEST_ERROR) {
2242
+ message = decodeRequestErrorPayload(payloadReader, payloadLength);
2243
+ } else if (typeId === MSG_GOAWAY) {
2244
+ message = decodeGoAwayPayload(payloadReader);
2245
+ } else {
2246
+ return {
2247
+ ok: false,
2248
+ error: new DecodeError(
2249
+ "UNKNOWN_MESSAGE_TYPE",
2250
+ `Unknown message type ID: 0x${typeId.toString(16)}`,
2251
+ 0
2252
+ )
2253
+ };
2254
+ }
2255
+ return { ok: true, value: message, bytesRead: reader.offset };
2256
+ } catch (e) {
2257
+ if (e instanceof DecodeError) {
2258
+ return { ok: false, error: e };
2259
+ }
2260
+ throw e;
2261
+ }
2262
+ }
2263
+ function createStreamDecoder() {
2264
+ let buffer = new Uint8Array(0);
2265
+ let offset = 0;
2266
+ return new TransformStream({
2267
+ transform(chunk, controller) {
2268
+ if (offset > 0) {
2269
+ buffer = buffer.subarray(offset);
2270
+ offset = 0;
2271
+ }
2272
+ const newBuffer = new Uint8Array(buffer.length + chunk.length);
2273
+ newBuffer.set(buffer, 0);
2274
+ newBuffer.set(chunk, buffer.length);
2275
+ buffer = newBuffer;
2276
+ while (offset < buffer.length) {
2277
+ const result = decodeMessage(buffer.subarray(offset));
2278
+ if (!result.ok) {
2279
+ if (result.error.code === "UNEXPECTED_END") {
2280
+ break;
2281
+ }
2282
+ controller.error(result.error);
2283
+ return;
2284
+ }
2285
+ controller.enqueue(result.value);
2286
+ offset += result.bytesRead;
2287
+ }
2288
+ },
2289
+ flush(controller) {
2290
+ if (offset < buffer.length) {
2291
+ controller.error(
2292
+ new DecodeError("UNEXPECTED_END", "Stream ended with incomplete message data", 0)
2293
+ );
2294
+ }
2295
+ }
2296
+ });
2297
+ }
2298
+ function createDraft20Codec() {
2299
+ return {
2300
+ draft: "20",
2301
+ encodeMessage,
2302
+ decodeMessage,
2303
+ encodeSubgroupStream,
2304
+ encodeDatagram,
2305
+ encodeFetchStream,
2306
+ decodeSubgroupStream,
2307
+ decodeDatagram,
2308
+ decodeFetchStream,
2309
+ decodeDataStream,
2310
+ createStreamDecoder,
2311
+ createSubgroupStreamDecoder,
2312
+ createFetchStreamDecoder,
2313
+ createDataStreamDecoder
2314
+ };
2315
+ }
2316
+
2317
+ export {
2318
+ MSG_REQUEST_UPDATE,
2319
+ MSG_SUBSCRIBE,
2320
+ MSG_SUBSCRIBE_OK,
2321
+ MSG_REQUEST_ERROR,
2322
+ MSG_PUBLISH_NAMESPACE,
2323
+ MSG_REQUEST_OK,
2324
+ MSG_NAMESPACE,
2325
+ MSG_PUBLISH_DONE,
2326
+ MSG_TRACK_STATUS,
2327
+ MSG_NAMESPACE_DONE,
2328
+ MSG_PUBLISH_SKIPPED,
2329
+ MSG_GOAWAY,
2330
+ MSG_FETCH,
2331
+ MSG_FETCH_OK,
2332
+ MSG_PUBLISH,
2333
+ MSG_PUBLISH_STATE_NOTIFY,
2334
+ MSG_PUBLISH_OK,
2335
+ MSG_SUBSCRIBE_NAMESPACE,
2336
+ MSG_SUBSCRIBE_TRACKS,
2337
+ MSG_SETUP,
2338
+ SETUP_OPT_PATH,
2339
+ SETUP_OPT_AUTHORIZATION_TOKEN,
2340
+ SETUP_OPT_MAX_AUTH_TOKEN_CACHE_SIZE,
2341
+ SETUP_OPT_AUTHORITY,
2342
+ SETUP_OPT_MAX_FILTER_RANGES,
2343
+ SETUP_OPT_MOQT_IMPLEMENTATION,
2344
+ SETUP_OPT_MAX_REQUEST_UPDATES,
2345
+ MESSAGE_TYPE_MAP,
2346
+ MESSAGE_ID_MAP,
2347
+ encodeSubgroupStream,
2348
+ encodeDatagram,
2349
+ encodeFetchStream,
2350
+ decodeSubgroupStream,
2351
+ decodeDatagram,
2352
+ decodeFetchStream,
2353
+ decodeDataStream,
2354
+ createSubgroupStreamDecoder,
2355
+ createFetchStreamDecoder,
2356
+ createDataStreamDecoder,
2357
+ UNKNOWN_STREAM_COUNT,
2358
+ encodeMessage,
2359
+ decodeMessage,
2360
+ createStreamDecoder,
2361
+ createDraft20Codec
2362
+ };