648540858
2024-04-01 fd2ef6effe7b484c70079a7ac054ac4834b394d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * zlm hook事件的参数
 * @author lin
 */
@Component
public class HookSubscribe {
 
    /**
     * 订阅数据过期时间
     */
    private final long subscribeExpire = 5 * 60 * 1000;
 
    @FunctionalInterface
    public interface Event{
        void response(HookData data);
    }
 
    /**
     * 流到来的处理
     */
    @Async("taskExecutor")
    @EventListener
    public void onApplicationEvent(MediaArrivalEvent event) {
        if ("rtsp".equals(event.getSchema())) {
            sendNotify(HookType.on_media_arrival, event);
        }
 
    }
 
    /**
     * 流结束事件
     */
    @Async("taskExecutor")
    @EventListener
    public void onApplicationEvent(MediaDepartureEvent event) {
        if ("rtsp".equals(event.getSchema())) {
            sendNotify(HookType.on_media_departure, event);
        }
 
    }
    /**
     * 推流鉴权事件
     */
    @Async("taskExecutor")
    @EventListener
    public void onApplicationEvent(MediaPublishEvent event) {
        sendNotify(HookType.on_publish, event);
    }
 
    private final Map<String, Event> allSubscribes = new ConcurrentHashMap<>();
    private final Map<String, Hook> allHook = new ConcurrentHashMap<>();
 
    private void sendNotify(HookType hookType, MediaEvent event) {
        Hook paramHook = Hook.getInstance(hookType, event.getApp(), event.getStream(), event.getMediaServer().getId());
        Event hookSubscribeEvent = allSubscribes.get(paramHook.toString());
        if (hookSubscribeEvent != null) {
            HookData data = HookData.getInstance(event);
            hookSubscribeEvent.response(data);
        }
    }
 
    public void addSubscribe(Hook hook, HookSubscribe.Event event) {
        if (hook.getCreateTime() == null) {
            hook.setCreateTime(System.currentTimeMillis());
        }
        allSubscribes.put(hook.toString(), event);
        allHook.put(hook.toString(), hook);
    }
 
    public void removeSubscribe(Hook hook) {
        allSubscribes.remove(hook.toString());
        allHook.remove(hook.toString());
    }
 
    /**
     * 对订阅数据进行过期清理
     */
    @Scheduled(fixedRate=subscribeExpire)   //每5分钟执行一次
    public void execute(){
        long expireTime = System.currentTimeMillis() - subscribeExpire;
        for (Hook hook : allHook.values()) {
            if (hook.getCreateTime() < expireTime) {
                allSubscribes.remove(hook.toString());
                allHook.remove(hook.toString());
            }
        }
    }
}