-
Notifications
You must be signed in to change notification settings - Fork 7
/
RTPPacket.java
814 lines (668 loc) · 20.5 KB
/
RTPPacket.java
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
package org.vidtec.rfc3550.rtp;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Objects;
/**
* An implementation of an RTP packet according to RFC 3550/.
* https://tools.ietf.org/html/rfc3550
*
* This class also supports the header extension.
*/
public class RTPPacket implements Comparable<RTPPacket>
{
// RTP Packet format is defined as: (per RFC 3550, section 5.1)
//
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |V=2|P|X| CC |M| PT | sequence number |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | timestamp |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | synchronization source (SSRC) identifier |
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
// | contributing source (CSRC) identifiers |
// | .... |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// Optional extension header - if X flag == true: (as per RFC 3550, section 5.3.1)
//
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | defined by profile | length |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | header extension |
// | .... |
// NB: Maximum possible size of the full header is
// = 4 + 4 (timestamp) + 4(ssrc) + 15 * 4(csrc) + 4(estension top) + 0xFFFF(extension)
// = 12 + 60 + 4 + 0xFFFF
// = 76 + 0xFFFF
// = 65611 (0x1004B) bytes
/** The RTP version constant. */
public static final short VERSION = 2;
/** The number of padding bytes in this packet. */
private final short paddingBytes;
/** The flag indicating if this packet has an extension header. */
private final boolean hasExtension;
/** The flag that indicates if this packet has a market set. */
private final boolean hasMarker;
/** The payload type for this packet (e.g. as set in RFC 3551). */
private final short payloadType;
/** The packet sequence number (16-bit). */
private final int sequenceNumber;
/** The packet timestamp (32-bit). */
private final long timestamp;
/** The synchronisation source identifier (SSRC). */
private final long ssrcIdentifier;
/** The contributing source identifiers (CSRC)s. */
private final long[] csrcIdentifiers;
/** The extension profile number (16-bit). */
private final int extensionProfile;
/** The extension header data. */
private final byte[] extensionHeader;
/** The actual packet payload. */
private final byte[] payload;
/**
* Create an RTP packet from a builder.
* NB: This constructor will validate the packet data is valid as per RFC 3550.
*
* @param builder The builder instance to construct a packet from.
*
* @throws IllegalArgumentException If there is a problem with the validity of the packet.
*/
private RTPPacket(final Builder builder)
{
hasMarker = builder.hasMarker;
if (builder.payloadType > 127 || builder.payloadType < 0)
{
throw new IllegalArgumentException("Expected valid payload type not " + builder.payloadType);
}
payloadType = (short)builder.payloadType;
if (builder.sequenceNumber > 0xFFFF || builder.sequenceNumber < 0)
{
throw new IllegalArgumentException("Expected valid sequence number not " + builder.sequenceNumber);
}
sequenceNumber = builder.sequenceNumber;
if (builder.timestamp > 0xFFFFFFFFL || builder.timestamp < 0)
{
throw new IllegalArgumentException("Expected valid timestamp not " + builder.timestamp);
}
timestamp = builder.timestamp;
if (builder.ssrcIdentifier > 0xFFFFFFFFL || builder.ssrcIdentifier < 0)
{
throw new IllegalArgumentException("Expected valid ssrcIdentifier not " + builder.ssrcIdentifier);
}
ssrcIdentifier = builder.ssrcIdentifier;
if (builder.csrcIdentifiers != null)
{
if (builder.csrcIdentifiers.length > 15)
{
throw new IllegalArgumentException("Expected valid ccsrc identifiers count not " + builder.csrcIdentifiers.length);
}
csrcIdentifiers = builder.csrcIdentifiers;
}
else
{
csrcIdentifiers = new long[0];
}
if (builder.hasExtension)
{
if (builder.extensionHeader == null)
{
throw new IllegalArgumentException("Expected valid header not null");
}
if (builder.extensionHeader.length > 0xFFFF)
{
throw new IllegalArgumentException("Expected valid header length not " + builder.extensionHeader.length);
}
if (builder.extensionProfile > 0xFFFF || builder.extensionProfile < 0)
{
throw new IllegalArgumentException("Expected valid extension profile not " + builder.extensionProfile);
}
hasExtension = true;
extensionProfile = builder.extensionProfile;
extensionHeader = builder.extensionHeader;
}
else
{
// No header extension.
hasExtension = false;
extensionProfile = -1;
extensionHeader = new byte[0];
}
if (builder.payload == null || builder.payload.length == 0)
{
throw new IllegalArgumentException("Expected valid payload not null or empty");
}
payload = builder.payload;
paddingBytes = builder.paddingBytes;
}
/**
* Create an RTP packet from a given byte array.
* NB: This constructor will validate the packet data is valid as per RFC 3550.
*
* @param bb The ByteBuffer to construct a packet from.
*
* @throws IllegalArgumentException If there is a problem with the validity of the packet.
*/
private RTPPacket(final ByteBuffer bb)
{
if (bb.remaining() < 13)
{
// As per RFC 3550 - the header is 12 bytes, there must be data - anything less is a bad packet.
throw new IllegalArgumentException("Packet too short, expecting at least 13 bytes, but found " + bb.remaining());
}
// Handle and unpack the 1st byte.
int firstByte = bb.get();
// First check the version number.
if ((firstByte & 0xC0) != VERSION << 6)
{
// This is not a valid version number.
throw new IllegalArgumentException("Invalid version number found, expecting " + VERSION);
}
// If padding flag is set, then get padding byte count, otherwise set 0.
paddingBytes = ((firstByte & 0x20) == 0x20) ? bb.get(bb.limit() - 1) : 0;
// Set the extension header if flag set.
hasExtension = ((firstByte & 0x10) == 0x10) ? true : false;
// Get number of csrc
short csrcCount = (short)(firstByte & 0x0F);
// Handle and unpack the 2nd byte.
int secondByte = bb.get();
// Set the marker header if flag set.
hasMarker = ((secondByte & 0x80) == 0x80) ? true : false;
// Set the payload type.
payloadType = (short)(secondByte & 0x7F);
// Sequence Number is bytes 3-4
sequenceNumber = 0xFFFF & bb.getShort();
// Timestamp is bytes 5-8
timestamp = 0xFFFFFFFF & bb.getInt();
// SSRC id is bytes 9-12
ssrcIdentifier = 0xFFFFFFFF & bb.getInt();
if (bb.remaining() < csrcCount * 4 + 1)
{
// As per RFC 3550 - each csrc is 4 bytes, there must be data - anything less is a bad packet.
throw new IllegalArgumentException("Packet too short, expecting at least " + (csrcCount * 4 + 1) + " bytes, but found " + bb.remaining());
}
// CSRCs follow ...
csrcIdentifiers = new long[csrcCount];
for (int i = 0 ; i < csrcCount ; i++)
{
csrcIdentifiers[i] = 0xFFFFFFFF & bb.getInt();
}
if (hasExtension)
{
if (bb.remaining() < 4 + 1)
{
// As per RFC 3550 - extn desc is 4 min bytes, there must be data - anything less is a bad packet.
throw new IllegalArgumentException("Packet too short, expecting at least " + (4 + 1) + " bytes, but found " + bb.remaining());
}
// handle header extension parts.
extensionProfile = 0xFFFF & bb.getShort();
int extensionLength = 0xFFFF & bb.getShort();
if (bb.remaining() < extensionLength + 1)
{
// As per RFC 3550 - extn header is extensionLength bytes, there must be data - anything less is a bad packet.
throw new IllegalArgumentException("Packet too short, expecting at least " + (extensionLength + 1) + " bytes, but found " + bb.remaining());
}
extensionHeader = new byte[extensionLength];
bb.get(extensionHeader);
}
else
{
// no extension.
extensionProfile = -1;
extensionHeader = new byte[0];
}
// handle payload length and payload
bb.limit(bb.limit() - paddingBytes);
short payloadLength = (short)bb.remaining();
payload = new byte[payloadLength];
bb.get(payload);
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode()
{
return Objects.hash(sequenceNumber);
}
/**
* A packet is deemed equal if it has the same sequence number.
* timestamp and payload data are NOT inspected.
*
* {@inheritDoc}
*/
@Override
public boolean equals(final Object obj)
{
if (obj == null || !(obj instanceof RTPPacket))
{
return false;
}
return this.sequenceNumber == ((RTPPacket)obj).sequenceNumber;
}
/**
* Packets are compared based on sequence number alone.
* There is a special case where a sequence number rolls over.
* ie. max-1 max 0
*
* This comparison WILL NOT handle that case.
*
* {@inheritDoc}
*/
public int compareTo(final RTPPacket o)
{
if (o == null)
{
throw new NullPointerException("Unexpect null in compareTo");
}
return this.sequenceNumber - o.sequenceNumber;
}
/**
* A packet cannot be cloned, it makes no sense as it has no mutable state.
*
* {@inheritDoc}
*/
@Override
protected Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}
/**
* Get the RTP protocol version - should be 2 as per RFC 3550.
*
* @return The RTP protocol version.
*/
public short version()
{
return VERSION;
}
/**
* Indicates if this packet is padded.
*
* @return true if padded, false otherwise.
*/
public boolean isPadded()
{
return paddingBytes > 0;
}
/**
* Gets the number of padding bytes.
*
* @return The number of padding bytes used, 0 indicates no padding.
*/
public short paddedBytesCount()
{
return paddingBytes;
}
/**
* Indicates if this packet has an extension header.
*
* @return true if extended, false otherwise.
*/
public boolean hasExtension()
{
return hasExtension;
}
/**
* Indicates if this packet has contributing sources.
*
* @return true if contributing sources present, false otherwise.
*/
public boolean hasCsrcs()
{
return csrcIdentifiers.length > 0;
}
/**
* Gets the number of contributing source elements.
*
* @return An integer count from 0 - 15.
*/
public short csrcCount()
{
return (short)csrcIdentifiers.length;
}
/**
* Indicates if the packets has a marker set.
*
* @return true if marker set, false otherwise.
*/
public boolean hasMarker()
{
return hasMarker;
}
/**
* Gets the payload type.
*
* @return The payload type from 0 - 127.
*/
public short payloadType()
{
return payloadType;
}
/**
* Gets the packet sequence number.
*
* @return The packet sequence number ranging from 0 - 65,536 (16-bit integer).
*/
public int sequenceNumber()
{
return sequenceNumber;
}
/**
* Gets the packet timestamp (exact format as per RFC 3550 is per clock rate needed).
*
* @return The packet timestamp as a 32-bit unsigned integer.
*/
public long timestamp()
{
return timestamp;
}
/**
* Gets the sync. source identifier.
*
* @return The ssrc identifier as a 32 bit unsigned integer.
*/
public long ssrcIdentifier()
{
return ssrcIdentifier;
}
/**
* Get the set of corresponding source identifiers as a long[].
*
* @return The array of csrc identifiers matching the csrc count.
*/
public long[] csrcIdentifiers()
{
return hasCsrcs() ? Arrays.copyOf(csrcIdentifiers, csrcIdentifiers.length) : new long[0];
}
/**
* Get the header extension profile (if extension is present).
*
* @return The extension profile, or -1 if no extension is present.
*/
public int extensionProfile()
{
return hasExtension() ? extensionProfile : -1;
}
/**
* Get the header extension length (if extension is present).
*
* @return The extension length, or -1 if no extension is present.
*/
public int extensionLength()
{
return hasExtension() ? extensionHeader.length : -1;
}
/**
* Get the header extension data (if extension is present).
*
* @return The extension data, or empty byte[] if no extension is present.
*/
public byte[] extensionHeaderAsByteArray()
{
return hasExtension() ? Arrays.copyOf(extensionHeader, extensionHeader.length) : new byte[0];
}
/**
* Gets the payload length WITHOUT PADDING.
* NB: If the payload length with padding is required, see payloadLengthRaw().
* NB: This is the normal usage, as you want padding removed in most cases.
*
* @return The payload length - padding byte count.
*/
public int payloadLength()
{
return payload.length;
}
/**
* Gets the payload length WITH PADDING.
* NB: If the payload length without padding is required, see payloadLength().
*
* @return The payload length - padding byte count.
*/
public int payloadLengthRaw()
{
return payload.length + paddingBytes;
}
/**
* Gets the payload data with padding REMOVED as a byte[].
* NB: If the payload with padding is required, see payloadRawAsByteArray().
* NB: This is the normal usage, as you want padding removed in most cases.
*
* @return a copy of the RDP packet payload as a ByteBuffer.
*/
public ByteBuffer payloadAsByteBuffer()
{
return ByteBuffer.wrap(Arrays.copyOf(payload, payload.length));
}
/**
* Gets the payload data with padding REMOVED.
* NB: If the payload with padding is required, see payloadRawAsByteArray().
* NB: This is the normal usage, as you want padding removed in most cases.
*
* @return a copy of the RDP packet payload as a byte[].
*/
public byte[] payloadAsByteArray()
{
return Arrays.copyOf(payload, payload.length);
}
/**
* Gets the payload data WITH padding as a byte[].
* NB: If the payload without padding is required, see payloadAsByteArray().
*
* @return a copy of the RDP packet payload as a byte[].
*/
public byte[] payloadRawAsByteArray()
{
final byte[] data = Arrays.copyOf(payload, payloadLengthRaw());
if (isPadded())
{
data[data.length - 1] = (byte)paddingBytes;
}
return data;
}
/**
* Return the full length of the packet in bytes.
*
* @return The number of bytes required for this packet.
*/
public int packetLength()
{
return 12 + (4 * csrcCount()) + (hasExtension ? 4 + extensionHeader.length : 0) + payloadLengthRaw();
}
/**
* Gets the packet data as a byte[].
*
* @return a copy of the RDP packet data.
*/
public byte[] asByteArray()
{
final byte[] data = new byte[packetLength()];
final ByteBuffer bb = ByteBuffer.wrap(data);
bb.put((byte)(VERSION << 6 | (isPadded() ? 0x20 : 0x00) | (hasExtension() ? 0x10 : 0x00) | csrcCount() ));
bb.put((byte)(hasMarker() ? 0x80 | payloadType : 0x00 | payloadType));
bb.putShort((short)sequenceNumber);
bb.putInt((int)timestamp);
bb.putInt((int)ssrcIdentifier);
Arrays.stream(csrcIdentifiers).forEach(c -> bb.putInt((int)c));
if (hasExtension())
{
bb.putShort((short)extensionProfile);
bb.putShort((short)(0xFFFF & extensionHeader.length));
bb.put(extensionHeader);
}
bb.put(payload);
if (isPadded())
{
data[data.length - 1] = (byte)paddingBytes;
}
return data;
}
/**
*
* @param address The InetAddress that the datagram will be sent to.
* @param port The port that the datagram will be sent to.
* @return The DatagramPacket instance that represents this packet.
*/
public DatagramPacket asDatagramPacket(InetAddress address, int port)
{
return new DatagramPacket(asByteArray(), packetLength(), address, port);
}
/**
* Returns an RTP packet derived from a given byte array.
*
* @param data The byte[] to construct a packet from.
* @return The RTPPacket instance.
*
* @throws IllegalArgumentException If there is a problem with the validity of the packet.
*/
public static RTPPacket fromByteArray(final byte[] data)
{
if (data == null)
{
throw new IllegalArgumentException("packet data cannot be null");
}
return new RTPPacket( ByteBuffer.wrap(data) );
}
/**
* Returns an RTP packet derived from a given DatagramPacket.
*
* @param packet DatagramPacket construct a packet from.
* @return The RTPPacket instance.
*
* @throws IllegalArgumentException If there is a problem with the validity of the packet.
*/
public static RTPPacket fromDatagramPacket(final DatagramPacket packet)
{
if (packet == null)
{
throw new IllegalArgumentException("packet cannot be null");
}
return new RTPPacket( ByteBuffer.wrap(packet.getData(), 0, packet.getLength()) );
}
/**
* Creates a builder to manually build an {@link RTPPacket}.
*
* @return The builder instance.
*/
public static Builder builder()
{
return new Builder();
}
/**
* A Builder class to build {@link RTPPacket} instances.
*/
public static final class Builder
{
private boolean hasMarker;
private int payloadType = -1;
private int sequenceNumber = -1;
private long timestamp = -1;
private long ssrcIdentifier = -1;
private long[] csrcIdentifiers;
private boolean hasExtension;
private int extensionProfile = -1;
private byte[] extensionHeader;
private byte[] payload;
private short paddingBytes = 0;
/**
* Private constructor.
*/
private Builder() { /* Empty Constructor */ }
/**
* This packet should have a marker set.
*
* @return The builder instance.
*/
public Builder withMarker()
{
this.hasMarker = true;
return this;
}
/**
* This packet should have required header fields set.
*
* @param payloadType The payload type.
* @param sequenceNumber The sequence number.
* @param timestamp The timestamp.
* @param ssrc The ssrc identifier.
* @return The builder instance.
*/
public Builder withRequiredHeaderFields(final int payloadType, final int sequenceNumber, final long timestamp, final long ssrc)
{
this.payloadType = payloadType;
this.sequenceNumber = sequenceNumber;
this.timestamp = timestamp;
this.ssrcIdentifier = ssrc;
return this;
}
/**
* This packet should have csrc identifiers set.
*
* @param csrcIdentifiers The csrc identifiers to set.
* @return The builder instance.
*/
public Builder withCsrcIdentifiers(final long ... csrcIdentifiers)
{
this.csrcIdentifiers = csrcIdentifiers;
return this;
}
/**
* This packet should have a header extension set.
*
* @param extensionProfile The profile.
* @param header The header data.
* @return The builder instance.
*/
public Builder withHeaderExtension(final int extensionProfile, final byte[] header)
{
this.extensionProfile = extensionProfile;
this.extensionHeader = header == null ? null : Arrays.copyOf(header, header.length);
this.hasExtension = true;
return this;
}
/**
* This packet should have a payload set.
*
* @param payload The payload data.
* @return The builder instance.
*/
public Builder withPayload(final byte[] payload)
{
this.payload = payload == null ? null : Arrays.copyOf(payload, payload.length);
return this;
}
/**
* This packet should have a payload set but be aligned to a given byte boundary
* (i.e. padded if needed).
*
* @param payload The payload data.
* @param alignToBytes The byte alignment boundary.
* @return The builder instance.
*/
public Builder withPayload(final byte[] payload, final int alignToBytes)
{
if (payload == null)
{
throw new IllegalArgumentException("cannot align to boundary of null data.");
}
this.payload = Arrays.copyOf(payload, payload.length);
this.paddingBytes = (short)(payload.length % alignToBytes);
return this;
}
/**
* Build the packet.
*
* @return The packet instance.
*
* @throws IllegalArgumentException If there is a problem with the supplied packet data.
*/
public RTPPacket build()
{
return new RTPPacket(this);
}
}
}