From 3d3db1ddbf227749c2de72856669171a9345937e Mon Sep 17 00:00:00 2001
From: Dana <Dana_Lee1016@126.com>
Date: 星期一, 01 十二月 2025 11:32:00 +0800
Subject: [PATCH] 1.h264Encoder加上 日志写入文件 2.返回文件列表修改

---
 app/src/main/java/com/anyun/h264/H264Encoder.java |   65 ++++++++++++++++----------------
 1 files changed, 33 insertions(+), 32 deletions(-)

diff --git a/app/src/main/java/com/anyun/h264/H264Encoder.java b/app/src/main/java/com/anyun/h264/H264Encoder.java
index b65ac15..80a992a 100644
--- a/app/src/main/java/com/anyun/h264/H264Encoder.java
+++ b/app/src/main/java/com/anyun/h264/H264Encoder.java
@@ -3,7 +3,6 @@
 import android.media.MediaCodec;
 import android.media.MediaCodecInfo;
 import android.media.MediaFormat;
-import android.util.Log;
 import com.anyun.libusbcamera.UsbCamera;
 
 import java.io.File;
@@ -12,6 +11,8 @@
 import java.nio.ByteBuffer;
 import java.util.Arrays;
 import java.util.concurrent.atomic.AtomicBoolean;
+
+import timber.log.Timber;
 
 /**
  * H264瑙嗛缂栫爜鍣�
@@ -143,7 +144,7 @@
      */
     public void setEnableNetworkTransmission(boolean enable) {
         this.enableNetworkTransmission = enable;
-        Log.d(TAG, "Network transmission " + (enable ? "enabled" : "disabled"));
+        Timber.d("Network transmission " + (enable ? "enabled" : "disabled"));
     }
 
     /**
@@ -167,27 +168,27 @@
                 if (result == 0) {
                     // 鎴愬姛锛岃烦鍑哄惊鐜�
                     if (attempt > 0) {
-                        Log.d(TAG, "prepareCamera succeeded on attempt " + (attempt + 1));
+                        Timber.d("prepareCamera succeeded on attempt " + (attempt + 1));
                     }
                     break;
                 } else {
                     // 澶辫触锛岃褰曟棩蹇�
-                    Log.w(TAG, "prepareCamera failed on attempt " + (attempt + 1) + ": " + result);
+                    Timber.w( "prepareCamera failed on attempt " + (attempt + 1) + ": " + result);
                     if (attempt < maxRetries - 1) {
-                        Log.d(TAG, "Retrying prepareCamera...");
+                        Timber.d( "Retrying prepareCamera...");
                     }
                 }
             }
 
             if (result != 0) {
-                Log.e(TAG, "prepareCamera failed after " + maxRetries + " attempts: " + result);
+                Timber.e("prepareCamera failed after " + maxRetries + " attempts: " + result);
                 return false;
             }
 
             // 鏇存柊瀹為檯鍒嗚鲸鐜�
             width = actualResolution[0];
             height = actualResolution[1];
-            Log.d(TAG, "Camera initialized with resolution: " + width + "x" + height);
+            Timber.d("Camera initialized with resolution: " + width + "x" + height);
 
             // 3. 鍒濆鍖朒264缂栫爜鍣�
             initEncoder();
@@ -199,19 +200,19 @@
                     return false;
                 }
             } else {
-                Log.d(TAG, "Network transmission disabled, skipping socket initialization");
+                Timber.d("Network transmission disabled, skipping socket initialization");
             }
 
             // 5. 鍒濆鍖栨枃浠惰緭鍑猴紙浠呭垱寤烘枃浠讹紝SPS/PPS鍦ㄧ涓�娆¤緭鍑烘椂鍐欏叆锛�
             if (enableFileOutput && outputFilePath != null && !outputFilePath.isEmpty()) {
                 if (!initFileOutput()) {
-                    Log.w(TAG, "File output initialization failed, continuing without file output");
+                    Timber.w("File output initialization failed, continuing without file output");
                 }
             }
 
             return true;
         } catch (Exception e) {
-            Log.e(TAG, "Initialize failed", e);
+            Timber.e(e,"Initialize failed");
             return false;
         }
     }
@@ -230,7 +231,7 @@
         encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
         encoder.start();
 
-        Log.d(TAG, "H264 encoder initialized");
+        Timber.d( "H264 encoder initialized");
     }
 
     /**
@@ -244,7 +245,7 @@
             if (parentDir != null && !parentDir.exists()) {
                 boolean created = parentDir.mkdirs();
                 if (!created && !parentDir.exists()) {
-                    Log.e(TAG, "Failed to create parent directory: " + parentDir.getAbsolutePath());
+                    Timber.e("Failed to create parent directory: " + parentDir.getAbsolutePath());
                     return false;
                 }
             }
@@ -252,15 +253,15 @@
             fileOutputStream = new FileOutputStream(file);
             spsPpsWritten = false;
 
-            Log.d(TAG, "File output initialized: " + outputFilePath);
+            Timber.d("File output initialized: " + outputFilePath);
             return true;
         } catch (Exception e) {
-            Log.e(TAG, "Initialize file output failed", e);
+            Timber.e(e,"Initialize file output failed");
             if (fileOutputStream != null) {
                 try {
                     fileOutputStream.close();
                 } catch (IOException ie) {
-                    Log.e(TAG, "Close file output stream failed", ie);
+                    Timber.e(ie, "Close file output stream failed");
                 }
                 fileOutputStream = null;
             }
@@ -321,12 +322,12 @@
                 fileOutputStream.flush();
 
                 spsPpsWritten = true;
-                Log.d(TAG, "SPS/PPS written to file, SPS size: " + spsLength + ", PPS size: " + ppsLength);
+                Timber.d("SPS/PPS written to file, SPS size: " + spsLength + ", PPS size: " + ppsLength);
             } else {
-                Log.w(TAG, "SPS/PPS not found in CSD, will extract from first key frame");
+                Timber.w("SPS/PPS not found in CSD, will extract from first key frame");
             }
         } catch (Exception e) {
-            Log.e(TAG, "Write SPS/PPS to file error", e);
+            Timber.e(e,"Write SPS/PPS to file error");
         }
     }
 
@@ -335,7 +336,7 @@
      */
     public void start() {
         if (isRunning.get()) {
-            Log.w(TAG, "Encoder is already running");
+            Timber.w("Encoder is already running");
             return;
         }
 
@@ -350,7 +351,7 @@
         });
         encodeThread.start();
 
-        Log.d(TAG, "H264 encoder started");
+        Timber.d("H264 encoder started");
     }
 
     /**
@@ -371,7 +372,7 @@
                 // processCamera - 璇诲彇涓�甯�
                 int processResult = usbCamera.processCamera();
                 if (processResult != 0) {
-                    Log.w(TAG, "processCamera returned: " + processResult);
+                    Timber.w("processCamera returned: " + processResult);
                     Thread.sleep(10);
                     continue;
                 }
@@ -384,7 +385,7 @@
                 encodeFrame(nv12ToNV21(yuvBuffer,width,height), timestamp, bufferInfo);
 
             } catch (Exception e) {
-                Log.e(TAG, "Encode loop error", e);
+                Timber.e(e, "Encode loop error");
                 try {
                     Thread.sleep(100);
                 } catch (InterruptedException ie) {
@@ -393,7 +394,7 @@
             }
         }
 
-        Log.d(TAG, "Encode loop exited");
+        Timber.d( "Encode loop exited");
     }
 
     /**
@@ -441,7 +442,7 @@
             }
 
         } catch (Exception e) {
-            Log.e(TAG, "Encode frame error", e);
+            Timber.e(e,"Encode frame error");
         }
     }
 
@@ -462,7 +463,7 @@
                 // MediaCodec杈撳嚭鐨勫叧閿抚閫氬父宸茬粡鍖呭惈SPS/PPS锛屼絾涓轰簡纭繚鏂囦欢瀹屾暣鎬э紝
                 // 鎴戜滑宸茬粡浠嶤SD鍐欏叆锛岃繖閲岀洿鎺ュ啓鍏ュ叧閿抚鏁版嵁鍗冲彲
                 if (!spsPpsWritten) {
-                    Log.d(TAG, "SPS/PPS will be included in key frame data");
+                    Timber.d("SPS/PPS will be included in key frame data");
                 }
             }
 
@@ -471,7 +472,7 @@
             fileOutputStream.write(data);
             fileOutputStream.flush();
         } catch (IOException e) {
-            Log.e(TAG, "Write to file error", e);
+            Timber.e(e, "Write to file error");
         }
     }
 
@@ -531,7 +532,7 @@
             }
 
         } catch (Exception e) {
-            Log.e(TAG, "Send encoded data error", e);
+            Timber.e(e,"Send encoded data error");
         }
     }
 
@@ -550,7 +551,7 @@
             try {
                 encodeThread.join(2000);
             } catch (InterruptedException e) {
-                Log.e(TAG, "Wait encode thread error", e);
+                Timber.e(e, "Wait encode thread error");
             }
         }
 
@@ -566,7 +567,7 @@
                 encoder.release();
                 encoder = null;
             } catch (Exception e) {
-                Log.e(TAG, "Release encoder error", e);
+                Timber.e(e, "Release encoder error");
             }
         }
 
@@ -579,7 +580,7 @@
         // 鍏抽棴鏂囦欢杈撳嚭
         closeFileOutput();
 
-        Log.d(TAG, "H264 encoder stopped");
+        Timber.d("H264 encoder stopped");
     }
 
     /**
@@ -590,9 +591,9 @@
             try {
                 fileOutputStream.flush();
                 fileOutputStream.close();
-                Log.d(TAG, "File output closed: " + outputFilePath);
+                Timber.d("File output closed: " + outputFilePath);
             } catch (IOException e) {
-                Log.e(TAG, "Close file output error", e);
+                Timber.e(e, "Close file output error");
             } finally {
                 fileOutputStream = null;
                 spsPpsWritten = false;

--
Gitblit v1.8.0