}
public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
MRTMPPacket packet = (MRTMPPacket) message;
MRTMPPacket.Header header = packet.getHeader();
ByteBuffer buf = null;
switch (header.getType()) {
case MRTMPPacket.CONNECT:
case MRTMPPacket.CLOSE:
buf = ByteBuffer.allocate(MRTMPPacket.COMMON_HEADER_LENGTH);
buf.setAutoExpand(true);
break;
case MRTMPPacket.RTMP:
buf = ByteBuffer.allocate(MRTMPPacket.RTMP_HEADER_LENGTH);
buf.setAutoExpand(true);
break;
default:
break;
}
if (buf == null) {
return;
}
buf.putShort(header.getType());
buf.putShort(MRTMPPacket.JAVA_ENCODING);
int preserved = header.isDynamic() ? 0x80000000 : 0;
buf.putInt(preserved);
buf.putInt(header.getClientId());
if (header.getType() == MRTMPPacket.CONNECT ||
header.getType() == MRTMPPacket.CLOSE) {
buf.putInt(MRTMPPacket.COMMON_HEADER_LENGTH);
buf.putInt(0);
} else if (header.getType() == MRTMPPacket.RTMP) {
buf.putInt(MRTMPPacket.RTMP_HEADER_LENGTH);
int bodyLengthPos = buf.position();
buf.putInt(0);
MRTMPPacket.RTMPHeader rtmpHeader = (MRTMPPacket.RTMPHeader) packet.getHeader();
buf.putInt(rtmpHeader.getRtmpType());
MRTMPPacket.RTMPBody body = (MRTMPPacket.RTMPBody) packet.getBody();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(body.getRtmpPacket());
oos.close();
buf.put(baos.toByteArray());