From 7ce4f44caa58b9020e26ffca4d11023b8e3e499a Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期一, 25 三月 2024 00:12:32 +0800
Subject: [PATCH] 支持设置abl配置中的hook信息

---
 src/main/java/com/genersoft/iot/vmp/media/abl/ABLRESTfulUtils.java            |   73 ++++++++++++-
 src/main/java/com/genersoft/iot/vmp/media/abl/ABLMediaServerStatusManger.java |  175 ++++++++++++++++++++++------------
 2 files changed, 178 insertions(+), 70 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 de65b64..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
@@ -5,6 +5,7 @@
 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;
@@ -22,6 +23,7 @@
 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.concurrent.ConcurrentHashMap;
@@ -232,72 +234,123 @@
     }
 
     public void setAblConfig(MediaServer mediaServerItem, boolean restart, AblServerConfig config) {
-        if (config.getHookEnable() == 0) {
-            logger.info("[濯掍綋鏈嶅姟鑺傜偣-ABL]  寮�鍚疕OOK鍔熻兘 锛歿}", mediaServerItem.getId());
-            JSONObject responseJSON = ablResTfulUtils.setConfigParamValue(mediaServerItem, "hook_enable", "1");
+        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"));
+                }
+            }
+        }catch (Exception e) {
+            logger.info("[濯掍綋鏈嶅姟鑺傜偣-ABL]  寮�鍚疕OOK鍔熻兘澶辫触 锛歿}", mediaServerItem.getId(), e);
         }
-        logger.info("[濯掍綋鏈嶅姟鑺傜偣] 姝e湪璁剧疆 锛歿} -> {}:{}",
-                mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
+        // 璁剧疆鐩稿叧鐨凥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);
-
-        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());
+        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);
             }
-        }else {
-            logger.info("[濯掍綋鏈嶅姟鑺傜偣] 璁剧疆濯掍綋鏈嶅姟鑺傜偣澶辫触 {} -> {}:{}",
-                    mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
         }
+
+
+
+
+//        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());
+//        }
     }
 
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/media/abl/ABLRESTfulUtils.java b/src/main/java/com/genersoft/iot/vmp/media/abl/ABLRESTfulUtils.java
index de68662..b76f59e 100755
--- a/src/main/java/com/genersoft/iot/vmp/media/abl/ABLRESTfulUtils.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/abl/ABLRESTfulUtils.java
@@ -116,15 +116,15 @@
 
                     if(e instanceof SocketTimeoutException){
                         //璇诲彇瓒呮椂瓒呮椂寮傚父
-                        logger.error(String.format("璇诲彇ZLM鏁版嵁瓒呮椂澶辫触: %s, %s", url, e.getMessage()));
+                        logger.error(String.format("璇诲彇ABL鏁版嵁瓒呮椂澶辫触: %s, %s", url, e.getMessage()));
                     }
                     if(e instanceof ConnectException){
                         //鍒ゆ柇杩炴帴寮傚父锛屾垜杩欓噷鏄姤Failed to connect to 10.7.5.144
-                        logger.error(String.format("杩炴帴ZLM杩炴帴澶辫触: %s, %s", url, e.getMessage()));
+                        logger.error(String.format("杩炴帴ABL杩炴帴澶辫触: %s, %s", url, e.getMessage()));
                     }
 
                 }catch (Exception e){
-                    logger.error(String.format("璁块棶ZLM澶辫触: %s, %s", url, e.getMessage()));
+                    logger.error(String.format("璁块棶ABL澶辫触: %s, %s", url, e.getMessage()));
                 }
             }else {
                 client.newCall(request).enqueue(new Callback(){
@@ -147,19 +147,71 @@
 
                     @Override
                     public void onFailure(@NotNull Call call, @NotNull IOException e) {
-                        logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", call.request().toString(), e.getMessage()));
+                        logger.error(String.format("杩炴帴ABL澶辫触: %s, %s", call.request().toString(), e.getMessage()));
 
                         if(e instanceof SocketTimeoutException){
                             //璇诲彇瓒呮椂瓒呮椂寮傚父
-                            logger.error(String.format("璇诲彇ZLM鏁版嵁澶辫触: %s, %s", call.request().toString(), e.getMessage()));
+                            logger.error(String.format("璇诲彇ABL鏁版嵁澶辫触: %s, %s", call.request().toString(), e.getMessage()));
                         }
                         if(e instanceof ConnectException){
                             //鍒ゆ柇杩炴帴寮傚父锛屾垜杩欓噷鏄姤Failed to connect to 10.7.5.144
-                            logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", call.request().toString(), e.getMessage()));
+                            logger.error(String.format("杩炴帴ABL澶辫触: %s, %s", call.request().toString(), e.getMessage()));
                         }
                     }
                 });
             }
+
+
+
+        return responseJSON;
+    }
+
+    public JSONObject sendGet(MediaServer mediaServerItem, String api, Map<String, Object> param) {
+        OkHttpClient client = getClient();
+
+        if (mediaServerItem == null) {
+            return null;
+        }
+        JSONObject responseJSON = null;
+        StringBuilder stringBuffer = new StringBuilder();
+        stringBuffer.append(String.format("http://%s:%s/index/api/%s",  mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api));
+        if (param != null && !param.keySet().isEmpty()) {
+            stringBuffer.append("?secret=").append(mediaServerItem.getSecret()).append("&");
+            int index = 1;
+            for (String key : param.keySet()){
+                if (param.get(key) != null) {
+                    stringBuffer.append(key + "=" + param.get(key));
+                    if (index < param.size()) {
+                        stringBuffer.append("&");
+                    }
+                }
+                index++;
+            }
+        }
+        String url = stringBuffer.toString();
+        logger.info("[璁块棶ABL]锛� {}", url);
+        Request request = new Request.Builder()
+                .get()
+                .url(url)
+                .build();
+        try {
+            Response response = client.newCall(request).execute();
+            if (response.isSuccessful()) {
+                ResponseBody responseBody = response.body();
+                if (responseBody != null) {
+                    String responseStr = responseBody.string();
+                    responseJSON = JSON.parseObject(responseStr);
+                }
+            }else {
+                response.close();
+                Objects.requireNonNull(response.body()).close();
+            }
+        } catch (ConnectException e) {
+            logger.error(String.format("杩炴帴ABL澶辫触: %s, %s", e.getCause().getMessage(), e.getMessage()));
+            logger.info("璇锋鏌edia閰嶇疆骞剁‘璁BL宸插惎鍔�...");
+        }catch (IOException e) {
+            logger.error(String.format("[ %s ]璇锋眰澶辫触: %s", url, e.getMessage()));
+        }
 
 
 
@@ -211,8 +263,8 @@
             }
             Objects.requireNonNull(response.body()).close();
         } catch (ConnectException e) {
-            logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", e.getCause().getMessage(), e.getMessage()));
-            logger.info("璇锋鏌edia閰嶇疆骞剁‘璁LM宸插惎鍔�...");
+            logger.error(String.format("杩炴帴ABL澶辫触: %s, %s", e.getCause().getMessage(), e.getMessage()));
+            logger.info("璇锋鏌edia閰嶇疆骞剁‘璁BL宸插惎鍔�...");
         } catch (IOException e) {
             logger.error(String.format("[ %s ]璇锋眰澶辫触: %s", url, e.getMessage()));
         }
@@ -286,7 +338,10 @@
     }
 
     public JSONObject setConfigParamValue(MediaServer mediaServerItem, String key, Object value){
-        return sendPost(mediaServerItem,"setConfigParamValue", param, null);
+        Map<String, Object> param =  new HashMap<>();
+        param.put("key", key);
+        param.put("value", value);
+        return sendGet(mediaServerItem,"setConfigParamValue", param);
     }
 
     public JSONObject openRtpServer(MediaServer mediaServerItem, Map<String, Object> param){

--
Gitblit v1.8.0