修复端口分配的并发问题

pull/964/head
648540858 2023-07-24 09:04:47 +08:00
parent 0cb68938b8
commit d48f89eee4
8 changed files with 265 additions and 119 deletions

View File

@ -0,0 +1,191 @@
package com.genersoft.iot.vmp.common;
import io.swagger.v3.oas.annotations.media.Schema;
public class CommonGbChannel {
/**
*
*/
@Schema(description = "归属")
private String owner;
/**
*
*/
@Schema(description = "行政区划")
private String civilCode;
/**
*
*/
@Schema(description = "安装地址")
private String address;
/**
*
*/
@Schema(description = "经度")
private Double longitude;
/**
*
*/
@Schema(description = "纬度")
private Double latitude;
/**
* :
* 1-;
* 2-;
* 3-;
* 4-
*/
@Schema(description = "摄像机类型")
private Integer ptzType;
/**
*
* 1-
* 2-
* 3-
* 4-广
* 5-
* 6-
* 7-
* 8-
* 9-
* 10-线
*/
@Schema(description = "摄像机位置类型扩展")
private Integer positionType;
/**
*
* 1-
* 2-
*/
@Schema(description = "安装位置室外、室内属性")
private Integer roomType;
/**
*
* 1-
* 2-
* 3-
*/
@Schema(description = "用途")
private Integer useType;
/**
*
* 1-
* 2-
* 3-
*/
@Schema(description = "补光属性")
private Integer supplyLightType;
/**
*
* 1-
* 2-西
* 3-
* 4-
* 5-
* 6-
* 7-西
* 8-西
*
*/
@Schema(description = "方位")
private Integer directionType;
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getCivilCode() {
return civilCode;
}
public void setCivilCode(String civilCode) {
this.civilCode = civilCode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Integer getPtzType() {
return ptzType;
}
public void setPtzType(Integer ptzType) {
this.ptzType = ptzType;
}
public Integer getPositionType() {
return positionType;
}
public void setPositionType(Integer positionType) {
this.positionType = positionType;
}
public Integer getRoomType() {
return roomType;
}
public void setRoomType(Integer roomType) {
this.roomType = roomType;
}
public Integer getUseType() {
return useType;
}
public void setUseType(Integer useType) {
this.useType = useType;
}
public Integer getSupplyLightType() {
return supplyLightType;
}
public void setSupplyLightType(Integer supplyLightType) {
this.supplyLightType = supplyLightType;
}
public Integer getDirectionType() {
return directionType;
}
public void setDirectionType(Integer directionType) {
this.directionType = directionType;
}
}

View File

@ -3,12 +3,14 @@ package com.genersoft.iot.vmp.media.zlm;
import com.genersoft.iot.vmp.common.VideoManagerConstants;
import com.genersoft.iot.vmp.conf.UserSetting;
import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
import com.genersoft.iot.vmp.media.zlm.dto.MediaSendRtpPortInfo;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
import com.genersoft.iot.vmp.utils.redis.RedisUtil;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
import org.springframework.stereotype.Component;
import java.util.HashMap;
@ -26,23 +28,14 @@ public class SendRtpPortManager {
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
private final String KEY = "VM_MEDIA_SEND_RTP_PORT_RANGE_";
private final String KEY = "VM_MEDIA_SEND_RTP_PORT_";
public void initServerPort(String mediaServerId, int startPort, int endPort){
String key = KEY + userSetting.getServerId() + "_" + mediaServerId;
MediaSendRtpPortInfo mediaSendRtpPortInfo = new MediaSendRtpPortInfo(startPort, endPort, mediaServerId);
redisTemplate.opsForValue().set(key, mediaSendRtpPortInfo);
public int getNextPort(MediaServerItem mediaServer) {
if (mediaServer == null) {
logger.warn("[发送端口管理] 参数错误mediaServer为NULL");
return -1;
}
public int getNextPort(String mediaServerId) {
String sendIndexKey = KEY + userSetting.getServerId() + "_" + mediaServerId;
MediaSendRtpPortInfo mediaSendRtpPortInfo = (MediaSendRtpPortInfo)redisTemplate.opsForValue().get(sendIndexKey);
if (mediaSendRtpPortInfo == null) {
logger.warn("[发送端口管理] 获取{}的发送端口时未找到端口信息", mediaServerId);
return 0;
}
String sendIndexKey = KEY + userSetting.getServerId() + "_" + mediaServer.getId();
String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX
+ userSetting.getServerId() + "_*";
List<Object> queryResult = RedisUtil.scan(redisTemplate, key);
@ -54,14 +47,39 @@ public class SendRtpPortManager {
sendRtpItemMap.put(sendRtpItem.getLocalPort(), sendRtpItem);
}
}
String sendRtpPortRange = mediaServer.getSendRtpPortRange();
int startPort;
int endPort;
if (sendRtpPortRange == null) {
logger.warn("{}未设置发送端口默认值自动使用40000-50000作为端口范围", mediaServer.getId());
String[] portArray = sendRtpPortRange.split(",");
if (portArray.length != 2 || !NumberUtils.isParsable(portArray[0]) || !NumberUtils.isParsable(portArray[1])) {
logger.warn("{}发送端口配置格式错误自动使用40000-50000作为端口范围", mediaServer.getId());
startPort = 50000;
endPort = 60000;
}else {
int port = getPort(mediaSendRtpPortInfo.getCurrent(),
mediaSendRtpPortInfo.getStart(),
mediaSendRtpPortInfo.getEnd(), checkPort -> sendRtpItemMap.get(checkPort) == null);
mediaSendRtpPortInfo.setCurrent(port);
redisTemplate.opsForValue().set(sendIndexKey, mediaSendRtpPortInfo);
return port;
if ( Integer.parseInt(portArray[1]) - Integer.parseInt(portArray[0]) < 1) {
logger.warn("{}发送端口配置错误,结束端口至少比开始端口大一自动使用40000-50000作为端口范围", mediaServer.getId());
startPort = 50000;
endPort = 60000;
}else {
startPort = Integer.parseInt(portArray[0]);
endPort = Integer.parseInt(portArray[1]);
}
}
}else {
startPort = 50000;
endPort = 60000;
}
if (redisTemplate == null || redisTemplate.getConnectionFactory() == null) {
logger.warn("{}获取redis连接信息失败", mediaServer.getId());
return -1;
}
RedisAtomicInteger redisAtomicInteger = new RedisAtomicInteger(sendIndexKey , redisTemplate.getConnectionFactory());
return redisAtomicInteger.getAndUpdate((current)->{
return getPort(current, startPort, endPort, checkPort-> !sendRtpItemMap.containsKey(checkPort));
});
}
interface CheckPortCallback{
@ -69,22 +87,25 @@ public class SendRtpPortManager {
}
private int getPort(int current, int start, int end, CheckPortCallback checkPortCallback) {
int port;
if (current %2 != 0) {
port = current + 1;
if (current <= 0) {
if (start%2 == 0) {
current = start;
}else {
port = current + 2;
current = start + 1;
}
if (port > end) {
if (start %2 != 0) {
port = start + 1;
}else {
port = start;
current += 2;
if (current > end) {
if (start%2 == 0) {
current = start;
}else {
current = start + 1;
}
}
if (!checkPortCallback.check(port)) {
return getPort(port, start, end, checkPortCallback);
}
return port;
if (!checkPortCallback.check(current)) {
return getPort(current + 2, start, end, checkPortCallback);
}
return current;
}
}

View File

@ -167,7 +167,7 @@ public class ZLMServerFactory {
// 默认为随机端口
int localPort = 0;
if (userSetting.getGbSendStreamStrict()) {
localPort = sendRtpPortManager.getNextPort(serverItem.getId());
localPort = sendRtpPortManager.getNextPort(serverItem);
if (localPort == 0) {
return null;
}
@ -203,7 +203,7 @@ public class ZLMServerFactory {
// 默认为随机端口
int localPort = 0;
if (userSetting.getGbSendStreamStrict()) {
localPort = sendRtpPortManager.getNextPort(serverItem.getId());
localPort = sendRtpPortManager.getNextPort(serverItem);
if (localPort == 0) {
return null;
}

View File

@ -1,50 +0,0 @@
package com.genersoft.iot.vmp.media.zlm.dto;
public class MediaSendRtpPortInfo {
private int start;
private int end;
private String mediaServerId;
private int current;
public MediaSendRtpPortInfo(int start, int end, String mediaServerId) {
this.start = start;
this.current = start;
this.end = end;
this.mediaServerId = mediaServerId;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public String getMediaServerId() {
return mediaServerId;
}
public void setMediaServerId(String mediaServerId) {
this.mediaServerId = mediaServerId;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
}

View File

@ -119,34 +119,6 @@ public class MediaServerServiceImpl implements IMediaServerService {
if (ssrcFactory.hasMediaServerSSRC(mediaServerItem.getId())) {
ssrcFactory.initMediaServerSSRC(mediaServerItem.getId(), null);
}
if (userSetting.getGbSendStreamStrict()) {
int startPort = 50000;
int endPort = 60000;
String sendRtpPortRange = mediaServerItem.getSendRtpPortRange();
if (sendRtpPortRange == null) {
logger.warn("[zlm] ] 未配置发流端口范围默认使用50000到60000");
}else {
String[] sendRtpPortRangeArray = sendRtpPortRange.trim().split(",");
if (sendRtpPortRangeArray.length != 2) {
logger.warn("[zlm] ] 发流端口范围错误默认使用50000到60000");
}else {
try {
startPort = Integer.parseInt(sendRtpPortRangeArray[0]);
endPort = Integer.parseInt(sendRtpPortRangeArray[1]);
if (endPort <= startPort) {
logger.warn("[zlm] ] 发流端口范围错误,结束端口应大于开始端口,使用默认端口");
startPort = 50000;
endPort = 60000;
}
}catch (NumberFormatException e) {
logger.warn("[zlm] ] 发流端口范围错误默认使用50000到60000");
}
}
}
logger.info("[[zlm] ] 配置发流端口范围,{}-{}", startPort, endPort);
sendRtpPortManager.initServerPort(mediaServerItem.getId(), startPort, endPort);
}
// 查询redis是否存在此mediaServer
String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerItem.getId();
Boolean hasKey = redisTemplate.hasKey(key);

View File

@ -269,7 +269,7 @@ public class PlayServiceImpl implements IPlayService {
InviteErrorCode.SUCCESS.getCode(),
InviteErrorCode.SUCCESS.getMsg(),
streamInfo);
logger.info("[点播成功] deviceId: {}, channelId:{}, 码流类型:{}", device.getDeviceId(),
logger.info("[点播成功] deviceId: {}, channelId:{}, 码流类型:{}", device.getDeviceId(), channelId,
device.isSwitchPrimarySubStream() ? "辅码流" : "主码流");
snapOnPlay(mediaServerItemInuse, device.getDeviceId(), channelId, ssrcInfo.getStream());
}, (event) -> {

View File

@ -139,8 +139,8 @@ public class RtpController {
redisTemplate.opsForValue().set(receiveKey, otherRtpSendInfo);
if (isSend != null && isSend) {
// 预创建发流信息
int portForVideo = sendRtpPortManager.getNextPort(mediaServerItem.getId());
int portForAudio = sendRtpPortManager.getNextPort(mediaServerItem.getId());
int portForVideo = sendRtpPortManager.getNextPort(mediaServerItem);
int portForAudio = sendRtpPortManager.getNextPort(mediaServerItem);
otherRtpSendInfo.setSendLocalIp(mediaServerItem.getSdpIp());
otherRtpSendInfo.setSendLocalPortForVideo(portForVideo);

View File

@ -8,6 +8,7 @@ import com.genersoft.iot.vmp.conf.SipConfig;
import com.genersoft.iot.vmp.conf.UserSetting;
import com.genersoft.iot.vmp.conf.VersionInfo;
import com.genersoft.iot.vmp.conf.exception.ControllerException;
import com.genersoft.iot.vmp.media.zlm.SendRtpPortManager;
import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe;
import com.genersoft.iot.vmp.media.zlm.dto.IHookSubscribe;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
@ -72,6 +73,9 @@ public class ServerController {
@Autowired
private IRedisCatchStorage redisCatchStorage;
@Autowired
private SendRtpPortManager sendRtpPortManager;
@GetMapping(value = "/media_server/list")
@ResponseBody
@ -262,4 +266,12 @@ public class ServerController {
return result;
}
@PostMapping(value = "/test/getPort")
@ResponseBody
public int getPort() {
int result = sendRtpPortManager.getNextPort(mediaServerService.getDefaultMediaServer());
System.out.println(result);
return result;
}
}