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