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

fix(video): peer终止证据写失败仍回收媒体

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

+ 5 - 0
video-service/src/main/java/com/ruoyi/wvp/gb28181/session/SipInviteSessionManager.java

@@ -350,6 +350,11 @@ public class SipInviteSessionManager {
                 && java.util.Objects.equals(binding.getToTag(), owned.getSipTransactionInfo().getFromTag());
                 && java.util.Objects.equals(binding.getToTag(), owned.getSipTransactionInfo().getFromTag());
     }
     }
 
 
+    /** Whether this real Request belongs to the same dialog, independent of receipt persistence. */
+    public boolean matchesPeerByeRequest(javax.sip.message.Request request, SsrcTransaction owned) {
+        return matchesPeerBye(SipRequestBinding.from(request, javax.sip.message.Request.BYE), owned);
+    }
+
     private static boolean matchedPeerBye(SsrcTransaction expected, Object raw) {
     private static boolean matchedPeerBye(SsrcTransaction expected, Object raw) {
         com.alibaba.fastjson2.JSONObject snapshot = com.alibaba.fastjson2.JSON.parseObject((String) raw);
         com.alibaba.fastjson2.JSONObject snapshot = com.alibaba.fastjson2.JSON.parseObject((String) raw);
         return sameDialog(expected, snapshot.getObject("dialog", SsrcTransaction.class))
         return sameDialog(expected, snapshot.getObject("dialog", SsrcTransaction.class))

+ 8 - 1
video-service/src/main/java/com/ruoyi/wvp/gb28181/transmit/event/request/impl/ByeRequestProcessor.java

@@ -90,10 +90,17 @@ public class ByeRequestProcessor extends SIPRequestProcessorParent implements In
             return;
             return;
         }
         }
         // The request must match the pre-reply snapshot and both indexes at the durable write.
         // The request must match the pre-reply snapshot and both indexes at the durable write.
-        if (!sessionManager.rememberPeerBye(request, transaction)) return;
+        // A persistence failure must not skip media cleanup for a genuinely matching dialog; it
+        // only means the SIP termination stays unconfirmed, so the Invite/dialog is retained.
+        boolean recorded;
+        try { recorded = sessionManager.rememberPeerBye(request, transaction); }
+        catch (RuntimeException unavailable) { recorded = false; }
+        if (!recorded && !sessionManager.matchesPeerByeRequest(request, transaction)) return;
         // Freeze original evidence before any Invite/dialog removal; unknown close stays retryable.
         // Freeze original evidence before any Invite/dialog removal; unknown close stays retryable.
         InviteInfo info = inviteStreamService.getInviteInfo(transaction.getType(), transaction.getChannelId(), transaction.getStream());
         InviteInfo info = inviteStreamService.getInviteInfo(transaction.getType(), transaction.getChannelId(), transaction.getStream());
         if (!rtpServerService.closeRTPServerForSession(transaction, info)) return;
         if (!rtpServerService.closeRTPServerForSession(transaction, info)) return;
+        // Media is reclaimed; without durable peer evidence this is not a confirmed SIP termination.
+        if (!recorded) return;
         if (info != null && !inviteStreamService.removeInviteInfoIfSame(info)) return;
         if (info != null && !inviteStreamService.removeInviteInfoIfSame(info)) return;
         if (!sessionManager.removeIfSame(transaction)) return;
         if (!sessionManager.removeIfSame(transaction)) return;
         if (transaction.getType() == InviteSessionType.PLAY) deviceChannelService.stopPlay(transaction.getChannelId());
         if (transaction.getType() == InviteSessionType.PLAY) deviceChannelService.stopPlay(transaction.getChannelId());

+ 13 - 0
video-service/src/test/java/com/zksy/video/lifecycle/LegacyRtpLifecycleCompatibilityTest.java

@@ -368,6 +368,19 @@ class LegacyRtpLifecycleCompatibilityTest {
         return media;
         return media;
     }
     }
 
 
+    @Test void peerByeStillClosesOriginalRtpWhenReceiptPersistenceFails() throws Exception {
+        VideoStopReceiptRestartTest.External world = new VideoStopReceiptRestartTest.External();
+        VideoStopReceiptRestartTest.Instance instance = new VideoStopReceiptRestartTest.Instance(world);
+        instance.legacy("call-old");
+        InviteInfo running = instance.invites.getInviteInfo(InviteSessionType.PLAY, 7, LEGACY);
+        StreamInfo stream = new StreamInfo(); stream.setMediaServer(world.node);
+        running.setStreamInfo(stream); running.setStatus(InviteSessionStatus.ok); instance.invites.updateInviteInfo(running);
+        // The peer marker is the only write that fails; media evidence stays writable.
+        world.redis.failWrite = value -> value.contains("peer-bye");
+        peerProcessor(world, instance).process(peerBye("call-old"));
+        assertEquals(List.of(LEGACY), world.closeRequests, "matching peer BYE must still reclaim the original RTP");
+    }
+
     private static ByeRequestProcessor peerProcessor(VideoStopReceiptRestartTest.External world,
     private static ByeRequestProcessor peerProcessor(VideoStopReceiptRestartTest.External world,
                                                        VideoStopReceiptRestartTest.Instance instance) {
                                                        VideoStopReceiptRestartTest.Instance instance) {
         ByeRequestProcessor bye = new ByeRequestProcessor();
         ByeRequestProcessor bye = new ByeRequestProcessor();