|  |  |  | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.Set; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.DynamicTask; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.exception.ControllerException; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.service.bean.MediaServerLoad; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.IRedisCatchStorage; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; | 
|---|
|  |  |  | import org.slf4j.Logger; | 
|---|
|  |  |  | import org.slf4j.LoggerFactory; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.transaction.TransactionDefinition; | 
|---|
|  |  |  | import org.springframework.transaction.TransactionStatus; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | import org.springframework.util.StringUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONArray; | 
|---|
|  |  |  | 
|---|
|  |  |  | 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.WVPResult; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import okhttp3.OkHttpClient; | 
|---|
|  |  |  | import okhttp3.Request; | 
|---|
|  |  |  | 
|---|
|  |  |  | public class MediaServerServiceImpl implements IMediaServerService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final static Logger logger = LoggerFactory.getLogger(MediaServerServiceImpl.class); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final String zlmKeepaliveKeyPrefix = "zlm-keepalive_"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SipConfig sipConfig; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ZLMRTPServerFactory zlmrtpServerFactory; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private EventPublisher publisher; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private DynamicTask dynamicTask; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private IRedisCatchStorage redisCatchStorage; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 初始化 | 
|---|
|  |  |  | 
|---|
|  |  |  | logger.info("media server [ {} ] ssrcConfig is null", mediaServerItem.getId()); | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | String ssrc = null; | 
|---|
|  |  |  | String ssrc; | 
|---|
|  |  |  | if (presetSsrc != null) { | 
|---|
|  |  |  | ssrc = presetSsrc; | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | 
|---|
|  |  |  | if (streamId == null) { | 
|---|
|  |  |  | streamId = String.format("%08x", Integer.parseInt(ssrc)).toUpperCase(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | int rtpServerPort = mediaServerItem.getRtpProxyPort(); | 
|---|
|  |  |  | int rtpServerPort; | 
|---|
|  |  |  | if (mediaServerItem.isRtpEnable()) { | 
|---|
|  |  |  | rtpServerPort = zlmrtpServerFactory.createRTPServer(mediaServerItem, streamId, ssrcCheck?Integer.parseInt(ssrc):0, port); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | rtpServerPort = mediaServerItem.getRtpProxyPort(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | RedisUtil.set(key, mediaServerItem); | 
|---|
|  |  |  | return new SSRCInfo(rtpServerPort, ssrc, streamId); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void closeRTPServer(String deviceId, String channelId, String stream) { | 
|---|
|  |  |  | String mediaServerId = streamSession.getMediaServerId(deviceId, channelId, stream); | 
|---|
|  |  |  | String ssrc = streamSession.getSSRC(deviceId, channelId, stream); | 
|---|
|  |  |  | MediaServerItem mediaServerItem = this.getOne(mediaServerId); | 
|---|
|  |  |  | if (mediaServerItem != null) { | 
|---|
|  |  |  | String streamId = String.format("%s_%s", deviceId, channelId); | 
|---|
|  |  |  | zlmrtpServerFactory.closeRTPServer(mediaServerItem, streamId); | 
|---|
|  |  |  | releaseSsrc(mediaServerItem.getId(), ssrc); | 
|---|
|  |  |  | public void closeRTPServer(MediaServerItem mediaServerItem, String streamId) { | 
|---|
|  |  |  | if (mediaServerItem == null) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | streamSession.remove(deviceId, channelId, stream); | 
|---|
|  |  |  | zlmrtpServerFactory.closeRTPServer(mediaServerItem, streamId); | 
|---|
|  |  |  | releaseSsrc(mediaServerItem.getId(), streamId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void closeRTPServer(String mediaServerId, String streamId) { | 
|---|
|  |  |  | MediaServerItem mediaServerItem = this.getOne(mediaServerId); | 
|---|
|  |  |  | closeRTPServer(mediaServerItem, streamId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | if (serverItem.isAutoConfig()) { | 
|---|
|  |  |  | setZLMConfig(serverItem, "0".equals(zlmServerConfig.getHookEnable())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | final String zlmKeepaliveKey = zlmKeepaliveKeyPrefix + serverItem.getId(); | 
|---|
|  |  |  | dynamicTask.stop(zlmKeepaliveKey); | 
|---|
|  |  |  | dynamicTask.startDelay(zlmKeepaliveKey, new KeepAliveTimeoutRunnable(serverItem), (serverItem.getHookAliveInterval() + 5) * 1000); | 
|---|
|  |  |  | publisher.zlmOnlineEventPublish(serverItem.getId()); | 
|---|
|  |  |  | logger.info("[ZLM] 连接成功 {} - {}:{} ", | 
|---|
|  |  |  | zlmServerConfig.getGeneralMediaServerId(), zlmServerConfig.getIp(), zlmServerConfig.getHttpPort()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | class KeepAliveTimeoutRunnable implements Runnable{ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private MediaServerItem serverItem; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public KeepAliveTimeoutRunnable(MediaServerItem serverItem) { | 
|---|
|  |  |  | this.serverItem = serverItem; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void run() { | 
|---|
|  |  |  | logger.info("[zlm心跳到期]:" + serverItem.getId()); | 
|---|
|  |  |  | // 发起http请求验证zlm是否确实无法连接,如果确实无法连接则发送离线事件,否则不作处理 | 
|---|
|  |  |  | JSONObject mediaServerConfig = zlmresTfulUtils.getMediaServerConfig(serverItem); | 
|---|
|  |  |  | if (mediaServerConfig != null && mediaServerConfig.getInteger("code") == 0) { | 
|---|
|  |  |  | logger.info("[zlm心跳到期]:{}验证后zlm仍在线,恢复心跳信息,请检查zlm是否可以正常向wvp发送心跳", serverItem.getId()); | 
|---|
|  |  |  | // 添加zlm信息 | 
|---|
|  |  |  | updateMediaServerKeepalive(serverItem.getId(), mediaServerConfig); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | publisher.zlmOfflineEventPublish(serverItem.getId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void zlmServerOffline(String mediaServerId) { | 
|---|
|  |  |  | delete(mediaServerId); | 
|---|
|  |  |  | final String zlmKeepaliveKey = zlmKeepaliveKeyPrefix + mediaServerId; | 
|---|
|  |  |  | dynamicTask.stop(zlmKeepaliveKey); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | if (RedisUtil.zScore(key, serverItem.getId()) == null) {  // 不存在则设置默认值 已存在则重置 | 
|---|
|  |  |  | RedisUtil.zAdd(key, serverItem.getId(), 0L); | 
|---|
|  |  |  | // 查询服务流数量 | 
|---|
|  |  |  | zlmresTfulUtils.getMediaList(serverItem, null, null, "rtmp",(mediaList ->{ | 
|---|
|  |  |  | zlmresTfulUtils.getMediaList(serverItem, null, null, "rtsp",(mediaList ->{ | 
|---|
|  |  |  | Integer code = mediaList.getInteger("code"); | 
|---|
|  |  |  | if (code == 0) { | 
|---|
|  |  |  | JSONArray data = mediaList.getJSONArray("data"); | 
|---|
|  |  |  | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | clearRTPServer(serverItem); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 获取分数最低的,及并发最低的 | 
|---|
|  |  |  | Set<Object> objects = RedisUtil.ZRange(key, 0, -1); | 
|---|
|  |  |  | Set<Object> objects = RedisUtil.zRange(key, 0, -1); | 
|---|
|  |  |  | ArrayList<Object> mediaServerObjectS = new ArrayList<>(objects); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String mediaServerId = (String)mediaServerObjectS.get(0); | 
|---|
|  |  |  | 
|---|
|  |  |  | mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); | 
|---|
|  |  |  | String protocol = sslEnabled ? "https" : "http"; | 
|---|
|  |  |  | String hookPrex = String.format("%s://%s:%s/index/hook", protocol, mediaServerItem.getHookIp(), serverPort); | 
|---|
|  |  |  | String recordHookPrex = null; | 
|---|
|  |  |  | if (mediaServerItem.getRecordAssistPort() != 0) { | 
|---|
|  |  |  | recordHookPrex = String.format("http://127.0.0.1:%s/api/record", mediaServerItem.getRecordAssistPort()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Map<String, Object> param = new HashMap<>(); | 
|---|
|  |  |  | param.put("api.secret",mediaServerItem.getSecret()); // -profile:v Baseline | 
|---|
|  |  |  | param.put("ffmpeg.cmd","%s -fflags nobuffer -i %s -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264  -f flv %s"); | 
|---|
|  |  |  | param.put("hook.enable","1"); | 
|---|
|  |  |  | param.put("hook.on_flow_report",String.format("%s/on_flow_report", hookPrex)); | 
|---|
|  |  |  | param.put("hook.on_play",String.format("%s/on_play", hookPrex)); | 
|---|
|  |  |  | param.put("hook.on_http_access",String.format("%s/on_http_access", hookPrex)); | 
|---|
|  |  |  | param.put("hook.on_publish", String.format("%s/on_publish", hookPrex)); | 
|---|
|  |  |  | param.put("hook.on_record_mp4",recordHookPrex != null? String.format("%s/on_record_mp4", recordHookPrex): ""); | 
|---|
|  |  |  | param.put("hook.on_record_ts",String.format("%s/on_record_ts", hookPrex)); | 
|---|
|  |  |  | param.put("hook.on_rtsp_auth",String.format("%s/on_rtsp_auth", hookPrex)); | 
|---|
|  |  |  | param.put("hook.on_rtsp_realm",String.format("%s/on_rtsp_realm", hookPrex)); | 
|---|
|  |  |  | 
|---|
|  |  |  | param.put("hook.on_stream_none_reader",String.format("%s/on_stream_none_reader", hookPrex)); | 
|---|
|  |  |  | 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)); | 
|---|
|  |  |  | 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 { | 
|---|
|  |  |  | param.put("hook.on_record_mp4",""); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | param.put("hook.timeoutSec","20"); | 
|---|
|  |  |  | param.put("general.streamNoneReaderDelayMS",mediaServerItem.getStreamNoneReaderDelayMS()==-1?"3600000":mediaServerItem.getStreamNoneReaderDelayMS() ); | 
|---|
|  |  |  | // 推流断开后可以在超时时间内重新连接上继续推流,这样播放器会接着播放。 | 
|---|
|  |  |  | // 置0关闭此特性(推流断开会导致立即断开播放器) | 
|---|
|  |  |  | // 此参数不应大于播放器超时时间 | 
|---|
|  |  |  | 
|---|
|  |  |  | param.put("general.continue_push_ms", "3000" ); | 
|---|
|  |  |  | // 最多等待未初始化的Track时间,单位毫秒,超时之后会忽略未初始化的Track, 设置此选项优化那些音频错误的不规范流, | 
|---|
|  |  |  | // 等zlm支持给每个rtpServer设置关闭音频的时候可以不设置此选项 | 
|---|
|  |  |  | param.put("general.wait_track_ready_ms", "3000" ); | 
|---|
|  |  |  | //        param.put("general.wait_track_ready_ms", "3000" ); | 
|---|
|  |  |  | if (mediaServerItem.isRtpEnable() && !ObjectUtils.isEmpty(mediaServerItem.getRtpPortRange())) { | 
|---|
|  |  |  | param.put("rtp_proxy.port_range", mediaServerItem.getRtpPortRange().replace(",", "-")); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | mediaServerItem.setStreamIp(ip); | 
|---|
|  |  |  | mediaServerItem.setHookIp(sipConfig.getIp()); | 
|---|
|  |  |  | mediaServerItem.setSdpIp(ip); | 
|---|
|  |  |  | mediaServerItem.setStreamNoneReaderDelayMS(zlmServerConfig.getGeneralStreamNoneReaderDelayMS()); | 
|---|
|  |  |  | return mediaServerItem; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | MediaServerItem mediaServerItem = getOne(mediaServerId); | 
|---|
|  |  |  | if (mediaServerItem == null) { | 
|---|
|  |  |  | // 缓存不存在,从数据库查询,如果数据库不存在则是错误的 | 
|---|
|  |  |  | MediaServerItem mediaServerItemFromDatabase = getOneFromDatabase(mediaServerId); | 
|---|
|  |  |  | if (mediaServerItemFromDatabase == null) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // zlm连接重试 | 
|---|
|  |  |  | logger.warn("[更新ZLM 保活信息]失败,未找到流媒体信息,尝试重连zlm"); | 
|---|
|  |  |  | //            reloadZlm(); | 
|---|
|  |  |  | mediaServerItem = getOne(mediaServerId); | 
|---|
|  |  |  | mediaServerItem = getOneFromDatabase(mediaServerId); | 
|---|
|  |  |  | if (mediaServerItem == null) { | 
|---|
|  |  |  | // zlm连接重试 | 
|---|
|  |  |  | logger.warn("[更新ZLM 保活信息]失败,未找到流媒体信息"); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // zlm连接重试 | 
|---|
|  |  |  | logger.warn("[更新ZLM 保活信息]尝试链接zml id {}", mediaServerId); | 
|---|
|  |  |  | SsrcConfig ssrcConfig = new SsrcConfig(mediaServerItem.getId(), null, sipConfig.getDomain()); | 
|---|
|  |  |  | mediaServerItem.setSsrcConfig(ssrcConfig); | 
|---|
|  |  |  | String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerItem.getId(); | 
|---|
|  |  |  | RedisUtil.set(key, mediaServerItem); | 
|---|
|  |  |  | clearRTPServer(mediaServerItem); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String key = VideoManagerConstants.MEDIA_SERVER_KEEPALIVE_PREFIX + userSetting.getServerId() + "_" + mediaServerId; | 
|---|
|  |  |  | int hookAliveInterval = mediaServerItem.getHookAliveInterval() + 2; | 
|---|
|  |  |  | RedisUtil.set(key, data, hookAliveInterval); | 
|---|
|  |  |  | final String zlmKeepaliveKey = zlmKeepaliveKeyPrefix + mediaServerItem.getId(); | 
|---|
|  |  |  | dynamicTask.stop(zlmKeepaliveKey); | 
|---|
|  |  |  | dynamicTask.startDelay(zlmKeepaliveKey, new KeepAliveTimeoutRunnable(mediaServerItem), (mediaServerItem.getHookAliveInterval() + 5) * 1000); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private MediaServerItem getOneFromDatabase(String mediaServerId) { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public boolean checkRtpServer(MediaServerItem mediaServerItem, String app, String stream) { | 
|---|
|  |  |  | JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaServerItem, stream); | 
|---|
|  |  |  | if(rtpInfo.getInteger("code") == 0){ | 
|---|
|  |  |  | return rtpInfo.getBoolean("exist"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public MediaServerLoad getLoad(MediaServerItem mediaServerItem) { | 
|---|
|  |  |  | MediaServerLoad result = new MediaServerLoad(); | 
|---|
|  |  |  | result.setId(mediaServerItem.getId()); | 
|---|
|  |  |  | result.setPush(redisCatchStorage.getPushStreamCount(mediaServerItem.getId())); | 
|---|
|  |  |  | result.setProxy(redisCatchStorage.getProxyStreamCount(mediaServerItem.getId())); | 
|---|
|  |  |  | result.setGbReceive(redisCatchStorage.getGbReceiveCount(mediaServerItem.getId())); | 
|---|
|  |  |  | result.setGbSend(redisCatchStorage.getGbSendCount(mediaServerItem.getId())); | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|