panlinlin
2021-03-30 56859d09df8d4226882d43934acf32d60a3b51d7
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java
@@ -1,5 +1,6 @@
package com.genersoft.iot.vmp.media.zlm;
import java.util.List;
import java.util.UUID;
import com.alibaba.fastjson.JSON;
@@ -51,8 +52,8 @@
   @Autowired
   private IRedisCatchStorage redisCatchStorage;
   // @Autowired
   // private ZLMRESTfulUtils zlmresTfulUtils;
    @Autowired
    private ZLMMediaListManager zlmMediaListManager;
   @Autowired
   private ZLMHttpHookSubscribe subscribe;
@@ -133,8 +134,6 @@
      if (logger.isDebugEnabled()) {
         logger.debug("ZLM HOOK on_publish API调用,参数:" + json.toString());
      }
      // String app = json.getString("app");
      // String streamId = json.getString("id");
      ZLMHttpHookSubscribe.Event subscribe = this.subscribe.getSubscribe(ZLMHttpHookSubscribe.HookType.on_publish, json);
      if (subscribe != null) subscribe.response(json);
@@ -212,6 +211,13 @@
      if (logger.isDebugEnabled()) {
         logger.debug("ZLM HOOK on_shell_login API调用,参数:" + json.toString());
      }
      // TODO 如果是带有rtpstream则开启按需拉流
      // String app = json.getString("app");
      // String stream = json.getString("stream");
      ZLMHttpHookSubscribe.Event subscribe = this.subscribe.getSubscribe(ZLMHttpHookSubscribe.HookType.on_publish, json);
      if (subscribe != null) subscribe.response(json);
      JSONObject ret = new JSONObject();
      ret.put("code", 0);
      ret.put("msg", "success");
@@ -232,6 +238,7 @@
      // 流消失移除redis play
      String app = json.getString("app");
      String streamId = json.getString("stream");
      String schema = json.getString("schema");
      boolean regist = json.getBoolean("regist");
      StreamInfo streamInfo = redisCatchStorage.queryPlayByStreamId(streamId);
      if ("rtp".equals(app) && !regist ) {
@@ -241,6 +248,10 @@
         }else{
            streamInfo = redisCatchStorage.queryPlaybackByStreamId(streamId);
            redisCatchStorage.stopPlayback(streamInfo);
         }
      }else {
         if (!"rtp".equals(app) && "rtsp".equals(schema)){
            zlmMediaListManager.updateMediaList();
         }
      }
      JSONObject ret = new JSONObject();
@@ -262,21 +273,35 @@
      }
      
      String streamId = json.getString("stream");
      cmder.streamByeCmd(streamId);
      String app = json.getString("app");
      StreamInfo streamInfo = redisCatchStorage.queryPlayByStreamId(streamId);
      if (streamInfo!=null){
         redisCatchStorage.stopPlay(streamInfo);
         storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
      }else{
         streamInfo = redisCatchStorage.queryPlaybackByStreamId(streamId);
         redisCatchStorage.stopPlayback(streamInfo);
      if ("rtp".equals(app)){
         JSONObject ret = new JSONObject();
         ret.put("code", 0);
         ret.put("close", true);
         if (streamInfo != null) {
            if (redisCatchStorage.isChannelSendingRTP(streamInfo.getChannelId())) {
               ret.put("close", false);
            } else {
               cmder.streamByeCmd(streamId);
               redisCatchStorage.stopPlay(streamInfo);
               storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
            }
         }else{
            cmder.streamByeCmd(streamId);
            streamInfo = redisCatchStorage.queryPlaybackByStreamId(streamId);
            redisCatchStorage.stopPlayback(streamInfo);
         }
         return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
      }else {
         JSONObject ret = new JSONObject();
         ret.put("code", 0);
         ret.put("close", false);
         return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
      }
      JSONObject ret = new JSONObject();
      ret.put("code", 0);
      ret.put("close", true);
      return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
   }
   
   /**
@@ -335,10 +360,21 @@
//      String data = json.getString("data");
//      List<MediaServerConfig> mediaServerConfigs = JSON.parseArray(JSON.toJSONString(json), MediaServerConfig.class);
//      MediaServerConfig mediaServerConfig = mediaServerConfigs.get(0);
      List<ZLMHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(ZLMHttpHookSubscribe.HookType.on_server_started);
      if (subscribes != null && subscribes.size() > 0) {
         for (ZLMHttpHookSubscribe.Event subscribe : subscribes) {
            subscribe.response(json);
         }
      }
      MediaServerConfig mediaServerConfig = JSON.toJavaObject(json, MediaServerConfig.class);
      mediaServerConfig.setWanIp(StringUtils.isEmpty(mediaWanIp)? mediaIp: mediaWanIp);
      mediaServerConfig.setLocalIP(mediaIp);
      redisCatchStorage.updateMediaInfo(mediaServerConfig);
      // 重新发起代理
      JSONObject ret = new JSONObject();
      ret.put("code", 0);
      ret.put("msg", "success");