648540858
2022-09-02 7db2bf7b51015a7e989ea5b0f4d9486dfd99d4bd
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java
@@ -7,12 +7,10 @@
import com.alibaba.fastjson.JSON;
import com.genersoft.iot.vmp.common.StreamInfo;
import com.genersoft.iot.vmp.conf.UserSetting;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.gb28181.bean.GbStream;
import com.genersoft.iot.vmp.gb28181.bean.SsrcTransaction;
import com.genersoft.iot.vmp.gb28181.bean.*;
import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform;
import com.genersoft.iot.vmp.media.zlm.dto.*;
import com.genersoft.iot.vmp.service.*;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
@@ -20,9 +18,11 @@
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.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -47,6 +47,9 @@
   @Autowired
   private SIPCommander cmder;
   @Autowired
   private SIPCommanderFroPlatform commanderFroPlatform;
   @Autowired
   private IPlayService playService;
@@ -76,7 +79,7 @@
    private ZLMMediaListManager zlmMediaListManager;
   @Autowired
   private ZLMHttpHookSubscribe subscribe;
   private ZlmHttpHookSubscribe subscribe;
   @Autowired
   private UserSetting userSetting;
@@ -90,6 +93,10 @@
   @Autowired
   private AssistRESTfulUtils assistRESTfulUtils;
   @Qualifier("taskExecutor")
   @Autowired
   private ThreadPoolTaskExecutor taskExecutor;
   /**
    * 服务器定时上报时间,上报间隔可配置,默认10s上报一次
    *
@@ -98,16 +105,15 @@
   @PostMapping(value = "/on_server_keepalive", produces = "application/json;charset=UTF-8")
   public ResponseEntity<String> onServerKeepalive(@RequestBody JSONObject json){
      if (logger.isDebugEnabled()) {
         logger.debug("[ ZLM HOOK ] on_server_keepalive API调用,参数:" + json.toString());
      }
      logger.info("[ ZLM HOOK ] on_server_keepalive API调用,参数:" + json.toString());
      String mediaServerId = json.getString("mediaServerId");
      List<ZLMHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(ZLMHttpHookSubscribe.HookType.on_server_keepalive);
      List<ZlmHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(HookType.on_server_keepalive);
      if (subscribes != null  && subscribes.size() > 0) {
         for (ZLMHttpHookSubscribe.Event subscribe : subscribes) {
         for (ZlmHttpHookSubscribe.Event subscribe : subscribes) {
            subscribe.response(null, json);
         }
      }
      mediaServerService.updateMediaServerKeepalive(mediaServerId, json.getJSONObject("data"));
      JSONObject ret = new JSONObject();
      ret.put("code", 0);
@@ -167,7 +173,7 @@
         logger.debug("[ ZLM HOOK ]on_play API调用,参数:" + JSON.toJSONString(param));
      }
      String mediaServerId = param.getMediaServerId();
      ZLMHttpHookSubscribe.Event subscribe = this.subscribe.getSubscribe(ZLMHttpHookSubscribe.HookType.on_play, json);
      ZlmHttpHookSubscribe.Event subscribe = this.subscribe.sendNotify(HookType.on_play, json);
      if (subscribe != null ) {
         MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
         if (mediaInfo != null) {
@@ -178,8 +184,7 @@
      if (!"rtp".equals(param.getApp())) {
         Map<String, String> paramMap = urlParamToMap(param.getParams());
         StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(param.getApp(), param.getStream());
         if (streamAuthorityInfo == null
               || (streamAuthorityInfo.getCallId() != null && !streamAuthorityInfo.getCallId().equals(paramMap.get("callId")))) {
         if (streamAuthorityInfo != null && streamAuthorityInfo.getCallId() != null && !streamAuthorityInfo.getCallId().equals(paramMap.get("callId"))) {
            ret.put("code", 401);
            ret.put("msg", "Unauthorized");
            return new ResponseEntity<>(ret.toString(),HttpStatus.OK);
@@ -237,9 +242,14 @@
         // 鉴权通过
         redisCatchStorage.updateStreamAuthorityInfo(param.getApp(), param.getStream(), streamAuthorityInfo);
         // 通知assist新的callId
         if (mediaInfo != null) {
            assistRESTfulUtils.addStreamCallInfo(mediaInfo, param.getApp(), param.getStream(), callId, null);
         }
         taskExecutor.execute(()->{
            if (mediaInfo != null && mediaInfo.getRecordAssistPort() > 0) {
               assistRESTfulUtils.addStreamCallInfo(mediaInfo, param.getApp(), param.getStream(), callId, null);
            }
         });
      }else {
         zlmMediaListManager.sendStreamEvent(param.getApp(),param.getStream(), param.getMediaServerId());
      }
      ret.put("code", 0);
@@ -250,7 +260,7 @@
      }
      ZLMHttpHookSubscribe.Event subscribe = this.subscribe.getSubscribe(ZLMHttpHookSubscribe.HookType.on_publish, json);
      ZlmHttpHookSubscribe.Event subscribe = this.subscribe.sendNotify(HookType.on_publish, json);
      if (subscribe != null) {
         if (mediaInfo != null) {
            subscribe.response(mediaInfo, json);
@@ -374,7 +384,7 @@
         logger.debug("[ ZLM HOOK ]on_shell_login API调用,参数:" + json.toString());
      }
      String mediaServerId = json.getString("mediaServerId");
      ZLMHttpHookSubscribe.Event subscribe = this.subscribe.getSubscribe(ZLMHttpHookSubscribe.HookType.on_shell_login, json);
      ZlmHttpHookSubscribe.Event subscribe = this.subscribe.sendNotify(HookType.on_shell_login, json);
      if (subscribe != null ) {
         MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
         if (mediaInfo != null) {
@@ -400,7 +410,7 @@
      logger.info("[ ZLM HOOK ]on_stream_changed API调用,参数:" + JSONObject.toJSONString(item));
      String mediaServerId = item.getMediaServerId();
      JSONObject json = (JSONObject) JSON.toJSON(item);
      ZLMHttpHookSubscribe.Event subscribe = this.subscribe.getSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, json);
      ZlmHttpHookSubscribe.Event subscribe = this.subscribe.sendNotify(HookType.on_stream_changed, json);
      if (subscribe != null ) {
         MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
         if (mediaInfo != null) {
@@ -413,19 +423,24 @@
      String schema = item.getSchema();
      List<MediaItem.MediaTrack> tracks = item.getTracks();
      boolean regist = item.isRegist();
      if (regist) {
         StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(app, stream);
         if (streamAuthorityInfo == null) {
            streamAuthorityInfo = StreamAuthorityInfo.getInstanceByHook(item);
      if (item.getOriginType() == OriginType.RTMP_PUSH.ordinal()
            || item.getOriginType() == OriginType.RTSP_PUSH.ordinal()
            || item.getOriginType() == OriginType.RTC_PUSH.ordinal()) {
         if (regist) {
            StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(app, stream);
            if (streamAuthorityInfo == null) {
               streamAuthorityInfo = StreamAuthorityInfo.getInstanceByHook(item);
            }else {
               streamAuthorityInfo.setOriginType(item.getOriginType());
               streamAuthorityInfo.setOriginTypeStr(item.getOriginTypeStr());
            }
            redisCatchStorage.updateStreamAuthorityInfo(app, stream, streamAuthorityInfo);
         }else {
            streamAuthorityInfo.setOriginType(item.getOriginType());
            streamAuthorityInfo.setOriginTypeStr(item.getOriginTypeStr());
            redisCatchStorage.removeStreamAuthorityInfo(app, stream);
         }
         redisCatchStorage.updateStreamAuthorityInfo(app, stream, streamAuthorityInfo);
      }else {
         redisCatchStorage.removeStreamAuthorityInfo(app, stream);
      }
      if ("rtmp".equals(schema)){
      if ("rtsp".equals(schema)){
         logger.info("on_stream_changed:注册->{}, app->{}, stream->{}", regist, app, stream);
         if (regist) {
            mediaServerService.addCount(mediaServerId);
@@ -442,13 +457,15 @@
            if (streamInfo!=null){
               redisCatchStorage.stopPlay(streamInfo);
               storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
               // 如果正在给上级推送,则发送bye
            }else{
               streamInfo = redisCatchStorage.queryPlayback(null, null, stream, null);
               if (streamInfo != null) {
                  redisCatchStorage.stopPlayback(streamInfo.getDeviceID(), streamInfo.getChannelId(),
                        streamInfo.getStream(), null);
               }
               // 如果正在给上级推送,则发送bye
            }
         }else {
            if (!"rtp".equals(app)){
@@ -458,10 +475,13 @@
               if (mediaServerItem != null){
                  if (regist) {
                     StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(app, stream);
                     String callId = null;
                     if (streamAuthorityInfo != null) {
                        callId = streamAuthorityInfo.getCallId();
                     }
                     StreamInfo streamInfoByAppAndStream = mediaService.getStreamInfoByAppAndStream(mediaServerItem,
                           app, stream, tracks, streamAuthorityInfo.getCallId());
                           app, stream, tracks, callId);
                     item.setStreamInfo(streamInfoByAppAndStream);
                     redisCatchStorage.addStream(mediaServerItem, type, app, stream, item);
                     if (item.getOriginType() == OriginType.RTSP_PUSH.ordinal()
                           || item.getOriginType() == OriginType.RTMP_PUSH.ordinal()
@@ -469,20 +489,6 @@
                        item.setSeverId(userSetting.getServerId());
                        zlmMediaListManager.addPush(item);
                     }
//                     List<GbStream> gbStreams = new ArrayList<>();
//                     if (streamPushItem == null || streamPushItem.getGbId() == null) {
//                        GbStream gbStream = storager.getGbStream(app, streamId);
//                        gbStreams.add(gbStream);
//                     }else {
//                        if (streamPushItem.getGbId() != null) {
//                           gbStreams.add(streamPushItem);
//                        }
//                     }
//                     if (gbStreams.size() > 0) {
//                        eventPublisher.catalogEventPublishForStream(null, gbStreams, CatalogEvent.ON);
//                     }
                  }else {
                     // 兼容流注销时类型从redis记录获取
                     MediaItem mediaItem = redisCatchStorage.getStreamInfo(app, stream, mediaServerId);
@@ -534,17 +540,21 @@
      if ("rtp".equals(app)){
         ret.put("close", true);
         StreamInfo streamInfoForPlayCatch = redisCatchStorage.queryPlayByStreamId(streamId);
         SsrcTransaction ssrcTransaction = sessionManager.getSsrcTransaction(null, null, null, streamId);
         if (streamInfoForPlayCatch != null) {
            // 如果在给上级推流,也不停止。
            // 收到无人观看说明流也没有在往上级推送
            if (redisCatchStorage.isChannelSendingRTP(streamInfoForPlayCatch.getChannelId())) {
               ret.put("close", false);
            } else {
               cmder.streamByeCmd(streamInfoForPlayCatch.getDeviceID(), streamInfoForPlayCatch.getChannelId(),
                     streamInfoForPlayCatch.getStream(), null);
               redisCatchStorage.stopPlay(streamInfoForPlayCatch);
               storager.stopPlay(streamInfoForPlayCatch.getDeviceID(), streamInfoForPlayCatch.getChannelId());
               List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServerByChnnelId(streamInfoForPlayCatch.getChannelId());
               if (sendRtpItems.size() > 0) {
                  for (SendRtpItem sendRtpItem : sendRtpItems) {
                     ParentPlatform parentPlatform = storager.queryParentPlatByServerGBId(sendRtpItem.getPlatformId());
                     commanderFroPlatform.streamByeCmd(parentPlatform, sendRtpItem.getCallId());
                  }
               }
            }
            cmder.streamByeCmd(streamInfoForPlayCatch.getDeviceID(), streamInfoForPlayCatch.getChannelId(),
                  streamInfoForPlayCatch.getStream(), null);
            redisCatchStorage.stopPlay(streamInfoForPlayCatch);
            storager.stopPlay(streamInfoForPlayCatch.getDeviceID(), streamInfoForPlayCatch.getChannelId());
         }else{
            StreamInfo streamInfoForPlayBackCatch = redisCatchStorage.queryPlayback(null, null, streamId, null);
            if (streamInfoForPlayBackCatch != null) {
@@ -626,21 +636,26 @@
      }
      String remoteAddr = request.getRemoteAddr();
      jsonObject.put("ip", remoteAddr);
      List<ZLMHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(ZLMHttpHookSubscribe.HookType.on_server_started);
      List<ZlmHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(HookType.on_server_started);
      if (subscribes != null  && subscribes.size() > 0) {
         for (ZLMHttpHookSubscribe.Event subscribe : subscribes) {
         for (ZlmHttpHookSubscribe.Event subscribe : subscribes) {
            subscribe.response(null, jsonObject);
         }
      }
      ZLMServerConfig zlmServerConfig = JSONObject.toJavaObject(jsonObject, ZLMServerConfig.class);
      if (zlmServerConfig !=null ) {
         mediaServerService.zlmServerOnline(zlmServerConfig);
      }
      JSONObject ret = new JSONObject();
      ret.put("code", 0);
      ret.put("msg", "success");
      return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
      return new ResponseEntity<>(ret.toString(),HttpStatus.OK);
   }
   private Map<String, String> urlParamToMap(String params) {
      HashMap<String, String> map = new HashMap<>();
      if (StringUtils.isEmpty(params)) {
      if (ObjectUtils.isEmpty(params)) {
         return map;
      }
      String[] paramsArray = params.split("&");