From 25ac86fe6a8d26dddd41ba7b4a276e6309b1afad Mon Sep 17 00:00:00 2001
From: Dana <Dana_Lee1016@126.com>
Date: 星期三, 03 十二月 2025 14:49:31 +0800
Subject: [PATCH] 1.sdcard/android/data/包名/files下的h264文件,超过五天删除掉

---
 app/src/main/java/com/anyun/h264/H264EncodeService.java |   39 +++++++++++++
 多进程方案使用说明.md                                            |  117 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 156 insertions(+), 0 deletions(-)

diff --git a/app/src/main/java/com/anyun/h264/H264EncodeService.java b/app/src/main/java/com/anyun/h264/H264EncodeService.java
index 5d9c404..bbdb554 100644
--- a/app/src/main/java/com/anyun/h264/H264EncodeService.java
+++ b/app/src/main/java/com/anyun/h264/H264EncodeService.java
@@ -36,6 +36,7 @@
     private H264FileTransmitter h264FileTransmitter; // H264鏂囦欢浼犺緭鍣�
     private String outputFileDirectory; // H264鏂囦欢杈撳嚭鐩綍
     private WatermarkInfo currentWatermarkInfo; // 褰撳墠姘村嵃淇℃伅
+    private static final int H264_FILE_RETENTION_DAYS = 5; // 鍙牴鎹渶姹傝皟鏁翠负3鎴�5澶�
     
     // 澶氳繘绋嬫敮鎸侊細绗簩涓憚鍍忓ご鐨勬湇鍔¤繛鎺�
     private IH264EncodeService camera2Service;
@@ -78,6 +79,9 @@
         // 鍒濆鍖栬緭鍑烘枃浠剁洰褰曪紙浣跨敤搴旂敤澶栭儴瀛樺偍鐩綍锛�
         outputFileDirectory = getExternalFilesDir(null).getAbsolutePath();
         Timber.d("Output file directory: %s", outputFileDirectory);
+
+        // 娓呯悊杩囨湡鐨凥264鏂囦欢
+        cleanupExpiredH264Files(H264_FILE_RETENTION_DAYS);
     }
     
     @Override
@@ -793,6 +797,41 @@
             currentWatermarkInfo = null;
         }
     }
+
+    /**
+     * 鍒犻櫎瓒呰繃淇濈暀鏈熺殑H264鏂囦欢
+     */
+    private void cleanupExpiredH264Files(int retentionDays) {
+        if (outputFileDirectory == null) {
+            Timber.w("cleanupExpiredH264Files: outputFileDirectory is null");
+            return;
+        }
+
+        File dir = new File(outputFileDirectory);
+        if (!dir.exists() || !dir.isDirectory()) {
+            Timber.w("cleanupExpiredH264Files: directory invalid -> %s", outputFileDirectory);
+            return;
+        }
+
+        long retentionMillis = TimeUnit.DAYS.toMillis(Math.max(1, retentionDays));
+        long cutoffTime = System.currentTimeMillis() - retentionMillis;
+
+        File[] files = dir.listFiles((d, name) -> name.toLowerCase(Locale.CHINA).endsWith(".h264"));
+        if (files == null || files.length == 0) {
+            return;
+        }
+
+        for (File file : files) {
+            if (file.lastModified() < cutoffTime) {
+                boolean deleted = file.delete();
+                if (deleted) {
+                    Timber.i("Deleted expired H264 file: %s", file.getAbsolutePath());
+                } else {
+                    Timber.w("Failed to delete expired H264 file: %s", file.getAbsolutePath());
+                }
+            }
+        }
+    }
     
     /**
      * 浠庢枃浠跺垱寤鸿祫婧愪俊鎭紙濡傛灉鏂囦欢鍦ㄦ椂闂磋寖鍥村唴锛�
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"
new file mode 100644
index 0000000..b3ead22
--- /dev/null
+++ "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"
@@ -0,0 +1,117 @@
+# 澶氳繘绋嬫柟妗堜娇鐢ㄨ鏄�
+
+## 姒傝堪
+
+鏈柟妗堥�氳繃澶氳繘绋嬪疄鐜颁袱涓猆SB鎽勫儚澶村悓鏃跺伐浣溿�傛瘡涓憚鍍忓ご杩愯鍦ㄧ嫭绔嬬殑杩涚▼涓紝姣忎釜杩涚▼鎷ユ湁鐙珛鐨凥264Encoder瀹炰緥锛屼粠鑰岄伩鍏嶄簡搴曞眰C搴撶殑鍗曞疄渚嬮檺鍒躲��
+
+## 鏋舵瀯璇存槑
+
+- **H264EncodeService**锛氫富杩涚▼鏈嶅姟锛屽鐞嗙涓�涓憚鍍忓ご锛坈ameraId=1锛�
+- **H264EncodeService2**锛氱嫭绔嬭繘绋嬫湇鍔★紙`:camera2`锛夛紝澶勭悊绗簩涓憚鍍忓ご锛坈ameraId=2锛�
+
+## 浣跨敤鏂规硶
+
+### 鍚姩绗竴涓憚鍍忓ご锛坈ameraId=1锛�
+
+```json
+{
+  "width": 640,
+  "height": 480,
+  "framerate": 25,
+  "cameraId": 1
+}
+```
+
+鎴栬�呬笉鎸囧畾cameraId锛堥粯璁や负1锛夛細
+
+```json
+{
+  "width": 640,
+  "height": 480,
+  "framerate": 25
+}
+```
+
+### 鍚姩绗簩涓憚鍍忓ご锛坈ameraId=2锛�
+
+```json
+{
+  "width": 640,
+  "height": 480,
+  "framerate": 25,
+  "cameraId": 2
+}
+```
+
+### 缃戠粶鎺ㄩ�佺ず渚�
+
+**绗竴涓憚鍍忓ご锛�**
+```json
+{
+  "ip": "192.168.1.100",
+  "port": 8888,
+  "width": 640,
+  "height": 480,
+  "framerate": 25,
+  "simPhone": "013120122580",
+  "cameraId": 1
+}
+```
+
+**绗簩涓憚鍍忓ご锛�**
+```json
+{
+  "ip": "192.168.1.100",
+  "port": 8889,
+  "width": 640,
+  "height": 480,
+  "framerate": 25,
+  "simPhone": "013120122580",
+  "cameraId": 2
+}
+```
+
+### 鍋滄鎸囧畾鎽勫儚澶�
+
+鍋滄绗竴涓憚鍍忓ご锛�
+```json
+{
+  "cameraId": 1
+}
+```
+
+鍋滄绗簩涓憚鍍忓ご锛�
+```json
+{
+  "cameraId": 2
+}
+```
+
+濡傛灉涓嶆寚瀹歝ameraId锛岄粯璁ゅ仠姝㈢涓�涓憚鍍忓ご銆�
+
+## 鍏抽敭鐗规��
+
+1. **杩涚▼闅旂**锛氫袱涓憚鍍忓ご杩愯鍦ㄥ畬鍏ㄧ嫭绔嬬殑杩涚▼涓紝浜掍笉骞叉壈
+2. **鑷姩璺敱**锛氫富鏈嶅姟鏍规嵁cameraId鑷姩璺敱鍒板搴旂殑杩涚▼
+3. **鐙珛閰嶇疆**锛氭瘡涓憚鍍忓ご鍙互鐙珛閰嶇疆鍒嗚鲸鐜囥�佸抚鐜囥�佺綉缁滃弬鏁扮瓑
+4. **鐙珛鏂囦欢**锛氱浜屼釜鎽勫儚澶寸殑鏂囦欢浼氭坊鍔燻camera2`鏍囪瘑锛屼究浜庡尯鍒�
+5. **鐙珛閫氶亾**锛氱浜屼釜鎽勫儚澶翠娇鐢╜logicalChannelId=2`锛岀涓�涓娇鐢╜logicalChannelId=1`
+
+## 鏂囦欢鍛藉悕
+
+- 绗竴涓憚鍍忓ご锛歚h264_1234567890123.h264`
+- 绗簩涓憚鍍忓ご锛歚h264_camera2_1234567890123.h264`
+
+## 娉ㄦ剰浜嬮」
+
+1. 纭繚涓や釜USB鎽勫儚澶撮兘宸叉纭繛鎺�
+2. 绗簩涓憚鍍忓ご鍥哄畾浣跨敤cameraId=2锛岀涓�涓憚鍍忓ご浣跨敤cameraId=1锛堟垨涓嶆寚瀹氾級
+3. 涓や釜杩涚▼鍙互鍚屾椂杩愯锛屼簰涓嶅奖鍝�
+4. 姣忎釜杩涚▼閮芥湁鐙珛鐨凥264Encoder瀹炰緥锛屽洜姝ゅ彲浠ョ湡姝e苟鍙戝伐浣�
+
+## 鎶�鏈疄鐜�
+
+- 涓绘湇鍔¢�氳繃AIDL缁戝畾鍒扮浜屼釜杩涚▼鐨勬湇鍔�
+- 绗簩涓繘绋嬮�氳繃`android:process=":camera2"`閰嶇疆鍦ㄧ嫭绔嬭繘绋嬩腑杩愯
+- 鎵�鏈夋搷浣滈兘閫氳繃AIDL鎺ュ彛杩涜杩涚▼闂撮�氫俊
+

--
Gitblit v1.8.0