RtspRequest.java
4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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();
}
}