From 74ad4235f1738ea8ca954bd396a35ddf90a6e37b Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期六, 01 七月 2023 13:01:56 +0800 Subject: [PATCH] 修复tcp主动的推流 --- src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java | 47 +++++++++++++++++++++++++++++------------------ 1 files changed, 29 insertions(+), 18 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 d63a656..e39474f 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 @@ -11,9 +11,6 @@ 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 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 org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -177,9 +174,10 @@ Map<String, Object> param = new HashMap<>(); param.put("stream_id", streamId); JSONObject jsonObject = zlmresTfulUtils.closeRtpServer(serverItem, param); + logger.info("鍏抽棴RTP Server " + jsonObject); if (jsonObject != null ) { if (jsonObject.getInteger("code") == 0) { - result = jsonObject.getInteger("hit") == 1; + result = jsonObject.getInteger("hit") >= 1; }else { logger.error("鍏抽棴RTP Server 澶辫触: " + jsonObject.getString("msg")); } @@ -227,13 +225,14 @@ * @param tcp 鏄惁涓簍cp * @return SendRtpItem */ - public SendRtpItem createSendRtpItem(MediaServerItem serverItem, String ip, int port, String ssrc, String platformId, String deviceId, String channelId, boolean tcp, boolean rtcp){ + public SendRtpItem createSendRtpItem(MediaServerItem serverItem, String ip, int port, String ssrc, String platformId, + String deviceId, String channelId, boolean tcp, boolean rtcp, KeepPortCallback callback){ // 榛樿涓洪殢鏈虹鍙� int localPort = 0; if (userSetting.getGbSendStreamStrict()) { if (userSetting.getGbSendStreamStrict()) { - localPort = keepPort(serverItem, ssrc, localPort); + localPort = keepPort(serverItem, ssrc, localPort, callback); if (localPort == 0) { return null; } @@ -265,11 +264,12 @@ * @param tcp 鏄惁涓簍cp * @return SendRtpItem */ - public SendRtpItem createSendRtpItem(MediaServerItem serverItem, String ip, int port, String ssrc, String platformId, String app, String stream, String channelId, boolean tcp, boolean rtcp){ + public SendRtpItem createSendRtpItem(MediaServerItem serverItem, String ip, int port, String ssrc, String platformId, + String app, String stream, String channelId, boolean tcp, boolean rtcp, KeepPortCallback callback){ // 榛樿涓洪殢鏈虹鍙� int localPort = 0; if (userSetting.getGbSendStreamStrict()) { - localPort = keepPort(serverItem, ssrc, localPort); + localPort = keepPort(serverItem, ssrc, localPort, callback); if (localPort == 0) { return null; } @@ -290,10 +290,14 @@ return sendRtpItem; } + public interface KeepPortCallback{ + Boolean keep(String ssrc); + } + /** * 淇濇寔绔彛锛岀洿鍒伴渶瑕侀渶瑕佸彂娴佹椂鍐嶉噴鏀� */ - public int keepPort(MediaServerItem serverItem, String ssrc, Integer localPort) { + public int keepPort(MediaServerItem serverItem, String ssrc, int localPort, KeepPortCallback keepPortCallback) { Map<String, Object> param = new HashMap<>(3); param.put("port", localPort); param.put("enable_tcp", 1); @@ -302,21 +306,23 @@ if (jsonObject.getInteger("code") == 0) { localPort = jsonObject.getInteger("port"); HookSubscribeForRtpServerTimeout hookSubscribeForRtpServerTimeout = HookSubscribeFactory.on_rtp_server_timeout(ssrc, null, serverItem.getId()); - Integer finalLocalPort = localPort; + // 璁㈤槄 zlm鍚姩浜嬩欢, 鏂扮殑zlm涔熶細浠庤繖閲岃繘鍏ョ郴缁� + int finalLocalPort = localPort; hookSubscribe.addSubscribe(hookSubscribeForRtpServerTimeout, (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); + if (ssrc.equals(rtpServerTimeoutHookParam.getStream_id())) { + if (keepPortCallback.keep(ssrc)) { + logger.info("[涓婄骇鐐规挱] {}->鐩戝惉绔彛鍒版湡缁х画淇濇寔鐩戝惉", ssrc); + keepPort(serverItem, ssrc, finalLocalPort, keepPortCallback); + }else { + logger.info("[涓婄骇鐐规挱] {}->鍙戦�佸彇娑堬紝鏃犻渶缁х画鐩戝惉", ssrc); + releasePort(serverItem, ssrc); + } } }); - logger.info("[涓婄骇鐐规挱] {}->鐩戝惉绔彛: {}", ssrc, localPort); + logger.info("[涓婄骇鐐规挱] {}->: {}", ssrc, localPort); return localPort; }else { logger.info("[涓婄骇鐐规挱] 鐩戝惉绔彛澶辫触: {}->{}", ssrc, localPort); @@ -347,6 +353,11 @@ * 璋冪敤zlm RESTFUL API 鈥斺�� startSendRtpPassive */ public JSONObject startSendRtpPassive(MediaServerItem mediaServerItem, Map<String, Object>param) { + System.out.println("====================="); + for (String s : param.keySet()) { + System.out.println(s + ": " + param.get(s)); + } + System.out.println("=========END============"); return zlmresTfulUtils.startSendRtpPassive(mediaServerItem, param); } -- Gitblit v1.8.0