From fff3d64f850b3ee61b9847cd80e7958c46cc6347 Mon Sep 17 00:00:00 2001
From: 庄梦蝶殇 <linhuaichuan1989@126.com>
Date: 星期三, 27 七月 2022 18:31:33 +0800
Subject: [PATCH] fix 优化pom依赖,去除几个重复客户端。并改了几个 equals判断不精准的bug

---
 src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRunner.java |   97 ++++++++++++++----------------------------------
 1 files changed, 28 insertions(+), 69 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRunner.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRunner.java
index 12829e9..1bfb730 100644
--- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRunner.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRunner.java
@@ -55,9 +55,6 @@
     @Autowired
     private DynamicTask dynamicTask;
 
-    @Qualifier("taskExecutor")
-    @Autowired
-    private ThreadPoolTaskExecutor taskExecutor;
 
     @Override
     public void run(String... strings) throws Exception {
@@ -78,21 +75,11 @@
                 if (startGetMedia != null) {
                     startGetMedia.remove(zlmServerConfig.getGeneralMediaServerId());
                 }
-                mediaServerService.zlmServerOnline(zlmServerConfig);
             }
         });
 
-        // 璁㈤槄 zlm淇濇椿浜嬩欢, 褰搝lm绂荤嚎鏃跺仛涓氬姟鐨勫鐞�
-        hookSubscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_server_keepalive,new JSONObject(),
-                (MediaServerItem mediaServerItem, JSONObject response)->{
-                    String mediaServerId = response.getString("mediaServerId");
-                    if (mediaServerId !=null ) {
-                        mediaServerService.updateMediaServerKeepalive(mediaServerId, response.getJSONObject("data"));
-                    }
-                });
-
         // 鑾峰彇zlm淇℃伅
-        logger.info("[zlm鎺ュ叆]绛夊緟榛樿zlm涓�...");
+        logger.info("[zlm] 绛夊緟榛樿zlm涓�...");
 
         // 鑾峰彇鎵�鏈夌殑zlm锛� 骞跺紑鍚富鍔ㄨ繛鎺�
         List<MediaServerItem> all = mediaServerService.getAllFromDatabase();
@@ -105,9 +92,7 @@
                 startGetMedia = new HashMap<>();
             }
             startGetMedia.put(mediaServerItem.getId(), true);
-            taskExecutor.execute(()->{
-                connectZlmServer(mediaServerItem);
-            });
+            connectZlmServer(mediaServerItem);
         }
         String taskKey = "zlm-connect-timeout";
         dynamicTask.startDelay(taskKey, ()->{
@@ -118,22 +103,39 @@
                 }
                 startGetMedia = null;
             }
+            hookSubscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_server_started, new JSONObject());
         //  TODO 娓呯悊鏁版嵁搴撲腑涓巖edis涓嶅尮閰嶇殑zlm
-        }, 6 * 1000 );
+        }, 60 * 1000 );
     }
 
     @Async
     public void connectZlmServer(MediaServerItem mediaServerItem){
-        ZLMServerConfig zlmServerConfig = getMediaServerConfig(mediaServerItem, 1);
-        if (zlmServerConfig != null) {
-            zlmServerConfig.setIp(mediaServerItem.getIp());
-            zlmServerConfig.setHttpPort(mediaServerItem.getHttpPort());
+        String connectZlmServerTaskKey = "connect-zlm-" + mediaServerItem.getId();
+        ZLMServerConfig zlmServerConfigFirst = getMediaServerConfig(mediaServerItem);
+        if (zlmServerConfigFirst != null) {
+            zlmServerConfigFirst.setIp(mediaServerItem.getIp());
+            zlmServerConfigFirst.setHttpPort(mediaServerItem.getHttpPort());
             startGetMedia.remove(mediaServerItem.getId());
-            mediaServerService.zlmServerOnline(zlmServerConfig);
+            mediaServerService.zlmServerOnline(zlmServerConfigFirst);
+        }else {
+            logger.info("[ {} ]-[ {}:{} ]涓诲姩杩炴帴澶辫触, 娓呯悊鐩稿叧璧勬簮锛� 寮�濮嬪皾璇曢噸璇曡繛鎺�",
+                    mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
+            publisher.zlmOfflineEventPublish(mediaServerItem.getId());
         }
+
+        dynamicTask.startCron(connectZlmServerTaskKey, ()->{
+            ZLMServerConfig zlmServerConfig = getMediaServerConfig(mediaServerItem);
+            if (zlmServerConfig != null) {
+                dynamicTask.stop(connectZlmServerTaskKey);
+                zlmServerConfig.setIp(mediaServerItem.getIp());
+                zlmServerConfig.setHttpPort(mediaServerItem.getHttpPort());
+                startGetMedia.remove(mediaServerItem.getId());
+                mediaServerService.zlmServerOnline(zlmServerConfig);
+            }
+        }, 2000);
     }
 
-    public ZLMServerConfig getMediaServerConfig(MediaServerItem mediaServerItem, int index) {
+    public ZLMServerConfig getMediaServerConfig(MediaServerItem mediaServerItem) {
         if (startGetMedia == null) { return null;}
         if (!mediaServerItem.isDefaultServer() && mediaServerService.getOne(mediaServerItem.getId()) == null) {
             return null;
@@ -149,53 +151,10 @@
                 zlmServerConfig = JSON.parseObject(JSON.toJSONString(data.get(0)), ZLMServerConfig.class);
             }
         } else {
-            logger.error("[ {} ]-[ {}:{} ]绗瑊}娆′富鍔ㄨ繛鎺ュけ璐�, 2s鍚庨噸璇�",
-                    mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort(), index);
-            if (index == 1 && !StringUtils.isEmpty(mediaServerItem.getId())) {
-                logger.info("[ {} ]-[ {}:{} ]绗瑊}娆′富鍔ㄨ繛鎺ュけ璐�, 寮�濮嬫竻鐞嗙浉鍏宠祫婧�",
-                        mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort(), index);
-                publisher.zlmOfflineEventPublish(mediaServerItem.getId());
-            }
-            try {
-                Thread.sleep(2000);
-            } catch (InterruptedException e) {
-                e.printStackTrace();
-            }
-            zlmServerConfig = getMediaServerConfig(mediaServerItem, index += 1);
+            logger.error("[ {} ]-[ {}:{} ]涓诲姩杩炴帴澶辫触, 2s鍚庨噸璇�",
+                    mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
         }
         return zlmServerConfig;
 
     }
-
-    /**
-     * zlm 杩炴帴鎴愬姛鎴栬�厇lm閲嶅惎鍚�
-     */
-//    private void zLmRunning(ZLMServerConfig zlmServerConfig){
-//        logger.info( "[ id: " + zlmServerConfig.getGeneralMediaServerId() + "] zlm鎺ュ叆鎴愬姛...");
-//        // 鍏抽棴寰幆鑾峰彇zlm閰嶇疆
-//        startGetMedia = false;
-//        MediaServerItem mediaServerItem = new MediaServerItem(zlmServerConfig, sipIp);
-//        storager.updateMediaServer(mediaServerItem);
-//
-//        if (mediaServerItem.isAutoConfig()) setZLMConfig(mediaServerItem);
-//        zlmServerManger.updateServerCatchFromHook(zlmServerConfig);
-//
-//        // 娓呯┖鎵�鏈塻ession
-////        zlmMediaListManager.clearAllSessions();
-//
-//        // 鏇存柊娴佸垪琛�
-//        zlmMediaListManager.updateMediaList(mediaServerItem);
-//        // 鎭㈠娴佷唬鐞�, 鍙煡鎵捐繖涓繖涓祦濯掍綋
-//        List<StreamProxyItem> streamProxyListForEnable = storager.getStreamProxyListForEnableInMediaServer(
-//                mediaServerItem.getId(), true);
-//        for (StreamProxyItem streamProxyDto : streamProxyListForEnable) {
-//            logger.info("鎭㈠娴佷唬鐞嗭紝" + streamProxyDto.getApp() + "/" + streamProxyDto.getStream());
-//            JSONObject jsonObject = streamProxyService.addStreamProxyToZlm(streamProxyDto);
-//            if (jsonObject == null) {
-//                // 璁剧疆涓烘湭鍚敤
-//                logger.info("鎭㈠娴佷唬鐞嗗け璐ワ紝璇锋鏌ユ祦鍦板潃鍚庨噸鏂板惎鐢�" + streamProxyDto.getApp() + "/" + streamProxyDto.getStream());
-//                streamProxyService.stop(streamProxyDto.getApp(), streamProxyDto.getStream());
-//            }
-//        }
-//    }
 }

--
Gitblit v1.8.0