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;
|
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-05-05 14:36:27 +08:00
|
|
|
|
SipStack createSipStack() throws PeerUnavailableException {
|
2020-07-16 17:31:41 +08:00
|
|
|
|
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());
|
2022-05-11 18:37:24 +08:00
|
|
|
|
/**
|
|
|
|
|
* 完整配置参考 gov.nist.javax.sip.SipStackImpl,需要下载源码
|
|
|
|
|
* gov/nist/javax/sip/SipStackImpl.class
|
2022-09-02 10:51:55 +08:00
|
|
|
|
* sip消息的解析在 gov.nist.javax.sip.stack.UDPMessageChannel的processIncomingDataPacket方法
|
2022-05-11 18:37:24 +08:00
|
|
|
|
*/
|
2022-09-02 10:51:55 +08:00
|
|
|
|
// * gov/nist/javax/sip/SipStackImpl.class
|
2022-08-26 18:03:09 +08:00
|
|
|
|
if (logger.isDebugEnabled()) {
|
2022-08-31 11:29:13 +08:00
|
|
|
|
properties.setProperty("gov.nist.javax.sip.LOG_MESSAGE_CONTENT", "false");
|
2022-08-26 18:03:09 +08:00
|
|
|
|
}
|
2022-08-11 16:37:55 +08:00
|
|
|
|
// 接收所有notify请求,即使没有订阅
|
|
|
|
|
properties.setProperty("gov.nist.javax.sip.DELIVER_UNSOLICITED_NOTIFY", "true");
|
2022-09-02 10:51:55 +08:00
|
|
|
|
properties.setProperty("gov.nist.javax.sip.AUTOMATIC_DIALOG_ERROR_HANDLING", "false");
|
|
|
|
|
properties.setProperty("gov.nist.javax.sip.CANCEL_CLIENT_TRANSACTION_CHECKED", "false");
|
2022-08-11 16:37:55 +08:00
|
|
|
|
// 为_NULL _对话框传递_终止的_事件
|
|
|
|
|
properties.setProperty("gov.nist.javax.sip.DELIVER_TERMINATED_EVENT_FOR_NULL_DIALOG", "true");
|
|
|
|
|
// 会话清理策略
|
|
|
|
|
properties.setProperty("gov.nist.javax.sip.RELEASE_REFERENCES_STRATEGY", "Normal");
|
|
|
|
|
// 处理由该服务器处理的基于底层TCP的保持生存超时
|
|
|
|
|
properties.setProperty("gov.nist.javax.sip.RELIABLE_CONNECTION_KEEP_ALIVE_TIMEOUT", "60");
|
|
|
|
|
|
2020-07-16 17:31:41 +08:00
|
|
|
|
/**
|
2022-08-31 11:29:13 +08:00
|
|
|
|
* sip_server_log.log 和 sip_debug_log.log ERROR, INFO, WARNING, OFF, DEBUG, TRACE
|
2020-07-16 17:31:41 +08:00
|
|
|
|
*/
|
2022-08-31 11:29:13 +08:00
|
|
|
|
properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "ERROR");
|
2022-09-02 10:51:55 +08:00
|
|
|
|
// properties.setProperty("gov.nist.javax.sip.SIP_MESSAGE_VALVE", "com.genersoft.iot.vmp.gb28181.session.SipMessagePreprocessing");
|
2022-08-31 11:29:13 +08:00
|
|
|
|
// if (logger.isDebugEnabled()) {
|
|
|
|
|
// properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "DEBUG");
|
|
|
|
|
// }
|
|
|
|
|
|
2020-07-16 17:31:41 +08:00
|
|
|
|
sipStack = (SipStackImpl) sipFactory.createSipStack(properties);
|
2022-03-10 20:53:56 +08:00
|
|
|
|
|
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-03-10 20:53:56 +08:00
|
|
|
|
// tcpSipProvider.setAutomaticDialogSupportEnabled(false);
|
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);
|
2022-03-10 20:53:56 +08:00
|
|
|
|
// udpSipProvider.setAutomaticDialogSupportEnabled(false);
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|