Commit 988dc36fa56a47cc4f331ab48c07577805a71425

Authored by 648540858
1 parent ba884fa9

重构点播,回放,下载时ssrc不一致以及TCP主动播放的逻辑

src/main/java/com/genersoft/iot/vmp/conf/DynamicTask.java
... ... @@ -46,6 +46,9 @@ public class DynamicTask {
46 46 * @return
47 47 */
48 48 public void startCron(String key, Runnable task, int cycleForCatalog) {
  49 + if(ObjectUtils.isEmpty(key)) {
  50 + return;
  51 + }
49 52 ScheduledFuture<?> future = futureMap.get(key);
50 53 if (future != null) {
51 54 if (future.isCancelled()) {
... ... @@ -74,6 +77,9 @@ public class DynamicTask {
74 77 * @return
75 78 */
76 79 public void startDelay(String key, Runnable task, int delay) {
  80 + if(ObjectUtils.isEmpty(key)) {
  81 + return;
  82 + }
77 83 stop(key);
78 84  
79 85 // 获取执行的时刻
... ... @@ -100,9 +106,12 @@ public class DynamicTask {
100 106 }
101 107  
102 108 public boolean stop(String key) {
  109 + if(ObjectUtils.isEmpty(key)) {
  110 + return false;
  111 + }
103 112 boolean result = false;
104 113 if (!ObjectUtils.isEmpty(futureMap.get(key)) && !futureMap.get(key).isCancelled() && !futureMap.get(key).isDone()) {
105   - result = futureMap.get(key).cancel(false);
  114 + result = futureMap.get(key).cancel(true);
106 115 futureMap.remove(key);
107 116 runnableMap.remove(key);
108 117 }
... ... @@ -110,6 +119,9 @@ public class DynamicTask {
110 119 }
111 120  
112 121 public boolean contains(String key) {
  122 + if(ObjectUtils.isEmpty(key)) {
  123 + return false;
  124 + }
113 125 return futureMap.get(key) != null;
114 126 }
115 127  
... ... @@ -118,6 +130,9 @@ public class DynamicTask {
118 130 }
119 131  
120 132 public Runnable get(String key) {
  133 + if(ObjectUtils.isEmpty(key)) {
  134 + return null;
  135 + }
121 136 return runnableMap.get(key);
122 137 }
123 138  
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/ByeRequestProcessor.java
... ... @@ -138,7 +138,7 @@ public class ByeRequestProcessor extends SIPRequestProcessorParent implements In
138 138 logger.info("[收到bye] {} 通知设备停止推流时未找到设备信息", streamId);
139 139 }
140 140 try {
141   - logger.warn("[停止点播] {}/{}", sendRtpItem.getDeviceId(), sendRtpItem.getChannelId());
  141 + logger.info("[停止点播] {}/{}", sendRtpItem.getDeviceId(), sendRtpItem.getChannelId());
142 142 cmder.streamByeCmd(device, sendRtpItem.getChannelId(), streamId, null);
143 143 } catch (InvalidArgumentException | ParseException | SipException |
144 144 SsrcTransactionNotFoundException e) {
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java
... ... @@ -33,7 +33,7 @@ import java.text.ParseException;
33 33 public class KeepaliveNotifyMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler {
34 34  
35 35  
36   - private Logger logger = LoggerFactory.getLogger(KeepaliveNotifyMessageHandler.class);
  36 + private final Logger logger = LoggerFactory.getLogger(KeepaliveNotifyMessageHandler.class);
37 37 private final static String cmdType = "Keepalive";
38 38  
39 39 @Autowired
... ... @@ -59,14 +59,19 @@ public class KeepaliveNotifyMessageHandler extends SIPRequestProcessorParent imp
59 59 // 未注册的设备不做处理
60 60 return;
61 61 }
62   - logger.info("[收到心跳], device: {}", device.getDeviceId());
63 62 SIPRequest request = (SIPRequest) evt.getRequest();
  63 + logger.info("[收到心跳], device: {}, callId: {}", device.getDeviceId(), request.getCallIdHeader().getCallId());
  64 +
64 65 // 回复200 OK
65 66 try {
66 67 responseAck(request, Response.OK);
67 68 } catch (SipException | InvalidArgumentException | ParseException e) {
68 69 logger.error("[命令发送失败] 心跳回复: {}", e.getMessage());
69 70 }
  71 + if (DateUtil.getDifferenceForNow(device.getKeepaliveTime()) <= 3000L){
  72 + logger.info("[收到心跳] 心跳发送过于频繁,已忽略 device: {}, callId: {}", device.getDeviceId(), request.getCallIdHeader().getCallId());
  73 + return;
  74 + }
70 75  
71 76 RemoteAddressInfo remoteAddressInfo = SipUtils.getRemoteAddressFromRequest(request, userSetting.getSipUseSourceIpAsRemoteAddress());
72 77 if (!device.getIp().equalsIgnoreCase(remoteAddressInfo.getIp()) || device.getPort() != remoteAddressInfo.getPort()) {
... ... @@ -80,7 +85,7 @@ public class KeepaliveNotifyMessageHandler extends SIPRequestProcessorParent imp
80 85 }else {
81 86 long lastTime = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(device.getKeepaliveTime());
82 87 if (System.currentTimeMillis()/1000-lastTime > 10) {
83   - device.setKeepaliveIntervalTime(new Long(System.currentTimeMillis()/1000-lastTime).intValue());
  88 + device.setKeepaliveIntervalTime(Long.valueOf(System.currentTimeMillis()/1000-lastTime).intValue());
84 89 }
85 90 }
86 91  
... ...
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java
... ... @@ -494,6 +494,7 @@ public class ZLMHttpHookListener {
494 494 Device device = deviceService.getDevice(inviteInfo.getDeviceId());
495 495 if (device != null) {
496 496 try {
  497 + // 多查询一次防止已经被处理了
497 498 InviteInfo info = inviteStreamService.getInviteInfo(inviteInfo.getType(),
498 499 inviteInfo.getDeviceId(), inviteInfo.getChannelId(), inviteInfo.getStream());
499 500 if (info != null) {
... ...
src/main/java/com/genersoft/iot/vmp/service/impl/InviteStreamServiceImpl.java
... ... @@ -98,6 +98,9 @@ public class InviteStreamServiceImpl implements IInviteStreamService {
98 98 "_" + inviteInfo.getChannelId() +
99 99 "_" + stream;
100 100 inviteInfoInDb.setStream(stream);
  101 + if (inviteInfoInDb.getSsrcInfo() != null) {
  102 + inviteInfoInDb.getSsrcInfo().setStream(stream);
  103 + }
101 104 redisTemplate.opsForValue().set(key, inviteInfoInDb);
102 105 return inviteInfoInDb;
103 106 }
... ...
src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java
... ... @@ -151,9 +151,14 @@ public class MediaServerServiceImpl implements IMediaServerService {
151 151 if (streamId == null) {
152 152 streamId = String.format("%08x", Integer.parseInt(ssrc)).toUpperCase();
153 153 }
  154 + int ssrcCheckParam = 0;
  155 + if (ssrcCheck && tcpMode > 1) {
  156 + // 目前zlm不支持 tcp模式更新ssrc,暂时关闭ssrc校验
  157 + logger.warn("[openRTPServer] TCP被动/TCP主动收流时,默认关闭ssrc检验");
  158 + }
154 159 int rtpServerPort;
155 160 if (mediaServerItem.isRtpEnable()) {
156   - rtpServerPort = zlmServerFactory.createRTPServer(mediaServerItem, streamId, ssrcCheck?Integer.parseInt(ssrc):0, port, reUsePort, tcpMode);
  161 + rtpServerPort = zlmServerFactory.createRTPServer(mediaServerItem, streamId, (ssrcCheck && tcpMode == 0)?Integer.parseInt(ssrc):0, port, reUsePort, tcpMode);
157 162 } else {
158 163 rtpServerPort = mediaServerItem.getRtpProxyPort();
159 164 }
... ...
src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
... ... @@ -17,6 +17,7 @@ import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
17 17 import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
18 18 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
19 19 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform;
  20 +import com.genersoft.iot.vmp.gb28181.utils.SipUtils;
20 21 import com.genersoft.iot.vmp.media.zlm.AssistRESTfulUtils;
21 22 import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
22 23 import com.genersoft.iot.vmp.media.zlm.ZLMServerFactory;
... ... @@ -34,6 +35,7 @@ import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
34 35 import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
35 36 import com.genersoft.iot.vmp.utils.DateUtil;
36 37 import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  38 +import gov.nist.javax.sip.message.SIPResponse;
37 39 import org.slf4j.Logger;
38 40 import org.slf4j.LoggerFactory;
39 41 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -95,7 +97,6 @@ public class PlayServiceImpl implements IPlayService {
95 97 @Autowired
96 98 private VideoStreamSessionManager streamSession;
97 99  
98   -
99 100 @Autowired
100 101 private IDeviceService deviceService;
101 102  
... ... @@ -108,25 +109,25 @@ public class PlayServiceImpl implements IPlayService {
108 109 @Autowired
109 110 private ZlmHttpHookSubscribe subscribe;
110 111  
111   - @Autowired
112   - private SSRCFactory ssrcFactory;
113   -
114   - @Autowired
115   - private RedisTemplate<Object, Object> redisTemplate;
116   -
117 112  
118 113 @Override
119 114 public SSRCInfo play(MediaServerItem mediaServerItem, String deviceId, String channelId, String ssrc, ErrorCallback<Object> callback) {
120 115 if (mediaServerItem == null) {
  116 + logger.warn("[点播] 未找到可用的zlm deviceId: {},channelId:{}", deviceId, channelId);
121 117 throw new ControllerException(ErrorCode.ERROR100.getCode(), "未找到可用的zlm");
122 118 }
123 119  
124 120 Device device = redisCatchStorage.getDevice(deviceId);
  121 + if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE") && !mediaServerItem.isRtpEnable()) {
  122 + logger.warn("[点播] 单端口收流时不支持TCP主动方式收流 deviceId: {},channelId:{}", deviceId, channelId);
  123 + throw new ControllerException(ErrorCode.ERROR100.getCode(), "单端口收流时不支持TCP主动方式收流");
  124 + }
125 125 InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channelId);
126 126 if (inviteInfo != null ) {
127 127 if (inviteInfo.getStreamInfo() == null) {
128 128 // 点播发起了但是尚未成功, 仅注册回调等待结果即可
129 129 inviteStreamService.once(InviteSessionType.PLAY, deviceId, channelId, null, callback);
  130 + logger.info("[点播开始] 已经请求中,等待结果, deviceId: {}, channelId: {}", device.getDeviceId(), channelId);
130 131 return inviteInfo.getSsrcInfo();
131 132 }else {
132 133 StreamInfo streamInfo = inviteInfo.getStreamInfo();
... ... @@ -149,6 +150,7 @@ public class PlayServiceImpl implements IPlayService {
149 150 InviteErrorCode.SUCCESS.getCode(),
150 151 InviteErrorCode.SUCCESS.getMsg(),
151 152 streamInfo);
  153 + logger.info("[点播已存在] 直接返回, deviceId: {}, channelId: {}", device.getDeviceId(), channelId);
152 154 return inviteInfo.getSsrcInfo();
153 155 }else {
154 156 // 点播发起了但是尚未成功, 仅注册回调等待结果即可
... ... @@ -171,7 +173,6 @@ public class PlayServiceImpl implements IPlayService {
171 173 null);
172 174 return null;
173 175 }
174   - // TODO 记录点播的状态
175 176 play(mediaServerItem, ssrcInfo, device, channelId, callback);
176 177 return ssrcInfo;
177 178 }
... ... @@ -187,8 +188,8 @@ public class PlayServiceImpl implements IPlayService {
187 188 null);
188 189 return;
189 190 }
190   - logger.info("[点播开始] deviceId: {}, channelId: {},码流类型:{}, 收流端口: {}, 收流模式:{}, SSRC: {}, SSRC校验:{}",
191   - device.getDeviceId(), channelId, device.isSwitchPrimarySubStream() ? "辅码流" : "主码流", ssrcInfo.getPort(),
  191 + logger.info("[点播开始] deviceId: {}, channelId: {},码流类型:{}, 收流端口: {}, STREAM:{}, 收流模式:{}, SSRC: {}, SSRC校验:{}",
  192 + device.getDeviceId(), channelId, device.isSwitchPrimarySubStream() ? "辅码流" : "主码流", ssrcInfo.getPort(), ssrcInfo.getStream(),
192 193 device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck());
193 194 //端口获取失败的ssrcInfo 没有必要发送点播指令
194 195 if (ssrcInfo.getPort() <= 0) {
... ... @@ -219,16 +220,6 @@ public class PlayServiceImpl implements IPlayService {
219 220 device.getDeviceId(), channelId, device.isSwitchPrimarySubStream() ? "辅码流" : "主码流",
220 221 ssrcInfo.getPort(), ssrcInfo.getSsrc());
221 222  
222   - // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
223   -// InviteInfo inviteInfoForTimeout = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.play, device.getDeviceId(), channelId);
224   -// if (inviteInfoForTimeout == null) {
225   -// return;
226   -// }
227   -// if (InviteSessionStatus.ok == inviteInfoForTimeout.getStatus() ) {
228   -// // TODO 发送bye
229   -// }else {
230   -// // TODO 发送cancel
231   -// }
232 223 callback.run(InviteErrorCode.ERROR_FOR_STREAM_TIMEOUT.getCode(), InviteErrorCode.ERROR_FOR_STREAM_TIMEOUT.getMsg(), null);
233 224 inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
234 225 InviteErrorCode.ERROR_FOR_STREAM_TIMEOUT.getCode(), InviteErrorCode.ERROR_FOR_STREAM_TIMEOUT.getMsg(), null);
... ... @@ -272,99 +263,10 @@ public class PlayServiceImpl implements IPlayService {
272 263 logger.info("[点播成功] deviceId: {}, channelId:{}, 码流类型:{}", device.getDeviceId(), channelId,
273 264 device.isSwitchPrimarySubStream() ? "辅码流" : "主码流");
274 265 snapOnPlay(mediaServerItemInuse, device.getDeviceId(), channelId, ssrcInfo.getStream());
275   - }, (event) -> {
276   - inviteInfo.setStatus(InviteSessionStatus.ok);
277   -
278   - ResponseEvent responseEvent = (ResponseEvent) event.event;
279   - String contentString = new String(responseEvent.getResponse().getRawContent());
280   - // 获取ssrc
281   - int ssrcIndex = contentString.indexOf("y=");
282   - // 检查是否有y字段
283   - if (ssrcIndex >= 0) {
284   - //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 TODO 后续对不规范的非10位ssrc兼容
285   - String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12).trim();
286   - // 查询到ssrc不一致且开启了ssrc校验则需要针对处理
287   - if (ssrcInfo.getSsrc().equals(ssrcInResponse)) {
288   - if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE")) {
289   - tcpActiveHandler(device, channelId, contentString, mediaServerItem, timeOutTaskKey, ssrcInfo, callback);
290   - }
291   - return;
292   - }
293   - logger.info("[点播消息] 收到invite 200, 发现下级自定义了ssrc: {}", ssrcInResponse);
294   - if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
295   - logger.info("[点播消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
296   - // 释放ssrc
297   - mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
298   - // 单端口模式streamId也有变化,重新设置监听即可
299   - if (!mediaServerItem.isRtpEnable()) {
300   - // 添加订阅
301   - HookSubscribeForStreamChange hookSubscribe = HookSubscribeFactory.on_stream_changed("rtp", ssrcInfo.getStream(), true, "rtsp", mediaServerItem.getId());
302   - subscribe.removeSubscribe(hookSubscribe);
303   - String stream = String.format("%08x", Integer.parseInt(ssrcInResponse)).toUpperCase();
304   - hookSubscribe.getContent().put("stream", stream);
305   - inviteStreamService.updateInviteInfoForStream(inviteInfo, stream);
306   - subscribe.addSubscribe(hookSubscribe, (mediaServerItemInUse, hookParam) -> {
307   - logger.info("[ZLM HOOK] ssrc修正后收到订阅消息: " + hookParam);
308   - dynamicTask.stop(timeOutTaskKey);
309   - // hook响应
310   - StreamInfo streamInfo = onPublishHandlerForPlay(mediaServerItemInUse, hookParam, device.getDeviceId(), channelId);
311   - if (streamInfo == null){
312   - callback.run(InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getCode(),
313   - InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getMsg(), null);
314   - inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
315   - InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getCode(),
316   - InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getMsg(), null);
317   - return;
318   - }
319   - callback.run(InviteErrorCode.SUCCESS.getCode(),
320   - InviteErrorCode.SUCCESS.getMsg(), streamInfo);
321   - inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
322   - InviteErrorCode.SUCCESS.getCode(),
323   - InviteErrorCode.SUCCESS.getMsg(),
324   - streamInfo);
325   - snapOnPlay(mediaServerItemInUse, device.getDeviceId(), channelId, stream);
326   - });
327   - return;
328   - }
329   -
330   - // 更新ssrc
331   - Boolean result = mediaServerService.updateRtpServerSSRC(mediaServerItem, ssrcInfo.getStream(), ssrcInResponse);
332   - if (!result) {
333   - try {
334   - logger.warn("[点播] 更新ssrc失败,停止点播 {}/{}", device.getDeviceId(), channelId);
335   - cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null, null);
336   - } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException e) {
337   - logger.error("[命令发送失败] 停止点播, 发送BYE: {}", e.getMessage());
338   - }
339   -
340   - dynamicTask.stop(timeOutTaskKey);
341   - // 释放ssrc
342   - mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
343   -
344   - streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
345   -
346   - callback.run(InviteErrorCode.ERROR_FOR_RESET_SSRC.getCode(),
347   - "下级自定义了ssrc,重新设置收流信息失败", null);
348   - inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
349   - InviteErrorCode.ERROR_FOR_RESET_SSRC.getCode(),
350   - "下级自定义了ssrc,重新设置收流信息失败", null);
351   -
352   - }else {
353   - if (ssrcInfo.getStream()!= null && !ssrcInfo.getStream().equals(inviteInfo.getStream())) {
354   - inviteStreamService.removeInviteInfo(inviteInfo);
355   - }
356   - ssrcInfo.setSsrc(ssrcInResponse);
357   - inviteInfo.setSsrcInfo(ssrcInfo);
358   - inviteInfo.setStream(ssrcInfo.getStream());
359   - if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE")) {
360   - tcpActiveHandler(device, channelId, contentString, mediaServerItem, timeOutTaskKey, ssrcInfo, callback);
361   - }
362   - }
363   - }else {
364   - logger.info("[点播消息] 收到invite 200, 下级自定义了ssrc, 但是当前模式无需修正");
365   - }
366   - }
367   - inviteStreamService.updateInviteInfo(inviteInfo);
  266 + }, (eventResult) -> {
  267 + // 处理收到200ok后的TCP主动连接以及SSRC不一致的问题
  268 + InviteOKHandler(eventResult, ssrcInfo, mediaServerItem, device, channelId,
  269 + timeOutTaskKey, callback, inviteInfo, InviteSessionType.PLAY);
368 270 }, (event) -> {
369 271 dynamicTask.stop(timeOutTaskKey);
370 272 mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
... ... @@ -548,19 +450,23 @@ public class PlayServiceImpl implements IPlayService {
548 450 String endTime, ErrorCallback<Object> callback) {
549 451 Device device = storager.queryVideoDevice(deviceId);
550 452 if (device == null) {
551   - return;
  453 + logger.warn("[录像回放] 未找到设备 deviceId: {},channelId:{}", deviceId, channelId);
  454 + throw new ControllerException(ErrorCode.ERROR100.getCode(), "未找到设备:" + deviceId);
552 455 }
  456 +
553 457 MediaServerItem newMediaServerItem = getNewMediaServerItem(device);
  458 + if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE") && ! newMediaServerItem.isRtpEnable()) {
  459 + logger.warn("[录像回放] 单端口收流时不支持TCP主动方式收流 deviceId: {},channelId:{}", deviceId, channelId);
  460 + throw new ControllerException(ErrorCode.ERROR100.getCode(), "单端口收流时不支持TCP主动方式收流");
  461 + }
554 462 String stream = null;
555 463 if (newMediaServerItem.isRtpEnable()) {
556 464 String startTimeStr = startTime.replace("-", "")
557 465 .replace(":", "")
558 466 .replace(" ", "");
559   - System.out.println(startTimeStr);
560 467 String endTimeTimeStr = endTime.replace("-", "")
561 468 .replace(":", "")
562 469 .replace(" ", "");
563   - System.out.println(endTimeTimeStr);
564 470 stream = deviceId + "_" + channelId + "_" + startTimeStr + "_" + endTimeTimeStr;
565 471 }
566 472 SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, stream, null, device.isSsrcCheck(), true, 0, false, device.getStreamModeForParam());
... ... @@ -636,84 +542,13 @@ public class PlayServiceImpl implements IPlayService {
636 542 try {
637 543 cmder.playbackStreamCmd(mediaServerItem, ssrcInfo, device, channelId, startTime, endTime,
638 544 hookEvent, eventResult -> {
639   - inviteInfo.setStatus(InviteSessionStatus.ok);
640   - ResponseEvent responseEvent = (ResponseEvent) eventResult.event;
641   - String contentString = new String(responseEvent.getResponse().getRawContent());
642   - // 获取ssrc
643   - int ssrcIndex = contentString.indexOf("y=");
644   - // 检查是否有y字段
645   - if (ssrcIndex >= 0) {
646   - //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 TODO 后续对不规范的非10位ssrc兼容
647   - String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12);
648   - // 查询到ssrc不一致且开启了ssrc校验则需要针对处理
649   - if (ssrcInfo.getSsrc().equals(ssrcInResponse)) {
650   - if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE")) {
651   - tcpActiveHandler(device, channelId, contentString, mediaServerItem, playBackTimeOutTaskKey, ssrcInfo, callback);
652   - }
653   - return;
654   - }
655   - logger.info("[录像回放] 收到invite 200, 发现下级自定义了ssrc: {}", ssrcInResponse);
656   - if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
657   - logger.info("[录像回放] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
658   -
659   - // 释放ssrc
660   - mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
661   -
662   - // 单端口模式streamId也有变化,需要重新设置监听
663   - if (!mediaServerItem.isRtpEnable()) {
664   - // 添加订阅
665   - HookSubscribeForStreamChange hookSubscribe = HookSubscribeFactory.on_stream_changed("rtp", ssrcInfo.getStream(), true, "rtsp", mediaServerItem.getId());
666   - subscribe.removeSubscribe(hookSubscribe);
667   - String stream = String.format("%08x", Integer.parseInt(ssrcInResponse)).toUpperCase();
668   - hookSubscribe.getContent().put("stream", stream);
669   - inviteStreamService.updateInviteInfoForStream(inviteInfo, stream);
670   - subscribe.addSubscribe(hookSubscribe, (mediaServerItemInUse, hookParam) -> {
671   - logger.info("[ZLM HOOK] ssrc修正后收到订阅消息: " + hookParam);
672   - dynamicTask.stop(playBackTimeOutTaskKey);
673   - // hook响应
674   - hookEvent.response(mediaServerItemInUse, hookParam);
675   - });
676   - }
677   - // 更新ssrc
678   - Boolean result = mediaServerService.updateRtpServerSSRC(mediaServerItem, ssrcInfo.getStream(), ssrcInResponse);
679   - if (!result) {
680   - try {
681   - logger.warn("[录像回放] 更新ssrc失败,停止录像回放 {}/{}", device.getDeviceId(), channelId);
682   - cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null, null);
683   - } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException e) {
684   - logger.error("[命令发送失败] 停止点播, 发送BYE: {}", e.getMessage());
685   -
686   - }
687   -
688   - dynamicTask.stop(playBackTimeOutTaskKey);
689   - // 释放ssrc
690   - mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
691   -
692   - streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
693   -
694   - callback.run(InviteErrorCode.ERROR_FOR_RESET_SSRC.getCode(),
695   - "下级自定义了ssrc,重新设置收流信息失败", null);
696   -
697   - }else {
698   - if (ssrcInfo.getStream()!= null && !ssrcInfo.getStream().equals(inviteInfo.getStream())) {
699   - inviteStreamService.removeInviteInfo(inviteInfo);
700   - }
701   -
702   - ssrcInfo.setSsrc(ssrcInResponse);
703   - inviteInfo.setSsrcInfo(ssrcInfo);
704   - inviteInfo.setStream(ssrcInfo.getStream());
705   - if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE")) {
706   - tcpActiveHandler(device, channelId, contentString, mediaServerItem, playBackTimeOutTaskKey, ssrcInfo, callback);
707   - }
708   - }
709   - }else {
710   - logger.info("[点播消息] 收到invite 200, 下级自定义了ssrc, 但是当前模式无需修正");
711   - }
712   - }
713   - inviteStreamService.updateInviteInfo(inviteInfo);
  545 + // 处理收到200ok后的TCP主动连接以及SSRC不一致的问题
  546 + InviteOKHandler(eventResult, ssrcInfo, mediaServerItem, device, channelId,
  547 + playBackTimeOutTaskKey, callback, inviteInfo, InviteSessionType.PLAYBACK);
  548 +
714 549 }, errorEvent);
715 550 } catch (InvalidArgumentException | SipException | ParseException e) {
716   - logger.error("[命令发送失败] 回放: {}", e.getMessage());
  551 + logger.error("[命令发送失败] 录像回放: {}", e.getMessage());
717 552  
718 553 SipSubscribe.EventResult eventResult = new SipSubscribe.EventResult();
719 554 eventResult.type = SipSubscribe.EventResultType.cmdSendFailEvent;
... ... @@ -724,6 +559,121 @@ public class PlayServiceImpl implements IPlayService {
724 559 }
725 560  
726 561  
  562 + private void InviteOKHandler(SipSubscribe.EventResult eventResult, SSRCInfo ssrcInfo, MediaServerItem mediaServerItem,
  563 + Device device, String channelId, String timeOutTaskKey, ErrorCallback<Object> callback,
  564 + InviteInfo inviteInfo, InviteSessionType inviteSessionType){
  565 + inviteInfo.setStatus(InviteSessionStatus.ok);
  566 + ResponseEvent responseEvent = (ResponseEvent) eventResult.event;
  567 + String contentString = new String(responseEvent.getResponse().getRawContent());
  568 + String ssrcInResponse = SipUtils.getSsrcFromSdp(contentString);
  569 + if (ssrcInfo.getSsrc().equals(ssrcInResponse)) {
  570 + // ssrc 一致
  571 + if (mediaServerItem.isRtpEnable()) {
  572 + // 多端口
  573 + if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE")) {
  574 + tcpActiveHandler(device, channelId, contentString, mediaServerItem, timeOutTaskKey, ssrcInfo, callback);
  575 + }
  576 + }else {
  577 + // 单端口
  578 + if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE")) {
  579 + logger.warn("[Invite 200OK] 单端口收流模式不支持tcp主动模式收流");
  580 + }
  581 +
  582 + }
  583 + }else {
  584 + logger.info("[Invite 200OK] 收到invite 200, 发现下级自定义了ssrc: {}", ssrcInResponse);
  585 + // ssrc 不一致
  586 + if (mediaServerItem.isRtpEnable()) {
  587 + // 多端口
  588 + if (device.isSsrcCheck()) {
  589 + // ssrc检验
  590 + // 更新ssrc
  591 + logger.info("[Invite 200OK] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
  592 + // 释放ssrc
  593 + mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  594 + Boolean result = mediaServerService.updateRtpServerSSRC(mediaServerItem, ssrcInfo.getStream(), ssrcInResponse);
  595 + if (!result) {
  596 + try {
  597 + logger.warn("[Invite 200OK] 更新ssrc失败,停止点播 {}/{}", device.getDeviceId(), channelId);
  598 + cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null, null);
  599 + } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException e) {
  600 + logger.error("[命令发送失败] 停止播放, 发送BYE: {}", e.getMessage());
  601 + }
  602 +
  603 + dynamicTask.stop(timeOutTaskKey);
  604 + // 释放ssrc
  605 + mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  606 +
  607 + streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  608 +
  609 + callback.run(InviteErrorCode.ERROR_FOR_RESET_SSRC.getCode(),
  610 + "下级自定义了ssrc,重新设置收流信息失败", null);
  611 + inviteStreamService.call(inviteSessionType, device.getDeviceId(), channelId, null,
  612 + InviteErrorCode.ERROR_FOR_RESET_SSRC.getCode(),
  613 + "下级自定义了ssrc,重新设置收流信息失败", null);
  614 +
  615 + }else {
  616 + ssrcInfo.setSsrc(ssrcInResponse);
  617 + inviteInfo.setSsrcInfo(ssrcInfo);
  618 + inviteInfo.setStream(ssrcInfo.getStream());
  619 + if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE")) {
  620 + if (mediaServerItem.isRtpEnable()) {
  621 + tcpActiveHandler(device, channelId, contentString, mediaServerItem, timeOutTaskKey, ssrcInfo, callback);
  622 + }else {
  623 + logger.warn("[Invite 200OK] 单端口收流模式不支持tcp主动模式收流");
  624 + }
  625 + }
  626 + inviteStreamService.updateInviteInfo(inviteInfo);
  627 + }
  628 + }
  629 + }else {
  630 + if (ssrcInResponse != null) {
  631 + // 单端口
  632 + // 重新订阅流上线
  633 + HookSubscribeForStreamChange hookSubscribe = HookSubscribeFactory.on_stream_changed("rtp",
  634 + ssrcInfo.getStream(), true, "rtsp", mediaServerItem.getId());
  635 + subscribe.removeSubscribe(hookSubscribe);
  636 + SsrcTransaction ssrcTransaction = streamSession.getSsrcTransaction(inviteInfo.getDeviceId(),
  637 + inviteInfo.getChannelId(), null, inviteInfo.getStream());
  638 + streamSession.remove(inviteInfo.getDeviceId(),
  639 + inviteInfo.getChannelId(), inviteInfo.getStream());
  640 +
  641 + String stream = String.format("%08x", Integer.parseInt(ssrcInResponse)).toUpperCase();
  642 + hookSubscribe.getContent().put("stream", stream);
  643 +
  644 + inviteStreamService.updateInviteInfoForStream(inviteInfo, stream);
  645 + streamSession.put(device.getDeviceId(), channelId, ssrcTransaction.getCallId(),
  646 + stream, ssrcInResponse, mediaServerItem.getId(), (SIPResponse) responseEvent.getResponse(), inviteSessionType);
  647 + subscribe.addSubscribe(hookSubscribe, (mediaServerItemInUse, hookParam) -> {
  648 + logger.info("[Invite 200OK] ssrc修正后收到订阅消息: " + hookParam);
  649 + dynamicTask.stop(timeOutTaskKey);
  650 + subscribe.removeSubscribe(hookSubscribe);
  651 + // hook响应
  652 + StreamInfo streamInfo = onPublishHandlerForPlay(mediaServerItemInUse, hookParam, device.getDeviceId(), channelId);
  653 + if (streamInfo == null){
  654 + callback.run(InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getCode(),
  655 + InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getMsg(), null);
  656 + inviteStreamService.call(inviteSessionType, device.getDeviceId(), channelId, null,
  657 + InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getCode(),
  658 + InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getMsg(), null);
  659 + return;
  660 + }
  661 + callback.run(InviteErrorCode.SUCCESS.getCode(),
  662 + InviteErrorCode.SUCCESS.getMsg(), streamInfo);
  663 + inviteStreamService.call(inviteSessionType, device.getDeviceId(), channelId, null,
  664 + InviteErrorCode.SUCCESS.getCode(),
  665 + InviteErrorCode.SUCCESS.getMsg(),
  666 + streamInfo);
  667 + if (inviteSessionType == InviteSessionType.PLAY) {
  668 + snapOnPlay(mediaServerItemInUse, device.getDeviceId(), channelId, stream);
  669 + }
  670 + });
  671 + }
  672 + }
  673 + }
  674 + }
  675 +
  676 +
727 677  
728 678 @Override
729 679 public void download(String deviceId, String channelId, String startTime, String endTime, int downloadSpeed, ErrorCallback<Object> callback) {
... ... @@ -738,7 +688,17 @@ public class PlayServiceImpl implements IPlayService {
738 688 null);
739 689 return;
740 690 }
741   - SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, null, null, device.isSsrcCheck(), true, 0, false, device.getStreamModeForParam());
  691 + String stream = null;
  692 + if (newMediaServerItem.isRtpEnable()) {
  693 + String startTimeStr = startTime.replace("-", "")
  694 + .replace(":", "")
  695 + .replace(" ", "");
  696 + String endTimeTimeStr = endTime.replace("-", "")
  697 + .replace(":", "")
  698 + .replace(" ", "");
  699 + stream = deviceId + "_" + channelId + "_" + startTimeStr + "_" + endTimeTimeStr;
  700 + }
  701 + SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, stream, null, device.isSsrcCheck(), true, 0, false, device.getStreamModeForParam());
742 702 download(newMediaServerItem, ssrcInfo, deviceId, channelId, startTime, endTime, downloadSpeed, callback);
743 703 }
744 704  
... ... @@ -806,79 +766,9 @@ public class PlayServiceImpl implements IPlayService {
806 766 try {
807 767 cmder.downloadStreamCmd(mediaServerItem, ssrcInfo, device, channelId, startTime, endTime, downloadSpeed,
808 768 hookEvent, errorEvent, eventResult ->{
809   - inviteInfo.setStatus(InviteSessionStatus.ok);
810   - ResponseEvent responseEvent = (ResponseEvent) eventResult.event;
811   - String contentString = new String(responseEvent.getResponse().getRawContent());
812   - // 获取ssrc
813   - int ssrcIndex = contentString.indexOf("y=");
814   - // 检查是否有y字段
815   - if (ssrcIndex >= 0) {
816   - //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 TODO 后续对不规范的非10位ssrc兼容
817   - String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12);
818   - // 查询到ssrc不一致且开启了ssrc校验则需要针对处理
819   - if (ssrcInfo.getSsrc().equals(ssrcInResponse)) {
820   - if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE")) {
821   - tcpActiveHandler(device, channelId, contentString, mediaServerItem, downLoadTimeOutTaskKey, ssrcInfo, callback);
822   - }
823   - return;
824   - }
825   - logger.info("[录像下载] 收到invite 200, 发现下级自定义了ssrc: {}", ssrcInResponse);
826   - if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
827   - logger.info("[录像下载] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
828   -
829   - // 释放ssrc
830   - mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
831   -
832   - // 单端口模式streamId也有变化,需要重新设置监听
833   - if (!mediaServerItem.isRtpEnable()) {
834   - // 添加订阅
835   - HookSubscribeForStreamChange hookSubscribe = HookSubscribeFactory.on_stream_changed("rtp", ssrcInfo.getStream(), true, "rtsp", mediaServerItem.getId());
836   - subscribe.removeSubscribe(hookSubscribe);
837   - String stream = String.format("%08x", Integer.parseInt(ssrcInResponse)).toUpperCase();
838   - hookSubscribe.getContent().put("stream", stream);
839   - inviteStreamService.updateInviteInfoForStream(inviteInfo, stream);
840   - subscribe.addSubscribe(hookSubscribe, (mediaServerItemInUse, hookParam) -> {
841   - logger.info("[ZLM HOOK] ssrc修正后收到订阅消息: " + hookParam);
842   - dynamicTask.stop(downLoadTimeOutTaskKey);
843   - hookEvent.response(mediaServerItemInUse, hookParam);
844   - });
845   - }
846   -
847   - // 更新ssrc
848   - Boolean result = mediaServerService.updateRtpServerSSRC(mediaServerItem, ssrcInfo.getStream(), ssrcInResponse);
849   - if (!result) {
850   - try {
851   - logger.warn("[录像下载] 更新ssrc失败,停止录像回放 {}/{}", device.getDeviceId(), channelId);
852   - cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null, null);
853   - } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException e) {
854   - logger.error("[命令发送失败] 停止点播, 发送BYE: {}", e.getMessage());
855   - }
856   -
857   - dynamicTask.stop(downLoadTimeOutTaskKey);
858   - // 释放ssrc
859   - mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
860   -
861   - streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
862   -
863   - callback.run(InviteErrorCode.ERROR_FOR_RESET_SSRC.getCode(),
864   - "下级自定义了ssrc,重新设置收流信息失败", null);
865   -
866   - }else {
867   - if (ssrcInfo.getStream()!= null && !ssrcInfo.getStream().equals(inviteInfo.getStream())) {
868   - inviteStreamService.removeInviteInfo(inviteInfo);
869   - }
870   - ssrcInfo.setSsrc(ssrcInResponse);
871   - inviteInfo.setSsrcInfo(ssrcInfo);
872   - inviteInfo.setStream(ssrcInfo.getStream());
873   - if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE")) {
874   - tcpActiveHandler(device, channelId, contentString, mediaServerItem, downLoadTimeOutTaskKey, ssrcInfo, callback);
875   - }
876   - }
877   - }else {
878   - logger.info("[录像下载] 收到invite 200, 下级自定义了ssrc, 但是当前模式无需修正");
879   - }
880   - }
881   - inviteStreamService.updateInviteInfo(inviteInfo);
  769 + // 处理收到200ok后的TCP主动连接以及SSRC不一致的问题
  770 + InviteOKHandler(eventResult, ssrcInfo, mediaServerItem, device, channelId,
  771 + downLoadTimeOutTaskKey, callback, inviteInfo, InviteSessionType.DOWNLOAD);
882 772 });
883 773 } catch (InvalidArgumentException | SipException | ParseException e) {
884 774 logger.error("[命令发送失败] 录像下载: {}", e.getMessage());
... ...
src/main/java/com/genersoft/iot/vmp/utils/DateUtil.java
... ... @@ -7,6 +7,7 @@ import java.time.LocalDateTime;
7 7 import java.time.ZoneId;
8 8 import java.time.format.DateTimeFormatter;
9 9 import java.time.format.DateTimeParseException;
  10 +import java.time.temporal.ChronoUnit;
10 11 import java.time.temporal.TemporalAccessor;
11 12  
12 13 import java.util.Locale;
... ... @@ -106,4 +107,9 @@ public class DateUtil {
106 107 LocalDateTime nowDateTime = LocalDateTime.now();
107 108 return formatterISO8601.format(nowDateTime);
108 109 }
  110 +
  111 + public static long getDifferenceForNow(String keepaliveTime) {
  112 + Instant beforeInstant = Instant.from(formatter.parse(keepaliveTime));
  113 + return ChronoUnit.MILLIS.between(beforeInstant, Instant.now());
  114 + }
109 115 }
... ...
src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/play/PlayController.java
... ... @@ -111,7 +111,7 @@ public class PlayController {
111 111 wvpResult.setCode(ErrorCode.ERROR100.getCode());
112 112 wvpResult.setMsg("点播超时");
113 113 requestMessage.setData(wvpResult);
114   - resultHolder.invokeResult(requestMessage);
  114 + resultHolder.invokeAllResult(requestMessage);
115 115 inviteStreamService.removeInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channelId);
116 116 storager.stopPlay(deviceId, channelId);
117 117 });
... ... @@ -166,7 +166,7 @@ public class PlayController {
166 166 }
167 167 if (InviteSessionStatus.ok == inviteInfo.getStatus()) {
168 168 try {
169   - logger.warn("[停止点播] {}/{}", device.getDeviceId(), channelId);
  169 + logger.info("[停止点播] {}/{}", device.getDeviceId(), channelId);
170 170 cmder.streamByeCmd(device, channelId, inviteInfo.getStream(), null, null);
171 171 } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException e) {
172 172 logger.error("[命令发送失败] 停止点播, 发送BYE: {}", e.getMessage());
... ...