Revision: 50340
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at August 18, 2011 05:35 by rowntreerob
Initial Code
public static DataPacket decode(ChannelBuffer buffer) throws IndexOutOfBoundsException {
if (buffer.readableBytes() < 12) {
throw new IllegalArgumentException("A RTP packet must be at least 12 octets long");
}
// Version, Padding, eXtension, CSRC Count
DataPacket packet = new DataPacket();
byte b = buffer.readByte();
packet.version = RtpVersion.fromByte(b);
boolean padding = (b & 0x20) > 0; // mask 0010 0000
boolean extension = (b & 0x10) > 0; // mask 0001 0000
int contributingSourcesCount = b & 0x0f; // mask 0000 1111
// Marker, Payload Type
b = buffer.readByte();
packet.marker = (b & 0x80) > 0; // mask 0000 0001
packet.payloadType = (b & 0x7f); // mask 0111 1111
packet.sequenceNumber = buffer.readUnsignedShort();
packet.timestamp = buffer.readUnsignedInt();
packet.ssrc = buffer.readUnsignedInt();
// Read extension headers & data
if (extension) {
packet.extensionHeaderData = buffer.readShort();
packet.extensionData = new byte[buffer.readUnsignedShort()];
buffer.readBytes(packet.extensionData);
}
// Read CCRC's
if (contributingSourcesCount > 0) {
packet.contributingSourceIds = new ArrayList<Long>(contributingSourcesCount);
for (int i = 0; i < contributingSourcesCount; i++) {
long contributingSource = buffer.readUnsignedInt();
packet.contributingSourceIds.add(contributingSource);
}
}
if (!padding) {
// No padding used, assume remaining data is the packet
byte[] remainingBytes = new byte[buffer.readableBytes()];
buffer.readBytes(remainingBytes);
packet.setData(remainingBytes);
} else {
// Padding bit was set, so last byte contains the number of padding octets that should be discarded.
short lastByte = buffer.getUnsignedByte(buffer.readerIndex() + buffer.readableBytes() - 1);
byte[] dataBytes = new byte[buffer.readableBytes() - lastByte];
buffer.readBytes(dataBytes);
packet.setData(dataBytes);
// Discard rest of buffer.
buffer.skipBytes(buffer.readableBytes());
}
return packet;
}
Initial URL
Initial Description
java handler for RTP raw audio packet in: com.biasedbit.com.efflux.packet.DataPacket
Initial Title
RTP packet handler
Initial Tags
Initial Language
Java