werift 0.25.1 → 0.25.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.mjs +4101 -1781
- package/lib/nonstandard/index.mjs +3582 -1221
- package/lib/polyfill/dom.mjs +16711 -17852
- package/lib/polyfill/index.mjs +16711 -17852
- package/lib/rtp/src/codec/av1.d.ts +61 -1
- package/lib/rtp/src/codec/av1.js +332 -33
- package/lib/rtp/src/codec/av1.js.map +1 -1
- package/lib/rtp/src/codec/base.d.ts +49 -1
- package/lib/rtp/src/codec/base.js +81 -1
- package/lib/rtp/src/codec/base.js.map +1 -1
- package/lib/rtp/src/codec/g711.d.ts +37 -0
- package/lib/rtp/src/codec/g711.js +109 -0
- package/lib/rtp/src/codec/g711.js.map +1 -0
- package/lib/rtp/src/codec/g722.d.ts +31 -0
- package/lib/rtp/src/codec/g722.js +82 -0
- package/lib/rtp/src/codec/g722.js.map +1 -0
- package/lib/rtp/src/codec/h264.d.ts +81 -7
- package/lib/rtp/src/codec/h264.js +382 -21
- package/lib/rtp/src/codec/h264.js.map +1 -1
- package/lib/rtp/src/codec/h265.d.ts +101 -0
- package/lib/rtp/src/codec/h265.js +615 -0
- package/lib/rtp/src/codec/h265.js.map +1 -0
- package/lib/rtp/src/codec/index.d.ts +6 -1
- package/lib/rtp/src/codec/index.js +25 -0
- package/lib/rtp/src/codec/index.js.map +1 -1
- package/lib/rtp/src/codec/mp4a.d.ts +88 -0
- package/lib/rtp/src/codec/mp4a.js +386 -0
- package/lib/rtp/src/codec/mp4a.js.map +1 -0
- package/lib/rtp/src/codec/opus.d.ts +14 -2
- package/lib/rtp/src/codec/opus.js +32 -2
- package/lib/rtp/src/codec/opus.js.map +1 -1
- package/lib/rtp/src/codec/telephoneEvent.d.ts +102 -0
- package/lib/rtp/src/codec/telephoneEvent.js +176 -0
- package/lib/rtp/src/codec/telephoneEvent.js.map +1 -0
- package/lib/rtp/src/codec/vp8.d.ts +26 -1
- package/lib/rtp/src/codec/vp8.js +76 -2
- package/lib/rtp/src/codec/vp8.js.map +1 -1
- package/lib/rtp/src/codec/vp9.d.ts +25 -1
- package/lib/rtp/src/codec/vp9.js +64 -2
- package/lib/rtp/src/codec/vp9.js.map +1 -1
- package/lib/rtp/src/const.d.ts +1 -0
- package/lib/rtp/src/const.js +5 -0
- package/lib/rtp/src/const.js.map +1 -0
- package/lib/rtp/src/extra/container/mp4/container.js +4 -1
- package/lib/rtp/src/extra/container/mp4/container.js.map +1 -1
- package/lib/rtp/src/extra/container/mp4/h264.d.ts +26 -0
- package/lib/rtp/src/extra/container/mp4/h264.js +54 -0
- package/lib/rtp/src/extra/container/mp4/h264.js.map +1 -1
- package/lib/rtp/src/extra/processor/mp4.d.ts +16 -0
- package/lib/rtp/src/extra/processor/mp4.js +54 -20
- package/lib/rtp/src/extra/processor/mp4.js.map +1 -1
- package/lib/rtp/src/index.d.ts +1 -0
- package/lib/rtp/src/index.js +1 -0
- package/lib/rtp/src/index.js.map +1 -1
- package/lib/rtp/src/rtp/mediaClock.d.ts +283 -0
- package/lib/rtp/src/rtp/mediaClock.js +655 -0
- package/lib/rtp/src/rtp/mediaClock.js.map +1 -0
- package/lib/rtp/src/util.d.ts +59 -6
- package/lib/rtp/src/util.js +88 -10
- package/lib/rtp/src/util.js.map +1 -1
- package/lib/webrtc/src/media/codec.d.ts +18 -0
- package/lib/webrtc/src/media/codec.js +43 -2
- package/lib/webrtc/src/media/codec.js.map +1 -1
- package/lib/webrtc/src/media/codecCompatibility.d.ts +5 -0
- package/lib/webrtc/src/media/codecCompatibility.js +15 -4
- package/lib/webrtc/src/media/codecCompatibility.js.map +1 -1
- package/lib/webrtc/src/nonstandard/dummyMedia.js +20 -6
- package/lib/webrtc/src/nonstandard/dummyMedia.js.map +1 -1
- package/lib/webrtc/src/nonstandard/userMedia/packetizer.d.ts +2 -2
- package/lib/webrtc/src/nonstandard/userMedia/packetizer.js +61 -306
- package/lib/webrtc/src/nonstandard/userMedia/packetizer.js.map +1 -1
- package/lib/webrtc/src/nonstandard/userMedia.d.ts +2 -0
- package/lib/webrtc/src/nonstandard/userMedia.js +41 -7
- package/lib/webrtc/src/nonstandard/userMedia.js.map +1 -1
- package/lib/webrtc/src/peerConnection.js +3 -1
- package/lib/webrtc/src/peerConnection.js.map +1 -1
- package/lib/webrtc/src/sdpManager.js +3 -2
- package/lib/webrtc/src/sdpManager.js.map +1 -1
- package/lib/webrtc/src/transceiverManager.js +2 -1
- package/lib/webrtc/src/transceiverManager.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// RFC 6184
|
|
3
|
-
//
|
|
2
|
+
// RFC 6184 — RTP Payload Format for H.264 Video
|
|
3
|
+
// docs/rfc/rfc6184.txt
|
|
4
|
+
//
|
|
5
|
+
// Packetizer targets packetization-mode=1 (Non-Interleaved):
|
|
6
|
+
// Single NAL (§5.6), STAP-A Type=24 (§5.7.1), FU-A Type=28 (§5.8).
|
|
7
|
+
// Keyframe SPS/PPS: STAP-A preferred when ≥2 parameter sets fit MTU;
|
|
8
|
+
// fallback is individual Single NAL (same policy as H265Packetizer AP).
|
|
9
|
+
// Depacketizer output is Annex-B (00 00 00 01 prefixed) for each NAL.
|
|
4
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.NalUnitType = exports.H264RtpPayload = void 0;
|
|
11
|
+
exports.H264Packetizer = exports.H264_MIN_NAL_LENGTH = exports.NalUnitType = exports.H264RtpPayload = void 0;
|
|
12
|
+
exports.splitH264NalUnits = splitH264NalUnits;
|
|
13
|
+
exports.selectMissingH264ParameterSets = selectMissingH264ParameterSets;
|
|
14
|
+
exports.buildH264StapA = buildH264StapA;
|
|
15
|
+
exports.parseAvcDecoderConfigurationRecord = parseAvcDecoderConfigurationRecord;
|
|
6
16
|
const src_1 = require("../../../common/src");
|
|
17
|
+
const base_1 = require("./base");
|
|
18
|
+
const h265_1 = require("./h265");
|
|
7
19
|
// FU indicator octet
|
|
8
20
|
// +---------------+
|
|
9
21
|
// |0|1|2|3|4|5|6|7|
|
|
@@ -57,32 +69,41 @@ class H264RtpPayload {
|
|
|
57
69
|
writable: true,
|
|
58
70
|
value: void 0
|
|
59
71
|
});
|
|
60
|
-
/**start of a fragmented NAL unit */
|
|
72
|
+
/**start of a fragmented NAL unit (FU-A / FU-B only) */
|
|
61
73
|
Object.defineProperty(this, "s", {
|
|
62
74
|
enumerable: true,
|
|
63
75
|
configurable: true,
|
|
64
76
|
writable: true,
|
|
65
77
|
value: void 0
|
|
66
78
|
});
|
|
67
|
-
/**end of a fragmented NAL unit */
|
|
79
|
+
/**end of a fragmented NAL unit (FU-A / FU-B only) */
|
|
68
80
|
Object.defineProperty(this, "e", {
|
|
69
81
|
enumerable: true,
|
|
70
82
|
configurable: true,
|
|
71
83
|
writable: true,
|
|
72
84
|
value: void 0
|
|
73
85
|
});
|
|
86
|
+
/**reserved bit of the FU header (FU-A / FU-B only) */
|
|
74
87
|
Object.defineProperty(this, "r", {
|
|
75
88
|
enumerable: true,
|
|
76
89
|
configurable: true,
|
|
77
90
|
writable: true,
|
|
78
91
|
value: void 0
|
|
79
92
|
});
|
|
93
|
+
/**nal_unit_type of the fragmented NAL unit (FU-A / FU-B only) */
|
|
80
94
|
Object.defineProperty(this, "nalUnitPayloadType", {
|
|
81
95
|
enumerable: true,
|
|
82
96
|
configurable: true,
|
|
83
97
|
writable: true,
|
|
84
98
|
value: void 0
|
|
85
99
|
});
|
|
100
|
+
/**nal_unit_types of the NAL units aggregated in a STAP-A packet */
|
|
101
|
+
Object.defineProperty(this, "aggregatedNalUnitTypes", {
|
|
102
|
+
enumerable: true,
|
|
103
|
+
configurable: true,
|
|
104
|
+
writable: true,
|
|
105
|
+
value: []
|
|
106
|
+
});
|
|
86
107
|
Object.defineProperty(this, "payload", {
|
|
87
108
|
enumerable: true,
|
|
88
109
|
configurable: true,
|
|
@@ -98,36 +119,58 @@ class H264RtpPayload {
|
|
|
98
119
|
}
|
|
99
120
|
static deSerialize(buf, fragment) {
|
|
100
121
|
const h264 = new H264RtpPayload();
|
|
122
|
+
if (buf.length < 1) {
|
|
123
|
+
throw new Error("H.264 RTP payload too short: expected at least 1 byte (NAL/FU indicator)");
|
|
124
|
+
}
|
|
101
125
|
let offset = 0;
|
|
102
126
|
const naluHeader = buf[offset];
|
|
103
127
|
h264.f = (0, src_1.getBit)(naluHeader, 0);
|
|
104
128
|
h264.nri = (0, src_1.getBit)(naluHeader, 1, 2);
|
|
105
129
|
h264.nalUnitType = (0, src_1.getBit)(naluHeader, 3, 5);
|
|
106
130
|
offset++;
|
|
107
|
-
h264.s = (0, src_1.getBit)(buf[offset], 0);
|
|
108
|
-
h264.e = (0, src_1.getBit)(buf[offset], 1);
|
|
109
|
-
h264.r = (0, src_1.getBit)(buf[offset], 2);
|
|
110
|
-
h264.nalUnitPayloadType = (0, src_1.getBit)(buf[offset], 3, 5);
|
|
111
|
-
offset++;
|
|
112
131
|
// デフォルトでは packetization-mode=0
|
|
113
132
|
// packetization-mode=0だとSingle NAL Unit Packetしか来ない
|
|
114
133
|
// https://datatracker.ietf.org/doc/html/rfc6184#section-6.2
|
|
115
|
-
// Single NAL Unit Packet
|
|
134
|
+
// Single NAL Unit Packet — full payload is the NAL (no second header byte)
|
|
116
135
|
if (0 < h264.nalUnitType && h264.nalUnitType < exports.NalUnitType.stap_a) {
|
|
117
136
|
h264.payload = this.packaging(buf);
|
|
137
|
+
return h264;
|
|
138
|
+
}
|
|
139
|
+
// STAP-A / FU-A need at least indicator + one more octet
|
|
140
|
+
if (buf.length < 2) {
|
|
141
|
+
throw new Error(`H.264 RTP payload too short for type ${h264.nalUnitType}: expected at least 2 bytes, got ${buf.length}`);
|
|
142
|
+
}
|
|
143
|
+
// The FU header exists only in FU-A / FU-B (RFC 6184 §5.8).
|
|
144
|
+
// For STAP-A the 2nd octet is the upper byte of the first NALU size (§5.7.1).
|
|
145
|
+
if (h264.nalUnitType === exports.NalUnitType.fu_a ||
|
|
146
|
+
h264.nalUnitType === exports.NalUnitType.fu_b) {
|
|
147
|
+
h264.s = (0, src_1.getBit)(buf[offset], 0);
|
|
148
|
+
h264.e = (0, src_1.getBit)(buf[offset], 1);
|
|
149
|
+
h264.r = (0, src_1.getBit)(buf[offset], 2);
|
|
150
|
+
h264.nalUnitPayloadType = (0, src_1.getBit)(buf[offset], 3, 5);
|
|
151
|
+
offset++;
|
|
118
152
|
}
|
|
119
153
|
// Single-time aggregation packet
|
|
120
|
-
|
|
121
|
-
let
|
|
154
|
+
if (h264.nalUnitType === exports.NalUnitType.stap_a) {
|
|
155
|
+
let stapOffset = stap_aHeaderSize;
|
|
122
156
|
let result = Buffer.alloc(0);
|
|
123
|
-
while (
|
|
124
|
-
|
|
125
|
-
|
|
157
|
+
while (stapOffset < buf.length) {
|
|
158
|
+
if (stapOffset + stap_aNALULengthSize > buf.length) {
|
|
159
|
+
throw new Error("H.264 STAP-A: NALU size field exceeds buffer");
|
|
160
|
+
}
|
|
161
|
+
const naluSize = buf.readUInt16BE(stapOffset);
|
|
162
|
+
stapOffset += stap_aNALULengthSize;
|
|
163
|
+
if (stapOffset + naluSize > buf.length) {
|
|
164
|
+
throw new Error(`H.264 STAP-A: NALU size ${naluSize} exceeds buffer`);
|
|
165
|
+
}
|
|
166
|
+
if (naluSize > 0) {
|
|
167
|
+
h264.aggregatedNalUnitTypes.push((0, src_1.getBit)(buf[stapOffset], 3, 5));
|
|
168
|
+
}
|
|
126
169
|
result = Buffer.concat([
|
|
127
170
|
result,
|
|
128
|
-
this.packaging(buf.subarray(
|
|
171
|
+
this.packaging(buf.subarray(stapOffset, stapOffset + naluSize)),
|
|
129
172
|
]);
|
|
130
|
-
|
|
173
|
+
stapOffset += naluSize;
|
|
131
174
|
}
|
|
132
175
|
h264.payload = result;
|
|
133
176
|
}
|
|
@@ -138,7 +181,7 @@ class H264RtpPayload {
|
|
|
138
181
|
}
|
|
139
182
|
const fu = buf.subarray(offset);
|
|
140
183
|
h264.fragment = Buffer.concat([fragment, fu]);
|
|
141
|
-
if (h264.e) {
|
|
184
|
+
if (h264.e && h264.nalUnitPayloadType !== undefined) {
|
|
142
185
|
const bitStream = new src_1.BitStream(Buffer.alloc(1))
|
|
143
186
|
.writeBits(1, 0)
|
|
144
187
|
.writeBits(2, h264.nri)
|
|
@@ -156,9 +199,19 @@ class H264RtpPayload {
|
|
|
156
199
|
static isDetectedFinalPacketInSequence(header) {
|
|
157
200
|
return header.marker;
|
|
158
201
|
}
|
|
202
|
+
/** true only when this packet carries (a part of) an IDR slice */
|
|
159
203
|
get isKeyframe() {
|
|
160
|
-
|
|
161
|
-
|
|
204
|
+
switch (this.nalUnitType) {
|
|
205
|
+
case exports.NalUnitType.idrSlice:
|
|
206
|
+
return true;
|
|
207
|
+
case exports.NalUnitType.fu_a:
|
|
208
|
+
case exports.NalUnitType.fu_b:
|
|
209
|
+
return this.nalUnitPayloadType === exports.NalUnitType.idrSlice;
|
|
210
|
+
case exports.NalUnitType.stap_a:
|
|
211
|
+
return this.aggregatedNalUnitTypes.includes(exports.NalUnitType.idrSlice);
|
|
212
|
+
default:
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
162
215
|
}
|
|
163
216
|
get isPartitionHead() {
|
|
164
217
|
if (this.nalUnitType === exports.NalUnitType.fu_a ||
|
|
@@ -171,6 +224,8 @@ class H264RtpPayload {
|
|
|
171
224
|
exports.H264RtpPayload = H264RtpPayload;
|
|
172
225
|
exports.NalUnitType = {
|
|
173
226
|
idrSlice: 5,
|
|
227
|
+
sps: 7,
|
|
228
|
+
pps: 8,
|
|
174
229
|
stap_a: 24,
|
|
175
230
|
stap_b: 25,
|
|
176
231
|
mtap16: 26,
|
|
@@ -181,4 +236,310 @@ exports.NalUnitType = {
|
|
|
181
236
|
const annex_bNALUStartCode = Buffer.from([0x00, 0x00, 0x00, 0x01]);
|
|
182
237
|
const stap_aHeaderSize = 1;
|
|
183
238
|
const stap_aNALULengthSize = 2;
|
|
239
|
+
// ---------------------------------------------------------------------------
|
|
240
|
+
// Annex-B / length-prefixed (AVCC) NAL splitting
|
|
241
|
+
// Annex-B syntax splitting is codec-agnostic (start codes only) and shared with
|
|
242
|
+
// H.265; NAL contents are never interpreted or filtered at this stage.
|
|
243
|
+
// ---------------------------------------------------------------------------
|
|
244
|
+
function looksLikeAnnexB(sample) {
|
|
245
|
+
if (sample.length < 3) {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
return ((sample[0] === 0 &&
|
|
249
|
+
sample[1] === 0 &&
|
|
250
|
+
sample[2] === 0 &&
|
|
251
|
+
sample[3] === 1) ||
|
|
252
|
+
(sample[0] === 0 && sample[1] === 0 && sample[2] === 1));
|
|
253
|
+
}
|
|
254
|
+
function readUintLength(buf, offset, size) {
|
|
255
|
+
if (size === 1)
|
|
256
|
+
return buf[offset];
|
|
257
|
+
if (size === 2)
|
|
258
|
+
return buf.readUInt16BE(offset);
|
|
259
|
+
if (size === 3) {
|
|
260
|
+
return (buf[offset] << 16) | (buf[offset + 1] << 8) | buf[offset + 2];
|
|
261
|
+
}
|
|
262
|
+
return buf.readUInt32BE(offset);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Split H.264 sample into NAL units (no start codes).
|
|
266
|
+
* Accepts Annex-B (start-code split, no NAL type filtering) or length-prefixed (AVCC).
|
|
267
|
+
*/
|
|
268
|
+
function splitH264NalUnits(sample, naluLengthSize = 4, inputFormat = "auto") {
|
|
269
|
+
if (naluLengthSize < 1 ||
|
|
270
|
+
naluLengthSize > 4 ||
|
|
271
|
+
!Number.isInteger(naluLengthSize)) {
|
|
272
|
+
throw new Error(`H.264 naluLengthSize must be an integer 1–4, got ${naluLengthSize}`);
|
|
273
|
+
}
|
|
274
|
+
if (sample.length === 0) {
|
|
275
|
+
return [];
|
|
276
|
+
}
|
|
277
|
+
const format = inputFormat === "auto"
|
|
278
|
+
? looksLikeAnnexB(sample)
|
|
279
|
+
? "annexb"
|
|
280
|
+
: "length-prefixed"
|
|
281
|
+
: inputFormat;
|
|
282
|
+
if (format === "annexb") {
|
|
283
|
+
if (!looksLikeAnnexB(sample)) {
|
|
284
|
+
throw new Error("H.264 inputFormat annexb: sample does not start with a start code");
|
|
285
|
+
}
|
|
286
|
+
// Pure syntax split. H264AnnexBParser (MP4 demux helper) drops NAL types
|
|
287
|
+
// >= 14 (prefix NAL, subset SPS, slice extension, ...), but RFC 6184 §5.6
|
|
288
|
+
// carries types 1-23 as Single NAL units, so nothing may be filtered here.
|
|
289
|
+
return new h265_1.H265AnnexBParser(sample).readAll();
|
|
290
|
+
}
|
|
291
|
+
const prefixed = parseLengthPrefixed(sample, naluLengthSize);
|
|
292
|
+
if (prefixed.nalus) {
|
|
293
|
+
return prefixed.nalus;
|
|
294
|
+
}
|
|
295
|
+
throw new Error(prefixed.error);
|
|
296
|
+
}
|
|
297
|
+
function parseLengthPrefixed(sample, naluLengthSize) {
|
|
298
|
+
const nalus = [];
|
|
299
|
+
let offset = 0;
|
|
300
|
+
while (offset < sample.length) {
|
|
301
|
+
if (offset + naluLengthSize > sample.length) {
|
|
302
|
+
return { error: "H.264 length-prefixed sample truncated (length field)" };
|
|
303
|
+
}
|
|
304
|
+
const length = readUintLength(sample, offset, naluLengthSize);
|
|
305
|
+
offset += naluLengthSize;
|
|
306
|
+
if (length === 0) {
|
|
307
|
+
return { error: "H.264 length-prefixed sample: NAL length 0 is invalid" };
|
|
308
|
+
}
|
|
309
|
+
if (offset + length > sample.length) {
|
|
310
|
+
return {
|
|
311
|
+
error: `H.264 length-prefixed sample: NAL length ${length} exceeds buffer`,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
nalus.push(sample.subarray(offset, offset + length));
|
|
315
|
+
offset += length;
|
|
316
|
+
}
|
|
317
|
+
return { nalus, error: "" };
|
|
318
|
+
}
|
|
319
|
+
function h264NalType(nal) {
|
|
320
|
+
if (nal.length < 1)
|
|
321
|
+
return 0;
|
|
322
|
+
return nal[0] & 0x1f;
|
|
323
|
+
}
|
|
324
|
+
function isH264ParameterSet(type) {
|
|
325
|
+
return type === exports.NalUnitType.sps || type === exports.NalUnitType.pps;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* From configured `parameterSets`, return only those whose type is not already
|
|
329
|
+
* present in the sample (SPS and PPS checked independently).
|
|
330
|
+
* E.g. sample with SPS-only → only missing PPS are returned.
|
|
331
|
+
*/
|
|
332
|
+
function selectMissingH264ParameterSets(parameterSets, nalUnits) {
|
|
333
|
+
const hasSps = nalUnits.some((n) => h264NalType(n) === exports.NalUnitType.sps);
|
|
334
|
+
const hasPps = nalUnits.some((n) => h264NalType(n) === exports.NalUnitType.pps);
|
|
335
|
+
return parameterSets.filter((ps) => {
|
|
336
|
+
const t = h264NalType(ps);
|
|
337
|
+
if (t === exports.NalUnitType.sps)
|
|
338
|
+
return !hasSps;
|
|
339
|
+
if (t === exports.NalUnitType.pps)
|
|
340
|
+
return !hasPps;
|
|
341
|
+
return false;
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Build STAP-A payload (RFC 6184 §5.7.1 / Figure 7):
|
|
346
|
+
* STAP-A header (F/NRI/Type=24) + repeated [16-bit NALU size][NAL]
|
|
347
|
+
* F = OR of F of aggregated NALs; NRI = max of NRI of aggregated NALs.
|
|
348
|
+
*/
|
|
349
|
+
/** Minimum NAL size: 1-byte header + at least 1 payload octet (packetizer contract). */
|
|
350
|
+
exports.H264_MIN_NAL_LENGTH = 2;
|
|
351
|
+
/**
|
|
352
|
+
* End of sequence (10) / end of stream (11) NALs have an empty RBSP, so the
|
|
353
|
+
* 1-byte header alone is valid (ITU-T H.264 §7.4.1.2.4 / §7.3.2.5-6).
|
|
354
|
+
*/
|
|
355
|
+
function minH264NalLength(nal) {
|
|
356
|
+
const type = nal.length > 0 ? nal[0] & 0x1f : 0;
|
|
357
|
+
return type === 10 || type === 11 ? 1 : exports.H264_MIN_NAL_LENGTH;
|
|
358
|
+
}
|
|
359
|
+
function buildH264StapA(nalus) {
|
|
360
|
+
if (nalus.length < 2) {
|
|
361
|
+
throw new Error("H.264 STAP-A requires at least two NAL units");
|
|
362
|
+
}
|
|
363
|
+
let f = 0;
|
|
364
|
+
let nri = 0;
|
|
365
|
+
for (const n of nalus) {
|
|
366
|
+
if (n.length < minH264NalLength(n)) {
|
|
367
|
+
throw new Error(`H.264 STAP-A: NAL unit too short (need ≥${minH264NalLength(n)} bytes, got ${n.length})`);
|
|
368
|
+
}
|
|
369
|
+
if (n.length > 0xffff) {
|
|
370
|
+
throw new Error("H.264 NAL unit too large for STAP-A size field");
|
|
371
|
+
}
|
|
372
|
+
f |= (n[0] >> 7) & 1;
|
|
373
|
+
const nriN = (n[0] >> 5) & 0x03;
|
|
374
|
+
if (nriN > nri)
|
|
375
|
+
nri = nriN;
|
|
376
|
+
}
|
|
377
|
+
const header = Buffer.from([(f << 7) | (nri << 5) | exports.NalUnitType.stap_a]);
|
|
378
|
+
const parts = [header];
|
|
379
|
+
for (const n of nalus) {
|
|
380
|
+
const size = Buffer.alloc(2);
|
|
381
|
+
size.writeUInt16BE(n.length, 0);
|
|
382
|
+
parts.push(size, n);
|
|
383
|
+
}
|
|
384
|
+
return Buffer.concat(parts);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Packetize H.264 access units (Annex-B or length-prefixed) for mode=1.
|
|
388
|
+
* Single NAL / STAP-A (SPS+PPS) / FU-A fragmentation (RFC 6184).
|
|
389
|
+
*/
|
|
390
|
+
class H264Packetizer extends base_1.PacketizerBase {
|
|
391
|
+
constructor(options = {}) {
|
|
392
|
+
super(options);
|
|
393
|
+
Object.defineProperty(this, "naluLengthSize", {
|
|
394
|
+
enumerable: true,
|
|
395
|
+
configurable: true,
|
|
396
|
+
writable: true,
|
|
397
|
+
value: void 0
|
|
398
|
+
});
|
|
399
|
+
Object.defineProperty(this, "inputFormat", {
|
|
400
|
+
enumerable: true,
|
|
401
|
+
configurable: true,
|
|
402
|
+
writable: true,
|
|
403
|
+
value: void 0
|
|
404
|
+
});
|
|
405
|
+
Object.defineProperty(this, "parameterSets", {
|
|
406
|
+
enumerable: true,
|
|
407
|
+
configurable: true,
|
|
408
|
+
writable: true,
|
|
409
|
+
value: void 0
|
|
410
|
+
});
|
|
411
|
+
Object.defineProperty(this, "defaultIsKeyframe", {
|
|
412
|
+
enumerable: true,
|
|
413
|
+
configurable: true,
|
|
414
|
+
writable: true,
|
|
415
|
+
value: void 0
|
|
416
|
+
});
|
|
417
|
+
const nls = options.naluLengthSize ?? 4;
|
|
418
|
+
if (nls < 1 || nls > 4 || !Number.isInteger(nls)) {
|
|
419
|
+
throw new Error(`H264Packetizer: naluLengthSize must be an integer 1–4, got ${nls}`);
|
|
420
|
+
}
|
|
421
|
+
this.naluLengthSize = nls;
|
|
422
|
+
this.inputFormat = options.inputFormat ?? "auto";
|
|
423
|
+
this.parameterSets = options.parameterSets ?? [];
|
|
424
|
+
this.defaultIsKeyframe = options.isKeyframe;
|
|
425
|
+
}
|
|
426
|
+
packetize(data, rtpTimestamp, options = {}) {
|
|
427
|
+
let nalUnits = splitH264NalUnits(data, this.naluLengthSize, this.inputFormat);
|
|
428
|
+
if (nalUnits.length === 0) {
|
|
429
|
+
return [];
|
|
430
|
+
}
|
|
431
|
+
const hasIdr = nalUnits.some((n) => h264NalType(n) === exports.NalUnitType.idrSlice);
|
|
432
|
+
const isKey = options.isKeyframe ?? this.defaultIsKeyframe ?? hasIdr;
|
|
433
|
+
// Prepend only parameter-set types absent from the sample (SPS ≠ PPS).
|
|
434
|
+
if (isKey && this.parameterSets.length > 0) {
|
|
435
|
+
const missing = selectMissingH264ParameterSets(this.parameterSets, nalUnits);
|
|
436
|
+
if (missing.length > 0) {
|
|
437
|
+
// An access unit delimiter (type 9) must stay first in the AU, so the
|
|
438
|
+
// parameter sets go right after it (ITU-T H.264 §7.4.1.2.3).
|
|
439
|
+
const at = nalUnits.findIndex((n) => h264NalType(n) !== 9);
|
|
440
|
+
const insertAt = at === -1 ? nalUnits.length : at;
|
|
441
|
+
nalUnits = [
|
|
442
|
+
...nalUnits.slice(0, insertAt),
|
|
443
|
+
...missing,
|
|
444
|
+
...nalUnits.slice(insertAt),
|
|
445
|
+
];
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
const packets = [];
|
|
449
|
+
// Try STAP-A for leading parameter sets (SPS/PPS), same policy as H265 AP
|
|
450
|
+
let index = 0;
|
|
451
|
+
if (nalUnits.length >= 2 && isH264ParameterSet(h264NalType(nalUnits[0]))) {
|
|
452
|
+
const aggregated = [];
|
|
453
|
+
let stapSize = 1; // STAP-A header
|
|
454
|
+
while (index < nalUnits.length &&
|
|
455
|
+
isH264ParameterSet(h264NalType(nalUnits[index]))) {
|
|
456
|
+
const n = nalUnits[index];
|
|
457
|
+
const need = 2 + n.length;
|
|
458
|
+
if (stapSize + need > this.maxPayloadSize) {
|
|
459
|
+
break;
|
|
460
|
+
}
|
|
461
|
+
aggregated.push(n);
|
|
462
|
+
stapSize += need;
|
|
463
|
+
index++;
|
|
464
|
+
}
|
|
465
|
+
// STAP-A requires at least two NAL units (RFC 6184 §5.7.1)
|
|
466
|
+
if (aggregated.length >= 2) {
|
|
467
|
+
const isLast = index >= nalUnits.length;
|
|
468
|
+
packets.push(this.buildPacket(buildH264StapA(aggregated), rtpTimestamp, isLast));
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
// Cannot STAP-A (one set only, or too large) → fall back to Single NAL
|
|
472
|
+
index = 0;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
for (; index < nalUnits.length; index++) {
|
|
476
|
+
const nal = nalUnits[index];
|
|
477
|
+
const marker = index === nalUnits.length - 1;
|
|
478
|
+
// Reject empty NALs and 1-byte NALs other than end-of-seq/stream (FU needs ≥2 octets)
|
|
479
|
+
if (nal.length < minH264NalLength(nal)) {
|
|
480
|
+
throw new Error(`H.264 NAL unit too short: expected at least ${minH264NalLength(nal)} bytes (NAL header + data), got ${nal.length}`);
|
|
481
|
+
}
|
|
482
|
+
if (nal.length <= this.maxPayloadSize) {
|
|
483
|
+
// Single NAL Unit Packet (RFC 6184 §5.6)
|
|
484
|
+
packets.push(this.buildPacket(nal, rtpTimestamp, marker));
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
// FU-A (RFC 6184 §5.8)
|
|
488
|
+
const nalHeader = nal[0];
|
|
489
|
+
const fragmentPayload = nal.subarray(1);
|
|
490
|
+
const fragmentSize = this.maxPayloadSize - 2; // FU indicator + FU header
|
|
491
|
+
if (fragmentSize <= 0) {
|
|
492
|
+
throw new Error("invalid H.264 RTP maxPayloadSize for FU-A");
|
|
493
|
+
}
|
|
494
|
+
const fuIndicator = (nalHeader & 0xe0) | exports.NalUnitType.fu_a;
|
|
495
|
+
const nalType = nalHeader & 0x1f;
|
|
496
|
+
for (let offset = 0; offset < fragmentPayload.length; offset += fragmentSize) {
|
|
497
|
+
const chunk = fragmentPayload.subarray(offset, Math.min(fragmentPayload.length, offset + fragmentSize));
|
|
498
|
+
const isStart = offset === 0;
|
|
499
|
+
const isEnd = offset + chunk.length >= fragmentPayload.length;
|
|
500
|
+
// S|E|R|Type
|
|
501
|
+
const fuHeader = Buffer.from([
|
|
502
|
+
(isStart ? 0x80 : 0x00) | (isEnd ? 0x40 : 0x00) | nalType,
|
|
503
|
+
]);
|
|
504
|
+
packets.push(this.buildPacket(Buffer.concat([Buffer.from([fuIndicator]), fuHeader, chunk]), rtpTimestamp, marker && isEnd));
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
return packets;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
exports.H264Packetizer = H264Packetizer;
|
|
511
|
+
/**
|
|
512
|
+
* Parse an avcC record (e.g. WebCodecs / mediabunny `decoderConfig.description`)
|
|
513
|
+
* into the NAL length size and SPS/PPS NAL units (no length prefix).
|
|
514
|
+
* Throws on a truncated record instead of returning partial parameter sets.
|
|
515
|
+
*/
|
|
516
|
+
function parseAvcDecoderConfigurationRecord(description) {
|
|
517
|
+
const record = ArrayBuffer.isView(description)
|
|
518
|
+
? Buffer.from(description.buffer, description.byteOffset, description.byteLength)
|
|
519
|
+
: Buffer.from(description);
|
|
520
|
+
if (record.length < 7) {
|
|
521
|
+
throw new Error(`invalid H.264 avcC: expected at least 7 bytes, got ${record.length}`);
|
|
522
|
+
}
|
|
523
|
+
const naluLengthSize = (record[4] & 0x03) + 1;
|
|
524
|
+
let offset = 5;
|
|
525
|
+
const readSets = (count) => {
|
|
526
|
+
const sets = [];
|
|
527
|
+
for (let i = 0; i < count; i++) {
|
|
528
|
+
if (offset + 2 > record.length) {
|
|
529
|
+
throw new Error("invalid H.264 avcC: truncated parameter set length");
|
|
530
|
+
}
|
|
531
|
+
const length = record.readUInt16BE(offset);
|
|
532
|
+
offset += 2;
|
|
533
|
+
if (offset + length > record.length) {
|
|
534
|
+
throw new Error("invalid H.264 avcC: truncated parameter set");
|
|
535
|
+
}
|
|
536
|
+
sets.push(record.subarray(offset, offset + length));
|
|
537
|
+
offset += length;
|
|
538
|
+
}
|
|
539
|
+
return sets;
|
|
540
|
+
};
|
|
541
|
+
const sps = readSets(record[offset++] & 0x1f);
|
|
542
|
+
const pps = offset < record.length ? readSets(record[offset++]) : [];
|
|
543
|
+
return { naluLengthSize, sps, pps, parameterSets: [...sps, ...pps] };
|
|
544
|
+
}
|
|
184
545
|
//# sourceMappingURL=h264.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"h264.js","sourceRoot":"","sources":["../../../../../rtp/src/codec/h264.ts"],"names":[],"mappings":";AAAA,gDAAgD;AAChD,WAAW;;;AAEX,6CAAwD;AAIxD,qBAAqB;AACrB,oBAAoB;AACpB,oBAAoB;AACpB,oBAAoB;AACpB,oBAAoB;AACpB,oBAAoB;AAEpB,YAAY;AACZ,oBAAoB;AACpB,oBAAoB;AACpB,oBAAoB;AACpB,oBAAoB;AACpB,oBAAoB;AAEpB,kEA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KAAK,mBAAW,CAAC,IAAI,EACrC,CAAC;YACD,OAAO,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAxGD,wCAwGC;AAEY,QAAA,WAAW,GAAG;IACzB,QAAQ,EAAE,CAAC;IACX,MAAM,EAAE,EAAE;IACV,MAAM,EAAE,EAAE;IACV,MAAM,EAAE,EAAE;IACV,MAAM,EAAE,EAAE;IACV,IAAI,EAAE,EAAE;IACR,IAAI,EAAE,EAAE;CACA,CAAC;AAEX,MAAM,oBAAoB,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAEnE,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAC3B,MAAM,oBAAoB,GAAG,CAAC,CAAC","sourcesContent":["// RFC 6184 - RTP Payload Format for H.264 Video\n// pion/rtp\n\nimport { BitStream, getBit } from \"../../../common/src\";\nimport type { RtpHeader } from \"../rtp/rtp\";\nimport type { DePacketizerBase } from \"./base\";\n\n// FU indicator octet\n// +---------------+\n// |0|1|2|3|4|5|6|7|\n// +-+-+-+-+-+-+-+-+\n// |F|NRI| Type |\n// +---------------+\n\n// FU header\n// +---------------+\n// |0|1|2|3|4|5|6|7|\n// +-+-+-+-+-+-+-+-+\n// |S|E|R| Type |\n// +---------------+\n\n// NAL Unit Type Content of NAL Unit NRI (binary)\n// ----------------------------------------------------------------\n// 1 non-IDR coded slice 10\n// 2 Coded slice data partition A 10\n// 3 Coded slice data partition B 01\n// 4 Coded slice data partition C 01\n\n// Payload Packet Single NAL Non-Interleaved Interleaved\n// Type Type Unit Mode Mode Mode\n// -------------------------------------------------------------\n// 0 reserved ig ig ig\n// 1-23 NAL unit yes yes no\n// 24 STAP-A no yes no\n// 25 STAP-B no no yes\n// 26 MTAP16 no no yes\n// 27 MTAP24 no no yes\n// 28 FU-A no yes yes\n// 29 FU-B no no yes\n// 30-31 reserved ig ig ig\n\nexport class H264RtpPayload implements DePacketizerBase {\n /**forbidden_zero_bit */\n f!: number;\n /**nal_ref_idc */\n nri!: number;\n /**nal_unit_types */\n nalUnitType!: number;\n /**start of a fragmented NAL unit */\n s!: number;\n /**end of a fragmented NAL unit */\n e!: number;\n r!: number;\n nalUnitPayloadType!: number;\n payload!: Buffer;\n fragment?: Buffer;\n\n static deSerialize(buf: Buffer, fragment?: Buffer) {\n const h264 = new H264RtpPayload();\n\n let offset = 0;\n\n const naluHeader = buf[offset];\n h264.f = getBit(naluHeader, 0);\n h264.nri = getBit(naluHeader, 1, 2);\n h264.nalUnitType = getBit(naluHeader, 3, 5);\n offset++;\n\n h264.s = getBit(buf[offset], 0);\n h264.e = getBit(buf[offset], 1);\n h264.r = getBit(buf[offset], 2);\n h264.nalUnitPayloadType = getBit(buf[offset], 3, 5);\n offset++;\n\n // デフォルトでは packetization-mode=0\n // packetization-mode=0だとSingle NAL Unit Packetしか来ない\n // https://datatracker.ietf.org/doc/html/rfc6184#section-6.2\n\n // Single NAL Unit Packet\n if (0 < h264.nalUnitType && h264.nalUnitType < NalUnitType.stap_a) {\n h264.payload = this.packaging(buf);\n }\n // Single-time aggregation packet\n else if (h264.nalUnitType === NalUnitType.stap_a) {\n let offset = stap_aHeaderSize;\n let result: Buffer = Buffer.alloc(0);\n while (offset < buf.length) {\n const naluSize = buf.readUInt16BE(offset);\n offset += stap_aNALULengthSize;\n\n result = Buffer.concat([\n result,\n this.packaging(buf.subarray(offset, offset + naluSize)),\n ]);\n offset += naluSize;\n }\n h264.payload = result;\n }\n // Fragmentation Units\n else if (h264.nalUnitType === NalUnitType.fu_a) {\n if (!fragment) {\n fragment = Buffer.alloc(0);\n }\n const fu = buf.subarray(offset);\n h264.fragment = Buffer.concat([fragment, fu]);\n\n if (h264.e) {\n const bitStream = new BitStream(Buffer.alloc(1))\n .writeBits(1, 0)\n .writeBits(2, h264.nri)\n .writeBits(5, h264.nalUnitPayloadType);\n const nalu = Buffer.concat([bitStream.uint8Array, h264.fragment]);\n h264.fragment = undefined;\n h264.payload = this.packaging(nalu);\n }\n }\n\n return h264;\n }\n\n private static packaging(buf: Buffer) {\n return Buffer.concat([annex_bNALUStartCode, buf]);\n }\n\n static isDetectedFinalPacketInSequence(header: RtpHeader) {\n return header.marker;\n }\n\n get isKeyframe() {\n return (\n this.nalUnitType === NalUnitType.idrSlice ||\n this.nalUnitPayloadType === NalUnitType.idrSlice\n );\n }\n\n get isPartitionHead() {\n if (\n this.nalUnitType === NalUnitType.fu_a ||\n this.nalUnitType === NalUnitType.fu_b\n ) {\n return this.s !== 0;\n }\n\n return true;\n }\n}\n\nexport const NalUnitType = {\n idrSlice: 5,\n stap_a: 24,\n stap_b: 25,\n mtap16: 26,\n mtap24: 27,\n fu_a: 28,\n fu_b: 29,\n} as const;\n\nconst annex_bNALUStartCode = Buffer.from([0x00, 0x00, 0x00, 0x01]);\n\nconst stap_aHeaderSize = 1;\nconst stap_aNALULengthSize = 2;\n"]}
|
|
1
|
+
{"version":3,"file":"h264.js","sourceRoot":"","sources":["../../../../../rtp/src/codec/h264.ts"],"names":[],"mappings":";AAAA,gDAAgD;AAChD,uBAAuB;AACvB,EAAE;AACF,6DAA6D;AAC7D,qEAAqE;AACrE,qEAAqE;AACrE,wEAAwE;AACxE,sEAAsE;;;AAmPtE,8CAuCC;AA0CD,wEAYC;AAmBD,wCA2BC;AAsMD,gFAsCC;AAxmBD,6CAAwD;AAGxD,iCAIgB;AAChB,iCAA0C;AAE1C,qBAAqB;AACrB,oBAAoB;AACpB,oBAAoB;AACpB,oBAAoB;AACpB,oBAAoB;AACpB,oBAAoB;AAEpB,YAAY;AACZ,oBAAoB;AACpB,oBAAoB;AACpB,oBAAoB;AACpB,oBAAoB;AACpB,oBAAoB;AAEpB,kEAAkE;AAClE,mEAAmE;AACnE,iEAAiE;AACjE,iEAAiE;AACjE,iEAAiE;AACjE,iEAAiE;AAEjE,iEAAiE;AACjE,8DAA8D;A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,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC;YACpD,MAAM,IAAI,MAAM,CAAC;QACnB,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,MAAM,GAAG,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;IAC9C,MAAM,GAAG,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IACrE,OAAO,EAAE,cAAc,EAAE,GAAG,EAAE,GAAG,EAAE,aAAa,EAAE,CAAC,GAAG,GAAG,EAAE,GAAG,GAAG,CAAC,EAAE,CAAC;AACvE,CAAC","sourcesContent":["// RFC 6184 — RTP Payload Format for H.264 Video\n// docs/rfc/rfc6184.txt\n//\n// Packetizer targets packetization-mode=1 (Non-Interleaved):\n// Single NAL (§5.6), STAP-A Type=24 (§5.7.1), FU-A Type=28 (§5.8).\n// Keyframe SPS/PPS: STAP-A preferred when ≥2 parameter sets fit MTU;\n// fallback is individual Single NAL (same policy as H265Packetizer AP).\n// Depacketizer output is Annex-B (00 00 00 01 prefixed) for each NAL.\n\nimport { BitStream, getBit } from \"../../../common/src\";\nimport type { RtpHeader, RtpPacket } from \"../rtp/rtp\";\nimport type { DePacketizerBase } from \"./base\";\nimport {\n type NalInputFormat,\n PacketizerBase,\n type PacketizerBaseOptions,\n} from \"./base\";\nimport { H265AnnexBParser } from \"./h265\";\n\n// FU indicator octet\n// +---------------+\n// |0|1|2|3|4|5|6|7|\n// +-+-+-+-+-+-+-+-+\n// |F|NRI| Type |\n// +---------------+\n\n// FU header\n// +---------------+\n// |0|1|2|3|4|5|6|7|\n// +-+-+-+-+-+-+-+-+\n// |S|E|R| Type |\n// +---------------+\n\n// NAL Unit Type Content of NAL Unit NRI (binary)\n// ----------------------------------------------------------------\n// 1 non-IDR coded slice 10\n// 2 Coded slice data partition A 10\n// 3 Coded slice data partition B 01\n// 4 Coded slice data partition C 01\n\n// Payload Packet Single NAL Non-Interleaved Interleaved\n// Type Type Unit Mode Mode Mode\n// -------------------------------------------------------------\n// 0 reserved ig ig ig\n// 1-23 NAL unit yes yes no\n// 24 STAP-A no yes no\n// 25 STAP-B no no yes\n// 26 MTAP16 no no yes\n// 27 MTAP24 no no yes\n// 28 FU-A no yes yes\n// 29 FU-B no no yes\n// 30-31 reserved ig ig ig\n\nexport class H264RtpPayload implements DePacketizerBase {\n /**forbidden_zero_bit */\n f!: number;\n /**nal_ref_idc */\n nri!: number;\n /**nal_unit_types */\n nalUnitType!: number;\n /**start of a fragmented NAL unit (FU-A / FU-B only) */\n s?: number;\n /**end of a fragmented NAL unit (FU-A / FU-B only) */\n e?: number;\n /**reserved bit of the FU header (FU-A / FU-B only) */\n r?: number;\n /**nal_unit_type of the fragmented NAL unit (FU-A / FU-B only) */\n nalUnitPayloadType?: number;\n /**nal_unit_types of the NAL units aggregated in a STAP-A packet */\n aggregatedNalUnitTypes: number[] = [];\n payload!: Buffer;\n fragment?: Buffer;\n\n static deSerialize(buf: Buffer, fragment?: Buffer) {\n const h264 = new H264RtpPayload();\n\n if (buf.length < 1) {\n throw new Error(\n \"H.264 RTP payload too short: expected at least 1 byte (NAL/FU indicator)\",\n );\n }\n\n let offset = 0;\n\n const naluHeader = buf[offset];\n h264.f = getBit(naluHeader, 0);\n h264.nri = getBit(naluHeader, 1, 2);\n h264.nalUnitType = getBit(naluHeader, 3, 5);\n offset++;\n\n // デフォルトでは packetization-mode=0\n // packetization-mode=0だとSingle NAL Unit Packetしか来ない\n // https://datatracker.ietf.org/doc/html/rfc6184#section-6.2\n\n // Single NAL Unit Packet — full payload is the NAL (no second header byte)\n if (0 < h264.nalUnitType && h264.nalUnitType < NalUnitType.stap_a) {\n h264.payload = this.packaging(buf);\n return h264;\n }\n\n // STAP-A / FU-A need at least indicator + one more octet\n if (buf.length < 2) {\n throw new Error(\n `H.264 RTP payload too short for type ${h264.nalUnitType}: expected at least 2 bytes, got ${buf.length}`,\n );\n }\n\n // The FU header exists only in FU-A / FU-B (RFC 6184 §5.8).\n // For STAP-A the 2nd octet is the upper byte of the first NALU size (§5.7.1).\n if (\n h264.nalUnitType === NalUnitType.fu_a ||\n h264.nalUnitType === NalUnitType.fu_b\n ) {\n h264.s = getBit(buf[offset], 0);\n h264.e = getBit(buf[offset], 1);\n h264.r = getBit(buf[offset], 2);\n h264.nalUnitPayloadType = getBit(buf[offset], 3, 5);\n offset++;\n }\n\n // Single-time aggregation packet\n if (h264.nalUnitType === NalUnitType.stap_a) {\n let stapOffset = stap_aHeaderSize;\n let result: Buffer = Buffer.alloc(0);\n while (stapOffset < buf.length) {\n if (stapOffset + stap_aNALULengthSize > buf.length) {\n throw new Error(\"H.264 STAP-A: NALU size field exceeds buffer\");\n }\n const naluSize = buf.readUInt16BE(stapOffset);\n stapOffset += stap_aNALULengthSize;\n if (stapOffset + naluSize > buf.length) {\n throw new Error(`H.264 STAP-A: NALU size ${naluSize} exceeds buffer`);\n }\n if (naluSize > 0) {\n h264.aggregatedNalUnitTypes.push(getBit(buf[stapOffset], 3, 5));\n }\n result = Buffer.concat([\n result,\n this.packaging(buf.subarray(stapOffset, stapOffset + naluSize)),\n ]);\n stapOffset += naluSize;\n }\n h264.payload = result;\n }\n // Fragmentation Units\n else if (h264.nalUnitType === NalUnitType.fu_a) {\n if (!fragment) {\n fragment = Buffer.alloc(0);\n }\n const fu = buf.subarray(offset);\n h264.fragment = Buffer.concat([fragment, fu]);\n\n if (h264.e && h264.nalUnitPayloadType !== undefined) {\n const bitStream = new BitStream(Buffer.alloc(1))\n .writeBits(1, 0)\n .writeBits(2, h264.nri)\n .writeBits(5, h264.nalUnitPayloadType);\n const nalu = Buffer.concat([bitStream.uint8Array, h264.fragment]);\n h264.fragment = undefined;\n h264.payload = this.packaging(nalu);\n }\n }\n\n return h264;\n }\n\n private static packaging(buf: Buffer) {\n return Buffer.concat([annex_bNALUStartCode, buf]);\n }\n\n static isDetectedFinalPacketInSequence(header: RtpHeader) {\n return header.marker;\n }\n\n /** true only when this packet carries (a part of) an IDR slice */\n get isKeyframe() {\n switch (this.nalUnitType) {\n case NalUnitType.idrSlice:\n return true;\n case NalUnitType.fu_a:\n case NalUnitType.fu_b:\n return this.nalUnitPayloadType === NalUnitType.idrSlice;\n case NalUnitType.stap_a:\n return this.aggregatedNalUnitTypes.includes(NalUnitType.idrSlice);\n default:\n return false;\n }\n }\n\n get isPartitionHead() {\n if (\n this.nalUnitType === NalUnitType.fu_a ||\n this.nalUnitType === NalUnitType.fu_b\n ) {\n return this.s !== 0;\n }\n\n return true;\n }\n}\n\nexport const NalUnitType = {\n idrSlice: 5,\n sps: 7,\n pps: 8,\n stap_a: 24,\n stap_b: 25,\n mtap16: 26,\n mtap24: 27,\n fu_a: 28,\n fu_b: 29,\n} as const;\n\nconst annex_bNALUStartCode = Buffer.from([0x00, 0x00, 0x00, 0x01]);\n\nconst stap_aHeaderSize = 1;\nconst stap_aNALULengthSize = 2;\n\n// ---------------------------------------------------------------------------\n// Annex-B / length-prefixed (AVCC) NAL splitting\n// Annex-B syntax splitting is codec-agnostic (start codes only) and shared with\n// H.265; NAL contents are never interpreted or filtered at this stage.\n// ---------------------------------------------------------------------------\n\nfunction looksLikeAnnexB(sample: Buffer): boolean {\n if (sample.length < 3) {\n return false;\n }\n return (\n (sample[0] === 0 &&\n sample[1] === 0 &&\n sample[2] === 0 &&\n sample[3] === 1) ||\n (sample[0] === 0 && sample[1] === 0 && sample[2] === 1)\n );\n}\n\nfunction readUintLength(buf: Buffer, offset: number, size: number): number {\n if (size === 1) return buf[offset];\n if (size === 2) return buf.readUInt16BE(offset);\n if (size === 3) {\n return (buf[offset] << 16) | (buf[offset + 1] << 8) | buf[offset + 2];\n }\n return buf.readUInt32BE(offset);\n}\n\n/**\n * Split H.264 sample into NAL units (no start codes).\n * Accepts Annex-B (start-code split, no NAL type filtering) or length-prefixed (AVCC).\n */\nexport function splitH264NalUnits(\n sample: Buffer,\n naluLengthSize = 4,\n inputFormat: NalInputFormat = \"auto\",\n): Buffer[] {\n if (\n naluLengthSize < 1 ||\n naluLengthSize > 4 ||\n !Number.isInteger(naluLengthSize)\n ) {\n throw new Error(\n `H.264 naluLengthSize must be an integer 1–4, got ${naluLengthSize}`,\n );\n }\n if (sample.length === 0) {\n return [];\n }\n const format =\n inputFormat === \"auto\"\n ? looksLikeAnnexB(sample)\n ? \"annexb\"\n : \"length-prefixed\"\n : inputFormat;\n if (format === \"annexb\") {\n if (!looksLikeAnnexB(sample)) {\n throw new Error(\n \"H.264 inputFormat annexb: sample does not start with a start code\",\n );\n }\n // Pure syntax split. H264AnnexBParser (MP4 demux helper) drops NAL types\n // >= 14 (prefix NAL, subset SPS, slice extension, ...), but RFC 6184 §5.6\n // carries types 1-23 as Single NAL units, so nothing may be filtered here.\n return new H265AnnexBParser(sample).readAll();\n }\n const prefixed = parseLengthPrefixed(sample, naluLengthSize);\n if (prefixed.nalus) {\n return prefixed.nalus;\n }\n throw new Error(prefixed.error);\n}\n\nfunction parseLengthPrefixed(\n sample: Buffer,\n naluLengthSize: number,\n): { nalus?: Buffer[]; error: string } {\n const nalus: Buffer[] = [];\n let offset = 0;\n while (offset < sample.length) {\n if (offset + naluLengthSize > sample.length) {\n return { error: \"H.264 length-prefixed sample truncated (length field)\" };\n }\n const length = readUintLength(sample, offset, naluLengthSize);\n offset += naluLengthSize;\n if (length === 0) {\n return { error: \"H.264 length-prefixed sample: NAL length 0 is invalid\" };\n }\n if (offset + length > sample.length) {\n return {\n error: `H.264 length-prefixed sample: NAL length ${length} exceeds buffer`,\n };\n }\n nalus.push(sample.subarray(offset, offset + length));\n offset += length;\n }\n return { nalus, error: \"\" };\n}\n\nfunction h264NalType(nal: Buffer): number {\n if (nal.length < 1) return 0;\n return nal[0] & 0x1f;\n}\n\nfunction isH264ParameterSet(type: number): boolean {\n return type === NalUnitType.sps || type === NalUnitType.pps;\n}\n\n/**\n * From configured `parameterSets`, return only those whose type is not already\n * present in the sample (SPS and PPS checked independently).\n * E.g. sample with SPS-only → only missing PPS are returned.\n */\nexport function selectMissingH264ParameterSets(\n parameterSets: Buffer[],\n nalUnits: Buffer[],\n): Buffer[] {\n const hasSps = nalUnits.some((n) => h264NalType(n) === NalUnitType.sps);\n const hasPps = nalUnits.some((n) => h264NalType(n) === NalUnitType.pps);\n return parameterSets.filter((ps) => {\n const t = h264NalType(ps);\n if (t === NalUnitType.sps) return !hasSps;\n if (t === NalUnitType.pps) return !hasPps;\n return false;\n });\n}\n\n/**\n * Build STAP-A payload (RFC 6184 §5.7.1 / Figure 7):\n * STAP-A header (F/NRI/Type=24) + repeated [16-bit NALU size][NAL]\n * F = OR of F of aggregated NALs; NRI = max of NRI of aggregated NALs.\n */\n/** Minimum NAL size: 1-byte header + at least 1 payload octet (packetizer contract). */\nexport const H264_MIN_NAL_LENGTH = 2;\n\n/**\n * End of sequence (10) / end of stream (11) NALs have an empty RBSP, so the\n * 1-byte header alone is valid (ITU-T H.264 §7.4.1.2.4 / §7.3.2.5-6).\n */\nfunction minH264NalLength(nal: Buffer): number {\n const type = nal.length > 0 ? nal[0] & 0x1f : 0;\n return type === 10 || type === 11 ? 1 : H264_MIN_NAL_LENGTH;\n}\n\nexport function buildH264StapA(nalus: Buffer[]): Buffer {\n if (nalus.length < 2) {\n throw new Error(\"H.264 STAP-A requires at least two NAL units\");\n }\n let f = 0;\n let nri = 0;\n for (const n of nalus) {\n if (n.length < minH264NalLength(n)) {\n throw new Error(\n `H.264 STAP-A: NAL unit too short (need ≥${minH264NalLength(n)} bytes, got ${n.length})`,\n );\n }\n if (n.length > 0xffff) {\n throw new Error(\"H.264 NAL unit too large for STAP-A size field\");\n }\n f |= (n[0] >> 7) & 1;\n const nriN = (n[0] >> 5) & 0x03;\n if (nriN > nri) nri = nriN;\n }\n const header = Buffer.from([(f << 7) | (nri << 5) | NalUnitType.stap_a]);\n const parts: Buffer[] = [header];\n for (const n of nalus) {\n const size = Buffer.alloc(2);\n size.writeUInt16BE(n.length, 0);\n parts.push(size, n);\n }\n return Buffer.concat(parts);\n}\n\n// ---------------------------------------------------------------------------\n// Packetizer (packetization-mode=1)\n// ---------------------------------------------------------------------------\n\nexport type H264PacketizerOptions = PacketizerBaseOptions & {\n /** Length field size for AVCC input (default 4). Ignored for Annex-B. */\n naluLengthSize?: number;\n /** Sample layout (default \"auto\"); see {@link NalInputFormat}. */\n inputFormat?: NalInputFormat;\n /**\n * Parameter sets (SPS/PPS as raw NAL units without start codes).\n * On IDR / keyframe, only types missing from the sample are prepended\n * (SPS and PPS checked independently — partial sample SPS-only gets PPS only).\n * Prefer STAP-A when ≥2 leading sets fit; else individual Single NAL.\n */\n parameterSets?: Buffer[];\n /**\n * Force keyframe path for parameter-set prepending even without IDR NAL.\n * Default: auto-detect IDR (type 5).\n */\n isKeyframe?: boolean;\n};\n\nexport type H264PacketizeOptions = {\n isKeyframe?: boolean;\n};\n\n/**\n * Packetize H.264 access units (Annex-B or length-prefixed) for mode=1.\n * Single NAL / STAP-A (SPS+PPS) / FU-A fragmentation (RFC 6184).\n */\nexport class H264Packetizer extends PacketizerBase {\n private readonly naluLengthSize: number;\n private readonly inputFormat: NalInputFormat;\n private readonly parameterSets: Buffer[];\n private readonly defaultIsKeyframe: boolean | undefined;\n\n constructor(options: H264PacketizerOptions = {}) {\n super(options);\n const nls = options.naluLengthSize ?? 4;\n if (nls < 1 || nls > 4 || !Number.isInteger(nls)) {\n throw new Error(\n `H264Packetizer: naluLengthSize must be an integer 1–4, got ${nls}`,\n );\n }\n this.naluLengthSize = nls;\n this.inputFormat = options.inputFormat ?? \"auto\";\n this.parameterSets = options.parameterSets ?? [];\n this.defaultIsKeyframe = options.isKeyframe;\n }\n\n packetize(\n data: Buffer,\n rtpTimestamp: number,\n options: H264PacketizeOptions = {},\n ): RtpPacket[] {\n let nalUnits = splitH264NalUnits(\n data,\n this.naluLengthSize,\n this.inputFormat,\n );\n if (nalUnits.length === 0) {\n return [];\n }\n\n const hasIdr = nalUnits.some(\n (n) => h264NalType(n) === NalUnitType.idrSlice,\n );\n const isKey = options.isKeyframe ?? this.defaultIsKeyframe ?? hasIdr;\n\n // Prepend only parameter-set types absent from the sample (SPS ≠ PPS).\n if (isKey && this.parameterSets.length > 0) {\n const missing = selectMissingH264ParameterSets(\n this.parameterSets,\n nalUnits,\n );\n if (missing.length > 0) {\n // An access unit delimiter (type 9) must stay first in the AU, so the\n // parameter sets go right after it (ITU-T H.264 §7.4.1.2.3).\n const at = nalUnits.findIndex((n) => h264NalType(n) !== 9);\n const insertAt = at === -1 ? nalUnits.length : at;\n nalUnits = [\n ...nalUnits.slice(0, insertAt),\n ...missing,\n ...nalUnits.slice(insertAt),\n ];\n }\n }\n\n const packets: RtpPacket[] = [];\n\n // Try STAP-A for leading parameter sets (SPS/PPS), same policy as H265 AP\n let index = 0;\n if (nalUnits.length >= 2 && isH264ParameterSet(h264NalType(nalUnits[0]))) {\n const aggregated: Buffer[] = [];\n let stapSize = 1; // STAP-A header\n while (\n index < nalUnits.length &&\n isH264ParameterSet(h264NalType(nalUnits[index]))\n ) {\n const n = nalUnits[index];\n const need = 2 + n.length;\n if (stapSize + need > this.maxPayloadSize) {\n break;\n }\n aggregated.push(n);\n stapSize += need;\n index++;\n }\n // STAP-A requires at least two NAL units (RFC 6184 §5.7.1)\n if (aggregated.length >= 2) {\n const isLast = index >= nalUnits.length;\n packets.push(\n this.buildPacket(buildH264StapA(aggregated), rtpTimestamp, isLast),\n );\n } else {\n // Cannot STAP-A (one set only, or too large) → fall back to Single NAL\n index = 0;\n }\n }\n\n for (; index < nalUnits.length; index++) {\n const nal = nalUnits[index];\n const marker = index === nalUnits.length - 1;\n\n // Reject empty NALs and 1-byte NALs other than end-of-seq/stream (FU needs ≥2 octets)\n if (nal.length < minH264NalLength(nal)) {\n throw new Error(\n `H.264 NAL unit too short: expected at least ${minH264NalLength(nal)} bytes (NAL header + data), got ${nal.length}`,\n );\n }\n\n if (nal.length <= this.maxPayloadSize) {\n // Single NAL Unit Packet (RFC 6184 §5.6)\n packets.push(this.buildPacket(nal, rtpTimestamp, marker));\n continue;\n }\n\n // FU-A (RFC 6184 §5.8)\n const nalHeader = nal[0];\n const fragmentPayload = nal.subarray(1);\n const fragmentSize = this.maxPayloadSize - 2; // FU indicator + FU header\n if (fragmentSize <= 0) {\n throw new Error(\"invalid H.264 RTP maxPayloadSize for FU-A\");\n }\n\n const fuIndicator = (nalHeader & 0xe0) | NalUnitType.fu_a;\n const nalType = nalHeader & 0x1f;\n\n for (\n let offset = 0;\n offset < fragmentPayload.length;\n offset += fragmentSize\n ) {\n const chunk = fragmentPayload.subarray(\n offset,\n Math.min(fragmentPayload.length, offset + fragmentSize),\n );\n const isStart = offset === 0;\n const isEnd = offset + chunk.length >= fragmentPayload.length;\n // S|E|R|Type\n const fuHeader = Buffer.from([\n (isStart ? 0x80 : 0x00) | (isEnd ? 0x40 : 0x00) | nalType,\n ]);\n packets.push(\n this.buildPacket(\n Buffer.concat([Buffer.from([fuIndicator]), fuHeader, chunk]),\n rtpTimestamp,\n marker && isEnd,\n ),\n );\n }\n }\n\n return packets;\n }\n}\n\n// ---------------------------------------------------------------------------\n// AVCDecoderConfigurationRecord (avcC, ISO/IEC 14496-15 §5.3.3.1)\n// ---------------------------------------------------------------------------\n\nexport type AvcDecoderConfigurationRecord = {\n /** lengthSizeMinusOne + 1: NAL length field size of AVCC samples (1, 2 or 4). */\n naluLengthSize: number;\n sps: Buffer[];\n pps: Buffer[];\n /** SPS followed by PPS, ready for {@link H264PacketizerOptions.parameterSets}. */\n parameterSets: Buffer[];\n};\n\n/**\n * Parse an avcC record (e.g. WebCodecs / mediabunny `decoderConfig.description`)\n * into the NAL length size and SPS/PPS NAL units (no length prefix).\n * Throws on a truncated record instead of returning partial parameter sets.\n */\nexport function parseAvcDecoderConfigurationRecord(\n description: ArrayBuffer | ArrayBufferView,\n): AvcDecoderConfigurationRecord {\n const record = ArrayBuffer.isView(description)\n ? Buffer.from(\n description.buffer,\n description.byteOffset,\n description.byteLength,\n )\n : Buffer.from(description);\n if (record.length < 7) {\n throw new Error(\n `invalid H.264 avcC: expected at least 7 bytes, got ${record.length}`,\n );\n }\n\n const naluLengthSize = (record[4] & 0x03) + 1;\n let offset = 5;\n const readSets = (count: number) => {\n const sets: Buffer[] = [];\n for (let i = 0; i < count; i++) {\n if (offset + 2 > record.length) {\n throw new Error(\"invalid H.264 avcC: truncated parameter set length\");\n }\n const length = record.readUInt16BE(offset);\n offset += 2;\n if (offset + length > record.length) {\n throw new Error(\"invalid H.264 avcC: truncated parameter set\");\n }\n sets.push(record.subarray(offset, offset + length));\n offset += length;\n }\n return sets;\n };\n\n const sps = readSets(record[offset++] & 0x1f);\n const pps = offset < record.length ? readSets(record[offset++]) : [];\n return { naluLengthSize, sps, pps, parameterSets: [...sps, ...pps] };\n}\n"]}
|