我是 netty 的初学者,我需要一种方法来一起处理 RTSP 和 RTP,如 ED-137_4B(嵌入式交错二进制数据)的规范中所述。 我目前使用的是 netty 框架版本 4.0.24,但在 4.0.34 等更高版本中找不到对嵌入式交错二进制数据的 RTSP 支持,其中 RTP 在 RTSP 包中交错。 我尝试了很多东西,例如我尝试用 RTPServerInitilizer 实现第二个 childHandler,它应该处理 RTP 解码,但这也不能与 RTSP 解码器一起工作。 我也尝试过实现两条管道(见下文),但这也没有一起工作。
总而言之,当我实现一个 RTSP 解码器和 RTP 解码器时,例如使用完全独立的 RTSP 和 RTP 初始化器,或者我用两个单独的管道实现时,总是只有一个解码器工作。例如,如果 RTSP 设置在第一位,则只有 RTSP 事件将被解码,如果 RTP 解码器位于第一位,则只有 RTP 包将被解码。 如果客户端断开连接,那么我可以看到另一个解码器将尝试像队列一样解码,但是来自客户端的消息不再可用。有可能通过与“NioServerSocketChannel.class”同步的同一个 TCP 套接字处理许多不同的 RTSP 客户端吗?
拜托,你能给我一个提示,或者你能帮我解决如何一起使用 RTSP 和 RTP,因为我在新工作的项目中需要它,它是迫切的?
提前致谢:-)
这是我的代码片段:
//***-->For your interest, functions that begin with HttpServer... handle the RTSP events!<--**//
public class HttpServerInitializer extends ChannelInitializer<SocketChannel>{
@Override
public void initChannel(SocketChannel ch) {
//Handling of RTSP
ChannelPipeline p = ch.pipeline();
p.addLast(new RtspResponseEncoder());
p.addFirst(new RtspRequestDecoder());
p.addLast(new HttpObjectAggregator(65536));
p.addLast(new HttpContentCompressor());
p.addLast(new ChunkedWriteHandler());
p.addLast(new HttpServerHandler());
//Second pipeline for handling of RTP
ChannelPipeline pp = ch.pipeline();
pp.addLast(new RTPDecoder());
pp.addLast(new RTPHandler());
}
}
public class HttpServer {
//static final int PORT = 554;
public Channel ch = null;
public void connect(int iPort) throws Exception {
// Configure the server.
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
//try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
//.childHandler(new RtpServerInitializer()) //Second separately initializer did not worked together with initializer for RTSP
//.handler(new LoggingHandler(LogLevel.ERROR))
.childHandler(new HttpServerInitializer());
ch = b.bind(iPort).sync().channel();
/* ch.closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}*/
}
}
public class HttpServerHandler extends SimpleChannelInboundHandler <HttpObject> {
private HttpRequest m_request;
/** Buffer that stores the response content for only log output**/
private final StringBuilder buf = new StringBuilder();
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
// TODO Auto-generated method stub
if (msg instanceof HttpRequest) {
m_request = (HttpRequest) msg;
....
}
...
}
}
public class RTPDecoder extends ByteToMessageDecoder {
private ByteBuf collector = Unpooled.buffer();
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
throws Exception {
…
}
...
}
public class RTPHandler extends SimpleChannelInboundHandler<ByteToMessageDecoder> {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) throws Exception {
super.exceptionCaught(ctx, e);
}
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
// TODO Auto-generated method stub
…
}
…
}
最佳答案
我遇到了和你一样的问题(几年后),在相当长的一段时间后,我注意到你只需要一个 ChannelPipeline 来处理 RTSP 和 RTP 数据包。请记住,添加到 ChannelPipe 的解码顺序至关重要。您必须先添加 RTP 解码器,然后再添加 RTSP 解码器/编码器。非 RTP 交错的数据包应通过 fireChannelRead 转发到下一个解码器(在本例中为 RTSP 解码器) ,你可以根据RFC 2326 10.12通过ASCII中的“$”符号来区分RTP交错数据包。 .当添加到 ChannelPipeline 时,同样的顺序应该适用于处理程序。
关于java - Netty服务端RTSP交错RTP(解码处理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35749293/