648540858
2022-03-11 45e9fc880c61e12d6415755ee2a29f79fcf81fbb
src/main/java/com/genersoft/iot/vmp/gb28181/event/offline/OfflineEventListener.java
@@ -1,15 +1,27 @@
package com.genersoft.iot.vmp.gb28181.event.offline;
import com.genersoft.iot.vmp.conf.UserSetup;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.gb28181.bean.SsrcTransaction;
import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
import com.genersoft.iot.vmp.service.IMediaServerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import com.genersoft.iot.vmp.common.VideoManagerConstants;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
import com.genersoft.iot.vmp.utils.redis.RedisUtil;
import java.util.List;
/**
 * @description: 离线事件监听器,监听到离线后,修改设备离在线状态。 设备离线有两个来源:
@@ -25,12 +37,26 @@
   
   @Autowired
   private IVideoManagerStorager storager;
   @Autowired
   private VideoStreamSessionManager streamSession;
   
   @Autowired
    private RedisUtil redis;
   @Autowired
    private UserSetup userSetup;
   @Autowired
    private EventPublisher eventPublisher;
   @Autowired
    private IMediaServerService mediaServerService;
   @Autowired
    private ZLMRTPServerFactory zlmrtpServerFactory;
   @Override
   public void onApplicationEvent(OfflineEvent event) {
@@ -56,10 +82,22 @@
            }
      }
      List<DeviceChannel> deviceChannelList = storager.queryOnlineChannelsByDeviceId(event.getDeviceId());
      eventPublisher.catalogEventPublish(null, deviceChannelList, CatalogEvent.OFF);
      // 处理离线监听
      storager.outline(event.getDeviceId());
      // TODO 离线取消订阅
      // 离线释放所有ssrc
      List<SsrcTransaction> ssrcTransactions = streamSession.getSsrcTransactionForAll(event.getDeviceId(), null, null, null);
      if (ssrcTransactions != null && ssrcTransactions.size() > 0) {
         for (SsrcTransaction ssrcTransaction : ssrcTransactions) {
            mediaServerService.releaseSsrc(ssrcTransaction.getMediaServerId(), ssrcTransaction.getSsrc());
            mediaServerService.closeRTPServer(event.getDeviceId(), ssrcTransaction.getChannelId(), ssrcTransaction.getStream());
            streamSession.remove(event.getDeviceId(), ssrcTransaction.getChannelId(), ssrcTransaction.getStream());
         }
      }
   }
}