From faad6b941d840167c30fd28efa019d8d81843386 Mon Sep 17 00:00:00 2001
From: Dana <Dana_Lee1016@126.com>
Date: 星期三, 24 十二月 2025 14:42:21 +0800
Subject: [PATCH] 1.日志最多打印5次(即使后续继续失败也不再打印)   2.连续5次 processCamera 返回 -1 时,自动终止编码推流和视频文件写入   3.成功获取帧数据后,计数器会重置,允许后续重新计数   4.终止编码线程  // 停止编码                                    isRunning.set(false);

---
 app/src/main/java/com/anyun/h264/model/WatermarkInfo.java |    1 +
 如何检查test.h264文件.md                                        |    1 +
 check_h264.py                                             |    1 +
 app/src/main/java/com/anyun/h264/model/ResourceInfo.java  |    1 +
 app/src/main/aidl/com/anyun/h264/model/ResourceInfo.aidl  |    1 +
 app/src/main/java/com/anyun/h264/H264Encoder.java         |   38 +++++++++++++++++++++++++++++++++++++-
 多进程方案使用说明.md                                              |    1 +
 README_H264_CHECK.md                                      |    1 +
 8 files changed, 44 insertions(+), 1 deletions(-)

diff --git a/README_H264_CHECK.md b/README_H264_CHECK.md
index c60a8ee..bb0553e 100644
--- a/README_H264_CHECK.md
+++ b/README_H264_CHECK.md
@@ -120,3 +120,4 @@
 
 
 
+
diff --git a/app/src/main/aidl/com/anyun/h264/model/ResourceInfo.aidl b/app/src/main/aidl/com/anyun/h264/model/ResourceInfo.aidl
index 5e54c23..fd778bb 100644
--- a/app/src/main/aidl/com/anyun/h264/model/ResourceInfo.aidl
+++ b/app/src/main/aidl/com/anyun/h264/model/ResourceInfo.aidl
@@ -7,3 +7,4 @@
 
 
 
+
diff --git a/app/src/main/java/com/anyun/h264/H264Encoder.java b/app/src/main/java/com/anyun/h264/H264Encoder.java
index 02f5bfe..c5b9bae 100644
--- a/app/src/main/java/com/anyun/h264/H264Encoder.java
+++ b/app/src/main/java/com/anyun/h264/H264Encoder.java
@@ -96,6 +96,11 @@
     private byte[] spsBuffer = null; // SPS 缂撳瓨
     private byte[] ppsBuffer = null; // PPS 缂撳瓨
 
+    // processCamera 澶辫触璁℃暟
+    private int consecutiveFailureCount = 0; // 杩炵画澶辫触娆℃暟
+    private int logPrintCount = 0; // 鏃ュ織鎵撳嵃娆℃暟锛堟渶澶�5娆★級
+    private static final int MAX_CONSECUTIVE_FAILURES = 5; // 鏈�澶ц繛缁け璐ユ鏁�
+
     // 缂栫爜鍥炶皟
     public interface OnFrameEncodedCallback {
         void onFrameEncoded(byte[] data, boolean isKeyFrame);
@@ -623,16 +628,47 @@
 
         MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
 
+        // 閲嶇疆澶辫触璁℃暟鍣�
+        consecutiveFailureCount = 0;
+        logPrintCount = 0;
+
         while (isRunning.get()) {
             try {
                 // processCamera - 璇诲彇涓�甯�
                 int processResult = usbCamera.processCamera();
                 if (processResult != 0) {
-                    Timber.w("processCamera returned: " + processResult);
+                    // 澧炲姞杩炵画澶辫触璁℃暟
+                    consecutiveFailureCount++;
+                    
+                    // 闄愬埗鏃ュ織鎵撳嵃娆℃暟锛堟渶澶�5娆★級
+                    if (logPrintCount < MAX_CONSECUTIVE_FAILURES) {
+                        Timber.w("processCamera returned: " + processResult);
+                        logPrintCount++;
+                    }
+                    
+                    // 濡傛灉杩炵画5娆″け璐ワ紝缁堟缂栫爜
+                    if (consecutiveFailureCount >= MAX_CONSECUTIVE_FAILURES) {
+                        Timber.e("processCamera failed %d times consecutively, stopping encoding", MAX_CONSECUTIVE_FAILURES);
+                        // 鍋滄缂栫爜
+                        isRunning.set(false);
+                        // 鍏抽棴鏂囦欢杈撳嚭
+                        closeFileOutput();
+                        // 绂佺敤鏂囦欢杈撳嚭鍜岀綉缁滀紶杈�
+                        enableFileOutput = false;
+                        if (enableNetworkTransmission && protocolHelper != null) {
+                            protocolHelper.closeSocket();
+                        }
+                        break;
+                    }
+                    
                     Thread.sleep(10);
                     continue;
                 }
 
+                // 鎴愬姛鑾峰彇甯ф暟鎹紝閲嶇疆澶辫触璁℃暟鍣�
+                consecutiveFailureCount = 0;
+                logPrintCount = 0; // 閲嶇疆鏃ュ織璁℃暟锛屽厑璁镐笅娆″け璐ユ椂閲嶆柊鎵撳嵃
+
                 // 鑾峰彇RGBA鏁版嵁 (type=1 琛ㄧず鎺ㄦ祦锛岃緭鍑篩UV420P鏍煎紡)
                 usbCamera.rgba(0, yuvBuffer);
 
diff --git a/app/src/main/java/com/anyun/h264/model/ResourceInfo.java b/app/src/main/java/com/anyun/h264/model/ResourceInfo.java
index 0deadda..19888f2 100644
--- a/app/src/main/java/com/anyun/h264/model/ResourceInfo.java
+++ b/app/src/main/java/com/anyun/h264/model/ResourceInfo.java
@@ -158,3 +158,4 @@
 
 
 
+
diff --git a/app/src/main/java/com/anyun/h264/model/WatermarkInfo.java b/app/src/main/java/com/anyun/h264/model/WatermarkInfo.java
index 74901c8..5557794 100644
--- a/app/src/main/java/com/anyun/h264/model/WatermarkInfo.java
+++ b/app/src/main/java/com/anyun/h264/model/WatermarkInfo.java
@@ -140,3 +140,4 @@
 
 
 
+
diff --git a/check_h264.py b/check_h264.py
index cb08c37..923d50f 100644
--- a/check_h264.py
+++ b/check_h264.py
@@ -210,3 +210,4 @@
 
 
 
+
diff --git "a/\345\244\232\350\277\233\347\250\213\346\226\271\346\241\210\344\275\277\347\224\250\350\257\264\346\230\216.md" "b/\345\244\232\350\277\233\347\250\213\346\226\271\346\241\210\344\275\277\347\224\250\350\257\264\346\230\216.md"
index 28e875c..428253c 100644
--- "a/\345\244\232\350\277\233\347\250\213\346\226\271\346\241\210\344\275\277\347\224\250\350\257\264\346\230\216.md"
+++ "b/\345\244\232\350\277\233\347\250\213\346\226\271\346\241\210\344\275\277\347\224\250\350\257\264\346\230\216.md"
@@ -118,3 +118,4 @@
 
 
 
+
diff --git "a/\345\246\202\344\275\225\346\243\200\346\237\245test.h264\346\226\207\344\273\266.md" "b/\345\246\202\344\275\225\346\243\200\346\237\245test.h264\346\226\207\344\273\266.md"
index 91a4510..2c49e34 100644
--- "a/\345\246\202\344\275\225\346\243\200\346\237\245test.h264\346\226\207\344\273\266.md"
+++ "b/\345\246\202\344\275\225\346\243\200\346\237\245test.h264\346\226\207\344\273\266.md"
@@ -184,3 +184,4 @@
 
 
 
+

--
Gitblit v1.8.0