Fixes technical errors in method invocations. Optimizes type checks on packet send and receive.

This commit is contained in:
lubomir.marinov 2014-11-12 14:12:37 +00:00
parent 088ca3609f
commit 54f6fd3f86
4 changed files with 388 additions and 496 deletions

View File

@ -7,13 +7,13 @@
*/ */
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 java.util.logging.*;
import org.ice4j.stack.*;
/** /**
* Implements a <tt>DatagramSocket</tt> which delegates its calls to a specific * Implements a <tt>DatagramSocket</tt> which delegates its calls to a specific
* <tt>DatagramSocket</tt>. * <tt>DatagramSocket</tt>.
@ -62,7 +62,7 @@ public class DelegatingDatagramSocket
private long lastLostPacketLogTime = 0; private long lastLostPacketLogTime = 0;
/** /**
* Assigns a factory to generate custom DatagramSocket to remplace classical * Assigns a factory to generate custom DatagramSocket to replace classical
* "java" DatamgramSocket. This way external applications can define the * "java" DatamgramSocket. This way external applications can define the
* type of DatagramSocket to use. * type of DatagramSocket to use.
* If "null", then the classical "java" DatagramSocket is used. Otherwise * If "null", then the classical "java" DatagramSocket is used. Otherwise
@ -203,7 +203,7 @@ public class DelegatingDatagramSocket
} }
// If not null, binds the delegate socket to the given address. // If not null, binds the delegate socket to the given address.
// Otherwise bind the "super" socket to this address. // Otherwise bind the "super" socket to this address.
this.bind(address); bind(address);
} }
} }
@ -482,7 +482,7 @@ public class DelegatingDatagramSocket
throws SocketException throws SocketException
{ {
return return
(delegate ==null) (delegate == null)
? super.getReuseAddress() ? super.getReuseAddress()
: delegate.getReuseAddress(); : delegate.getReuseAddress();
} }
@ -619,37 +619,27 @@ public class DelegatingDatagramSocket
{ {
// If the packet length is too small, then the // If the packet length is too small, then the
// DatagramSocket.receive function will truncate the received // DatagramSocket.receive function will truncate the received
// datagram. // datagram. This problem appears when reusing the same
// This problem appears when reusing the same DatagramPacket: i.e. // DatagramPacket i.e. if the first time we use the DatagramPacket
// if the first time we use the DatagramPacket to receive a small // to receive a small packet and the second time a bigger one, then
// packet and the second time a bigger one, then after the first // after the first packet is received, the length is set to the size
// packet is received, the length is set to the size of the first // of the first packet and the second packet is truncated.
// packet and the second packet is truncated.
// http://docs.oracle.com/javase/6/docs/api/java/net/DatagramSocket.html // http://docs.oracle.com/javase/6/docs/api/java/net/DatagramSocket.html
byte[] data = p.getData(); byte[] data = p.getData();
if(data == null)
{ p.setLength((data == null) ? 0 : (data.length - p.getOffset()));
p.setLength(0);
}
else
{
p.setLength(data.length - p.getOffset());
}
super.receive(p); super.receive(p);
// no exception packet is successfully received, log it. // No exception, a packet is successfully received - log it. If it
// If this is not a STUN/TURN packet, then this is a RTP packet. // is not a STUN/TURN packet, then it is an RTP packet.
if(isRtpPacket(p)) if(isRtpPacket(p))
{
++nbReceivedRtpPackets; ++nbReceivedRtpPackets;
}
logPacketToPcap( logPacketToPcap(
p, p,
this.nbReceivedRtpPackets, nbReceivedRtpPackets,
false, false,
super.getLocalAddress(), super.getLocalAddress(), super.getLocalPort());
super.getLocalPort());
// Log RTP losses if > 5%. // Log RTP losses if > 5%.
updateRtpLosses(p); updateRtpLosses(p);
} }
@ -687,7 +677,8 @@ public class DelegatingDatagramSocket
// Sends the packet to the final DatagramSocket // Sends the packet to the final DatagramSocket
if (delegate == null) if (delegate == null)
{ {
try{ try
{
super.send(p); super.send(p);
} }
// DIRTY, DIRTY, DIRTY!!! // DIRTY, DIRTY, DIRTY!!!
@ -722,10 +713,9 @@ public class DelegatingDatagramSocket
++nbSentRtpPackets; ++nbSentRtpPackets;
logPacketToPcap( logPacketToPcap(
p, p,
this.nbSentRtpPackets, nbSentRtpPackets,
true, true,
super.getLocalAddress(), super.getLocalAddress(), super.getLocalPort());
super.getLocalPort());
} }
// Else, the delegate socket will encapsulate the packet. // Else, the delegate socket will encapsulate the packet.
else else
@ -956,7 +946,6 @@ public class DelegatingDatagramSocket
* Determines the sequence number of a RTP packet. * Determines the sequence number of a RTP packet.
* *
* @param p the last RTP packet received. * @param p the last RTP packet received.
*
* @return The last RTP sequence number. * @return The last RTP sequence number.
*/ */
public static long getRtpSequenceNumber(DatagramPacket p) public static long getRtpSequenceNumber(DatagramPacket p)
@ -972,28 +961,28 @@ public class DelegatingDatagramSocket
} }
/** /**
* Updates and Logs information about RTP losses if there is more then 5% of * Updates and logs information about RTP losses if there is more then 5% of
* RTP packet lost (at most every 5 seconds). * RTP packet lost (at most every 5 seconds).
* *
* @param p The last packet received. * @param p The last packet received.
*/ */
public void updateRtpLosses(DatagramPacket p) private void updateRtpLosses(DatagramPacket p)
{ {
// If this is not a STUN/TURN packet, then this is a RTP packet. // If this is not a STUN/TURN packet, then this is a RTP packet.
if(isRtpPacket(p)) if(isRtpPacket(p))
{ {
long newSeq = getRtpSequenceNumber(p); long newSeq = getRtpSequenceNumber(p);
if(this.lastRtpSequenceNumber != -1)
{
nbLostRtpPackets
+= getNbLost(this.lastRtpSequenceNumber, newSeq);
}
this.lastRtpSequenceNumber = newSeq;
this.lastLostPacketLogTime = logRtpLosses( if(lastRtpSequenceNumber != -1)
this.nbLostRtpPackets, nbLostRtpPackets += getNbLost(lastRtpSequenceNumber, newSeq);
this.nbReceivedRtpPackets,
this.lastLostPacketLogTime); lastRtpSequenceNumber = newSeq;
lastLostPacketLogTime
= logRtpLosses(
nbLostRtpPackets,
nbReceivedRtpPackets,
lastLostPacketLogTime);
} }
} }
@ -1130,24 +1119,25 @@ public class DelegatingDatagramSocket
/** /**
* Determines whether <tt>p</tt> is an RTP packet (and something else, like, * Determines whether <tt>p</tt> is an RTP packet (and something else, like,
* a STUN, RTCP or DTLS packet). * a STUN, RTCP or DTLS packet).
*
* @param p the packet. * @param p the packet.
* @return <tt>true</tt> if <tt>p</tt> appears to be an RTP packet. * @return <tt>true</tt> if <tt>p</tt> appears to be an RTP packet.
*/ */
private boolean isRtpPacket(DatagramPacket p) private boolean isRtpPacket(DatagramPacket p)
{ {
byte[] data = p.getData();
int off = p.getOffset();
int len = p.getLength(); int len = p.getLength();
if (len >= 4) if (len >= 4)
{ {
byte[] data = p.getData();
int off = p.getOffset();
if (((data[off + 0] & 0xc0) >> 6) == 2) if (((data[off + 0] & 0xc0) >> 6) == 2)
{ {
//Either RTP or RTCP //Either RTP or RTCP
return !RtcpDemuxPacketFilter.isRtcpPacket(p); return !RtcpDemuxPacketFilter.isRtcpPacket(p);
} }
} }
return false; return false;
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -17,20 +17,26 @@ import java.net.*;
public class IceTcpSocketWrapper public class IceTcpSocketWrapper
extends IceSocketWrapper extends IceSocketWrapper
{ {
/**
* InputStream for this socket.
*/
private final InputStream inputStream;
/**
* OutputStream for this socket.
*/
private final OutputStream outputStream;
/** /**
* Delegate TCP <tt>Socket</tt>. * Delegate TCP <tt>Socket</tt>.
*/ */
private final Socket socket; private final Socket socket;
/** /**
* InputStream for this socket. * A <tt>DelegatingSocket</tt> view of {@link #socket} if the latter
* implements the former; otherwise, <tt>null</tt>.
*/ */
private InputStream inputStream = null; private final DelegatingSocket socketAsDelegatingSocket;
/**
* OutputStream for this socket.
*/
private OutputStream outputStream = null;
/** /**
* Constructor. * Constructor.
@ -42,54 +48,20 @@ public class IceTcpSocketWrapper
public IceTcpSocketWrapper(Socket delegate) public IceTcpSocketWrapper(Socket delegate)
throws IOException throws IOException
{ {
this.socket = delegate; socket = delegate;
if(!(delegate instanceof DelegatingSocket))
{
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
}
}
/** if(delegate instanceof DelegatingSocket)
* {@inheritDoc}
*/
@Override
public void send(DatagramPacket p)
throws IOException
{ {
if(socket instanceof DelegatingSocket) inputStream = null;
{ outputStream = null;
((DelegatingSocket)socket).send(p); socketAsDelegatingSocket = (DelegatingSocket) delegate;
return;
} }
else
int len = p.getLength();
int off = p.getOffset();
byte data[] = new byte[len + 2];
data[0] = (byte)((len >> 8) & 0xff);
data[1] = (byte)(len & 0xff);
System.arraycopy(p.getData(), off, data, 2, len);
outputStream.write(data, 0, len + 2);
data = null;
}
/**
* {@inheritDoc}
*/
@Override
public void receive(DatagramPacket p) throws IOException
{ {
if(socket instanceof DelegatingSocket) inputStream = delegate.getInputStream();
{ outputStream = delegate.getOutputStream();
((DelegatingSocket)socket).receive(p); socketAsDelegatingSocket = null;
return;
} }
DelegatingSocket.receiveFromNetwork(
p,
inputStream,
this.getLocalAddress(),
this.getLocalPort());
} }
/** /**
@ -151,4 +123,47 @@ public class IceTcpSocketWrapper
{ {
return null; return null;
} }
/**
* {@inheritDoc}
*/
@Override
public void receive(DatagramPacket p) throws IOException
{
if (socketAsDelegatingSocket != null)
{
socketAsDelegatingSocket.receive(p);
}
else
{
DelegatingSocket.receiveFromInputStream(
p,
inputStream,
getLocalAddress(), getLocalPort());
}
}
/**
* {@inheritDoc}
*/
@Override
public void send(DatagramPacket p)
throws IOException
{
if (socketAsDelegatingSocket != null)
{
socketAsDelegatingSocket.send(p);
}
else
{
int len = p.getLength();
int off = p.getOffset();
byte data[] = new byte[len + 2];
data[0] = (byte)((len >> 8) & 0xff);
data[1] = (byte)(len & 0xff);
System.arraycopy(p.getData(), off, data, 2, len);
outputStream.write(data, 0, len + 2);
}
}
} }

View File

@ -49,6 +49,7 @@ public class RtcpDemuxPacketFilter
public static boolean isRtcpPacket(DatagramPacket p) public static boolean isRtcpPacket(DatagramPacket p)
{ {
int len = p.getLength(); int len = p.getLength();
if (len >= 4) //minimum RTCP message length if (len >= 4) //minimum RTCP message length
{ {
byte[] data = p.getData(); byte[] data = p.getData();
@ -57,10 +58,10 @@ public class RtcpDemuxPacketFilter
if (((data[off + 0] & 0xc0) >> 6) == 2) //RTP/RTCP version field if (((data[off + 0] & 0xc0) >> 6) == 2) //RTP/RTCP version field
{ {
int pt = data[off + 1] & 0xff; int pt = data[off + 1] & 0xff;
return (200 <= pt && pt <= 211); return (200 <= pt && pt <= 211);
} }
} }
return false; return false;
} }