648540858
2021-08-09 dbc525e8fb59f70c474863304e3c580e81dcedf0
src/main/java/com/genersoft/iot/vmp/gb28181/SipLayer.java
@@ -12,6 +12,7 @@
import javax.sip.message.Response;
import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
import gov.nist.javax.sip.SipProviderImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -39,7 +40,7 @@
   @Autowired
   private SipSubscribe sipSubscribe;
   private SipStack sipStack;
   private SipStackImpl sipStack;
   private SipFactory sipFactory;
@@ -52,7 +53,7 @@
   private ThreadPoolExecutor initSipServer() {
      
      int processThreadNum = Runtime.getRuntime().availableProcessors() * 10;
      LinkedBlockingQueue<Runnable> processQueue = new LinkedBlockingQueue<Runnable>(10000);
      LinkedBlockingQueue<Runnable> processQueue = new LinkedBlockingQueue<>(10000);
      processThreadPool = new ThreadPoolExecutor(processThreadNum,processThreadNum,
            0L,TimeUnit.MILLISECONDS,processQueue,
            new ThreadPoolExecutor.CallerRunsPolicy());
@@ -88,27 +89,48 @@
   @Bean("tcpSipProvider")
   @DependsOn("sipStack")
   private SipProvider startTcpListener() {
   private SipProviderImpl startTcpListener() {
      ListeningPoint tcpListeningPoint = null;
      SipProvider tcpSipProvider  = null;
      SipProviderImpl tcpSipProvider  = null;
      try {
         tcpListeningPoint = sipStack.createListeningPoint(sipConfig.getSipIp(), sipConfig.getSipPort(), "TCP");
         tcpSipProvider = sipStack.createSipProvider(tcpListeningPoint);
         tcpListeningPoint = sipStack.createListeningPoint(sipConfig.getMonitorIp(), sipConfig.getSipPort(), "TCP");
         tcpSipProvider = (SipProviderImpl)sipStack.createSipProvider(tcpListeningPoint);
         tcpSipProvider.addSipListener(this);
         logger.info("Sip Server TCP 启动成功 port {" + sipConfig.getSipPort() + "}");
      } catch (TransportNotSupportedException | InvalidArgumentException | TooManyListenersException | ObjectInUseException e) {
         logger.error(String.format("创建SIP服务失败: %s", e.getMessage()));
         logger.info("Sip Server TCP 启动成功 port {" + sipConfig.getMonitorIp() + ":" + sipConfig.getSipPort() + "}");
      } catch (TransportNotSupportedException e) {
         e.printStackTrace();
      } catch (InvalidArgumentException e) {
         logger.error("无法使用 [ {}:{} ]作为SIP[ TCP ]服务,可排查: 1. sip.monitor-ip 是否为本机网卡IP; 2. sip.port 是否已被占用"
               , sipConfig.getMonitorIp(), sipConfig.getSipPort());
      } catch (TooManyListenersException e) {
         e.printStackTrace();
      } catch (ObjectInUseException e) {
         e.printStackTrace();
      }
      return tcpSipProvider;
   }
   
   @Bean("udpSipProvider")
   @DependsOn("sipStack")
   private SipProvider startUdpListener() throws Exception {
      ListeningPoint udpListeningPoint = sipStack.createListeningPoint(sipConfig.getSipIp(), sipConfig.getSipPort(), "UDP");
      SipProvider udpSipProvider = sipStack.createSipProvider(udpListeningPoint);
      udpSipProvider.addSipListener(this);
      logger.info("Sip Server UDP 启动成功 port {" + sipConfig.getSipPort() + "}");
   private SipProviderImpl startUdpListener() {
      ListeningPoint udpListeningPoint = null;
      SipProviderImpl udpSipProvider = null;
      try {
         udpListeningPoint = sipStack.createListeningPoint(sipConfig.getMonitorIp(), sipConfig.getSipPort(), "UDP");
         udpSipProvider = (SipProviderImpl)sipStack.createSipProvider(udpListeningPoint);
         udpSipProvider.addSipListener(this);
//         udpSipProvider.setAutomaticDialogSupportEnabled(false);
      } catch (TransportNotSupportedException e) {
         e.printStackTrace();
      } catch (InvalidArgumentException e) {
         logger.error("无法使用 [ {}:{} ]作为SIP[ UDP ]服务,可排查: 1. sip.monitor-ip 是否为本机网卡IP; 2. sip.port 是否已被占用"
               , sipConfig.getMonitorIp(), sipConfig.getSipPort());
      } catch (TooManyListenersException e) {
         e.printStackTrace();
      } catch (ObjectInUseException e) {
         e.printStackTrace();
      }
      logger.info("Sip Server UDP 启动成功 port [" + sipConfig.getMonitorIp() + ":" + sipConfig.getSipPort() + "]");
      return udpSipProvider;
   }
@@ -118,7 +140,7 @@
    */
   @Override
   public void processRequest(RequestEvent evt) {
//      logger.debug(evt.getRequest().toString());
      logger.debug(evt.getRequest().toString());
      // 由于jainsip是单线程程序,为提高性能并发处理
      processThreadPool.execute(() -> {
         if (processorFactory != null) {
@@ -130,7 +152,7 @@
   @Override
   public void processResponse(ResponseEvent evt) {
      Response response = evt.getResponse();
//      logger.debug(evt.getResponse().toString());
      logger.debug(evt.getResponse().toString());
      int status = response.getStatusCode();
      if (((status >= 200) && (status < 300)) || status == 401) { // Success!
         ISIPResponseProcessor processor = processorFactory.createResponseProcessor(evt);
@@ -140,6 +162,7 @@
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
         if (evt.getResponse() != null && sipSubscribe.getOkSubscribesSize() > 0 ) {
            CallIdHeader callIdHeader = (CallIdHeader)evt.getResponse().getHeader(CallIdHeader.NAME);
            if (callIdHeader != null) {
@@ -197,7 +220,6 @@
   @Override
   public void processIOException(IOExceptionEvent exceptionEvent) {
      // TODO Auto-generated method stub
   }
   /**
@@ -213,7 +235,6 @@
   @Override
   public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {
      // TODO Auto-generated method stub
   }
   /**