From 74431b1e983b2f9c22d31eef596b2412f8f81641 Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: Fri, 2 Jun 2023 15:46:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=82=B9=E6=92=AD=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E4=B8=ADssrc=E7=9A=84=E9=87=8A=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iot/vmp/gb28181/session/SSRCFactory.java | 24 +++------ .../iot/vmp/service/impl/PlayServiceImpl.java | 52 ++++++------------- 2 files changed, 24 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/session/SSRCFactory.java b/src/main/java/com/genersoft/iot/vmp/gb28181/session/SSRCFactory.java index ec8e0ba6..657bb2fa 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/session/SSRCFactory.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/session/SSRCFactory.java @@ -1,6 +1,7 @@ package com.genersoft.iot.vmp.gb28181.session; import com.genersoft.iot.vmp.conf.SipConfig; +import com.genersoft.iot.vmp.conf.UserSetting; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; @@ -31,10 +32,13 @@ public class SSRCFactory { @Autowired private SipConfig sipConfig; + @Autowired + private UserSetting userSetting; + public void initMediaServerSSRC(String mediaServerId, Set usedSet) { String ssrcPrefix = sipConfig.getDomain().substring(3, 8); - String redisKey = SSRC_INFO_KEY + mediaServerId; + String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId; List ssrcList = new ArrayList<>(); for (int i = 1; i < MAX_STREAM_COUNT; i++) { String ssrc = String.format("%s%04d", ssrcPrefix, i); @@ -77,7 +81,7 @@ public class SSRCFactory { return; } String sn = ssrc.substring(1); - String redisKey = SSRC_INFO_KEY + mediaServerId; + String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId; redisTemplate.opsForSet().add(redisKey, sn); } @@ -86,7 +90,7 @@ public class SSRCFactory { */ private String getSN(String mediaServerId) { String sn = null; - String redisKey = SSRC_INFO_KEY + mediaServerId; + String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId; Long size = redisTemplate.opsForSet().size(redisKey); if (size == null || size == 0) { throw new RuntimeException("ssrc已经用完"); @@ -113,20 +117,8 @@ public class SSRCFactory { * @param mediaServerId 流媒体服务ID */ public boolean hasMediaServerSSRC(String mediaServerId) { - String redisKey = SSRC_INFO_KEY + mediaServerId; + String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId; return redisTemplate.opsForSet().members(redisKey) != null; } - /** - * 查询ssrc是否可用 - * - * @param mediaServerId - * @param ssrc - * @return - */ - public boolean checkSsrc(String mediaServerId, String ssrc) { - String sn = ssrc.substring(1); - String redisKey = SSRC_INFO_KEY + mediaServerId; - return redisTemplate.opsForSet().isMember(redisKey, sn) != null; - } } diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java index 51574392..2fbe6bbc 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java @@ -228,7 +228,14 @@ public class PlayServiceImpl implements IPlayService { ZlmHttpHookSubscribe.Event hookEvent, SipSubscribe.Event errorEvent, InviteTimeOutCallback timeoutCallback) { - logger.info("[点播开始] deviceId: {}, channelId: {},收流端口: {}, 收流模式:{}, SSRC: {}, SSRC校验:{}", device.getDeviceId(), channelId, ssrcInfo.getPort(), device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck()); + logger.info("\r\n" + + "[点播开始] \r\n" + + "deviceId : {}, \r\n" + + "channelId : {},\r\n" + + "收流端口 :{}, \r\n" + + "收流模式 :{}, \r\n" + + "SSRC : {}, \r\n" + + "SSRC校验 :{} ", device.getDeviceId(), channelId, ssrcInfo.getPort(), device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck()); // 超时处理 String timeOutTaskKey = UUID.randomUUID().toString(); dynamicTask.startDelay(timeOutTaskKey, () -> { @@ -254,7 +261,7 @@ public class PlayServiceImpl implements IPlayService { }, userSetting.getPlayTimeout()); //端口获取失败的ssrcInfo 没有必要发送点播指令 if (ssrcInfo.getPort() <= 0) { - logger.info("[点播端口分配异常],deviceId={},channelId={},ssrcInfo={}", device.getDeviceId(), channelId, ssrcInfo); + logger.info("[点播端口分配异常],deviceId={},channelId={},ssrcInfo={}", device.getDeviceId(), channelId, JSON.toJSONString(ssrcInfo)); dynamicTask.stop(timeOutTaskKey); // 释放ssrc mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); @@ -327,17 +334,8 @@ public class PlayServiceImpl implements IPlayService { if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) { logger.info("[点播消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse); - if (!ssrcFactory.checkSsrc(mediaServerItem.getId(),ssrcInResponse)) { - // ssrc 不可用 - // 释放ssrc - ssrcFactory.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); - streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); - event.msg = "下级自定义了ssrc,但是此ssrc不可用"; - event.statusCode = 400; - errorEvent.response(event); - return; - } - + // 释放不被使用的ssrc + mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); // 单端口模式streamId也有变化,需要重新设置监听 if (!mediaServerItem.isRtpEnable()) { // 添加订阅 @@ -352,6 +350,7 @@ public class PlayServiceImpl implements IPlayService { hookEvent.response(mediaServerItemInUse, response); }); } + // 关闭rtp server mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream(), result->{ if (result) { @@ -367,8 +366,6 @@ public class PlayServiceImpl implements IPlayService { } dynamicTask.stop(timeOutTaskKey); - // 释放ssrc - mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); event.msg = "下级自定义了ssrc,重新设置收流信息失败"; @@ -590,17 +587,8 @@ public class PlayServiceImpl implements IPlayService { if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) { logger.info("[回放消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse); - if (!ssrcFactory.checkSsrc(mediaServerItem.getId(),ssrcInResponse)) { - // ssrc 不可用 - // 释放ssrc - dynamicTask.stop(playBackTimeOutTaskKey); - mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); - streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); - eventResult.msg = "下级自定义了ssrc,但是此ssrc不可用"; - eventResult.statusCode = 400; - errorEvent.response(eventResult); - return; - } + // 释放不被使用的ssrc + mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); // 单端口模式streamId也有变化,需要重新设置监听 if (!mediaServerItem.isRtpEnable()) { @@ -752,16 +740,8 @@ public class PlayServiceImpl implements IPlayService { if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) { logger.info("[录像下载] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse); - if (!ssrcFactory.checkSsrc(mediaServerItem.getId(),ssrcInResponse)) { - // ssrc 不可用 - // 释放ssrc - mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); - streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); - eventResult.msg = "下级自定义了ssrc,但是此ssrc不可用"; - eventResult.statusCode = 400; - errorEvent.response(eventResult); - return; - } + // 释放不被使用的ssrc + mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); // 单端口模式streamId也有变化,需要重新设置监听 if (!mediaServerItem.isRtpEnable()) {