From c6dfb63f8fd5f04fa00ac6c45da2eb6bcc5cada4 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期一, 25 三月 2024 23:59:50 +0800
Subject: [PATCH] 优化hook通知

---
 src/main/java/com/genersoft/iot/vmp/media/abl/ABLMediaServerStatusManger.java |  235 +++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 143 insertions(+), 92 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/media/abl/ABLMediaServerStatusManger.java b/src/main/java/com/genersoft/iot/vmp/media/abl/ABLMediaServerStatusManger.java
index 28181e8..dcb4655 100644
--- a/src/main/java/com/genersoft/iot/vmp/media/abl/ABLMediaServerStatusManger.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/abl/ABLMediaServerStatusManger.java
@@ -1,20 +1,17 @@
 package com.genersoft.iot.vmp.media.abl;
 
-import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import com.genersoft.iot.vmp.conf.DynamicTask;
 import com.genersoft.iot.vmp.conf.UserSetting;
 import com.genersoft.iot.vmp.media.abl.bean.AblServerConfig;
+import com.genersoft.iot.vmp.media.abl.bean.ConfigKeyId;
 import com.genersoft.iot.vmp.media.abl.event.HookAblServerKeepaliveEvent;
 import com.genersoft.iot.vmp.media.abl.event.HookAblServerStartEvent;
 import com.genersoft.iot.vmp.media.event.MediaServerChangeEvent;
 import com.genersoft.iot.vmp.media.event.MediaServerDeleteEvent;
 import com.genersoft.iot.vmp.media.service.IMediaServerService;
 import com.genersoft.iot.vmp.media.zlm.dto.MediaServer;
-import com.genersoft.iot.vmp.media.zlm.dto.ZLMServerConfig;
-import com.genersoft.iot.vmp.media.zlm.event.HookZlmServerKeepaliveEvent;
-import com.genersoft.iot.vmp.media.zlm.event.HookZlmServerStartEvent;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -26,9 +23,9 @@
 import org.springframework.util.ObjectUtils;
 
 import java.io.File;
+import java.lang.reflect.Field;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
@@ -136,17 +133,17 @@
                     continue;
                 }
                 logger.info("[ABL-灏濊瘯杩炴帴] ID锛歿}, 鍦板潃锛� {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
-                JSONObject responseJson = ablResTfulUtils.getMediaServerConfig(mediaServerItem);
+                JSONObject responseJson = ablResTfulUtils.getServerConfig(mediaServerItem);
                 AblServerConfig ablServerConfig = null;
                 if (responseJson == null) {
                     logger.info("[ABL-灏濊瘯杩炴帴]澶辫触, ID锛歿}, 鍦板潃锛� {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
                     continue;
                 }
-                JSONArray data = responseJson.getJSONArray("data");
+                JSONArray data = responseJson.getJSONArray("params");
                 if (data == null || data.isEmpty()) {
                     logger.info("[ABL-灏濊瘯杩炴帴]澶辫触, ID锛歿}, 鍦板潃锛� {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
                 }else {
-                    ablServerConfig = JSON.parseObject(JSON.toJSONString(data.get(0)), AblServerConfig.class);
+                    ablServerConfig = AblServerConfig.getInstance(data);
                     initPort(mediaServerItem, ablServerConfig);
                     online(mediaServerItem, ablServerConfig);
                 }
@@ -158,21 +155,21 @@
                     continue;
                 }
                 logger.info("[ABL-灏濊瘯杩炴帴] ID锛歿}, 鍦板潃锛� {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
-                JSONObject responseJson = ablResTfulUtils.getMediaServerConfig(mediaServerItem);
-                ZLMServerConfig zlmServerConfig = null;
+                JSONObject responseJson = ablResTfulUtils.getServerConfig(mediaServerItem);
+                AblServerConfig ablServerConfig = null;
                 if (responseJson == null) {
                     logger.info("[ABL-灏濊瘯杩炴帴]澶辫触, ID锛歿}, 鍦板潃锛� {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
                     offlineAblTimeMap.put(mediaServerItem.getId(), System.currentTimeMillis());
                     continue;
                 }
-                JSONArray data = responseJson.getJSONArray("data");
+                JSONArray data = responseJson.getJSONArray("params");
                 if (data == null || data.isEmpty()) {
                     logger.info("[ABL-灏濊瘯杩炴帴]澶辫触, ID锛歿}, 鍦板潃锛� {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
                     offlineAblTimeMap.put(mediaServerItem.getId(), System.currentTimeMillis());
                 }else {
-                    zlmServerConfig = JSON.parseObject(JSON.toJSONString(data.get(0)), ZLMServerConfig.class);
-                    initPort(mediaServerItem, zlmServerConfig);
-                    online(mediaServerItem, zlmServerConfig);
+                    ablServerConfig = AblServerConfig.getInstance(data);
+                    initPort(mediaServerItem, ablServerConfig);
+                    online(mediaServerItem, ablServerConfig);
                 }
             }
         }
@@ -189,16 +186,15 @@
             mediaServerService.update(mediaServerItem);
             if(mediaServerItem.isAutoConfig()) {
                 if (config == null) {
-                    JSONObject responseJSON = ablResTfulUtils.getMediaServerConfig(mediaServerItem);
-                    JSONArray data = responseJSON.getJSONArray("data");
+                    JSONObject responseJSON = ablResTfulUtils.getServerConfig(mediaServerItem);
+                    JSONArray data = responseJSON.getJSONArray("params");
                     if (data != null && !data.isEmpty()) {
-                        config = JSON.parseObject(JSON.toJSONString(data.get(0)), AblServerConfig.class);
+                        config = AblServerConfig.getInstance(data);
                     }
                 }
                 if (config != null) {
                     initPort(mediaServerItem, config);
-                    setZLMConfig(mediaServerItem, "0".equals(config.getHookEnable())
-                            || !Objects.equals(mediaServerItem.getHookAliveInterval(), config.getHookAliveInterval()));
+                    setAblConfig(mediaServerItem, false, config);
                 }
             }
             mediaServerService.update(mediaServerItem);
@@ -216,90 +212,145 @@
     }
     private void initPort(MediaServer mediaServerItem, AblServerConfig ablServerConfig) {
         // 绔彛鍙細浠庨厤缃腑璇诲彇涓�娆★紝涓�鏃﹁嚜宸遍厤缃垨鑰呰鍙栬繃浜嗗皢涓嶅湪閰嶇疆
-        if (mediaServerItem.getHttpSSlPort() == 0) {
-            mediaServerItem.setHttpSSlPort(ablServerConfig.getHttpSSLport());
-        }
+//        if (mediaServerItem.getHttpSSlPort() == 0) {
+//            mediaServerItem.setHttpSSlPort(ablServerConfig.getHttpSSLport());
+//        }
         if (mediaServerItem.getRtmpPort() == 0) {
             mediaServerItem.setRtmpPort(ablServerConfig.getRtmpPort());
         }
-        if (mediaServerItem.getRtmpSSlPort() == 0) {
-            mediaServerItem.setRtmpSSlPort(ablServerConfig.getRtmpSslPort());
-        }
+//        if (mediaServerItem.getRtmpSSlPort() == 0) {
+//            mediaServerItem.setRtmpSSlPort(ablServerConfig.getRtmpSslPort());
+//        }
         if (mediaServerItem.getRtspPort() == 0) {
             mediaServerItem.setRtspPort(ablServerConfig.getRtspPort());
         }
-        if (mediaServerItem.getRtspSSLPort() == 0) {
-            mediaServerItem.setRtspSSLPort(ablServerConfig.getRtspSSlport());
-        }
-        if (mediaServerItem.getRtpProxyPort() == 0) {
-            mediaServerItem.setRtpProxyPort(ablServerConfig.getRtpProxyPort());
-        }
+//        if (mediaServerItem.getRtspSSLPort() == 0) {
+//            mediaServerItem.setRtspSSLPort(ablServerConfig.getRtspSSlport());
+//        }
+//        if (mediaServerItem.getRtpProxyPort() == 0) {
+//            mediaServerItem.setRtpProxyPort(ablServerConfig.getRtpProxyPort());
+//        }
         mediaServerItem.setHookAliveInterval(10F);
     }
 
-    public void setZLMConfig(MediaServer mediaServerItem, boolean restart) {
-        logger.info("[濯掍綋鏈嶅姟鑺傜偣] 姝e湪璁剧疆 锛歿} -> {}:{}",
-                mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
-        String protocol = sslEnabled ? "https" : "http";
-        String hookPrefix = String.format("%s://%s:%s/index/hook", protocol, mediaServerItem.getHookIp(), serverPort);
-
-        Map<String, Object> param = new HashMap<>();
-        param.put("api.secret",mediaServerItem.getSecret()); // -profile:v Baseline
-        if (mediaServerItem.getRtspPort() != 0) {
-            param.put("ffmpeg.snap", "%s -rtsp_transport tcp -i %s -y -f mjpeg -frames:v 1 %s");
-        }
-        param.put("hook.enable","1");
-        param.put("hook.on_flow_report","");
-        param.put("hook.on_play",String.format("%s/on_play", hookPrefix));
-        param.put("hook.on_http_access","");
-        param.put("hook.on_publish", String.format("%s/on_publish", hookPrefix));
-        param.put("hook.on_record_ts","");
-        param.put("hook.on_rtsp_auth","");
-        param.put("hook.on_rtsp_realm","");
-        param.put("hook.on_server_started",String.format("%s/on_server_started", hookPrefix));
-        param.put("hook.on_shell_login","");
-        param.put("hook.on_stream_changed",String.format("%s/on_stream_changed", hookPrefix));
-        param.put("hook.on_stream_none_reader",String.format("%s/on_stream_none_reader", hookPrefix));
-        param.put("hook.on_stream_not_found",String.format("%s/on_stream_not_found", hookPrefix));
-        param.put("hook.on_server_keepalive",String.format("%s/on_server_keepalive", hookPrefix));
-        param.put("hook.on_send_rtp_stopped",String.format("%s/on_send_rtp_stopped", hookPrefix));
-        param.put("hook.on_rtp_server_timeout",String.format("%s/on_rtp_server_timeout", hookPrefix));
-        param.put("hook.on_record_mp4",String.format("%s/on_record_mp4", hookPrefix));
-        param.put("hook.timeoutSec","30");
-        param.put("hook.alive_interval", mediaServerItem.getHookAliveInterval());
-        // 鎺ㄦ祦鏂紑鍚庡彲浠ュ湪瓒呮椂鏃堕棿鍐呴噸鏂拌繛鎺ヤ笂缁х画鎺ㄦ祦锛岃繖鏍锋挱鏀惧櫒浼氭帴鐫�鎾斁銆�
-        // 缃�0鍏抽棴姝ょ壒鎬�(鎺ㄦ祦鏂紑浼氬鑷寸珛鍗虫柇寮�鎾斁鍣�)
-        // 姝ゅ弬鏁颁笉搴斿ぇ浜庢挱鏀惧櫒瓒呮椂鏃堕棿
-        // 浼樺寲姝ゆ秷鎭互鏇村揩鐨勬敹鍒版祦娉ㄩ攢浜嬩欢
-        param.put("protocol.continue_push_ms", "3000" );
-        // 鏈�澶氱瓑寰呮湭鍒濆鍖栫殑Track鏃堕棿锛屽崟浣嶆绉掞紝瓒呮椂涔嬪悗浼氬拷鐣ユ湭鍒濆鍖栫殑Track, 璁剧疆姝ら�夐」浼樺寲閭d簺闊抽閿欒鐨勪笉瑙勮寖娴侊紝
-        // 绛墇lm鏀寔缁欐瘡涓猺tpServer璁剧疆鍏抽棴闊抽鐨勬椂鍊欏彲浠ヤ笉璁剧疆姝ら�夐」
-        if (mediaServerItem.isRtpEnable() && !ObjectUtils.isEmpty(mediaServerItem.getRtpPortRange())) {
-            param.put("rtp_proxy.port_range", mediaServerItem.getRtpPortRange().replace(",", "-"));
-        }
-
-        if (!ObjectUtils.isEmpty(mediaServerItem.getRecordPath())) {
-            File recordPathFile = new File(mediaServerItem.getRecordPath());
-            param.put("protocol.mp4_save_path", recordPathFile.getParentFile().getPath());
-            param.put("protocol.downloadRoot", recordPathFile.getParentFile().getPath());
-            param.put("record.appName", recordPathFile.getName());
-        }
-
-        JSONObject responseJSON = ablResTfulUtils.setServerConfig(mediaServerItem, param);
-
-        if (responseJSON != null && responseJSON.getInteger("code") == 0) {
-            if (restart) {
-                logger.info("[濯掍綋鏈嶅姟鑺傜偣] 璁剧疆鎴愬姛,寮�濮嬮噸鍚互淇濊瘉閰嶇疆鐢熸晥 {} -> {}:{}",
-                        mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
-                ablResTfulUtils.restartServer(mediaServerItem);
-            }else {
-                logger.info("[濯掍綋鏈嶅姟鑺傜偣] 璁剧疆鎴愬姛 {} -> {}:{}",
-                        mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
+    public void setAblConfig(MediaServer mediaServerItem, boolean restart, AblServerConfig config) {
+        try {
+            if (config.getHookEnable() == 0) {
+                logger.info("[濯掍綋鏈嶅姟鑺傜偣-ABL]  寮�鍚疕OOK鍔熻兘 锛歿}", mediaServerItem.getId());
+                JSONObject responseJSON = ablResTfulUtils.setConfigParamValue(mediaServerItem, "hook_enable", "1");
+                if (responseJSON.getInteger("code") == 0) {
+                    logger.info("[濯掍綋鏈嶅姟鑺傜偣-ABL]  寮�鍚疕OOK鍔熻兘鎴愬姛 锛歿}", mediaServerItem.getId());
+                }else {
+                    logger.info("[濯掍綋鏈嶅姟鑺傜偣-ABL]  寮�鍚疕OOK鍔熻兘澶辫触 锛歿}->{}", mediaServerItem.getId(), responseJSON.getString("memo"));
+                }
             }
-        }else {
-            logger.info("[濯掍綋鏈嶅姟鑺傜偣] 璁剧疆濯掍綋鏈嶅姟鑺傜偣澶辫触 {} -> {}:{}",
-                    mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
+        }catch (Exception e) {
+            logger.info("[濯掍綋鏈嶅姟鑺傜偣-ABL]  寮�鍚疕OOK鍔熻兘澶辫触 锛歿}", mediaServerItem.getId(), e);
         }
+        // 璁剧疆鐩稿叧鐨凥OOK
+        String[] hookUrlArray = {
+                "on_stream_arrive",
+                "on_stream_none_reader",
+                "on_record_mp4",
+                "on_stream_disconnect",
+                "on_stream_not_found",
+                "on_server_started",
+                "on_publish",
+                "on_play",
+                "on_record_progress",
+                "on_server_keepalive",
+                "on_stream_not_arrive",
+                "on_delete_record_mp4",
+        };
+
+        String protocol = sslEnabled ? "https" : "http";
+        String hookPrefix = String.format("%s://%s:%s/index/hook/abl", protocol, mediaServerItem.getHookIp(), serverPort);
+        Field[] fields = AblServerConfig.class.getDeclaredFields();
+        for (Field field : fields) {
+            try {
+                if (field.isAnnotationPresent(ConfigKeyId.class)) {
+                    ConfigKeyId configKeyId = field.getAnnotation(ConfigKeyId.class);
+                    for (String hook : hookUrlArray) {
+                        if (configKeyId.value().equals(hook)) {
+                            String hookUrl =  String.format("%s/%s", hookPrefix, hook);
+                            field.setAccessible(true);
+                            // 鍒╃敤鍙嶅皠鑾峰彇鍊煎悗瀵规瘮鏄惁涓庨厤缃腑鐩稿悓锛屼笉鍚屽垯杩涜璁剧疆
+                            if (!hookUrl.equals(field.get(config))) {
+                                JSONObject responseJSON = ablResTfulUtils.setConfigParamValue(mediaServerItem, hook, hookUrl);
+                                if (responseJSON.getInteger("code") == 0) {
+                                    logger.info("[濯掍綋鏈嶅姟鑺傜偣-ABL]  璁剧疆HOOK {} 鎴愬姛 锛歿}", hook, mediaServerItem.getId());
+                                }else {
+                                    logger.info("[濯掍綋鏈嶅姟鑺傜偣-ABL]  璁剧疆HOOK {} 澶辫触 锛歿}->{}", hook, mediaServerItem.getId(), responseJSON.getString("memo"));
+                                }
+                            }
+                        }
+                    }
+                }
+            }catch (Exception e) {
+                logger.info("[濯掍綋鏈嶅姟鑺傜偣-ABL]  璁剧疆HOOK 澶辫触 锛歿}", mediaServerItem.getId(), e);
+            }
+        }
+
+
+
+
+//        Map<String, Object> param = new HashMap<>();
+//        param.put("api.secret",mediaServerItem.getSecret()); // -profile:v Baseline
+//        if (mediaServerItem.getRtspPort() != 0) {
+//            param.put("ffmpeg.snap", "%s -rtsp_transport tcp -i %s -y -f mjpeg -frames:v 1 %s");
+//        }
+//        param.put("hook.enable","1");
+//        param.put("hook.on_flow_report","");
+//        param.put("hook.on_play",String.format("%s/on_play", hookPrefix));
+//        param.put("hook.on_http_access","");
+//        param.put("hook.on_publish", String.format("%s/on_publish", hookPrefix));
+//        param.put("hook.on_record_ts","");
+//        param.put("hook.on_rtsp_auth","");
+//        param.put("hook.on_rtsp_realm","");
+//        param.put("hook.on_server_started",String.format("%s/on_server_started", hookPrefix));
+//        param.put("hook.on_shell_login","");
+//        param.put("hook.on_stream_changed",String.format("%s/on_stream_changed", hookPrefix));
+//        param.put("hook.on_stream_none_reader",String.format("%s/on_stream_none_reader", hookPrefix));
+//        param.put("hook.on_stream_not_found",String.format("%s/on_stream_not_found", hookPrefix));
+//        param.put("hook.on_server_keepalive",String.format("%s/on_server_keepalive", hookPrefix));
+//        param.put("hook.on_send_rtp_stopped",String.format("%s/on_send_rtp_stopped", hookPrefix));
+//        param.put("hook.on_rtp_server_timeout",String.format("%s/on_rtp_server_timeout", hookPrefix));
+//        param.put("hook.on_record_mp4",String.format("%s/on_record_mp4", hookPrefix));
+//        param.put("hook.timeoutSec","30");
+//        param.put("hook.alive_interval", mediaServerItem.getHookAliveInterval());
+//        // 鎺ㄦ祦鏂紑鍚庡彲浠ュ湪瓒呮椂鏃堕棿鍐呴噸鏂拌繛鎺ヤ笂缁х画鎺ㄦ祦锛岃繖鏍锋挱鏀惧櫒浼氭帴鐫�鎾斁銆�
+//        // 缃�0鍏抽棴姝ょ壒鎬�(鎺ㄦ祦鏂紑浼氬鑷寸珛鍗虫柇寮�鎾斁鍣�)
+//        // 姝ゅ弬鏁颁笉搴斿ぇ浜庢挱鏀惧櫒瓒呮椂鏃堕棿
+//        // 浼樺寲姝ゆ秷鎭互鏇村揩鐨勬敹鍒版祦娉ㄩ攢浜嬩欢
+//        param.put("protocol.continue_push_ms", "3000" );
+//        // 鏈�澶氱瓑寰呮湭鍒濆鍖栫殑Track鏃堕棿锛屽崟浣嶆绉掞紝瓒呮椂涔嬪悗浼氬拷鐣ユ湭鍒濆鍖栫殑Track, 璁剧疆姝ら�夐」浼樺寲閭d簺闊抽閿欒鐨勪笉瑙勮寖娴侊紝
+//        // 绛墇lm鏀寔缁欐瘡涓猺tpServer璁剧疆鍏抽棴闊抽鐨勬椂鍊欏彲浠ヤ笉璁剧疆姝ら�夐」
+//        if (mediaServerItem.isRtpEnable() && !ObjectUtils.isEmpty(mediaServerItem.getRtpPortRange())) {
+//            param.put("rtp_proxy.port_range", mediaServerItem.getRtpPortRange().replace(",", "-"));
+//        }
+//
+//        if (!ObjectUtils.isEmpty(mediaServerItem.getRecordPath())) {
+//            File recordPathFile = new File(mediaServerItem.getRecordPath());
+//            param.put("protocol.mp4_save_path", recordPathFile.getParentFile().getPath());
+//            param.put("protocol.downloadRoot", recordPathFile.getParentFile().getPath());
+//            param.put("record.appName", recordPathFile.getName());
+//        }
+//
+//        JSONObject responseJSON = ablResTfulUtils.setConfigParamValue(mediaServerItem, param);
+//
+//        if (responseJSON != null && responseJSON.getInteger("code") == 0) {
+//            if (restart) {
+//                logger.info("[濯掍綋鏈嶅姟鑺傜偣] 璁剧疆鎴愬姛,寮�濮嬮噸鍚互淇濊瘉閰嶇疆鐢熸晥 {} -> {}:{}",
+//                        mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
+//                ablResTfulUtils.restartServer(mediaServerItem);
+//            }else {
+//                logger.info("[濯掍綋鏈嶅姟鑺傜偣] 璁剧疆鎴愬姛 {} -> {}:{}",
+//                        mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
+//            }
+//        }else {
+//            logger.info("[濯掍綋鏈嶅姟鑺傜偣] 璁剧疆濯掍綋鏈嶅姟鑺傜偣澶辫触 {} -> {}:{}",
+//                    mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
+//        }
     }
 
 }

--
Gitblit v1.8.0