RTSPHandler.java
6.72 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package cn.org.hentai.jtt1078.server;
import cn.org.hentai.jtt1078.rtsp.RtspRequest;
import cn.org.hentai.jtt1078.rtsp.RtspSessionManager;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.rtsp.*;
import io.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
import java.util.*;
public class RTSPHandler extends ChannelInboundHandlerAdapter {
private final static Logger log = LoggerFactory.getLogger(RTSPHandler.class);
private String channelId = "138000099998-1";
private HttpMethod currentMethod = RtspMethods.OPTIONS;
private List<HttpMethod> methods = Arrays.asList(RtspMethods.OPTIONS, RtspMethods.ANNOUNCE, RtspMethods.SETUP, RtspMethods.RECORD, null);
private RtspRequest rtspRequest = new RtspRequest("192.168.169.100", 9555, String.format("/schedule/%s?sign=41db35390ddad33f83944f44b8b75ded", channelId));
/*
When notified that the channel is active, sends a message. A channel is active
when a connection has been established, so the method is invoked when the connections
is established.
*/
@Override
public void channelActive(final ChannelHandlerContext ctx) {
log.debug("channelActive, connection established: {}", ctx);
log.debug("Sending request to the server");
ctx.writeAndFlush(rtspRequest.option());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
log.info("exceptionCaught: {}", cause);
ctx.close();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
log.info("Received from RTSP Server: {}", ctx);
log.info("Received from RTSP msg: {}", msg.toString());
log.debug("Received Class Type: {}", msg.getClass().getTypeName());
if (msg instanceof DefaultHttpResponse) {
DefaultHttpResponse res = (DefaultHttpResponse) msg;
if (RtspResponseStatuses.OK.equals(res.status())) {
log.debug("{}: {}", currentMethod, res.status());
if (res.headers().contains(RtspHeaderNames.SESSION)) {
rtspRequest.setSessionID(res.headers().get(RtspHeaderNames.SESSION));
}
nextMethod(ctx);
} else {
ctx.close();
}
} else if (msg instanceof DefaultHttpContent) {
DefaultHttpContent content = (DefaultHttpContent) msg;
log.info("Content: {}", content);
ByteBuf byteBuf = content.content();
} else {
log.debug("dataType error: {}", msg.getClass().getTypeName());
}
}
private void nextMethod(ChannelHandlerContext ctx) {
for (int i = 0;i < methods.size(); i++) {
if (methods.get(i).equals(currentMethod) && i < methods.size() - 1) {
currentMethod = methods.get(i + 1);
break;
}
}
if (currentMethod == null) {
RtspSessionManager.register(channelId);
return;
}
if (currentMethod == RtspMethods.ANNOUNCE) {
ctx.writeAndFlush(rtspRequest.announce());
}
if (currentMethod == RtspMethods.SETUP) {
ctx.writeAndFlush(rtspRequest.setup());
}
if (currentMethod == RtspMethods.RECORD) {
ctx.writeAndFlush(rtspRequest.record());
}
}
private void parseSdp(String sdp) {
log.debug("Parsing SDP: {}", sdp);
Map<String, List<String>> mediaMap = new HashMap<>(10);
String[] array = sdp.split("\\n");
String mediaName = "";
for (int i = 0; i < array.length; i++) {
String line = array[i];
if (line.startsWith("m=")) {
mediaName = line.substring(line.indexOf("=") + 1, line.indexOf(" "));
if (mediaName.equals("video") || mediaName.equals("audio")) {
mediaMap.put(mediaName, new ArrayList<>());
} else {
mediaName = "";
}
} else if (!StringUtil.isNullOrEmpty(mediaName)) {
if (line.startsWith("b=") || line.startsWith("a=")) {
List<String> medialist = mediaMap.get(mediaName);
medialist.add(line);
}
}
}
for (String mediaKey : mediaMap.keySet()) {
StringBuilder stringBuilder = new StringBuilder();
List<String> mediaInfo = mediaMap.get(mediaKey);
mediaInfo.forEach((s) -> {
stringBuilder.append("\n");
stringBuilder.append(s);
});
log.info("[>>>>> {} <<<<<] {}", mediaKey, stringBuilder.toString());
}
}
public static void main(String[] args) {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap clientBootstrap = new Bootstrap();
clientBootstrap.group(group)
.option(ChannelOption.TCP_NODELAY, true)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress("192.168.169.100", 9555))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) {
socketChannel.pipeline()
.addLast(new RtspEncoder())
.addLast(new RtspDecoder())
.addLast(new RTSPHandler());
}
});
ChannelFuture f = null;
while (true) {
log.info("Waiting for server connection");
f = clientBootstrap.connect();
f.awaitUninterruptibly();
if (f.isSuccess()) {
log.info("RTSP Connection success!");
break;
}
Thread.sleep(1000);
}
// Wait for the server to close the connection.
f.channel().closeFuture().sync();
} catch (Exception e) {
log.error("Error ->", e);
log.error("<- Error");
} finally {
// Shut down executor threads to exit.Ahkk
try {
log.info("ShutdownGracefully the connection group");
group.shutdownGracefully().sync();
} catch (InterruptedException e) {
log.error("", e);
}
}
}
}