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