Skip to main content

Realtime Transport Protocol Help

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
Posted by DDDDDasettan_1989 on September 8, 2012 at 2:44 PM PDT

Hi all, I have the source code of RTP packet class. But dont know how to include payload to it and send it from a source to a destination through the normal client server program. Please help. The class RtpPacket is given below........

package lack;

import java.io.Serializable;
import java.lang.*;
import java.util.*;
import pkg.ByteUtil;
import pkg.RtpException;
//import gov.nist.util.ByteUtil;
//import pkg.ByteUtil;

/**
027.
* This class implements a RTP packet as defined in IETF RFC 3550 with the
029.
* following exceptions:
030.
*

031.
* 1. No CSRC support.
032.
* 2. No header extension
033.
*

034.
* Future versions of this class may support CSRC and RTP header extensions.
035.
*
036.
* The RTP header has the following format:
037.
*

038.
*
039.
*

040.
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
041.
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
042.
* |V=2|P|X| CC |M| PT | sequence number (SN) |
043.
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
044.
* | timestamp (TS) |
045.
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
046.
* | synchronization source (SSRC) identifier |
047.
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
048.
* | contributing source (CSRC) identifiers |
049.
* | .... |
050.
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
051.
*
052.
*
053.
* @author steveq@nist.gov
054.
* @version $Revision: 1.3 $, $Date: 2007-06-03 18:33:24 $
055.
* @since 1.5
056.
*/

public class RtpPacket implements Serializable {

/***************************************************************************
060.
* Constants
061.
**************************************************************************/

/**
064.
*
065.
*/

private static final long serialVersionUID = 0;

/** Constant that identifies the total byte length of fixed fields. */

public final static int FIXED_HEADER_LENGTH = 12; // V..SSRC only

/** The maximum buffer (byte array) size for payload data. */

public static final int MAX_PAYLOAD_BUFFER_SIZE = 512;

/***************************************************************************
075.
* Variables
076.
**************************************************************************/

/*
079.
* Note that each of the following use data types that are larger than what
080.
* is required. The motivation here is to the keep two's complement negative
081.
* bit separate from the bits that represent the value.
082.
*/

/** Version number (2 bits). */

private int V = 0;
/** Padding (1 bit). Note that support for padding is not implemented in
088.
* this version. */

private int P = 0;

/** Header extension (1 bit). */

private int X = 0;

/** CSRC count (4 bits). */

private int CC = 0;

/** Marker (1 bit). */

private int M = 0;

/** Payload type (7 bits). */

private int PT = 0;

/** Sequence number (16 bits). */

protected int SN = (int) (1000 * Math.random());

/** Time stamp (32 bits). */

private long TS = 0;

/** Synchronization source (32 bits). */

private long SSRC = 0;

/** Contributing sources (32 bits) -- not supported yet. */

// private CSRC;

/** Header extension Defined By Profile (16 bits) -- not supported yet. */

// private short DP = 0;

/** Header extension length (16 bits) -- not supported yet. */

// private short EL = 0;

/** The payload.*/

private byte[] payload = null;

/** The length of the payload. */

private int payloadLength = 0;

/***************************************************************************
126.
* Constructor
127.
**************************************************************************/

/**
130.
* Construct an RTP packet.
131.
*/

public RtpPacket() {

}

/**
137.
* Set this RTP packet with the given byte array.
138.
*
139.
* @param bytes
140.
* The byte array for populating this RTP packet.
141.
* @param length
142.
* The number of bytes to read from the byte array.
143.
*/

public RtpPacket(byte[] bytes, int length) {

/*
147.
* Since V..SN are 32 bits, build an int to hold V..SN before
148.
* extracting.
149.
*/

int V_SN_length = 4; // # bytes

byte[] V_SN_bytes = new byte[V_SN_length];

System.arraycopy(bytes, 0, V_SN_bytes, 0, V_SN_length);

/* Extract V..SN */

int V_SN = ByteUtil.bytesToInt(V_SN_bytes);

V = (V_SN >>> 0x1E) & 0x03;

P = (V_SN >>> 0x1D) & 0x01;

X = (V_SN >>> 0x1C) & 0x01;

CC = (V_SN >>> 0x18) & 0x0F;

M = (V_SN >>> 0x17) & 0x01;

PT = (V_SN >>> 0x10) & 0x7F;

SN = (V_SN & 0xFFFF);

int offset = V_SN_length;

/* Extract TS */

int TS_length = 4; // 4 bytes arriving, need to store as long

byte[] TS_bytes = new byte[TS_length];

System.arraycopy(bytes, offset, TS_bytes, 0, TS_length);

byte[] longTS_bytes = new byte[8]; // Copy to long byte array

System.arraycopy(TS_bytes, 0, longTS_bytes, 4, 4);

TS = ByteUtil.bytesToLong(longTS_bytes);

offset += TS_length;

// Extract SSRC

int SSRC_length = 4; // 4 bytes arriving, need to store as long

byte[] SSRC_bytes = new byte[SSRC_length];

System.arraycopy(bytes, offset, SSRC_bytes, 0, SSRC_length);

byte[] longSSRC_bytes = new byte[8]; // Copy to long byte array

System.arraycopy(SSRC_bytes, 0, longSSRC_bytes, 4, 4);

SSRC = ByteUtil.bytesToLong(longSSRC_bytes);

offset += SSRC_length;

// Extract Payload

int payload_length = (length - offset); // # bytes

payloadLength = payload_length;

payload = new byte[payload_length];

System.arraycopy(bytes, offset, payload, 0, payload_length);

// System.out.println("[RTPPacket] Unpacking: "

// + ByteUtil.writeBytes(bytes));

//

// System.out.println("[RTPPacket] Unpacked V: " + V);

// System.out.println("[RTPPacket] Unpacked P: " + P);

// System.out.println("[RTPPacket] Unpacked X: " + X);

// System.out.println("[RTPPacket] Unpacked CC: " + CC);

// System.out.println("[RTPPacket] Unpacked M: " + M);

// System.out.println("[RTPPacket] Unpacked PT: " + PT);

// System.out.println("[RTPPacket] Unpacked SN: " + SN);

// System.out.println("[RTPPacket] Unpacked TS: " + TS);

// System.out.println("[RTPPacket] Unpacked SSRC: " + SSRC);

// System.out.println("[RTPPacket] Unpacked payload: "

// + ByteUtil.writeBytes(payload));

}

/***************************************************************************
211.
* Methods
212.
**************************************************************************/

/**
215.
* Get the data of this RTP packet as a byte array.
216.
*
217.
* @return The data of this RTP packet as a byte array.
218.
*/

/*
220.
* (steveq) Note that we use the same convention for the method name as used
221.
* in DatagramPacket.getData(byte[]).
222.
*/

public byte[] getData() {

/* Since V..SN are 32 bits, create a (int) byte array for V..SN. */

int V_SN = 0;

V_SN |= V; // Add V

V_SN <<= 0x01; // Make room for P

V_SN |= P; // Add P

V_SN <<= 0x01; // Make room for X

V_SN |= X; // Add X

V_SN <<= 0x04; // Make room for CC

V_SN |= CC; // Add CC

V_SN <<= 0x01; // Make room for M

V_SN |= M; // Add M

V_SN <<= 0x07; // Make room for PT

V_SN |= PT; // Add PT

V_SN <<= 0x10; // Make room for SN

V_SN |= SN; // Add SN

byte[] V_SN_bytes = ByteUtil.intToBytes(V_SN);

/*
243.
* Create a byte array for TS. Cast from long to int (we won't lose
244.
* precision because there are never more than 4 bytes of data).
245.
*/

byte[] TS_bytes = ByteUtil.intToBytes((int) TS);

/*
249.
* Create a byte array for SSRC. Cast from long to int (we won't lose
250.
* precision because there are never more than 4 bytes of data).
251.
*/

byte[] SSRC_bytes = ByteUtil.intToBytes((int) SSRC);

/* Create byte array for all data. */

int length = V_SN_bytes.length + TS_bytes.length + SSRC_bytes.length

+ payloadLength;

byte[] data = new byte[length];

int offset = 0;

System.arraycopy(V_SN_bytes, 0, data, offset, V_SN_bytes.length);

offset += V_SN_bytes.length;

System.arraycopy(TS_bytes, 0, data, offset, TS_bytes.length);

offset += TS_bytes.length;

System.arraycopy(SSRC_bytes, 0, data, offset, SSRC_bytes.length);

offset += SSRC_bytes.length;

System.arraycopy(payload, 0, data, offset, payloadLength);

// System.out.println("[RTPPacket] Packing V: " + V);

// System.out.println("[RTPPacket] Packing P: " + P);

// System.out.println("[RTPPacket] Packing X: " + X);

// System.out.println("[RTPPacket] Packing CC: " + CC);

// System.out.println("[RTPPacket] Packing M: " + M);

// System.out.println("[RTPPacket] Packing PT: " + PT);

// System.out.println("[RTPPacket] Packing SN: " + SN);

// System.out.println("[RTPPacket] Packing TS: " + TS);

// System.out.println("[RTPPacket] Packing SSRC: " + SSRC);

// System.out.println("[RTPPacket] Packing payload: "

// + ByteUtil.writeBytes(payload));

//

// System.out.println("[RTPPacket] Packed: " + ByteUtil.writeBytes(data));

return data;

}

/*
293.
* (steveq) The following uses method names based on solely on block
294.
* diagrams for setters and getters. The reasoning here is that those that
295.
* will be using this RTP stack will want to methods that are directly
296.
* reflective of the field parameter mnemonic (e.g., setV for field V).
297.
* Those that require further description are directed to refer to the
298.
* javadoc for this class.
299.
*/

/**
301.
* Set this RTP version.
302.
*
303.
* @param i
304.
* This RTP version (2 bits)
305.
* @throws IllegalArgumentException
306.
*/

public void setV(int i) throws IllegalArgumentException {

if ((0 <= i) && (i <= ByteUtil.getMaxIntValueForNumBits(2)))

V = i;

else

throw new IllegalArgumentException(RtpException.OUT_OF_RANGE);

}

/**
317.
* Get the RTP version.
318.
*/

public int getV() {

return V;

}

/**
326.
* Set the padding bit.
327.
*
328.
* @param i
329.
* The padding (1 bit).
330.
* @throws IllegalArgumentException
331.
*/

public void setP(int i) throws IllegalArgumentException {

if ((0 <= i) && (i <= ByteUtil.getMaxIntValueForNumBits(1)))

P = i;

else

throw new IllegalArgumentException(RtpException.OUT_OF_RANGE);

}

/**
342.
* Get the padding bit.
343.
*
344.
* @return The padding.
345.
*/

public int getP() {

return P;

}

/**
353.
* Set the extension.
354.
*
355.
* @param i
356.
* The extension (1 bit)
357.
* @throws IllegalArgumentException
358.
*/

public void setX(int i) throws IllegalArgumentException {

if ((0 <= i) && (i <= ByteUtil.getMaxIntValueForNumBits(1)))

X = i;

else

throw new IllegalArgumentException(RtpException.OUT_OF_RANGE);

}

/**
369.
* Get the extension.
370.
*
371.
* @return the extension.
372.
*/

public int getX() {

return X;

}

/**
380.
* Set the CSRC count.
381.
*
382.
* @param i
383.
* The CSRC count (4 bits)
384.
* @throws IllegalArgumentException
385.
*/

public void setCC(int i) throws IllegalArgumentException {

if ((0 <= i) && (i <= ByteUtil.getMaxIntValueForNumBits(4)))

CC = i;

else

throw new IllegalArgumentException(RtpException.OUT_OF_RANGE);

}
/**
396.
* Get the CSRC count.
397.
*
398.
* @return the CSRC count.
399.
*/

public int getCC() {

return CC;

}

/**
407.
* Set the marker.
408.
*
409.
* @param i
410.
* The marker (1 bit)
411.
* @throws IllegalArgumentException
412.
*/

public void setM(int i) throws IllegalArgumentException {

if ((0 <= i) && (i <= ByteUtil.getMaxIntValueForNumBits(1)))

M = i;

else

throw new IllegalArgumentException(RtpException.OUT_OF_RANGE);

}

/**
423.
* Get the marker.
424.
*
425.
* @return the marker.
426.
*/

public int getM() {

return M;

}

/**
434.
* Set the payload type.
435.
*
436.
* @param i
437.
* The payload type (7 bits)
438.
* @throws IllegalArgumentException
439.
*/

public void setPT(int i) throws IllegalArgumentException {

if ((0 <= i) && (i <= ByteUtil.getMaxIntValueForNumBits(7)))

PT = i;

else

throw new IllegalArgumentException(RtpException.OUT_OF_RANGE);

}

/**
450.
* Get the payload type.
451.
*
452.
* @return The payload type.
453.
*/

public int getPT() {

return PT;

}

/**
461.
* Set the sequence number.
462.
*
463.
* @param i
464.
* The sequence number (16 bits)
465.
* @throws IllegalArgumentException
466.
*/

protected void setSN(int i) throws IllegalArgumentException {

if ((0 <= i) && (i <= ByteUtil.getMaxIntValueForNumBits(16)))

SN = i;

else

throw new IllegalArgumentException(RtpException.OUT_OF_RANGE);

}

/**
477.
* Get the sequence number.
478.
*
479.
* @return the sequence number.
480.
*/

public int getSN() {

return SN;

}

/**
488.
* Set the time stamp.
489.
*
490.
* @param timeStamp
491.
* The time stamp (32 bits).
492.
* @throws IllegalArgumentException
493.
*/

public void setTS(long timeStamp) throws IllegalArgumentException {

if ((0 <= timeStamp) && (timeStamp <= ByteUtil.getMaxLongValueForNumBits(32)))

TS = timeStamp;

else

throw new IllegalArgumentException(RtpException.OUT_OF_RANGE);

}

/**
504.
* Get the time stamp.
505.
*
506.
* @return the time stamp.
507.
*/

public long getTS() {

return TS;

}

/**
515.
* Set the synchronization source identifier.
516.
*
517.
* @param ssrc
518.
* the synchronization source identifier (32 bits)
519.
* @throws IllegalArgumentException
520.
*/

public void setSSRC(long ssrc) throws IllegalArgumentException {

if ((0 <= ssrc) && (ssrc <= ByteUtil.getMaxLongValueForNumBits(32)))

SSRC = ssrc;

else

throw new IllegalArgumentException(RtpException.OUT_OF_RANGE + ssrc);

}

/**
531.
* Get the synchronization source identifier.
532.
*
533.
* @return the synchronization source identifier.
534.
*/

public long getSSRC() {

return SSRC;

}

// public int getCSRC() {}

// public void setCSRC() {}
/***************************************************************************
545.
* RTP Header Extensions
546.
**************************************************************************/

// public void getDP(){}

// public void setDP(){}

// public void getEL(){}

// public void setEL(){}

/***************************************************************************
553.
* Other Methods
554.
**************************************************************************/

/**
557.
* Get the payload of this RTP packet.
558.
*
559.
* @return the payload of this RTP packet.
560.
*/

public byte[] getPayload() {

return payload;

}

/**
568.
* Set the payload of this RTP packet.
569.
*
570.
* @param bytes
571.
* the byte buffer containing the payload
572.
* @param length
573.
* the number of buffer bytes containing the payload.
574.
*/

public void setPayload(byte[] bytes, int length)

throws IllegalArgumentException {

if (length > MAX_PAYLOAD_BUFFER_SIZE)

throw new IllegalArgumentException(

"Payload is too large Max Size is limited to "

+ MAX_PAYLOAD_BUFFER_SIZE);

payloadLength = length;

payload = bytes;

}

/**
589.
* Get the payload length.
590.
*
591.
* @return they payload length.
592.
*/

public int getPayloadLength() {

return payloadLength;

}

/**
600.
* Get the XML formatted string representation.
601.
*
602.
* @return the XML formatted string representation.
603.
*/

public String toString() {

StringBuffer sb = new StringBuffer();

sb.append("

"\nheaderExtension \"" + X + "\"").append(

"\nmarker = \"" + M + "\"").append(

"\npayloadType =\"" + PT + "\"").append(

"\nSequenceNumber =\"" + SN + "\"").append(

"\nimeStamp = \"" + TS + "\"").append(

"\nSSRC= \"" + SSRC + "\"").append("\n/>");

return sb.toString();

}

}