From 7ed84bed7f3383507b8a954cb947920b2da0f84a Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期二, 09 四月 2024 20:11:10 +0800 Subject: [PATCH] Merge pull request #1411 from 648540858/dev/zlm --- src/main/java/com/genersoft/iot/vmp/media/event/hook/HookSubscribe.java | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 107 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/media/event/hook/HookSubscribe.java b/src/main/java/com/genersoft/iot/vmp/media/event/hook/HookSubscribe.java new file mode 100755 index 0000000..907e904 --- /dev/null +++ b/src/main/java/com/genersoft/iot/vmp/media/event/hook/HookSubscribe.java @@ -0,0 +1,107 @@ +package com.genersoft.iot.vmp.media.event.hook; + +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.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 (event.getSchema() == null || "rtsp".equals(event.getSchema())) { + sendNotify(HookType.on_media_arrival, event); + } + + } + + /** + * 娴佺粨鏉熶簨浠� + */ + @Async("taskExecutor") + @EventListener + public void onApplicationEvent(MediaDepartureEvent event) { + if (event.getSchema() == null || "rtsp".equals(event.getSchema())) { + sendNotify(HookType.on_media_departure, event); + } + + } + /** + * 鎺ㄦ祦閴存潈浜嬩欢 + */ + @Async("taskExecutor") + @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<>(); + 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()); + } + } + } +} -- Gitblit v1.8.0