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;
|
2022-09-21 18:18:37 +08:00
|
|
|
import com.genersoft.iot.vmp.gb28181.conf.DefaultProperties;
|
2021-11-17 00:07:34 +08:00
|
|
|
import com.genersoft.iot.vmp.gb28181.transmit.ISIPProcessorObserver;
|
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;
|
2022-05-05 14:36:27 +08:00
|
|
|
import org.springframework.context.annotation.Configuration;
|
2020-07-16 17:31:41 +08:00
|
|
|
import org.springframework.context.annotation.DependsOn;
|
2020-07-16 16:09:48 +08:00
|
|
|
|
2021-11-05 18:27:41 +08:00
|
|
|
import javax.sip.*;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.TooManyListenersException;
|
2020-07-16 16:09:48 +08:00
|
|
|
|
2022-05-05 14:36:27 +08:00
|
|
|
@Configuration
|
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")
|
2022-05-05 14:36:27 +08:00
|
|
|
SipFactory createSipFactory() {
|
2020-07-16 17:31:41 +08:00
|
|
|
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"})
|
2022-09-23 22:45:23 +08:00
|
|
|
SipStackImpl createSipStack() throws PeerUnavailableException {
|
2022-09-21 18:18:37 +08:00
|
|
|
sipStack = ( SipStackImpl )sipFactory.createSipStack(DefaultProperties.getProperties(sipConfig.getMonitorIp(), false));
|
2020-07-16 17:31:41 +08:00
|
|
|
return sipStack;
|
|
|
|
}
|
|
|
|
|
2021-11-09 11:56:21 +08:00
|
|
|
@Bean(name = "tcpSipProvider")
|
2020-07-16 17:31:41 +08:00
|
|
|
@DependsOn("sipStack")
|
2022-05-05 14:36:27 +08:00
|
|
|
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-12-02 17:37:29 +08:00
|
|
|
tcpSipProvider.setDialogErrorsAutomaticallyHandled();
|
2021-11-05 18:27:41 +08:00
|
|
|
tcpSipProvider.addSipListener(sipProcessorObserver);
|
2022-05-13 15:32:30 +08:00
|
|
|
logger.info("[Sip Server] TCP 启动成功 {}:{}", sipConfig.getMonitorIp(), sipConfig.getPort());
|
2021-07-16 16:34:51 +08:00
|
|
|
} catch (TransportNotSupportedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (InvalidArgumentException e) {
|
2022-05-13 15:32:30 +08:00
|
|
|
logger.error("[Sip Server] 无法使用 [ {}:{} ]作为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")
|
2022-05-05 14:36:27 +08:00
|
|
|
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) {
|
2022-05-13 15:32:30 +08:00
|
|
|
logger.error("[Sip Server] 无法使用 [ {}:{} ]作为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();
|
|
|
|
}
|
2022-05-13 15:32:30 +08:00
|
|
|
logger.info("[Sip Server] UDP 启动成功 {}:{}", sipConfig.getMonitorIp(), sipConfig.getPort());
|
2020-07-16 17:31:41 +08:00
|
|
|
return udpSipProvider;
|
2020-07-16 16:09:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|