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 8da6df8b..59749189 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 @@ -8,6 +8,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.genersoft.iot.vmp.media.zlm.ZLMRunner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -54,6 +55,9 @@ public class MediaServerServiceImpl implements IMediaServerService { @Autowired private SipConfig sipConfig; + @Autowired + private ZLMRunner zlmRunner; + @Value("${server.ssl.enabled:false}") private boolean sslEnabled; @@ -279,7 +283,9 @@ public class MediaServerServiceImpl implements IMediaServerService { String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerId; MediaServerItem serverItem=(MediaServerItem)redisUtil.get(key); if(null==serverItem){ - serverItem=mediaServerMapper.queryOne(mediaServerId); + //zlm服务不在线,启动重连 + reloadZlm(); + serverItem=(MediaServerItem)redisUtil.get(key); } return serverItem; } @@ -474,8 +480,13 @@ public class MediaServerServiceImpl implements IMediaServerService { String key = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); if (redisUtil.zSize(key) == null || redisUtil.zSize(key) == 0) { - logger.info("获取负载最低的节点时无在线节点"); - return null; + logger.info("获取负载最低的节点时无在线节点,启动重连机制"); + //启动重连 + reloadZlm(); + if (redisUtil.zSize(key) == null || redisUtil.zSize(key) == 0) { + logger.info("获取负载最低的节点时无在线节点"); + return null; + } } // 获取分数最低的,及并发最低的 @@ -637,8 +648,14 @@ public class MediaServerServiceImpl implements IMediaServerService { MediaServerItem mediaServerItem = getOne(mediaServerId); if (mediaServerItem == null) { // zlm连接重试 - logger.warn("[更新ZLM 保活信息]失败,未找到流媒体信息"); - return; + logger.warn("[更新ZLM 保活信息]失败,未找到流媒体信息,尝试重连zlm"); + reloadZlm(); + mediaServerItem = getOne(mediaServerId); + if (mediaServerItem == null) { + // zlm连接重试 + logger.warn("[更新ZLM 保活信息]失败,未找到流媒体信息"); + return; + } } String key = VideoManagerConstants.MEDIA_SERVER_KEEPALIVE_PREFIX + userSetting.getServerId() + "_" + mediaServerId; int hookAliveInterval = mediaServerItem.getHookAliveInterval() + 2; @@ -661,4 +678,12 @@ public class MediaServerServiceImpl implements IMediaServerService { } } + public void reloadZlm(){ + try { + zlmRunner.run(); + Thread.sleep(500);//延迟0.5秒缓冲时间 + } catch (Exception e) { + logger.warn("尝试重连zlm失败!",e); + } + } } diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/media/MediaController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/media/MediaController.java index 94fe8df2..48973f9c 100644 --- a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/media/MediaController.java +++ b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/media/MediaController.java @@ -6,6 +6,7 @@ import com.genersoft.iot.vmp.conf.security.dto.LoginUser; import com.genersoft.iot.vmp.media.zlm.dto.OnPublishHookParam; import com.genersoft.iot.vmp.media.zlm.dto.StreamAuthorityInfo; import com.genersoft.iot.vmp.service.IMediaServerService; +import com.genersoft.iot.vmp.service.IStreamProxyService; import com.genersoft.iot.vmp.service.IStreamPushService; import com.genersoft.iot.vmp.service.IMediaService; import com.genersoft.iot.vmp.storager.IRedisCatchStorage; @@ -37,6 +38,8 @@ public class MediaController { @Autowired private IMediaService mediaService; + @Autowired + private IStreamProxyService streamProxyService; /** @@ -95,8 +98,30 @@ public class MediaController { result.setMsg("scccess"); result.setData(streamInfo); }else { - result.setCode(-1); - result.setMsg("fail"); + //获取流失败,重启拉流后重试一次 + streamProxyService.stop(app,stream); + boolean start = streamProxyService.start(app, stream); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if (useSourceIpAsStreamIp != null && useSourceIpAsStreamIp) { + String host = request.getHeader("Host"); + String localAddr = host.split(":")[0]; + logger.info("使用{}作为返回流的ip", localAddr); + streamInfo = mediaService.getStreamInfoByAppAndStreamWithCheck(app, stream, mediaServerId, localAddr, authority); + }else { + streamInfo = mediaService.getStreamInfoByAppAndStreamWithCheck(app, stream, mediaServerId, authority); + } + if (streamInfo != null){ + result.setCode(0); + result.setMsg("scccess"); + result.setData(streamInfo); + }else { + result.setCode(-1); + result.setMsg("fail"); + } } return result; }