panlinlin
2021-04-27 bf8fb0c51f4e7894a62636dede6c69a2a85d8458
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
@@ -10,14 +10,18 @@
import javax.sip.header.ViaHeader;
import javax.sip.message.Request;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.common.StreamInfo;
import com.genersoft.iot.vmp.conf.MediaServerConfig;
import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookSubscribe;
import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
import gov.nist.javax.sip.message.SIPRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -75,6 +79,9 @@
   @Autowired
   private ZLMRTPServerFactory zlmrtpServerFactory;
   @Autowired
   private ZLMRESTfulUtils zlmresTfulUtils;
   @Value("${media.rtp.enable}")
   private boolean rtpEnable;
@@ -83,6 +90,9 @@
   @Value("${media.autoApplyPlay}")
   private boolean autoApplyPlay;
   @Value("${userSettings.waitTrack}")
   private boolean waitTrack;
   @Autowired
   private ZLMHttpHookSubscribe subscribe;
@@ -270,7 +280,7 @@
   public boolean frontEndCmd(Device device, String channelId, int cmdCode, int parameter1, int parameter2, int combineCode2) {
      try {
         String cmdStr= frontEndCmdString(cmdCode, parameter1, parameter2, combineCode2);
         System.out.println("控制字符串:" + cmdStr);
         logger.info("控制字符串:" + cmdStr);
         StringBuffer ptzXml = new StringBuffer(200);
         ptzXml.append("<?xml version=\"1.0\" ?>\r\n");
         ptzXml.append("<Control>\r\n");
@@ -369,7 +379,7 @@
         subscribeKey.put("regist", true);
         subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey, json->{
            if (json.getJSONArray("tracks") == null) return;
            if (waitTrack && json.getJSONArray("tracks") == null) return;
            event.response(json);
            subscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey);
         });
@@ -419,19 +429,12 @@
            content.append("a=rtpmap:96 PS/90000\r\n");
            content.append("a=rtpmap:98 H264/90000\r\n");
            content.append("a=rtpmap:97 MPEG4/90000\r\n");
            if("TCP-PASSIVE".equals(streamMode)) { // tcp被动模式
            if ("TCP-PASSIVE".equals(streamMode)) { // tcp被动模式
               content.append("a=setup:passive\r\n");
               content.append("a=recvonly\r\n");
               content.append("a=rtpmap:96 PS/90000\r\n");
               content.append("a=rtpmap:98 H264/90000\r\n");
               content.append("a=rtpmap:97 MPEG4/90000\r\n");
               if ("TCP-PASSIVE".equals(streamMode)) { // tcp被动模式
                  content.append("a=setup:passive\r\n");
                  content.append("a=connection:new\r\n");
               } else if ("TCP-ACTIVE".equals(streamMode)) { // tcp主动模式
                  content.append("a=setup:active\r\n");
                  content.append("a=connection:new\r\n");
               }
               content.append("a=connection:new\r\n");
            } else if ("TCP-ACTIVE".equals(streamMode)) { // tcp主动模式
               content.append("a=setup:active\r\n");
               content.append("a=connection:new\r\n");
            }
         }
@@ -577,13 +580,39 @@
      
      try {
         ClientTransaction transaction = streamSession.getTransaction(deviceId, channelId);
         // 服务重启后
         // 服务重启后, 无法直接发送bye, 通过手动构建发送
         if (transaction == null) {
            StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
            if (streamInfo != null) {
               JSONObject mediaList = zlmresTfulUtils.getMediaList(streamInfo.getApp(), streamInfo.getStreamId());
               if (mediaList != null) { // 仍在推流才发送
                  if (mediaList.getInteger("code") == 0) {
                     JSONArray data = mediaList.getJSONArray("data");
                     if (data != null && data.size() > 0) {
                        Device device = storager.queryVideoDevice(deviceId);
                        if (device != null) {
                           StreamInfo.TransactionInfo transactionInfo = streamInfo.getTransactionInfo();
                           try {
                              Request byteRequest = headerProvider.createByteRequest(device, channelId,
                                    transactionInfo.branch,
                                    transactionInfo.localTag,
                                    transactionInfo.remoteTag,
                                    transactionInfo.callId);
                              transmitRequest(device, byteRequest);
                           } catch (InvalidArgumentException e) {
                              e.printStackTrace();
                           }
                        }
                     }
                  }
               }
               redisCatchStorage.stopPlay(streamInfo);
            }
            okEvent.response(null);
            if (okEvent != null) {
               okEvent.response(null);
            }
            return;
         }
         
@@ -593,15 +622,9 @@
         }
         Request byeRequest = dialog.createRequest(Request.BYE);
         SipURI byeURI = (SipURI) byeRequest.getRequestURI();
         String vh = transaction.getRequest().getHeader(ViaHeader.NAME).toString();
         Pattern p = Pattern.compile("(\\d+\\.\\d+\\.\\d+\\.\\d+)\\:(\\d+)");
         Matcher matcher = p.matcher(vh);
         if (matcher.find()) {
            String ip = matcher.group(1);
            byeURI.setHost(ip);
            String port = matcher.group(2);
            byeURI.setPort(Integer.parseInt(port));
         }
         SIPRequest request = (SIPRequest)transaction.getRequest();
         byeURI.setHost(request.getRemoteAddress().getHostName());
         byeURI.setPort(request.getRemotePort());
         ViaHeader viaHeader = (ViaHeader) byeRequest.getHeader(ViaHeader.NAME);
         String protocol = viaHeader.getTransport().toUpperCase();
         ClientTransaction clientTransaction = null;