RtspRequest.java 4.34 KB
package cn.org.hentai.jtt1078.rtsp;

import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.rtsp.RtspHeaderNames;
import io.netty.handler.codec.rtsp.RtspMethods;
import io.netty.handler.codec.rtsp.RtspVersions;
import io.netty.util.internal.StringUtil;

/**
 * Rtsp请求
 */
public class RtspRequest {

    private final String CRLF = "\r\n";

    private final String VERSION = "RTSP/1.0";

    private int seq = 0;

    private String host;

    private int port;

    private String path;

    private String sessionID;

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

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getSessionID() {
        return sessionID;
    }

    public void setSessionID(String sessionID) {
        this.sessionID = sessionID;
    }

    public FullHttpRequest option() {
        FullHttpRequest request = new DefaultFullHttpRequest(RtspVersions.RTSP_1_0, RtspMethods.OPTIONS, String.format("rtsp://%s:%d%s", host, port, path));
        request.headers().set(RtspHeaderNames.CSEQ, ++seq);

        return request;
    }

    public FullHttpRequest announce() {
        StringBuilder body = new StringBuilder();
        FullHttpRequest request = new DefaultFullHttpRequest(RtspVersions.RTSP_1_0, RtspMethods.ANNOUNCE, String.format("rtsp://%s:%d%s", host, port, path));
        request.headers().set(RtspHeaderNames.CSEQ, ++seq);
        request.headers().set(RtspHeaderNames.CONTENT_TYPE, "application/sdp");

        body.append("v=0").append(CRLF)
                .append("o=- 0 0 IN IP4 127.0.0.1").append(CRLF)
                .append("s=No Name").append(CRLF)
                .append("c=IN IP4 ").append(this.host).append(CRLF)
                .append("t=0 0").append(CRLF)
                .append("a=tool:libavformat 61.9.100").append(CRLF)
                .append("m=video 0 RTP/AVP 96").append(CRLF)
                .append("b=AS:3943").append(CRLF)
                .append("a=rtpmap:96 H264/90000").append(CRLF)
                .append("a=fmtp:96 packetization-mode=1; sprop-parameter-sets=Z00AKp2oHgCJ+WbgICAoAAADAAgAAAMBlCA=,aO48gA==; profile-level-id=4D002A").append(CRLF)
                .append("a=control:streamid=0").append(CRLF);
        request.content().writeBytes(body.toString().getBytes());
        request.headers().set(RtspHeaderNames.CONTENT_LENGTH, body.toString().getBytes().length);

        return request;
    }

    public FullHttpRequest setup() {
        FullHttpRequest request = new DefaultFullHttpRequest(RtspVersions.RTSP_1_0, RtspMethods.SETUP, String.format("rtsp://%s:%d%s/streamid=0", host, port, path));
        request.headers().set(RtspHeaderNames.CSEQ, ++seq);
        request.headers().set(RtspHeaderNames.TRANSPORT, "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record");
        request.headers().set(RtspHeaderNames.SESSION, sessionID);

        return request;
    }

    public FullHttpRequest record() {
        FullHttpRequest request = new DefaultFullHttpRequest(RtspVersions.RTSP_1_0, RtspMethods.RECORD, String.format("rtsp://%s:%d%s/streamid=0", host, port, path));
        request.headers().set(RtspHeaderNames.CSEQ, ++seq);
        request.headers().set(RtspHeaderNames.RANGE, "npt=0.000-");
        request.headers().set(RtspHeaderNames.SESSION, sessionID);

        return request;
    }

    public String teardown() {
        StringBuilder sb = new StringBuilder();

        return sb.toString();
    }

    private String header(int length) {
        StringBuilder sb = new StringBuilder();

        return sb.append("CSeq: ").append(++seq).append(CRLF)
                .append("User-Agent: jt1078").append(CRLF)
                .append("Content-Length: ").append(length).append(CRLF)
                .append(StringUtil.isNullOrEmpty(sessionID) ? "" : "Session: ").append(StringUtil.isNullOrEmpty(sessionID) ? "" : sessionID).append(StringUtil.isNullOrEmpty(sessionID) ? "" : CRLF).append(CRLF).toString();
    }
}