ソースを参照

fix(video): 协商SSRC后由token归属真实归还池号

Kazerin 3 週間 前
親
コミット
933317f5e0

+ 8 - 5
video-service/src/main/java/com/ruoyi/wvp/gb28181/service/impl/PlayServiceImpl.java

@@ -1068,12 +1068,15 @@ public class PlayServiceImpl implements IPlayService {
                                 "下级自定义了ssrc,重新设置收流信息失败", null);
 
                     }else {
-                        // The legacy path changes only the device-facing SSRC; the pool number that
-                        // must come back is the one originally allocated for this token.
+                        // The legacy path changes only the device-facing SSRC. Its pool number must be
+                        // returned through the token-aware Factory: an unowned release is refused.
                         if (stopReceipts != null) {
-                            try { stopReceipts.recordNegotiatedSsrc(mediaServerItem.getId(), ssrcInfo.getStream(),
-                                    ssrcInfo, ssrcInResponse, true); }
-                            catch (RuntimeException ex) { log.warn("[Invite 200OK] 协商 SSRC 保存失败: {}", ex.getMessage()); }
+                            try {
+                                String poolSsrc = ssrcInfo.getSsrc(), token = ssrcInfo.getTimeOutTaskKey();
+                                stopReceipts.recordNegotiatedSsrc(mediaServerItem.getId(), ssrcInfo.getStream(),
+                                        ssrcInfo, ssrcInResponse,
+                                        () -> ssrcFactory.releaseSsrc(mediaServerItem.getId(), poolSsrc, token));
+                            } catch (RuntimeException ex) { log.warn("[Invite 200OK] 协商 SSRC 保存失败: {}", ex.getMessage()); }
                         }
                         ssrcInfo.setSsrc(ssrcInResponse);
                         inviteInfo.setSsrcInfo(ssrcInfo);

+ 12 - 4
video-service/src/main/java/com/zksy/video/lifecycle/VideoStopReceipts.java

@@ -89,12 +89,12 @@ public class VideoStopReceipts {
     }
 
     /** Legacy negotiation happens before any stop receipt exists; create the original allocation
-     * from the typed handle and remember that its pool number was already returned by that path. */
+     * from the typed handle. The pool number is returned only if returnPoolSsrc actually succeeds. */
     public boolean recordNegotiatedSsrc(String nodeId, String stream, SSRCInfo original, String negotiatedSsrc,
-                                        boolean poolAlreadyReturned) {
+                                        java.util.function.Supplier<Boolean> returnPoolSsrc) {
         if (nodeId == null || stream == null || original == null || original.getTimeOutTaskKey() == null
                 || original.getSsrc() == null || negotiatedSsrc == null || !"rtp".equals(original.getApp())) return false;
-        return update(nodeId, stream, snapshot -> {
+        Snapshot recorded = update(nodeId, stream, snapshot -> {
             Allocation allocation = snapshot.allocation;
             if (allocation == null) {
                 allocation = new Allocation(); allocation.token = original.getTimeOutTaskKey();
@@ -105,7 +105,15 @@ public class VideoStopReceipts {
                     || !Objects.equals(allocation.ssrc, original.getSsrc())) return null;
             if (allocation.negotiatedSsrc != null && !Objects.equals(allocation.negotiatedSsrc, negotiatedSsrc)) return null;
             allocation.negotiatedSsrc = negotiatedSsrc;
-            allocation.poolReturned = allocation.poolReturned || poolAlreadyReturned;
+            return snapshot;
+        });
+        if (recorded == null || !Boolean.TRUE.equals(returnPoolSsrc.get())) return false;
+        // Persist the positive return receipt only after the Factory confirmed this token returned it.
+        return update(nodeId, stream, snapshot -> {
+            Allocation allocation = snapshot.allocation;
+            if (allocation == null || !Objects.equals(allocation.token, original.getTimeOutTaskKey())
+                    || !Objects.equals(allocation.ssrc, original.getSsrc())) return null;
+            allocation.poolReturned = true;
             return snapshot;
         }) != null;
     }

+ 28 - 29
video-service/src/test/java/com/zksy/video/lifecycle/VideoStopReceiptRestartTest.java

@@ -71,24 +71,6 @@ class VideoStopReceiptRestartTest {
         assertTrue(world.open.contains("other_legacy_stream"));
     }
 
-    @Test void negotiatedSsrcKeepsTheOriginalPoolNumberForCloseAndReturn() throws Exception {
-        External world = new External();
-        Instance instance = new Instance(world);
-        String pool = "CITY_VIDEO_VMP_SSRC_INFO_restart-test_node-a";
-        world.ssrcRedis.strings().delete(pool);
-        world.ssrcRedis.strings().opsForSet().add(pool, "200000001");
-        SSRCInfo original = instance.open(STREAM);
-        // The device renegotiates a different SSRC while the original pool number stays owned.
-        String negotiated = "0999999999";
-        assertTrue(instance.receipts.recordNegotiatedSsrc(world.node.getId(), STREAM, original, negotiated, false));
-        SSRCInfo current = new SSRCInfo(original.getPort(), negotiated, "rtp", STREAM, original.getTimeOutTaskKey());
-        assertTrue(new Instance(world).rtp.closeManagedRTPServer(world.node, current));
-        assertEquals(1L, world.ssrcRedis.strings().opsForSet().size(pool), "the original pool number is returned once");
-        // The next allocation proves which number came back: the original, never the negotiated one.
-        assertEquals(original.getSsrc(), new Instance(world).open("next_stream").getSsrc());
-        assertEquals(0L, world.ssrcRedis.strings().opsForSet().size(pool));
-    }
-
     @Test void delayedLegacyCloseReturnsSsrcWhenTheCallerNeverRetries() throws Exception {
         External world = new External(); world.delayClose = true;
         Instance instance = new Instance(world);
@@ -102,21 +84,36 @@ class VideoStopReceiptRestartTest {
         assertEquals(1, world.closeRequests.size());
     }
 
-    @Test void ssrcRenegotiationKeepsTheOriginalPoolIdentityForCleanup() throws Exception {
+    @Test void negotiatedSsrcReturnsTheOriginalPoolNumberExactlyOnce() throws Exception {
         External world = new External();
         Instance instance = new Instance(world);
         String pool = "CITY_VIDEO_VMP_SSRC_INFO_restart-test_node-a";
         SSRCInfo original = instance.open(LEGACY);
         assertNotNull(original);
-        String negotiated = "9777777777";
-        // Device-selected SSRC: the receipt keeps the pool number but accepts the real current SSRC.
-        assertTrue(instance.receipts.recordNegotiatedSsrc(world.node.getId(), LEGACY, original, negotiated, false));
-        SSRCInfo current = new SSRCInfo(original.getPort(), negotiated, "rtp", LEGACY, original.getTimeOutTaskKey());
-        new Instance(world).rtp.closeRTPServer(world.node, current);
-        assertEquals(1, world.closeRequests.size());
-        // The returned number is the original pool value; a device-negotiated SSRC is never returned.
+        assertEquals(99L, world.ssrcRedis.strings().opsForSet().size(pool), "the pool number is acquired, not available");
+        // A token-owned SN cannot be returned by the unowned legacy release path.
+        instance.ssrcFactory.releaseSsrc(world.node.getId(), original.getSsrc());
+        assertFalse(instance.ssrcFactory.wasReleased(world.node.getId(), original.getSsrc(), original.getTimeOutTaskKey()));
+        assertTrue(instance.receipts.recordNegotiatedSsrc(world.node.getId(), LEGACY, original, "9777777777",
+                () -> instance.ssrcFactory.releaseSsrc(world.node.getId(), original.getSsrc(), original.getTimeOutTaskKey())));
+        assertTrue(instance.ssrcFactory.wasReleased(world.node.getId(), original.getSsrc(), original.getTimeOutTaskKey()));
+        assertEquals(100L, world.ssrcRedis.strings().opsForSet().size(pool), "returned once");
         assertTrue(world.ssrcRedis.strings().opsForSet().isMember(pool, original.getSsrc().substring(1)));
-        assertFalse(world.ssrcRedis.strings().opsForSet().isMember(pool, negotiated.substring(1)));
+        // The negotiated value was never a pool number.
+        assertFalse(world.ssrcRedis.strings().opsForSet().isMember(pool, "9777777777".substring(1)));
+        // Closing afterwards must not return it a second time.
+        new Instance(world).rtp.closeRTPServer(world.node, new SSRCInfo(original.getPort(), "9777777777", "rtp", LEGACY, original.getTimeOutTaskKey()));
+        assertEquals(100L, world.ssrcRedis.strings().opsForSet().size(pool), "a duplicate close never returns it twice");
+    }
+
+    @Test void failedPoolReturnIsNeverRecordedAsReturned() throws Exception {
+        External world = new External();
+        Instance instance = new Instance(world);
+        SSRCInfo original = instance.open(LEGACY);
+        assertFalse(instance.receipts.recordNegotiatedSsrc(world.node.getId(), LEGACY, original, "9666666666", () -> false));
+        VideoStopReceipts.Allocation allocation = instance.receipts.read(world.node.getId(), LEGACY).allocation;
+        assertFalse(allocation.poolReturned, "a refused return must not be claimed as done");
+        assertEquals("9666666666", allocation.negotiatedSsrc);
     }
 
     @Test void staleAllocationTokenCannotAnnotateANewerSameNameAllocation() throws Exception {
@@ -126,7 +123,8 @@ class VideoStopReceiptRestartTest {
         instance.rtp.closeRTPServer(world.node, original);
         SSRCInfo replacement = instance.open(LEGACY);
         assertNotEquals(original.getTimeOutTaskKey(), replacement.getTimeOutTaskKey());
-        assertFalse(instance.receipts.recordNegotiatedSsrc(world.node.getId(), LEGACY, original, "9666666666", false));
+        assertFalse(instance.receipts.recordNegotiatedSsrc(world.node.getId(), LEGACY, original, "9666666666",
+                () -> instance.ssrcFactory.releaseSsrc(world.node.getId(), original.getSsrc(), original.getTimeOutTaskKey())));
         new Instance(world).rtp.closeRTPServer(world.node, replacement);
         assertEquals(2, world.closeRequests.size());
     }
@@ -263,6 +261,7 @@ class VideoStopReceiptRestartTest {
         final SIPProcessorObserver observer = new SIPProcessorObserver();
         final SIPCommander commander = new SIPCommander();
         final External world;
+        final SSRCFactory ssrcFactory;
         final VideoStopReceipts receipts;
         Instance(External world) throws Exception {
             this.world = world;
@@ -271,7 +270,7 @@ class VideoStopReceiptRestartTest {
             StringRedisTemplate strings = world.redis.strings();
             receipts = new VideoStopReceipts(redis);
             RedisVideoLifecycleStore lifecycle = new RedisVideoLifecycleStore(strings);
-            SSRCFactory ssrc = new SSRCFactory(); wire(ssrc, "redisTemplate", world.ssrcRedis.strings()); wire(ssrc, "userSetting", settings);
+            SSRCFactory ssrc = new SSRCFactory(); ssrcFactory = ssrc; wire(ssrcFactory, "redisTemplate", world.ssrcRedis.strings()); wire(ssrcFactory, "userSetting", settings);
             wire(invites, "redisTemplate", redis); wire(invites, "userSetting", settings);
             wire(sessions, "redisTemplate", redis); wire(sessions, "userSetting", settings);
             RedisCatchStorageImpl cache = new RedisCatchStorageImpl();