// Save the header
rtmp.setLastReadHeader(channelId, header);
// Check to see if this is a new packets or continue decoding an
// existing one.
Packet packet = rtmp.getLastReadPacket(channelId);
if (packet == null) {
packet = new Packet(header);
rtmp.setLastReadPacket(channelId, packet);
}
final ByteBuffer buf = packet.getData();
final int addSize = (header.getTimer() == 0xffffff ? 4 : 0);
final int readRemaining = header.getSize() + addSize - buf.position();
final int chunkSize = rtmp.getReadChunkSize();
final int readAmount = (readRemaining > chunkSize) ? chunkSize
: readRemaining;
if (in.remaining() < readAmount) {
if (log.isDebugEnabled()) {
log.debug("Chunk too small, buffering (" + in.remaining() + ','
+ readAmount);
}
// skip the position back to the start
in.position(position);
rtmp.bufferDecoding(headerLength + readAmount);
return null;
}
BufferUtils.put(buf, in, readAmount);
if (buf.position() < header.getSize() + addSize) {
rtmp.continueDecoding();
return null;
}
if (log.isWarnEnabled()) {
// Check workaround for SN-19 to find cause for BufferOverflowException
if (buf.position() > header.getSize() + addSize) {
log.warn("Packet size expanded from " + (header.getSize() + addSize) +
" to " + buf.position() + " (" + header + ")");
}
}
buf.flip();
try {
final IRTMPEvent message = decodeMessage(rtmp, packet.getHeader(), buf);
packet.setMessage(message);
if (message instanceof ChunkSize) {
ChunkSize chunkSizeMsg = (ChunkSize) message;
rtmp.setReadChunkSize(chunkSizeMsg.getSize());
}