|  |  |  | 
|---|
|  |  |  | package com.genersoft.iot.vmp.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.time.LocalDateTime; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Collections; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.Set; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSON; | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSONArray; | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSONObject; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.common.VideoManagerConstants; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.DynamicTask; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.SipConfig; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.UserSetting; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.exception.ControllerException; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.event.EventPublisher; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.session.SsrcConfig; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.ZLMServerConfig; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.dto.ServerKeepaliveData; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.service.IMediaServerService; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.service.bean.MediaServerLoad; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.service.bean.SSRCInfo; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.IRedisCatchStorage; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.dao.MediaServerMapper; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.utils.DateUtil; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.utils.redis.RedisUtil; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; | 
|---|
|  |  |  | import okhttp3.OkHttpClient; | 
|---|
|  |  |  | import okhttp3.Request; | 
|---|
|  |  |  | import okhttp3.Response; | 
|---|
|  |  |  | import org.slf4j.Logger; | 
|---|
|  |  |  | import org.slf4j.LoggerFactory; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.transaction.TransactionStatus; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSON; | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSONArray; | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSONObject; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.common.VideoManagerConstants; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.SipConfig; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.UserSetting; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.event.EventPublisher; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.session.SsrcConfig; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.ZLMServerConfig; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.service.IMediaServerService; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.service.bean.SSRCInfo; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.dao.MediaServerMapper; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.utils.DateUtil; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.utils.redis.RedisUtil; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import okhttp3.OkHttpClient; | 
|---|
|  |  |  | import okhttp3.Request; | 
|---|
|  |  |  | import okhttp3.Response; | 
|---|
|  |  |  | import java.time.LocalDateTime; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 媒体服务器节点管理 | 
|---|
|  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public SSRCInfo openRTPServer(MediaServerItem mediaServerItem, String streamId, String presetSsrc, boolean ssrcCheck, boolean isPlayback, Integer port) { | 
|---|
|  |  |  | if (mediaServerItem == null || mediaServerItem.getId() == null) { | 
|---|
|  |  |  | logger.info("[openRTPServer] 失败, mediaServerItem == null || mediaServerItem.getId() == null"); | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 获取mediaServer可用的ssrc | 
|---|
|  |  |  | 
|---|
|  |  |  | if (mediaServerItem == null) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | zlmrtpServerFactory.closeRTPServer(mediaServerItem, streamId); | 
|---|
|  |  |  | releaseSsrc(mediaServerItem.getId(), streamId); | 
|---|
|  |  |  | zlmrtpServerFactory.closeRtpServer(mediaServerItem, streamId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | public void add(MediaServerItem mediaServerItem) { | 
|---|
|  |  |  | mediaServerItem.setCreateTime(DateUtil.getNow()); | 
|---|
|  |  |  | mediaServerItem.setUpdateTime(DateUtil.getNow()); | 
|---|
|  |  |  | mediaServerItem.setHookAliveInterval(120); | 
|---|
|  |  |  | mediaServerItem.setHookAliveInterval(30f); | 
|---|
|  |  |  | JSONObject responseJSON = zlmresTfulUtils.getMediaServerConfig(mediaServerItem); | 
|---|
|  |  |  | if (responseJSON != null) { | 
|---|
|  |  |  | JSONArray data = responseJSON.getJSONArray("data"); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | final String zlmKeepaliveKey = zlmKeepaliveKeyPrefix + serverItem.getId(); | 
|---|
|  |  |  | dynamicTask.stop(zlmKeepaliveKey); | 
|---|
|  |  |  | dynamicTask.startDelay(zlmKeepaliveKey, new KeepAliveTimeoutRunnable(serverItem), (serverItem.getHookAliveInterval() + 5) * 1000); | 
|---|
|  |  |  | dynamicTask.startDelay(zlmKeepaliveKey, new KeepAliveTimeoutRunnable(serverItem), (Math.getExponent(serverItem.getHookAliveInterval()) + 5) * 1000); | 
|---|
|  |  |  | publisher.zlmOnlineEventPublish(serverItem.getId()); | 
|---|
|  |  |  | logger.info("[ZLM] 连接成功 {} - {}:{} ", | 
|---|
|  |  |  | zlmServerConfig.getGeneralMediaServerId(), zlmServerConfig.getIp(), zlmServerConfig.getHttpPort()); | 
|---|
|  |  |  | 
|---|
|  |  |  | param.put("hook.on_stream_not_found",String.format("%s/on_stream_not_found", hookPrex)); | 
|---|
|  |  |  | param.put("hook.on_server_keepalive",String.format("%s/on_server_keepalive", hookPrex)); | 
|---|
|  |  |  | param.put("hook.on_send_rtp_stopped",String.format("%s/on_send_rtp_stopped", hookPrex)); | 
|---|
|  |  |  | param.put("hook.on_rtp_server_timeout",String.format("%s/on_rtp_server_timeout", hookPrex)); | 
|---|
|  |  |  | if (mediaServerItem.getRecordAssistPort() > 0) { | 
|---|
|  |  |  | param.put("hook.on_record_mp4",String.format("http://127.0.0.1:%s/api/record/on_record_mp4", mediaServerItem.getRecordAssistPort())); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | 
|---|
|  |  |  | // 置0关闭此特性(推流断开会导致立即断开播放器) | 
|---|
|  |  |  | // 此参数不应大于播放器超时时间 | 
|---|
|  |  |  | // 优化此消息以更快的收到流注销事件 | 
|---|
|  |  |  | param.put("general.continue_push_ms", "3000" ); | 
|---|
|  |  |  | param.put("general.publishToHls", "0" ); | 
|---|
|  |  |  | param.put("protocol.continue_push_ms", "3000" ); | 
|---|
|  |  |  | // 最多等待未初始化的Track时间,单位毫秒,超时之后会忽略未初始化的Track, 设置此选项优化那些音频错误的不规范流, | 
|---|
|  |  |  | // 等zlm支持给每个rtpServer设置关闭音频的时候可以不设置此选项 | 
|---|
|  |  |  | //        param.put("general.wait_track_ready_ms", "3000" ); | 
|---|
|  |  |  | 
|---|
|  |  |  | mediaServerItem.setRtspSSLPort(zlmServerConfig.getRtspSSlport()); | 
|---|
|  |  |  | mediaServerItem.setRtpProxyPort(zlmServerConfig.getRtpProxyPort()); | 
|---|
|  |  |  | mediaServerItem.setStreamIp(ip); | 
|---|
|  |  |  | mediaServerItem.setHookIp(sipConfig.getIp()); | 
|---|
|  |  |  | mediaServerItem.setHookIp(sipConfig.getIp().split(",")[0]); | 
|---|
|  |  |  | mediaServerItem.setSdpIp(ip); | 
|---|
|  |  |  | return mediaServerItem; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | final String zlmKeepaliveKey = zlmKeepaliveKeyPrefix + mediaServerItem.getId(); | 
|---|
|  |  |  | dynamicTask.stop(zlmKeepaliveKey); | 
|---|
|  |  |  | dynamicTask.startDelay(zlmKeepaliveKey, new KeepAliveTimeoutRunnable(mediaServerItem), (mediaServerItem.getHookAliveInterval() + 5) * 1000); | 
|---|
|  |  |  | dynamicTask.startDelay(zlmKeepaliveKey, new KeepAliveTimeoutRunnable(mediaServerItem), (mediaServerItem.getHookAliveInterval().intValue() + 5) * 1000); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private MediaServerItem getOneFromDatabase(String mediaServerId) { | 
|---|