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 表示推流,输出YUV420P格式)
                usbCamera.rgba(0, yuvBuffer);