From a3d871022c68053ed5fa2c3d213fca7ec4d2136d Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: Fri, 21 Jul 2023 15:13:20 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=A9=BA=E6=8C=87?= =?UTF-8?q?=E9=92=88=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vmp/web/gb28181/ApiStreamController.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/web/gb28181/ApiStreamController.java b/src/main/java/com/genersoft/iot/vmp/web/gb28181/ApiStreamController.java index 8a1d079d..90b0c2f2 100644 --- a/src/main/java/com/genersoft/iot/vmp/web/gb28181/ApiStreamController.java +++ b/src/main/java/com/genersoft/iot/vmp/web/gb28181/ApiStreamController.java @@ -120,17 +120,34 @@ public class ApiStreamController { result.put("ChannelName", deviceChannel.getName()); result.put("ChannelCustomName", ""); result.put("FLV", streamInfo.getFlv().getUrl()); - result.put("HTTPS_FLV", streamInfo.getHttps_flv().getUrl()); + if (streamInfo.getHttps_flv() != null) { + result.put("HTTPS_FLV", streamInfo.getHttps_flv().getUrl()); + } + result.put("WS_FLV", streamInfo.getWs_flv().getUrl()); - result.put("WSS_FLV", streamInfo.getWss_flv().getUrl()); + if (streamInfo.getWss_flv() != null) { + result.put("WSS_FLV", streamInfo.getWss_flv().getUrl()); + } + result.put("RTMP", streamInfo.getRtmp().getUrl()); - result.put("RTMPS", streamInfo.getRtmps().getUrl()); + if (streamInfo.getRtmps() != null) { + result.put("RTMPS", streamInfo.getRtmps().getUrl()); + } + result.put("HLS", streamInfo.getHls().getUrl()); - result.put("HTTPS_HLS", streamInfo.getHttps_hls().getUrl()); + if (streamInfo.getHttps_hls() != null) { + result.put("HTTPS_HLS", streamInfo.getHttps_hls().getUrl()); + } result.put("RTSP", streamInfo.getRtsp().getUrl()); - result.put("RTSPS", streamInfo.getRtsps().getUrl()); + if (streamInfo.getRtsps() != null) { + result.put("RTSPS", streamInfo.getRtsps().getUrl()); + } + result.put("WEBRTC", streamInfo.getRtc().getUrl()); - result.put("HTTPS_WEBRTC", streamInfo.getRtcs().getUrl()); + if (streamInfo.getRtcs() != null) { + result.put("HTTPS_WEBRTC", streamInfo.getRtcs().getUrl()); + } + result.put("CDN", ""); result.put("SnapURL", ""); result.put("Transport", device.getTransport()); From 17ea37506eb736077f911c6d76e041db04e01b7e Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: Mon, 24 Jul 2023 09:06:49 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E5=88=86=E9=85=8D=E7=9A=84=E5=B9=B6=E5=8F=91=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iot/vmp/media/zlm/SendRtpPortManager.java | 93 ++++++++++++------- .../vmp/media/zlm/ZLMRTPServerFactory.java | 4 +- .../service/impl/MediaServerServiceImpl.java | 28 ------ .../iot/vmp/vmanager/rtp/RtpController.java | 4 +- 4 files changed, 61 insertions(+), 68 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/SendRtpPortManager.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/SendRtpPortManager.java index 5c857da8..f960c7dc 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/SendRtpPortManager.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/SendRtpPortManager.java @@ -3,12 +3,14 @@ package com.genersoft.iot.vmp.media.zlm; import com.genersoft.iot.vmp.common.VideoManagerConstants; import com.genersoft.iot.vmp.conf.UserSetting; import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem; -import com.genersoft.iot.vmp.media.zlm.dto.MediaSendRtpPortInfo; +import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; import com.genersoft.iot.vmp.utils.redis.RedisUtil; +import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.support.atomic.RedisAtomicInteger; import org.springframework.stereotype.Component; import java.util.HashMap; @@ -26,23 +28,14 @@ public class SendRtpPortManager { @Autowired private RedisTemplate redisTemplate; - private final String KEY = "VM_MEDIA_SEND_RTP_PORT_RANGE_"; + private final String KEY = "VM_MEDIA_SEND_RTP_PORT_"; - - public void initServerPort(String mediaServerId, int startPort, int endPort){ - String key = KEY + userSetting.getServerId() + "_" + mediaServerId; - MediaSendRtpPortInfo mediaSendRtpPortInfo = new MediaSendRtpPortInfo(startPort, endPort, mediaServerId); - redisTemplate.opsForValue().set(key, mediaSendRtpPortInfo); - } - - public int getNextPort(String mediaServerId) { - String sendIndexKey = KEY + userSetting.getServerId() + "_" + mediaServerId; - MediaSendRtpPortInfo mediaSendRtpPortInfo = (MediaSendRtpPortInfo)redisTemplate.opsForValue().get(sendIndexKey); - if (mediaSendRtpPortInfo == null) { - logger.warn("[发送端口管理] 获取{}的发送端口时未找到端口信息", mediaSendRtpPortInfo); - return 0; + public int getNextPort(MediaServerItem mediaServer) { + if (mediaServer == null) { + logger.warn("[发送端口管理] 参数错误,mediaServer为NULL"); + return -1; } - + String sendIndexKey = KEY + userSetting.getServerId() + "_" + mediaServer.getId(); String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX + userSetting.getServerId() + "_*"; List queryResult = RedisUtil.scan(redisTemplate, key); @@ -54,14 +47,39 @@ public class SendRtpPortManager { sendRtpItemMap.put(sendRtpItem.getLocalPort(), sendRtpItem); } } + String sendRtpPortRange = mediaServer.getSendRtpPortRange(); + int startPort; + int endPort; + if (sendRtpPortRange == null) { + logger.warn("{}未设置发送端口默认值,自动使用40000-50000作为端口范围", mediaServer.getId()); + String[] portArray = sendRtpPortRange.split(","); + if (portArray.length != 2 || !NumberUtils.isParsable(portArray[0]) || !NumberUtils.isParsable(portArray[1])) { + logger.warn("{}发送端口配置格式错误,自动使用40000-50000作为端口范围", mediaServer.getId()); + startPort = 50000; + endPort = 60000; + }else { - int port = getPort(mediaSendRtpPortInfo.getCurrent(), - mediaSendRtpPortInfo.getStart(), - mediaSendRtpPortInfo.getEnd(), checkPort -> sendRtpItemMap.get(checkPort) == null); - - mediaSendRtpPortInfo.setCurrent(port); - redisTemplate.opsForValue().set(sendIndexKey, mediaSendRtpPortInfo); - return port; + if ( Integer.parseInt(portArray[1]) - Integer.parseInt(portArray[0]) < 1) { + logger.warn("{}发送端口配置错误,结束端口至少比开始端口大一,自动使用40000-50000作为端口范围", mediaServer.getId()); + startPort = 50000; + endPort = 60000; + }else { + startPort = Integer.parseInt(portArray[0]); + endPort = Integer.parseInt(portArray[1]); + } + } + }else { + startPort = 50000; + endPort = 60000; + } + if (redisTemplate == null || redisTemplate.getConnectionFactory() == null) { + logger.warn("{}获取redis连接信息失败", mediaServer.getId()); + return -1; + } + RedisAtomicInteger redisAtomicInteger = new RedisAtomicInteger(sendIndexKey , redisTemplate.getConnectionFactory()); + return redisAtomicInteger.getAndUpdate((current)->{ + return getPort(current, startPort, endPort, checkPort-> !sendRtpItemMap.containsKey(checkPort)); + }); } interface CheckPortCallback{ @@ -69,22 +87,25 @@ public class SendRtpPortManager { } private int getPort(int current, int start, int end, CheckPortCallback checkPortCallback) { - int port; - if (current %2 != 0) { - port = current + 1; - }else { - port = current + 2; - } - if (port > end) { - if (start %2 != 0) { - port = start + 1; + if (current <= 0) { + if (start%2 == 0) { + current = start; }else { - port = start; + current = start + 1; + } + }else { + current += 2; + if (current > end) { + if (start%2 == 0) { + current = start; + }else { + current = start + 1; + } } } - if (!checkPortCallback.check(port)) { - return getPort(port, start, end, checkPortCallback); + if (!checkPortCallback.check(current)) { + return getPort(current + 2, start, end, checkPortCallback); } - return port; + return current; } } 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 82a0faf4..76ab7332 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 @@ -167,7 +167,7 @@ public class ZLMRTPServerFactory { int localPort = 0; if (userSetting.getGbSendStreamStrict()) { if (userSetting.getGbSendStreamStrict()) { - localPort = sendRtpPortManager.getNextPort(serverItem.getId()); + localPort = sendRtpPortManager.getNextPort(serverItem); if (localPort == 0) { return null; } @@ -204,7 +204,7 @@ public class ZLMRTPServerFactory { // 默认为随机端口 int localPort = 0; if (userSetting.getGbSendStreamStrict()) { - localPort = sendRtpPortManager.getNextPort(serverItem.getId()); + localPort = sendRtpPortManager.getNextPort(serverItem); if (localPort == 0) { return null; } diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java index 789974b8..630794b0 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java @@ -116,34 +116,6 @@ public class MediaServerServiceImpl implements IMediaServerService { if (ssrcFactory.hasMediaServerSSRC(mediaServerItem.getId())) { ssrcFactory.initMediaServerSSRC(mediaServerItem.getId(), null); } - if (userSetting.getGbSendStreamStrict()) { - int startPort = 50000; - int endPort = 60000; - String sendRtpPortRange = mediaServerItem.getSendRtpPortRange(); - if (sendRtpPortRange == null) { - logger.warn("[zlm] ] 未配置发流端口范围,默认使用50000到60000"); - }else { - String[] sendRtpPortRangeArray = sendRtpPortRange.trim().split(","); - if (sendRtpPortRangeArray.length != 2) { - logger.warn("[zlm] ] 发流端口范围错误,默认使用50000到60000"); - }else { - try { - startPort = Integer.parseInt(sendRtpPortRangeArray[0]); - endPort = Integer.parseInt(sendRtpPortRangeArray[1]); - if (endPort <= startPort) { - logger.warn("[zlm] ] 发流端口范围错误,结束端口应大于开始端口,使用默认端口"); - startPort = 50000; - endPort = 60000; - } - - }catch (NumberFormatException e) { - logger.warn("[zlm] ] 发流端口范围错误,默认使用50000到60000"); - } - } - } - logger.info("[[zlm] ] 配置发流端口范围,{}-{}", startPort, endPort); - sendRtpPortManager.initServerPort(mediaServerItem.getId(), startPort, endPort); - } // 查询redis是否存在此mediaServer String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerItem.getId(); Boolean hasKey = redisTemplate.hasKey(key); diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java index 18215223..ba4473f8 100644 --- a/src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java +++ b/src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java @@ -140,8 +140,8 @@ public class RtpController { if (isSend != null && isSend) { String key = VideoManagerConstants.WVP_OTHER_SEND_RTP_INFO + userSetting.getServerId() + "_" + callId; // 预创建发流信息 - int portForVideo = sendRtpPortManager.getNextPort(mediaServerItem.getId()); - int portForAudio = sendRtpPortManager.getNextPort(mediaServerItem.getId()); + int portForVideo = sendRtpPortManager.getNextPort(mediaServerItem); + int portForAudio = sendRtpPortManager.getNextPort(mediaServerItem); otherRtpSendInfo.setSendLocalIp(mediaServerItem.getSdpIp()); otherRtpSendInfo.setSendLocalPortForVideo(portForVideo); From 3de49d1c2a4385eba7c4a05952d7c9356a9bb603 Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: Mon, 31 Jul 2023 16:42:12 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8F=91=E6=B5=81?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../genersoft/iot/vmp/conf/UserSetting.java | 10 -------- .../vmp/media/zlm/ZLMRTPServerFactory.java | 23 ++++++------------- src/main/resources/all-application.yml | 3 --- 3 files changed, 7 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/conf/UserSetting.java b/src/main/java/com/genersoft/iot/vmp/conf/UserSetting.java index b3ccf773..aa98d58d 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/UserSetting.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/UserSetting.java @@ -43,8 +43,6 @@ public class UserSetting { private Boolean pushAuthority = Boolean.TRUE; - private Boolean gbSendStreamStrict = Boolean.FALSE; - private Boolean syncChannelOnDeviceOnline = Boolean.FALSE; private Boolean sipLog = Boolean.FALSE; @@ -204,14 +202,6 @@ public class UserSetting { this.pushAuthority = pushAuthority; } - public Boolean getGbSendStreamStrict() { - return gbSendStreamStrict; - } - - public void setGbSendStreamStrict(Boolean gbSendStreamStrict) { - this.gbSendStreamStrict = gbSendStreamStrict; - } - public Boolean getSyncChannelOnDeviceOnline() { return syncChannelOnDeviceOnline; } 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 76ab7332..0f0c139d 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 @@ -163,15 +163,9 @@ public class ZLMRTPServerFactory { public SendRtpItem createSendRtpItem(MediaServerItem serverItem, String ip, int port, String ssrc, String platformId, String deviceId, String channelId, boolean tcp, boolean rtcp){ - // 默认为随机端口 - int localPort = 0; - if (userSetting.getGbSendStreamStrict()) { - if (userSetting.getGbSendStreamStrict()) { - localPort = sendRtpPortManager.getNextPort(serverItem); - if (localPort == 0) { - return null; - } - } + int localPort = sendRtpPortManager.getNextPort(serverItem); + if (localPort == 0) { + return null; } SendRtpItem sendRtpItem = new SendRtpItem(); sendRtpItem.setIp(ip); @@ -201,13 +195,10 @@ public class ZLMRTPServerFactory { */ public SendRtpItem createSendRtpItem(MediaServerItem serverItem, String ip, int port, String ssrc, String platformId, String app, String stream, String channelId, boolean tcp, boolean rtcp){ - // 默认为随机端口 - int localPort = 0; - if (userSetting.getGbSendStreamStrict()) { - localPort = sendRtpPortManager.getNextPort(serverItem); - if (localPort == 0) { - return null; - } + + int localPort = sendRtpPortManager.getNextPort(serverItem); + if (localPort == 0) { + return null; } SendRtpItem sendRtpItem = new SendRtpItem(); sendRtpItem.setIp(ip); diff --git a/src/main/resources/all-application.yml b/src/main/resources/all-application.yml index ceaa82f7..8ad1d31c 100644 --- a/src/main/resources/all-application.yml +++ b/src/main/resources/all-application.yml @@ -170,9 +170,6 @@ user-settings: stream-on-demand: true # 推流鉴权, 默认开启 push-authority: true - # 国标级联发流严格模式,严格模式会使用与sdp信息中一致的端口发流,端口共享media.rtp.port-range,这会损失一些性能, - # 非严格模式使用随机端口发流,性能更好, 默认关闭 - gb-send-stream-strict: false # 设备上线时是否自动同步通道 sync-channel-on-device-online: false # 是否使用设备来源Ip作为回复IP, 不设置则为 false