Created RTSPEncoder and RTSPDecoder which are now common for both requests and responses to be able to handle both types of messages on the same channel.

Keep RTSPRequestEncoder, RTSPRequestDecoder, RTSPResponseEncoder and
RTSPResponseDecoder for backwards compatibility but they now just extends
the generic encoder/decoder and are markes as deprecated.

Renamed the decoder test, because the decoder is now generic. Added
testcase for when ANNOUNCE request is received from server.

Created testcases for encoder.

Mark abstract base classes RTSPObjectEncoder and RTSPObjectDecoder as
deprecated, that functionality is now in RTSPEncoder and RTSPDecoder.

Added annotation in RtspHeaders to suppress warnings about deprecation, no need when
whole class is deprecated.
This commit is contained in:
Sverker Abrahamsson 2015-05-23 01:34:21 +02:00 committed by Norman Maurer
parent 1bcfb910d1
commit 3b2648c3d1
10 changed files with 513 additions and 204 deletions

View File

@ -0,0 +1,174 @@
/*
* Copyright 2015 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.rtsp;
import java.util.regex.Pattern;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpObjectDecoder;
import io.netty.handler.codec.http.HttpResponseStatus;
/**
* Decodes {@link ByteBuf}s into RTSP messages represented in
* {@link HttpMessage}s.
* <p>
* <h3>Parameters that prevents excessive memory consumption</h3>
* <table border="1">
* <tr>
* <th>Name</th><th>Meaning</th>
* </tr>
* <tr>
* <td>{@code maxInitialLineLength}</td>
* <td>The maximum length of the initial line
* (e.g. {@code "SETUP / RTSP/1.0"} or {@code "RTSP/1.0 200 OK"})
* If the length of the initial line exceeds this value, a
* {@link TooLongFrameException} will be raised.</td>
* </tr>
* <tr>
* <td>{@code maxHeaderSize}</td>
* <td>The maximum length of all headers. If the sum of the length of each
* header exceeds this value, a {@link TooLongFrameException} will be
* raised.</td>
* </tr>
* <tr>
* <td>{@code maxContentLength}</td>
* <td>The maximum length of the content. If the content length exceeds this
* value, a {@link TooLongFrameException} will be raised.</td>
* </tr>
* </table>
*/
public class RtspDecoder extends HttpObjectDecoder {
/**
* Status code for unknown responses.
*/
private static final HttpResponseStatus UNKNOWN_STATUS =
new HttpResponseStatus(999, "Unknown");
/**
* True if the message to decode is a request.
* False if the message to decode is a response.
*/
private boolean isDecodingRequest;
/**
* Regex used on first line in message to detect if it is a response.
*/
private static final Pattern versionPattern = Pattern.compile("RTSP/\\d\\.\\d");
/**
* Constant for default max initial line length.
*/
public static final int DEFAULT_MAX_INITIAL_LINE_LENGTH = 4096;
/**
* Constant for default max header size.
*/
public static final int DEFAULT_MAX_HEADER_SIZE = 8192;
/**
* Constant for default max content length.
*/
public static final int DEFAULT_MAX_CONTENT_LENGTH = 8192;
/**
* Creates a new instance with the default
* {@code maxInitialLineLength (4096)}, {@code maxHeaderSize (8192)}, and
* {@code maxContentLength (8192)}.
*/
public RtspDecoder() {
this(DEFAULT_MAX_INITIAL_LINE_LENGTH,
DEFAULT_MAX_HEADER_SIZE,
DEFAULT_MAX_CONTENT_LENGTH);
}
/**
* Creates a new instance with the specified parameters.
* @param maxInitialLineLength The max allowed length of initial line
* @param maxHeaderSize The max allowed size of header
* @param maxContentLength The max allowed content length
*/
public RtspDecoder(final int maxInitialLineLength,
final int maxHeaderSize,
final int maxContentLength) {
super(maxInitialLineLength, maxHeaderSize, maxContentLength * 2, false);
}
/**
* Creates a new instance with the specified parameters.
* @param maxInitialLineLength The max allowed length of initial line
* @param maxHeaderSize The max allowed size of header
* @param maxContentLength The max allowed content length
* @param validateHeaders Set to true if headers should be validated
*/
public RtspDecoder(final int maxInitialLineLength,
final int maxHeaderSize,
final int maxContentLength,
final boolean validateHeaders) {
super(maxInitialLineLength,
maxHeaderSize,
maxContentLength * 2,
false,
validateHeaders);
}
@Override
protected HttpMessage createMessage(final String[] initialLine)
throws Exception {
// If the first element of the initial line is a version string then
// this is a response
if (versionPattern.matcher(initialLine[0]).matches()) {
isDecodingRequest = false;
return new DefaultHttpResponse(RtspVersions.valueOf(initialLine[0]),
new HttpResponseStatus(Integer.parseInt(initialLine[1]),
initialLine[2]),
validateHeaders);
} else {
isDecodingRequest = true;
return new DefaultHttpRequest(RtspVersions.valueOf(initialLine[2]),
RtspMethods.valueOf(initialLine[0]),
initialLine[1],
validateHeaders);
}
}
@Override
protected boolean isContentAlwaysEmpty(final HttpMessage msg) {
// Unlike HTTP, RTSP always assumes zero-length body if Content-Length
// header is absent.
return super.isContentAlwaysEmpty(msg) || !msg.headers().contains(RtspHeaders.Names.CONTENT_LENGTH);
}
@Override
protected HttpMessage createInvalidMessage() {
if (isDecodingRequest) {
return new DefaultFullHttpRequest(RtspVersions.RTSP_1_0,
RtspMethods.OPTIONS, "/bad-request", Unpooled.EMPTY_BUFFER, validateHeaders);
} else {
return new DefaultFullHttpResponse(RtspVersions.RTSP_1_0,
UNKNOWN_STATUS, Unpooled.EMPTY_BUFFER,
validateHeaders);
}
}
@Override
protected boolean isDecodingRequest() {
return isDecodingRequest;
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright 2015 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.rtsp;
import static io.netty.handler.codec.http.HttpConstants.CR;
import static io.netty.handler.codec.http.HttpConstants.LF;
import static io.netty.handler.codec.http.HttpConstants.SP;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.UnsupportedMessageTypeException;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpObjectEncoder;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.StringUtil;
/**
* Encodes an RTSP message represented in {@link HttpMessage} or an {@link HttpContent} into
* a {@link ByteBuf}.
*/
public class RtspEncoder extends HttpObjectEncoder<HttpMessage> {
/**
* Constant for CRLF.
*/
private static final byte[] CRLF = {CR, LF};
@Override
public boolean acceptOutboundMessage(final Object msg)
throws Exception {
return super.acceptOutboundMessage(msg) && ((msg instanceof HttpRequest) || (msg instanceof HttpResponse));
}
@Override
protected void encodeInitialLine(final ByteBuf buf, final HttpMessage message)
throws Exception {
if (message instanceof HttpRequest) {
HttpRequest request = (HttpRequest) message;
HttpHeaders.encodeAscii(request.getMethod().toString(), buf);
buf.writeByte(SP);
buf.writeBytes(request.getUri().getBytes(CharsetUtil.UTF_8));
buf.writeByte(SP);
HttpHeaders.encodeAscii(request.getProtocolVersion().toString(), buf);
buf.writeBytes(CRLF);
} else if (message instanceof HttpResponse) {
HttpResponse response = (HttpResponse) message;
HttpHeaders.encodeAscii(response.getProtocolVersion().toString(),
buf);
buf.writeByte(SP);
buf.writeBytes(String.valueOf(response.getStatus().code())
.getBytes(CharsetUtil.US_ASCII));
buf.writeByte(SP);
HttpHeaders.encodeAscii(String.valueOf(response.getStatus().reasonPhrase()),
buf);
buf.writeBytes(CRLF);
} else {
throw new UnsupportedMessageTypeException("Unsupported type "
+ StringUtil.simpleClassName(message));
}
}
}

View File

@ -47,7 +47,10 @@ import io.netty.handler.codec.http.HttpObjectDecoder;
* value, a {@link TooLongFrameException} will be raised.</td> * value, a {@link TooLongFrameException} will be raised.</td>
* </tr> * </tr>
* </table> * </table>
*
* @deprecated Use {@link RtspDecoder} instead.
*/ */
@Deprecated
public abstract class RtspObjectDecoder extends HttpObjectDecoder { public abstract class RtspObjectDecoder extends HttpObjectDecoder {
/** /**

View File

@ -24,8 +24,11 @@ import io.netty.handler.codec.http.HttpObjectEncoder;
/** /**
* Encodes an RTSP message represented in {@link FullHttpMessage} into * Encodes an RTSP message represented in {@link FullHttpMessage} into
* a {@link ByteBuf}. * a {@link ByteBuf}.
*
* @deprecated Use {@link RtspEncoder} instead.
*/ */
@Sharable @Sharable
@Deprecated
public abstract class RtspObjectEncoder<H extends HttpMessage> extends HttpObjectEncoder<H> { public abstract class RtspObjectEncoder<H extends HttpMessage> extends HttpObjectEncoder<H> {
/** /**

View File

@ -15,77 +15,9 @@
*/ */
package io.netty.handler.codec.rtsp; package io.netty.handler.codec.rtsp;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.TooLongFrameException;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpRequest;
/** /**
* Decodes {@link ByteBuf}s into RTSP requests represented in * @deprecated Use {@link RtspDecoder} directly instead
* {@link HttpRequest}s.
* <p>
* <h3>Parameters that prevents excessive memory consumption</h3>
* <table border="1">
* <tr>
* <th>Name</th><th>Meaning</th>
* </tr>
* <tr>
* <td>{@code maxInitialLineLength}</td>
* <td>The maximum length of the initial line (e.g. {@code "SETUP / RTSP/1.0"})
* If the length of the initial line exceeds this value, a
* {@link TooLongFrameException} will be raised.</td>
* </tr>
* <tr>
* <td>{@code maxHeaderSize}</td>
* <td>The maximum length of all headers. If the sum of the length of each
* header exceeds this value, a {@link TooLongFrameException} will be raised.</td>
* </tr>
* <tr>
* <td>{@code maxContentLength}</td>
* <td>The maximum length of the content. If the content length exceeds this
* value, a {@link TooLongFrameException} will be raised.</td>
* </tr>
* </table>
*/ */
public class RtspRequestDecoder extends RtspObjectDecoder { @Deprecated
public class RtspRequestDecoder extends RtspDecoder {
/**
* Creates a new instance with the default
* {@code maxInitialLineLength (4096}}, {@code maxHeaderSize (8192)}, and
* {@code maxContentLength (8192)}.
*/
public RtspRequestDecoder() {
}
/**
* Creates a new instance with the specified parameters.
*/
public RtspRequestDecoder(int maxInitialLineLength, int maxHeaderSize, int maxContentLength) {
super(maxInitialLineLength, maxHeaderSize, maxContentLength);
}
public RtspRequestDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxContentLength, boolean validateHeaders) {
super(maxInitialLineLength, maxHeaderSize, maxContentLength, validateHeaders);
}
@Override
protected HttpMessage createMessage(String[] initialLine) throws Exception {
return new DefaultHttpRequest(RtspVersions.valueOf(initialLine[2]),
RtspMethods.valueOf(initialLine[0]), initialLine[1], validateHeaders);
}
@Override
protected HttpMessage createInvalidMessage() {
return new DefaultFullHttpRequest(
RtspVersions.RTSP_1_0, RtspMethods.OPTIONS, "/bad-request", Unpooled.EMPTY_BUFFER, validateHeaders);
}
@Override
protected boolean isDecodingRequest() {
return true;
}
} }

View File

@ -15,35 +15,9 @@
*/ */
package io.netty.handler.codec.rtsp; package io.netty.handler.codec.rtsp;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.CharsetUtil;
import static io.netty.handler.codec.http.HttpConstants.*;
/** /**
* Encodes an RTSP request represented in {@link FullHttpRequest} into * @deprecated Use {@link RtspEncoder} directly instead
* a {@link ByteBuf}.
*/ */
public class RtspRequestEncoder extends RtspObjectEncoder<HttpRequest> { @Deprecated
private static final byte[] CRLF = { CR, LF }; public class RtspRequestEncoder extends RtspEncoder {
@Override
public boolean acceptOutboundMessage(Object msg) throws Exception {
return msg instanceof FullHttpRequest;
}
@Override
@SuppressWarnings("deprecation")
protected void encodeInitialLine(ByteBuf buf, HttpRequest request) throws Exception {
HttpHeaders.encodeAscii(request.getMethod().toString(), buf);
buf.writeByte(SP);
buf.writeBytes(request.getUri().getBytes(CharsetUtil.UTF_8));
buf.writeByte(SP);
encodeAscii(request.getProtocolVersion().toString(), buf);
buf.writeBytes(CRLF);
}
} }

View File

@ -15,82 +15,9 @@
*/ */
package io.netty.handler.codec.rtsp; package io.netty.handler.codec.rtsp;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.TooLongFrameException;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
/** /**
* Decodes {@link ByteBuf}s into RTSP responses represented in * @deprecated Use {@link RtspDecoder} directly instead
* {@link HttpResponse}s.
* <p>
* <h3>Parameters that prevents excessive memory consumption</h3>
* <table border="1">
* <tr>
* <th>Name</th><th>Meaning</th>
* </tr>
* <tr>
* <td>{@code maxInitialLineLength}</td>
* <td>The maximum length of the initial line (e.g. {@code "RTSP/1.0 200 OK"})
* If the length of the initial line exceeds this value, a
* {@link TooLongFrameException} will be raised.</td>
* </tr>
* <tr>
* <td>{@code maxHeaderSize}</td>
* <td>The maximum length of all headers. If the sum of the length of each
* header exceeds this value, a {@link TooLongFrameException} will be raised.</td>
* </tr>
* <tr>
* <td>{@code maxContentLength}</td>
* <td>The maximum length of the content. If the content length exceeds this
* value, a {@link TooLongFrameException} will be raised.</td>
* </tr>
* </table>
*/ */
public class RtspResponseDecoder extends RtspObjectDecoder { @Deprecated
public class RtspResponseDecoder extends RtspDecoder {
private static final HttpResponseStatus UNKNOWN_STATUS = new HttpResponseStatus(999, "Unknown");
/**
* Creates a new instance with the default
* {@code maxInitialLineLength (4096}}, {@code maxHeaderSize (8192)}, and
* {@code maxContentLength (8192)}.
*/
public RtspResponseDecoder() {
}
/**
* Creates a new instance with the specified parameters.
*/
public RtspResponseDecoder(int maxInitialLineLength, int maxHeaderSize,
int maxContentLength) {
super(maxInitialLineLength, maxHeaderSize, maxContentLength);
}
public RtspResponseDecoder(int maxInitialLineLength, int maxHeaderSize,
int maxContentLength, boolean validateHeaders) {
super(maxInitialLineLength, maxHeaderSize, maxContentLength, validateHeaders);
}
@Override
protected HttpMessage createMessage(String[] initialLine) throws Exception {
return new DefaultHttpResponse(
RtspVersions.valueOf(initialLine[0]),
new HttpResponseStatus(Integer.parseInt(initialLine[1]), initialLine[2]), validateHeaders);
}
@Override
protected HttpMessage createInvalidMessage() {
return new DefaultFullHttpResponse(
RtspVersions.RTSP_1_0, UNKNOWN_STATUS, Unpooled.EMPTY_BUFFER, validateHeaders);
}
@Override
protected boolean isDecodingRequest() {
return false;
}
} }

View File

@ -15,34 +15,9 @@
*/ */
package io.netty.handler.codec.rtsp; package io.netty.handler.codec.rtsp;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.util.CharsetUtil;
import static io.netty.handler.codec.http.HttpConstants.*;
/** /**
* Encodes an RTSP response represented in {@link FullHttpResponse} into * @deprecated Use {@link RtspEncoder} directly instead
* a {@link ByteBuf}.
*/ */
public class RtspResponseEncoder extends RtspObjectEncoder<HttpResponse> { @Deprecated
private static final byte[] CRLF = { CR, LF }; public class RtspResponseEncoder extends RtspEncoder {
@Override
public boolean acceptOutboundMessage(Object msg) throws Exception {
return msg instanceof FullHttpResponse;
}
@Override
@SuppressWarnings("deprecation")
protected void encodeInitialLine(ByteBuf buf, HttpResponse response) throws Exception {
HttpHeaders.encodeAscii(response.getProtocolVersion().toString(), buf);
buf.writeByte(SP);
buf.writeBytes(String.valueOf(response.getStatus().code()).getBytes(CharsetUtil.US_ASCII));
buf.writeByte(SP);
encodeAscii(String.valueOf(response.getStatus().reasonPhrase()), buf);
buf.writeBytes(CRLF);
}
} }

View File

@ -0,0 +1,73 @@
/*
* Copyright 2015 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.rtsp;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpObjectAggregator;
import org.junit.Test;
/**
* Test cases for RTSP decoder.
*/
public class RtspDecoderTest {
/**
* There was a problem when an ANNOUNCE request was issued by the server,
* i.e. entered through the response decoder. First the decoder failed to
* parse the ANNOUNCE request, then it stopped receiving any more
* responses. This test verifies that the issue is solved.
*/
@Test
public void testReceiveAnnounce() {
byte[] data1 = ("ANNOUNCE rtsp://172.20.184.218:554/d3abaaa7-65f2-"
+ "42b4-8d6b-379f492fcf0f RTSP/1.0\r\n"
+ "CSeq: 2\r\n"
+ "Session: 2777476816092819869\r\n"
+ "x-notice: 5402 \"Session Terminated by Server\" "
+ "event-date=20150514T075303Z\r\n"
+ "Range: npt=0\r\n\r\n").getBytes();
byte[] data2 = ("RTSP/1.0 200 OK\r\n" +
"Server: Orbit2x\r\n" +
"CSeq: 172\r\n" +
"Session: 2547019973447939919\r\n" +
"\r\n").getBytes();
EmbeddedChannel ch = new EmbeddedChannel(new RtspDecoder(),
new HttpObjectAggregator(1048576));
ch.writeInbound(Unpooled.wrappedBuffer(data1),
Unpooled.wrappedBuffer(data2));
HttpObject res1 = (HttpObject) ch.readInbound();
System.out.println(res1);
assertNotNull(res1);
assertTrue(res1 instanceof FullHttpRequest);
((FullHttpRequest) res1).release();
HttpObject res2 = (HttpObject) ch.readInbound();
System.out.println(res2);
assertNotNull(res2);
assertTrue(res2 instanceof FullHttpResponse);
((FullHttpResponse) res2).release();
}
}

View File

@ -0,0 +1,173 @@
/*
* Copyright 2015 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.rtsp;
import static org.junit.Assert.assertEquals;
import io.netty.buffer.ByteBuf;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.util.CharsetUtil;
import org.junit.Test;
/**
* Test cases for RTSP encoder.
*/
public class RtspEncoderTest {
/**
* Test of a SETUP request, with no body.
*/
@Test
public void testSendSetupRequest() {
String expected = "SETUP rtsp://172.10.20.30:554/d3abaaa7-65f2-42b4-"
+ "8d6b-379f492fcf0f RTSP/1.0\r\n"
+ "Transport: MP2T/DVBC/UDP;unicast;client=01234567;"
+ "source=172.10.20.30;"
+ "destination=1.1.1.1;client_port=6922\r\n"
+ "CSeq: 1\r\n"
+ "\r\n";
HttpRequest request = new DefaultHttpRequest(RtspVersions.RTSP_1_0,
RtspMethods.SETUP,
"rtsp://172.10.20.30:554/d3abaaa7-65f2-42b4-8d6b-379f492fcf0f");
request.headers().add(RtspHeaders.Names.TRANSPORT,
"MP2T/DVBC/UDP;unicast;client=01234567;source=172.10.20.30;" +
"destination=1.1.1.1;client_port=6922");
request.headers().add(RtspHeaders.Names.CSEQ, "1");
EmbeddedChannel ch = new EmbeddedChannel(new RtspEncoder());
ch.writeOutbound(request);
ByteBuf buf = (ByteBuf) ch.readOutbound();
String actual = buf.toString(CharsetUtil.UTF_8);
buf.release();
assertEquals(expected, actual);
}
/**
* Test of a GET_PARAMETER request, with body.
*/
@Test
public void testSendGetParameterRequest() {
String expected = "GET_PARAMETER rtsp://172.10.20.30:554 RTSP/1.0\r\n"
+ "Session: 2547019973447939919\r\n"
+ "CSeq: 3\r\n"
+ "Content-Length: 31\r\n"
+ "Content-Type: text/parameters\r\n"
+ "\r\n"
+ "stream_state\r\n"
+ "position\r\n"
+ "scale\r\n";
byte[] content = ("stream_state\r\n"
+ "position\r\n"
+ "scale\r\n").getBytes(CharsetUtil.UTF_8);
FullHttpRequest request = new DefaultFullHttpRequest(
RtspVersions.RTSP_1_0,
RtspMethods.GET_PARAMETER,
"rtsp://172.10.20.30:554");
request.headers().add(RtspHeaders.Names.SESSION, "2547019973447939919");
request.headers().add(RtspHeaders.Names.CSEQ, "3");
request.headers().add(RtspHeaders.Names.CONTENT_LENGTH,
"" + content.length);
request.headers().add(RtspHeaders.Names.CONTENT_TYPE, "text/parameters");
request.content().writeBytes(content);
EmbeddedChannel ch = new EmbeddedChannel(new RtspEncoder());
ch.writeOutbound(request);
ByteBuf buf = (ByteBuf) ch.readOutbound();
String actual = buf.toString(CharsetUtil.UTF_8);
buf.release();
assertEquals(expected, actual);
}
/**
* Test of a 200 OK response, without body.
*/
@Test
public void testSend200OkResponseWithoutBody() {
String expected = "RTSP/1.0 200 OK\r\n"
+ "Server: Testserver\r\n"
+ "CSeq: 1\r\n"
+ "Session: 2547019973447939919\r\n"
+ "\r\n";
HttpResponse response = new DefaultHttpResponse(RtspVersions.RTSP_1_0,
RtspResponseStatuses.OK);
response.headers().add(RtspHeaders.Names.SERVER, "Testserver");
response.headers().add(RtspHeaders.Names.CSEQ, "1");
response.headers().add(RtspHeaders.Names.SESSION, "2547019973447939919");
EmbeddedChannel ch = new EmbeddedChannel(new RtspEncoder());
ch.writeOutbound(response);
ByteBuf buf = (ByteBuf) ch.readOutbound();
String actual = buf.toString(CharsetUtil.UTF_8);
buf.release();
assertEquals(expected, actual);
}
/**
* Test of a 200 OK response, with body.
*/
@Test
public void testSend200OkResponseWithBody() {
String expected = "RTSP/1.0 200 OK\r\n"
+ "Server: Testserver\r\n"
+ "Session: 2547019973447939919\r\n"
+ "Content-Type: text/parameters\r\n"
+ "Content-Length: 50\r\n"
+ "CSeq: 3\r\n"
+ "\r\n"
+ "position: 24\r\n"
+ "stream_state: playing\r\n"
+ "scale: 1.00\r\n";
byte[] content = ("position: 24\r\n"
+ "stream_state: playing\r\n"
+ "scale: 1.00\r\n").getBytes(CharsetUtil.UTF_8);
FullHttpResponse response =
new DefaultFullHttpResponse(RtspVersions.RTSP_1_0,
RtspResponseStatuses.OK);
response.headers().add(RtspHeaders.Names.SERVER, "Testserver");
response.headers().add(RtspHeaders.Names.SESSION, "2547019973447939919");
response.headers().add(RtspHeaders.Names.CONTENT_TYPE,
"text/parameters");
response.headers().add(RtspHeaders.Names.CONTENT_LENGTH,
"" + content.length);
response.headers().add(RtspHeaders.Names.CSEQ, "3");
response.content().writeBytes(content);
EmbeddedChannel ch = new EmbeddedChannel(new RtspEncoder());
ch.writeOutbound(response);
ByteBuf buf = (ByteBuf) ch.readOutbound();
String actual = buf.toString(CharsetUtil.UTF_8);
buf.release();
assertEquals(expected, actual);
}
}