Corrects RTP adresses logged in pcap file. Adds an informational log when the RTP loss rate is > 5% (at most an entry each 5 seconds).

This commit is contained in:
chenzo 2012-06-12 16:11:06 +00:00
parent d5441d45f3
commit b0c3647242
3 changed files with 339 additions and 35 deletions

View File

@ -7,9 +7,12 @@
*/ */
package org.ice4j.socket; package org.ice4j.socket;
import org.ice4j.stack.*;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.util.logging.*;
import org.ice4j.stack.*; import org.ice4j.stack.*;
@ -22,6 +25,12 @@ import org.ice4j.stack.*;
public class DelegatingDatagramSocket public class DelegatingDatagramSocket
extends DatagramSocket extends DatagramSocket
{ {
/**
* The <tt>Logger</tt> used by the <tt>Agent</tt> class and its instances
* for logging output.
*/
private static final Logger logger
= Logger.getLogger(DelegatingDatagramSocket.class.getName());
/** /**
* The <tt>DatagramSocket</tt> to which this * The <tt>DatagramSocket</tt> to which this
@ -29,6 +38,31 @@ public class DelegatingDatagramSocket
*/ */
protected final DatagramSocket delegate; protected final DatagramSocket delegate;
/**
* The number of RTP packets received for this socket.
*/
private long nbReceivedRtpPackets = 0;
/**
* The number of RTP packets sent for this socket.
*/
private long nbSentRtpPackets = 0;
/**
* The number of RTP packets lost (not received) for this socket.
*/
private long nbLostRtpPackets = 0;
/**
* The last RTP sequence number received for this socket.
*/
private long lastRtpSequenceNumber = -1;
/**
* The last time an information about packet lost has been logged.
*/
private long lastLostPacketLogTime = 0;
/** /**
* Initializes a new <tt>DelegatingDatagramSocket</tt> instance and binds it * Initializes a new <tt>DelegatingDatagramSocket</tt> instance and binds it
* to any available port on the local host machine. The socket will be * to any available port on the local host machine. The socket will be
@ -533,17 +567,16 @@ public class DelegatingDatagramSocket
{ {
super.receive(p); super.receive(p);
// no exception packet is successfully received, log it // no exception packet is successfully received, log it.
if(StunStack.isPacketLoggerEnabled()) ++nbReceivedRtpPackets;
{ logPacketToPcap(
StunStack.getPacketLogger().logPacket( p,
p.getAddress().getAddress(), this.nbReceivedRtpPackets,
p.getPort(), false,
super.getLocalAddress().getAddress(), super.getLocalAddress(),
super.getLocalPort(), super.getLocalPort());
p.getData(), // Log RTP losses if > 5%.
false); updateRtpLosses(p);
}
} }
else else
{ {
@ -581,17 +614,14 @@ public class DelegatingDatagramSocket
{ {
super.send(p); super.send(p);
// no exception packet is successfully sent, log it // no exception packet is successfully sent, log it.
if(StunStack.isPacketLoggerEnabled()) ++nbSentRtpPackets;
{ logPacketToPcap(
StunStack.getPacketLogger().logPacket( p,
super.getLocalAddress().getAddress(), this.nbSentRtpPackets,
super.getLocalPort(), true,
p.getAddress().getAddress(), super.getLocalAddress(),
p.getPort(), super.getLocalPort());
p.getData(),
true);
}
} }
// Else, the delegate socket will encapsulate the packet. // Else, the delegate socket will encapsulate the packet.
else else
@ -746,4 +776,191 @@ public class DelegatingDatagramSocket
else else
delegate.setTrafficClass(tc); delegate.setTrafficClass(tc);
} }
/**
* Determines whether a RTP packet which has a specific number in
* the total number of sent or received is to be logged in pcap file.
*
* @param numOfPacket the number of the <tt>RawPacket</tt> in the total
* number of sent <tt>RawPacket</tt>s
* @return <tt>true</tt> if the <tt>RawPacket</tt> with the specified
* <tt>numOfPacket</tt> is to be logged by <tt>PacketLoggingService</tt>;
* otherwise, <tt>false</tt>
*/
public static boolean logRTPPacket(long numOfPacket)
{
return (numOfPacket == 1)
|| (numOfPacket == 300)
|| (numOfPacket == 500)
|| (numOfPacket == 1000)
|| ((numOfPacket % 5000) == 0);
}
/**
* Logs interesting packets (STUN/TURN or RTP) to the pcap file.
*
* @param p the <tt>DatagramPacket</tt> received/sent.
* @param nbSentOrReceivedRtpPackets the number of RTP packets already
* received or sent.
* @param isSent True if this packet is sent. False if this packet is
* received.
* @param interfaceAddress The InetAddress bind by this socket to the local
* interface (0.0.0.0 or ::0 is the socket is not bound).
* @param interfacePort The port bind by this socket to the local interface.
*/
public static void logPacketToPcap(
DatagramPacket p,
long nbSentOrReceivedRtpPackets,
boolean isSent,
InetAddress interfaceAddress,
int interfacePort)
{
boolean isStunPacket = StunDatagramPacketFilter.isStunPacket(p);
boolean logThisRtpPacket = false;
// If this is not a STUN/TURN packet, then this is a RTP packet.
if(!isStunPacket)
{
logThisRtpPacket = DelegatingDatagramSocket.logRTPPacket(
nbSentOrReceivedRtpPackets);
}
if(isStunPacket || logThisRtpPacket)
{
InetAddress[] addr = {interfaceAddress, p.getAddress()};
int[] port = {interfacePort, p.getPort()};
int fromIndex = isSent ? 0 : 1;
int toIndex = isSent ? 1 : 0;
if(StunStack.isPacketLoggerEnabled())
{
StunStack.getPacketLogger().logPacket(
addr[fromIndex].getAddress(),
port[fromIndex],
addr[toIndex].getAddress(),
port[toIndex],
p.getData(),
isSent);
}
}
}
/**
* Determines the sequence number of a RTP packet.
*
* @param p the last RTP packet received.
*
* @return The last RTP sequence number.
*/
public static long getRtpSequenceNumber(DatagramPacket p)
{
// The sequence number is contained in the third and fourth bytes of the
// RTP header (stored in big endian).
byte[] data = p.getData();
int offset = p.getOffset();
long seq_high = data[offset + 2] & 0xff;
long seq_low = data[offset + 3] & 0xff;
return seq_high << 8 | seq_low;
}
/**
* Updates and Logs information about RTP losses if there is more then 5% of
* RTP packet lost (at most every 5 seconds).
*
* @param p The last packet received.
*/
public void updateRtpLosses(DatagramPacket p)
{
// If this is not a STUN/TURN packet, then this is a RTP packet.
if(!StunDatagramPacketFilter.isStunPacket(p))
{
long newSeq = getRtpSequenceNumber(p);
if(this.lastRtpSequenceNumber != -1)
{
nbLostRtpPackets
+= getNbLost(this.lastRtpSequenceNumber, newSeq);
}
this.lastRtpSequenceNumber = newSeq;
this.lastLostPacketLogTime = logRtpLosses(
this.nbLostRtpPackets,
this.nbReceivedRtpPackets,
this.lastLostPacketLogTime);
}
}
/**
* Logs information about RTP losses if there is more then 5% of RTP packet
* lost (at most every 5 seconds).
*
* @param totalNbLost The total number of lost packet since the beginning of
* this stream.
* @param totalNbReceived The total number of received packet since the
* begnining of this tream.
* @param lastLogTime The last time we have logged information about RTP
* losses.
*
* @return the last log time updated if this function as log new information
* about RTP losses. Otherwise, returns the same last log time value as
* given in parameter.
*/
public static long logRtpLosses(
long totalNbLost,
long totalNbReceived,
long lastLogTime)
{
double percentLost = ((double) totalNbLost)
/ ((double) (totalNbLost + totalNbReceived));
// Log the information if the loss rate is upper 5% and if the last
// log is before 5 seconds.
if(percentLost > 0.05)
{
long currentTime = System.currentTimeMillis();
if(currentTime - lastLogTime >= 5000)
{
logger.info("RTP lost > 5%: " + percentLost);
return currentTime;
}
}
return lastLogTime;
}
/**
* Return the number of loss between the 2 last RTP packets received.
*
* @param lastRtpSequenceNumber The previous RTP sequence number.
* @param newSeq The current RTP sequence number.
*
* @return the number of loss between the 2 last RTP packets received.
*/
public static long getNbLost(
long lastRtpSequenceNumber,
long newSeq)
{
long newNbLost = 0;
if(lastRtpSequenceNumber <= newSeq)
{
newNbLost = newSeq - lastRtpSequenceNumber;
}
else
{
newNbLost = (0xffff - lastRtpSequenceNumber) + newSeq;
}
if(newNbLost > 1)
{
if(newNbLost < 0x00ff)
{
return newNbLost - 1;
}
// Else the paquet is desequenced, then count it as a
// single loss.
else
{
return 1;
}
}
return 0;
}
} }

View File

@ -36,6 +36,31 @@ public class DelegatingSocket
*/ */
private OutputStream outputStream = null; private OutputStream outputStream = null;
/**
* The number of RTP packets received for this socket.
*/
private long nbReceivedRtpPackets = 0;
/**
* The number of RTP packets sent for this socket.
*/
private long nbSentRtpPackets = 0;
/**
* The number of RTP packets lost (not received) for this socket.
*/
private long nbLostRtpPackets = 0;
/**
* The last RTP sequence number received for this socket.
*/
private long lastRtpSequenceNumber = -1;
/**
* The last time an information about packet lost has been logged.
*/
private long lastLostPacketLogTime = 0;
/** /**
* Initializes a new <tt>DelegatingSocket</tt>. * Initializes a new <tt>DelegatingSocket</tt>.
*/ */
@ -762,19 +787,16 @@ public class DelegatingSocket
// Else, sends the packet to the final socket (outputStream). // Else, sends the packet to the final socket (outputStream).
outputStream.write(p.getData(), p.getOffset(), p.getLength()); outputStream.write(p.getData(), p.getOffset(), p.getLength());
// no exception packet is successfully sent, log it // no exception packet is successfully sent, log it.
if(StunStack.isPacketLoggerEnabled()) ++nbSentRtpPackets;
{ InetSocketAddress localAddress
InetSocketAddress localAddress = (InetSocketAddress) super.getLocalSocketAddress();
= (InetSocketAddress) super.getLocalSocketAddress(); DelegatingDatagramSocket.logPacketToPcap(
StunStack.getPacketLogger().logPacket( p,
localAddress.getAddress().getAddress(), this.nbSentRtpPackets,
localAddress.getPort(), true,
p.getAddress().getAddress(), localAddress.getAddress(),
p.getPort(), localAddress.getPort());
p.getData(),
true);
}
} }
/** /**
@ -843,6 +865,19 @@ public class DelegatingSocket
{ {
throw new SocketException("Failed to receive data from socket"); throw new SocketException("Failed to receive data from socket");
} }
// no exception packet is successfully received, log it.
++nbReceivedRtpPackets;
InetSocketAddress localAddress
= (InetSocketAddress) super.getLocalSocketAddress();
DelegatingDatagramSocket.logPacketToPcap(
p,
this.nbReceivedRtpPackets,
false,
localAddress.getAddress(),
localAddress.getPort());
// Log RTP losses if > 5%.
updateRtpLosses(p);
} }
/** /**
@ -855,4 +890,30 @@ public class DelegatingSocket
if(this.inputStream == null && inputStream != null) if(this.inputStream == null && inputStream != null)
this.inputStream = inputStream; this.inputStream = inputStream;
} }
/**
* Updates and Logs information about RTP losses if there is more then 5% of
* RTP packet lost (at most every 5 seconds).
*
* @param p The last packet received.
*/
public void updateRtpLosses(DatagramPacket p)
{
// If this is not a STUN/TURN packet, then this is a RTP packet.
if(!StunDatagramPacketFilter.isStunPacket(p))
{
long newSeq = DelegatingDatagramSocket.getRtpSequenceNumber(p);
if(this.lastRtpSequenceNumber != -1)
{
nbLostRtpPackets += DelegatingDatagramSocket
.getNbLost(this.lastRtpSequenceNumber, newSeq);
}
this.lastRtpSequenceNumber = newSeq;
this.lastLostPacketLogTime = DelegatingDatagramSocket.logRtpLosses(
this.nbLostRtpPackets,
this.nbReceivedRtpPackets,
this.lastLostPacketLogTime);
}
}
} }

View File

@ -154,4 +154,30 @@ public class StunDatagramPacketFilter
return false; return false;
} }
/**
* Determines whether a specific <tt>DatagramPacket</tt> represents a STUN
* (or TURN) packet.
*
* @param p the <tt>DatagramPacket</tt> which is to be checked whether it is
* a STUN message which is part of the communicator with the STUN server
* associated with this instance
*
* @return True if the <tt>DatagramPacket</tt> represents a STUN
* (or TURN) packet. False, otherwise.
*/
public static boolean isStunPacket(DatagramPacket p)
{
/*
* The most significant 2 bits of every STUN message MUST be
* zeroes. This can be used to differentiate STUN packets from
* other protocols when STUN is multiplexed with other protocols
* on the same port.
*/
byte[] data = p.getData();
int offset = p.getOffset();
byte b0 = data[offset];
return ((b0 & 0xC0) == 0);
}
} }