forked from iizukanao/node-rtsp-rtmp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtp.coffee
460 lines (408 loc) · 15.2 KB
/
rtp.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# RTP spec:
# RFC 3550 http://tools.ietf.org/html/rfc3550
# RTP payload format for H.264 video:
# RFC 6184 http://tools.ietf.org/html/rfc6184
# RTP payload format for AAC audio:
# RFC 3640 http://tools.ietf.org/html/rfc3640
# RFC 5691 http://tools.ietf.org/html/rfc5691
#
# TODO: Use DON (decoding order number) to carry B-frames.
# DON is to RTP what DTS is to MPEG-TS.
bits = require './bits'
# Number of seconds from 1900-01-01 to 1970-01-01
EPOCH = 2208988800
# Constant for calculating NTP fractional second
NTP_SCALE_FRAC = 4294.967295
TIMESTAMP_ROUNDOFF = 4294967296 # 32 bits
# Minimum length of an RTP header
RTP_HEADER_LEN = 12
MAX_PAYLOAD_SIZE = 1360
api =
# Number of bytes in RTP header
RTP_HEADER_LEN: RTP_HEADER_LEN
RTCP_PACKET_TYPE_SENDER_REPORT : 200 # SR
RTCP_PACKET_TYPE_RECEIVER_REPORT : 201 # RR
RTCP_PACKET_TYPE_SOURCE_DESCRIPTION : 202 # SDES
RTCP_PACKET_TYPE_GOODBYE : 203 # BYE
RTCP_PACKET_TYPE_APPLICATION_DEFINED: 204 # APP
readRTCPSenderReport: ->
# RFC 3550 - 6.4.1 SR: Sender Report RTCP Packet
startBytePos = bits.current_position().byte
info = {}
info.version = bits.read_bits 2
info.padding = bits.read_bit()
info.reportCount = bits.read_bits 5
info.payloadType = bits.read_byte() # == 200
if info.payloadType isnt api.RTCP_PACKET_TYPE_SENDER_REPORT
throw new Error "payload type must be #{api.RTCP_PACKET_TYPE_SENDER_REPORT}"
info.wordsMinusOne = bits.read_bits 16
info.totalBytes = (info.wordsMinusOne + 1) * 4
info.ssrc = bits.read_bits 32
info.ntpTimestamp = bits.read_bits 64 # loses some precision
info.rtpTimestamp = bits.read_bits 32
info.senderPacketCount = bits.read_bits 32
info.senderOctetCount = bits.read_bits 32
info.reportBlocks = []
for i in [0...info.reportCount]
reportBlock = {}
reportBlock.ssrc = bits.read_bits 32
reportBlock.fractionLost = bits.read_byte()
reportBlock.packetsLost = bits.read_int 24
reportBlock.highestSequenceNumber = bits.read_bits 32
reportBlock.jitter = bits.read_bits 32
reportBlock.lastSR = bits.read_bits 32
reportBlock.delaySinceLastSR = bits.read_bits 32
info.reportBlocks.push reportBlock
# skip padding bytes
readBytes = bits.current_position().byte - startBytePos
if readBytes < info.totalBytes
bits.skip_bytes info.totalBytes - readBytes
return info
readRTCPReceiverReport: ->
# RFC 3550 - 6.4.2 RR: Receiver Report RTCP Packet
startBytePos = bits.current_position().byte
info = {}
info.version = bits.read_bits 2
info.padding = bits.read_bit()
info.reportCount = bits.read_bits 5
info.payloadType = bits.read_byte() # == 201
if info.payloadType isnt api.RTCP_PACKET_TYPE_RECEIVER_REPORT
throw new Error "payload type must be #{api.RTCP_PACKET_TYPE_RECEIVER_REPORT}"
info.wordsMinusOne = bits.read_bits 16
info.totalBytes = (info.wordsMinusOne + 1) * 4
info.ssrc = bits.read_bits 32
info.reportBlocks = []
for i in [0...info.reportCount]
reportBlock = {}
reportBlock.ssrc = bits.read_bits 32
reportBlock.fractionLost = bits.read_byte()
reportBlock.packetsLost = bits.read_int 24
reportBlock.highestSequenceNumber = bits.read_bits 32
reportBlock.jitter = bits.read_bits 32
reportBlock.lastSR = bits.read_bits 32
reportBlock.delaySinceLastSR = bits.read_bits 32
info.reportBlocks.push reportBlock
# skip padding bytes
readBytes = bits.current_position().byte - startBytePos
if readBytes < info.totalBytes
bits.skip_bytes info.totalBytes - readBytes
return info
readRTCPSourceDescription: ->
# RFC 3550 - 6.5 SDES: Source Description RTCP Packet
startBytePos = bits.current_position().byte
info = {}
info.version = bits.read_bits 2
info.padding = bits.read_bit()
info.sourceCount = bits.read_bits 5
info.payloadType = bits.read_byte() # == 202
if info.payloadType isnt api.RTCP_PACKET_TYPE_SOURCE_DESCRIPTION
throw new Error "payload type must be #{api.RTCP_PACKET_TYPE_SOURCE_DESCRIPTION}"
info.wordsMinusOne = bits.read_bits 16
info.totalBytes = (info.wordsMinusOne + 1) * 4
info.chunks = []
for i in [0...info.sourceCount]
chunk = {}
chunk.ssrc_csrc = bits.read_bits 32
chunk.sdesItems = []
chunk.sdes = {}
moreSDESItems = true
while moreSDESItems
sdesItem = {}
sdesItem.type = bits.read_byte()
sdesItem.octetCount = bits.read_byte()
if sdesItem.octetCount > 255
throw new Error "octet count too large: #{sdesItem.octetCount} <= 255"
sdesItem.text = bits.read_bytes(sdesItem.octetCount).toString 'utf8'
switch sdesItem.type
when 0 # terminate the list
# skip until the next 32-bit boundary
bytesPastBoundary = (bits.current_position().byte - startBytePos) % 4
if bytesPastBoundary > 0
while bytesPastBoundary < 4
nullOctet = bits.read_byte()
if nullOctet isnt 0x00
throw new Error "padding octet must be 0x00: #{nullOctet}"
bytesPastBoundary++
moreSDESItems = false # end the loop
when 1 # Canonical End-Point Identifier
chunk.sdes.cname = sdesItem.text
when 2 # User Name
chunk.sdes.name = sdesItem.text
when 3 # Electronic Mail Address
chunk.sdes.email = sdesItem.text
when 4 # Phone Number
chunk.sdes.phone = sdesItem.text
when 5 # Geographic User Location
chunk.sdes.loc = sdesItem.text
when 6 # Application or Tool Name
chunk.sdes.tool = sdesItem.text
when 7 # Notice/Status
chunk.sdes.note = sdesItem.text
when 8 # Private Extensions
chunk.sdes.priv = sdesItem.text
else
throw new Error "unknown SDES item type in source description " +
"RTCP packet: #{chunk.type} (maybe not implemented yet)"
chunk.sdesItems.push sdesItem
info.chunks.push chunk
# skip padding bytes
readBytes = bits.current_position().byte - startBytePos
if readBytes < info.totalBytes
bits.skip_bytes info.totalBytes - readBytes
return info
readRTCPGoodbye: ->
# RFC 3550 - 6.6 BYE: Goodbye RTCP Packet
startBytePos = bits.current_position().byte
info = {}
info.version = bits.read_bits 2
info.padding = bits.read_bit()
info.sourceCount = bits.read_bits 5
info.payloadType = bits.read_byte() # == 203
if info.payloadType isnt api.RTCP_PACKET_TYPE_GOODBYE
throw new Error "payload type must be #{api.RTCP_PACKET_TYPE_GOODBYE}"
info.wordsMinusOne = bits.read_bits 16
info.totalBytes = (info.wordsMinusOne + 1) * 4
info.ssrc = bits.read_bits 32
if bits.has_more_data()
info.reasonOctetCount = bits.read_byte()
reason = bits.read_bytes info.reasonOctetCount
# skip padding bytes
readBytes = bits.current_position().byte - startBytePos
if readBytes < info.totalBytes
bits.skip_bytes info.totalBytes - readBytes
return info
readRTCPApplicationDefined: ->
# RFC 3550 - 6.7 APP: Application-Defined RTCP Packet
startBytePos = bits.current_position().byte
info = {}
info.version = bits.read_bits 2
info.padding = bits.read_bit()
info.subtype = bits.read_bits 5
info.payloadType = bits.read_byte() # == 204
if info.payloadType isnt api.RTCP_PACKET_TYPE_APPLICATION_DEFINED
throw new Error "payload type must be #{api.RTCP_PACKET_TYPE_APPLICATION_DEFINED}"
info.wordsMinusOne = bits.read_bits 16
info.totalBytes = (info.wordsMinusOne + 1) * 4
info.ssrc_csrc = bits.read_bits 32
info.name = bits.read_bytes(4).toString 'ascii'
# read the application-dependent data (remaining bytes)
readBytes = bits.current_position().byte - startBytePos
if readBytes < info.totalBytes
info.applicationData = bits.read_bytes info.totalBytes - readBytes
else
info.applicationData = null
return info
readRTPFixedHeader: ->
# RFC 3550 - 5.1 RTP Fixed Header Fields
info = {}
info.version = bits.read_bits 2
info.padding = bits.read_bit()
info.extension = bits.read_bit()
info.csrcCount = bits.read_bits 4
info.marker = bits.read_bit()
info.payloadType = bits.read_bits 7
info.sequenceNumber = bits.read_bits 16
info.timestamp = bits.read_bits 32
info.ssrc = bits.read_bits 32
info.csrc = []
for i in [0...info.csrcCount]
info.csrc.push bits.read_bits 32
return info
parsePackets: (buf) ->
bits.push_stash()
bits.set_data buf
packets = []
while bits.has_more_data()
packet = {}
packet.payloadValue = bits.get_byte_at 1
switch packet.payloadValue
when api.RTCP_PACKET_TYPE_SENDER_REPORT
packet.rtcpSenderReport = api.readRTCPSenderReport()
when api.RTCP_PACKET_TYPE_RECEIVER_REPORT
packet.rtcpReceiverReport = api.readRTCPReceiverReport()
when api.RTCP_PACKET_TYPE_SOURCE_DESCRIPTION
packet.rtcpSourceDescription = api.readRTCPSourceDescription()
when api.RTCP_PACKET_TYPE_GOODBYE
packet.rtcpGoodbye = api.readRTCPGoodbye()
when api.RTCP_PACKET_TYPE_APPLICATION_DEFINED
packet.rtcpApplicationDefined = api.readRTCPApplicationDefined()
else # RTP data transfer protocol - fixed header
packet.rtpHeader = api.readRTPFixedHeader()
packets.push packet
bits.pop_stash()
return packets
# Replace SSRC in-place in the given RTP header
replaceSSRCInRTP: (buf, ssrc) ->
buf[8] = (ssrc >>> 24) & 0xff
buf[9] = (ssrc >>> 16) & 0xff
buf[10] = (ssrc >>> 8) & 0xff
buf[11] = ssrc & 0xff
return
# Get NTP timestamp for a time
# time is expressed the same as Date.now()
getNTPTimestamp: (time) ->
sec = parseInt(time / 1000)
ms = time - (sec * 1000)
ntp_sec = sec + EPOCH
ntp_usec = Math.round(ms * 1000 * NTP_SCALE_FRAC)
return [ntp_sec, ntp_usec]
# Used for encapsulating AAC audio data
# opts:
# accessUnitLength (number): number of bytes in the access unit
createAudioHeader: (opts) ->
if opts.accessUnits.length > 4095
throw new Error "too many audio access units: #{opts.accessUnits.length} (must be <= 4095)"
numBits = opts.accessUnits.length * 16 # 2 bytes per access unit
header = [
## payload
## See section 3.2.1 and 3.3.6 of RFC 3640 for details
## AU Header Section
# AU-headers-length(16) for AAC-hbr
# Number of bits in the AU-headers
(numBits >> 8) & 0xff,
numBits & 0xff,
]
for accessUnit in opts.accessUnits
header = header.concat api.createAudioAUHeader accessUnit.length
return header
groupAudioFrames: (adtsFrames) ->
packetSize = RTP_HEADER_LEN
groups = []
currentGroup = []
for adtsFrame, i in adtsFrames
packetSize += adtsFrame.length + 2 # 2 bytes for AU-Header
if packetSize > MAX_PAYLOAD_SIZE
groups.push currentGroup
currentGroup = []
packetSize = RTP_HEADER_LEN + adtsFrame.length + 2
currentGroup.push adtsFrame
if currentGroup.length > 0
groups.push currentGroup
return groups
createAudioAUHeader: (accessUnitLength) ->
return [
# AU Header
# AU-size(13) by SDP
# AU-Index(3) or AU-Index-Delta(3)
# AU-Index is used for the first access unit, and the value must be 0.
# AU-Index-Delta is used for the consecutive access units.
# When interleaving is not applied, AU-Index-Delta is 0.
accessUnitLength >> 5,
(accessUnitLength & 0b11111) << 3,
# There is no Auxiliary Section for AAC-hbr
]
# Used for encapsulating H.264 video data
createFragmentationUnitHeader: (opts) ->
return [
# Fragmentation Unit
# See section 5.8 of RFC 6184 for details
#
# FU indicator
# forbidden_zero_bit(1), nal_ref_idc(2), type(5)
# type is 28 for FU-A
opts.nal_ref_idc | 28,
# FU header
# start bit(1) == 0, end bit(1) == 1, reserved bit(1), type(5)
(opts.isStart << 7) | (opts.isEnd << 6) | opts.nal_unit_type
]
# Create RTP header
# opts:
# marker (boolean): true if this is the last packet of the
# access unit indicated by the RTP timestamp
# payloadType (number): payload type
# sequenceNumber (number): sequence number
# timestamp (number): timestamp in 90 kHz clock rate
# ssrc (number): SSRC (can be null)
createRTPHeader: (opts) ->
seqNum = opts.sequenceNumber
ts = opts.timestamp
ssrc = opts.ssrc ? 0
return [
# version(2): 2
# padding(1): 0
# extension(1): 0
# CSRC count(4): 0
0b10000000,
# marker(1)
# payload type(7)
(opts.marker << 7) | opts.payloadType,
# sequence number(16)
seqNum >>> 8,
seqNum & 0xff,
# timestamp(32) in 90 kHz clock rate
(ts >>> 24) & 0xff,
(ts >>> 16) & 0xff,
(ts >>> 8) & 0xff,
ts & 0xff,
# SSRC(32)
(ssrc >>> 24) & 0xff,
(ssrc >>> 16) & 0xff,
(ssrc >>> 8) & 0xff,
ssrc & 0xff,
]
# Create RTCP Sender Report packet
# opts:
# time: timestamp of the packet
# rtpTime: timestamp relative to the start point of media
# ssrc: SSRC
# packetCount: packet count
# octetCount: octetCount
createSenderReport: (opts) ->
if not opts?.ssrc?
throw new Error "createSenderReport: ssrc is required"
ssrc = opts.ssrc
if not opts?.packetCount?
throw new Error "createSenderReport: packetCount is required"
packetCount = opts.packetCount
if not opts?.octetCount?
throw new Error "createSenderReport: octetCount is required"
octetCount = opts.octetCount
if not opts?.time?
throw new Error "createSenderReport: time is required"
ntp_ts = api.getNTPTimestamp opts.time
if not opts?.rtpTime?
throw new Error "createSenderReport: rtpTime is required"
rtp_ts = opts.rtpTime
length = 6 # 28 (packet bytes) / 4 (32-bit word) - 1
return [
# See section 6.4.1 for details
# version(2): 2 (RTP version 2)
# padding(1): 0 (padding doesn't exist)
# reception report count(5): 0 (no reception report blocks)
0b10000000,
# packet type(8): 200 (RTCP Sender Report)
200,
# length(16)
length >> 8, length & 0xff,
# SSRC of sender(32)
(ssrc >>> 24) & 0xff,
(ssrc >>> 16) & 0xff,
(ssrc >>> 8) & 0xff,
ssrc & 0xff,
# [sender info]
# NTP timestamp(64)
(ntp_ts[0] >>> 24) & 0xff,
(ntp_ts[0] >>> 16) & 0xff,
(ntp_ts[0] >>> 8) & 0xff,
ntp_ts[0] & 0xff,
(ntp_ts[1] >>> 24) & 0xff,
(ntp_ts[1] >>> 16) & 0xff,
(ntp_ts[1] >>> 8) & 0xff,
ntp_ts[1] & 0xff,
# RTP timestamp(32)
(rtp_ts >>> 24) & 0xff,
(rtp_ts >>> 16) & 0xff,
(rtp_ts >>> 8) & 0xff,
rtp_ts & 0xff,
# sender's packet count(32)
(packetCount >>> 24) & 0xff,
(packetCount >>> 16) & 0xff,
(packetCount >>> 8) & 0xff,
packetCount & 0xff,
# sender's octet count(32)
(octetCount >>> 24) & 0xff,
(octetCount >>> 16) & 0xff,
(octetCount >>> 8) & 0xff,
octetCount & 0xff,
]
module.exports = api