ZhaoYandong00
2024-07-08 d1af677fff6ad931d0ba9a8506f516a9ceda79ab
src/main/java/com/genersoft/iot/vmp/media/event/hook/HookSubscribe.java
@@ -1,17 +1,11 @@
package com.genersoft.iot.vmp.media.event.hook;
import com.genersoft.iot.vmp.media.event.media.MediaArrivalEvent;
import com.genersoft.iot.vmp.media.event.media.MediaDepartureEvent;
import com.genersoft.iot.vmp.media.event.media.MediaEvent;
import com.genersoft.iot.vmp.media.event.media.MediaPublishEvent;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import com.genersoft.iot.vmp.media.event.media.*;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -25,7 +19,7 @@
    /**
     * 订阅数据过期时间
     */
    private final long subscribeExpire = 5 * 1000;
    private final long subscribeExpire = 5 * 60 * 1000;
    @FunctionalInterface
    public interface Event{
@@ -38,8 +32,7 @@
    @Async("taskExecutor")
    @EventListener
    public void onApplicationEvent(MediaArrivalEvent event) {
        if ("rtsp".equals(event.getSchema())) {
            System.out.println("流到来的处理: " + allSubscribes.size());
        if (event.getSchema() == null || "rtsp".equals(event.getSchema())) {
            sendNotify(HookType.on_media_arrival, event);
        }
@@ -51,7 +44,7 @@
    @Async("taskExecutor")
    @EventListener
    public void onApplicationEvent(MediaDepartureEvent event) {
        if ("rtsp".equals(event.getSchema())) {
        if (event.getSchema() == null || "rtsp".equals(event.getSchema())) {
            sendNotify(HookType.on_media_departure, event);
        }
@@ -63,6 +56,14 @@
    @EventListener
    public void onApplicationEvent(MediaPublishEvent event) {
        sendNotify(HookType.on_publish, event);
    }
    /**
     * 生成录像文件事件
     */
    @Async("taskExecutor")
    @EventListener
    public void onApplicationEvent(MediaRecordMp4Event event) {
        sendNotify(HookType.on_record_mp4, event);
    }
    private final Map<String, Event> allSubscribes = new ConcurrentHashMap<>();
@@ -78,8 +79,8 @@
    }
    public void addSubscribe(Hook hook, HookSubscribe.Event event) {
        if (hook.getCreateTime() == null) {
            hook.setCreateTime(System.currentTimeMillis());
        if (hook.getExpireTime() == null) {
            hook.setExpireTime(System.currentTimeMillis() + subscribeExpire);
        }
        allSubscribes.put(hook.toString(), event);
        allHook.put(hook.toString(), hook);
@@ -95,9 +96,9 @@
     */
    @Scheduled(fixedRate=subscribeExpire)   //每5分钟执行一次
    public void execute(){
        long expireTime = System.currentTimeMillis() - subscribeExpire;
        long expireTime = System.currentTimeMillis();
        for (Hook hook : allHook.values()) {
            if (hook.getCreateTime() < expireTime) {
            if (hook.getExpireTime() < expireTime) {
                allSubscribes.remove(hook.toString());
                allHook.remove(hook.toString());
            }