panlinlin
2020-12-27 6968839f21fb05bf6e3204c2040ee47130006cd0
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
@@ -4,24 +4,22 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.sip.ClientTransaction;
import javax.sip.Dialog;
import javax.sip.InvalidArgumentException;
import javax.sip.SipException;
import javax.sip.SipFactory;
import javax.sip.SipProvider;
import javax.sip.TransactionDoesNotExistException;
import javax.sip.*;
import javax.sip.address.SipURI;
import javax.sip.header.CallIdHeader;
import javax.sip.header.Header;
import javax.sip.header.ViaHeader;
import javax.sip.message.Request;
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.bean.DeviceChannel;
import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookSubscribe;
import com.genersoft.iot.vmp.media.zlm.ZLMUtils;
import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
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.beans.factory.annotation.Value;
@@ -41,6 +39,8 @@
 */
@Component
public class SIPCommander implements ISIPCommander {
   private final Logger logger = LoggerFactory.getLogger(SIPCommander.class);
   
   @Autowired
   private SipConfig sipConfig;
@@ -63,13 +63,16 @@
   private SipProvider udpSipProvider;
   @Autowired
   private ZLMUtils zlmUtils;
   private ZLMRTPServerFactory zlmrtpServerFactory;
   @Value("${media.rtp.enable}")
   private boolean rtpEnable;
   @Autowired
   private ZLMHttpHookSubscribe subscribe;
   @Autowired
   private SipSubscribe sipSubscribe;
@@ -223,7 +226,7 @@
         
         Request request = headerProvider.createMessageRequest(device, ptzXml.toString(), "ViaPtzBranch", "FromPtzTag", "ToPtzTag");
         
         transmitRequest(device, request);
         transmitRequest(device, request, null);
         return true;
      } catch (SipException | ParseException | InvalidArgumentException e) {
         e.printStackTrace();
@@ -258,22 +261,23 @@
         ptzXml.append("</Control>\r\n");
         
         Request request = headerProvider.createMessageRequest(device, ptzXml.toString(), "ViaPtzBranch", "FromPtzTag", "ToPtzTag");
         transmitRequest(device, request);
         transmitRequest(device, request, null);
         return true;
      } catch (SipException | ParseException | InvalidArgumentException e) {
         e.printStackTrace();
      } 
      return false;
   }
   /**
    * 请求预览视频流
    *
    *    请求预览视频流
    * @param device  视频设备
    * @param channelId  预览通道
    * @param event hook订阅
    * @param errorEvent sip错误订阅
    */
   @Override
   public void playStreamCmd(Device device, String channelId, ZLMHttpHookSubscribe.Event event) {
   public void playStreamCmd(Device device, String channelId, ZLMHttpHookSubscribe.Event event, SipSubscribe.Event errorEvent) {
      try {
         String ssrc = streamSession.createPlaySsrc();
@@ -285,10 +289,14 @@
         }
         String streamMode = device.getStreamMode().toUpperCase();
         MediaServerConfig mediaInfo = storager.getMediaInfo();
         if (mediaInfo == null) {
            logger.warn("点播时发现ZLM尚未连接...");
            return;
         }
         String mediaPort = null;
         // 使用动态udp端口
         if (rtpEnable) {
            mediaPort = zlmUtils.getNewRTPPort(streamId) + "";
            mediaPort = zlmrtpServerFactory.createRTPServer(streamId) + "";
         }else {
            mediaPort = mediaInfo.getRtpProxyPort();
         }
@@ -302,7 +310,8 @@
         //
         StringBuffer content = new StringBuffer(200);
         content.append("v=0\r\n");
         content.append("o="+channelId+" 0 0 IN IP4 "+mediaInfo.getWanIp()+"\r\n");
//         content.append("o="+channelId+" 0 0 IN IP4 "+mediaInfo.getWanIp()+"\r\n");
         content.append("o="+"00000"+" 0 0 IN IP4 "+mediaInfo.getWanIp()+"\r\n");
         content.append("s=Play\r\n");
         content.append("c=IN IP4 "+mediaInfo.getWanIp()+"\r\n");
         content.append("t=0 0\r\n");
@@ -334,15 +343,13 @@
         Request request = headerProvider.createInviteRequest(device, channelId, content.toString(), null, "live", null, ssrc);
         ClientTransaction transaction = transmitRequest(device, request);
         ClientTransaction transaction = transmitRequest(device, request, errorEvent);
         streamSession.put(streamId, transaction);
         DeviceChannel deviceChannel = storager.queryChannel(device.getDeviceId(), channelId);
         if (deviceChannel != null) {
            deviceChannel.setStreamId(streamId);
            storager.updateChannel(device.getDeviceId(), deviceChannel);
         }
         // TODO 订阅SIP response,处理对方的错误返回
      } catch ( SipException | ParseException | InvalidArgumentException e) {
@@ -359,7 +366,8 @@
    * @param endTime 结束时间,格式要求:yyyy-MM-dd HH:mm:ss
    */ 
   @Override
   public void playbackStreamCmd(Device device, String channelId, String startTime, String endTime, ZLMHttpHookSubscribe.Event event) {
   public void playbackStreamCmd(Device device, String channelId, String startTime, String endTime, ZLMHttpHookSubscribe.Event event
         , SipSubscribe.Event errorEvent) {
      try {
         MediaServerConfig mediaInfo = storager.getMediaInfo();
         String ssrc = streamSession.createPlayBackSsrc();
@@ -383,7 +391,7 @@
         String mediaPort = null;
         // 使用动态udp端口
         if (rtpEnable) {
            mediaPort = zlmUtils.getNewRTPPort(streamId) + "";
            mediaPort = zlmrtpServerFactory.createRTPServer(streamId) + "";
         }else {
            mediaPort = mediaInfo.getRtpProxyPort();
         }
@@ -415,8 +423,8 @@
           content.append("y="+ssrc+"\r\n");//ssrc
           
           Request request = headerProvider.createPlaybackInviteRequest(device, channelId, content.toString(), null, "playback", null);
           ClientTransaction transaction = transmitRequest(device, request);
           ClientTransaction transaction = transmitRequest(device, request, errorEvent);
           streamSession.put(streamId, transaction);
      } catch ( SipException | ParseException | InvalidArgumentException e) {
@@ -459,6 +467,7 @@
         }
         dialog.sendRequest(clientTransaction);
         streamSession.remove(streamId);
         zlmrtpServerFactory.closeRTPServer(streamId);
      } catch (TransactionDoesNotExistException e) {
         e.printStackTrace();
      } catch (SipException e) {
@@ -576,7 +585,8 @@
         catalogXml.append("</Query>\r\n");
         
         Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaDeviceInfoBranch", "FromDeviceInfoTag", "ToDeviceInfoTag");
         transmitRequest(device, request);
         transmitRequest(device, request, null);
         
      } catch (SipException | ParseException | InvalidArgumentException e) {
         e.printStackTrace();
@@ -591,7 +601,7 @@
    * @param device 视频设备
    */ 
   @Override
   public boolean catalogQuery(Device device) {
   public boolean catalogQuery(Device device, SipSubscribe.Event errorEvent) {
      // 清空通道
      storager.cleanChannelsForDevice(device.getDeviceId());
      try {
@@ -603,8 +613,9 @@
         catalogXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
         catalogXml.append("</Query>\r\n");
         
         Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaCatalogBranch", "FromCatalogTag", "ToCatalogTag");
         transmitRequest(device, request);
         Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaCatalogBranch", "FromCatalogTag", null);
         transmitRequest(device, request, errorEvent);
      } catch (SipException | ParseException | InvalidArgumentException e) {
         e.printStackTrace();
         return false;
@@ -636,8 +647,10 @@
         recordInfoXml.append("<Type>all</Type>\r\n");
         recordInfoXml.append("</Query>\r\n");
         
         Request request = headerProvider.createMessageRequest(device, recordInfoXml.toString(), "ViaRecordInfoBranch", "FromRecordInfoTag", "ToRecordInfoTag");
         transmitRequest(device, request);
         Request request = headerProvider.createMessageRequest(device, recordInfoXml.toString(), "ViaRecordInfoBranch", "FromRecordInfoTag", null);
         transmitRequest(device, request, null);
      } catch (SipException | ParseException | InvalidArgumentException e) {
         e.printStackTrace();
         return false;
@@ -689,16 +702,30 @@
      return false;
   }
   
   private ClientTransaction transmitRequest(Device device, Request request) throws SipException {
   private ClientTransaction transmitRequest(Device device, Request request, SipSubscribe.Event errorEvent) throws SipException {
      ClientTransaction clientTransaction = null;
      if("TCP".equals(device.getTransport())) {
         clientTransaction = tcpSipProvider.getNewClientTransaction(request);
      } else if("UDP".equals(device.getTransport())) {
         clientTransaction = udpSipProvider.getNewClientTransaction(request);
      }
      // 添加订阅
      if (errorEvent != null) {
         CallIdHeader callIdHeader = (CallIdHeader)request.getHeader(CallIdHeader.NAME);
         sipSubscribe.addSubscribe(callIdHeader.getCallId(), errorEvent);
      }
      clientTransaction.sendRequest();
      return clientTransaction;
   }
   @Override
   public void closeRTPServer(Device device, String channelId) {
      if (rtpEnable) {
         String streamId = String.format("gb_play_%s_%s", device.getDeviceId(), channelId);
         zlmrtpServerFactory.closeRTPServer(streamId);
      }
   }
}