Просмотр исходного кода

feat(video): 驾驶舱回放控制端点与归属校验

Kazerin 3 недель назад
Родитель
Сommit
a65865fc35

+ 11 - 0
pipe-network-service/zksy-admin/src/main/java/com/zksy/web/controller/cockpit/CockpitVideoController.java

@@ -82,6 +82,17 @@ public class CockpitVideoController {
         return success(videos.stop(sessionId(id), field(body, "sessionToken")), request);
         return success(videos.stop(sessionId(id), field(body, "sessionToken")), request);
     }
     }
 
 
+    /** Controls this viewer's own playback session; values are bounded by the reused seam. */
+    @PostMapping("/sessions/{id}/control")
+    public ResponseEntity<AjaxResult> control(@PathVariable("id") String id,
+            @RequestBody Map<String, Object> body, HttpServletRequest request) {
+        if (body == null || !(body.get("sessionToken") instanceof String) || !(body.get("action") instanceof String))
+            throw new CockpitVideoException(400, "INVALID_REQUEST", "视频请求参数无效");
+        Double value = body.get("value") instanceof Number ? ((Number) body.get("value")).doubleValue() : null;
+        return success(videos.control(sessionId(id), (String) body.get("sessionToken"),
+                (String) body.get("action"), value), request);
+    }
+
     private static String sessionId(String id) {
     private static String sessionId(String id) {
         if (id == null || !id.matches("[A-Za-z0-9_-]{1,64}"))
         if (id == null || !id.matches("[A-Za-z0-9_-]{1,64}"))
             throw new CockpitVideoException(400, "INVALID_REQUEST", "播放会话标识无效");
             throw new CockpitVideoException(400, "INVALID_REQUEST", "播放会话标识无效");

+ 26 - 1
pipe-network-service/zksy-admin/src/test/java/com/zksy/web/controller/cockpit/CockpitVideoControllerTest.java

@@ -171,6 +171,31 @@ class CockpitVideoControllerTest {
                 .andExpect(jsonPath("$.data.records").isEmpty());
                 .andExpect(jsonPath("$.data.records").isEmpty());
     }
     }
 
 
+    @Test
+    void playbackControlReachesTheOwnedSessionAndRejectsInvalidRates() throws Exception {
+        when(collector.post(eq("/api/playback/start"), any())).thenReturn(Map.of());
+        when(collector.get(eq("/api/playback/start"), any())).thenReturn(Map.of("stream", "stream_pb", "native", true,
+                "flv", "https://media.invalid/pb.flv"));
+        JSONObject owned = JSON.parseObject(mvc.perform(post(ROOT + "/playback").contentType(MediaType.APPLICATION_JSON)
+                        .content(JSON.toJSONString(Map.of("deviceCode", CODE,
+                                "startTime", "2026-11-10 01:00:00", "endTime", "2026-11-10 02:00:00"))))
+                .andExpect(status().isOk()).andReturn().getResponse().getContentAsString()).getJSONObject("data");
+        String token = owned.getString("sessionToken");
+        String id = owned.getString("sessionId");
+        // A confirmed capability is forwarded to the reused media seam.
+        mvc.perform(post(ROOT + "/sessions/" + id + "/control").contentType(MediaType.APPLICATION_JSON)
+                        .content(JSON.toJSONString(Map.of("sessionToken", token, "action", "pause"))))
+                .andExpect(status().isOk());
+        // An unsupported rate is refused before any media call.
+        mvc.perform(post(ROOT + "/sessions/" + id + "/control").contentType(MediaType.APPLICATION_JSON)
+                        .content(JSON.toJSONString(Map.of("sessionToken", token, "action", "speed", "value", 3))))
+                .andExpect(status().isServiceUnavailable());
+        // Another viewer's token cannot control this session.
+        mvc.perform(post(ROOT + "/sessions/" + id + "/control").contentType(MediaType.APPLICATION_JSON)
+                        .content(JSON.toJSONString(Map.of("sessionToken", "A".repeat(43), "action", "pause"))))
+                .andExpect(status().isForbidden());
+    }
+
     @Test
     @Test
     void aFailedRecordingQueryIsItsOwnCategoryNotAGenericVideoRead() throws Exception {
     void aFailedRecordingQueryIsItsOwnCategoryNotAGenericVideoRead() throws Exception {
         when(collector.get(RECORD_QUERY, Map.of("deviceId", GB, "channelId", CHANNEL,
         when(collector.get(RECORD_QUERY, Map.of("deviceId", GB, "channelId", CHANNEL,
@@ -280,7 +305,7 @@ class CockpitVideoControllerTest {
     @ParameterizedTest
     @ParameterizedTest
     @CsvSource({"GET,/live", "GET,/sessions/example/stop", "GET,/sessions/example/heartbeat",
     @CsvSource({"GET,/live", "GET,/sessions/example/stop", "GET,/sessions/example/heartbeat",
             "GET,/devices/remove", "GET,/record", "POST,/device", "PUT,/live", "DELETE,/live",
             "GET,/devices/remove", "GET,/record", "POST,/device", "PUT,/live", "DELETE,/live",
-            "POST,/sessions/example/control", "POST,/ptz", "POST,/playback/control", "POST,/devices",
+            "POST,/sessions/example/playback", "POST,/ptz", "POST,/playback/control", "POST,/devices",
             "POST,/sessions/example/nested/stop", "POST,/live/nested"})
             "POST,/sessions/example/nested/stop", "POST,/live/nested"})
     void unpublishedVideoMethodAndPathPairsRequireAuthentication(String method, String path) throws Exception {
     void unpublishedVideoMethodAndPathPairsRequireAuthentication(String method, String path) throws Exception {
         mvc.perform(request(org.springframework.http.HttpMethod.valueOf(method), ROOT + path))
         mvc.perform(request(org.springframework.http.HttpMethod.valueOf(method), ROOT + path))

+ 1 - 0
pipe-network-service/zksy-framework/src/main/java/com/zksy/framework/config/SecurityConfig.java

@@ -131,6 +131,7 @@ public class SecurityConfig
                         "/api/cockpit/v1/video/sessions/*/live",
                         "/api/cockpit/v1/video/sessions/*/live",
                         "/api/cockpit/v1/video/sessions/*/status",
                         "/api/cockpit/v1/video/sessions/*/status",
                         "/api/cockpit/v1/video/sessions/*/heartbeat",
                         "/api/cockpit/v1/video/sessions/*/heartbeat",
+                        "/api/cockpit/v1/video/sessions/*/control",
                         "/api/cockpit/v1/video/sessions/*/stop").permitAll()
                         "/api/cockpit/v1/video/sessions/*/stop").permitAll()
                     .antMatchers("/api/cockpit/v1/video/**").authenticated();
                     .antMatchers("/api/cockpit/v1/video/**").authenticated();
                 permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
                 permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());

+ 13 - 0
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/video/CockpitVideoService.java

@@ -222,6 +222,19 @@ public class CockpitVideoService {
         }
         }
     }
     }
 
 
+    /** Applies one playback control to this viewer's own session; the reused seam enforces value bounds. */
+    public Map<String, Object> control(String id, String token, String action, Double value) {
+        VideoRequests.Control input = new VideoRequests.Control();
+        input.setAction(action);
+        input.setValue(value);
+        try {
+            videos.control(id, token, input);
+        } catch (RuntimeException failure) {
+            throw sessionError(failure, false);
+        }
+        return sessionState(id, "ACTIVE");
+    }
+
     private Map<String, Object> preparedResult(Map<String, Object> raw, String id, String token) {
     private Map<String, Object> preparedResult(Map<String, Object> raw, String id, String token) {
         Map<String, Object> data = new LinkedHashMap<>();
         Map<String, Object> data = new LinkedHashMap<>();
         for (String field : List.of("sessionId", "status", "mode")) data.put(field, raw.get(field));
         for (String field : List.of("sessionId", "status", "mode")) data.put(field, raw.get(field));