648540858
2022-11-25 dfb87c768538e280cf5e17d1d5ee5bbed1e1888d
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/AckRequestProcessor.java
@@ -4,6 +4,7 @@
import com.alibaba.fastjson2.JSONObject;
import com.genersoft.iot.vmp.conf.DynamicTask;
import com.genersoft.iot.vmp.conf.exception.SsrcTransactionNotFoundException;
import com.genersoft.iot.vmp.gb28181.bean.AudioBroadcastCatch;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
@@ -15,6 +16,8 @@
import com.genersoft.iot.vmp.gb28181.transmit.event.request.SIPRequestProcessorParent;
import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe;
import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeFactory;
import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeForRtpServerTimeout;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
import com.genersoft.iot.vmp.service.IDeviceService;
import com.genersoft.iot.vmp.service.IMediaServerService;
@@ -130,73 +133,47 @@
      param.put("use_ps", sendRtpItem.isUsePs() ? "1" : "0");
      param.put("only_audio", sendRtpItem.isOnlyAudio() ? "1" : "0");
      if (!sendRtpItem.isTcp()) {
         // 开启rtcp保活
         // udp模式下开启rtcp保活
         param.put("udp_rtcp_timeout", sendRtpItem.isRtcp()? "1":"0");
      }
      JSONObject jsonObject;
      if (sendRtpItem.isTcpActive()) {
         jsonObject = zlmrtpServerFactory.startSendRtpPassive(mediaInfo, param);
      if (mediaInfo == null) {
         RequestPushStreamMsg requestPushStreamMsg = RequestPushStreamMsg.getInstance(
               sendRtpItem.getMediaServerId(), sendRtpItem.getApp(), sendRtpItem.getStreamId(),
               sendRtpItem.getIp(), sendRtpItem.getPort(), sendRtpItem.getSsrc(), sendRtpItem.isTcp(),
               sendRtpItem.getLocalPort(), sendRtpItem.getPt(), sendRtpItem.isUsePs(), sendRtpItem.isOnlyAudio());
         redisGbPlayMsgListener.sendMsgForStartSendRtpStream(sendRtpItem.getServerId(), requestPushStreamMsg, json -> {
            startSendRtpStreamHand(evt, sendRtpItem, parentPlatform, json, param, callIdHeader);
         });
      } else {
         param.put("is_udp", is_Udp);
         param.put("dst_url", sendRtpItem.getIp());
         param.put("dst_port", sendRtpItem.getPort());
         jsonObject = zlmrtpServerFactory.startSendRtpStream(mediaInfo, param);
         System.out.println(JSON.toJSONString(param));
         System.out.println();
         System.out.println(jsonObject);
      }
         if (jsonObject == null) {
            logger.error("RTP推流失败: 请检查ZLM服务");
         } else if (jsonObject.getInteger("code") == 0) {
            logger.info("RTP推流成功[ {}/{} ],{}->{}:{}, ", param.get("app"), param.get("stream"), jsonObject.getString("local_port"), param.get("dst_url"), param.get("dst_port"));
         } else {
            logger.error("RTP推流失败: {}, 参数:{}", jsonObject.getString("msg"), JSON.toJSON(param));
            if (sendRtpItem.isOnlyAudio()) {
               // 语音对讲
               Device device = deviceService.getDevice(platformGbId);
               if (device != null) {
                  try {
                     cmder.streamByeCmd(device, sendRtpItem.getChannelId(), sendRtpItem.getStreamId(), null);
                  } catch (SipException | ParseException | InvalidArgumentException |
                         SsrcTransactionNotFoundException e) {
                     logger.error("[命令发送失败] 停止语音对讲: {}", e.getMessage());
                  }
               }
            } else {
               // 向上级平台
               try {
                  commanderForPlatform.streamByeCmd(parentPlatform, callIdHeader.getCallId());
               } catch (SipException | InvalidArgumentException | ParseException e) {
                  logger.error("[命令发送失败] 国标级联, 回复BYE: {}", e.getMessage());
               }
            }
            if (mediaInfo == null) {
               RequestPushStreamMsg requestPushStreamMsg = RequestPushStreamMsg.getInstance(
                     sendRtpItem.getMediaServerId(), sendRtpItem.getApp(), sendRtpItem.getStreamId(),
                     sendRtpItem.getIp(), sendRtpItem.getPort(), sendRtpItem.getSsrc(), sendRtpItem.isTcp(),
                     sendRtpItem.getLocalPort(), sendRtpItem.getPt(), sendRtpItem.isUsePs(), sendRtpItem.isOnlyAudio());
               redisGbPlayMsgListener.sendMsgForStartSendRtpStream(sendRtpItem.getServerId(), requestPushStreamMsg, json -> {
                  startSendRtpStreamHand(evt, sendRtpItem, parentPlatform, json, param, callIdHeader);
               });
            } else {
               // 如果是非严格模式,需要关闭端口占用
               JSONObject startSendRtpStreamResult = null;
               if (sendRtpItem.getLocalPort() != 0) {
                  if (zlmrtpServerFactory.releasePort(mediaInfo, sendRtpItem.getSsrc())) {
                     startSendRtpStreamResult = zlmrtpServerFactory.startSendRtpStream(mediaInfo, param);
                  }
         // 如果是非严格模式,需要关闭端口占用
         JSONObject startSendRtpStreamResult = null;
         if (sendRtpItem.getLocalPort() != 0) {
            HookSubscribeForRtpServerTimeout hookSubscribeForRtpServerTimeout = HookSubscribeFactory.on_rtp_server_timeout(sendRtpItem.getSsrc(), null, mediaInfo.getId());
            hookSubscribe.removeSubscribe(hookSubscribeForRtpServerTimeout);
            if (zlmrtpServerFactory.releasePort(mediaInfo, sendRtpItem.getSsrc())) {
               if (sendRtpItem.isTcpActive()) {
                  startSendRtpStreamResult = zlmrtpServerFactory.startSendRtpPassive(mediaInfo, param);
               }else {
                  param.put("is_udp", is_Udp);
                  param.put("dst_url", sendRtpItem.getIp());
                  param.put("dst_port", sendRtpItem.getPort());
                  startSendRtpStreamResult = zlmrtpServerFactory.startSendRtpStream(mediaInfo, param);
               }
               if (startSendRtpStreamResult != null) {
                  startSendRtpStreamHand(evt, sendRtpItem, parentPlatform, startSendRtpStreamResult, param, callIdHeader);
               }
            }
         }else {
            if (sendRtpItem.isTcpActive()) {
               startSendRtpStreamResult = zlmrtpServerFactory.startSendRtpPassive(mediaInfo, param);
            }else {
               param.put("is_udp", is_Udp);
               param.put("dst_url", sendRtpItem.getIp());
               param.put("dst_port", sendRtpItem.getPort());
               startSendRtpStreamResult = zlmrtpServerFactory.startSendRtpStream(mediaInfo, param);
            }
         }
         if (startSendRtpStreamResult != null) {
            startSendRtpStreamHand(evt, sendRtpItem, parentPlatform, startSendRtpStreamResult, param, callIdHeader);
         }
      }
   }
   private void startSendRtpStreamHand(RequestEvent evt, SendRtpItem sendRtpItem, ParentPlatform parentPlatform,
@@ -209,7 +186,16 @@
      } else {
         logger.error("RTP推流失败: {}, 参数:{}",jsonObject.getString("msg"), JSON.toJSONString(param));
         if (sendRtpItem.isOnlyAudio()) {
            // TODO 可能是语音对讲
            Device device = deviceService.getDevice(sendRtpItem.getDeviceId());
            AudioBroadcastCatch audioBroadcastCatch = audioBroadcastManager.get(sendRtpItem.getDeviceId(), sendRtpItem.getChannelId());
            if (audioBroadcastCatch != null) {
               try {
                  cmder.streamByeCmd(device, sendRtpItem.getChannelId(), audioBroadcastCatch.getSipTransactionInfo(), null);
               } catch (SipException | ParseException | InvalidArgumentException |
                      SsrcTransactionNotFoundException e) {
                  logger.error("[命令发送失败] 停止语音对讲: {}", e.getMessage());
               }
            }
         }else {
            // 向上级平台
            try {