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:
parent
d5441d45f3
commit
b0c3647242
@ -7,9 +7,12 @@
|
||||
*/
|
||||
package org.ice4j.socket;
|
||||
|
||||
import org.ice4j.stack.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.channels.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
import org.ice4j.stack.*;
|
||||
|
||||
@ -22,6 +25,12 @@ import org.ice4j.stack.*;
|
||||
public class DelegatingDatagramSocket
|
||||
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
|
||||
@ -29,6 +38,31 @@ public class DelegatingDatagramSocket
|
||||
*/
|
||||
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
|
||||
* to any available port on the local host machine. The socket will be
|
||||
@ -533,17 +567,16 @@ public class DelegatingDatagramSocket
|
||||
{
|
||||
super.receive(p);
|
||||
|
||||
// no exception packet is successfully received, log it
|
||||
if(StunStack.isPacketLoggerEnabled())
|
||||
{
|
||||
StunStack.getPacketLogger().logPacket(
|
||||
p.getAddress().getAddress(),
|
||||
p.getPort(),
|
||||
super.getLocalAddress().getAddress(),
|
||||
super.getLocalPort(),
|
||||
p.getData(),
|
||||
false);
|
||||
}
|
||||
// no exception packet is successfully received, log it.
|
||||
++nbReceivedRtpPackets;
|
||||
logPacketToPcap(
|
||||
p,
|
||||
this.nbReceivedRtpPackets,
|
||||
false,
|
||||
super.getLocalAddress(),
|
||||
super.getLocalPort());
|
||||
// Log RTP losses if > 5%.
|
||||
updateRtpLosses(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -581,17 +614,14 @@ public class DelegatingDatagramSocket
|
||||
{
|
||||
super.send(p);
|
||||
|
||||
// no exception packet is successfully sent, log it
|
||||
if(StunStack.isPacketLoggerEnabled())
|
||||
{
|
||||
StunStack.getPacketLogger().logPacket(
|
||||
super.getLocalAddress().getAddress(),
|
||||
super.getLocalPort(),
|
||||
p.getAddress().getAddress(),
|
||||
p.getPort(),
|
||||
p.getData(),
|
||||
true);
|
||||
}
|
||||
// no exception packet is successfully sent, log it.
|
||||
++nbSentRtpPackets;
|
||||
logPacketToPcap(
|
||||
p,
|
||||
this.nbSentRtpPackets,
|
||||
true,
|
||||
super.getLocalAddress(),
|
||||
super.getLocalPort());
|
||||
}
|
||||
// Else, the delegate socket will encapsulate the packet.
|
||||
else
|
||||
@ -746,4 +776,191 @@ public class DelegatingDatagramSocket
|
||||
else
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,6 +36,31 @@ public class DelegatingSocket
|
||||
*/
|
||||
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>.
|
||||
*/
|
||||
@ -762,19 +787,16 @@ public class DelegatingSocket
|
||||
// Else, sends the packet to the final socket (outputStream).
|
||||
outputStream.write(p.getData(), p.getOffset(), p.getLength());
|
||||
|
||||
// no exception packet is successfully sent, log it
|
||||
if(StunStack.isPacketLoggerEnabled())
|
||||
{
|
||||
InetSocketAddress localAddress
|
||||
= (InetSocketAddress) super.getLocalSocketAddress();
|
||||
StunStack.getPacketLogger().logPacket(
|
||||
localAddress.getAddress().getAddress(),
|
||||
localAddress.getPort(),
|
||||
p.getAddress().getAddress(),
|
||||
p.getPort(),
|
||||
p.getData(),
|
||||
true);
|
||||
}
|
||||
// no exception packet is successfully sent, log it.
|
||||
++nbSentRtpPackets;
|
||||
InetSocketAddress localAddress
|
||||
= (InetSocketAddress) super.getLocalSocketAddress();
|
||||
DelegatingDatagramSocket.logPacketToPcap(
|
||||
p,
|
||||
this.nbSentRtpPackets,
|
||||
true,
|
||||
localAddress.getAddress(),
|
||||
localAddress.getPort());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -843,6 +865,19 @@ public class DelegatingSocket
|
||||
{
|
||||
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)
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -154,4 +154,30 @@ public class StunDatagramPacketFilter
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user