我正在尝试使用Netty编写RTSP服务器。
现在客户端发送请求
OPTIONS rtsp://localhost:8080 RTSP/1.0
CSeq: 2
User-Agent: LibVLC/2.2.4 (LIVE555 Streaming Media v2016.02.22)
我想发回以下回复
RTSP/1.0 200 OK
CSeq: 2
Public: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE
我应该使用什么来构建http响应。我应该使用HttpResponse还是只使用普通字节数组并将其转换为ByteBuf?
我使用的Netty版本是4.1.5
提前致谢。
答案 0 :(得分:1)
OPTIONS请求的RTSP响应仅包含标题。
然后你可以简单地创建响应并使用:
填充它FullHttpResponse response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.OK);
response.headers().add(RtspHeadersNames.PUBLIC, "DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
response.headers().add(RtspHeadersNames.CSEQ, cseq);
回答OPTIONS请求的RTSP服务器的简化实现可能是:
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.rtsp.*;
public class RtspServer {
public static class RtspServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof DefaultHttpRequest) {
DefaultHttpRequest req = (DefaultHttpRequest) msg;
FullHttpResponse response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.OK);
response.headers().add(RtspHeadersNames.PUBLIC, "DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
response.headers().add(RtspHeadersNames.CSEQ, req.headers().get("CSEQ"));
response.headers().set(RtspHeadersNames.CONNECTION, RtspHeadersValues.KEEP_ALIVE);
ctx.write(response);
}
}
}
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);
b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
p.addLast(new RtspDecoder(), new RtspEncoder());
p.addLast(new RtspServerHandler());
}
});
Channel ch = b.bind(8554).sync().channel();
System.err.println("Connect to rtsp://127.0.0.1:8554");
ch.closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
答案 1 :(得分:0)
您希望FullHttpResponse
与管道中的Rtsp处理程序一起使用。