优化点播流程中ssrc的释放

pull/887/head
648540858 2023-06-02 15:46:07 +08:00
parent 8ef5e2618d
commit 74431b1e98
2 changed files with 24 additions and 52 deletions

View File

@ -1,6 +1,7 @@
package com.genersoft.iot.vmp.gb28181.session; package com.genersoft.iot.vmp.gb28181.session;
import com.genersoft.iot.vmp.conf.SipConfig; import com.genersoft.iot.vmp.conf.SipConfig;
import com.genersoft.iot.vmp.conf.UserSetting;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -31,10 +32,13 @@ public class SSRCFactory {
@Autowired @Autowired
private SipConfig sipConfig; private SipConfig sipConfig;
@Autowired
private UserSetting userSetting;
public void initMediaServerSSRC(String mediaServerId, Set<String> usedSet) { public void initMediaServerSSRC(String mediaServerId, Set<String> usedSet) {
String ssrcPrefix = sipConfig.getDomain().substring(3, 8); String ssrcPrefix = sipConfig.getDomain().substring(3, 8);
String redisKey = SSRC_INFO_KEY + mediaServerId; String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId;
List<String> ssrcList = new ArrayList<>(); List<String> ssrcList = new ArrayList<>();
for (int i = 1; i < MAX_STREAM_COUNT; i++) { for (int i = 1; i < MAX_STREAM_COUNT; i++) {
String ssrc = String.format("%s%04d", ssrcPrefix, i); String ssrc = String.format("%s%04d", ssrcPrefix, i);
@ -77,7 +81,7 @@ public class SSRCFactory {
return; return;
} }
String sn = ssrc.substring(1); String sn = ssrc.substring(1);
String redisKey = SSRC_INFO_KEY + mediaServerId; String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId;
redisTemplate.opsForSet().add(redisKey, sn); redisTemplate.opsForSet().add(redisKey, sn);
} }
@ -86,7 +90,7 @@ public class SSRCFactory {
*/ */
private String getSN(String mediaServerId) { private String getSN(String mediaServerId) {
String sn = null; String sn = null;
String redisKey = SSRC_INFO_KEY + mediaServerId; String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId;
Long size = redisTemplate.opsForSet().size(redisKey); Long size = redisTemplate.opsForSet().size(redisKey);
if (size == null || size == 0) { if (size == null || size == 0) {
throw new RuntimeException("ssrc已经用完"); throw new RuntimeException("ssrc已经用完");
@ -113,20 +117,8 @@ public class SSRCFactory {
* @param mediaServerId ID * @param mediaServerId ID
*/ */
public boolean hasMediaServerSSRC(String mediaServerId) { 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; 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;
}
} }

View File

@ -228,7 +228,14 @@ public class PlayServiceImpl implements IPlayService {
ZlmHttpHookSubscribe.Event hookEvent, SipSubscribe.Event errorEvent, ZlmHttpHookSubscribe.Event hookEvent, SipSubscribe.Event errorEvent,
InviteTimeOutCallback timeoutCallback) { 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(); String timeOutTaskKey = UUID.randomUUID().toString();
dynamicTask.startDelay(timeOutTaskKey, () -> { dynamicTask.startDelay(timeOutTaskKey, () -> {
@ -254,7 +261,7 @@ public class PlayServiceImpl implements IPlayService {
}, userSetting.getPlayTimeout()); }, userSetting.getPlayTimeout());
//端口获取失败的ssrcInfo 没有必要发送点播指令 //端口获取失败的ssrcInfo 没有必要发送点播指令
if (ssrcInfo.getPort() <= 0) { 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); dynamicTask.stop(timeOutTaskKey);
// 释放ssrc // 释放ssrc
mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
@ -327,17 +334,8 @@ public class PlayServiceImpl implements IPlayService {
if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) { if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
logger.info("[点播消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse); logger.info("[点播消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
if (!ssrcFactory.checkSsrc(mediaServerItem.getId(),ssrcInResponse)) { // 释放不被使用的ssrc
// ssrc 不可用 mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
// 释放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;
}
// 单端口模式streamId也有变化需要重新设置监听 // 单端口模式streamId也有变化需要重新设置监听
if (!mediaServerItem.isRtpEnable()) { if (!mediaServerItem.isRtpEnable()) {
// 添加订阅 // 添加订阅
@ -352,6 +350,7 @@ public class PlayServiceImpl implements IPlayService {
hookEvent.response(mediaServerItemInUse, response); hookEvent.response(mediaServerItemInUse, response);
}); });
} }
// 关闭rtp server // 关闭rtp server
mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream(), result->{ mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream(), result->{
if (result) { if (result) {
@ -367,8 +366,6 @@ public class PlayServiceImpl implements IPlayService {
} }
dynamicTask.stop(timeOutTaskKey); dynamicTask.stop(timeOutTaskKey);
// 释放ssrc
mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
event.msg = "下级自定义了ssrc,重新设置收流信息失败"; event.msg = "下级自定义了ssrc,重新设置收流信息失败";
@ -590,17 +587,8 @@ public class PlayServiceImpl implements IPlayService {
if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) { if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
logger.info("[回放消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse); logger.info("[回放消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
if (!ssrcFactory.checkSsrc(mediaServerItem.getId(),ssrcInResponse)) { // 释放不被使用的ssrc
// ssrc 不可用
// 释放ssrc
dynamicTask.stop(playBackTimeOutTaskKey);
mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
eventResult.msg = "下级自定义了ssrc,但是此ssrc不可用";
eventResult.statusCode = 400;
errorEvent.response(eventResult);
return;
}
// 单端口模式streamId也有变化需要重新设置监听 // 单端口模式streamId也有变化需要重新设置监听
if (!mediaServerItem.isRtpEnable()) { if (!mediaServerItem.isRtpEnable()) {
@ -752,16 +740,8 @@ public class PlayServiceImpl implements IPlayService {
if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) { if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
logger.info("[录像下载] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse); logger.info("[录像下载] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
if (!ssrcFactory.checkSsrc(mediaServerItem.getId(),ssrcInResponse)) { // 释放不被使用的ssrc
// ssrc 不可用
// 释放ssrc
mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
eventResult.msg = "下级自定义了ssrc,但是此ssrc不可用";
eventResult.statusCode = 400;
errorEvent.response(eventResult);
return;
}
// 单端口模式streamId也有变化需要重新设置监听 // 单端口模式streamId也有变化需要重新设置监听
if (!mediaServerItem.isRtpEnable()) { if (!mediaServerItem.isRtpEnable()) {