648540858
2020-10-14 b55374c75a17c87b9ccb8d5c12abe188875af256
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
@@ -1,35 +1,41 @@
package com.genersoft.iot.vmp.gb28181.transmit.cmd.impl;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.sip.ClientTransaction;
import javax.sip.Dialog;
import javax.sip.InvalidArgumentException;
import javax.sip.SipException;
import javax.sip.SipFactory;
import javax.sip.SipProvider;
import javax.sip.TransactionDoesNotExistException;
import javax.sip.address.Address;
import javax.sip.address.SipURI;
import javax.sip.header.ViaHeader;
import javax.sip.message.Request;
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.common.StreamInfo;
import com.genersoft.iot.vmp.conf.MediaServerConfig;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.media.zlm.ZLMUtils;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties.Headers;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.genersoft.iot.vmp.conf.SipConfig;
import com.genersoft.iot.vmp.gb28181.SipLayer;
import com.genersoft.iot.vmp.gb28181.bean.Device;
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 java.util.regex.Pattern;
import java.util.regex.Matcher;
/**    
 * @Description:设备能力接口,用于定义设备的控制、查询能力   
 * @author: songww
 * @author: swwheihei
 * @date:   2020年5月3日 下午9:22:48     
 */
@Component
@@ -42,11 +48,27 @@
   private SIPRequestHeaderProvider headerProvider;
   
   @Autowired
   private SipLayer sipLayer;
   private VideoStreamSessionManager streamSession;
   @Autowired
   private IVideoManagerStorager storager;
   
   @Autowired
   private VideoStreamSessionManager streamSession;
   @Qualifier(value="tcpSipProvider")
   private SipProvider tcpSipProvider;
   
   @Autowired
   @Qualifier(value="udpSipProvider")
   private SipProvider udpSipProvider;
   @Autowired
   private ZLMUtils zlmUtils;
   @Value("${media.rtp.enable}")
   private boolean rtpEnable;
   /**
    * 云台方向放控制,使用配置文件中的默认镜头移动速度
    * 
@@ -54,7 +76,6 @@
    * @param channelId  预览通道
    * @param leftRight  镜头左移右移 0:停止 1:左移 2:右移
     * @param upDown     镜头上移下移 0:停止 1:上移 2:下移
     * @param moveSpeed  镜头移动速度
    */
   @Override
   public boolean ptzdirectCmd(Device device, String channelId, int leftRight, int upDown) {
@@ -187,23 +208,31 @@
    * @param channelId  预览通道
    */  
   @Override
   public String playStreamCmd(Device device, String channelId) {
   public StreamInfo playStreamCmd(Device device, String channelId) {
      try {
         String ssrc = streamSession.createPlaySsrc();
         String transport = device.getTransport();
         MediaServerConfig mediaInfo = storager.getMediaInfo();
         String mediaPort = null;
         // 使用动态udp端口
         if (rtpEnable) {
            mediaPort = zlmUtils.getNewRTPPort(ssrc) + "";
         }else {
            mediaPort = mediaInfo.getRtpProxyPort();
         }
         //
         StringBuffer content = new StringBuffer(200);
           content.append("v=0\r\n");
           content.append("o="+channelId+" 0 0 IN IP4 "+sipConfig.getSipIp()+"\r\n");
           content.append("o="+channelId+" 0 0 IN IP4 "+mediaInfo.getLocalIP()+"\r\n");
           content.append("s=Play\r\n");
           content.append("c=IN IP4 "+sipConfig.getMediaIp()+"\r\n");
           content.append("c=IN IP4 "+mediaInfo.getLocalIP()+"\r\n");
           content.append("t=0 0\r\n");
           if("TCP".equals(transport)) {
              content.append("m=video "+sipConfig.getMediaPort()+" TCP/RTP/AVP 96 98 97\r\n");
              content.append("m=video "+ mediaPort +" TCP/RTP/AVP 96 98 97\r\n");
         }
           if("UDP".equals(transport)) {
              content.append("m=video "+sipConfig.getMediaPort()+" RTP/AVP 96 98 97\r\n");
              content.append("m=video "+ mediaPort +" RTP/AVP 96 98 97\r\n");
         }
           content.append("a=recvonly\r\n");
           content.append("a=rtpmap:96 PS/90000\r\n");
@@ -219,7 +248,18 @@
   
           ClientTransaction transaction = transmitRequest(device, request);
           streamSession.put(ssrc, transaction);
         return ssrc;
         DeviceChannel deviceChannel = storager.queryChannel(device.getDeviceId(), channelId);
         if (deviceChannel != null) {
            deviceChannel.setSsrc(ssrc);
            storager.updateChannel(device.getDeviceId(), deviceChannel);
         }
         StreamInfo streamInfo = new StreamInfo();
         streamInfo.setSsrc(ssrc);
         streamInfo.setCahnnelId(channelId);
         streamInfo.setDeviceID(device.getDeviceId());
         storager.startPlay(streamInfo);
         return streamInfo;
      } catch ( SipException | ParseException | InvalidArgumentException e) {
         e.printStackTrace();
         return null;
@@ -237,7 +277,7 @@
   @Override
   public String playbackStreamCmd(Device device, String channelId, String startTime, String endTime) {
      try {
         MediaServerConfig mediaInfo = storager.getMediaInfo();
         String ssrc = streamSession.createPlayBackSsrc();
         //
         StringBuffer content = new StringBuffer(200);
@@ -245,13 +285,13 @@
           content.append("o="+sipConfig.getSipId()+" 0 0 IN IP4 "+sipConfig.getSipIp()+"\r\n");
           content.append("s=Playback\r\n");
           content.append("u="+channelId+":0\r\n");
           content.append("c=IN IP4 "+sipConfig.getMediaIp()+"\r\n");
           content.append("c=IN IP4 "+mediaInfo.getLocalIP()+"\r\n");
           content.append("t="+DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime)+" "+DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime) +"\r\n");
           if(device.getTransport().equals("TCP")) {
              content.append("m=video "+sipConfig.getMediaPort()+" TCP/RTP/AVP 96 98 97\r\n");
              content.append("m=video "+mediaInfo.getRtpProxyPort()+" TCP/RTP/AVP 96 98 97\r\n");
         }
           if(device.getTransport().equals("UDP")) {
              content.append("m=video "+sipConfig.getMediaPort()+" RTP/AVP 96 98 97\r\n");
              content.append("m=video "+mediaInfo.getRtpProxyPort()+" RTP/AVP 96 98 97\r\n");
         }
           content.append("a=recvonly\r\n");
           content.append("a=rtpmap:96 PS/90000\r\n");
@@ -268,6 +308,7 @@
           ClientTransaction transaction = transmitRequest(device, request);
           streamSession.put(ssrc, transaction);
         return ssrc;
      } catch ( SipException | ParseException | InvalidArgumentException e) {
         e.printStackTrace();
         return null;
@@ -277,8 +318,6 @@
   /**
    * 视频流停止
    * 
    * @param device  视频设备
    * @param channelId  预览通道
    */
   @Override
   public void streamByeCmd(String ssrc) {
@@ -305,9 +344,9 @@
         String protocol = viaHeader.getTransport().toUpperCase();
         ClientTransaction clientTransaction = null;
         if("TCP".equals(protocol)) {
            clientTransaction = sipLayer.getTcpSipProvider().getNewClientTransaction(byeRequest);
            clientTransaction = tcpSipProvider.getNewClientTransaction(byeRequest);
         } else if("UDP".equals(protocol)) {
            clientTransaction = sipLayer.getUdpSipProvider().getNewClientTransaction(byeRequest);
            clientTransaction = udpSipProvider.getNewClientTransaction(byeRequest);
         }
         dialog.sendRequest(clientTransaction);
      } catch (TransactionDoesNotExistException e) {
@@ -443,6 +482,8 @@
    */ 
   @Override
   public boolean catalogQuery(Device device) {
      // 清空通道
      storager.cleanChannelsForDevice(device.getDeviceId());
      try {
         StringBuffer catalogXml = new StringBuffer(200);
         catalogXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
@@ -541,12 +582,13 @@
   private ClientTransaction transmitRequest(Device device, Request request) throws SipException {
      ClientTransaction clientTransaction = null;
      if("TCP".equals(device.getTransport())) {
         clientTransaction = sipLayer.getTcpSipProvider().getNewClientTransaction(request);
         clientTransaction = tcpSipProvider.getNewClientTransaction(request);
      } else if("UDP".equals(device.getTransport())) {
         clientTransaction = sipLayer.getUdpSipProvider().getNewClientTransaction(request);
         clientTransaction = udpSipProvider.getNewClientTransaction(request);
      }
      clientTransaction.sendRequest();
      return clientTransaction;
   }
}