| | |
| | | import com.genersoft.iot.vmp.conf.DynamicTask;
|
| | | import com.genersoft.iot.vmp.conf.SipConfig;
|
| | | import com.genersoft.iot.vmp.conf.UserSetting;
|
| | | import com.genersoft.iot.vmp.gb28181.bean.Device;
|
| | | import com.genersoft.iot.vmp.gb28181.bean.InviteStreamCallback;
|
| | | import com.genersoft.iot.vmp.gb28181.bean.InviteStreamInfo;
|
| | | import com.genersoft.iot.vmp.gb28181.bean.SsrcTransaction;
|
| | | import com.genersoft.iot.vmp.gb28181.bean.*;
|
| | | import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
|
| | | import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
|
| | | import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
|
| | |
| | | import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
|
| | | import gov.nist.javax.sip.SipProviderImpl;
|
| | | import gov.nist.javax.sip.SipStackImpl;
|
| | | import gov.nist.javax.sip.message.MessageFactoryImpl;
|
| | | import gov.nist.javax.sip.message.SIPRequest;
|
| | | import gov.nist.javax.sip.stack.SIPDialog;
|
| | | import org.slf4j.Logger;
|
| | |
| | |
|
| | | import javax.sip.*;
|
| | | import javax.sip.address.SipURI;
|
| | | import javax.sip.header.CallIdHeader;
|
| | | import javax.sip.header.ViaHeader;
|
| | | import javax.sip.header.*;
|
| | | import javax.sip.message.Request;
|
| | | import java.lang.reflect.Field;
|
| | | import java.text.ParseException;
|
| | |
| | |
|
| | | @Autowired
|
| | | private SipConfig sipConfig;
|
| | |
|
| | | @Autowired
|
| | | private SipFactory sipFactory;
|
| | |
|
| | | @Autowired
|
| | | @Qualifier(value="tcpSipProvider")
|
| | |
| | | * @param channelId 预览通道
|
| | | * @param event hook订阅
|
| | | * @param errorEvent sip错误订阅
|
| | | */
|
| | | */
|
| | | @Override
|
| | | public void playStreamCmd(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, Device device, String channelId,
|
| | | ZLMHttpHookSubscribe.Event event, SipSubscribe.Event errorEvent) {
|
| | | ZLMHttpHookSubscribe.Event event, SipSubscribe.Event okEvent, SipSubscribe.Event errorEvent) {
|
| | | String streamId = ssrcInfo.getStream();
|
| | | try {
|
| | | if (device == null) return;
|
| | | if (device == null) {
|
| | | return;
|
| | | }
|
| | | String streamMode = device.getStreamMode().toUpperCase();
|
| | |
|
| | | logger.info("{} 分配的ZLM为: {} [{}:{}]", streamId, mediaServerItem.getId(), mediaServerItem.getIp(), ssrcInfo.getPort());
|
| | |
| | | // 这里为例避免一个通道的点播只有一个callID这个参数使用一个固定值
|
| | | streamSession.put(device.getDeviceId(), channelId ,"play", streamId, ssrcInfo.getSsrc(), mediaServerItem.getId(), ((ResponseEvent)e.event).getClientTransaction(), VideoStreamSessionManager.SessionType.play);
|
| | | streamSession.put(device.getDeviceId(), channelId ,"play", e.dialog);
|
| | | okEvent.response(e);
|
| | | });
|
| | |
|
| | |
|
| | |
| | | if (callId != null) {
|
| | | dialog = streamSession.getDialogByCallId(deviceId, channelId, callId);
|
| | | }else {
|
| | | if (stream == null) return;
|
| | | if (stream == null) {
|
| | | return;
|
| | | }
|
| | | dialog = streamSession.getDialogByStream(deviceId, channelId, stream);
|
| | | }
|
| | | if (ssrcTransaction != null) {
|
| | |
| | | * @param device 视频设备
|
| | | */
|
| | | @Override
|
| | | public boolean catalogQuery(Device device, SipSubscribe.Event errorEvent) {
|
| | | public boolean catalogQuery(Device device, int sn, SipSubscribe.Event errorEvent) {
|
| | | try {
|
| | | StringBuffer catalogXml = new StringBuffer(200);
|
| | | String charset = device.getCharset();
|
| | | catalogXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n");
|
| | | catalogXml.append("<Query>\r\n");
|
| | | catalogXml.append("<CmdType>Catalog</CmdType>\r\n");
|
| | | catalogXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
|
| | | catalogXml.append("<SN>" + sn + "</SN>\r\n");
|
| | | catalogXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
|
| | | catalogXml.append("</Query>\r\n");
|
| | |
|
| | |
| | | * @param device 视频设备
|
| | | * @return true = 命令发送成功
|
| | | */
|
| | | public boolean mobilePositionSubscribe(Device device, SipSubscribe.Event okEvent ,SipSubscribe.Event errorEvent) {
|
| | | @Override
|
| | | public boolean mobilePositionSubscribe(Device device, Dialog dialog, SipSubscribe.Event okEvent ,SipSubscribe.Event errorEvent) {
|
| | | try {
|
| | | StringBuffer subscribePostitionXml = new StringBuffer(200);
|
| | | String charset = device.getCharset();
|
| | |
| | | }
|
| | | subscribePostitionXml.append("</Query>\r\n");
|
| | |
|
| | | String tm = Long.toString(System.currentTimeMillis());
|
| | |
|
| | | CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
|
| | | : udpSipProvider.getNewCallId();
|
| | |
|
| | | Request request = headerProvider.createSubscribeRequest(device, subscribePostitionXml.toString(), "z9hG4bK-viaPos-" + tm, "fromTagPos" + tm, null, device.getSubscribeCycleForMobilePosition(), "presence" ,callIdHeader); //Position;id=" + tm.substring(tm.length() - 4));
|
| | | Request request;
|
| | | if (dialog != null) {
|
| | | logger.info("发送移动位置订阅消息时 dialog的状态为: {}", dialog.getState());
|
| | | request = dialog.createRequest(Request.SUBSCRIBE);
|
| | | ContentTypeHeader contentTypeHeader = sipFactory.createHeaderFactory().createContentTypeHeader("Application", "MANSCDP+xml");
|
| | | request.setContent(subscribePostitionXml.toString(), contentTypeHeader);
|
| | | ExpiresHeader expireHeader = sipFactory.createHeaderFactory().createExpiresHeader(device.getSubscribeCycleForMobilePosition());
|
| | | request.addHeader(expireHeader);
|
| | | }else {
|
| | | String tm = Long.toString(System.currentTimeMillis());
|
| | | CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
|
| | | : udpSipProvider.getNewCallId();
|
| | | request = headerProvider.createSubscribeRequest(device, subscribePostitionXml.toString(), "z9hG4bK-viaPos-" + tm, "fromTagPos" + tm, null, device.getSubscribeCycleForMobilePosition(), "presence" ,callIdHeader); //Position;id=" + tm.substring(tm.length() - 4));
|
| | | }
|
| | | transmitRequest(device, request, errorEvent, okEvent);
|
| | |
|
| | | return true;
|
| | |
| | | * @param endTime 报警发生终止时间(可选)
|
| | | * @return true = 命令发送成功
|
| | | */
|
| | | @Override
|
| | | public boolean alarmSubscribe(Device device, int expires, String startPriority, String endPriority, String alarmMethod, String alarmType, String startTime, String endTime) {
|
| | | try {
|
| | | StringBuffer cmdXml = new StringBuffer(200);
|
| | |
| | | }
|
| | |
|
| | | @Override
|
| | | public boolean catalogSubscribe(Device device, SipSubscribe.Event okEvent, SipSubscribe.Event errorEvent) {
|
| | | public boolean catalogSubscribe(Device device, Dialog dialog, SipSubscribe.Event okEvent, SipSubscribe.Event errorEvent) {
|
| | | try {
|
| | | StringBuffer cmdXml = new StringBuffer(200);
|
| | | String charset = device.getCharset();
|
| | |
| | | cmdXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
|
| | | cmdXml.append("</Query>\r\n");
|
| | |
|
| | | String tm = Long.toString(System.currentTimeMillis());
|
| | |
|
| | | CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
|
| | | : udpSipProvider.getNewCallId();
|
| | | Request request;
|
| | | if (dialog != null) {
|
| | | logger.info("发送目录订阅消息时 dialog的状态为: {}", dialog.getState());
|
| | | request = dialog.createRequest(Request.SUBSCRIBE);
|
| | | ContentTypeHeader contentTypeHeader = sipFactory.createHeaderFactory().createContentTypeHeader("Application", "MANSCDP+xml");
|
| | | request.setContent(cmdXml.toString(), contentTypeHeader);
|
| | | ExpiresHeader expireHeader = sipFactory.createHeaderFactory().createExpiresHeader(device.getSubscribeCycleForMobilePosition());
|
| | | request.addHeader(expireHeader);
|
| | | }else {
|
| | | String tm = Long.toString(System.currentTimeMillis());
|
| | |
|
| | | // 有效时间默认为60秒以上
|
| | | Request request = headerProvider.createSubscribeRequest(device, cmdXml.toString(), "z9hG4bK-viaPos-" + tm,
|
| | | "fromTagPos" + tm, null, device.getSubscribeCycleForCatalog(), "Catalog" ,
|
| | | callIdHeader);
|
| | | CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
|
| | | : udpSipProvider.getNewCallId();
|
| | |
|
| | | // 有效时间默认为60秒以上
|
| | | request = headerProvider.createSubscribeRequest(device, cmdXml.toString(), "z9hG4bK-viaPos-" + tm,
|
| | | "fromTagPos" + tm, null, device.getSubscribeCycleForCatalog(), "Catalog" ,
|
| | | callIdHeader);
|
| | |
|
| | | }
|
| | | transmitRequest(device, request, errorEvent, okEvent);
|
| | |
|
| | | return true;
|
| | |
|
| | | } catch ( NumberFormatException | ParseException | InvalidArgumentException | SipException e) {
|
| | |
| | | content.append("CSeq: " + cseq + "\r\n");
|
| | | content.append("Range: npt=now-\r\n");
|
| | | Request request = headerProvider.createInfoRequest(device, streamInfo, content.toString());
|
| | | if (request == null) return;
|
| | | if (request == null) {
|
| | | return;
|
| | | }
|
| | | logger.info(request.toString());
|
| | | ClientTransaction clientTransaction = null;
|
| | | if ("TCP".equals(device.getTransport())) {
|
| | |
| | | content.append("Range: npt=" + Math.abs(seekTime) + "-\r\n");
|
| | |
|
| | | Request request = headerProvider.createInfoRequest(device, streamInfo, content.toString());
|
| | | if (request == null) return;
|
| | | if (request == null) {
|
| | | return;
|
| | | }
|
| | | logger.info(request.toString());
|
| | | ClientTransaction clientTransaction = null;
|
| | | if ("TCP".equals(device.getTransport())) {
|
| | |
| | | content.append("CSeq: " + cseq + "\r\n");
|
| | | content.append("Scale: " + String.format("%.1f",speed) + "\r\n");
|
| | | Request request = headerProvider.createInfoRequest(device, streamInfo, content.toString());
|
| | | if (request == null) return;
|
| | | if (request == null) {
|
| | | return;
|
| | | }
|
| | | logger.info(request.toString());
|
| | | ClientTransaction clientTransaction = null;
|
| | | if ("TCP".equals(device.getTransport())) {
|
| | |
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | @Override
|
| | | public boolean sendAlarmMessage(Device device, DeviceAlarm deviceAlarm) {
|
| | | if (device == null) {
|
| | | return false;
|
| | | }
|
| | | logger.info("[发送 报警通知] {}/{}->{},{}", device.getDeviceId(), deviceAlarm.getChannelId(),
|
| | | deviceAlarm.getLongitude(), deviceAlarm.getLatitude());
|
| | | try {
|
| | | String characterSet = device.getCharset();
|
| | | StringBuffer deviceStatusXml = new StringBuffer(600);
|
| | | deviceStatusXml.append("<?xml version=\"1.0\" encoding=\"" + characterSet + "\"?>\r\n");
|
| | | deviceStatusXml.append("<Notify>\r\n");
|
| | | deviceStatusXml.append("<CmdType>Alarm</CmdType>\r\n");
|
| | | deviceStatusXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
|
| | | deviceStatusXml.append("<DeviceID>" + deviceAlarm.getChannelId() + "</DeviceID>\r\n");
|
| | | deviceStatusXml.append("<AlarmPriority>" + deviceAlarm.getAlarmPriority() + "</AlarmPriority>\r\n");
|
| | | deviceStatusXml.append("<AlarmMethod>" + deviceAlarm.getAlarmMethod() + "</AlarmMethod>\r\n");
|
| | | deviceStatusXml.append("<AlarmTime>" + deviceAlarm.getAlarmTime() + "</AlarmTime>\r\n");
|
| | | deviceStatusXml.append("<AlarmDescription>" + deviceAlarm.getAlarmDescription() + "</AlarmDescription>\r\n");
|
| | | deviceStatusXml.append("<Longitude>" + deviceAlarm.getLongitude() + "</Longitude>\r\n");
|
| | | deviceStatusXml.append("<Latitude>" + deviceAlarm.getLatitude() + "</Latitude>\r\n");
|
| | | deviceStatusXml.append("<info>\r\n");
|
| | | deviceStatusXml.append("<AlarmType>" + deviceAlarm.getAlarmType() + "</AlarmType>\r\n");
|
| | | deviceStatusXml.append("</info>\r\n");
|
| | | deviceStatusXml.append("</Notify>\r\n");
|
| | |
|
| | | CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
|
| | | : udpSipProvider.getNewCallId();
|
| | | String tm = Long.toString(System.currentTimeMillis());
|
| | | Request request = headerProvider.createMessageRequest(device, deviceStatusXml.toString(), "z9hG4bK-ViaPtz-" + tm, "FromPtz" + tm, null, callIdHeader);
|
| | | transmitRequest(device, request);
|
| | |
|
| | |
|
| | | } catch (SipException | ParseException e) {
|
| | | e.printStackTrace();
|
| | | return false;
|
| | | } catch (InvalidArgumentException e) {
|
| | | throw new RuntimeException(e);
|
| | | }
|
| | | return true;
|
| | | }
|
| | |
|
| | | private void sendNotify(Device device, String catalogXmlContent,
|
| | | SubscribeInfo subscribeInfo, SipSubscribe.Event errorEvent, SipSubscribe.Event okEvent )
|
| | | throws NoSuchFieldException, IllegalAccessException, SipException, ParseException {
|
| | | MessageFactoryImpl messageFactory = (MessageFactoryImpl) sipFactory.createMessageFactory();
|
| | | String characterSet = device.getCharset();
|
| | | // 设置编码, 防止中文乱码
|
| | | messageFactory.setDefaultContentEncodingCharset(characterSet);
|
| | | Dialog dialog = subscribeInfo.getDialog();
|
| | | if (dialog == null || !dialog.getState().equals(DialogState.CONFIRMED)) {
|
| | | return;
|
| | | }
|
| | | SIPRequest notifyRequest = (SIPRequest)dialog.createRequest(Request.NOTIFY);
|
| | | ContentTypeHeader contentTypeHeader = sipFactory.createHeaderFactory().createContentTypeHeader("Application", "MANSCDP+xml");
|
| | | notifyRequest.setContent(catalogXmlContent, contentTypeHeader);
|
| | |
|
| | | SubscriptionStateHeader subscriptionState = sipFactory.createHeaderFactory()
|
| | | .createSubscriptionStateHeader(SubscriptionStateHeader.ACTIVE);
|
| | | notifyRequest.addHeader(subscriptionState);
|
| | |
|
| | | EventHeader event = sipFactory.createHeaderFactory().createEventHeader(subscribeInfo.getEventType());
|
| | | if (subscribeInfo.getEventId() != null) {
|
| | | event.setEventId(subscribeInfo.getEventId());
|
| | | }
|
| | | notifyRequest.addHeader(event);
|
| | |
|
| | | SipURI sipURI = (SipURI) notifyRequest.getRequestURI();
|
| | | if (subscribeInfo.getTransaction() != null) {
|
| | | SIPRequest request = (SIPRequest) subscribeInfo.getTransaction().getRequest();
|
| | | sipURI.setHost(request.getRemoteAddress().getHostAddress());
|
| | | sipURI.setPort(request.getRemotePort());
|
| | | }else {
|
| | | sipURI.setHost(device.getIp());
|
| | | sipURI.setPort(device.getPort());
|
| | | }
|
| | |
|
| | | ClientTransaction transaction = null;
|
| | | if ("TCP".equals(device.getTransport())) {
|
| | | transaction = tcpSipProvider.getNewClientTransaction(notifyRequest);
|
| | | } else if ("UDP".equals(device.getTransport())) {
|
| | | transaction = udpSipProvider.getNewClientTransaction(notifyRequest);
|
| | | }
|
| | | // 添加错误订阅
|
| | | if (errorEvent != null) {
|
| | | sipSubscribe.addErrorSubscribe(subscribeInfo.getCallId(), errorEvent);
|
| | | }
|
| | | // 添加订阅
|
| | | if (okEvent != null) {
|
| | | sipSubscribe.addOkSubscribe(subscribeInfo.getCallId(), okEvent);
|
| | | }
|
| | | if (transaction == null) {
|
| | | logger.error("平台{}的Transport错误:{}",device.getDeviceId(), device.getTransport());
|
| | | return;
|
| | | }
|
| | | dialog.sendRequest(transaction);
|
| | |
|
| | | }
|
| | | }
|