2016-10-12 20 views
1

Nettyを使用してRTSPサーバーを作成しようとしています。Nettyを使用してhttp応答を送信する

は今、クライアントが要求

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に変換する必要がありますか?

私が使用しているネッティーバージョンは、事前に4.1.5

おかげです。

答えて

1

OPTIONS要求のRTSP応答にはヘッダーのみが含まれます。

その後、あなたは、単にREPONSEを作成し、使用してそれを埋めることができます。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); 

簡略化の実装は次のようになります。

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(); 
     }  
    } 
} 
0

パイプラインのRtspハンドラでFullHttpResponseを使用します。

関連する問題