1 /** |
|
2 * Java RTP Library (jlibrtp) |
|
3 * Copyright (C) 2006 Arne Kepp |
|
4 * |
|
5 * This library is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU Lesser General Public |
|
7 * License as published by the Free Software Foundation; either |
|
8 * version 2.1 of the License, or (at your option) any later version. |
|
9 * |
|
10 * This library is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 * Lesser General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Lesser General Public |
|
16 * License along with this library; if not, write to the Free Software |
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 */ |
|
19 package jlibrtp; |
|
20 |
|
21 import java.net.InetSocketAddress; |
|
22 |
|
23 /** |
|
24 * A participant represents a peer in an RTPSession. Based on the information stored on |
|
25 * these objects, packets are processed and statistics generated for RTCP. |
|
26 */ |
|
27 public class Participant { |
|
28 /** Whether the participant is unexpected, e.g. arrived through unicast with SDES */ |
|
29 protected boolean unexpected = false; |
|
30 /** Where to send RTP packets (unicast)*/ |
|
31 protected InetSocketAddress rtpAddress = null; |
|
32 /** Where to send RTCP packets (unicast) */ |
|
33 protected InetSocketAddress rtcpAddress = null; |
|
34 /** Where the first RTP packet was received from */ |
|
35 protected InetSocketAddress rtpReceivedFromAddress = null; |
|
36 /** Where the first RTCP packet was received from */ |
|
37 protected InetSocketAddress rtcpReceivedFromAddress = null; |
|
38 |
|
39 /** SSRC of participant */ |
|
40 protected long ssrc = -1; |
|
41 /** SDES CNAME */ |
|
42 protected String cname = null; |
|
43 /** SDES The participant's real name */ |
|
44 protected String name = null; |
|
45 /** SDES The participant's email */ |
|
46 protected String email = null; |
|
47 /** SDES The participant's phone number */ |
|
48 protected String phone = null; |
|
49 /** SDES The participant's location*/ |
|
50 protected String loc = null; |
|
51 /** SDES The tool the participants is using */ |
|
52 protected String tool = null; |
|
53 /** SDES A note */ |
|
54 protected String note = null; |
|
55 /** SDES A priv string, loosely defined */ |
|
56 protected String priv = null; |
|
57 |
|
58 // Receiver Report Items |
|
59 /** RR First sequence number */ |
|
60 protected int firstSeqNumber = -1; |
|
61 /** RR Last sequence number */ |
|
62 protected int lastSeqNumber = 0; |
|
63 /** RR Number of times sequence number has rolled over */ |
|
64 protected long seqRollOverCount = 0; |
|
65 /** RR Number of packets received */ |
|
66 protected long receivedPkts = 0; |
|
67 /** RR Number of octets received */ |
|
68 protected long receivedOctets = 0; |
|
69 /** RR Number of packets received since last SR */ |
|
70 protected int receivedSinceLastSR = 0; |
|
71 /** RR Sequence number associated with last SR */ |
|
72 protected int lastSRRseqNumber = 0; |
|
73 /** RR Interarrival jitter */ |
|
74 protected double interArrivalJitter = -1.0; |
|
75 /** RR Last received RTP Timestamp */ |
|
76 protected long lastRtpTimestamp = 0; |
|
77 |
|
78 /** RR Middle 32 bits of the NTP timestamp in the last SR */ |
|
79 protected long timeStampLSR = 0; |
|
80 /** RR The time when we actually got the last SR */ |
|
81 protected long timeReceivedLSR = 0; |
|
82 |
|
83 /** Gradient where UNIX timestamp = ntpGradient*RTPTimestamp * ntpOffset */ |
|
84 protected double ntpGradient = -1; |
|
85 /** Offset where UNIX timestamp = ntpGradient*RTPTimestamp * ntpOffset */ |
|
86 protected long ntpOffset = -1; |
|
87 /** Last NTP received in SR packet, MSB */ |
|
88 protected long lastNtpTs1 = 0; //32 bits |
|
89 /** Last NTP received in SR packet, LSB */ |
|
90 protected long lastNtpTs2 = 0; //32 bits |
|
91 /** RTP Timestamp in last SR packet */ |
|
92 protected long lastSRRtpTs = 0; //32 bits |
|
93 |
|
94 /** UNIX time when a BYE was received from this participant, for pruning */ |
|
95 protected long timestampBYE = -1; // The user said BYE at this time |
|
96 |
|
97 /** Store the packets received from this participant */ |
|
98 protected PktBuffer pktBuffer = null; |
|
99 |
|
100 /** UNIX time of last RTP packet, to check whether this participant has sent anything recently */ |
|
101 protected long lastRtpPkt = -1; //Time of last RTP packet |
|
102 /** UNIX time of last RTCP packet, to check whether this participant has sent anything recently */ |
|
103 protected long lastRtcpPkt = -1; //Time of last RTCP packet |
|
104 /** UNIX time this participant was added by application, to check whether we ever heard back */ |
|
105 protected long addedByApp = -1; //Time the participant was added by application |
|
106 /** UNIX time of last time we sent an RR to this user */ |
|
107 protected long lastRtcpRRPkt = -1; //Timestamp of last time we sent this person an RR packet |
|
108 /** Unix time of second to last time we sent and RR to this user */ |
|
109 protected long secondLastRtcpRRPkt = -1; //Timestamp of 2nd to last time we sent this person an RR Packet |
|
110 |
|
111 /** |
|
112 * Create a basic participant. If this is a <b>unicast</b> session you must provide network address (ipv4 or ipv6) and ports for RTP and RTCP, |
|
113 * as well as a cname for this contact. These things should be negotiated through SIP or a similar protocol. |
|
114 * |
|
115 * jlibrtp will listen for RTCP packets to obtain a matching SSRC for this participant, based on cname. |
|
116 * @param networkAddress string representation of network address (ipv4 or ipv6). Use "127.0.0.1" for multicast session. |
|
117 * @param rtpPort port on which peer expects RTP packets. Use 0 if this is a sender-only, or this is a multicast session. |
|
118 * @param rtcpPort port on which peer expects RTCP packets. Use 0 if this is a sender-only, or this is a multicast session. |
|
119 */ |
|
120 public Participant(String networkAddress, int rtpPort, int rtcpPort) { |
|
121 if(RTPSession.rtpDebugLevel > 6) { |
|
122 System.out.println("Creating new participant: " + networkAddress); |
|
123 } |
|
124 |
|
125 // RTP |
|
126 if(rtpPort > 0) { |
|
127 try { |
|
128 rtpAddress = new InetSocketAddress(networkAddress, rtpPort); |
|
129 } catch (Exception e) { |
|
130 System.out.println("Couldn't resolve " + networkAddress); |
|
131 } |
|
132 //isReceiver = true; |
|
133 } |
|
134 |
|
135 // RTCP |
|
136 if(rtcpPort > 0) { |
|
137 try { |
|
138 rtcpAddress = new InetSocketAddress(networkAddress, rtcpPort); |
|
139 } catch (Exception e) { |
|
140 System.out.println("Couldn't resolve " + networkAddress); |
|
141 } |
|
142 } |
|
143 |
|
144 //By default this is a sender |
|
145 //isSender = true; |
|
146 } |
|
147 |
|
148 // We got a packet, but we don't know this person yet. |
|
149 protected Participant(InetSocketAddress rtpAdr, InetSocketAddress rtcpAdr, long SSRC) { |
|
150 rtpReceivedFromAddress = rtpAdr; |
|
151 rtcpReceivedFromAddress = rtcpAdr; |
|
152 ssrc = SSRC; |
|
153 unexpected = true; |
|
154 } |
|
155 |
|
156 // Dummy constructor to ease testing |
|
157 protected Participant() { |
|
158 System.out.println("Don't use the Participan(void) Constructor!"); |
|
159 } |
|
160 |
|
161 /** |
|
162 * RTP Address registered with this participant. |
|
163 * |
|
164 * @return address of participant |
|
165 */ |
|
166 InetSocketAddress getRtpSocketAddress() { |
|
167 return rtpAddress; |
|
168 } |
|
169 |
|
170 |
|
171 /** |
|
172 * RTCP Address registered with this participant. |
|
173 * |
|
174 * @return address of participant |
|
175 */ |
|
176 InetSocketAddress getRtcpSocketAddress() { |
|
177 return rtcpAddress; |
|
178 } |
|
179 |
|
180 /** |
|
181 * InetSocketAddress this participant has used to |
|
182 * send us RTP packets. |
|
183 * |
|
184 * @return address of participant |
|
185 */ |
|
186 InetSocketAddress getRtpReceivedFromAddress() { |
|
187 return rtpAddress; |
|
188 } |
|
189 |
|
190 |
|
191 |
|
192 /** |
|
193 * InetSocketAddress this participant has used to |
|
194 * send us RTCP packets. |
|
195 * |
|
196 * @return address of participant |
|
197 */ |
|
198 InetSocketAddress getRtcpReceivedFromAddress() { |
|
199 return rtcpAddress; |
|
200 } |
|
201 |
|
202 |
|
203 /** |
|
204 * CNAME registered for this participant. |
|
205 * |
|
206 * @return the cname |
|
207 */ |
|
208 public String getCNAME() { |
|
209 return cname; |
|
210 } |
|
211 |
|
212 |
|
213 /** |
|
214 * NAME registered for this participant. |
|
215 * |
|
216 * @return the name |
|
217 */ |
|
218 public String getNAME() { |
|
219 return name; |
|
220 } |
|
221 |
|
222 /** |
|
223 * EMAIL registered for this participant. |
|
224 * |
|
225 * @return the email address |
|
226 */ |
|
227 public String getEmail() { |
|
228 return email; |
|
229 } |
|
230 |
|
231 /** |
|
232 * PHONE registered for this participant. |
|
233 * |
|
234 * @return the phone number |
|
235 */ |
|
236 public String getPhone() { |
|
237 return phone; |
|
238 } |
|
239 |
|
240 /** |
|
241 * LOCATION registered for this participant. |
|
242 * |
|
243 * @return the location |
|
244 */ |
|
245 public String getLocation() { |
|
246 return loc; |
|
247 } |
|
248 |
|
249 /** |
|
250 * NOTE registered for this participant. |
|
251 * |
|
252 * @return the note |
|
253 */ |
|
254 public String getNote() { |
|
255 return note; |
|
256 } |
|
257 |
|
258 /** |
|
259 * PRIVATE something registered for this participant. |
|
260 * |
|
261 * @return the private-string |
|
262 */ |
|
263 public String getPriv() { |
|
264 return priv; |
|
265 } |
|
266 |
|
267 /** |
|
268 * TOOL something registered for this participant. |
|
269 * |
|
270 * @return the tool |
|
271 */ |
|
272 public String getTool() { |
|
273 return tool; |
|
274 } |
|
275 |
|
276 /** |
|
277 * SSRC for participant, determined through RTCP SDES |
|
278 * |
|
279 * @return SSRC (32 bit unsigned integer as long) |
|
280 */ |
|
281 public long getSSRC() { |
|
282 return this.ssrc; |
|
283 } |
|
284 |
|
285 /** |
|
286 * Updates the participant with information for receiver reports. |
|
287 * |
|
288 * @param packetLength to keep track of received octets |
|
289 * @param pkt the most recently received packet |
|
290 */ |
|
291 protected void updateRRStats(int packetLength, RtpPkt pkt) { |
|
292 int curSeqNum = pkt.getSeqNumber(); |
|
293 |
|
294 if(firstSeqNumber < 0) { |
|
295 firstSeqNumber = curSeqNum; |
|
296 } |
|
297 |
|
298 receivedOctets += packetLength; |
|
299 receivedSinceLastSR++; |
|
300 receivedPkts++; |
|
301 |
|
302 long curTime = System.currentTimeMillis(); |
|
303 |
|
304 if( this.lastSeqNumber < curSeqNum ) { |
|
305 //In-line packet, best thing you could hope for |
|
306 this.lastSeqNumber = curSeqNum; |
|
307 |
|
308 } else if(this.lastSeqNumber - this.lastSeqNumber < -100) { |
|
309 //Sequence counter rolled over |
|
310 this.lastSeqNumber = curSeqNum; |
|
311 seqRollOverCount++; |
|
312 |
|
313 } else { |
|
314 //This was probably a duplicate or a late arrival. |
|
315 } |
|
316 |
|
317 // Calculate jitter |
|
318 if(this.lastRtpPkt > 0) { |
|
319 |
|
320 long D = (pkt.getTimeStamp() - curTime) - (this.lastRtpTimestamp - this.lastRtpPkt); |
|
321 if(D < 0) |
|
322 D = (-1)*D; |
|
323 |
|
324 this.interArrivalJitter += ((double)D - this.interArrivalJitter) / 16.0; |
|
325 } |
|
326 |
|
327 lastRtpPkt = curTime; |
|
328 lastRtpTimestamp = pkt.getTimeStamp(); |
|
329 } |
|
330 |
|
331 /** |
|
332 * Calculates the extended highest sequence received by adding |
|
333 * the last sequence number to 65536 times the number of times |
|
334 * the sequence counter has rolled over. |
|
335 * |
|
336 * @return extended highest sequence |
|
337 */ |
|
338 protected long getExtHighSeqRecv() { |
|
339 return (65536*seqRollOverCount + lastSeqNumber); |
|
340 } |
|
341 |
|
342 /** |
|
343 * Get the fraction of lost packets, calculated as described |
|
344 * in RFC 3550 as a fraction of 256. |
|
345 * |
|
346 * @return the fraction of lost packets since last SR received |
|
347 */ |
|
348 protected int getFractionLost() { |
|
349 int expected = (lastSeqNumber - lastSRRseqNumber); |
|
350 if(expected < 0) |
|
351 expected = 65536 + expected; |
|
352 |
|
353 int fraction = 256 * (expected - receivedSinceLastSR); |
|
354 if(expected > 0) { |
|
355 fraction = (fraction / expected); |
|
356 } else { |
|
357 fraction = 0; |
|
358 } |
|
359 |
|
360 //Clear counters |
|
361 receivedSinceLastSR = 0; |
|
362 lastSRRseqNumber = lastSeqNumber; |
|
363 |
|
364 return fraction; |
|
365 } |
|
366 |
|
367 /** |
|
368 * The total number of packets lost during the session. |
|
369 * |
|
370 * Returns zero if loss is negative, i.e. duplicates have been received. |
|
371 * |
|
372 * @return number of lost packets, or zero. |
|
373 */ |
|
374 protected long getLostPktCount() { |
|
375 long lost = (this.getExtHighSeqRecv() - this.firstSeqNumber) - receivedPkts; |
|
376 |
|
377 if(lost < 0) |
|
378 lost = 0; |
|
379 return lost; |
|
380 } |
|
381 |
|
382 /** |
|
383 * |
|
384 * @return the interArrivalJitter, calculated continuously |
|
385 */ |
|
386 protected double getInterArrivalJitter() { |
|
387 return this.interArrivalJitter; |
|
388 } |
|
389 |
|
390 /** |
|
391 * Set the timestamp for last sender report |
|
392 * |
|
393 * @param ntp1 high order bits |
|
394 * @param ntp2 low order bits |
|
395 */ |
|
396 protected void setTimeStampLSR(long ntp1, long ntp2) { |
|
397 // Use what we've got |
|
398 byte[] high = StaticProcs.uIntLongToByteWord(ntp1); |
|
399 byte[] low = StaticProcs.uIntLongToByteWord(ntp2); |
|
400 low[3] = low[1]; |
|
401 low[2] = low[0]; |
|
402 low[1] = high[3]; |
|
403 low[0] = high[2]; |
|
404 |
|
405 this.timeStampLSR = StaticProcs.bytesToUIntLong(low, 0); |
|
406 } |
|
407 |
|
408 /** |
|
409 * Calculate the delay between the last received sender report |
|
410 * and now. |
|
411 * |
|
412 * @return the delay in units of 1/65.536ms |
|
413 */ |
|
414 protected long delaySinceLastSR() { |
|
415 if(this.timeReceivedLSR < 1) |
|
416 return 0; |
|
417 |
|
418 long delay = System.currentTimeMillis() - this.timeReceivedLSR; |
|
419 |
|
420 //Convert ms into 1/65536s = 1/65.536ms |
|
421 return (long) ((double)delay * 65.536); |
|
422 } |
|
423 |
|
424 /** |
|
425 * Only for debugging purposes |
|
426 */ |
|
427 public void debugPrint() { |
|
428 System.out.print(" Participant.debugPrint() SSRC:"+this.ssrc+" CNAME:"+this.cname); |
|
429 if(this.rtpAddress != null) |
|
430 System.out.print(" RTP:"+this.rtpAddress.toString()); |
|
431 if(this.rtcpAddress != null) |
|
432 System.out.print(" RTCP:"+this.rtcpAddress.toString()); |
|
433 System.out.println(""); |
|
434 |
|
435 System.out.println(" Packets received:"+this.receivedPkts); |
|
436 } |
|
437 } |
|