Commit 74431b1e983b2f9c22d31eef596b2412f8f81641

Authored by 648540858
1 parent 8ef5e261

优化点播流程中ssrc的释放

src/main/java/com/genersoft/iot/vmp/gb28181/session/SSRCFactory.java
1 1 package com.genersoft.iot.vmp.gb28181.session;
2 2  
3 3 import com.genersoft.iot.vmp.conf.SipConfig;
  4 +import com.genersoft.iot.vmp.conf.UserSetting;
4 5 import org.springframework.beans.factory.annotation.Autowired;
5 6 import org.springframework.data.redis.core.StringRedisTemplate;
6 7 import org.springframework.stereotype.Component;
... ... @@ -31,10 +32,13 @@ public class SSRCFactory {
31 32 @Autowired
32 33 private SipConfig sipConfig;
33 34  
  35 + @Autowired
  36 + private UserSetting userSetting;
  37 +
34 38  
35 39 public void initMediaServerSSRC(String mediaServerId, Set<String> usedSet) {
36 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 42 List<String> ssrcList = new ArrayList<>();
39 43 for (int i = 1; i < MAX_STREAM_COUNT; i++) {
40 44 String ssrc = String.format("%s%04d", ssrcPrefix, i);
... ... @@ -77,7 +81,7 @@ public class SSRCFactory {
77 81 return;
78 82 }
79 83 String sn = ssrc.substring(1);
80   - String redisKey = SSRC_INFO_KEY + mediaServerId;
  84 + String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId;
81 85 redisTemplate.opsForSet().add(redisKey, sn);
82 86 }
83 87  
... ... @@ -86,7 +90,7 @@ public class SSRCFactory {
86 90 */
87 91 private String getSN(String mediaServerId) {
88 92 String sn = null;
89   - String redisKey = SSRC_INFO_KEY + mediaServerId;
  93 + String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId;
90 94 Long size = redisTemplate.opsForSet().size(redisKey);
91 95 if (size == null || size == 0) {
92 96 throw new RuntimeException("ssrc已经用完");
... ... @@ -113,20 +117,8 @@ public class SSRCFactory {
113 117 * @param mediaServerId 流媒体服务ID
114 118 */
115 119 public boolean hasMediaServerSSRC(String mediaServerId) {
116   - String redisKey = SSRC_INFO_KEY + mediaServerId;
  120 + String redisKey = SSRC_INFO_KEY + userSetting.getServerId() + "_" + mediaServerId;
117 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 228 ZlmHttpHookSubscribe.Event hookEvent, SipSubscribe.Event errorEvent,
229 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 240 String timeOutTaskKey = UUID.randomUUID().toString();
234 241 dynamicTask.startDelay(timeOutTaskKey, () -> {
... ... @@ -254,7 +261,7 @@ public class PlayServiceImpl implements IPlayService {
254 261 }, userSetting.getPlayTimeout());
255 262 //端口获取失败的ssrcInfo 没有必要发送点播指令
256 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 265 dynamicTask.stop(timeOutTaskKey);
259 266 // 释放ssrc
260 267 mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
... ... @@ -327,17 +334,8 @@ public class PlayServiceImpl implements IPlayService {
327 334 if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
328 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 339 // 单端口模式streamId也有变化,需要重新设置监听
342 340 if (!mediaServerItem.isRtpEnable()) {
343 341 // 添加订阅
... ... @@ -352,6 +350,7 @@ public class PlayServiceImpl implements IPlayService {
352 350 hookEvent.response(mediaServerItemInUse, response);
353 351 });
354 352 }
  353 +
355 354 // 关闭rtp server
356 355 mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream(), result->{
357 356 if (result) {
... ... @@ -367,8 +366,6 @@ public class PlayServiceImpl implements IPlayService {
367 366 }
368 367  
369 368 dynamicTask.stop(timeOutTaskKey);
370   - // 释放ssrc
371   - mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
372 369  
373 370 streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
374 371 event.msg = "下级自定义了ssrc,重新设置收流信息失败";
... ... @@ -590,17 +587,8 @@ public class PlayServiceImpl implements IPlayService {
590 587 if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
591 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 593 // 单端口模式streamId也有变化,需要重新设置监听
606 594 if (!mediaServerItem.isRtpEnable()) {
... ... @@ -752,16 +740,8 @@ public class PlayServiceImpl implements IPlayService {
752 740 if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
753 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 746 // 单端口模式streamId也有变化,需要重新设置监听
767 747 if (!mediaServerItem.isRtpEnable()) {
... ...