KenKenKen
KenKenKen

Reputation: 467

Send http response using Netty

I'm trying to write a RTSP server using Netty.

Now the client sends the request

OPTIONS rtsp://localhost:8080 RTSP/1.0
CSeq: 2
User-Agent: LibVLC/2.2.4 (LIVE555 Streaming Media v2016.02.22)

And I want to send the following response back

RTSP/1.0 200 OK
CSeq: 2
Public: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE

What should I use to construct the http response. Should I use HttpResponse or just use a plain byte array and convert it to ByteBuf?

The Netty version I'm using is 4.1.5

Thanks in advance.

Upvotes: 1

Views: 2398

Answers (2)

mpromonet
mpromonet

Reputation: 11942

The RTSP response of OPTIONS request contains only headers.

Then you can simply create the reponse and fill it using :

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);

A simplified implementation of an RTSP server answering to OPTIONS request could be :

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

Upvotes: 3

Norman Maurer
Norman Maurer

Reputation: 23557

You want to use FullHttpResponse with the Rtsp handlers in the pipeline.

Upvotes: 0

Related Questions