From 90f0e6d751e91206bacde671e950ec6f6d40ef64 Mon Sep 17 00:00:00 2001
From: Dana <Dana_Lee1016@126.com>
Date: 星期日, 30 十一月 2025 17:19:58 +0800
Subject: [PATCH] 1.h264文件以rtp sendPacket 添加到service ,未测试
---
app/src/main/java/com/anyun/h264/H264EncodeService.java | 214 ++++++++++++++++++++++++++++++++++++++++++++++++-----
app/src/main/aidl/com/anyun/h264/IH264EncodeService.aidl | 19 +++-
2 files changed, 207 insertions(+), 26 deletions(-)
diff --git a/app/src/main/aidl/com/anyun/h264/IH264EncodeService.aidl b/app/src/main/aidl/com/anyun/h264/IH264EncodeService.aidl
index 3ae93a7..4521fe8 100644
--- a/app/src/main/aidl/com/anyun/h264/IH264EncodeService.aidl
+++ b/app/src/main/aidl/com/anyun/h264/IH264EncodeService.aidl
@@ -8,11 +8,20 @@
*/
interface IH264EncodeService {
/**
- * 鎺у埗H264缂栫爜
- * @param action 鎿嶄綔绫诲瀷锛�0-寮�鍚痟264鏂囦欢鍐欏叆锛�1-鍋滄h264缂栫爜骞跺仠姝㈠啓鍏ユ枃浠讹紝2-寮�鍚綉缁滄帹閫乭264锛堜笉鍐欏叆鏂囦欢锛夛紝3-鍋滄h264缂栫爜骞跺仠姝㈢綉缁滄帹閫�
- * @param jsonConfig JSON鏍煎紡鐨勯厤缃弬鏁帮紝鍖呭惈锛歩p锛堟湇鍔″櫒IP锛夈�乸ort锛堟湇鍔″櫒绔彛锛夈�亀idth锛堣棰戝搴︼級銆乭eight锛堣棰戦珮搴︼級銆乫ramerate锛堝抚鐜囷級銆乻imPhone锛圫IM鍗″彿锛�
- * 绀轰緥锛歿"ip":"192.168.1.100","port":8888,"width":640,"height":480,"framerate":25,"simPhone":"013120122580"}
- * 濡傛灉action涓�1鎴�3锛堝仠姝㈡搷浣滐級锛屾鍙傛暟鍙负绌烘垨null
+ * 鎺у埗H264缂栫爜鍜屾枃浠朵紶杈�
+ * @param action 鎿嶄綔绫诲瀷锛�
+ * 0-寮�鍚痟264鏂囦欢鍐欏叆锛�
+ * 1-鍋滄h264缂栫爜骞跺仠姝㈠啓鍏ユ枃浠讹紝
+ * 2-寮�鍚綉缁滄帹閫乭264锛堜笉鍐欏叆鏂囦欢锛夛紝
+ * 3-鍋滄h264缂栫爜骞跺仠姝㈢綉缁滄帹閫侊紝
+ * 4-寮�濮嬩紶杈揌264鏂囦欢锛堜粠鏂囦欢璇诲彇骞剁綉缁滄帹閫侊級锛�
+ * 5-鍋滄H264鏂囦欢浼犺緭
+ * @param jsonConfig JSON鏍煎紡鐨勯厤缃弬鏁�
+ * action 0/2: 鍖呭惈锛歩p锛堟湇鍔″櫒IP锛夈�乸ort锛堟湇鍔″櫒绔彛锛夈�亀idth锛堣棰戝搴︼級銆乭eight锛堣棰戦珮搴︼級銆乫ramerate锛堝抚鐜囷級銆乻imPhone锛圫IM鍗″彿锛�
+ * 绀轰緥锛歿"ip":"192.168.1.100","port":8888,"width":640,"height":480,"framerate":25,"simPhone":"013120122580"}
+ * action 4: 鍖呭惈锛歩p锛堟湇鍔″櫒IP锛夈�乸ort锛堟湇鍔″櫒绔彛锛夈�乫ramerate锛堝抚鐜囷級銆乻imPhone锛圫IM鍗″彿锛夈�乫ilePath锛圚264鏂囦欢璺緞锛夈�乸rotocolType锛堝崗璁被鍨嬶紝鍙�夛紝1-UDP锛�2-TCP锛岄粯璁CP锛�
+ * 绀轰緥锛歿"ip":"192.168.1.100","port":8888,"framerate":25,"simPhone":"013120122580","filePath":"/sdcard/video.h264","protocolType":2}
+ * action 1/3/5: 姝ゅ弬鏁板彲涓虹┖鎴杗ull锛堝仠姝㈡搷浣滀笉闇�瑕侀厤缃級
* @return 0-鎴愬姛锛�1-澶辫触
*/
int controlEncode(int action, String jsonConfig);
diff --git a/app/src/main/java/com/anyun/h264/H264EncodeService.java b/app/src/main/java/com/anyun/h264/H264EncodeService.java
index 6157ba4..1d78498 100644
--- a/app/src/main/java/com/anyun/h264/H264EncodeService.java
+++ b/app/src/main/java/com/anyun/h264/H264EncodeService.java
@@ -27,6 +27,7 @@
private static final String TAG = "H264EncodeService";
private H264Encoder h264Encoder;
+ private H264FileTransmitter h264FileTransmitter; // H264鏂囦欢浼犺緭鍣�
private String outputFileDirectory; // H264鏂囦欢杈撳嚭鐩綍
// 榛樿缂栫爜鍙傛暟
@@ -80,8 +81,9 @@
super.onDestroy();
Log.d(TAG, "Service destroyed");
- // 鍋滄骞堕噴鏀剧紪鐮佸櫒
+ // 鍋滄骞堕噴鏀剧紪鐮佸櫒鍜屾枃浠朵紶杈撳櫒
stopEncoder();
+ stopFileTransmitter();
}
/**
@@ -122,42 +124,91 @@
}
/**
- * 鎺у埗H264缂栫爜
- * @param action 鎿嶄綔绫诲瀷锛�0-寮�鍚痟264鏂囦欢鍐欏叆锛�1-鍋滄h264缂栫爜骞跺仠姝㈠啓鍏ユ枃浠讹紝2-寮�鍚綉缁滄帹閫乭264锛堜笉鍐欏叆鏂囦欢锛夛紝3-鍋滄h264缂栫爜骞跺仠姝㈢綉缁滄帹閫�
- * @param jsonConfig JSON鏍煎紡鐨勯厤缃弬鏁帮紝鍖呭惈锛歩p銆乸ort銆亀idth銆乭eight銆乫ramerate銆乻imPhone
+ * 鏂囦欢浼犺緭閰嶇疆绫�
+ */
+ private static class FileTransmitConfig {
+ String ip;
+ int port;
+ int framerate;
+ String simPhone;
+ String filePath; // H264鏂囦欢璺緞
+ int protocolType; // 鍗忚绫诲瀷锛�1-UDP锛�2-TCP
+
+ // 浠嶫SON瑙f瀽閰嶇疆
+ static FileTransmitConfig fromJson(String jsonConfig) throws JSONException {
+ FileTransmitConfig config = new FileTransmitConfig();
+ if (jsonConfig == null || jsonConfig.trim().isEmpty()) {
+ throw new JSONException("File transmit config cannot be empty");
+ }
+
+ JSONObject json = new JSONObject(jsonConfig);
+ config.ip = json.optString("ip", null);
+ config.port = json.optInt("port", 0);
+ config.framerate = json.optInt("framerate", DEFAULT_FRAME_RATE);
+ config.simPhone = json.optString("simPhone", "013120122580");
+ config.filePath = json.optString("filePath", null);
+ // 鍗忚绫诲瀷锛氶粯璁CP锛�2锛夛紝1-UDP锛�2-TCP
+ config.protocolType = json.optInt("protocolType", JT1076ProtocolHelper.PROTOCOL_TYPE_TCP);
+
+ return config;
+ }
+ }
+
+ /**
+ * 鎺у埗H264缂栫爜鍜屾枃浠朵紶杈�
+ * @param action 鎿嶄綔绫诲瀷锛�
+ * 0-寮�鍚痟264鏂囦欢鍐欏叆锛�
+ * 1-鍋滄h264缂栫爜骞跺仠姝㈠啓鍏ユ枃浠讹紝
+ * 2-寮�鍚綉缁滄帹閫乭264锛堜笉鍐欏叆鏂囦欢锛夛紝
+ * 3-鍋滄h264缂栫爜骞跺仠姝㈢綉缁滄帹閫侊紝
+ * 4-寮�濮嬩紶杈揌264鏂囦欢锛堜粠鏂囦欢璇诲彇骞剁綉缁滄帹閫侊級锛�
+ * 5-鍋滄H264鏂囦欢浼犺緭
+ * @param jsonConfig JSON鏍煎紡鐨勯厤缃弬鏁�
+ * action 0/2: 鍖呭惈锛歩p銆乸ort銆亀idth銆乭eight銆乫ramerate銆乻imPhone
+ * action 4: 鍖呭惈锛歩p銆乸ort銆乫ramerate銆乻imPhone銆乫ilePath銆乸rotocolType锛堝彲閫夛紝1-UDP锛�2-TCP锛岄粯璁CP锛�
+ * action 1/3/5: 姝ゅ弬鏁板彲涓虹┖鎴杗ull
* @return 0-鎴愬姛锛�1-澶辫触
*/
private synchronized int controlEncode(int action, String jsonConfig) {
Log.d(TAG, "controlEncode called with action: " + action + ", jsonConfig: " + jsonConfig);
try {
- EncodeConfig config = null;
-
- // 瀵逛簬鍋滄鎿嶄綔锛屼笉闇�瑕佽В鏋愰厤缃�
- if (action != 1 && action != 3) {
- try {
- config = EncodeConfig.fromJson(jsonConfig);
- Log.d(TAG, "Parsed config - width: " + config.width + ", height: " + config.height +
- ", framerate: " + config.framerate + ", ip: " + config.ip + ", port: " + config.port +
- ", simPhone: " + config.simPhone);
- } catch (JSONException e) {
- Log.e(TAG, "Failed to parse JSON config: " + jsonConfig, e);
- return 1; // 澶辫触
- }
- }
-
switch (action) {
case 0: // 寮�鍚痟264鏂囦欢鍐欏叆
- return startFileEncode(config);
+ try {
+ EncodeConfig config0 = EncodeConfig.fromJson(jsonConfig);
+ return startFileEncode(config0);
+ } catch (JSONException e) {
+ Log.e(TAG, "Failed to parse JSON config: " + jsonConfig, e);
+ return 1;
+ }
case 1: // 鍋滄h264缂栫爜骞跺仠姝㈠啓鍏ユ枃浠�
return stopEncoder();
case 2: // 寮�鍚綉缁滄帹閫乭264锛堜笉鍐欏叆鏂囦欢锛�
- return startNetworkEncode(config);
+ try {
+ EncodeConfig config2 = EncodeConfig.fromJson(jsonConfig);
+ return startNetworkEncode(config2);
+ } catch (JSONException e) {
+ Log.e(TAG, "Failed to parse JSON config: " + jsonConfig, e);
+ return 1;
+ }
case 3: // 鍋滄h264缂栫爜骞跺仠姝㈢綉缁滄帹閫�
return stopEncoder();
+
+ case 4: // 寮�濮嬩紶杈揌264鏂囦欢
+ try {
+ FileTransmitConfig config4 = FileTransmitConfig.fromJson(jsonConfig);
+ return startFileTransmit(config4);
+ } catch (JSONException e) {
+ Log.e(TAG, "Failed to parse JSON config: " + jsonConfig, e);
+ return 1;
+ }
+
+ case 5: // 鍋滄H264鏂囦欢浼犺緭
+ return stopFileTransmitter();
default:
Log.e(TAG, "Unknown action: " + action);
@@ -310,6 +361,127 @@
}
/**
+ * 鍚姩鏂囦欢浼犺緭妯″紡锛堜粠H264鏂囦欢璇诲彇骞剁綉缁滄帹閫侊級
+ */
+ private int startFileTransmit(FileTransmitConfig config) {
+ Log.d(TAG, "Starting file transmit mode");
+
+ // 濡傛灉鏂囦欢浼犺緭鍣ㄥ凡缁忓湪杩愯锛屽厛鍋滄
+ if (h264FileTransmitter != null) {
+ Log.w(TAG, "File transmitter is already running, stopping it first");
+ stopFileTransmitter();
+ }
+
+ // 妫�鏌ュ繀闇�鐨勯厤缃弬鏁�
+ if (config == null || config.ip == null || config.ip.trim().isEmpty() || config.port <= 0) {
+ Log.e(TAG, "File transmit requires valid ip and port in config");
+ return 1; // 澶辫触
+ }
+
+ if (config.filePath == null || config.filePath.trim().isEmpty()) {
+ Log.e(TAG, "File transmit requires valid filePath in config");
+ return 1; // 澶辫触
+ }
+
+ try {
+ // 妫�鏌ユ枃浠舵槸鍚﹀瓨鍦�
+ File file = new File(config.filePath);
+ if (!file.exists() || !file.isFile()) {
+ Log.e(TAG, "File does not exist: " + config.filePath);
+ return 1; // 澶辫触
+ }
+
+ // 鍒涘缓鏂囦欢浼犺緭鍣�
+ h264FileTransmitter = new H264FileTransmitter();
+
+ // 璁剧疆鏈嶅姟鍣ㄥ湴鍧�
+ h264FileTransmitter.setServerAddress(config.ip, config.port);
+
+ // 璁剧疆鍗忚绫诲瀷
+ h264FileTransmitter.setProtocolType(config.protocolType);
+
+ // 璁剧疆鍗忚鍙傛暟锛圫IM鍗″彿鍜岄�昏緫閫氶亾鍙凤級
+ String simPhone = config.simPhone != null && !config.simPhone.trim().isEmpty()
+ ? config.simPhone : "013120122580";
+ h264FileTransmitter.setProtocolParams(simPhone, (byte)1);
+
+ // 璁剧疆甯х巼锛堢敤浜庤绠楁椂闂存埑闂撮殧锛�
+ int framerate = config.framerate > 0 ? config.framerate : DEFAULT_FRAME_RATE;
+ h264FileTransmitter.setFrameRate(framerate);
+
+ // 璁剧疆杩涘害鍥炶皟锛堝彲閫夛紝鐢ㄤ簬鏃ュ織杈撳嚭锛�
+ h264FileTransmitter.setOnTransmitProgressCallback(new H264FileTransmitter.OnTransmitProgressCallback() {
+ @Override
+ public void onProgress(int currentFrame, int totalFrames) {
+ Log.d(TAG, "File transmit progress: frame " + currentFrame +
+ (totalFrames > 0 ? " of " + totalFrames : ""));
+ }
+
+ @Override
+ public void onComplete() {
+ Log.d(TAG, "File transmit completed");
+ }
+
+ @Override
+ public void onError(String error) {
+ Log.e(TAG, "File transmit error: " + error);
+ }
+ });
+
+ // 鍒濆鍖朣ocket杩炴帴
+ if (!h264FileTransmitter.initialize()) {
+ Log.e(TAG, "Failed to initialize file transmitter socket");
+ h264FileTransmitter = null;
+ return 1; // 澶辫触
+ }
+
+ // 寮�濮嬩紶杈撴枃浠�
+ h264FileTransmitter.transmitFile(config.filePath);
+
+ Log.d(TAG, "File transmit started successfully, file: " + config.filePath +
+ ", server: " + config.ip + ":" + config.port +
+ ", protocol: " + (config.protocolType == JT1076ProtocolHelper.PROTOCOL_TYPE_UDP ? "UDP" : "TCP") +
+ ", framerate: " + framerate);
+ return 0; // 鎴愬姛
+
+ } catch (Exception e) {
+ Log.e(TAG, "Failed to start file transmit", e);
+ if (h264FileTransmitter != null) {
+ try {
+ h264FileTransmitter.stop();
+ } catch (Exception ex) {
+ Log.e(TAG, "Error stopping file transmitter after failure", ex);
+ }
+ h264FileTransmitter = null;
+ }
+ return 1; // 澶辫触
+ }
+ }
+
+ /**
+ * 鍋滄鏂囦欢浼犺緭鍣�
+ */
+ private int stopFileTransmitter() {
+ Log.d(TAG, "Stopping file transmitter");
+
+ if (h264FileTransmitter != null) {
+ try {
+ h264FileTransmitter.stop();
+ h264FileTransmitter = null;
+ Log.d(TAG, "File transmitter stopped successfully");
+ return 0; // 鎴愬姛
+ } catch (Exception e) {
+ Log.e(TAG, "Error stopping file transmitter", e);
+ h264FileTransmitter = null;
+ return 1; // 澶辫触
+ }
+ } else {
+ Log.w(TAG, "File transmitter is not running");
+ return 0; // 鎴愬姛锛堟病鏈夎繍琛岀殑鏂囦欢浼犺緭鍣紝瑙嗕负鎴愬姛锛�
+ }
+ }
+
+ /**
* 鑾峰彇璧勬簮鍒楄〃锛堟牴鎹甁T/T 1076-2016琛�23瀹氫箟锛�
* @param startTime 寮�濮嬫椂闂达紙鏍煎紡锛歒YMMDDHHmmss锛�
* @param endTime 缁撴潫鏃堕棿锛堟牸寮忥細YYMMDDHHmmss锛�
--
Gitblit v1.8.0