| | |
| | | package com.anyun.h264; |
| | | |
| | | import android.content.Context; |
| | | import android.media.MediaCodec; |
| | | import android.media.MediaCodecInfo; |
| | | import android.media.MediaFormat; |
| | | import com.anyun.libusbcamera.UsbCamera; |
| | | import com.anyun.libusbcamera.WatermarkParam; |
| | | import com.anyun.h264.model.WatermarkInfo; |
| | | import com.anyun.h264.util.FileUtil; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | |
| | | private int cameraId = 1; // 摄像头ID,默认为1(第一个摄像头) |
| | | private long currentFileStartTime = 0; // 当前文件的开始时间(毫秒) |
| | | private static final long FILE_DURATION_MS = 60 * 1000; // 文件时长:1分钟(毫秒) |
| | | |
| | | // Context 和清理配置 |
| | | private Context context; // Context 对象,用于清理 TF 卡文件 |
| | | private long maxH264TotalSizeGB = 5; // 最大 H264 文件总大小(GB),默认 5GB |
| | | |
| | | // 网络传输控制 |
| | | private boolean enableNetworkTransmission = true; // 是否启用TCP/UDP网络传输 |
| | |
| | | */ |
| | | public void setCameraId(int cameraId) { |
| | | this.cameraId = cameraId; |
| | | } |
| | | |
| | | /** |
| | | * 设置 Context(用于清理 TF 卡文件) |
| | | * @param context Context 对象 |
| | | */ |
| | | public void setContext(Context context) { |
| | | this.context = context; |
| | | } |
| | | |
| | | /** |
| | | * 设置最大 H264 文件总大小(GB) |
| | | * @param maxTotalSizeGB 最大总大小(GB),默认 5GB |
| | | */ |
| | | public void setMaxH264TotalSizeGB(long maxTotalSizeGB) { |
| | | this.maxH264TotalSizeGB = maxTotalSizeGB; |
| | | } |
| | | |
| | | /** |
| | |
| | | fileOutputStream = null; |
| | | } |
| | | |
| | | // 检查并清理 TF 卡上的 h264 文件(如果需要) |
| | | if (context != null && outputFileDirectory != null && !outputFileDirectory.isEmpty()) { |
| | | try { |
| | | // 判断当前目录是否在 TF 卡的 h264 目录下 |
| | | // 目录结构:/sdcard/h264/yyyyMMdd/,h264 根目录应该是父目录的父目录 |
| | | File currentDir = new File(outputFileDirectory); |
| | | File parentDir = currentDir.getParentFile(); |
| | | if (parentDir != null && "h264".equals(parentDir.getName())) { |
| | | // 当前目录在 h264 目录下,获取 h264 根目录 |
| | | String h264RootDir = parentDir.getAbsolutePath(); |
| | | Timber.d("Checking and cleaning up h264 files in: %s", h264RootDir); |
| | | FileUtil.cleanupH264Files(context, h264RootDir, maxH264TotalSizeGB, 1); |
| | | } |
| | | } catch (Exception e) { |
| | | Timber.e(e, "Error during h264 files cleanup check"); |
| | | // 清理失败不影响文件创建,继续执行 |
| | | } |
| | | } |
| | | |
| | | // 生成新文件名 |
| | | long timeFile = System.currentTimeMillis() / 1000 * 1000; |
| | | currentFileStartTime = timeFile; |