Jtt1078Handler.java
8.33 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.genersoft.iot.vmp.jt1078.server;
import com.genersoft.iot.vmp.VManageBootstrap;
import com.genersoft.iot.vmp.conf.MediaConfig;
import com.genersoft.iot.vmp.jt1078.app.VideoServerApp;
import com.genersoft.iot.vmp.jt1078.publisher.Channel;
import com.genersoft.iot.vmp.jt1078.publisher.PublishManager;
import com.genersoft.iot.vmp.jt1078.util.Packet;
import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem;
import com.genersoft.iot.vmp.service.IStreamProxyService;
import com.genersoft.iot.vmp.utils.SpringBeanFactory;
import com.genersoft.iot.vmp.vmanager.jt1078.platform.config.Jt1078ConfigBean;
import com.genersoft.iot.vmp.vmanager.streamProxy.StreamProxyController;
import io.lettuce.core.support.caching.RedisCache;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
public class Jtt1078Handler extends SimpleChannelInboundHandler<Packet> {
static Logger logger = LoggerFactory.getLogger(Jtt1078Handler.class);
private static final AttributeKey<Session> SESSION_KEY = AttributeKey.valueOf("session-key");
private ChannelHandlerContext context;
private String tagMapping;
private Integer httpPort;
public Jtt1078Handler(String tagMapping, Integer httpPort) {
this.tagMapping = tagMapping;
this.httpPort = httpPort;
}
protected void channelRead0(ChannelHandlerContext ctx, Packet packet) throws Exception {
this.context = ctx;
packet.seek(8);
String sim = packet.nextBCD() + packet.nextBCD() + packet.nextBCD() + packet.nextBCD() + packet.nextBCD() + packet.nextBCD();
int channel = packet.nextByte() & 255;
String tag = sim + "-" + channel;
if (StringUtils.isEmpty(this.tagMapping)) {
this.tagMapping = tag;
}
if (!StringUtils.endsWith(this.tagMapping, ".flv")) {
endWithMapping(this.tagMapping);
}
Session session = this.getSession();
if (null == session) {
this.setSession(session = new Session());
Channel chl = PublishManager.getInstance().open(this.tagMapping, this.httpPort);
session.set("tag", this.tagMapping);
logger.info("start publishing: {} -> {}-{}", new Object[]{Long.toHexString((long) chl.hashCode() & 4294967295L), sim, channel});
}
Integer sequence = (Integer) session.get("video-sequence");
if (sequence == null) {
sequence = 0;
}
int lengthOffset = 28;
int dataType = packet.seek(15).nextByte() >> 4 & 15;
int pkType = packet.seek(15).nextByte() & 15;
if (dataType == 4) {
lengthOffset = 16;
} else if (dataType == 3) {
lengthOffset = 24;
}
int pt = packet.seek(5).nextByte() & 127;
long timestamp;
if (dataType != 0 && dataType != 1 && dataType != 2) {
if (dataType == 3) {
timestamp = packet.seek(16).nextLong();
byte[] data = packet.seek(lengthOffset + 2).nextBytes();
PublishManager.getInstance().publishVideo(this.tagMapping, sequence, timestamp, pt, data);
}
} else {
if (pkType == 0 || pkType == 2) {
sequence = sequence + 1;
session.set("video-sequence", sequence);
}
timestamp = packet.seek(16).nextLong();
PublishManager.getInstance().publishVideo(this.tagMapping, sequence, timestamp, pt, packet.seek(lengthOffset + 2).nextBytes());
}
}
private byte[] convert(String str) {
byte[] bytes = str.getBytes();
new StringBuilder();
int length = bytes.length;
byte[] bys = new byte[length];
for (int i = 0; i < length; ++i) {
bys[i] = Byte.valueOf(Integer.toBinaryString(bytes[i]) + "");
}
return bys;
}
public final Session getSession() {
Attribute<Session> attr = this.context.channel().attr(SESSION_KEY);
return null == attr ? null : (Session) attr.get();
}
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
this.release();
}
public final void setSession(Session session) {
this.context.channel().attr(SESSION_KEY).set(session);
}
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
this.release();
ctx.close();
}
private void release() {
String tag = (String) this.getSession().get("tag");
if (tag != null) {
logger.info("close netty channel: {}", tag);
PublishManager.getInstance().close(tag);
}
}
public static void createStreamProxy(String stream, Integer port) throws URISyntaxException, IOException {
Jt1078ConfigBean jt1078ConfigBean = VManageBootstrap.getBean(Jt1078ConfigBean.class);
MediaConfig mediaConfig = VManageBootstrap.getBean(MediaConfig.class);
StreamProxyController streamProxyController = VManageBootstrap.getBean(StreamProxyController.class);
if (StringUtils.endsWith(stream, ".flv")) {
stream = StringUtils.substringBeforeLast(stream, ".flv");
}
if(Objects.isNull(jt1078ConfigBean)){
return;
}
String url = StringUtils.replace(jt1078ConfigBean.getGetURL(), "{stream}", stream);
url = StringUtils.replace(url, "{port}", port + "");
if (!StringUtils.endsWith(url, ".flv")) {
url = url + ".flv";
}
StreamProxyItem item = new StreamProxyItem();
item.setApp("schedule");
item.setEnable(true);
item.setEnableAudio(true);
item.setRtpType("default");
item.setStream(stream);
item.setMediaServerId(mediaConfig.getId());
item.setUrl(url);
item.setFfmpegCmdKey("ffmpeg.cmd");
item.setEnable(true);
item.setEnableAudio(true);
item.setEnableMp4(false);
item.setEnableRemoveNoneReader(false);
item.setEnableDisableNoneReader(false);
item.setName(stream);
StringRedisTemplate redisTemplate = VManageBootstrap.getBean(StringRedisTemplate.class);
String key = "jtt1078:" + stream;
String closeKey = "jt1078:count:"+stream;
String timeoutKey = "timeout:"+stream;
Object timeOutVal = redisTemplate.opsForValue().get(timeoutKey);
if(Objects.equals("2",timeOutVal)){
IStreamProxyService streamProxyService = VManageBootstrap.getBean(IStreamProxyService.class);
streamProxyService.stop1("schedule",stream);
redisTemplate.delete(timeoutKey);
}
if(redisTemplate.hasKey(closeKey)){
IStreamProxyService streamProxyService = VManageBootstrap.getBean(IStreamProxyService.class);
streamProxyService.del("schedule",stream);
}else if (redisTemplate.hasKey(key)) {
redisTemplate.opsForValue().set(key, "1", 300, TimeUnit.SECONDS);
} else {
try {
streamProxyController.save(item);
}catch (Exception e){
logger.error(e.getMessage());
}
timeOutVal = redisTemplate.opsForValue().get(timeoutKey);
if(Objects.equals("2",timeOutVal)) {
IStreamProxyService streamProxyService = VManageBootstrap.getBean(IStreamProxyService.class);
streamProxyService.stop1("schedule", stream);
redisTemplate.delete(timeoutKey);
}
}
}
private synchronized String endWithMapping(String tagMapping) throws URISyntaxException, IOException {
if (!StringUtils.endsWith(this.tagMapping, ".flv")) {
this.tagMapping = this.tagMapping + ".flv";
}
return this.tagMapping;
}
}