2020-07-16 16:09:48 +08:00
|
|
|
package com.genersoft.iot.vmp.gb28181;
|
|
|
|
|
2021-11-05 18:27:41 +08:00
|
|
|
import com.genersoft.iot.vmp.conf.SipConfig;
|
2020-12-26 16:44:27 +08:00
|
|
|
import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
|
2021-11-17 00:07:34 +08:00
|
|
|
import com.genersoft.iot.vmp.gb28181.transmit.ISIPProcessorObserver;
|
2021-11-05 18:27:41 +08:00
|
|
|
import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorObserver;
|
2021-07-26 11:40:32 +08:00
|
|
|
import gov.nist.javax.sip.SipProviderImpl;
|
2021-11-05 18:27:41 +08:00
|
|
|
import gov.nist.javax.sip.SipStackImpl;
|
2020-07-16 16:09:48 +08:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2020-07-16 17:31:41 +08:00
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.DependsOn;
|
2020-07-16 16:09:48 +08:00
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
2021-11-05 18:27:41 +08:00
|
|
|
import javax.sip.*;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.TooManyListenersException;
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2020-07-16 16:09:48 +08:00
|
|
|
|
|
|
|
@Component
|
2021-11-05 18:27:41 +08:00
|
|
|
public class SipLayer{
|
2020-07-16 16:09:48 +08:00
|
|
|
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(SipLayer.class);
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private SipConfig sipConfig;
|
|
|
|
|
|
|
|
@Autowired
|
2021-11-17 00:07:34 +08:00
|
|
|
private ISIPProcessorObserver sipProcessorObserver;
|
2020-12-26 16:44:27 +08:00
|
|
|
|
2021-07-26 11:40:32 +08:00
|
|
|
private SipStackImpl sipStack;
|
2020-07-16 16:09:48 +08:00
|
|
|
|
2020-07-16 17:31:41 +08:00
|
|
|
private SipFactory sipFactory;
|
2020-07-16 16:09:48 +08:00
|
|
|
|
2021-11-05 18:27:41 +08:00
|
|
|
|
2020-07-16 17:31:41 +08:00
|
|
|
@Bean("sipFactory")
|
|
|
|
private SipFactory createSipFactory() {
|
|
|
|
sipFactory = SipFactory.getInstance();
|
2020-07-16 16:09:48 +08:00
|
|
|
sipFactory.setPathName("gov.nist");
|
2020-07-16 17:31:41 +08:00
|
|
|
return sipFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean("sipStack")
|
2021-11-05 18:27:41 +08:00
|
|
|
@DependsOn({"sipFactory"})
|
2020-07-16 17:31:41 +08:00
|
|
|
private SipStack createSipStack() throws PeerUnavailableException {
|
|
|
|
Properties properties = new Properties();
|
|
|
|
properties.setProperty("javax.sip.STACK_NAME", "GB28181_SIP");
|
2021-06-07 15:42:01 +08:00
|
|
|
properties.setProperty("javax.sip.IP_ADDRESS", sipConfig.getMonitorIp());
|
2020-07-16 17:31:41 +08:00
|
|
|
properties.setProperty("gov.nist.javax.sip.LOG_MESSAGE_CONTENT", "false");
|
|
|
|
/**
|
|
|
|
* sip_server_log.log 和 sip_debug_log.log public static final int TRACE_NONE =
|
|
|
|
* 0; public static final int TRACE_MESSAGES = 16; public static final int
|
|
|
|
* TRACE_EXCEPTION = 17; public static final int TRACE_DEBUG = 32;
|
|
|
|
*/
|
|
|
|
properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "0");
|
|
|
|
properties.setProperty("gov.nist.javax.sip.SERVER_LOG", "sip_server_log");
|
|
|
|
properties.setProperty("gov.nist.javax.sip.DEBUG_LOG", "sip_debug_log");
|
|
|
|
sipStack = (SipStackImpl) sipFactory.createSipStack(properties);
|
|
|
|
return sipStack;
|
|
|
|
}
|
|
|
|
|
2021-11-09 11:56:21 +08:00
|
|
|
@Bean(name = "tcpSipProvider")
|
2020-07-16 17:31:41 +08:00
|
|
|
@DependsOn("sipStack")
|
2021-07-26 11:40:32 +08:00
|
|
|
private SipProviderImpl startTcpListener() {
|
2020-10-16 09:50:19 +08:00
|
|
|
ListeningPoint tcpListeningPoint = null;
|
2021-07-26 11:40:32 +08:00
|
|
|
SipProviderImpl tcpSipProvider = null;
|
2020-10-16 09:50:19 +08:00
|
|
|
try {
|
2021-08-10 15:42:15 +08:00
|
|
|
tcpListeningPoint = sipStack.createListeningPoint(sipConfig.getMonitorIp(), sipConfig.getPort(), "TCP");
|
2021-07-26 11:40:32 +08:00
|
|
|
tcpSipProvider = (SipProviderImpl)sipStack.createSipProvider(tcpListeningPoint);
|
2021-11-05 18:27:41 +08:00
|
|
|
tcpSipProvider.addSipListener(sipProcessorObserver);
|
2021-08-10 15:42:15 +08:00
|
|
|
logger.info("Sip Server TCP 启动成功 port {" + sipConfig.getMonitorIp() + ":" + sipConfig.getPort() + "}");
|
2021-07-16 16:34:51 +08:00
|
|
|
} catch (TransportNotSupportedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (InvalidArgumentException e) {
|
|
|
|
logger.error("无法使用 [ {}:{} ]作为SIP[ TCP ]服务,可排查: 1. sip.monitor-ip 是否为本机网卡IP; 2. sip.port 是否已被占用"
|
2021-08-10 15:42:15 +08:00
|
|
|
, sipConfig.getMonitorIp(), sipConfig.getPort());
|
2021-07-16 16:34:51 +08:00
|
|
|
} catch (TooManyListenersException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (ObjectInUseException e) {
|
|
|
|
e.printStackTrace();
|
2020-10-16 09:50:19 +08:00
|
|
|
}
|
2020-07-16 17:31:41 +08:00
|
|
|
return tcpSipProvider;
|
2020-07-16 16:09:48 +08:00
|
|
|
}
|
2020-07-16 17:31:41 +08:00
|
|
|
|
2021-11-09 11:56:21 +08:00
|
|
|
@Bean(name = "udpSipProvider")
|
2020-07-16 17:31:41 +08:00
|
|
|
@DependsOn("sipStack")
|
2021-07-26 11:40:32 +08:00
|
|
|
private SipProviderImpl startUdpListener() {
|
2021-07-16 16:34:51 +08:00
|
|
|
ListeningPoint udpListeningPoint = null;
|
2021-07-26 11:40:32 +08:00
|
|
|
SipProviderImpl udpSipProvider = null;
|
2021-07-16 16:34:51 +08:00
|
|
|
try {
|
2021-08-10 15:42:15 +08:00
|
|
|
udpListeningPoint = sipStack.createListeningPoint(sipConfig.getMonitorIp(), sipConfig.getPort(), "UDP");
|
2021-07-26 11:40:32 +08:00
|
|
|
udpSipProvider = (SipProviderImpl)sipStack.createSipProvider(udpListeningPoint);
|
2021-11-05 18:27:41 +08:00
|
|
|
udpSipProvider.addSipListener(sipProcessorObserver);
|
2021-07-16 16:34:51 +08:00
|
|
|
} catch (TransportNotSupportedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (InvalidArgumentException e) {
|
|
|
|
logger.error("无法使用 [ {}:{} ]作为SIP[ UDP ]服务,可排查: 1. sip.monitor-ip 是否为本机网卡IP; 2. sip.port 是否已被占用"
|
2021-08-10 15:42:15 +08:00
|
|
|
, sipConfig.getMonitorIp(), sipConfig.getPort());
|
2021-07-16 16:34:51 +08:00
|
|
|
} catch (TooManyListenersException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (ObjectInUseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2021-08-10 15:42:15 +08:00
|
|
|
logger.info("Sip Server UDP 启动成功 port [" + sipConfig.getMonitorIp() + ":" + sipConfig.getPort() + "]");
|
2020-07-16 17:31:41 +08:00
|
|
|
return udpSipProvider;
|
2020-07-16 16:09:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|