From 872bf7e8ef4d7d3ab64c34bf4e92319917391776 Mon Sep 17 00:00:00 2001 From: "lubomir.marinov" Date: Fri, 10 Oct 2014 18:34:22 +0000 Subject: [PATCH] Does not use instanceof on each and every RTP packet. --- src/org/ice4j/socket/TCPOutputStream.java | 45 ++++++++++++----------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/org/ice4j/socket/TCPOutputStream.java b/src/org/ice4j/socket/TCPOutputStream.java index ed6708b..02709f6 100644 --- a/src/org/ice4j/socket/TCPOutputStream.java +++ b/src/org/ice4j/socket/TCPOutputStream.java @@ -18,6 +18,14 @@ import java.io.*; public class TCPOutputStream extends OutputStream { + /** + * The indicator which determines whether this TCPOutputStream is + * to frame RTP and RTCP packets in accord with RFC 4571 "Framing + * Real-time Transport Protocol (RTP) and RTP Control Protocol (RTCP) + * Packets over Connection-Oriented Transport". + */ + private final boolean frame; + /** * Original OutputStream that this class wraps. */ @@ -31,6 +39,12 @@ public class TCPOutputStream public TCPOutputStream(OutputStream outputStream) { this.outputStream = outputStream; + + // GoogleRelayedCandidateSocket will encapsulate data in TURN message so + // do not frame. + frame + = !(outputStream + instanceof GoogleRelayedCandidateSocket.TCPOutputStream); } /** @@ -53,16 +67,6 @@ public class TCPOutputStream outputStream.flush(); } - /** - * {@inheritDoc} - */ - @Override - public void write(byte[] b) - throws IOException - { - write(b, 0, b.length); - } - /** * {@inheritDoc} */ @@ -70,22 +74,19 @@ public class TCPOutputStream public void write(byte[] b, int off, int len) throws IOException { - // GoogleRelayedCandidateSocket will encapsulate data in TURN message so - // do not add framing here - if (outputStream - instanceof GoogleRelayedCandidateSocket.TCPOutputStream) + if (frame) { - outputStream.write(b, off, len); + int newLen = len + 2; + byte newB[] = new byte[newLen]; + + newB[0] = (byte) ((len >> 8) & 0xFF); + newB[1] = (byte) (len & 0xFF); + System.arraycopy(b, off, newB, 2, len); + outputStream.write(newB, 0, newLen); } else { - int dataLength = len + 2; - byte data[] = new byte[dataLength]; - - data[0] = (byte) ((len >> 8) & 0xFF); - data[1] = (byte) (len & 0xFF); - System.arraycopy(b, off, data, 2, len); - outputStream.write(data, 0, dataLength); + outputStream.write(b, off, len); } }