forked from Thirdparty/wvp
1
0
Fork 0

Merge pull request #10 from lawrencehj/master

修正Invite后无法发送ack、bye无法找到对话对方IP地址的问题等
master
swwhaha 2020-06-26 12:05:23 +08:00 committed by GitHub
commit 92cbbefcf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 43 deletions

View File

@ -1,5 +1,6 @@
package com.genersoft.iot.vmp.gb28181; package com.genersoft.iot.vmp.gb28181;
import java.text.ParseException;
import java.util.Properties; import java.util.Properties;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -128,11 +129,18 @@ public class SipLayer implements SipListener, Runnable {
int status = response.getStatusCode(); int status = response.getStatusCode();
if ((status >= 200) && (status < 300)) { // Success! if ((status >= 200) && (status < 300)) { // Success!
ISIPResponseProcessor processor = processorFactory.createResponseProcessor(evt); ISIPResponseProcessor processor = processorFactory.createResponseProcessor(evt);
processor.process(evt, this, sipConfig); try {
} else if (status == Response.TRYING) { processor.process(evt, this, sipConfig);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// } else if (status == Response.TRYING) {
// trying不会回复 // trying不会回复
} else if ((status >= 100) && (status < 200)) {
// 增加其它无需回复的响应如101、180等
} else { } else {
logger.warn("接收到失败的response响应status" + status + ",message:" + response.getContent().toString()); logger.warn("接收到失败的response响应status" + status + ",message:" + response.getReasonPhrase()/* .getContent().toString()*/);
} }
// trying不会回复 // trying不会回复
// if (status == Response.TRYING) { // if (status == Response.TRYING) {

View File

@ -79,7 +79,8 @@ public class SIPRequestHeaderProvider {
SipURI requestLine = layer.getAddressFactory().createSipURI(channelId, host.getAddress()); SipURI requestLine = layer.getAddressFactory().createSipURI(channelId, host.getAddress());
//via //via
ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>(); ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
ViaHeader viaHeader = layer.getHeaderFactory().createViaHeader(sipConfig.getSipIp(), sipConfig.getSipPort(), device.getTransport(), viaTag); // ViaHeader viaHeader = layer.getHeaderFactory().createViaHeader(sipConfig.getSipIp(), sipConfig.getSipPort(), device.getTransport(), viaTag);
ViaHeader viaHeader = layer.getHeaderFactory().createViaHeader(device.getHost().getIp(), device.getHost().getPort(), device.getTransport(), viaTag);
viaHeader.setRPort(); viaHeader.setRPort();
viaHeaders.add(viaHeader); viaHeaders.add(viaHeader);
//from //from
@ -108,6 +109,7 @@ public class SIPRequestHeaderProvider {
request = layer.getMessageFactory().createRequest(requestLine, Request.INVITE, callIdHeader, cSeqHeader,fromHeader, toHeader, viaHeaders, maxForwards); request = layer.getMessageFactory().createRequest(requestLine, Request.INVITE, callIdHeader, cSeqHeader,fromHeader, toHeader, viaHeaders, maxForwards);
Address concatAddress = layer.getAddressFactory().createAddress(layer.getAddressFactory().createSipURI(sipConfig.getSipId(), sipConfig.getSipIp()+":"+sipConfig.getSipPort())); Address concatAddress = layer.getAddressFactory().createAddress(layer.getAddressFactory().createSipURI(sipConfig.getSipId(), sipConfig.getSipIp()+":"+sipConfig.getSipPort()));
// Address concatAddress = layer.getAddressFactory().createAddress(layer.getAddressFactory().createSipURI(sipConfig.getSipId(), device.getHost().getIp()+":"+device.getHost().getPort()));
request.addHeader(layer.getHeaderFactory().createContactHeader(concatAddress)); request.addHeader(layer.getHeaderFactory().createContactHeader(concatAddress));
ContentTypeHeader contentTypeHeader = layer.getHeaderFactory().createContentTypeHeader("Application", "SDP"); ContentTypeHeader contentTypeHeader = layer.getHeaderFactory().createContentTypeHeader("Application", "SDP");
@ -122,7 +124,8 @@ public class SIPRequestHeaderProvider {
SipURI requestLine = layer.getAddressFactory().createSipURI(device.getDeviceId(), host.getAddress()); SipURI requestLine = layer.getAddressFactory().createSipURI(device.getDeviceId(), host.getAddress());
//via //via
ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>(); ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
ViaHeader viaHeader = layer.getHeaderFactory().createViaHeader(sipConfig.getSipIp(), sipConfig.getSipPort(), device.getTransport(), viaTag); // ViaHeader viaHeader = layer.getHeaderFactory().createViaHeader(sipConfig.getSipIp(), sipConfig.getSipPort(), device.getTransport(), viaTag);
ViaHeader viaHeader = layer.getHeaderFactory().createViaHeader(device.getHost().getIp(), device.getHost().getPort(), device.getTransport(), viaTag);
viaHeader.setRPort(); viaHeader.setRPort();
viaHeaders.add(viaHeader); viaHeaders.add(viaHeader);
//from //from
@ -151,6 +154,7 @@ public class SIPRequestHeaderProvider {
request = layer.getMessageFactory().createRequest(requestLine, Request.INVITE, callIdHeader, cSeqHeader,fromHeader, toHeader, viaHeaders, maxForwards); request = layer.getMessageFactory().createRequest(requestLine, Request.INVITE, callIdHeader, cSeqHeader,fromHeader, toHeader, viaHeaders, maxForwards);
Address concatAddress = layer.getAddressFactory().createAddress(layer.getAddressFactory().createSipURI(sipConfig.getSipId(), sipConfig.getSipIp()+":"+sipConfig.getSipPort())); Address concatAddress = layer.getAddressFactory().createAddress(layer.getAddressFactory().createSipURI(sipConfig.getSipId(), sipConfig.getSipIp()+":"+sipConfig.getSipPort()));
// Address concatAddress = layer.getAddressFactory().createAddress(layer.getAddressFactory().createSipURI(sipConfig.getSipId(), device.getHost().getIp()+":"+device.getHost().getPort()));
request.addHeader(layer.getHeaderFactory().createContactHeader(concatAddress)); request.addHeader(layer.getHeaderFactory().createContactHeader(concatAddress));
ContentTypeHeader contentTypeHeader = layer.getHeaderFactory().createContentTypeHeader("Application", "SDP"); ContentTypeHeader contentTypeHeader = layer.getHeaderFactory().createContentTypeHeader("Application", "SDP");

View File

@ -7,10 +7,13 @@ import javax.sip.Dialog;
import javax.sip.InvalidArgumentException; import javax.sip.InvalidArgumentException;
import javax.sip.SipException; import javax.sip.SipException;
import javax.sip.TransactionDoesNotExistException; import javax.sip.TransactionDoesNotExistException;
import javax.sip.address.Address;
import javax.sip.address.SipURI;
import javax.sip.header.ViaHeader; import javax.sip.header.ViaHeader;
import javax.sip.message.Request; import javax.sip.message.Request;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties.Headers;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.genersoft.iot.vmp.conf.SipConfig; import com.genersoft.iot.vmp.conf.SipConfig;
@ -21,6 +24,9 @@ import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.SIPRequestHeaderProvider; import com.genersoft.iot.vmp.gb28181.transmit.cmd.SIPRequestHeaderProvider;
import com.genersoft.iot.vmp.gb28181.utils.DateUtil; import com.genersoft.iot.vmp.gb28181.utils.DateUtil;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/** /**
* @Description: * @Description:
* @author: songww * @author: songww
@ -288,6 +294,13 @@ public class SIPCommander implements ISIPCommander {
return; return;
} }
Request byeRequest = dialog.createRequest(Request.BYE); Request byeRequest = dialog.createRequest(Request.BYE);
SipURI byeURI = (SipURI) byeRequest.getRequestURI();
String vh = transaction.getRequest().getHeader(ViaHeader.NAME).toString();
Pattern p = Pattern.compile("(\\d+\\.\\d+\\.\\d+\\.\\d+)\\:(\\d+)");
Matcher matcher = p.matcher(vh);
if (matcher.find()) {
byeURI.setHost(matcher.group(1));
}
ViaHeader viaHeader = (ViaHeader) byeRequest.getHeader(ViaHeader.NAME); ViaHeader viaHeader = (ViaHeader) byeRequest.getHeader(ViaHeader.NAME);
String protocol = viaHeader.getTransport().toUpperCase(); String protocol = viaHeader.getTransport().toUpperCase();
ClientTransaction clientTransaction = null; ClientTransaction clientTransaction = null;
@ -301,6 +314,8 @@ public class SIPCommander implements ISIPCommander {
e.printStackTrace(); e.printStackTrace();
} catch (SipException e) { } catch (SipException e) {
e.printStackTrace(); e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} }
} }

View File

@ -100,6 +100,7 @@ public class MessageRequestProcessor implements ISIPRequestProcessor {
Request request = evt.getRequest(); Request request = evt.getRequest();
SAXReader reader = new SAXReader(); SAXReader reader = new SAXReader();
reader.setEncoding("gbk");
Document xml; Document xml;
try { try {
xml = reader.read(new ByteArrayInputStream(request.getRawContent())); xml = reader.read(new ByteArrayInputStream(request.getRawContent()));
@ -375,7 +376,7 @@ public class MessageRequestProcessor implements ISIPRequestProcessor {
private Element getRootElement(RequestEvent evt) throws DocumentException { private Element getRootElement(RequestEvent evt) throws DocumentException {
Request request = evt.getRequest(); Request request = evt.getRequest();
SAXReader reader = new SAXReader(); SAXReader reader = new SAXReader();
reader.setEncoding("GB2312"); reader.setEncoding("gbk");
Document xml = reader.read(new ByteArrayInputStream(request.getRawContent())); Document xml = reader.read(new ByteArrayInputStream(request.getRawContent()));
return xml.getRootElement(); return xml.getRootElement();
} }

View File

@ -1,5 +1,7 @@
package com.genersoft.iot.vmp.gb28181.transmit.response; package com.genersoft.iot.vmp.gb28181.transmit.response;
import java.text.ParseException;
import javax.sip.ResponseEvent; import javax.sip.ResponseEvent;
import com.genersoft.iot.vmp.conf.SipConfig; import com.genersoft.iot.vmp.conf.SipConfig;
@ -12,6 +14,6 @@ import com.genersoft.iot.vmp.gb28181.SipLayer;
*/ */
public interface ISIPResponseProcessor { public interface ISIPResponseProcessor {
public void process(ResponseEvent evt, SipLayer layer, SipConfig config); public void process(ResponseEvent evt, SipLayer layer, SipConfig config) throws ParseException;
} }

View File

@ -7,8 +7,11 @@ import javax.sip.Dialog;
import javax.sip.InvalidArgumentException; import javax.sip.InvalidArgumentException;
import javax.sip.ResponseEvent; import javax.sip.ResponseEvent;
import javax.sip.SipException; import javax.sip.SipException;
import javax.sip.address.Address;
import javax.sip.address.SipURI; import javax.sip.address.SipURI;
import javax.sip.header.CSeqHeader; import javax.sip.header.CSeqHeader;
import javax.sip.header.ContactHeader;
import javax.sip.header.FromHeader;
import javax.sip.header.ViaHeader; import javax.sip.header.ViaHeader;
import javax.sip.message.Request; import javax.sip.message.Request;
import javax.sip.message.Response; import javax.sip.message.Response;
@ -22,10 +25,13 @@ import com.genersoft.iot.vmp.gb28181.SipLayer;
import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorFactory; import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorFactory;
import com.genersoft.iot.vmp.gb28181.transmit.response.ISIPResponseProcessor; import com.genersoft.iot.vmp.gb28181.transmit.response.ISIPResponseProcessor;
// import java.util.regex.Pattern;
// import java.util.regex.Matcher;
/** /**
* @Description:INVITE * @Description:INVITE
* @author: songww * @author: songww
* @date: 202053 4:43:52 * @date: 202053 4:43:52
*/ */
@Component @Component
public class InviteResponseProcessor implements ISIPResponseProcessor { public class InviteResponseProcessor implements ISIPResponseProcessor {
@ -35,44 +41,62 @@ public class InviteResponseProcessor implements ISIPResponseProcessor {
/** /**
* invite * invite
* *
* @param evt * @param evt
* * @throws ParseException
*/ */
@Override @Override
public void process(ResponseEvent evt, SipLayer layer, SipConfig config) { public void process(ResponseEvent evt, SipLayer layer, SipConfig config) throws ParseException {
try { try {
Response response = evt.getResponse(); Response response = evt.getResponse();
int statusCode = response.getStatusCode(); int statusCode = response.getStatusCode();
//trying不会回复 // trying不会回复
if(statusCode == Response.TRYING){ if (statusCode == Response.TRYING) {
} }
//成功响应 // 成功响应
//下发ack // 下发ack
if(statusCode == Response.OK){ if (statusCode == Response.OK) {
// ClientTransaction clientTransaction = evt.getClientTransaction(); // ClientTransaction clientTransaction = evt.getClientTransaction();
// if(clientTransaction == null){ // if(clientTransaction == null){
// logger.error("回复ACK时clientTransaction为null >>> {}",response); // logger.error("回复ACK时clientTransaction为null >>> {}",response);
// return; // return;
// } // }
// Dialog clientDialog = clientTransaction.getDialog(); // Dialog clientDialog = clientTransaction.getDialog();
//
// CSeqHeader clientCSeqHeader = (CSeqHeader) response.getHeader(CSeqHeader.NAME); // CSeqHeader clientCSeqHeader = (CSeqHeader)
// long cseqId = clientCSeqHeader.getSeqNumber(); // response.getHeader(CSeqHeader.NAME);
// /* // long cseqId = clientCSeqHeader.getSeqNumber();
// createAck函数创建的ackRequest会采用Invite响应的200OK中的contact字段中的地址作为目标地址。 // /*
// 有的终端传上来的可能还是内网地址会造成ack发送不出去。接受不到音视频流 // createAck函数创建的ackRequest会采用Invite响应的200OK中的contact字段中的地址作为目标地址。
// 所以在此处统一替换地址。和响应消息的Via头中的地址保持一致。 // 有的终端传上来的可能还是内网地址会造成ack发送不出去。接受不到音视频流
// */ // 所以在此处统一替换地址。和响应消息的Via头中的地址保持一致。
// Request ackRequest = clientDialog.createAck(cseqId); // */
// SipURI requestURI = (SipURI) ackRequest.getRequestURI(); // Request ackRequest = clientDialog.createAck(cseqId);
// ViaHeader viaHeader = (ViaHeader) response.getHeader(ViaHeader.NAME); // SipURI requestURI = (SipURI) ackRequest.getRequestURI();
// requestURI.setHost(viaHeader.getHost()); // ViaHeader viaHeader = (ViaHeader) response.getHeader(ViaHeader.NAME);
// requestURI.setPort(viaHeader.getPort()); // try {
// clientDialog.sendAck(ackRequest); // requestURI.setHost(viaHeader.getHost());
// } catch (Exception e) {
// e.printStackTrace();
// }
// requestURI.setPort(viaHeader.getPort());
// clientDialog.sendAck(ackRequest);
Dialog dialog = evt.getDialog(); Dialog dialog = evt.getDialog();
Request reqAck =dialog.createAck(1L); CSeqHeader cseq = (CSeqHeader) response.getHeader(CSeqHeader.NAME);
Request reqAck = dialog.createAck(cseq.getSeqNumber());
SipURI requestURI = (SipURI) reqAck.getRequestURI();
ViaHeader viaHeader = (ViaHeader) response.getHeader(ViaHeader.NAME);
// String viaHost =viaHeader.getHost();
//getHost()函数取回的IP地址是“[xxx.xxx.xxx.xxx:yyyy]”的格式需用正则表达式截取为“xxx.xxx.xxx.xxx"格式
// Pattern p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+");
// Matcher matcher = p.matcher(viaHeader.getHost());
// if (matcher.find()) {
// requestURI.setHost(matcher.group());
// }
requestURI.setHost(viaHeader.getHost());
requestURI.setPort(viaHeader.getPort());
reqAck.setRequestURI(requestURI);
dialog.sendAck(reqAck); dialog.sendAck(reqAck);
} }
} catch (InvalidArgumentException | SipException e) { } catch (InvalidArgumentException | SipException e) {

View File

@ -22,7 +22,7 @@ import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
* @date: 202058 10:46:48 * @date: 202058 10:46:48
*/ */
@RestController @RestController
@RequestMapping("/hook/zlm") @RequestMapping("/index/hook")
public class ZLMHttpHookListener { public class ZLMHttpHookListener {
private final static Logger logger = LoggerFactory.getLogger(ZLMHttpHookListener.class); private final static Logger logger = LoggerFactory.getLogger(ZLMHttpHookListener.class);