| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package com.zksy.video.lifecycle;
- import java.net.URI;
- import java.util.Locale;
- import java.util.Map;
- import java.util.Set;
- import org.springframework.stereotype.Component;
- import com.zksy.video.ManualVideoService;
- /** MANUAL 原生地址不创建媒体资源;代理始终绑定代次流名、实际 key 和记录节点。 */
- @Component
- public class ManualLifecycleMedia implements VideoLifecycleMedia {
- private static final Set<String> PROXY_SCHEMES = Set.of("rtsp", "rtsps", "rtmp", "rtmps");
- private final ManualVideoService manual;
- public ManualLifecycleMedia(ManualVideoService manual) { this.manual = manual; }
- @Override public boolean supports(String sourceType) { return "MANUAL".equals(sourceType); }
- private static boolean proxy(VideoLifecycleRecord record) {
- String url = record.getUrl();
- if (url == null || url.isBlank()) throw new IllegalArgumentException("手动视频地址缺失");
- URI uri = URI.create(url);
- String scheme = String.valueOf(uri.getScheme()).toLowerCase(Locale.ROOT);
- if (uri.getHost() == null || !(PROXY_SCHEMES.contains(scheme) || Set.of("http", "https").contains(scheme)))
- throw new IllegalArgumentException("不支持的手动视频地址");
- return PROXY_SCHEMES.contains(scheme);
- }
- @Override public String intendedStream(VideoLifecycleRecord record) {
- return proxy(record) ? managedStream(record) : null;
- }
- @Override public void prepare(VideoLifecycleRecord record) {
- if (record.isCancelled()) return;
- if (!proxy(record)) {
- if (record.getStream() != null || record.getProxyKey() != null)
- throw new IllegalArgumentException("原生地址不能接管媒体代理");
- return;
- }
- if (record.getStream() == null) {
- record.setStream(managedStream(record));
- record.setMediaServerId(manual.selectMediaServer());
- } else {
- // 已有流绝不迁移到默认节点,也不另起一个新流。
- manual.recordedServer(record.getMediaServerId());
- }
- }
- @Override public Map<String, Object> start(VideoLifecycleRecord record) {
- if (record.isCancelled()) return cancel(record);
- if (!proxy(record)) return outcome(record, "ACTIVE", Map.of("native", true, "url", record.getUrl()));
- if (record.getStream() == null) return outcome(record, "CLEANUP_PENDING", null);
- if (!record.getStream().equals(managedStream(record)) || record.getProxyKey() != null) return status(record);
- try {
- String key = manual.start(record.getMediaServerId(), record.getVideoId(), record.getUrl(), record.getStream());
- record.setProxyKey(key); // 在后续探测可能失败之前保留实际 add 响应。
- if (key == null) return outcome(record, "CLEANUP_PENDING", null);
- return status(record);
- } catch (RuntimeException unknown) {
- return outcome(record, "CLEANUP_PENDING", null);
- }
- }
- @Override public Map<String, Object> status(VideoLifecycleRecord record) {
- if (record.isCancelled()) return cancel(record);
- try {
- if (!proxy(record)) return outcome(record, "ACTIVE", Map.of("native", true, "url", record.getUrl()));
- Map<String, Object> media = manual.status(record.getMediaServerId(), record.getStream(), record.getUrl());
- if (record.getProxyKey() == null || record.getProxyKey().isBlank()) return outcome(record, "CLEANUP_PENDING", null);
- if (media == null && !record.getStream().equals(managedStream(record)))
- return outcome(record, "CLEANUP_PENDING", null);
- if (media != null) media.put("proxyKey", record.getProxyKey());
- return outcome(record, media == null ? "STARTING" : "ACTIVE", media);
- } catch (RuntimeException unknown) {
- return outcome(record, "CLEANUP_PENDING", null);
- }
- }
- @Override public Map<String, Object> cancel(VideoLifecycleRecord record) {
- String key = record.getProxyKey();
- if (key != null && !key.isBlank()) {
- boolean stopped = manual.stop(record.getMediaServerId(), key);
- // A persisted actual key is only available after add completed (or on read-only
- // adoption). Exact confirmed deletion then fences that allocation despite a lost RPC.
- return outcome(record, stopped ? "STOPPED" : "CLEANUP_PENDING", null);
- }
- // 原生地址确实没有外部资源;代理缺 actual key 永远不能假报已回收。
- boolean nativeOnly = false;
- try { nativeOnly = !proxy(record) && record.getStream() == null; } catch (RuntimeException unknown) { /* 未知来源 */ }
- return outcome(record, nativeOnly ? "STOPPED" : "CLEANUP_PENDING", null);
- }
- static String managedStream(VideoLifecycleRecord record) {
- return "manual_" + java.util.UUID.fromString(record.getGeneration()).toString().replace("-", "");
- }
- private static Map<String, Object> outcome(VideoLifecycleRecord record, String status, Map<String, Object> media) {
- Map<String, Object> result = new java.util.LinkedHashMap<>();
- result.put("status", status);
- if ("STOPPED".equals(status)) result.put("allocationClosed", true);
- record.setMedia(media);
- if (media != null) result.put("media", media);
- if (record.getStream() != null) result.put("stream", record.getStream());
- if (record.getProxyKey() != null) result.put("proxyKey", record.getProxyKey());
- if (record.getMediaServerId() != null) result.put("mediaServerId", record.getMediaServerId());
- return result;
- }
- }
|