| | |
| | | import com.genersoft.iot.vmp.conf.SipConfig; |
| | | import com.genersoft.iot.vmp.gb28181.bean.Device; |
| | | import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform; |
| | | import com.genersoft.iot.vmp.gb28181.bean.ParentPlatformCatch; |
| | | import com.genersoft.iot.vmp.gb28181.event.SipSubscribe; |
| | | import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager; |
| | | import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform; |
| | | import com.genersoft.iot.vmp.gb28181.transmit.cmd.SIPRequestHeaderPlarformProvider; |
| | | import com.genersoft.iot.vmp.gb28181.transmit.cmd.SIPRequestHeaderProvider; |
| | | import com.genersoft.iot.vmp.media.zlm.ZLMUtils; |
| | | import com.genersoft.iot.vmp.storager.IRedisCatchStorage; |
| | | import com.genersoft.iot.vmp.storager.IVideoManagerStorager; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Qualifier; |
| | |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.sip.*; |
| | | import javax.sip.header.CallIdHeader; |
| | | import javax.sip.header.WWWAuthenticateHeader; |
| | | import javax.sip.message.Request; |
| | | import java.text.ParseException; |
| | | import java.util.UUID; |
| | | |
| | | @Component |
| | | public class SIPCommanderFroPlatform implements ISIPCommanderForPlatform { |
| | |
| | | private SIPRequestHeaderProvider headerProvider; |
| | | |
| | | @Autowired |
| | | private SIPRequestHeaderPlarformProvider headerProviderPlarformProvider; |
| | | |
| | | @Autowired |
| | | private VideoStreamSessionManager streamSession; |
| | | |
| | | @Autowired |
| | | private IVideoManagerStorager storager; |
| | | |
| | | @Autowired |
| | | private IRedisCatchStorage redisCatchStorage; |
| | | |
| | | @Autowired |
| | | private SipSubscribe sipSubscribe; |
| | | |
| | | @Autowired |
| | | @Qualifier(value="tcpSipProvider") |
| | |
| | | @Qualifier(value="udpSipProvider") |
| | | private SipProvider udpSipProvider; |
| | | |
| | | @Autowired |
| | | private ZLMUtils zlmUtils; |
| | | |
| | | @Value("${media.rtp.enable}") |
| | | private boolean rtpEnable; |
| | | |
| | | @Override |
| | | public boolean register(ParentPlatform parentPlatform, @Nullable String callId, @Nullable String realm, @Nullable String nonce, @Nullable String scheme ) { |
| | | public boolean register(ParentPlatform parentPlatform) { |
| | | return register(parentPlatform, null, null, null, null); |
| | | } |
| | | |
| | | @Override |
| | | public boolean unregister(ParentPlatform parentPlatform, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) { |
| | | parentPlatform.setExpires("0"); |
| | | ParentPlatformCatch parentPlatformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getDeviceGBId()); |
| | | if (parentPlatformCatch != null) { |
| | | parentPlatformCatch.setParentPlatform(parentPlatform); |
| | | redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch); |
| | | } |
| | | |
| | | return register(parentPlatform, null, null, errorEvent, okEvent); |
| | | } |
| | | |
| | | @Override |
| | | public boolean register(ParentPlatform parentPlatform, @Nullable String callId, @Nullable WWWAuthenticateHeader www, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) { |
| | | try { |
| | | Request request = null; |
| | | if (realm == null || nonce == null) { |
| | | request = headerProvider.createRegisterRequest(parentPlatform, 1L, null, null); |
| | | |
| | | if (www == null ) { |
| | | request = headerProviderPlarformProvider.createRegisterRequest(parentPlatform, 1L, null, null); |
| | | }else { |
| | | request = headerProvider.createRegisterRequest(parentPlatform, null, null, callId, realm, nonce, scheme); |
| | | request = headerProviderPlarformProvider.createRegisterRequest(parentPlatform, null, null, callId, www); |
| | | } |
| | | |
| | | transmitRequest(parentPlatform, request); |
| | | transmitRequest(parentPlatform, request, errorEvent, okEvent); |
| | | return true; |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public String keepalive(ParentPlatform parentPlatform) { |
| | | String callId = null; |
| | | try { |
| | | |
| | | StringBuffer keepaliveXml = new StringBuffer(200); |
| | | keepaliveXml.append("<?xml version=\"1.0\" encoding=\"GB2312\" ?>\r\n"); |
| | | keepaliveXml.append("<Notify>\r\n"); |
| | | keepaliveXml.append("<CmdType>Keepalive</CmdType>\r\n"); |
| | | keepaliveXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); |
| | | keepaliveXml.append("<DeviceID>" + parentPlatform.getServerGBId() + "</DeviceID>\r\n"); |
| | | keepaliveXml.append("<Status>OK</Status>\r\n"); |
| | | keepaliveXml.append("</Notify>\r\n"); |
| | | |
| | | Request request = headerProviderPlarformProvider.createKeetpaliveMessageRequest( |
| | | parentPlatform, |
| | | keepaliveXml.toString(), |
| | | UUID.randomUUID().toString().replace("-", ""), |
| | | UUID.randomUUID().toString().replace("-", ""), |
| | | null); |
| | | transmitRequest(parentPlatform, request); |
| | | CallIdHeader callIdHeader = (CallIdHeader)request.getHeader(CallIdHeader.NAME); |
| | | callId = callIdHeader.getCallId(); |
| | | } catch (ParseException | InvalidArgumentException | SipException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return callId; |
| | | } |
| | | |
| | | private void transmitRequest(ParentPlatform parentPlatform, Request request) throws SipException { |
| | | transmitRequest(parentPlatform, request, null, null); |
| | | } |
| | | |
| | | private void transmitRequest(ParentPlatform parentPlatform, Request request, SipSubscribe.Event errorEvent) throws SipException { |
| | | transmitRequest(parentPlatform, request, errorEvent, null); |
| | | } |
| | | |
| | | private void transmitRequest(ParentPlatform parentPlatform, Request request, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws SipException { |
| | | if("TCP".equals(parentPlatform.getTransport())) { |
| | | tcpSipProvider.sendRequest(request); |
| | | } else if("UDP".equals(parentPlatform.getTransport())) { |
| | | udpSipProvider.sendRequest(request); |
| | | } |
| | | |
| | | CallIdHeader callIdHeader = (CallIdHeader)request.getHeader(CallIdHeader.NAME); |
| | | // 添加错误订阅 |
| | | if (errorEvent != null) { |
| | | sipSubscribe.addErrorSubscribe(callIdHeader.getCallId(), errorEvent); |
| | | } |
| | | // 添加订阅 |
| | | if (okEvent != null) { |
| | | sipSubscribe.addOkSubscribe(callIdHeader.getCallId(), okEvent); |
| | | } |
| | | |
| | | } |
| | | } |