From b05ddb2003bb8510694d433c33c6ae09ee94e140 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期三, 10 三月 2021 20:43:29 +0800
Subject: [PATCH] Merge pull request #64 from lawrencehj/wvp-28181-2.0

---
 src/main/java/com/genersoft/iot/vmp/gb28181/event/platformKeepaliveExpire/PlatformKeepaliveExpireEventLister.java |    1 
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java                                   |    4 +-
 src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java                                            |   50 ++++++++++++------------
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/ByeRequestProcessor.java                        |   42 +++++++++++++++++++++
 src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java                                                |    4 ++
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/SIPProcessorFactory.java                                     |    2 +
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java                        |    6 +-
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/AckRequestProcessor.java                        |   11 +++--
 8 files changed, 86 insertions(+), 34 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/event/platformKeepaliveExpire/PlatformKeepaliveExpireEventLister.java b/src/main/java/com/genersoft/iot/vmp/gb28181/event/platformKeepaliveExpire/PlatformKeepaliveExpireEventLister.java
index c02b7e1..a52a925 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/event/platformKeepaliveExpire/PlatformKeepaliveExpireEventLister.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/event/platformKeepaliveExpire/PlatformKeepaliveExpireEventLister.java
@@ -64,6 +64,7 @@
             // 鏈�3娆℃湭鏀跺埌蹇冭烦鍥炲, 璁剧疆骞冲彴鐘舵�佷负绂荤嚎, 寮�濮嬮噸鏂版敞鍐�
             logger.warn("鏈�3娆℃湭鏀跺埌蹇冭烦鍥炲,鏍囪璁剧疆骞冲彴鐘舵�佷负绂荤嚎, 骞堕噸鏂版敞鍐� 骞冲彴鍥芥爣ID锛�" + event.getPlatformGbID());
             publisher.platformNotRegisterEventPublish(event.getPlatformGbID());
+            parentPlatformCatch.setKeepAliveReply(0);
         }else {
             // 鍐嶆鍙戦�佸績璺�
             String callId = sipCommanderForPlatform.keepalive(parentPlatform);
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/SIPProcessorFactory.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/SIPProcessorFactory.java
index 2d1ca09..b3b2fba 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/SIPProcessorFactory.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/SIPProcessorFactory.java
@@ -154,6 +154,8 @@
 		} else if (Request.BYE.equals(method)) {
 			ByeRequestProcessor processor = new ByeRequestProcessor();
 			processor.setRequestEvent(evt);
+			processor.setRedisCatchStorage(redisCatchStorage);
+			processor.setZlmrtpServerFactory(zlmrtpServerFactory);
 			return processor;
 		} else if (Request.CANCEL.equals(method)) {
 			CancelRequestProcessor processor = new CancelRequestProcessor();
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
index 980c856..912189c 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
@@ -893,7 +893,7 @@
 			catalogXml.append("</Query>\r\n");
 			
 			String tm = Long.toString(System.currentTimeMillis());
-			Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaDeviceInfoBranch", "FromDev" + tm, null);
+			Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "z9hG4bK-ViaDeviceInfo" + tm, "FromDev" + tm, null);
 
 			transmitRequest(device, request);
 			
@@ -923,7 +923,7 @@
 			catalogXml.append("</Query>\r\n");
 			
 			String tm = Long.toString(System.currentTimeMillis());
-			Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaCatalogBranch", "FromCat" + tm, null);
+			Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "z9hG4bK-ViaCatalog" + tm, "FromCat" + tm, null);
 
 			transmitRequest(device, request, errorEvent);
 		} catch (SipException | ParseException | InvalidArgumentException e) {
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java
index ae9dfa7..28761ed 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java
@@ -118,18 +118,18 @@
         try {
 
             StringBuffer keepaliveXml = new StringBuffer(200);
-            keepaliveXml.append("<?xml version=\"1.0\" encoding=\"GB2312\" ?>\r\n");
+            keepaliveXml.append("<?xml version=\"1.0\"?>\r\n");//" encoding=\"GB2312\"?>\r\n");
             keepaliveXml.append("<Notify>\r\n");
             keepaliveXml.append("<CmdType>Keepalive</CmdType>\r\n");
             keepaliveXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
-            keepaliveXml.append("<DeviceID>" + parentPlatform.getServerGBId() + "</DeviceID>\r\n");
+            keepaliveXml.append("<DeviceID>" + parentPlatform.getDeviceGBId() + "</DeviceID>\r\n");
             keepaliveXml.append("<Status>OK</Status>\r\n");
             keepaliveXml.append("</Notify>\r\n");
 
             Request request = headerProviderPlarformProvider.createKeetpaliveMessageRequest(
                     parentPlatform,
                     keepaliveXml.toString(),
-                    UUID.randomUUID().toString().replace("-", ""),
+                    "z9hG4bK-" + UUID.randomUUID().toString().replace("-", ""),
                     UUID.randomUUID().toString().replace("-", ""),
                     null);
             transmitRequest(parentPlatform, request);
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/AckRequestProcessor.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/AckRequestProcessor.java
index 7f58de5..ad7b070 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/AckRequestProcessor.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/AckRequestProcessor.java
@@ -22,12 +22,9 @@
 @Component
 public class AckRequestProcessor extends SIPRequestAbstractProcessor {
 
-    //@Autowired
     private IRedisCatchStorage redisCatchStorage;
 
-	//@Autowired
 	private ZLMRTPServerFactory zlmrtpServerFactory;
-
 
 	/**   
 	 * 澶勭悊  ACK璇锋眰
@@ -49,6 +46,8 @@
 			String is_Udp = sendRtpItem.isTcp() ? "0" : "1";
 			String deviceId = sendRtpItem.getDeviceId();
 			StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
+			sendRtpItem.setStreamId(streamInfo.getStreamId());
+			redisCatchStorage.updateSendRTPSever(sendRtpItem);
 			System.out.println(platformGbId);
 			System.out.println(channelId);
 			Map<String, Object> param = new HashMap<>();
@@ -68,11 +67,16 @@
 					if (System.currentTimeMillis() - startTime < 30 * 1000) {
 						if (zlmrtpServerFactory.isRtpReady(streamInfo.getStreamId())) {
 							rtpPushed = true;
+							System.out.println("宸茶幏鍙栬澶囨帹娴侊紝寮�濮嬪悜涓婄骇鎺ㄦ祦");
 							zlmrtpServerFactory.startSendRtpStream(param);
 						} else {
+							System.out.println("绛夊緟璁惧鎺ㄦ祦.......");
 							Thread.sleep(2000);
 							continue;
 						}
+					} else {
+						rtpPushed = true;
+						System.out.println("璁惧鎺ㄦ祦瓒呮椂锛岀粓姝㈠悜涓婄骇鎺ㄦ祦");
 					}
 				} catch (InterruptedException e) {
 					e.printStackTrace();
@@ -108,5 +112,4 @@
 	public void setZlmrtpServerFactory(ZLMRTPServerFactory zlmrtpServerFactory) {
 		this.zlmrtpServerFactory = zlmrtpServerFactory;
 	}
-
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/ByeRequestProcessor.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/ByeRequestProcessor.java
index 0ba6bd8..a14a4cc 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/ByeRequestProcessor.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/ByeRequestProcessor.java
@@ -1,13 +1,20 @@
 package com.genersoft.iot.vmp.gb28181.transmit.request.impl;
 
+import javax.sip.Dialog;
+import javax.sip.DialogState;
 import javax.sip.InvalidArgumentException;
 import javax.sip.RequestEvent;
 import javax.sip.SipException;
 import javax.sip.message.Response;
 
+import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
 import com.genersoft.iot.vmp.gb28181.transmit.request.SIPRequestAbstractProcessor;
+import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
+import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
 
 import java.text.ParseException;
+import java.util.HashMap;
+import java.util.Map;
 
 /**    
  * @Description: BYE璇锋眰澶勭悊鍣�
@@ -15,6 +22,10 @@
  * @date:   2020骞�5鏈�3鏃� 涓嬪崍5:32:05     
  */
 public class ByeRequestProcessor extends SIPRequestAbstractProcessor {
+
+    private IRedisCatchStorage redisCatchStorage;
+
+	private ZLMRTPServerFactory zlmrtpServerFactory;
 
 	/**
 	 * 澶勭悊BYE璇锋眰
@@ -24,6 +35,22 @@
 	public void process(RequestEvent evt) {
 		try {
 			responseAck(evt);
+			Dialog dialog = evt.getDialog();
+			if (dialog == null) return;
+			if (dialog.getState().equals(DialogState.TERMINATED)) {
+				String remoteUri = dialog.getRemoteParty().getURI().toString();
+				String localUri = dialog.getLocalParty().getURI().toString();
+				String platformGbId = remoteUri.substring(remoteUri.indexOf(":") + 1, remoteUri.indexOf("@"));
+				String channelId = localUri.substring(remoteUri.indexOf(":") + 1, remoteUri.indexOf("@"));
+				SendRtpItem sendRtpItem =  redisCatchStorage.querySendRTPServer(platformGbId, channelId);
+				String streamId = sendRtpItem.getStreamId();
+				Map<String, Object> param = new HashMap<>();
+				param.put("vhost","__defaultVhost__");
+				param.put("app","rtp");
+				param.put("stream",streamId);
+				System.out.println("鍋滄鍚戜笂绾ф帹娴侊細" + streamId);
+				zlmrtpServerFactory.stopSendRtpStream(param);
+			}
 		} catch (SipException e) {
 			e.printStackTrace();
 		} catch (InvalidArgumentException e) {
@@ -47,4 +74,19 @@
 		getServerTransaction(evt).sendResponse(response);
 	}
 
+	public IRedisCatchStorage getRedisCatchStorage() {
+		return redisCatchStorage;
+	}
+
+	public void setRedisCatchStorage(IRedisCatchStorage redisCatchStorage) {
+		this.redisCatchStorage = redisCatchStorage;
+	}
+
+	public ZLMRTPServerFactory getZlmrtpServerFactory() {
+		return zlmrtpServerFactory;
+	}
+
+	public void setZlmrtpServerFactory(ZLMRTPServerFactory zlmrtpServerFactory) {
+		this.zlmrtpServerFactory = zlmrtpServerFactory;
+	}
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
index e3cc8a4..42670a7 100644
--- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
@@ -123,4 +123,8 @@
     public JSONObject startSendRtp(Map<String, Object> param) {
         return sendPost("startSendRtp",param);
     }
+
+    public JSONObject stopSendRtp(Map<String, Object> param) {
+        return sendPost("stopSendRtp",param);
+    }
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java
index 819c6a8..00951ba 100644
--- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java
@@ -127,46 +127,46 @@
     }
 
     /**
-     * 
+     * 璋冪敤zlm RESTful API 鈥斺�� startSendRtp
      */
     public Boolean startSendRtpStream(Map<String, Object>param) {
         Boolean result = false;
         JSONObject jsonObject = zlmresTfulUtils.startSendRtp(param);
         System.out.println(jsonObject);
-        if (jsonObject != null) {
-            switch (jsonObject.getInteger("code")){
-                case 0:
-                    result= true;
-                    logger.error("RTP鎺ㄦ祦璇锋眰鎴愬姛锛屾湰鍦版帹娴佺鍙o細" + jsonObject.getString("local_port"));
-                    break;
-                // case -300: // id宸茬粡瀛樺湪
-                //     result = false;
-                //     break;
-                // case -400: // 绔彛鍗犵敤
-                //     result= false;
-                //     break;
-                default:
-                    logger.error("RTP鎺ㄦ祦澶辫触: " + jsonObject.getString("msg"));
-                    break;
-            }
-        }else {
-            //  妫�鏌LM鐘舵��
+        if (jsonObject == null) {
             logger.error("RTP鎺ㄦ祦澶辫触: 璇锋鏌LM鏈嶅姟");
+        } else if (jsonObject.getInteger("code") == 0) {
+            result= true;
+            logger.error("RTP鎺ㄦ祦璇锋眰鎴愬姛锛屾湰鍦版帹娴佺鍙o細" + jsonObject.getString("local_port"));
+        } else {
+            logger.error("RTP鎺ㄦ祦澶辫触: " + jsonObject.getString("msg"));
         }
         return result;
     }
 
     /**
-     * 
+     * 鏌ヨ寰呰浆鎺ㄧ殑娴佹槸鍚﹀氨缁�
      */
     public Boolean isRtpReady(String streamId) {
         JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo("rtp", "rtmp", streamId);
-        if (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online")) {
-            logger.info("璁惧RTP鎺ㄦ祦鎴愬姛");
-            return true;
+        return (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online"));
+    }
+
+    /**
+     * 璋冪敤zlm RESTful API 鈥斺�� stopSendRtp
+     */
+    public Boolean stopSendRtpStream(Map<String, Object>param) {
+        Boolean result = false;
+        JSONObject jsonObject = zlmresTfulUtils.stopSendRtp(param);
+        System.out.println(jsonObject);
+        if (jsonObject == null) {
+            logger.error("鍋滄RTP鎺ㄦ祦澶辫触: 璇锋鏌LM鏈嶅姟");
+        } else if (jsonObject.getInteger("code") == 0) {
+            result= true;
+            logger.error("鍋滄RTP鎺ㄦ祦鎴愬姛");
         } else {
-            logger.info("璁惧RTP鎺ㄦ祦鏈畬鎴�");
-            return false;
+            logger.error("鍋滄RTP鎺ㄦ祦澶辫触: " + jsonObject.getString("msg"));
         }
+        return result;
     }
 }

--
Gitblit v1.8.0