|  | @@ -2,11 +2,9 @@ package com.genersoft.iot.vmp.gb28181.transmit.cmd;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import java.text.ParseException;
 | 
	
		
			
				|  |  |  import java.util.ArrayList;
 | 
	
		
			
				|  |  | +import java.util.List;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import javax.sip.Dialog;
 | 
	
		
			
				|  |  | -import javax.sip.InvalidArgumentException;
 | 
	
		
			
				|  |  | -import javax.sip.PeerUnavailableException;
 | 
	
		
			
				|  |  | -import javax.sip.SipFactory;
 | 
	
		
			
				|  |  | +import javax.sip.*;
 | 
	
		
			
				|  |  |  import javax.sip.address.Address;
 | 
	
		
			
				|  |  |  import javax.sip.address.SipURI;
 | 
	
		
			
				|  |  |  import javax.sip.header.*;
 | 
	
	
		
			
				|  | @@ -15,7 +13,11 @@ import javax.sip.message.Request;
 | 
	
		
			
				|  |  |  import com.genersoft.iot.vmp.common.StreamInfo;
 | 
	
		
			
				|  |  |  import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
 | 
	
		
			
				|  |  |  import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
 | 
	
		
			
				|  |  | +import gov.nist.javax.sip.SipProviderImpl;
 | 
	
		
			
				|  |  | +import gov.nist.javax.sip.SipStackImpl;
 | 
	
		
			
				|  |  | +import gov.nist.javax.sip.stack.SIPDialog;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | +import org.springframework.beans.factory.annotation.Qualifier;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import com.genersoft.iot.vmp.conf.SipConfig;
 | 
	
	
		
			
				|  | @@ -40,6 +42,14 @@ public class SIPRequestHeaderProvider {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	@Autowired
 | 
	
		
			
				|  |  |  	private VideoStreamSessionManager streamSession;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	@Autowired
 | 
	
		
			
				|  |  | +	@Qualifier(value="tcpSipProvider")
 | 
	
		
			
				|  |  | +	private SipProviderImpl tcpSipProvider;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	@Autowired
 | 
	
		
			
				|  |  | +	@Qualifier(value="udpSipProvider")
 | 
	
		
			
				|  |  | +	private SipProviderImpl udpSipProvider;
 | 
	
		
			
				|  |  |  	
 | 
	
		
			
				|  |  |  	public Request createMessageRequest(Device device, String content, String viaTag, String fromTag, String toTag, CallIdHeader callIdHeader) throws ParseException, InvalidArgumentException, PeerUnavailableException {
 | 
	
		
			
				|  |  |  		Request request = null;
 | 
	
	
		
			
				|  | @@ -95,7 +105,7 @@ public class SIPRequestHeaderProvider {
 | 
	
		
			
				|  |  |  		MaxForwardsHeader maxForwards = sipFactory.createHeaderFactory().createMaxForwardsHeader(70);
 | 
	
		
			
				|  |  |  		
 | 
	
		
			
				|  |  |  		//ceq
 | 
	
		
			
				|  |  | -		CSeqHeader cSeqHeader = sipFactory.createHeaderFactory().createCSeqHeader(redisCatchStorage.getCSEQ(Request.INVITE), Request.INVITE);
 | 
	
		
			
				|  |  | +		CSeqHeader cSeqHeader = sipFactory.createHeaderFactory().createCSeqHeader(redisCatchStorage.getCSEQ(), Request.INVITE);
 | 
	
		
			
				|  |  |  		request = sipFactory.createMessageFactory().createRequest(requestLine, Request.INVITE, callIdHeader, cSeqHeader,fromHeader, toHeader, viaHeaders, maxForwards);
 | 
	
		
			
				|  |  |  		
 | 
	
		
			
				|  |  |  		Address concatAddress = sipFactory.createAddressFactory().createAddress(sipFactory.createAddressFactory().createSipURI(sipConfig.getId(), sipConfig.getIp()+":"+sipConfig.getPort()));
 | 
	
	
		
			
				|  | @@ -131,7 +141,7 @@ public class SIPRequestHeaderProvider {
 | 
	
		
			
				|  |  |  		MaxForwardsHeader maxForwards = sipFactory.createHeaderFactory().createMaxForwardsHeader(70);
 | 
	
		
			
				|  |  |  		
 | 
	
		
			
				|  |  |  		//ceq
 | 
	
		
			
				|  |  | -		CSeqHeader cSeqHeader = sipFactory.createHeaderFactory().createCSeqHeader(redisCatchStorage.getCSEQ(Request.INVITE), Request.INVITE);
 | 
	
		
			
				|  |  | +		CSeqHeader cSeqHeader = sipFactory.createHeaderFactory().createCSeqHeader(redisCatchStorage.getCSEQ(), Request.INVITE);
 | 
	
		
			
				|  |  |  		request = sipFactory.createMessageFactory().createRequest(requestLine, Request.INVITE, callIdHeader, cSeqHeader,fromHeader, toHeader, viaHeaders, maxForwards);
 | 
	
		
			
				|  |  |  		
 | 
	
		
			
				|  |  |  		Address concatAddress = sipFactory.createAddressFactory().createAddress(sipFactory.createAddressFactory().createSipURI(sipConfig.getId(), sipConfig.getIp()+":"+sipConfig.getPort()));
 | 
	
	
		
			
				|  | @@ -200,7 +210,7 @@ public class SIPRequestHeaderProvider {
 | 
	
		
			
				|  |  |  		MaxForwardsHeader maxForwards = sipFactory.createHeaderFactory().createMaxForwardsHeader(70);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  		// ceq
 | 
	
		
			
				|  |  | -		CSeqHeader cSeqHeader = sipFactory.createHeaderFactory().createCSeqHeader(redisCatchStorage.getCSEQ(Request.SUBSCRIBE), Request.SUBSCRIBE);
 | 
	
		
			
				|  |  | +		CSeqHeader cSeqHeader = sipFactory.createHeaderFactory().createCSeqHeader(redisCatchStorage.getCSEQ(), Request.SUBSCRIBE);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  		request = sipFactory.createMessageFactory().createRequest(requestURI, Request.SUBSCRIBE, callIdHeader, cSeqHeader, fromHeader,
 | 
	
		
			
				|  |  |  				toHeader, viaHeaders, maxForwards);
 | 
	
	
		
			
				|  | @@ -226,55 +236,55 @@ public class SIPRequestHeaderProvider {
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	public Request createInfoRequest(Device device, StreamInfo streamInfo, String content)
 | 
	
		
			
				|  |  | -			throws PeerUnavailableException, ParseException, InvalidArgumentException {
 | 
	
		
			
				|  |  | -		Request request = null;
 | 
	
		
			
				|  |  | +			throws SipException, ParseException, InvalidArgumentException {
 | 
	
		
			
				|  |  |  		if (streamInfo == null) {
 | 
	
		
			
				|  |  |  			return null;
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  | -		Dialog dialog = streamSession.getDialogByStream(streamInfo.getDeviceID(), streamInfo.getChannelId(), streamInfo.getStream());
 | 
	
		
			
				|  |  | +		Request request = null;
 | 
	
		
			
				|  |  | +		SIPDialog dialog = streamSession.getDialogByStream(streamInfo.getDeviceID(), streamInfo.getChannelId(), streamInfo.getStream());
 | 
	
		
			
				|  |  |  		if (dialog == null) {
 | 
	
		
			
				|  |  |  			return null;
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -		SipURI requestLine = sipFactory.createAddressFactory().createSipURI(device.getDeviceId(),
 | 
	
		
			
				|  |  | -				device.getHostAddress());
 | 
	
		
			
				|  |  | -		// via
 | 
	
		
			
				|  |  | -		ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
 | 
	
		
			
				|  |  | -		ViaHeader viaHeader = sipFactory.createHeaderFactory().createViaHeader(device.getIp(), device.getPort(),
 | 
	
		
			
				|  |  | -				device.getTransport(), null);
 | 
	
		
			
				|  |  | +		SipStack sipStack = udpSipProvider.getSipStack();
 | 
	
		
			
				|  |  | +		SIPDialog sipDialog = ((SipStackImpl) sipStack).putDialog(dialog);
 | 
	
		
			
				|  |  | +		if (dialog != sipDialog) {
 | 
	
		
			
				|  |  | +			dialog = sipDialog;
 | 
	
		
			
				|  |  | +		}else {
 | 
	
		
			
				|  |  | +			dialog.setSipProvider(udpSipProvider);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		streamSession.put(streamInfo.getDeviceID(), streamInfo.getChannelId(), dialog.getCallId().getCallId(), dialog);
 | 
	
		
			
				|  |  | +		Request infoRequest = dialog.createRequest(Request.INFO);
 | 
	
		
			
				|  |  | +		SipURI sipURI = (SipURI) infoRequest.getRequestURI();
 | 
	
		
			
				|  |  | +		sipURI.setHost(device.getIp());
 | 
	
		
			
				|  |  | +		sipURI.setPort(device.getPort());
 | 
	
		
			
				|  |  | +		sipURI.setUser(streamInfo.getChannelId());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		ViaHeader viaHeader = (ViaHeader) infoRequest.getHeader(ViaHeader.NAME);
 | 
	
		
			
				|  |  |  		viaHeader.setRPort();
 | 
	
		
			
				|  |  | -		viaHeaders.add(viaHeader);
 | 
	
		
			
				|  |  | -		// from
 | 
	
		
			
				|  |  | -		SipURI fromSipURI = sipFactory.createAddressFactory().createSipURI(sipConfig.getId(),
 | 
	
		
			
				|  |  | -				sipConfig.getDomain());
 | 
	
		
			
				|  |  | -		Address fromAddress = sipFactory.createAddressFactory().createAddress(fromSipURI);
 | 
	
		
			
				|  |  | -		FromHeader fromHeader = sipFactory.createHeaderFactory().createFromHeader(fromAddress, dialog.getLocalTag());
 | 
	
		
			
				|  |  | -		// to
 | 
	
		
			
				|  |  | -		SipURI toSipURI = sipFactory.createAddressFactory().createSipURI(streamInfo.getChannelId(),
 | 
	
		
			
				|  |  | -				sipConfig.getDomain());
 | 
	
		
			
				|  |  | -		Address toAddress = sipFactory.createAddressFactory().createAddress(toSipURI);
 | 
	
		
			
				|  |  | -		ToHeader toHeader = sipFactory.createHeaderFactory().createToHeader(toAddress, dialog.getRemoteTag());
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		// callid
 | 
	
		
			
				|  |  | -		CallIdHeader callIdHeader = dialog.getCallId();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		// Forwards
 | 
	
		
			
				|  |  | -		MaxForwardsHeader maxForwards = sipFactory.createHeaderFactory().createMaxForwardsHeader(70);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		Long cseq = redisCatchStorage.getCSEQ(Request.INVITE);
 | 
	
		
			
				|  |  | -		// ceq
 | 
	
		
			
				|  |  | -		CSeqHeader cSeqHeader = sipFactory.createHeaderFactory()
 | 
	
		
			
				|  |  | -				.createCSeqHeader(cseq, Request.INFO);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		request = sipFactory.createMessageFactory().createRequest(requestLine, Request.INFO, callIdHeader, cSeqHeader,
 | 
	
		
			
				|  |  | -				fromHeader, toHeader, viaHeaders, maxForwards);
 | 
	
		
			
				|  |  | +		// 增加Contact header
 | 
	
		
			
				|  |  |  		Address concatAddress = sipFactory.createAddressFactory().createAddress(sipFactory.createAddressFactory()
 | 
	
		
			
				|  |  |  				.createSipURI(sipConfig.getId(), sipConfig.getIp() + ":" + sipConfig.getPort()));
 | 
	
		
			
				|  |  | -		request.addHeader(sipFactory.createHeaderFactory().createContactHeader(concatAddress));
 | 
	
		
			
				|  |  | +		infoRequest.addHeader(sipFactory.createHeaderFactory().createContactHeader(concatAddress));
 | 
	
		
			
				|  |  | +		List<String> agentParam = new ArrayList<>();
 | 
	
		
			
				|  |  | +		agentParam.add("wvp-pro");
 | 
	
		
			
				|  |  | +		// TODO 添加版本信息以及日期
 | 
	
		
			
				|  |  | +		UserAgentHeader userAgentHeader = null;
 | 
	
		
			
				|  |  | +		try {
 | 
	
		
			
				|  |  | +			userAgentHeader = sipFactory.createHeaderFactory().createUserAgentHeader(agentParam);
 | 
	
		
			
				|  |  | +		} catch (ParseException e) {
 | 
	
		
			
				|  |  | +			throw new RuntimeException(e);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		infoRequest.addHeader(userAgentHeader);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  		ContentTypeHeader contentTypeHeader = sipFactory.createHeaderFactory().createContentTypeHeader("Application",
 | 
	
		
			
				|  |  |  				"MANSRTSP");
 | 
	
		
			
				|  |  | -		request.setContent(content, contentTypeHeader);
 | 
	
		
			
				|  |  | -		return request;
 | 
	
		
			
				|  |  | +		infoRequest.setContent(content, contentTypeHeader);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		CSeqHeader cSeqHeader = (CSeqHeader)infoRequest.getHeader(CSeqHeader.NAME);
 | 
	
		
			
				|  |  | +		cSeqHeader.setSeqNumber(redisCatchStorage.getCSEQ());
 | 
	
		
			
				|  |  | +		// ceq
 | 
	
		
			
				|  |  | +		infoRequest.addHeader(cSeqHeader);
 | 
	
		
			
				|  |  | +		return infoRequest;
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  }
 |