Commit 74431b1e983b2f9c22d31eef596b2412f8f81641

Authored by 648540858
1 parent 8ef5e261

优化点播流程中ssrc的释放

src/main/java/com/genersoft/iot/vmp/gb28181/session/SSRCFactory.java
1 package com.genersoft.iot.vmp.gb28181.session; 1 package com.genersoft.iot.vmp.gb28181.session;
2 2
3 import com.genersoft.iot.vmp.conf.SipConfig; 3 import com.genersoft.iot.vmp.conf.SipConfig;
  4 +import com.genersoft.iot.vmp.conf.UserSetting;
4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.data.redis.core.StringRedisTemplate; 6 import org.springframework.data.redis.core.StringRedisTemplate;
6 import org.springframework.stereotype.Component; 7 import org.springframework.stereotype.Component;
@@ -31,10 +32,13 @@ public class SSRCFactory { @@ -31,10 +32,13 @@ public class SSRCFactory {
31 @Autowired 32 @Autowired
32 private SipConfig sipConfig; 33 private SipConfig sipConfig;
33 34
  35 + @Autowired
  36 + private UserSetting userSetting;
  37 +
34 38
35 public void initMediaServerSSRC(String mediaServerId, Set<String> usedSet) { 39 public void initMediaServerSSRC(String mediaServerId, Set<String> usedSet) {
36 String ssrcPrefix = sipConfig.getDomain().substring(3, 8); 40 String ssrcPrefix = sipConfig.getDomain().substring(3, 8);
37 - String redisKey = SSRC_INFO_KEY + mediaServerId; 41 + String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId;
38 List<String> ssrcList = new ArrayList<>(); 42 List<String> ssrcList = new ArrayList<>();
39 for (int i = 1; i < MAX_STREAM_COUNT; i++) { 43 for (int i = 1; i < MAX_STREAM_COUNT; i++) {
40 String ssrc = String.format("%s%04d", ssrcPrefix, i); 44 String ssrc = String.format("%s%04d", ssrcPrefix, i);
@@ -77,7 +81,7 @@ public class SSRCFactory { @@ -77,7 +81,7 @@ public class SSRCFactory {
77 return; 81 return;
78 } 82 }
79 String sn = ssrc.substring(1); 83 String sn = ssrc.substring(1);
80 - String redisKey = SSRC_INFO_KEY + mediaServerId; 84 + String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId;
81 redisTemplate.opsForSet().add(redisKey, sn); 85 redisTemplate.opsForSet().add(redisKey, sn);
82 } 86 }
83 87
@@ -86,7 +90,7 @@ public class SSRCFactory { @@ -86,7 +90,7 @@ public class SSRCFactory {
86 */ 90 */
87 private String getSN(String mediaServerId) { 91 private String getSN(String mediaServerId) {
88 String sn = null; 92 String sn = null;
89 - String redisKey = SSRC_INFO_KEY + mediaServerId; 93 + String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId;
90 Long size = redisTemplate.opsForSet().size(redisKey); 94 Long size = redisTemplate.opsForSet().size(redisKey);
91 if (size == null || size == 0) { 95 if (size == null || size == 0) {
92 throw new RuntimeException("ssrc已经用完"); 96 throw new RuntimeException("ssrc已经用完");
@@ -113,20 +117,8 @@ public class SSRCFactory { @@ -113,20 +117,8 @@ public class SSRCFactory {
113 * @param mediaServerId 流媒体服务ID 117 * @param mediaServerId 流媒体服务ID
114 */ 118 */
115 public boolean hasMediaServerSSRC(String mediaServerId) { 119 public boolean hasMediaServerSSRC(String mediaServerId) {
116 - String redisKey = SSRC_INFO_KEY + mediaServerId; 120 + String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId;
117 return redisTemplate.opsForSet().members(redisKey) != null; 121 return redisTemplate.opsForSet().members(redisKey) != null;
118 } 122 }
119 123
120 - /**  
121 - * 查询ssrc是否可用  
122 - *  
123 - * @param mediaServerId  
124 - * @param ssrc  
125 - * @return  
126 - */  
127 - public boolean checkSsrc(String mediaServerId, String ssrc) {  
128 - String sn = ssrc.substring(1);  
129 - String redisKey = SSRC_INFO_KEY + mediaServerId;  
130 - return redisTemplate.opsForSet().isMember(redisKey, sn) != null;  
131 - }  
132 } 124 }
src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
@@ -228,7 +228,14 @@ public class PlayServiceImpl implements IPlayService { @@ -228,7 +228,14 @@ public class PlayServiceImpl implements IPlayService {
228 ZlmHttpHookSubscribe.Event hookEvent, SipSubscribe.Event errorEvent, 228 ZlmHttpHookSubscribe.Event hookEvent, SipSubscribe.Event errorEvent,
229 InviteTimeOutCallback timeoutCallback) { 229 InviteTimeOutCallback timeoutCallback) {
230 230
231 - logger.info("[点播开始] deviceId: {}, channelId: {},收流端口: {}, 收流模式:{}, SSRC: {}, SSRC校验:{}", device.getDeviceId(), channelId, ssrcInfo.getPort(), device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck()); 231 + logger.info("\r\n" +
  232 + "[点播开始] \r\n" +
  233 + "deviceId : {}, \r\n" +
  234 + "channelId : {},\r\n" +
  235 + "收流端口 :{}, \r\n" +
  236 + "收流模式 :{}, \r\n" +
  237 + "SSRC : {}, \r\n" +
  238 + "SSRC校验 :{} ", device.getDeviceId(), channelId, ssrcInfo.getPort(), device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck());
232 // 超时处理 239 // 超时处理
233 String timeOutTaskKey = UUID.randomUUID().toString(); 240 String timeOutTaskKey = UUID.randomUUID().toString();
234 dynamicTask.startDelay(timeOutTaskKey, () -> { 241 dynamicTask.startDelay(timeOutTaskKey, () -> {
@@ -254,7 +261,7 @@ public class PlayServiceImpl implements IPlayService { @@ -254,7 +261,7 @@ public class PlayServiceImpl implements IPlayService {
254 }, userSetting.getPlayTimeout()); 261 }, userSetting.getPlayTimeout());
255 //端口获取失败的ssrcInfo 没有必要发送点播指令 262 //端口获取失败的ssrcInfo 没有必要发送点播指令
256 if (ssrcInfo.getPort() <= 0) { 263 if (ssrcInfo.getPort() <= 0) {
257 - logger.info("[点播端口分配异常],deviceId={},channelId={},ssrcInfo={}", device.getDeviceId(), channelId, ssrcInfo); 264 + logger.info("[点播端口分配异常],deviceId={},channelId={},ssrcInfo={}", device.getDeviceId(), channelId, JSON.toJSONString(ssrcInfo));
258 dynamicTask.stop(timeOutTaskKey); 265 dynamicTask.stop(timeOutTaskKey);
259 // 释放ssrc 266 // 释放ssrc
260 mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); 267 mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
@@ -327,17 +334,8 @@ public class PlayServiceImpl implements IPlayService { @@ -327,17 +334,8 @@ public class PlayServiceImpl implements IPlayService {
327 if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) { 334 if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
328 logger.info("[点播消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse); 335 logger.info("[点播消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
329 336
330 - if (!ssrcFactory.checkSsrc(mediaServerItem.getId(),ssrcInResponse)) {  
331 - // ssrc 不可用  
332 - // 释放ssrc  
333 - ssrcFactory.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());  
334 - streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());  
335 - event.msg = "下级自定义了ssrc,但是此ssrc不可用";  
336 - event.statusCode = 400;  
337 - errorEvent.response(event);  
338 - return;  
339 - }  
340 - 337 + // 释放不被使用的ssrc
  338 + mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
341 // 单端口模式streamId也有变化,需要重新设置监听 339 // 单端口模式streamId也有变化,需要重新设置监听
342 if (!mediaServerItem.isRtpEnable()) { 340 if (!mediaServerItem.isRtpEnable()) {
343 // 添加订阅 341 // 添加订阅
@@ -352,6 +350,7 @@ public class PlayServiceImpl implements IPlayService { @@ -352,6 +350,7 @@ public class PlayServiceImpl implements IPlayService {
352 hookEvent.response(mediaServerItemInUse, response); 350 hookEvent.response(mediaServerItemInUse, response);
353 }); 351 });
354 } 352 }
  353 +
355 // 关闭rtp server 354 // 关闭rtp server
356 mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream(), result->{ 355 mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream(), result->{
357 if (result) { 356 if (result) {
@@ -367,8 +366,6 @@ public class PlayServiceImpl implements IPlayService { @@ -367,8 +366,6 @@ public class PlayServiceImpl implements IPlayService {
367 } 366 }
368 367
369 dynamicTask.stop(timeOutTaskKey); 368 dynamicTask.stop(timeOutTaskKey);
370 - // 释放ssrc  
371 - mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());  
372 369
373 streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); 370 streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
374 event.msg = "下级自定义了ssrc,重新设置收流信息失败"; 371 event.msg = "下级自定义了ssrc,重新设置收流信息失败";
@@ -590,17 +587,8 @@ public class PlayServiceImpl implements IPlayService { @@ -590,17 +587,8 @@ public class PlayServiceImpl implements IPlayService {
590 if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) { 587 if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
591 logger.info("[回放消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse); 588 logger.info("[回放消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
592 589
593 - if (!ssrcFactory.checkSsrc(mediaServerItem.getId(),ssrcInResponse)) {  
594 - // ssrc 不可用  
595 - // 释放ssrc  
596 - dynamicTask.stop(playBackTimeOutTaskKey);  
597 - mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());  
598 - streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());  
599 - eventResult.msg = "下级自定义了ssrc,但是此ssrc不可用";  
600 - eventResult.statusCode = 400;  
601 - errorEvent.response(eventResult);  
602 - return;  
603 - } 590 + // 释放不被使用的ssrc
  591 + mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
604 592
605 // 单端口模式streamId也有变化,需要重新设置监听 593 // 单端口模式streamId也有变化,需要重新设置监听
606 if (!mediaServerItem.isRtpEnable()) { 594 if (!mediaServerItem.isRtpEnable()) {
@@ -752,16 +740,8 @@ public class PlayServiceImpl implements IPlayService { @@ -752,16 +740,8 @@ public class PlayServiceImpl implements IPlayService {
752 if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) { 740 if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
753 logger.info("[录像下载] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse); 741 logger.info("[录像下载] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
754 742
755 - if (!ssrcFactory.checkSsrc(mediaServerItem.getId(),ssrcInResponse)) {  
756 - // ssrc 不可用  
757 - // 释放ssrc  
758 - mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());  
759 - streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());  
760 - eventResult.msg = "下级自定义了ssrc,但是此ssrc不可用";  
761 - eventResult.statusCode = 400;  
762 - errorEvent.response(eventResult);  
763 - return;  
764 - } 743 + // 释放不被使用的ssrc
  744 + mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
765 745
766 // 单端口模式streamId也有变化,需要重新设置监听 746 // 单端口模式streamId也有变化,需要重新设置监听
767 if (!mediaServerItem.isRtpEnable()) { 747 if (!mediaServerItem.isRtpEnable()) {