From ae4d33013136dfdceb129e96ae52bdf375d91dbe Mon Sep 17 00:00:00 2001
From: Lawrence <1934378145@qq.com>
Date: 星期一, 11 一月 2021 20:51:58 +0800
Subject: [PATCH] 修正播放回放ssrc

---
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java |   96 +++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 87 insertions(+), 9 deletions(-)

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 baf6e5a..19ffd61 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
@@ -3,10 +3,13 @@
 import com.genersoft.iot.vmp.conf.SipConfig;
 import com.genersoft.iot.vmp.gb28181.bean.Device;
 import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
+import com.genersoft.iot.vmp.gb28181.bean.ParentPlatformCatch;
+import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
 import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
 import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
+import com.genersoft.iot.vmp.gb28181.transmit.cmd.SIPRequestHeaderPlarformProvider;
 import com.genersoft.iot.vmp.gb28181.transmit.cmd.SIPRequestHeaderProvider;
-import com.genersoft.iot.vmp.media.zlm.ZLMUtils;
+import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
 import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
@@ -15,8 +18,11 @@
 import org.springframework.stereotype.Component;
 
 import javax.sip.*;
+import javax.sip.header.CallIdHeader;
+import javax.sip.header.WWWAuthenticateHeader;
 import javax.sip.message.Request;
 import java.text.ParseException;
+import java.util.UUID;
 
 @Component
 public class SIPCommanderFroPlatform implements ISIPCommanderForPlatform {
@@ -28,10 +34,19 @@
     private SIPRequestHeaderProvider headerProvider;
 
     @Autowired
+    private SIPRequestHeaderPlarformProvider headerProviderPlarformProvider;
+
+    @Autowired
     private VideoStreamSessionManager streamSession;
 
     @Autowired
     private IVideoManagerStorager storager;
+
+    @Autowired
+    private IRedisCatchStorage redisCatchStorage;
+
+    @Autowired
+    private SipSubscribe sipSubscribe;
 
     @Autowired
     @Qualifier(value="tcpSipProvider")
@@ -41,23 +56,38 @@
     @Qualifier(value="udpSipProvider")
     private SipProvider udpSipProvider;
 
-    @Autowired
-    private ZLMUtils zlmUtils;
-
     @Value("${media.rtp.enable}")
     private boolean rtpEnable;
 
     @Override
-    public boolean register(ParentPlatform parentPlatform, @Nullable String callId, @Nullable String realm, @Nullable String nonce, @Nullable String scheme ) {
+    public boolean register(ParentPlatform parentPlatform) {
+        return register(parentPlatform, null, null, null, null);
+    }
+
+    @Override
+    public boolean unregister(ParentPlatform parentPlatform, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) {
+        parentPlatform.setExpires("0");
+        ParentPlatformCatch parentPlatformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getDeviceGBId());
+        if (parentPlatformCatch != null) {
+            parentPlatformCatch.setParentPlatform(parentPlatform);
+            redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch);
+        }
+
+        return register(parentPlatform, null, null, errorEvent, okEvent);
+    }
+
+    @Override
+    public boolean register(ParentPlatform parentPlatform, @Nullable String callId, @Nullable WWWAuthenticateHeader www, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) {
         try {
             Request request = null;
-            if (realm == null || nonce == null) {
-                request = headerProvider.createRegisterRequest(parentPlatform, null, null);
+
+            if (www == null ) {
+                request = headerProviderPlarformProvider.createRegisterRequest(parentPlatform, 1L, null, null);
             }else {
-                request = headerProvider.createRegisterRequest(parentPlatform, null, null, callId, realm, nonce, scheme);
+                request = headerProviderPlarformProvider.createRegisterRequest(parentPlatform, null, null, callId, www);
             }
 
-            transmitRequest(parentPlatform, request);
+            transmitRequest(parentPlatform, request, errorEvent, okEvent);
             return true;
         } catch (ParseException e) {
             e.printStackTrace();
@@ -71,11 +101,59 @@
         return false;
     }
 
+    @Override
+    public String keepalive(ParentPlatform parentPlatform) {
+        String callId = null;
+        try {
+
+            StringBuffer keepaliveXml = new StringBuffer(200);
+            keepaliveXml.append("<?xml version=\"1.0\" 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("<Status>OK</Status>\r\n");
+            keepaliveXml.append("</Notify>\r\n");
+
+            Request request = headerProviderPlarformProvider.createKeetpaliveMessageRequest(
+                    parentPlatform,
+                    keepaliveXml.toString(),
+                    UUID.randomUUID().toString().replace("-", ""),
+                    UUID.randomUUID().toString().replace("-", ""),
+                    null);
+            transmitRequest(parentPlatform, request);
+            CallIdHeader callIdHeader = (CallIdHeader)request.getHeader(CallIdHeader.NAME);
+            callId = callIdHeader.getCallId();
+        } catch (ParseException | InvalidArgumentException | SipException e) {
+            e.printStackTrace();
+        }
+        return callId;
+    }
+
     private void transmitRequest(ParentPlatform parentPlatform, Request request) throws SipException {
+        transmitRequest(parentPlatform, request, null, null);
+    }
+
+    private void transmitRequest(ParentPlatform parentPlatform, Request request, SipSubscribe.Event errorEvent) throws SipException {
+        transmitRequest(parentPlatform, request, errorEvent, null);
+    }
+
+    private void transmitRequest(ParentPlatform parentPlatform, Request request, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws SipException {
         if("TCP".equals(parentPlatform.getTransport())) {
             tcpSipProvider.sendRequest(request);
         } else if("UDP".equals(parentPlatform.getTransport())) {
             udpSipProvider.sendRequest(request);
         }
+
+        CallIdHeader callIdHeader = (CallIdHeader)request.getHeader(CallIdHeader.NAME);
+        // 娣诲姞閿欒璁㈤槄
+        if (errorEvent != null) {
+            sipSubscribe.addErrorSubscribe(callIdHeader.getCallId(), errorEvent);
+        }
+        // 娣诲姞璁㈤槄
+        if (okEvent != null) {
+            sipSubscribe.addOkSubscribe(callIdHeader.getCallId(), okEvent);
+        }
+
     }
 }

--
Gitblit v1.8.0