优化国标级联的端口保持

This commit is contained in:
648540858
2023-06-21 14:03:31 +08:00
parent 3350b65259
commit 7b24d51db9
20 changed files with 168 additions and 74 deletions

View File

@@ -124,14 +124,12 @@ public class ZLMHttpHookListener {
@PostMapping(value = "/on_server_keepalive", produces = "application/json;charset=UTF-8")
public HookResult onServerKeepalive(@RequestBody OnServerKeepaliveHookParam param) {
// logger.info("[ZLM HOOK] 收到zlm心跳" + param.getMediaServerId());
taskExecutor.execute(() -> {
List<ZlmHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(HookType.on_server_keepalive);
JSONObject json = (JSONObject) JSON.toJSON(param);
if (subscribes != null && subscribes.size() > 0) {
for (ZlmHttpHookSubscribe.Event subscribe : subscribes) {
subscribe.response(null, json);
subscribe.response(null, param);
}
}
});
@@ -158,7 +156,7 @@ public class ZLMHttpHookListener {
if (subscribe != null) {
MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
if (mediaInfo != null) {
subscribe.response(mediaInfo, json);
subscribe.response(mediaInfo, param);
}
}
});
@@ -234,7 +232,7 @@ public class ZLMHttpHookListener {
ZlmHttpHookSubscribe.Event subscribe = this.subscribe.sendNotify(HookType.on_publish, json);
if (subscribe != null) {
if (mediaInfo != null) {
subscribe.response(mediaInfo, json);
subscribe.response(mediaInfo, param);
} else {
new HookResultForOnPublish(1, "zlm not register");
}
@@ -306,7 +304,7 @@ public class ZLMHttpHookListener {
return;
}
if (subscribe != null) {
subscribe.response(mediaInfo, json);
subscribe.response(mediaInfo, param);
}
List<OnStreamChangedHookParam.MediaTrack> tracks = param.getTracks();
@@ -649,7 +647,7 @@ public class ZLMHttpHookListener {
List<ZlmHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(HookType.on_server_started);
if (subscribes != null && subscribes.size() > 0) {
for (ZlmHttpHookSubscribe.Event subscribe : subscribes) {
subscribe.response(null, jsonObject);
subscribe.response(null, zlmServerConfig);
}
}
mediaServerService.zlmServerOnline(zlmServerConfig);
@@ -704,7 +702,7 @@ public class ZLMHttpHookListener {
List<ZlmHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(HookType.on_rtp_server_timeout);
if (subscribes != null && subscribes.size() > 0) {
for (ZlmHttpHookSubscribe.Event subscribe : subscribes) {
subscribe.response(null, json);
subscribe.response(null, param);
}
}
});

View File

@@ -270,6 +270,11 @@ public class ZLMRESTfulUtils {
}
public JSONObject openRtpServer(MediaServerItem mediaServerItem, Map<String, Object> param){
System.out.println("==============openRtpServer=================");
for (String s : param.keySet()) {
System.out.println(s + "-->" + param.get(s));
}
System.out.println("===============================");
return sendPost(mediaServerItem, "openRtpServer",param, null);
}

View File

@@ -9,6 +9,8 @@ import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeFactory;
import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeForRtpServerTimeout;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
import com.genersoft.iot.vmp.media.zlm.dto.hook.HookParam;
import com.genersoft.iot.vmp.media.zlm.dto.hook.OnRtpServerTimeoutHookParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -225,7 +227,8 @@ public class ZLMRTPServerFactory {
int localPort = 0;
if (userSetting.getGbSendStreamStrict()) {
if (userSetting.getGbSendStreamStrict()) {
localPort = keepPort(serverItem, ssrc);
System.out.println("createSendRtpItem1");
localPort = keepPort(serverItem, ssrc, localPort);
if (localPort == 0) {
return null;
}
@@ -261,7 +264,8 @@ public class ZLMRTPServerFactory {
// 默认为随机端口
int localPort = 0;
if (userSetting.getGbSendStreamStrict()) {
localPort = keepPort(serverItem, ssrc);
System.out.println("createSendRtpItem2");
localPort = keepPort(serverItem, ssrc, localPort);
if (localPort == 0) {
return null;
}
@@ -285,30 +289,37 @@ public class ZLMRTPServerFactory {
/**
* 保持端口,直到需要需要发流时再释放
*/
public int keepPort(MediaServerItem serverItem, String ssrc) {
int localPort = 0;
public int keepPort(MediaServerItem serverItem, String ssrc, Integer localPort) {
Map<String, Object> param = new HashMap<>(3);
param.put("port", 0);
param.put("port", localPort);
param.put("enable_tcp", 1);
param.put("stream_id", ssrc);
System.out.println("用于收流");
JSONObject jsonObject = zlmresTfulUtils.openRtpServer(serverItem, param);
System.out.println(jsonObject);
if (jsonObject.getInteger("code") == 0) {
localPort = jsonObject.getInteger("port");
HookSubscribeForRtpServerTimeout hookSubscribeForRtpServerTimeout = HookSubscribeFactory.on_rtp_server_timeout(ssrc, null, serverItem.getId());
Integer finalLocalPort = localPort;
hookSubscribe.addSubscribe(hookSubscribeForRtpServerTimeout,
(MediaServerItem mediaServerItem, JSONObject response)->{
logger.info("[上级点播] {}->监听端口到期继续保持监听", ssrc);
int port = keepPort(serverItem, ssrc);
(MediaServerItem mediaServerItem, HookParam hookParam)->{
logger.info("[上级点播] {}->监听端口到期继续保持监听: {}", ssrc, finalLocalPort);
OnRtpServerTimeoutHookParam rtpServerTimeoutHookParam = (OnRtpServerTimeoutHookParam) hookParam;
if (!ssrc.equals(rtpServerTimeoutHookParam.getSsrc())) {
return;
}
int port = keepPort(serverItem, ssrc, finalLocalPort);
if (port == 0) {
logger.info("[上级点播] {}->监听端口失败,移除监听", ssrc);
hookSubscribe.removeSubscribe(hookSubscribeForRtpServerTimeout);
}
});
logger.info("[上级点播] {}->监听端口: {}", ssrc, localPort);
return localPort;
}else {
logger.info("[上级点播] 监听端口失败: {}", ssrc);
logger.info("[上级点播] 监听端口失败: {}->{}", ssrc, localPort);
return 0;
}
return localPort;
}
/**

View File

@@ -65,8 +65,8 @@ public class ZLMRunner implements CommandLineRunner {
HookSubscribeForServerStarted hookSubscribeForServerStarted = HookSubscribeFactory.on_server_started();
// 订阅 zlm启动事件, 新的zlm也会从这里进入系统
hookSubscribe.addSubscribe(hookSubscribeForServerStarted,
(MediaServerItem mediaServerItem, JSONObject response)->{
ZLMServerConfig zlmServerConfig = response.to(ZLMServerConfig.class);
(mediaServerItem, hookParam)->{
ZLMServerConfig zlmServerConfig = (ZLMServerConfig)hookParam;
if (zlmServerConfig !=null ) {
if (startGetMedia != null) {
startGetMedia.remove(zlmServerConfig.getGeneralMediaServerId());

View File

@@ -1,8 +1,9 @@
package com.genersoft.iot.vmp.media.zlm;
import com.alibaba.fastjson2.annotation.JSONField;
import com.genersoft.iot.vmp.media.zlm.dto.hook.HookParam;
public class ZLMServerConfig {
public class ZLMServerConfig extends HookParam {
@JSONField(name = "api.apiDebug")
private String apiDebug;

View File

@@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSONObject;
import com.genersoft.iot.vmp.media.zlm.dto.HookType;
import com.genersoft.iot.vmp.media.zlm.dto.IHookSubscribe;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
import com.genersoft.iot.vmp.media.zlm.dto.hook.HookParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
@@ -26,7 +27,7 @@ public class ZlmHttpHookSubscribe {
@FunctionalInterface
public interface Event{
void response(MediaServerItem mediaServerItem, JSONObject response);
void response(MediaServerItem mediaServerItem, HookParam hookParam);
}
private Map<HookType, Map<IHookSubscribe, ZlmHttpHookSubscribe.Event>> allSubscribes = new ConcurrentHashMap<>();

View File

@@ -50,4 +50,6 @@ public class HookResultForOnPublish extends HookResult{
public void setMp4_save_path(String mp4_save_path) {
this.mp4_save_path = mp4_save_path;
}
}

View File

@@ -81,6 +81,15 @@ public class OnPlayHookParam extends HookParam{
@Override
public String toString() {
return String.format("%s://%s:%s/%s/%s?%s", schema, ip, port, app, stream, params);
return "OnPlayHookParam{" +
"id='" + id + '\'' +
", app='" + app + '\'' +
", stream='" + stream + '\'' +
", ip='" + ip + '\'' +
", params='" + params + '\'' +
", port=" + port +
", schema='" + schema + '\'' +
", vhost='" + vhost + '\'' +
'}';
}
}

View File

@@ -81,6 +81,15 @@ public class OnPublishHookParam extends HookParam{
@Override
public String toString() {
return String.format("%s://%s:%s/%s/%s?%s", schema, ip, port, app, stream, params);
return "OnPublishHookParam{" +
"id='" + id + '\'' +
", app='" + app + '\'' +
", stream='" + stream + '\'' +
", ip='" + ip + '\'' +
", params='" + params + '\'' +
", port=" + port +
", schema='" + schema + '\'' +
", vhost='" + vhost + '\'' +
'}';
}
}

View File

@@ -50,4 +50,15 @@ public class OnRtpServerTimeoutHookParam extends HookParam{
public void setSsrc(String ssrc) {
this.ssrc = ssrc;
}
@Override
public String toString() {
return "OnRtpServerTimeoutHookParam{" +
"local_port=" + local_port +
", stream_id='" + stream_id + '\'' +
", tcpMode=" + tcpMode +
", re_use_port=" + re_use_port +
", ssrc='" + ssrc + '\'' +
'}';
}
}

View File

@@ -24,4 +24,12 @@ public class OnSendRtpStoppedHookParam extends HookParam{
public void setStream(String stream) {
this.stream = stream;
}
@Override
public String toString() {
return "OnSendRtpStoppedHookParam{" +
"app='" + app + '\'' +
", stream='" + stream + '\'' +
'}';
}
}

View File

@@ -17,4 +17,11 @@ public class OnServerKeepaliveHookParam extends HookParam{
public void setData(ServerKeepaliveData data) {
this.data = data;
}
@Override
public String toString() {
return "OnServerKeepaliveHookParam{" +
"data=" + data +
'}';
}
}

View File

@@ -430,4 +430,14 @@ public class OnStreamChangedHookParam extends HookParam{
public void setCallId(String callId) {
this.callId = callId;
}
@Override
public String toString() {
return "OnStreamChangedHookParam{" +
"regist=" + regist +
", app='" + app + '\'' +
", stream='" + stream + '\'' +
", severId='" + severId + '\'' +
'}';
}
}

View File

@@ -38,4 +38,14 @@ public class OnStreamNoneReaderHookParam extends HookParam{
public void setVhost(String vhost) {
this.vhost = vhost;
}
@Override
public String toString() {
return "OnStreamNoneReaderHookParam{" +
"schema='" + schema + '\'' +
", app='" + app + '\'' +
", stream='" + stream + '\'' +
", vhost='" + vhost + '\'' +
'}';
}
}

View File

@@ -81,6 +81,15 @@ public class OnStreamNotFoundHookParam extends HookParam{
@Override
public String toString() {
return String.format("%s://%s:%s/%s/%s?%s", schema, ip, port, app, stream, params);
return "OnStreamNotFoundHookParam{" +
"id='" + id + '\'' +
", app='" + app + '\'' +
", stream='" + stream + '\'' +
", ip='" + ip + '\'' +
", params='" + params + '\'' +
", port=" + port +
", schema='" + schema + '\'' +
", vhost='" + vhost + '\'' +
'}';
}
}