Commit c42d391290adc50348807d623c4b689ab1f109fc

Authored by 648540858
1 parent 9fc3db1f

修复数据存储

src/main/java/com/genersoft/iot/vmp/media/zlm/SendRtpPortManager.java
1 package com.genersoft.iot.vmp.media.zlm; 1 package com.genersoft.iot.vmp.media.zlm;
2 2
  3 +import com.genersoft.iot.vmp.common.VideoManagerConstants;
3 import com.genersoft.iot.vmp.conf.UserSetting; 4 import com.genersoft.iot.vmp.conf.UserSetting;
  5 +import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
4 import com.genersoft.iot.vmp.media.zlm.dto.MediaSendRtpPortInfo; 6 import com.genersoft.iot.vmp.media.zlm.dto.MediaSendRtpPortInfo;
  7 +import com.genersoft.iot.vmp.utils.redis.RedisUtil;
5 import org.slf4j.Logger; 8 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory; 9 import org.slf4j.LoggerFactory;
7 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.data.redis.core.RedisTemplate; 11 import org.springframework.data.redis.core.RedisTemplate;
9 import org.springframework.stereotype.Component; 12 import org.springframework.stereotype.Component;
10 13
  14 +import java.util.HashMap;
  15 +import java.util.List;
  16 +import java.util.Map;
  17 +
11 @Component 18 @Component
12 public class SendRtpPortManager { 19 public class SendRtpPortManager {
13 20
@@ -29,27 +36,54 @@ public class SendRtpPortManager { @@ -29,27 +36,54 @@ public class SendRtpPortManager {
29 } 36 }
30 37
31 public int getNextPort(String mediaServerId) { 38 public int getNextPort(String mediaServerId) {
32 - String key = KEY + userSetting.getServerId() + "_" + mediaServerId;  
33 - MediaSendRtpPortInfo mediaSendRtpPortInfo = (MediaSendRtpPortInfo)redisTemplate.opsForValue().get(key); 39 + String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX
  40 + + userSetting.getServerId() + "_*";
  41 + List<Object> queryResult = RedisUtil.scan(redisTemplate, key);
  42 + Map<Integer, SendRtpItem> sendRtpItemMap = new HashMap<>();
  43 +
  44 + for (Object o : queryResult) {
  45 + SendRtpItem sendRtpItem = (SendRtpItem) redisTemplate.opsForValue().get(o);
  46 + if (sendRtpItem != null) {
  47 + sendRtpItemMap.put(sendRtpItem.getLocalPort(), sendRtpItem);
  48 + }
  49 + }
  50 +
  51 + String sendIndexKey = KEY + userSetting.getServerId() + "_" + mediaServerId;
  52 + MediaSendRtpPortInfo mediaSendRtpPortInfo = (MediaSendRtpPortInfo)redisTemplate.opsForValue().get(sendIndexKey);
34 if (mediaSendRtpPortInfo == null) { 53 if (mediaSendRtpPortInfo == null) {
35 logger.warn("[发送端口管理] 获取{}的发送端口时未找到端口信息", mediaSendRtpPortInfo); 54 logger.warn("[发送端口管理] 获取{}的发送端口时未找到端口信息", mediaSendRtpPortInfo);
36 return 0; 55 return 0;
37 } 56 }
  57 + int port = getPort(mediaSendRtpPortInfo.getCurrent(),
  58 + mediaSendRtpPortInfo.getStart(),
  59 + mediaSendRtpPortInfo.getEnd(), checkPort -> sendRtpItemMap.get(checkPort) == null);
  60 +
  61 + mediaSendRtpPortInfo.setCurrent(port);
  62 + redisTemplate.opsForValue().set(sendIndexKey, mediaSendRtpPortInfo);
  63 + return port;
  64 + }
  65 +
  66 + interface CheckPortCallback{
  67 + boolean check(int port);
  68 + }
  69 +
  70 + private int getPort(int current, int start, int end, CheckPortCallback checkPortCallback) {
38 int port; 71 int port;
39 - if (mediaSendRtpPortInfo.getCurrent() %2 != 0) {  
40 - port = mediaSendRtpPortInfo.getCurrent() + 1; 72 + if (current %2 != 0) {
  73 + port = current + 1;
41 }else { 74 }else {
42 - port = mediaSendRtpPortInfo.getCurrent() + 2; 75 + port = current + 2;
43 } 76 }
44 - if (port > mediaSendRtpPortInfo.getEnd()) {  
45 - if (mediaSendRtpPortInfo.getStart() %2 != 0) {  
46 - port = mediaSendRtpPortInfo.getStart() + 1; 77 + if (port > end) {
  78 + if (start %2 != 0) {
  79 + port = start + 1;
47 }else { 80 }else {
48 - port = mediaSendRtpPortInfo.getStart(); 81 + port = start;
49 } 82 }
50 } 83 }
51 - mediaSendRtpPortInfo.setCurrent(port);  
52 - redisTemplate.opsForValue().set(key, mediaSendRtpPortInfo); 84 + if (!checkPortCallback.check(port)) {
  85 + return getPort(port, start, end, checkPortCallback);
  86 + }
53 return port; 87 return port;
54 } 88 }
55 } 89 }
src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java
@@ -142,11 +142,12 @@ public class RtpController { @@ -142,11 +142,12 @@ public class RtpController {
142 // 预创建发流信息 142 // 预创建发流信息
143 int portForVideo = sendRtpPortManager.getNextPort(mediaServerItem.getId()); 143 int portForVideo = sendRtpPortManager.getNextPort(mediaServerItem.getId());
144 int portForAudio = sendRtpPortManager.getNextPort(mediaServerItem.getId()); 144 int portForAudio = sendRtpPortManager.getNextPort(mediaServerItem.getId());
145 - // 将信息写入redis中,以备后用  
146 - redisTemplate.opsForValue().set(key, otherRtpSendInfo, 300, TimeUnit.SECONDS); 145 +
147 otherRtpSendInfo.setSendLocalIp(mediaServerItem.getSdpIp()); 146 otherRtpSendInfo.setSendLocalIp(mediaServerItem.getSdpIp());
148 otherRtpSendInfo.setSendLocalPortForVideo(portForVideo); 147 otherRtpSendInfo.setSendLocalPortForVideo(portForVideo);
149 otherRtpSendInfo.setSendLocalPortForAudio(portForAudio); 148 otherRtpSendInfo.setSendLocalPortForAudio(portForAudio);
  149 + // 将信息写入redis中,以备后用
  150 + redisTemplate.opsForValue().set(key, otherRtpSendInfo, 300, TimeUnit.SECONDS);
150 logger.info("[第三方服务对接->开启收流和获取发流信息] 结果,callId->{}, {}", callId, otherRtpSendInfo); 151 logger.info("[第三方服务对接->开启收流和获取发流信息] 结果,callId->{}, {}", callId, otherRtpSendInfo);
151 } 152 }
152 return otherRtpSendInfo; 153 return otherRtpSendInfo;