From 4ca7307eea4364567f5fa51b099dbb98b4c49e9f Mon Sep 17 00:00:00 2001 From: Lawrence <1934378145@qq.com> Date: Thu, 25 Jun 2020 15:33:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3invite=E5=90=8E=E6=94=B6?= =?UTF-8?q?=E5=88=B0=E9=83=A8=E5=88=86=E8=AE=BE=E5=A4=87200=20OK=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=9B=9E=E5=A4=8Dack=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/InviteResponseProcessor.java | 94 ++++++++++++------- 1 file changed, 59 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/response/impl/InviteResponseProcessor.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/response/impl/InviteResponseProcessor.java index 9b1783f..9c862fa 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/response/impl/InviteResponseProcessor.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/response/impl/InviteResponseProcessor.java @@ -7,8 +7,11 @@ import javax.sip.Dialog; import javax.sip.InvalidArgumentException; import javax.sip.ResponseEvent; import javax.sip.SipException; +import javax.sip.address.Address; import javax.sip.address.SipURI; import javax.sip.header.CSeqHeader; +import javax.sip.header.ContactHeader; +import javax.sip.header.FromHeader; import javax.sip.header.ViaHeader; import javax.sip.message.Request; import javax.sip.message.Response; @@ -22,57 +25,78 @@ import com.genersoft.iot.vmp.gb28181.SipLayer; import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorFactory; import com.genersoft.iot.vmp.gb28181.transmit.response.ISIPResponseProcessor; -/** +// import java.util.regex.Pattern; +// import java.util.regex.Matcher; + +/** * @Description:处理INVITE响应 * @author: songww - * @date: 2020年5月3日 下午4:43:52 + * @date: 2020年5月3日 下午4:43:52 */ @Component public class InviteResponseProcessor implements ISIPResponseProcessor { private final static Logger logger = LoggerFactory.getLogger(SIPProcessorFactory.class); - + /** * 处理invite响应 * - * @param evt - * 响应消息 - */ + * @param evt 响应消息 + * @throws ParseException + */ @Override - public void process(ResponseEvent evt, SipLayer layer, SipConfig config) { + public void process(ResponseEvent evt, SipLayer layer, SipConfig config) throws ParseException { try { Response response = evt.getResponse(); int statusCode = response.getStatusCode(); - //trying不会回复 - if(statusCode == Response.TRYING){ - + // trying不会回复 + if (statusCode == Response.TRYING) { } - //成功响应 - //下发ack - if(statusCode == Response.OK){ -// ClientTransaction clientTransaction = evt.getClientTransaction(); -// if(clientTransaction == null){ -// logger.error("回复ACK时,clientTransaction为null >>> {}",response); -// return; -// } -// Dialog clientDialog = clientTransaction.getDialog(); -// -// CSeqHeader clientCSeqHeader = (CSeqHeader) response.getHeader(CSeqHeader.NAME); -// long cseqId = clientCSeqHeader.getSeqNumber(); -// /* -// createAck函数,创建的ackRequest,会采用Invite响应的200OK,中的contact字段中的地址,作为目标地址。 -// 有的终端传上来的可能还是内网地址,会造成ack发送不出去。接受不到音视频流 -// 所以在此处统一替换地址。和响应消息的Via头中的地址保持一致。 -// */ -// Request ackRequest = clientDialog.createAck(cseqId); -// SipURI requestURI = (SipURI) ackRequest.getRequestURI(); -// ViaHeader viaHeader = (ViaHeader) response.getHeader(ViaHeader.NAME); -// requestURI.setHost(viaHeader.getHost()); -// requestURI.setPort(viaHeader.getPort()); -// clientDialog.sendAck(ackRequest); - + // 成功响应 + // 下发ack + if (statusCode == Response.OK) { + // ClientTransaction clientTransaction = evt.getClientTransaction(); + // if(clientTransaction == null){ + // logger.error("回复ACK时,clientTransaction为null >>> {}",response); + // return; + // } + // Dialog clientDialog = clientTransaction.getDialog(); + + // CSeqHeader clientCSeqHeader = (CSeqHeader) + // response.getHeader(CSeqHeader.NAME); + // long cseqId = clientCSeqHeader.getSeqNumber(); + // /* + // createAck函数,创建的ackRequest,会采用Invite响应的200OK,中的contact字段中的地址,作为目标地址。 + // 有的终端传上来的可能还是内网地址,会造成ack发送不出去。接受不到音视频流 + // 所以在此处统一替换地址。和响应消息的Via头中的地址保持一致。 + // */ + // Request ackRequest = clientDialog.createAck(cseqId); + // SipURI requestURI = (SipURI) ackRequest.getRequestURI(); + // ViaHeader viaHeader = (ViaHeader) response.getHeader(ViaHeader.NAME); + // try { + // requestURI.setHost(viaHeader.getHost()); + // } catch (Exception e) { + // e.printStackTrace(); + // } + // requestURI.setPort(viaHeader.getPort()); + // clientDialog.sendAck(ackRequest); + 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); } } catch (InvalidArgumentException | SipException e) {