From fec69c714c632eaed01f77cb419d1d6a02e273ea Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期日, 25 六月 2023 22:40:26 +0800
Subject: [PATCH] 优化国标信令

---
 src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java |   56 +++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 43 insertions(+), 13 deletions(-)

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 68f1bee..eb7d1a0 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
@@ -9,6 +9,8 @@
 import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeFactory;
 import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeForRtpServerTimeout;
 import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
+import com.genersoft.iot.vmp.media.zlm.dto.hook.HookParam;
+import com.genersoft.iot.vmp.media.zlm.dto.hook.OnRtpServerTimeoutHookParam;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -225,7 +227,7 @@
         int localPort = 0;
         if (userSetting.getGbSendStreamStrict()) {
             if (userSetting.getGbSendStreamStrict()) {
-                localPort = keepPort(serverItem, ssrc);
+                localPort = keepPort(serverItem, ssrc, localPort);
                 if (localPort == 0) {
                     return null;
                 }
@@ -261,7 +263,7 @@
         // 榛樿涓洪殢鏈虹鍙�
         int localPort = 0;
         if (userSetting.getGbSendStreamStrict()) {
-            localPort = keepPort(serverItem, ssrc);
+            localPort = keepPort(serverItem, ssrc, localPort);
             if (localPort == 0) {
                 return null;
             }
@@ -285,27 +287,35 @@
     /**
      * 淇濇寔绔彛锛岀洿鍒伴渶瑕侀渶瑕佸彂娴佹椂鍐嶉噴鏀�
      */
-    public int keepPort(MediaServerItem serverItem, String ssrc) {
-        int localPort = 0;
+    public int keepPort(MediaServerItem serverItem, String ssrc, Integer localPort) {
         Map<String, Object> param = new HashMap<>(3);
-        param.put("port", 0);
+        param.put("port", localPort);
         param.put("enable_tcp", 1);
         param.put("stream_id", ssrc);
         JSONObject jsonObject = zlmresTfulUtils.openRtpServer(serverItem, param);
         if (jsonObject.getInteger("code") == 0) {
             localPort = jsonObject.getInteger("port");
             HookSubscribeForRtpServerTimeout hookSubscribeForRtpServerTimeout = HookSubscribeFactory.on_rtp_server_timeout(ssrc, null, serverItem.getId());
-            // 璁㈤槄 zlm鍚姩浜嬩欢, 鏂扮殑zlm涔熶細浠庤繖閲岃繘鍏ョ郴缁�
+            Integer finalLocalPort = localPort;
             hookSubscribe.addSubscribe(hookSubscribeForRtpServerTimeout,
-                    (MediaServerItem mediaServerItem, JSONObject response)->{
-                        logger.info("[涓婄骇鐐规挱] {}->鐩戝惉绔彛鍒版湡缁х画淇濇寔鐩戝惉", ssrc);
-                        keepPort(serverItem, ssrc);
+                    (MediaServerItem mediaServerItem, HookParam hookParam)->{
+                        logger.info("[涓婄骇鐐规挱] {}->鐩戝惉绔彛鍒版湡缁х画淇濇寔鐩戝惉: {}", ssrc, finalLocalPort);
+                        OnRtpServerTimeoutHookParam rtpServerTimeoutHookParam = (OnRtpServerTimeoutHookParam) hookParam;
+                        if (!ssrc.equals(rtpServerTimeoutHookParam.getSsrc())) {
+                            return;
+                        }
+                        int port = keepPort(serverItem, ssrc, finalLocalPort);
+                        if (port == 0) {
+                            logger.info("[涓婄骇鐐规挱] {}->鐩戝惉绔彛澶辫触锛岀Щ闄ょ洃鍚�", ssrc);
+                            hookSubscribe.removeSubscribe(hookSubscribeForRtpServerTimeout);
+                        }
                     });
             logger.info("[涓婄骇鐐规挱] {}->鐩戝惉绔彛: {}", ssrc, localPort);
+            return localPort;
         }else {
-            logger.info("[涓婄骇鐐规挱] 鐩戝惉绔彛澶辫触: {}", ssrc);
+            logger.info("[涓婄骇鐐规挱] 鐩戝惉绔彛澶辫触: {}->{}", ssrc, localPort);
+            return 0;
         }
-        return localPort;
     }
 
     /**
@@ -332,6 +342,9 @@
      */
     public Boolean isRtpReady(MediaServerItem mediaServerItem, String streamId) {
         JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo(mediaServerItem,"rtp", "rtsp", streamId);
+        if (mediaInfo.getInteger("code") == -2) {
+            return null;
+        }
         return (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online"));
     }
 
@@ -340,8 +353,10 @@
      */
     public Boolean isStreamReady(MediaServerItem mediaServerItem, String app, String streamId) {
         JSONObject mediaInfo = zlmresTfulUtils.getMediaList(mediaServerItem, app, streamId);
-        return mediaInfo != null && (mediaInfo.getInteger("code") == 0
-
+        if (mediaInfo == null || (mediaInfo.getInteger("code") == -2)) {
+            return null;
+        }
+        return  (mediaInfo.getInteger("code") == 0
                 && mediaInfo.getJSONArray("data") != null
                 && mediaInfo.getJSONArray("data").size() > 0);
     }
@@ -388,4 +403,19 @@
     public void closeAllSendRtpStream() {
 
     }
+
+    public Boolean updateRtpServerSSRC(MediaServerItem mediaServerItem, String streamId, String ssrc) {
+        boolean result = false;
+        JSONObject jsonObject = zlmresTfulUtils.updateRtpServerSSRC(mediaServerItem, streamId, ssrc);
+        if (jsonObject == null) {
+            logger.error("[鏇存柊RTPServer] 澶辫触: 璇锋鏌LM鏈嶅姟");
+        } else if (jsonObject.getInteger("code") == 0) {
+            result= true;
+            logger.info("[鏇存柊RTPServer] 鎴愬姛");
+        } else {
+            logger.error("[鏇存柊RTPServer] 澶辫触: {}, streamId锛歿}锛宻src锛歿}->\r\n{}",jsonObject.getString("msg"),
+                    streamId, ssrc, jsonObject);
+        }
+        return result;
+    }
 }

--
Gitblit v1.8.0