Netty RtspEncoder / Decoder问题

时间:2014-03-25 13:57:26

标签: java netty rtsp

为了开发RtspClient(只是消息交易,而不是播放视频),我想我会使用Netty,并且我已经创建了以下类,(rtsp给我带来了太多问题,我不知道,这是因为知识滞后吗?任何方式......),

TestRtspClient.java

    public class TestRtspClient {

    private final String host;
    private final int port;

    public TestRtspClient(String host, int port) {
        this.host = host;
        this.port = port;       
    }

    public void start() throws Exception {

        EventLoopGroup group = new NioEventLoopGroup();

        try {

            Bootstrap bootstrap = new Bootstrap();

            bootstrap.group(group);
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.handler(new TestRtspClientInitializer());

            Channel channel = bootstrap.connect(host, port).sync().channel();

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            //DESCRIBE rtsp://192.168.1.26:8554/nature.ts RTSP/1.0\r\nCSeq: 3\r\n\r\n
            while(true) {
                channel.write(br.readLine() + "\r\n");
                channel.flush();
            }

        } finally {
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new TestRtspClient("192.168.1.26", 8554).start();
    }   
}

这里是TestRtspClientInitializer.java

    public class TestRtspClientInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {

        ChannelPipeline pipe = ch.pipeline();

//      pipe.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
//      pipe.addLast("decoder", new StringDecoder());
//      pipe.addLast("encoder", new StringEncoder());

//      pipe.addLast("encoder", new RtspRequestEncoder());
//      pipe.addLast("decoder", new RtspResponseDecoder());

        pipe.addLast("encoder", new HttpRequestEncoder());
        pipe.addLast("decoder", new HttpResponseDecoder());

        pipe.addLast("handler", new TestRtspClientHandler());
    }
}

这里是TestRtspClientHandler.java

public class TestRtspClientHandler extends ChannelInboundMessageHandlerAdapter<HttpObject> {

      @Override
      public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
              if(msg instanceof HttpResponse) {
                 System.out.println("Rtsp Response"); 
              } else if(msg instanceof HttpRequest) {
                 System.out.println("Rtsp Request"); 
              } else {
                 System.err.println("not supported format");
              }
       }

}

我正在使用Live555MediaServer作为RtspServer和Netty 4.0.0.CR3。当我使用DelimiterBasedFrameDecoder与stringdecoder和编码器工作正常,但如果我使用RtspRequest / Response编码器/解码器我得到以下警告,并且没有msg将被发送到L555。(也与HttpReq / Resp编码器和解码器相同)< / p>

在eclipse中将其作为命令行arg传递

  

DESCRIBE rtsp://192.168.1.26:8554 / nature.ts RTSP / 1.0 \ r \ nCSeq:3 \ r \ n

     

2014年3月25日下午6:45:28   io.netty.channel.DefaultChannelPipeline $ ByteHeadHandler flush警告:   丢弃1个到达头部的出站消息   管道。请检查您的管道配置。

帮我解决这个问题,并简要解释一下这个代码模块中的错误。

谢谢。

1 个答案:

答案 0 :(得分:0)

首先,请升级到最新的Netty 4.0.x版本。

由于您在<String>中扩展ChannelInboundMessageHandlerAdapter时指定了TestRtspClientHandler,因此只会收到类型为String的邮件,但情况并非如此。您必须使用HttpObject代替String