Parcourir la source

fix(d2-02): FrameBytes 入参判空 + 测试改按 header 长度定位帧体整段比对(消除脆弱断言)

huangjie il y a 1 jour
Parent
commit
498962a55b

+ 6 - 2
ivf_tl_operate_2.0/control/IvfTl.ControlHost.Tests/MjpegStreamWriterTests.cs

@@ -43,8 +43,12 @@ namespace IvfTl.ControlHost.Tests
             Assert.Contains("--frame", head);
             Assert.Contains("Content-Type: image/jpeg", head);
             Assert.Contains("Content-Length: 6", head);
-            // 帧尾部含 jpeg 原始字节 + 末尾 \r\n
-            Assert.Equal(0xFF, frame[frame.Length - 8]); // jpeg 起点附近(粗校验帧体存在)
+            // 帧体起点 = header 长度;尾部是 \r\n
+            string headerStr = "--frame\r\nContent-Type: image/jpeg\r\nContent-Length: 6\r\n\r\n";
+            int bodyStart = System.Text.Encoding.ASCII.GetByteCount(headerStr);
+            var body = new byte[jpeg.Length];
+            System.Array.Copy(frame, bodyStart, body, 0, jpeg.Length);
+            Assert.Equal(jpeg, body);   // 帧体整段 == 原 jpeg
             Assert.Equal((byte)'\r', frame[frame.Length - 2]);
             Assert.Equal((byte)'\n', frame[frame.Length - 1]);
         }

+ 1 - 0
ivf_tl_operate_2.0/control/ivf_tl_ControlHost/Debug/MjpegStreamWriter.cs

@@ -34,6 +34,7 @@ namespace IvfTl.ControlHost.Debug
         /// <summary>把 JPEG 字节封成一个 multipart/x-mixed-replace 帧(含 boundary 头 + 帧体 + \r\n)。</summary>
         public static byte[] FrameBytes(byte[] jpeg)
         {
+            if (jpeg == null) return null;
             string header = $"--{Boundary}\r\nContent-Type: image/jpeg\r\nContent-Length: {jpeg.Length}\r\n\r\n";
             byte[] head = Encoding.ASCII.GetBytes(header);
             byte[] tail = Encoding.ASCII.GetBytes("\r\n");