From 7ce4f44caa58b9020e26ffca4d11023b8e3e499a Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: Mon, 25 Mar 2024 00:12:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AEabl?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=B8=AD=E7=9A=84hook=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../media/abl/ABLMediaServerStatusManger.java | 175 ++++++++++++------ .../iot/vmp/media/abl/ABLRESTfulUtils.java | 73 +++++++- 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 de65b64f..dcb46554 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.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; @@ -22,6 +23,7 @@ import org.springframework.stereotype.Component; 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 class ABLMediaServerStatusManger { } public void setAblConfig(MediaServer mediaServerItem, boolean restart, AblServerConfig config) { - if (config.getHookEnable() == 0) { - logger.info("[媒体服务节点-ABL] 开启HOOK功能 :{}", mediaServerItem.getId()); - JSONObject responseJSON = ablResTfulUtils.setConfigParamValue(mediaServerItem, "hook_enable", "1"); + try { + if (config.getHookEnable() == 0) { + logger.info("[媒体服务节点-ABL] 开启HOOK功能 :{}", mediaServerItem.getId()); + JSONObject responseJSON = ablResTfulUtils.setConfigParamValue(mediaServerItem, "hook_enable", "1"); + if (responseJSON.getInteger("code") == 0) { + logger.info("[媒体服务节点-ABL] 开启HOOK功能成功 :{}", mediaServerItem.getId()); + }else { + logger.info("[媒体服务节点-ABL] 开启HOOK功能失败 :{}->{}", mediaServerItem.getId(), responseJSON.getString("memo")); + } + } + }catch (Exception e) { + logger.info("[媒体服务节点-ABL] 开启HOOK功能失败 :{}", mediaServerItem.getId(), e); } - logger.info("[媒体服务节点] 正在设置 :{} -> {}:{}", - mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); + // 设置相关的HOOK + 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 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, 设置此选项优化那些音频错误的不规范流, - // 等zlm支持给每个rtpServer设置关闭音频的时候可以不设置此选项 - 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 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, 设置此选项优化那些音频错误的不规范流, +// // 等zlm支持给每个rtpServer设置关闭音频的时候可以不设置此选项 +// 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 de686622..b76f59ef 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 @@ public class ABLRESTfulUtils { 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,15 +147,15 @@ public class ABLRESTfulUtils { @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())); } } }); @@ -163,6 +163,58 @@ public class ABLRESTfulUtils { + return responseJSON; + } + + public JSONObject sendGet(MediaServer mediaServerItem, String api, Map 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("请检查media配置并确认ABL已启动..."); + }catch (IOException e) { + logger.error(String.format("[ %s ]请求失败: %s", url, e.getMessage())); + } + + + return responseJSON; } @@ -211,8 +263,8 @@ public class ABLRESTfulUtils { } Objects.requireNonNull(response.body()).close(); } catch (ConnectException e) { - logger.error(String.format("连接ZLM失败: %s, %s", e.getCause().getMessage(), e.getMessage())); - logger.info("请检查media配置并确认ZLM已启动..."); + logger.error(String.format("连接ABL失败: %s, %s", e.getCause().getMessage(), e.getMessage())); + logger.info("请检查media配置并确认ABL已启动..."); } catch (IOException e) { logger.error(String.format("[ %s ]请求失败: %s", url, e.getMessage())); } @@ -286,7 +338,10 @@ public class ABLRESTfulUtils { } public JSONObject setConfigParamValue(MediaServer mediaServerItem, String key, Object value){ - return sendPost(mediaServerItem,"setConfigParamValue", param, null); + Map param = new HashMap<>(); + param.put("key", key); + param.put("value", value); + return sendGet(mediaServerItem,"setConfigParamValue", param); } public JSONObject openRtpServer(MediaServer mediaServerItem, Map param){