|
@@ -0,0 +1,138 @@
|
|
|
|
|
+package com.zksy.video.lifecycle;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.LinkedHashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 观看代次协调:意图先落库,媒体调用在后;cancel 是墓碑,永远优先于迟到的 start。
|
|
|
|
|
+ * 本类不复制主服务的观看归属/共享计数,只对“这一个代次的真实媒体句柄”负责。
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+public class VideoLifecycleService {
|
|
|
|
|
+ private static final Set<String> TERMINAL = Set.of("STOPPED", "FAILED");
|
|
|
|
|
+ private static final Set<String> STATES = Set.of("STARTING", "ACTIVE", "CLEANUP_PENDING", "STOPPED", "FAILED");
|
|
|
|
|
+
|
|
|
|
|
+ private final VideoLifecycleStore store;
|
|
|
|
|
+ private final List<VideoLifecycleMedia> adapters;
|
|
|
|
|
+
|
|
|
|
|
+ public VideoLifecycleService(VideoLifecycleStore store, List<VideoLifecycleMedia> adapters) {
|
|
|
|
|
+ this.store = store; this.adapters = adapters;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Map<String, Object> start(Map<String, Object> body) {
|
|
|
|
|
+ VideoLifecycleRecord record = parse(body);
|
|
|
|
|
+ VideoLifecycleRecord stored = store.find(record.getGeneration());
|
|
|
|
|
+ if (stored != null && stored.isCancelled()) return result(stored, "STOPPED", null);
|
|
|
|
|
+ VideoLifecycleMedia media;
|
|
|
|
|
+ try {
|
|
|
|
|
+ media = adapter(record);
|
|
|
|
|
+ } catch (IllegalArgumentException unsupported) {
|
|
|
|
|
+ return settle(stored != null ? stored : record, "FAILED");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (stored == null) {
|
|
|
|
|
+ String intended = media.intendedStream(record);
|
|
|
|
|
+ if (intended != null && record.getStream() == null) record.setStream(intended);
|
|
|
|
|
+ record.setStatus("STARTING");
|
|
|
|
|
+ store.save(record); // 意图先于任何媒体调用落库
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if (TERMINAL.contains(stored.getStatus())) return result(stored, stored.getStatus(), null);
|
|
|
|
|
+ record = stored;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ return merge(record, media.start(record));
|
|
|
|
|
+ } catch (IllegalArgumentException invalid) {
|
|
|
|
|
+ return settle(record, "FAILED"); // 校验失败:确认未创建任何媒体
|
|
|
|
|
+ } catch (RuntimeException unknown) {
|
|
|
|
|
+ return settle(record, "CLEANUP_PENDING"); // 结果未知:既不谎报成功也不谎报已停
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Map<String, Object> status(Map<String, Object> body) {
|
|
|
|
|
+ VideoLifecycleRecord record = store.find(parse(body).getGeneration());
|
|
|
|
|
+ if (record == null) return result(parse(body), "CLEANUP_PENDING", null);
|
|
|
|
|
+ if (TERMINAL.contains(record.getStatus())) return result(record, record.getStatus(), null);
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 已取消的代次只确认回收,不再启动;未取消的代次只查询。
|
|
|
|
|
+ return merge(record, record.isCancelled() ? adapter(record).cancel(record) : adapter(record).status(record));
|
|
|
|
|
+ } catch (RuntimeException unknown) {
|
|
|
|
|
+ return result(record, "CLEANUP_PENDING", null);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Map<String, Object> cancel(Map<String, Object> body) {
|
|
|
|
|
+ VideoLifecycleRecord request = parse(body);
|
|
|
|
|
+ VideoLifecycleRecord record = store.find(request.getGeneration());
|
|
|
|
|
+ if (record == null) {
|
|
|
|
|
+ // 未知代次:留终止墓碑,并如实等待——没有句柄就无法证明已回收。
|
|
|
|
|
+ request.setCancelled(true); request.setStatus("CLEANUP_PENDING"); store.save(request);
|
|
|
|
|
+ if (request.getStream() != null) store.markStreamCancelled(request.getStream());
|
|
|
|
|
+ return result(request, "CLEANUP_PENDING", null);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (TERMINAL.contains(record.getStatus())) return result(record, record.getStatus(), null);
|
|
|
|
|
+ record.setCancelled(true); record.setStatus("CLEANUP_PENDING");
|
|
|
|
|
+ store.save(record); // 墓碑先行,之后任何 start 都不得复活
|
|
|
|
|
+ if (record.getStream() != null) store.markStreamCancelled(record.getStream());
|
|
|
|
|
+ try {
|
|
|
|
|
+ return merge(record, adapter(record).cancel(record));
|
|
|
|
|
+ } catch (RuntimeException unknown) {
|
|
|
|
|
+ return result(record, "CLEANUP_PENDING", null);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Map<String, Object> merge(VideoLifecycleRecord record, Map<String, Object> outcome) {
|
|
|
|
|
+ String state = text(outcome.get("status"));
|
|
|
|
|
+ if (STATE_OK(state)) record.setStatus(state);
|
|
|
|
|
+ if (outcome.get("stream") instanceof String) record.setStream((String) outcome.get("stream"));
|
|
|
|
|
+ if (outcome.get("proxyKey") instanceof String) record.setProxyKey((String) outcome.get("proxyKey"));
|
|
|
|
|
+ if (record.isCancelled() && !TERMINAL.contains(record.getStatus())) record.setStatus("CLEANUP_PENDING");
|
|
|
|
|
+ store.save(record);
|
|
|
|
|
+ return result(record, record.getStatus(), outcome.get("media"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Map<String, Object> settle(VideoLifecycleRecord record, String state) {
|
|
|
|
|
+ record.setStatus(state); store.save(record); return result(record, state, null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private VideoLifecycleMedia adapter(VideoLifecycleRecord record) {
|
|
|
|
|
+ return adapters.stream().filter(item -> item.supports(record.getSourceType())).findFirst()
|
|
|
|
|
+ .orElseThrow(() -> new IllegalArgumentException("不支持的视频来源类型"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean STATE_OK(String state) { return state != null && STATES.contains(state); }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ private static VideoLifecycleRecord parse(Map<String, Object> body) {
|
|
|
|
|
+ if (body == null) throw new IllegalArgumentException("请求体缺失");
|
|
|
|
|
+ VideoLifecycleRecord record = new VideoLifecycleRecord();
|
|
|
|
|
+ record.setGeneration(text(body.get("generation")));
|
|
|
|
|
+ if (record.getGeneration() == null || !record.getGeneration().matches("[A-Za-z0-9_-]{1,64}"))
|
|
|
|
|
+ throw new IllegalArgumentException("观看代次标识无效");
|
|
|
|
|
+ if (!(body.get("source") instanceof Map)) throw new IllegalArgumentException("视频来源缺失");
|
|
|
|
|
+ Map<String, Object> source = (Map<String, Object>) body.get("source");
|
|
|
|
|
+ record.setSourceType(text(source.get("type")));
|
|
|
|
|
+ record.setVideoId(text(source.get("videoId")));
|
|
|
|
|
+ record.setDeviceId(text(source.get("deviceId")));
|
|
|
|
|
+ record.setChannelId(text(source.get("channelId")));
|
|
|
|
|
+ record.setUrl(text(source.get("url")));
|
|
|
|
|
+ if (body.get("startDeadline") instanceof Number) record.setDeadline(((Number) body.get("startDeadline")).longValue());
|
|
|
|
|
+ if (body.get("existingMedia") instanceof Map) {
|
|
|
|
|
+ Map<String, Object> existing = (Map<String, Object>) body.get("existingMedia");
|
|
|
|
|
+ record.setStream(text(existing.get("stream")));
|
|
|
|
|
+ record.setProxyKey(text(existing.get("proxyKey")));
|
|
|
|
|
+ }
|
|
|
|
|
+ return record;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static String text(Object value) { return value instanceof String && !((String) value).isBlank() ? (String) value : null; }
|
|
|
|
|
+
|
|
|
|
|
+ private static Map<String, Object> result(VideoLifecycleRecord record, String status, Object media) {
|
|
|
|
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
|
|
+ result.put("generation", record.getGeneration());
|
|
|
|
|
+ result.put("status", status);
|
|
|
|
|
+ if (media != null) result.put("media", media);
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|