1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.rtsp;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.ByteBufUtil;
20 import io.netty.handler.codec.UnsupportedMessageTypeException;
21 import io.netty.handler.codec.http.HttpContent;
22 import io.netty.handler.codec.http.HttpMessage;
23 import io.netty.handler.codec.http.HttpObjectEncoder;
24 import io.netty.handler.codec.http.HttpRequest;
25 import io.netty.handler.codec.http.HttpResponse;
26 import io.netty.util.CharsetUtil;
27
28 import static io.netty.handler.codec.http.HttpConstants.*;
29
30
31
32
33
34 public class RtspEncoder extends HttpObjectEncoder<HttpMessage> {
35 private static final int CRLF_SHORT = (CR << 8) | LF;
36
37 @Override
38 public boolean acceptOutboundMessage(final Object msg)
39 throws Exception {
40 return super.acceptOutboundMessage(msg) && ((msg instanceof HttpRequest) || (msg instanceof HttpResponse));
41 }
42
43 @Override
44 protected void encodeInitialLine(final ByteBuf buf, final HttpMessage message)
45 throws Exception {
46 if (message instanceof HttpRequest) {
47 HttpRequest request = (HttpRequest) message;
48 ByteBufUtil.copy(request.method().asciiName(), buf);
49 buf.writeByte(SP);
50 buf.writeCharSequence(request.uri(), CharsetUtil.UTF_8);
51 buf.writeByte(SP);
52 buf.writeCharSequence(request.protocolVersion().toString(), CharsetUtil.US_ASCII);
53 ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
54 } else if (message instanceof HttpResponse) {
55 HttpResponse response = (HttpResponse) message;
56 buf.writeCharSequence(response.protocolVersion().toString(), CharsetUtil.US_ASCII);
57 buf.writeByte(SP);
58 ByteBufUtil.copy(response.status().codeAsText(), buf);
59 buf.writeByte(SP);
60 buf.writeCharSequence(response.status().reasonPhrase(), CharsetUtil.US_ASCII);
61 ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
62 } else {
63 throw new UnsupportedMessageTypeException(message, HttpRequest.class, HttpResponse.class);
64 }
65 }
66 }