|  |  | 
 |  |  | 
 | 
 |  |  | import javax.sip.*;
 | 
 |  |  | import javax.sip.header.CallIdHeader;
 | 
 |  |  | import javax.sip.header.Header;
 | 
 |  |  | 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;
 | 
 |  |  | 
 |  |  |    @Autowired
 | 
 |  |  |    private SipSubscribe sipSubscribe;
 | 
 |  |  | 
 | 
 |  |  |    private SipStack sipStack;
 | 
 |  |  |    private SipStackImpl sipStack;
 | 
 |  |  | 
 | 
 |  |  |    private SipFactory sipFactory;
 | 
 |  |  | 
 | 
 |  |  | 
 |  |  |    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());
 | 
 |  |  | 
 |  |  |    private SipStack createSipStack() throws PeerUnavailableException {
 | 
 |  |  |       Properties properties = new Properties();
 | 
 |  |  |       properties.setProperty("javax.sip.STACK_NAME", "GB28181_SIP");
 | 
 |  |  |       properties.setProperty("javax.sip.IP_ADDRESS", sipConfig.getSipIp());
 | 
 |  |  |       properties.setProperty("javax.sip.IP_ADDRESS", sipConfig.getMonitorIp());
 | 
 |  |  |       properties.setProperty("gov.nist.javax.sip.LOG_MESSAGE_CONTENT", "false");
 | 
 |  |  |       /**
 | 
 |  |  |        * sip_server_log.log 和 sip_debug_log.log public static final int TRACE_NONE =
 | 
 |  |  | 
 |  |  | 
 | 
 |  |  |    @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.getPort(), "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.getPort() + "}");
 | 
 |  |  |       } catch (TransportNotSupportedException e) {
 | 
 |  |  |          e.printStackTrace();
 | 
 |  |  |       } catch (InvalidArgumentException e) {
 | 
 |  |  |          logger.error("无法使用 [ {}:{} ]作为SIP[ TCP ]服务,可排查: 1. sip.monitor-ip 是否为本机网卡IP; 2. sip.port 是否已被占用"
 | 
 |  |  |                , sipConfig.getMonitorIp(), sipConfig.getPort());
 | 
 |  |  |       } 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.getPort(), "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.getPort());
 | 
 |  |  |       } catch (TooManyListenersException e) {
 | 
 |  |  |          e.printStackTrace();
 | 
 |  |  |       } catch (ObjectInUseException e) {
 | 
 |  |  |          e.printStackTrace();
 | 
 |  |  |       }
 | 
 |  |  |       logger.info("Sip Server UDP 启动成功 port [" + sipConfig.getMonitorIp() + ":" + sipConfig.getPort() + "]");
 | 
 |  |  |       return udpSipProvider;
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  | 
 |  |  |       logger.debug(evt.getRequest().toString());
 | 
 |  |  |       // 由于jainsip是单线程程序,为提高性能并发处理
 | 
 |  |  |       processThreadPool.execute(() -> {
 | 
 |  |  |          processorFactory.createRequestProcessor(evt).process();
 | 
 |  |  |          if (processorFactory != null) {
 | 
 |  |  |             processorFactory.createRequestProcessor(evt).process();
 | 
 |  |  |          }
 | 
 |  |  |       });
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  | 
 |  |  |       Response response = evt.getResponse();
 | 
 |  |  |       logger.debug(evt.getResponse().toString());
 | 
 |  |  |       int status = response.getStatusCode();
 | 
 |  |  |       if ((status >= 200) && (status < 300)) { // Success!
 | 
 |  |  |       if (((status >= 200) && (status < 300)) || status == 401) { // Success!
 | 
 |  |  |          ISIPResponseProcessor processor = processorFactory.createResponseProcessor(evt);
 | 
 |  |  |          try {
 | 
 |  |  |             processor.process(evt, this, sipConfig);
 | 
 |  |  | 
 |  |  |             // TODO Auto-generated catch block
 | 
 |  |  |             e.printStackTrace();
 | 
 |  |  |          }
 | 
 |  |  |       // } else if (status == Response.TRYING) {
 | 
 |  |  |          // trying不会回复
 | 
 |  |  | 
 | 
 |  |  |          if (evt.getResponse() != null && sipSubscribe.getOkSubscribesSize() > 0 ) {
 | 
 |  |  |             CallIdHeader callIdHeader = (CallIdHeader)evt.getResponse().getHeader(CallIdHeader.NAME);
 | 
 |  |  |             if (callIdHeader != null) {
 | 
 |  |  |                SipSubscribe.Event subscribe = sipSubscribe.getOkSubscribe(callIdHeader.getCallId());
 | 
 |  |  |                if (subscribe != null) {
 | 
 |  |  |                   SipSubscribe.EventResult eventResult = new SipSubscribe.EventResult(evt);
 | 
 |  |  |                   subscribe.response(eventResult);
 | 
 |  |  |                }
 | 
 |  |  |             }
 | 
 |  |  |          }
 | 
 |  |  |       } else if ((status >= 100) && (status < 200)) {
 | 
 |  |  |          // 增加其它无需回复的响应,如101、180等
 | 
 |  |  |       } else {
 | 
 |  |  |          logger.warn("接收到失败的response响应!status:" + status + ",message:" + response.getReasonPhrase()/* .getContent().toString()*/);
 | 
 |  |  |          if (evt.getResponse() != null && sipSubscribe.getSize() > 0 ) {
 | 
 |  |  |          if (evt.getResponse() != null && sipSubscribe.getErrorSubscribesSize() > 0 ) {
 | 
 |  |  |             CallIdHeader callIdHeader = (CallIdHeader)evt.getResponse().getHeader(CallIdHeader.NAME);
 | 
 |  |  |             if (callIdHeader != null) {
 | 
 |  |  |                SipSubscribe.Event subscribe = sipSubscribe.getSubscribe(callIdHeader.getCallId());
 | 
 |  |  |                SipSubscribe.Event subscribe = sipSubscribe.getErrorSubscribe(callIdHeader.getCallId());
 | 
 |  |  |                if (subscribe != null) {
 | 
 |  |  |                   subscribe.response(evt);
 | 
 |  |  |                   SipSubscribe.EventResult eventResult = new SipSubscribe.EventResult(evt);
 | 
 |  |  |                   subscribe.response(eventResult);
 | 
 |  |  |                }
 | 
 |  |  |             }
 | 
 |  |  |          }
 | 
 |  |  | 
 |  |  |    @Override
 | 
 |  |  |    public void processTimeout(TimeoutEvent timeoutEvent) {
 | 
 |  |  |       // TODO Auto-generated method stub
 | 
 |  |  | 
 | 
 |  |  |       CallIdHeader callIdHeader = timeoutEvent.getClientTransaction().getDialog().getCallId();
 | 
 |  |  |       String callId = callIdHeader.getCallId();
 | 
 |  |  |       SipSubscribe.Event errorSubscribe = sipSubscribe.getErrorSubscribe(callId);
 | 
 |  |  |       SipSubscribe.EventResult<TimeoutEvent> timeoutEventEventResult = new SipSubscribe.EventResult<>(timeoutEvent);
 | 
 |  |  |       errorSubscribe.response(timeoutEventEventResult);
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  |    /**
 | 
 |  |  | 
 |  |  |    @Override
 | 
 |  |  |    public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {
 | 
 |  |  |       // TODO Auto-generated method stub
 | 
 |  |  | 
 | 
 |  |  | //      CallIdHeader callIdHeader = transactionTerminatedEvent.getClientTransaction().getDialog().getCallId();
 | 
 |  |  | //      String callId = callIdHeader.getCallId();
 | 
 |  |  | //      SipSubscribe.Event errorSubscribe = sipSubscribe.getErrorSubscribe(callId);
 | 
 |  |  | //      SipSubscribe.EventResult<TransactionTerminatedEvent> eventResult = new SipSubscribe.EventResult<>(transactionTerminatedEvent);
 | 
 |  |  | //      errorSubscribe.response(eventResult);
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  |    /**
 | 
 |  |  | 
 |  |  |    @Override
 | 
 |  |  |    public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
 | 
 |  |  |       // TODO Auto-generated method stub
 | 
 |  |  | //      CallIdHeader callIdHeader = dialogTerminatedEvent.getDialog().getCallId();
 | 
 |  |  | //      String callId = callIdHeader.getCallId();
 | 
 |  |  | //      SipSubscribe.Event errorSubscribe = sipSubscribe.getErrorSubscribe(callId);
 | 
 |  |  | //      SipSubscribe.EventResult<DialogTerminatedEvent> eventResult = new SipSubscribe.EventResult<>(dialogTerminatedEvent);
 | 
 |  |  | //      errorSubscribe.response(eventResult);
 | 
 |  |  | 
 | 
 |  |  |    }
 | 
 |  |  | 
 |