ManualLifecycleMedia.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.zksy.video.lifecycle;
  2. import java.net.URI;
  3. import java.util.Locale;
  4. import java.util.Map;
  5. import java.util.Set;
  6. import org.springframework.stereotype.Component;
  7. import com.zksy.video.ManualVideoService;
  8. /** MANUAL 原生地址不创建媒体资源;代理始终绑定代次流名、实际 key 和记录节点。 */
  9. @Component
  10. public class ManualLifecycleMedia implements VideoLifecycleMedia {
  11. private static final Set<String> PROXY_SCHEMES = Set.of("rtsp", "rtsps", "rtmp", "rtmps");
  12. private final ManualVideoService manual;
  13. public ManualLifecycleMedia(ManualVideoService manual) { this.manual = manual; }
  14. @Override public boolean supports(String sourceType) { return "MANUAL".equals(sourceType); }
  15. private static boolean proxy(VideoLifecycleRecord record) {
  16. String url = record.getUrl();
  17. if (url == null || url.isBlank()) throw new IllegalArgumentException("手动视频地址缺失");
  18. URI uri = URI.create(url);
  19. String scheme = String.valueOf(uri.getScheme()).toLowerCase(Locale.ROOT);
  20. if (uri.getHost() == null || !(PROXY_SCHEMES.contains(scheme) || Set.of("http", "https").contains(scheme)))
  21. throw new IllegalArgumentException("不支持的手动视频地址");
  22. return PROXY_SCHEMES.contains(scheme);
  23. }
  24. @Override public String intendedStream(VideoLifecycleRecord record) {
  25. return proxy(record) ? managedStream(record) : null;
  26. }
  27. @Override public void prepare(VideoLifecycleRecord record) {
  28. if (record.isCancelled()) return;
  29. if (!proxy(record)) {
  30. if (record.getStream() != null || record.getProxyKey() != null)
  31. throw new IllegalArgumentException("原生地址不能接管媒体代理");
  32. return;
  33. }
  34. if (record.getStream() == null) {
  35. record.setStream(managedStream(record));
  36. record.setMediaServerId(manual.selectMediaServer());
  37. } else {
  38. // 已有流绝不迁移到默认节点,也不另起一个新流。
  39. manual.recordedServer(record.getMediaServerId());
  40. }
  41. }
  42. @Override public Map<String, Object> start(VideoLifecycleRecord record) {
  43. if (record.isCancelled()) return cancel(record);
  44. if (!proxy(record)) return outcome(record, "ACTIVE", Map.of("native", true, "url", record.getUrl()));
  45. if (record.getStream() == null) return outcome(record, "CLEANUP_PENDING", null);
  46. if (!record.getStream().equals(managedStream(record)) || record.getProxyKey() != null) return status(record);
  47. try {
  48. String key = manual.start(record.getMediaServerId(), record.getVideoId(), record.getUrl(), record.getStream());
  49. record.setProxyKey(key); // 在后续探测可能失败之前保留实际 add 响应。
  50. if (key == null) return outcome(record, "CLEANUP_PENDING", null);
  51. return status(record);
  52. } catch (RuntimeException unknown) {
  53. return outcome(record, "CLEANUP_PENDING", null);
  54. }
  55. }
  56. @Override public Map<String, Object> status(VideoLifecycleRecord record) {
  57. if (record.isCancelled()) return cancel(record);
  58. try {
  59. if (!proxy(record)) return outcome(record, "ACTIVE", Map.of("native", true, "url", record.getUrl()));
  60. Map<String, Object> media = manual.status(record.getMediaServerId(), record.getStream(), record.getUrl());
  61. if (record.getProxyKey() == null || record.getProxyKey().isBlank()) return outcome(record, "CLEANUP_PENDING", null);
  62. if (media == null && !record.getStream().equals(managedStream(record)))
  63. return outcome(record, "CLEANUP_PENDING", null);
  64. if (media != null) media.put("proxyKey", record.getProxyKey());
  65. return outcome(record, media == null ? "STARTING" : "ACTIVE", media);
  66. } catch (RuntimeException unknown) {
  67. return outcome(record, "CLEANUP_PENDING", null);
  68. }
  69. }
  70. @Override public Map<String, Object> cancel(VideoLifecycleRecord record) {
  71. String key = record.getProxyKey();
  72. if (key != null && !key.isBlank()) {
  73. boolean stopped = manual.stop(record.getMediaServerId(), key);
  74. // A persisted actual key is only available after add completed (or on read-only
  75. // adoption). Exact confirmed deletion then fences that allocation despite a lost RPC.
  76. return outcome(record, stopped ? "STOPPED" : "CLEANUP_PENDING", null);
  77. }
  78. // 原生地址确实没有外部资源;代理缺 actual key 永远不能假报已回收。
  79. boolean nativeOnly = false;
  80. try { nativeOnly = !proxy(record) && record.getStream() == null; } catch (RuntimeException unknown) { /* 未知来源 */ }
  81. return outcome(record, nativeOnly ? "STOPPED" : "CLEANUP_PENDING", null);
  82. }
  83. static String managedStream(VideoLifecycleRecord record) {
  84. return "manual_" + java.util.UUID.fromString(record.getGeneration()).toString().replace("-", "");
  85. }
  86. private static Map<String, Object> outcome(VideoLifecycleRecord record, String status, Map<String, Object> media) {
  87. Map<String, Object> result = new java.util.LinkedHashMap<>();
  88. result.put("status", status);
  89. if ("STOPPED".equals(status)) result.put("allocationClosed", true);
  90. record.setMedia(media);
  91. if (media != null) result.put("media", media);
  92. if (record.getStream() != null) result.put("stream", record.getStream());
  93. if (record.getProxyKey() != null) result.put("proxyKey", record.getProxyKey());
  94. if (record.getMediaServerId() != null) result.put("mediaServerId", record.getMediaServerId());
  95. return result;
  96. }
  97. }