|  |  |  | 
|---|
|  |  |  | 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.bean.SsrcTransaction; | 
|---|
|  |  |  | 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.service.bean.SSRCInfo; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.IVideoManagerStorage; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.dao.MediaServerMapper; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.utils.DateUtil; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.utils.redis.JedisUtil; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.utils.redis.RedisUtil; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.vmanager.bean.WVPResult; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.text.ParseException; | 
|---|
|  |  |  | import java.text.SimpleDateFormat; | 
|---|
|  |  |  | import java.time.LocalDateTime; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 媒体服务器节点管理 | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | JedisUtil jedisUtil; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 初始化 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void updateVmServer(List<MediaServerItem>  mediaServerItemList) { | 
|---|
|  |  |  | logger.info("[缓存初始化] Media Server "); | 
|---|
|  |  |  | logger.info("[zlm] 缓存初始化 "); | 
|---|
|  |  |  | for (MediaServerItem mediaServerItem : mediaServerItemList) { | 
|---|
|  |  |  | if (StringUtils.isEmpty(mediaServerItem.getId())) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public SSRCInfo openRTPServer(MediaServerItem mediaServerItem, String streamId, boolean ssrcCheck) { | 
|---|
|  |  |  | return openRTPServer(mediaServerItem, streamId, null, ssrcCheck,false); | 
|---|
|  |  |  | public SSRCInfo openRTPServer(MediaServerItem mediaServerItem, String streamId, boolean ssrcCheck, boolean isPlayback) { | 
|---|
|  |  |  | return openRTPServer(mediaServerItem, streamId, null, ssrcCheck,isPlayback); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | public void clearRTPServer(MediaServerItem mediaServerItem) { | 
|---|
|  |  |  | mediaServerItem.setSsrcConfig(new SsrcConfig(mediaServerItem.getId(), null, sipConfig.getDomain())); | 
|---|
|  |  |  | redisUtil.zAdd(VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(), mediaServerItem.getId(), 0); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | result.sort((serverItem1, serverItem2)->{ | 
|---|
|  |  |  | int sortResult = 0; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | sortResult = format.parse(serverItem1.getCreateTime()).compareTo(format.parse(serverItem2.getCreateTime())); | 
|---|
|  |  |  | } catch (ParseException e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | LocalDateTime localDateTime1 = LocalDateTime.parse(serverItem1.getCreateTime(), DateUtil.formatter); | 
|---|
|  |  |  | LocalDateTime localDateTime2 = LocalDateTime.parse(serverItem2.getCreateTime(), DateUtil.formatter); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | sortResult = localDateTime1.compareTo(localDateTime2); | 
|---|
|  |  |  | return  sortResult; | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public WVPResult<String> add(MediaServerItem mediaServerItem) { | 
|---|
|  |  |  | WVPResult<String> result = new WVPResult<>(); | 
|---|
|  |  |  | mediaServerItem.setCreateTime(this.format.format(System.currentTimeMillis())); | 
|---|
|  |  |  | mediaServerItem.setUpdateTime(this.format.format(System.currentTimeMillis())); | 
|---|
|  |  |  | mediaServerItem.setCreateTime(DateUtil.getNow()); | 
|---|
|  |  |  | mediaServerItem.setUpdateTime(DateUtil.getNow()); | 
|---|
|  |  |  | mediaServerItem.setHookAliveInterval(120); | 
|---|
|  |  |  | JSONObject responseJSON = zlmresTfulUtils.getMediaServerConfig(mediaServerItem); | 
|---|
|  |  |  | if (responseJSON != null) { | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void zlmServerOnline(ZLMServerConfig zlmServerConfig) { | 
|---|
|  |  |  | logger.info("[ ZLM:{} ]-[ {}:{} ]正在连接", | 
|---|
|  |  |  | logger.info("[ZLM] 正在连接 : {} -> {}:{}", | 
|---|
|  |  |  | zlmServerConfig.getGeneralMediaServerId(), zlmServerConfig.getIp(), zlmServerConfig.getHttpPort()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | MediaServerItem serverItem = mediaServerMapper.queryOne(zlmServerConfig.getGeneralMediaServerId()); | 
|---|
|  |  |  | 
|---|
|  |  |  | setZLMConfig(serverItem, "0".equals(zlmServerConfig.getHookEnable())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | publisher.zlmOnlineEventPublish(serverItem.getId()); | 
|---|
|  |  |  | logger.info("[ ZLM:{} ]-[ {}:{} ]连接成功", | 
|---|
|  |  |  | logger.info("[ZLM] 连接成功 {} - {}:{} ", | 
|---|
|  |  |  | zlmServerConfig.getGeneralMediaServerId(), zlmServerConfig.getIp(), zlmServerConfig.getHttpPort()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void setZLMConfig(MediaServerItem mediaServerItem, boolean restart) { | 
|---|
|  |  |  | logger.info("[ ZLM:{} ]-[ {}:{} ]正在设置zlm", | 
|---|
|  |  |  | logger.info("[ZLM] 正在设置 :{} -> {}:{}", | 
|---|
|  |  |  | mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); | 
|---|
|  |  |  | String protocol = sslEnabled ? "https" : "http"; | 
|---|
|  |  |  | String hookPrex = String.format("%s://%s:%s/index/hook", protocol, mediaServerItem.getHookIp(), serverPort); | 
|---|
|  |  |  | 
|---|
|  |  |  | 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",""); | 
|---|
|  |  |  | 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",""); | 
|---|
|  |  |  | 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",""); | 
|---|
|  |  |  | param.put("hook.on_rtsp_auth",""); | 
|---|
|  |  |  | param.put("hook.on_rtsp_realm",""); | 
|---|
|  |  |  | 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_server_started",String.format("%s/on_server_started", hookPrex)); | 
|---|
|  |  |  | param.put("hook.on_shell_login",String.format("%s/on_shell_login", hookPrex)); | 
|---|
|  |  |  | param.put("hook.on_stream_changed",String.format("%s/on_stream_changed", hookPrex)); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (responseJSON != null && responseJSON.getInteger("code") == 0) { | 
|---|
|  |  |  | if (restart) { | 
|---|
|  |  |  | logger.info("[ ZLM:{} ]-[ {}:{} ]设置zlm成功, 开始重启以保证配置生效", | 
|---|
|  |  |  | logger.info("[ZLM] 设置成功,开始重启以保证配置生效 {} -> {}:{}", | 
|---|
|  |  |  | mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); | 
|---|
|  |  |  | zlmresTfulUtils.restartServer(mediaServerItem); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | logger.info("[ ZLM:{} ]-[ {}:{} ]设置zlm成功", | 
|---|
|  |  |  | logger.info("[ZLM] 设置成功 {} -> {}:{}", | 
|---|
|  |  |  | mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | logger.info("[ ZLM:{} ]-[ {}:{} ]设置zlm失败", | 
|---|
|  |  |  | logger.info("[ZLM] 设置zlm失败 {} -> {}:{}", | 
|---|
|  |  |  | mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|