diff --git a/src/main/java/com/genersoft/iot/vmp/conf/MediaStatusTimerTask.java b/src/main/java/com/genersoft/iot/vmp/conf/MediaStatusTimerTask.java new file mode 100644 index 00000000..cb9de7dc --- /dev/null +++ b/src/main/java/com/genersoft/iot/vmp/conf/MediaStatusTimerTask.java @@ -0,0 +1,16 @@ +package com.genersoft.iot.vmp.conf; + +import com.alibaba.fastjson.JSONObject; +import org.springframework.scheduling.annotation.Scheduled; + +/** + * 定时向zlm同步媒体流状态 + */ +public class MediaStatusTimerTask { + + + @Scheduled(fixedRate = 2 * 1000) //每3秒执行一次 + public void execute(){ + + } +} diff --git a/src/main/java/com/genersoft/iot/vmp/conf/ProxyServletConfig.java b/src/main/java/com/genersoft/iot/vmp/conf/ProxyServletConfig.java index dd99d020..93b975e2 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/ProxyServletConfig.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/ProxyServletConfig.java @@ -18,6 +18,7 @@ import org.springframework.util.StringUtils; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.ConnectException; @@ -126,7 +127,11 @@ public class ProxyServletConfig { MediaServerItem getMediaInfoByUri(String uri){ String[] split = uri.split("/"); String mediaServerId = split[2]; - return mediaServerService.getOne(mediaServerId); + if ("default".equals(mediaServerId)) { + return mediaServerService.getDefaultMediaServer(); + }else { + return mediaServerService.getOne(mediaServerId); + } } /** @@ -140,13 +145,13 @@ public class ProxyServletConfig { if (mediaInfo == null) { return url; } - return url.replace(mediaInfo.getId() + "/", ""); + return url.replace(mediaInfo.getId() + "/", "").replace("default/", ""); } } @Bean public ServletRegistrationBean recordServletRegistrationBean(){ - ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new RecordProxySerlet(),"/record_proxy/*"); + ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new RecordProxyServlet(),"/record_proxy/*"); servletRegistrationBean.setName("record_proxy"); servletRegistrationBean.addInitParameter("targetUri", "http://127.0.0.1:18081"); servletRegistrationBean.addUrlMappings(); @@ -156,8 +161,22 @@ public class ProxyServletConfig { return servletRegistrationBean; } - class RecordProxySerlet extends ProxyServlet{ + class RecordProxyServlet extends ProxyServlet{ + @Override + protected String rewriteQueryStringFromRequest(HttpServletRequest servletRequest, String queryString) { + String queryStr = super.rewriteQueryStringFromRequest(servletRequest, queryString); + MediaServerItem mediaInfo = getMediaInfoByUri(servletRequest.getRequestURI()); + String remoteHost = String.format("http://%s:%s", mediaInfo.getIp(), mediaInfo.getHttpPort()); + if (mediaInfo != null) { + if (!StringUtils.isEmpty(queryStr)) { + queryStr += "&remoteHost=" + remoteHost; + }else { + queryStr = "remoteHost=" + remoteHost; + } + } + return queryStr; + } /** * 异常处理 @@ -222,7 +241,12 @@ public class ProxyServletConfig { MediaServerItem getMediaInfoByUri(String uri){ String[] split = uri.split("/"); String mediaServerId = split[2]; - return mediaServerService.getOne(mediaServerId); + if ("default".equals(mediaServerId)) { + return mediaServerService.getDefaultMediaServer(); + }else { + return mediaServerService.getOne(mediaServerId); + } + } /** @@ -236,7 +260,7 @@ public class ProxyServletConfig { if (mediaInfo == null) { return url; } - return url.replace(mediaInfo.getId() + "/", ""); + return url.replace(mediaInfo.getId() + "/", "").replace("default/", ""); } } diff --git a/src/main/java/com/genersoft/iot/vmp/conf/ThreadPoolTaskConfig.java b/src/main/java/com/genersoft/iot/vmp/conf/ThreadPoolTaskConfig.java index 2ef33230..bde6b3fc 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/ThreadPoolTaskConfig.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/ThreadPoolTaskConfig.java @@ -38,7 +38,7 @@ public class ThreadPoolTaskConfig { /** * 线程池名前缀 */ - private static final String threadNamePrefix = "wvp-sip-handle-"; + private static final String threadNamePrefix = "wvp-sip-"; @Bean("taskExecutor") // bean的名称,默认为首字母小写的方法名 public ThreadPoolTaskExecutor taskExecutor() { diff --git a/src/main/java/com/genersoft/iot/vmp/conf/SipDeviceRunner.java b/src/main/java/com/genersoft/iot/vmp/conf/runner/SipDeviceRunner.java similarity index 96% rename from src/main/java/com/genersoft/iot/vmp/conf/SipDeviceRunner.java rename to src/main/java/com/genersoft/iot/vmp/conf/runner/SipDeviceRunner.java index da1664bd..f944b11d 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/SipDeviceRunner.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/runner/SipDeviceRunner.java @@ -1,4 +1,4 @@ -package com.genersoft.iot.vmp.conf; +package com.genersoft.iot.vmp.conf.runner; import com.genersoft.iot.vmp.storager.IRedisCatchStorage; import com.genersoft.iot.vmp.storager.IVideoManagerStorager; diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/SipLayer.java b/src/main/java/com/genersoft/iot/vmp/gb28181/SipLayer.java index 420a30a7..f3d542c8 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/SipLayer.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/SipLayer.java @@ -70,6 +70,7 @@ public class SipLayer{ try { tcpListeningPoint = sipStack.createListeningPoint(sipConfig.getMonitorIp(), sipConfig.getPort(), "TCP"); tcpSipProvider = (SipProviderImpl)sipStack.createSipProvider(tcpListeningPoint); + tcpSipProvider.setDialogErrorsAutomaticallyHandled(); tcpSipProvider.addSipListener(sipProcessorObserver); logger.info("Sip Server TCP 启动成功 port {" + sipConfig.getMonitorIp() + ":" + sipConfig.getPort() + "}"); } catch (TransportNotSupportedException e) { diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/SIPProcessorObserver.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/SIPProcessorObserver.java index 3724a3be..116236f5 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/SIPProcessorObserver.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/SIPProcessorObserver.java @@ -4,12 +4,11 @@ import com.genersoft.iot.vmp.gb28181.event.SipSubscribe; import com.genersoft.iot.vmp.gb28181.transmit.event.request.ISIPRequestProcessor; import com.genersoft.iot.vmp.gb28181.transmit.event.response.ISIPResponseProcessor; import com.genersoft.iot.vmp.gb28181.transmit.event.timeout.ITimeoutProcessor; +import gov.nist.javax.sip.message.SIPRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.scheduling.annotation.Async; -import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Component; import javax.sip.*; diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRunner.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRunner.java index f20f74d8..c77bd80d 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRunner.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRunner.java @@ -3,6 +3,7 @@ package com.genersoft.iot.vmp.media.zlm; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.genersoft.iot.vmp.common.VideoManagerConstants; import com.genersoft.iot.vmp.conf.MediaConfig; import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; import com.genersoft.iot.vmp.service.IMediaServerService; @@ -12,6 +13,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import java.util.*; @@ -41,22 +43,9 @@ public class ZLMRunner implements CommandLineRunner { @Override public void run(String... strings) throws Exception { - // 清楚redis缓存的在线zlm信息 - mediaServerService.clearMediaServerForOnline(); - - // 将配置文件的meida配置写入数据库 -// MediaServerItem presetMediaServer = mediaServerService.getOneByHostAndPort( -// mediaConfig.getIp(), mediaConfig.getHttpPort()); -// if (presetMediaServer != null) { -// MediaServerItem mediaSerItem = mediaConfig.getMediaSerItem(); -// mediaSerItem.setId(presetMediaServer.getId()); -// mediaServerService.update(mediaSerItem); -// }else { -// if (mediaConfig.getId() != null) { -// MediaServerItem mediaSerItem = mediaConfig.getMediaSerItem(); -// mediaServerService.add(mediaSerItem); -// } -// } + if (mediaServerService.getDefaultMediaServer() == null) { + mediaServerService.addToDatabase(mediaConfig.getMediaSerItem()); + } // 订阅 zlm启动事件, 新的zlm也会从这里进入系统 hookSubscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_server_started,null, @@ -65,7 +54,6 @@ public class ZLMRunner implements CommandLineRunner { if (zlmServerConfig !=null ) { startGetMedia.remove(zlmServerConfig.getGeneralMediaServerId()); mediaServerService.handLeZLMServerConfig(zlmServerConfig); -// zLmRunning(zlmServerConfig); } }); @@ -80,17 +68,7 @@ public class ZLMRunner implements CommandLineRunner { for (MediaServerItem mediaServerItem : all) { if (startGetMedia == null) startGetMedia = new HashMap<>(); startGetMedia.put(mediaServerItem.getId(), true); - new Thread(() -> { - - ZLMServerConfig zlmServerConfig = getMediaServerConfig(mediaServerItem); - if (zlmServerConfig != null) { - zlmServerConfig.setIp(mediaServerItem.getIp()); - zlmServerConfig.setHttpPort(mediaServerItem.getHttpPort()); - startGetMedia.remove(mediaServerItem.getId()); - mediaServerService.handLeZLMServerConfig(zlmServerConfig); - } - - }).start(); + connectZlmServer(mediaServerItem); } Timer timer = new Timer(); // 2分钟后未连接到则不再去主动连接, TODO 并对重启前使用此在zlm的通道发送bye @@ -109,6 +87,17 @@ public class ZLMRunner implements CommandLineRunner { }, 60 * 1000 * 2); } + @Async + public void connectZlmServer(MediaServerItem mediaServerItem){ + ZLMServerConfig zlmServerConfig = getMediaServerConfig(mediaServerItem); + if (zlmServerConfig != null) { + zlmServerConfig.setIp(mediaServerItem.getIp()); + zlmServerConfig.setHttpPort(mediaServerItem.getHttpPort()); + startGetMedia.remove(mediaServerItem.getId()); + mediaServerService.handLeZLMServerConfig(zlmServerConfig); + } + } + public ZLMServerConfig getMediaServerConfig(MediaServerItem mediaServerItem) { if (startGetMedia == null) { return null;} if (!mediaServerItem.isDefaultServer() && mediaServerService.getOne(mediaServerItem.getId()) == null) { diff --git a/src/main/java/com/genersoft/iot/vmp/service/IMediaServerService.java b/src/main/java/com/genersoft/iot/vmp/service/IMediaServerService.java index 657d2808..43d36d95 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/IMediaServerService.java +++ b/src/main/java/com/genersoft/iot/vmp/service/IMediaServerService.java @@ -54,6 +54,8 @@ public interface IMediaServerService { WVPResult add(MediaServerItem mediaSerItem); + int addToDatabase(MediaServerItem mediaSerItem); + void resetOnlineServerItem(MediaServerItem serverItem); WVPResult checkMediaServer(String ip, int port, String secret); diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java index 51f99d92..e457a336 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java @@ -27,6 +27,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; import java.text.SimpleDateFormat; import java.util.*; @@ -80,6 +81,9 @@ public class MediaServerServiceImpl implements IMediaServerService, CommandLineR logger.info("Media Server 缓存初始化"); List mediaServerItemList = mediaServerMapper.queryAll(); for (MediaServerItem mediaServerItem : mediaServerItemList) { + if (StringUtils.isEmpty(mediaServerItem.getId())) { + continue; + } // 更新 if (mediaServerItem.getSsrcConfig() == null) { SsrcConfig ssrcConfig = new SsrcConfig(mediaServerItem.getId(), null, sipConfig.getDomain()); @@ -283,6 +287,11 @@ public class MediaServerServiceImpl implements IMediaServerService, CommandLineR return result; } + @Override + public int addToDatabase(MediaServerItem mediaSerItem) { + return mediaServerMapper.add(mediaSerItem); + } + /** * 处理zlm上线 * @param zlmServerConfig zlm上线携带的参数 @@ -296,69 +305,84 @@ public class MediaServerServiceImpl implements IMediaServerService, CommandLineR if (serverItem == null) { serverItem = mediaServerMapper.queryOneByHostAndPort(zlmServerConfig.getIp(), zlmServerConfig.getHttpPort()); } - if (zlmServerConfig.getGeneralMediaServerId().equals(mediaConfig.getId()) - || (zlmServerConfig.getIp().equals(mediaConfig.getIp()) && zlmServerConfig.getHttpPort() == mediaConfig.getHttpPort())) { - // 配置文件的zlm - // 如果是配置文件中的zlm。 也就是默认zlm。 一切以配置文件内容为准 - // docker部署不会使用zlm配置的端口号不是默认的则不做更新, 配置修改需要自行修改server配置; - MediaServerItem serverItemFromConfig = mediaConfig.getMediaSerItem(); - serverItemFromConfig.setId(zlmServerConfig.getGeneralMediaServerId()); - if (mediaConfig.getHttpPort() == 0) { - serverItemFromConfig.setHttpPort(zlmServerConfig.getHttpPort()); - } - if (mediaConfig.getHttpSSlPort() == 0) { - serverItemFromConfig.setHttpSSlPort(zlmServerConfig.getHttpSSLport()); - } - if (mediaConfig.getRtmpPort() == 0) { - serverItemFromConfig.setRtmpPort(zlmServerConfig.getRtmpPort()); - } - if (mediaConfig.getRtmpSSlPort() == 0) { - serverItemFromConfig.setRtmpSSlPort(zlmServerConfig.getRtmpSslPort()); - } - if (mediaConfig.getRtspPort() == 0) { - serverItemFromConfig.setRtspPort(zlmServerConfig.getRtspPort()); - } - if (mediaConfig.getRtspSSLPort() == 0) { - serverItemFromConfig.setRtspSSLPort(zlmServerConfig.getRtspSSlport()); - } - if (mediaConfig.getRtpProxyPort() == 0) { - serverItemFromConfig.setRtpProxyPort(zlmServerConfig.getRtpProxyPort()); - } - if (serverItem != null){ - mediaServerMapper.delDefault(); - mediaServerMapper.add(serverItemFromConfig); - String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + serverItemFromConfig.getId(); - MediaServerItem serverItemInRedis = (MediaServerItem)redisUtil.get(key); - if (serverItemInRedis != null) { - serverItemFromConfig.setSsrcConfig(serverItemInRedis.getSsrcConfig()); - }else { - serverItemFromConfig.setSsrcConfig(new SsrcConfig(serverItemFromConfig.getId(), null, sipConfig.getDomain())); - } - redisUtil.set(key, serverItemFromConfig); - }else { - String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + serverItemFromConfig.getId(); - serverItemFromConfig.setSsrcConfig(new SsrcConfig(serverItemFromConfig.getId(), null, sipConfig.getDomain())); - redisUtil.set(key, serverItemFromConfig); - mediaServerMapper.add(serverItemFromConfig); - } - resetOnlineServerItem(serverItemFromConfig); - setZLMConfig(serverItemFromConfig); - }else { - String now = this.format.format(System.currentTimeMillis()); - if (serverItem == null){ - // 一个新的zlm接入wvp - serverItem = new MediaServerItem(zlmServerConfig, sipConfig.getIp()); - serverItem.setCreateTime(now); - serverItem.setUpdateTime(now); - String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + serverItem.getId(); - serverItem.setSsrcConfig(new SsrcConfig(serverItem.getId(), null, sipConfig.getDomain())); - redisUtil.set(key, serverItem); - // 存入数据库 - mediaServerMapper.add(serverItem); - setZLMConfig(serverItem); - } - resetOnlineServerItem(serverItem); + if (serverItem == null) { + logger.warn("[未注册的zlm] 拒接接入:来自{}:{}", zlmServerConfig.getIp(),zlmServerConfig.getHttpPort() ); + return; } + if (StringUtils.isEmpty(serverItem.getId())) { + serverItem.setId(zlmServerConfig.getGeneralMediaServerId()); + redisUtil.set(VideoManagerConstants.MEDIA_SERVER_PREFIX + serverItem.getId(), serverItem); + } + serverItem.setStatus(true); + mediaServerMapper.update(serverItem); + resetOnlineServerItem(serverItem); + setZLMConfig(serverItem); + +// if (zlmServerConfig.getGeneralMediaServerId().equals(mediaConfig.getId()) +// || (zlmServerConfig.getIp().equals(mediaConfig.getIp()) && zlmServerConfig.getHttpPort() == mediaConfig.getHttpPort())) { +// // 配置文件的zlm +// // 如果是配置文件中的zlm。 也就是默认zlm。 一切以配置文件内容为准 +// // wvp互惠修改zlm的端口,需要自行配置。 +// MediaServerItem serverItemFromConfig = mediaConfig.getMediaSerItem(); +// serverItemFromConfig.setId(zlmServerConfig.getGeneralMediaServerId()); +// if (mediaConfig.getHttpPort() == 0) { +// serverItemFromConfig.setHttpPort(zlmServerConfig.getHttpPort()); +// } +// if (mediaConfig.getHttpSSlPort() == 0) { +// serverItemFromConfig.setHttpSSlPort(zlmServerConfig.getHttpSSLport()); +// } +// if (mediaConfig.getRtmpPort() == 0) { +// serverItemFromConfig.setRtmpPort(zlmServerConfig.getRtmpPort()); +// } +// if (mediaConfig.getRtmpSSlPort() == 0) { +// serverItemFromConfig.setRtmpSSlPort(zlmServerConfig.getRtmpSslPort()); +// } +// if (mediaConfig.getRtspPort() == 0) { +// serverItemFromConfig.setRtspPort(zlmServerConfig.getRtspPort()); +// } +// if (mediaConfig.getRtspSSLPort() == 0) { +// serverItemFromConfig.setRtspSSLPort(zlmServerConfig.getRtspSSlport()); +// } +// if (mediaConfig.getRtpProxyPort() == 0) { +// serverItemFromConfig.setRtpProxyPort(zlmServerConfig.getRtpProxyPort()); +// } +// if (serverItem != null){ +// mediaServerMapper.delDefault(); +// mediaServerMapper.add(serverItemFromConfig); +// String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + serverItemFromConfig.getId(); +// MediaServerItem serverItemInRedis = (MediaServerItem)redisUtil.get(key); +// if (serverItemInRedis != null) { +// serverItemFromConfig.setSsrcConfig(serverItemInRedis.getSsrcConfig()); +// }else { +// serverItemFromConfig.setSsrcConfig(new SsrcConfig(serverItemFromConfig.getId(), null, sipConfig.getDomain())); +// } +// redisUtil.set(key, serverItemFromConfig); +// }else { +// String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + serverItemFromConfig.getId(); +// serverItemFromConfig.setSsrcConfig(new SsrcConfig(serverItemFromConfig.getId(), null, sipConfig.getDomain())); +// redisUtil.set(key, serverItemFromConfig); +// mediaServerMapper.add(serverItemFromConfig); +// } +// resetOnlineServerItem(serverItemFromConfig); +// setZLMConfig(serverItemFromConfig); +// } + // 移除未添加的zlm的接入,所有的zlm必须先添加后才可以加入使用 +// else { +// String now = this.format.format(System.currentTimeMillis()); +// if (serverItem == null){ +// // 一个新的zlm接入wvp +// serverItem = new MediaServerItem(zlmServerConfig, sipConfig.getIp()); +// serverItem.setCreateTime(now); +// serverItem.setUpdateTime(now); +// String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + serverItem.getId(); +// serverItem.setSsrcConfig(new SsrcConfig(serverItem.getId(), null, sipConfig.getDomain())); +// redisUtil.set(key, serverItem); +// // 存入数据库 +// mediaServerMapper.add(serverItem); +// setZLMConfig(serverItem); +// } +// resetOnlineServerItem(serverItem); +// } } @Override diff --git a/web_src/src/components/MediaServerManger.vue b/web_src/src/components/MediaServerManger.vue index 07b05236..c412bcb9 100644 --- a/web_src/src/components/MediaServerManger.vue +++ b/web_src/src/components/MediaServerManger.vue @@ -18,7 +18,8 @@
{{item.id}} - 编辑 + 编辑 + 查看 移除
{{item.ip}} @@ -27,6 +28,7 @@
+ 默认 @@ -172,6 +174,13 @@ top: 20px; color: #808080; font-size: 18px; + } + .server-card-default{ + position: absolute; + left: 20px; + top: 20px; + color: #808080; + font-size: 18px; } .server-card:hover { border: 1px solid #adadad; diff --git a/web_src/src/components/dialog/MediaServerEdit.vue b/web_src/src/components/dialog/MediaServerEdit.vue index 8dab76b9..7206bace 100644 --- a/web_src/src/components/dialog/MediaServerEdit.vue +++ b/web_src/src/components/dialog/MediaServerEdit.vue @@ -12,13 +12,13 @@
- + - + - +
@@ -34,34 +34,34 @@ - - + + - - + + - - + + - + - + - + - + - + - + @@ -69,36 +69,36 @@ - + - + - + - + - + - + - - + - + - - + - + - + @@ -108,8 +108,9 @@
- 提交 - 取消 + 提交 + 取消 + 关闭