|  |  |  | 
|---|
|  |  |  | import javax.sip.InvalidArgumentException; | 
|---|
|  |  |  | import javax.sip.PeerUnavailableException; | 
|---|
|  |  |  | import javax.sip.SipFactory; | 
|---|
|  |  |  | import javax.sip.SipProvider; | 
|---|
|  |  |  | import javax.sip.address.Address; | 
|---|
|  |  |  | import javax.sip.address.SipURI; | 
|---|
|  |  |  | import javax.sip.header.*; | 
|---|
|  |  |  | import javax.sip.message.Request; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SipFactory sipFactory; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | @Qualifier(value="tcpSipProvider") | 
|---|
|  |  |  | private SipProvider tcpSipProvider; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | @Qualifier(value="udpSipProvider") | 
|---|
|  |  |  | private SipProvider udpSipProvider; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Request createMessageRequest(Device device, String content, String viaTag, String fromTag, String toTag) throws ParseException, InvalidArgumentException, PeerUnavailableException { | 
|---|
|  |  |  | public Request createMessageRequest(Device device, String content, String viaTag, String fromTag, String toTag, CallIdHeader callIdHeader) throws ParseException, InvalidArgumentException, PeerUnavailableException { | 
|---|
|  |  |  | Request request = null; | 
|---|
|  |  |  | // sipuri | 
|---|
|  |  |  | SipURI requestURI = sipFactory.createAddressFactory().createSipURI(device.getDeviceId(), device.getHostAddress()); | 
|---|
|  |  |  | 
|---|
|  |  |  | SipURI toSipURI = sipFactory.createAddressFactory().createSipURI(device.getDeviceId(), sipConfig.getSipDomain()); | 
|---|
|  |  |  | Address toAddress = sipFactory.createAddressFactory().createAddress(toSipURI); | 
|---|
|  |  |  | ToHeader toHeader = sipFactory.createHeaderFactory().createToHeader(toAddress, toTag); | 
|---|
|  |  |  | // callid | 
|---|
|  |  |  | CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId() | 
|---|
|  |  |  | : udpSipProvider.getNewCallId(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // Forwards | 
|---|
|  |  |  | MaxForwardsHeader maxForwards = sipFactory.createHeaderFactory().createMaxForwardsHeader(70); | 
|---|
|  |  |  | // ceq | 
|---|
|  |  |  | 
|---|
|  |  |  | return request; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Request createInviteRequest(Device device, String channelId, String content, String viaTag, String fromTag, String toTag, String ssrc) throws ParseException, InvalidArgumentException, PeerUnavailableException { | 
|---|
|  |  |  | public Request createInviteRequest(Device device, String channelId, String content, String viaTag, String fromTag, String toTag, String ssrc, CallIdHeader callIdHeader) throws ParseException, InvalidArgumentException, PeerUnavailableException { | 
|---|
|  |  |  | Request request = null; | 
|---|
|  |  |  | //请求行 | 
|---|
|  |  |  | SipURI requestLine = sipFactory.createAddressFactory().createSipURI(channelId, device.getHostAddress()); | 
|---|
|  |  |  | 
|---|
|  |  |  | SipURI toSipURI = sipFactory.createAddressFactory().createSipURI(channelId,sipConfig.getSipDomain()); | 
|---|
|  |  |  | Address toAddress = sipFactory.createAddressFactory().createAddress(toSipURI); | 
|---|
|  |  |  | ToHeader toHeader = sipFactory.createHeaderFactory().createToHeader(toAddress,null); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //callid | 
|---|
|  |  |  | CallIdHeader callIdHeader = null; | 
|---|
|  |  |  | if(device.getTransport().equals("TCP")) { | 
|---|
|  |  |  | callIdHeader = tcpSipProvider.getNewCallId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(device.getTransport().equals("UDP")) { | 
|---|
|  |  |  | callIdHeader = udpSipProvider.getNewCallId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //Forwards | 
|---|
|  |  |  | MaxForwardsHeader maxForwards = sipFactory.createHeaderFactory().createMaxForwardsHeader(70); | 
|---|
|  |  |  | 
|---|
|  |  |  | return request; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Request createPlaybackInviteRequest(Device device, String channelId, String content, String viaTag, String fromTag, String toTag) throws ParseException, InvalidArgumentException, PeerUnavailableException { | 
|---|
|  |  |  | public Request createPlaybackInviteRequest(Device device, String channelId, String content, String viaTag, String fromTag, String toTag, CallIdHeader callIdHeader) throws ParseException, InvalidArgumentException, PeerUnavailableException { | 
|---|
|  |  |  | Request request = null; | 
|---|
|  |  |  | //请求行 | 
|---|
|  |  |  | SipURI requestLine = sipFactory.createAddressFactory().createSipURI(device.getDeviceId(), device.getHostAddress()); | 
|---|
|  |  |  | 
|---|
|  |  |  | SipURI toSipURI = sipFactory.createAddressFactory().createSipURI(channelId,sipConfig.getSipDomain()); | 
|---|
|  |  |  | Address toAddress = sipFactory.createAddressFactory().createAddress(toSipURI); | 
|---|
|  |  |  | ToHeader toHeader = sipFactory.createHeaderFactory().createToHeader(toAddress,null); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //callid | 
|---|
|  |  |  | CallIdHeader callIdHeader = null; | 
|---|
|  |  |  | if(device.getTransport().equals("TCP")) { | 
|---|
|  |  |  | callIdHeader = tcpSipProvider.getNewCallId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(device.getTransport().equals("UDP")) { | 
|---|
|  |  |  | callIdHeader = udpSipProvider.getNewCallId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //Forwards | 
|---|
|  |  |  | MaxForwardsHeader maxForwards = sipFactory.createHeaderFactory().createMaxForwardsHeader(70); | 
|---|
|  |  |  | 
|---|
|  |  |  | return request; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Request createSubscribeRequest(Device device, String content, String viaTag, String fromTag, String toTag, Integer expires, String event) throws ParseException, InvalidArgumentException, PeerUnavailableException { | 
|---|
|  |  |  | public Request createSubscribeRequest(Device device, String content, String viaTag, String fromTag, String toTag, Integer expires, String event, CallIdHeader callIdHeader) throws ParseException, InvalidArgumentException, PeerUnavailableException { | 
|---|
|  |  |  | Request request = null; | 
|---|
|  |  |  | // sipuri | 
|---|
|  |  |  | SipURI requestURI = sipFactory.createAddressFactory().createSipURI(device.getDeviceId(), device.getHostAddress()); | 
|---|
|  |  |  | 
|---|
|  |  |  | SipURI toSipURI = sipFactory.createAddressFactory().createSipURI(device.getDeviceId(), sipConfig.getSipDomain()); | 
|---|
|  |  |  | Address toAddress = sipFactory.createAddressFactory().createAddress(toSipURI); | 
|---|
|  |  |  | ToHeader toHeader = sipFactory.createHeaderFactory().createToHeader(toAddress, toTag); | 
|---|
|  |  |  | // callid | 
|---|
|  |  |  | CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId() | 
|---|
|  |  |  | : udpSipProvider.getNewCallId(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // Forwards | 
|---|
|  |  |  | MaxForwardsHeader maxForwards = sipFactory.createHeaderFactory().createMaxForwardsHeader(70); | 
|---|
|  |  |  | // ceq | 
|---|
|  |  |  | 
|---|
|  |  |  | request = sipFactory.createMessageFactory().createRequest(requestURI, Request.SUBSCRIBE, callIdHeader, cSeqHeader, fromHeader, | 
|---|
|  |  |  | toHeader, viaHeaders, maxForwards); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Address concatAddress = sipFactory.createAddressFactory().createAddress(sipFactory.createAddressFactory().createSipURI(sipConfig.getSipId(), sipConfig.getSipIp()+":"+sipConfig.getSipPort())); | 
|---|
|  |  |  | request.addHeader(sipFactory.createHeaderFactory().createContactHeader(concatAddress)); | 
|---|
|  |  |  |  | 
|---|