mk1990
2022-05-16 e6790d88fc5b1ac33d239c0d90ce6cdea666b821
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
@@ -10,7 +10,7 @@
import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.SIPRequestHeaderProvider;
import com.genersoft.iot.vmp.gb28181.utils.DateUtil;
import com.genersoft.iot.vmp.utils.DateUtil;
import com.genersoft.iot.vmp.gb28181.utils.NumericUtil;
import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookSubscribe;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
@@ -37,7 +37,9 @@
import javax.sip.message.Request;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**    
 * @description:设备能力接口,用于定义设备的控制、查询能力   
@@ -49,7 +51,7 @@
public class SIPCommander implements ISIPCommander {
   private final Logger logger = LoggerFactory.getLogger(SIPCommander.class);
   @Autowired
   private SipConfig sipConfig;
@@ -346,7 +348,9 @@
                       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());
@@ -366,7 +370,7 @@
         //
         StringBuffer content = new StringBuffer(200);
         content.append("v=0\r\n");
         content.append("o="+ sipConfig.getId()+" 0 0 IN IP4 "+ mediaServerItem.getSdpIp() +"\r\n");
         content.append("o="+ channelId+" 0 0 IN IP4 "+ mediaServerItem.getSdpIp() +"\r\n");
         content.append("s=Play\r\n");
         content.append("c=IN IP4 "+ mediaServerItem.getSdpIp() +"\r\n");
         content.append("t=0 0\r\n");
@@ -385,8 +389,7 @@
            content.append("a=rtpmap:126 H264/90000\r\n");
            content.append("a=rtpmap:125 H264S/90000\r\n");
            content.append("a=fmtp:125 profile-level-id=42e01e\r\n");
            content.append("a=rtpmap:99 MP4V-ES/90000\r\n");
            content.append("a=fmtp:99 profile-level-id=3\r\n");
            content.append("a=rtpmap:99 H265/90000\r\n");
            content.append("a=rtpmap:98 H264/90000\r\n");
            content.append("a=rtpmap:97 MPEG4/90000\r\n");
            if("TCP-PASSIVE".equals(streamMode)){ // tcp被动模式
@@ -398,16 +401,17 @@
            }
         }else {
            if("TCP-PASSIVE".equals(streamMode)) {
               content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 98 97\r\n");
               content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 97 98 99\r\n");
            }else if ("TCP-ACTIVE".equals(streamMode)) {
               content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 98 97\r\n");
               content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 97 98 99\r\n");
            }else if("UDP".equals(streamMode)) {
               content.append("m=video "+ ssrcInfo.getPort() +" RTP/AVP 96 98 97\r\n");
               content.append("m=video "+ ssrcInfo.getPort() +" RTP/AVP 96 97 98 99\r\n");
            }
            content.append("a=recvonly\r\n");
            content.append("a=rtpmap:96 PS/90000\r\n");
            content.append("a=rtpmap:98 H264/90000\r\n");
            content.append("a=rtpmap:97 MPEG4/90000\r\n");
            content.append("a=rtpmap:99 H265/90000\r\n");
            if ("TCP-PASSIVE".equals(streamMode)) { // tcp被动模式
               content.append("a=setup:passive\r\n");
               content.append("a=connection:new\r\n");
@@ -463,7 +467,7 @@
         StringBuffer content = new StringBuffer(200);
           content.append("v=0\r\n");
           content.append("o="+sipConfig.getId()+" 0 0 IN IP4 " + mediaServerItem.getSdpIp() + "\r\n");
           content.append("o="+channelId+" 0 0 IN IP4 " + mediaServerItem.getSdpIp() + "\r\n");
           content.append("s=Playback\r\n");
           content.append("u="+channelId+":0\r\n");
           content.append("c=IN IP4 "+mediaServerItem.getSdpIp()+"\r\n");
@@ -486,8 +490,7 @@
            content.append("a=rtpmap:126 H264/90000\r\n");
            content.append("a=rtpmap:125 H264S/90000\r\n");
            content.append("a=fmtp:125 profile-level-id=42e01e\r\n");
            content.append("a=rtpmap:99 MP4V-ES/90000\r\n");
            content.append("a=fmtp:99 profile-level-id=3\r\n");
            content.append("a=rtpmap:99 H265/90000\r\n");
            content.append("a=rtpmap:98 H264/90000\r\n");
            content.append("a=rtpmap:97 MPEG4/90000\r\n");
            if("TCP-PASSIVE".equals(streamMode)){ // tcp被动模式
@@ -499,16 +502,17 @@
            }
         }else {
            if("TCP-PASSIVE".equals(streamMode)) {
               content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 98 97\r\n");
               content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 97 98 99\r\n");
            }else if ("TCP-ACTIVE".equals(streamMode)) {
               content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 98 97\r\n");
               content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 97 98 99\r\n");
            }else if("UDP".equals(streamMode)) {
               content.append("m=video "+ ssrcInfo.getPort() +" RTP/AVP 96 98 97\r\n");
               content.append("m=video "+ ssrcInfo.getPort() +" RTP/AVP 96 97 98 99\r\n");
            }
            content.append("a=recvonly\r\n");
            content.append("a=rtpmap:96 PS/90000\r\n");
            content.append("a=rtpmap:98 H264/90000\r\n");
            content.append("a=rtpmap:97 MPEG4/90000\r\n");
            content.append("a=rtpmap:98 H264/90000\r\n");
            content.append("a=rtpmap:99 H265/90000\r\n");
            if("TCP-PASSIVE".equals(streamMode)){ // tcp被动模式
               content.append("a=setup:passive\r\n");
               content.append("a=connection:new\r\n");
@@ -573,7 +577,7 @@
         StringBuffer content = new StringBuffer(200);
           content.append("v=0\r\n");
           content.append("o="+sipConfig.getId()+" 0 0 IN IP4 " + mediaServerItem.getSdpIp() + "\r\n");
           content.append("o="+channelId+" 0 0 IN IP4 " + mediaServerItem.getSdpIp() + "\r\n");
           content.append("s=Download\r\n");
           content.append("u="+channelId+":0\r\n");
           content.append("c=IN IP4 "+mediaServerItem.getSdpIp()+"\r\n");
@@ -609,16 +613,17 @@
            }
         }else {
            if("TCP-PASSIVE".equals(streamMode)) {
               content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 98 97\r\n");
               content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 97 98 99\r\n");
            }else if ("TCP-ACTIVE".equals(streamMode)) {
               content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 98 97\r\n");
               content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 97 98 99\r\n");
            }else if("UDP".equals(streamMode)) {
               content.append("m=video "+ ssrcInfo.getPort() +" RTP/AVP 96 98 97\r\n");
               content.append("m=video "+ ssrcInfo.getPort() +" RTP/AVP 96 97 98 99\r\n");
            }
            content.append("a=recvonly\r\n");
            content.append("a=rtpmap:96 PS/90000\r\n");
            content.append("a=rtpmap:98 H264/90000\r\n");
            content.append("a=rtpmap:97 MPEG4/90000\r\n");
            content.append("a=rtpmap:98 H264/90000\r\n");
            content.append("a=rtpmap:99 H265/90000\r\n");
            if("TCP-PASSIVE".equals(streamMode)){ // tcp被动模式
               content.append("a=setup:passive\r\n");
               content.append("a=connection:new\r\n");
@@ -647,6 +652,17 @@
               (MediaServerItem mediaServerItemInUse, JSONObject json)->{
                  hookEvent.call(new InviteStreamInfo(mediaServerItem, json, callIdHeader.getCallId(), "rtp", ssrcInfo.getStream()));
                  subscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey);
                  subscribeKey.put("regist", false);
                  subscribeKey.put("schema", "rtmp");
                  // 添加流注销的订阅,注销了后向设备发送bye
                  subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey,
                        (MediaServerItem mediaServerItemForEnd, JSONObject jsonForEnd)->{
                           ClientTransaction transaction = streamSession.getTransaction(device.getDeviceId(), channelId, ssrcInfo.getStream(), callIdHeader.getCallId());
                           if (transaction != null) {
                              logger.info("[录像]下载结束, 发送BYE");
                              streamByeCmd(device.getDeviceId(), channelId, ssrcInfo.getStream(), callIdHeader.getCallId());
                           }
                        });
               });
           Request request = headerProvider.createPlaybackInviteRequest(device, channelId, content.toString(), null, "fromplybck" + tm, null, callIdHeader, ssrcInfo.getSsrc());
@@ -679,10 +695,10 @@
   @Override
   public void streamByeCmd(String deviceId, String channelId, String stream, String callId, SipSubscribe.Event okEvent) {
      try {
         SsrcTransaction ssrcTransaction = streamSession.getSsrcTransaction(deviceId, channelId, null, stream);
         ClientTransaction transaction = streamSession.getTransactionByStream(deviceId, channelId, stream);
         SsrcTransaction ssrcTransaction = streamSession.getSsrcTransaction(deviceId, channelId, callId, stream);
         ClientTransaction transaction = streamSession.getTransaction(deviceId, channelId, stream, callId);
         if (transaction == null) {
         if (transaction == null ) {
            logger.warn("[ {} -> {}]停止视频流的时候发现事务已丢失", deviceId, channelId);
            SipSubscribe.EventResult<Object> eventResult = new SipSubscribe.EventResult<>();
            if (okEvent != null) {
@@ -694,7 +710,9 @@
         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) {
@@ -1454,6 +1472,7 @@
    * @param device   视频设备
    * @return         true = 命令发送成功
    */
   @Override
   public boolean mobilePositionSubscribe(Device device, Dialog dialog, SipSubscribe.Event okEvent ,SipSubscribe.Event errorEvent) {
      try {
         StringBuffer subscribePostitionXml = new StringBuffer(200);
@@ -1505,6 +1524,7 @@
    * @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);
@@ -1637,13 +1657,25 @@
      } else if("UDP".equals(device.getTransport())) {
         clientTransaction = udpSipProvider.getNewClientTransaction(request);
      }
      if (request.getHeader(UserAgentHeader.NAME) == null) {
         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);
         }
         request.addHeader(userAgentHeader);
      }
      CallIdHeader callIdHeader = (CallIdHeader)request.getHeader(CallIdHeader.NAME);
      // 添加错误订阅
      if (errorEvent != null) {
         sipSubscribe.addErrorSubscribe(callIdHeader.getCallId(), (eventResult -> {
            errorEvent.response(eventResult);
            sipSubscribe.removeErrorSubscribe(eventResult.callId);
            sipSubscribe.removeOkSubscribe(eventResult.callId);
         }));
      }
      // 添加订阅
@@ -1651,6 +1683,7 @@
         sipSubscribe.addOkSubscribe(callIdHeader.getCallId(), eventResult ->{
            okEvent.response(eventResult);
            sipSubscribe.removeOkSubscribe(eventResult.callId);
            sipSubscribe.removeErrorSubscribe(eventResult.callId);
         });
      }
@@ -1701,7 +1734,9 @@
         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())) {
@@ -1730,7 +1765,9 @@
         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())) {
@@ -1758,7 +1795,9 @@
         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())) {
@@ -1824,7 +1863,9 @@
      // 设置编码, 防止中文乱码
      messageFactory.setDefaultContentEncodingCharset(characterSet);
      Dialog dialog  = subscribeInfo.getDialog();
      if (dialog == null || !dialog.getState().equals(DialogState.CONFIRMED)) return;
      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);