-
Notifications
You must be signed in to change notification settings - Fork 191
/
RTPpacket.java
184 lines (155 loc) · 5.57 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
//class RTPpacket
import java.util.*;
public class RTPpacket{
//size of the RTP header:
static int HEADER_SIZE = 12;
//Fields that compose the RTP header
public int Version;
public int Padding;
public int Extension;
public int CC;
public int Marker;
public int PayloadType;
public int SequenceNumber;
public int TimeStamp;
public int Ssrc;
//Bitstream of the RTP header
public byte[] header;
//size of the RTP payload
public int payload_size;
//Bitstream of the RTP payload
public byte[] payload;
//--------------------------
//Constructor of an RTPpacket object from header fields and payload bitstream
//--------------------------
public RTPpacket(int PType, int Framenb, int Time, byte[] data, int data_length){
//fill by default header fields:
Version = 2;
Padding = 0;
Extension = 0;
CC = 0;
Marker = 0;
Ssrc = 1337; // Identifies the server
//fill changing header fields:
SequenceNumber = Framenb;
TimeStamp = Time;
PayloadType = PType;
//build the header bistream:
header = new byte[HEADER_SIZE];
//fill the header array of byte with RTP header fields
header[0] = (byte)(Version << 6 | Padding << 5 | Extension << 4 | CC);
header[1] = (byte)(Marker << 7 | PayloadType & 0x000000FF);
header[2] = (byte)(SequenceNumber >> 8);
header[3] = (byte)(SequenceNumber & 0xFF);
header[4] = (byte)(TimeStamp >> 24);
header[5] = (byte)(TimeStamp >> 16);
header[6] = (byte)(TimeStamp >> 8);
header[7] = (byte)(TimeStamp & 0xFF);
header[8] = (byte)(Ssrc >> 24);
header[9] = (byte)(Ssrc >> 16);
header[10] = (byte)(Ssrc >> 8);
header[11] = (byte)(Ssrc & 0xFF);
//fill the payload bitstream:
payload_size = data_length;
payload = new byte[data_length];
//fill payload array of byte from data (given in parameter of the constructor)
payload = Arrays.copyOf(data, payload_size);
}
//--------------------------
//Constructor of an RTPpacket object from the packet bistream
//--------------------------
public RTPpacket(byte[] packet, int packet_size)
{
//fill default fields:
Version = 2;
Padding = 0;
Extension = 0;
CC = 0;
Marker = 0;
Ssrc = 0;
//check if total packet size is lower than the header size
if (packet_size >= HEADER_SIZE)
{
//get the header bitsream:
header = new byte[HEADER_SIZE];
for (int i=0; i < HEADER_SIZE; i++)
header[i] = packet[i];
//get the payload bitstream:
payload_size = packet_size - HEADER_SIZE;
payload = new byte[payload_size];
for (int i=HEADER_SIZE; i < packet_size; i++)
payload[i-HEADER_SIZE] = packet[i];
//interpret the changing fields of the header:
Version = (header[0] & 0xFF) >>> 6;
PayloadType = header[1] & 0x7F;
SequenceNumber = (header[3] & 0xFF) + ((header[2] & 0xFF) << 8);
TimeStamp = (header[7] & 0xFF) + ((header[6] & 0xFF) << 8) + ((header[5] & 0xFF) << 16) + ((header[4] & 0xFF) << 24);
}
}
//--------------------------
//getpayload: return the payload bistream of the RTPpacket and its size
//--------------------------
public int getpayload(byte[] data) {
for (int i=0; i < payload_size; i++)
data[i] = payload[i];
return(payload_size);
}
//--------------------------
//getpayload_length: return the length of the payload
//--------------------------
public int getpayload_length() {
return(payload_size);
}
//--------------------------
//getlength: return the total length of the RTP packet
//--------------------------
public int getlength() {
return(payload_size + HEADER_SIZE);
}
//--------------------------
//getpacket: returns the packet bitstream and its length
//--------------------------
public int getpacket(byte[] packet)
{
//construct the packet = header + payload
for (int i=0; i < HEADER_SIZE; i++)
packet[i] = header[i];
for (int i=0; i < payload_size; i++)
packet[i+HEADER_SIZE] = payload[i];
//return total size of the packet
return(payload_size + HEADER_SIZE);
}
//--------------------------
//gettimestamp
//--------------------------
public int gettimestamp() {
return(TimeStamp);
}
//--------------------------
//getsequencenumber
//--------------------------
public int getsequencenumber() {
return(SequenceNumber);
}
//--------------------------
//getpayloadtype
//--------------------------
public int getpayloadtype() {
return(PayloadType);
}
//--------------------------
//print headers without the SSRC
//--------------------------
public void printheader()
{
System.out.print("[RTP-Header] ");
System.out.println("Version: " + Version
+ ", Padding: " + Padding
+ ", Extension: " + Extension
+ ", CC: " + CC
+ ", Marker: " + Marker
+ ", PayloadType: " + PayloadType
+ ", SequenceNumber: " + SequenceNumber
+ ", TimeStamp: " + TimeStamp);
}
}