优化端口预占用

结构优化
648540858 2023-06-25 10:18:29 +08:00
parent a45fab423b
commit 66eda32ab9
2 changed files with 16 additions and 11 deletions

View File

@ -228,7 +228,7 @@ public class ZLMRTPServerFactory {
int localPort = 0; int localPort = 0;
if (userSetting.getGbSendStreamStrict()) { if (userSetting.getGbSendStreamStrict()) {
if (userSetting.getGbSendStreamStrict()) { if (userSetting.getGbSendStreamStrict()) {
localPort = keepPort(serverItem, ssrc); localPort = keepPort(serverItem, ssrc, null);
if (localPort == 0) { if (localPort == 0) {
return null; return null;
} }
@ -264,7 +264,7 @@ public class ZLMRTPServerFactory {
// 默认为随机端口 // 默认为随机端口
int localPort = 0; int localPort = 0;
if (userSetting.getGbSendStreamStrict()) { if (userSetting.getGbSendStreamStrict()) {
localPort = keepPort(serverItem, ssrc); localPort = keepPort(serverItem, ssrc, null);
if (localPort == 0) { if (localPort == 0) {
return null; return null;
} }
@ -288,23 +288,28 @@ public class ZLMRTPServerFactory {
/** /**
* *
*/ */
public int keepPort(MediaServerItem serverItem, String ssrc) { public int keepPort(MediaServerItem serverItem, String ssrc, Integer localPort) {
int localPort = 0;
Map<String, Object> param = new HashMap<>(3); Map<String, Object> param = new HashMap<>(3);
param.put("port", 0); if (localPort == null) {
localPort = 0;
}
param.put("port", localPort);
param.put("enable_tcp", 1); param.put("enable_tcp", 1);
param.put("stream_id", ssrc); param.put("stream_id", ssrc);
JSONObject jsonObject = zlmresTfulUtils.openRtpServer(serverItem, param); JSONObject jsonObject = zlmresTfulUtils.openRtpServer(serverItem, param);
if (jsonObject.getInteger("code") == 0) { if (jsonObject.getInteger("code") == 0) {
localPort = jsonObject.getInteger("port"); localPort = jsonObject.getInteger("port");
HookSubscribeForRtpServerTimeout hookSubscribeForRtpServerTimeout = HookSubscribeFactory.on_rtp_server_timeout(ssrc, null, serverItem.getId()); HookSubscribeForRtpServerTimeout hookSubscribeForRtpServerTimeout = HookSubscribeFactory.on_rtp_server_timeout(ssrc, null, serverItem.getId());
Integer finalLocalPort = localPort;
hookSubscribe.addSubscribe(hookSubscribeForRtpServerTimeout, hookSubscribe.addSubscribe(hookSubscribeForRtpServerTimeout,
(MediaServerItem mediaServerItem, JSONObject response)->{ (MediaServerItem mediaServerItem, JSONObject response)->{
logger.info("[上级点播] {}->监听端口到期继续保持监听", ssrc); if (ssrc.equals(response.getString("ssrc"))) {
int port = keepPort(serverItem, ssrc); logger.info("[上级点播] {}->监听端口到期继续保持监听", ssrc);
if (port == 0) { int port = keepPort(serverItem, ssrc, finalLocalPort);
logger.info("[上级点播] {}->监听端口失败,移除监听", ssrc); if (port == 0) {
hookSubscribe.removeSubscribe(hookSubscribeForRtpServerTimeout); logger.info("[上级点播] {}->监听端口失败,移除监听", ssrc);
hookSubscribe.removeSubscribe(hookSubscribeForRtpServerTimeout);
}
} }
}); });
logger.info("[保持端口] {}->监听端口: {}", ssrc, localPort); logger.info("[保持端口] {}->监听端口: {}", ssrc, localPort);

View File

@ -235,7 +235,7 @@ public class PlayServiceImpl implements IPlayService {
sendRtpItem.setReceiveStream(stream + "_talk"); sendRtpItem.setReceiveStream(stream + "_talk");
int port = zlmrtpServerFactory.keepPort(mediaServerItem, playSsrc); int port = zlmrtpServerFactory.keepPort(mediaServerItem, playSsrc, null);
//端口获取失败的ssrcInfo 没有必要发送点播指令 //端口获取失败的ssrcInfo 没有必要发送点播指令
if (port <= 0) { if (port <= 0) {
logger.info("[语音对讲] 端口分配异常deviceId={},channelId={}", device.getDeviceId(), channelId); logger.info("[语音对讲] 端口分配异常deviceId={},channelId={}", device.getDeviceId(), channelId);