panlinlin
2021-04-16 c5f2aba9b20089f184ce175b58a33a881ea9e0b1
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
@@ -23,7 +23,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
// import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@@ -333,17 +332,17 @@
   
    /**
    *    请求预览视频流
    * @param device  视频设备
    * @param channelId  预览通道
    * @param event hook订阅
    * @param errorEvent sip错误订阅
    */
     * @param device  视频设备
     * @param channelId  预览通道
     * @param event hook订阅
     * @param errorEvent sip错误订阅
     */
   @Override
   public void playStreamCmd(Device device, String channelId, ZLMHttpHookSubscribe.Event event, SipSubscribe.Event errorEvent) {
      String streamId = null;
      try {
         if (device == null) return;
         String ssrc = streamSession.createPlaySsrc();
         String streamId = null;
         if (rtpEnable) {
            streamId = String.format("gb_play_%s_%s", device.getDeviceId(), channelId);
         }else {
@@ -366,9 +365,14 @@
         // 添加订阅
         JSONObject subscribeKey = new JSONObject();
         subscribeKey.put("app", "rtp");
         subscribeKey.put("id", streamId);
         subscribeKey.put("stream", streamId);
         subscribeKey.put("regist", true);
         subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_publish, subscribeKey, event);
         subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey, json->{
            if (json.getJSONArray("tracks") == null) return;
            event.response(json);
            subscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey);
         });
         //
         StringBuffer content = new StringBuffer(200);
         content.append("v=0\r\n");
@@ -440,9 +444,12 @@
         Request request = headerProvider.createInviteRequest(device, channelId, content.toString(), null, "FromInvt" + tm, null, ssrc, callIdHeader);
         ClientTransaction transaction = transmitRequest(device, request, errorEvent);
         streamSession.put(streamId,ssrc, transaction);
         ClientTransaction transaction = transmitRequest(device, request, (e -> {
            streamSession.remove(device.getDeviceId(), channelId);
            errorEvent.response(e);
         }));
         streamSession.put(device.getDeviceId(), channelId ,ssrc,streamId, transaction);
      } catch ( SipException | ParseException | InvalidArgumentException e) {
         e.printStackTrace();
      }
@@ -466,9 +473,14 @@
         // 添加订阅
         JSONObject subscribeKey = new JSONObject();
         subscribeKey.put("app", "rtp");
         subscribeKey.put("id", streamId);
         subscribeKey.put("stream", streamId);
         subscribeKey.put("regist", true);
         subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_publish, subscribeKey, event);
         subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey, json->{
            if (json.getJSONArray("tracks") == null) return;
            event.response(json);
            subscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey);
         });
         StringBuffer content = new StringBuffer(200);
           content.append("v=0\r\n");
@@ -543,7 +555,7 @@
           Request request = headerProvider.createPlaybackInviteRequest(device, channelId, content.toString(), null, "fromplybck" + tm, null, callIdHeader);
           ClientTransaction transaction = transmitRequest(device, request, errorEvent);
           streamSession.put(streamId, ssrc, transaction);
           streamSession.put(device.getDeviceId(), channelId, ssrc, streamId, transaction);
      } catch ( SipException | ParseException | InvalidArgumentException e) {
         e.printStackTrace();
@@ -557,20 +569,21 @@
    * 
    */
   @Override
   public void streamByeCmd(String ssrc) {
      streamByeCmd(ssrc, null);
   public void streamByeCmd(String deviceId, String channelId) {
      streamByeCmd(deviceId, channelId, null);
   }
   @Override
   public void streamByeCmd(String streamId, SipSubscribe.Event okEvent) {
   public void streamByeCmd(String deviceId, String channelId, SipSubscribe.Event okEvent) {
      
      try {
         ClientTransaction transaction = streamSession.get(streamId);
         ClientTransaction transaction = streamSession.getTransaction(deviceId, channelId);
         // 服务重启后
         if (transaction == null) {
            StreamInfo streamInfo = redisCatchStorage.queryPlayByStreamId(streamId);
            StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
            if (streamInfo != null) {
               redisCatchStorage.stopPlay(streamInfo);
            }
            okEvent.response(null);
            return;
         }
         
@@ -604,14 +617,9 @@
         }
         dialog.sendRequest(clientTransaction);
         streamSession.remove(streamId);
         zlmrtpServerFactory.closeRTPServer(streamId);
      } catch (TransactionDoesNotExistException e) {
         e.printStackTrace();
      } catch (SipException e) {
         e.printStackTrace();
      } catch (ParseException e) {
         zlmrtpServerFactory.closeRTPServer(streamSession.getStreamId(deviceId, channelId));
         streamSession.remove(deviceId, channelId);
      } catch (SipException | ParseException e) {
         e.printStackTrace();
      }
   }
@@ -632,7 +640,6 @@
    * 语音广播
    * 
    * @param device  视频设备
    * @param channelId  预览通道
    */
   @Override
   public boolean audioBroadcastCmd(Device device) {
@@ -1131,7 +1138,7 @@
    * @param device      视频设备
    * @param startPriority   报警起始级别(可选)
    * @param endPriority   报警终止级别(可选)
    * @param alarmMethods   报警方式条件(可选)
    * @param alarmMethod   报警方式条件(可选)
    * @param alarmType      报警类型
    * @param startTime      报警发生起始时间(可选)
    * @param endTime      报警发生终止时间(可选)
@@ -1419,5 +1426,6 @@
         String streamId = String.format("gb_play_%s_%s", device.getDeviceId(), channelId);
         zlmrtpServerFactory.closeRTPServer(streamId);
      }
      streamSession.remove(device.getDeviceId(), channelId);
   }
}