From 597ab1d3451e000597ba3e12749f087f6b45a328 Mon Sep 17 00:00:00 2001
From: Dana <Dana_Lee1016@126.com>
Date: 星期四, 29 一月 2026 13:14:49 +0800
Subject: [PATCH] 1.p2摄像头 跨进程服务,未测试
---
app/src/main/java/com/safeluck/floatwindow/manager/UsbCameraPushManager.java | 4
app/src/main/java/com/safeluck/floatwindow/P2UsbCameraVideoService.java | 172 ++++++++++++++++++++++++++++
app/src/main/aidl/com/safeluck/floatwindow/IMediaAidlInterface.aidl | 2
app/src/main/java/com/safeluck/floatwindow/MainActivity.kt | 182 ++++++++++++++++++++++++++++--
4 files changed, 344 insertions(+), 16 deletions(-)
diff --git a/app/src/main/aidl/com/safeluck/floatwindow/IMediaAidlInterface.aidl b/app/src/main/aidl/com/safeluck/floatwindow/IMediaAidlInterface.aidl
index cb25522..224a06b 100644
--- a/app/src/main/aidl/com/safeluck/floatwindow/IMediaAidlInterface.aidl
+++ b/app/src/main/aidl/com/safeluck/floatwindow/IMediaAidlInterface.aidl
@@ -15,7 +15,7 @@
// void transferInfo(String info);
void startMedia(in MediaArgu media);
- void stopMedia();
+ void stopMedia(in MediaArgu media);
void registerCallback(in IMyCallback cb);
void unregisterCallback(in IMyCallback cb);
}
diff --git a/app/src/main/java/com/safeluck/floatwindow/MainActivity.kt b/app/src/main/java/com/safeluck/floatwindow/MainActivity.kt
index 437b645..76a24d5 100644
--- a/app/src/main/java/com/safeluck/floatwindow/MainActivity.kt
+++ b/app/src/main/java/com/safeluck/floatwindow/MainActivity.kt
@@ -80,7 +80,11 @@
onStartUsbRecord = { startUsbRecord() },
onStopUsbRecord = { stopUsbRecord() },
onStartUsbPush = { startUsbPush() },
- onStopUsbPush = { stopUsbPush() }
+ onStopUsbPush = { stopUsbPush() },
+ onStartP2Push = { startP2Push() },
+ onStopP2Push = { stopP2Push() },
+ onStartP2Record = { startP2Record() },
+ onStopP2Record = { stopP2Record() }
)
}
}
@@ -119,7 +123,12 @@
}
try {
- mediaAidlInterface?.stopMedia()
+ val mediaArgu = MediaArgu().apply {
+ isPush = false
+ isUsedOutCamera = false // Android 鍐呯疆鎽勫儚澶�
+ usbCameraId = 0 // Android 鐩告満锛寀sbCameraId 涓� 0
+ }
+ mediaAidlInterface?.stopMedia(mediaArgu)
Timber.d("Stopped Android camera record")
} catch (e: RemoteException) {
Timber.e(e, "Error stopping Android record")
@@ -136,6 +145,7 @@
val mediaArgu = MediaArgu().apply {
isPush = false
isUsedOutCamera = true // USB 鎽勫儚澶�
+ usbCameraId = 1 // P1 USB 鎽勫儚澶�
codeRate = 0
frameRate = 0
m_screen = MediaArgu.ScreenSolution(640, 480) // 榛樿鍒嗚鲸鐜�
@@ -145,7 +155,7 @@
mediaAidlInterface?.registerCallback(callback)
mediaAidlInterface?.startMedia(mediaArgu)
- Timber.d("Started USB camera record")
+ Timber.d("Started USB camera record (P1)")
} catch (e: RemoteException) {
Timber.e(e, "Error starting USB record")
}
@@ -158,8 +168,13 @@
}
try {
- mediaAidlInterface?.stopMedia()
- Timber.d("Stopped USB camera record")
+ val mediaArgu = MediaArgu().apply {
+ isPush = false
+ isUsedOutCamera = true // USB 鎽勫儚澶�
+ usbCameraId = 1 // P1 USB 鎽勫儚澶�
+ }
+ mediaAidlInterface?.stopMedia(mediaArgu)
+ Timber.d("Stopped USB camera record (P1)")
} catch (e: RemoteException) {
Timber.e(e, "Error stopping USB record")
}
@@ -173,6 +188,7 @@
try {
val mediaArgu = MediaArgu().apply {
+ usbCameraId = 1 // P1 USB 鎽勫儚澶�
isPush = true
isUsedOutCamera = true // USB 鎽勫儚澶�
codeRate = 0
@@ -185,7 +201,7 @@
mediaAidlInterface?.registerCallback(callback)
mediaAidlInterface?.startMedia(mediaArgu)
- Timber.d("Started USB camera push")
+ Timber.d("Started USB camera push (P1)")
} catch (e: RemoteException) {
Timber.e(e, "Error starting USB push")
}
@@ -198,10 +214,106 @@
}
try {
- mediaAidlInterface?.stopMedia()
- Timber.d("Stopped USB camera push")
+ val mediaArgu = MediaArgu().apply {
+ usbCameraId = 1 // P1 USB 鎽勫儚澶�
+ isPush = true
+ isUsedOutCamera = true // USB 鎽勫儚澶�
+ }
+ mediaAidlInterface?.stopMedia(mediaArgu)
+ Timber.d("Stopped USB camera push (P1)")
} catch (e: RemoteException) {
Timber.e(e, "Error stopping USB push")
+ }
+ }
+
+ private fun startP2Push() {
+ if (mediaAidlInterface == null) {
+ Timber.w("Service not bound, cannot start P2 push")
+ return
+ }
+
+ try {
+ val mediaArgu = MediaArgu().apply {
+ usbCameraId = 2 // P2 USB 鎽勫儚澶�
+ isPush = true
+ isUsedOutCamera = true // USB 鎽勫儚澶�
+ codeRate = 0
+ frameRate = 0
+ m_screen = MediaArgu.ScreenSolution(640, 480) // 榛樿鍒嗚鲸鐜�
+ url = "rtmp://192.168.16.143/live/livestream" // TODO: 闇�瑕佽缃疄闄呯殑鎺ㄦ祦鍦板潃
+ userName = ""
+ pwd = ""
+ }
+
+ mediaAidlInterface?.registerCallback(callback)
+ mediaAidlInterface?.startMedia(mediaArgu)
+ Timber.d("Started P2 USB camera push")
+ } catch (e: RemoteException) {
+ Timber.e(e, "Error starting P2 push")
+ }
+ }
+
+ private fun stopP2Push() {
+ if (mediaAidlInterface == null) {
+ Timber.w("Service not bound, cannot stop P2 push")
+ return
+ }
+
+ try {
+ val mediaArgu = MediaArgu().apply {
+ usbCameraId = 2 // P2 USB 鎽勫儚澶�
+ isPush = true
+ isUsedOutCamera = true // USB 鎽勫儚澶�
+ }
+ mediaAidlInterface?.stopMedia(mediaArgu)
+ Timber.d("Stopped P2 USB camera push")
+ } catch (e: RemoteException) {
+ Timber.e(e, "Error stopping P2 push")
+ }
+ }
+
+ private fun startP2Record() {
+ if (mediaAidlInterface == null) {
+ Timber.w("Service not bound, cannot start P2 record")
+ return
+ }
+
+ try {
+ val mediaArgu = MediaArgu().apply {
+ isPush = false
+ isUsedOutCamera = true // USB 鎽勫儚澶�
+ usbCameraId = 2 // P2 USB 鎽勫儚澶�
+ codeRate = 0
+ frameRate = 0
+ m_screen = MediaArgu.ScreenSolution(640, 480) // 榛樿鍒嗚鲸鐜�
+ recordTime = 0
+ tfCardFlag = 0 // 鍐呴儴瀛樺偍
+ }
+
+ mediaAidlInterface?.registerCallback(callback)
+ mediaAidlInterface?.startMedia(mediaArgu)
+ Timber.d("Started P2 USB camera record")
+ } catch (e: RemoteException) {
+ Timber.e(e, "Error starting P2 record")
+ }
+ }
+
+ private fun stopP2Record() {
+ if (mediaAidlInterface == null) {
+ Timber.w("Service not bound, cannot stop P2 record")
+ return
+ }
+
+ try {
+ val mediaArgu = MediaArgu().apply {
+ isPush = false
+ isUsedOutCamera = true // USB 鎽勫儚澶�
+ usbCameraId = 2 // P2 USB 鎽勫儚澶�
+ }
+ mediaAidlInterface?.stopMedia(mediaArgu)
+ Timber.d("Stopped P2 USB camera record")
+ } catch (e: RemoteException) {
+ Timber.e(e, "Error stopping P2 record")
}
}
@@ -240,7 +352,11 @@
onStartUsbRecord: () -> Unit,
onStopUsbRecord: () -> Unit,
onStartUsbPush: () -> Unit,
- onStopUsbPush: () -> Unit
+ onStopUsbPush: () -> Unit,
+ onStartP2Push: () -> Unit,
+ onStopP2Push: () -> Unit,
+ onStartP2Record: () -> Unit,
+ onStopP2Record: () -> Unit
) {
Column(
modifier = Modifier.fillMaxSize()
@@ -339,22 +455,62 @@
Divider(modifier = Modifier.padding(vertical = 8.dp))
- // 鎸夐挳 7: 寮�濮� USB 鎺ㄦ祦
+ // 鎸夐挳 7: 寮�濮� USB 鎺ㄦ祦 (P1)
Button(
onClick = onStartUsbPush,
modifier = Modifier.fillMaxWidth(),
enabled = isServiceBound
) {
- Text("7. 寮�濮� USB 鎺ㄦ祦")
+ Text("7. 寮�濮� USB 鎺ㄦ祦 (P1)")
}
- // 鎸夐挳 8: 缁撴潫 USB 鎺ㄦ祦
+ // 鎸夐挳 8: 缁撴潫 USB 鎺ㄦ祦 (P1)
Button(
onClick = onStopUsbPush,
modifier = Modifier.fillMaxWidth(),
enabled = isServiceBound
) {
- Text("8. 缁撴潫 USB 鎺ㄦ祦")
+ Text("8. 缁撴潫 USB 鎺ㄦ祦 (P1)")
+ }
+
+ Divider(modifier = Modifier.padding(vertical = 8.dp))
+
+ // 鎸夐挳 9: 寮�濮� P2 USB 鎺ㄦ祦
+ Button(
+ onClick = onStartP2Push,
+ modifier = Modifier.fillMaxWidth(),
+ enabled = isServiceBound
+ ) {
+ Text("9. 寮�濮� P2 USB 鎺ㄦ祦")
+ }
+
+ // 鎸夐挳 10: 缁撴潫 P2 USB 鎺ㄦ祦
+ Button(
+ onClick = onStopP2Push,
+ modifier = Modifier.fillMaxWidth(),
+ enabled = isServiceBound
+ ) {
+ Text("10. 缁撴潫 P2 USB 鎺ㄦ祦")
+ }
+
+ Divider(modifier = Modifier.padding(vertical = 8.dp))
+
+ // 鎸夐挳 11: 寮�濮� P2 USB 褰曞儚
+ Button(
+ onClick = onStartP2Record,
+ modifier = Modifier.fillMaxWidth(),
+ enabled = isServiceBound
+ ) {
+ Text("11. 寮�濮� P2 USB 褰曞儚")
+ }
+
+ // 鎸夐挳 12: 缁撴潫 P2 USB 褰曞儚
+ Button(
+ onClick = onStopP2Record,
+ modifier = Modifier.fillMaxWidth(),
+ enabled = isServiceBound
+ ) {
+ Text("12. 缁撴潫 P2 USB 褰曞儚")
}
Spacer(modifier = Modifier.height(12.dp))
diff --git a/app/src/main/java/com/safeluck/floatwindow/P2UsbCameraVideoService.java b/app/src/main/java/com/safeluck/floatwindow/P2UsbCameraVideoService.java
new file mode 100644
index 0000000..fbd7398
--- /dev/null
+++ b/app/src/main/java/com/safeluck/floatwindow/P2UsbCameraVideoService.java
@@ -0,0 +1,172 @@
+package com.safeluck.floatwindow;
+
+import android.app.Service;
+import android.content.Context;
+import android.content.Intent;
+import android.os.IBinder;
+import android.os.RemoteCallbackList;
+import android.os.RemoteException;
+
+import com.safeluck.floatwindow.manager.UsbCameraPushManager;
+import com.safeluck.floatwindow.manager.UsbCameraRecordManager;
+
+import timber.log.Timber;
+
+/**
+ * 杩愯鍦ㄧ嫭绔嬭繘绋�(:p2)鐨� USB 鎽勫儚澶磋棰戞湇鍔★紝鐢ㄤ簬鍚屾椂鏀寔涓よ矾 USB 鎽勫儚澶村伐浣溿��
+ * 绾﹀畾锛氬綋 MediaArgu.usbCameraId == 2 鏃讹紝璋冪敤鏂瑰簲浣跨敤璇ユ湇鍔¤繘琛屾帹娴�/褰曞儚銆�
+ */
+public class P2UsbCameraVideoService extends Service {
+ private static final String TAG = "P2UsbCameraVideoService";
+
+ private Context context;
+
+ private UsbCameraPushManager usbCameraPushManager;
+ private UsbCameraRecordManager usbCameraRecordManager;
+
+ private ManagerType currentManagerType = ManagerType.NONE;
+
+ private enum ManagerType {
+ NONE,
+ USB_PUSH,
+ USB_RECORD
+ }
+
+ private final RemoteCallbackList<IMyCallback> mCallbacks = new RemoteCallbackList<>();
+
+ private final IMediaAidlInterface.Stub mBinder = new IMediaAidlInterface.Stub() {
+ @Override
+ public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) {
+ Timber.d("[%s] basicTypes called", TAG);
+ }
+
+ @Override
+ public void startMedia(MediaArgu media) throws RemoteException {
+ Timber.d("[%s] startMedia called via AIDL", TAG);
+ P2UsbCameraVideoService.this.startMedia(media);
+ }
+
+ @Override
+ public void stopMedia(MediaArgu media) throws RemoteException {
+ Timber.d("[%s] stopMedia called via AIDL", TAG);
+ P2UsbCameraVideoService.this.stopMedia(media);
+ }
+
+ @Override
+ public void registerCallback(IMyCallback cb) throws RemoteException {
+ if (cb != null) {
+ mCallbacks.register(cb);
+ Timber.d("[%s] Callback registered", TAG);
+ }
+ }
+
+ @Override
+ public void unregisterCallback(IMyCallback cb) throws RemoteException {
+ if (cb != null) {
+ mCallbacks.unregister(cb);
+ Timber.d("[%s] Callback unregistered", TAG);
+ }
+ }
+ };
+
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ context = this;
+
+ usbCameraPushManager = new UsbCameraPushManager(context);
+ usbCameraPushManager.setCallback(this::notifyCallback);
+
+ usbCameraRecordManager = new UsbCameraRecordManager(context);
+ usbCameraRecordManager.setCallback(this::notifyCallback);
+
+ Timber.d("[%s] onCreate", TAG);
+ }
+
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ return START_STICKY;
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ Timber.d("[%s] onBind", TAG);
+ return mBinder;
+ }
+
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+ stopCurrentManager();
+ mCallbacks.kill();
+ Timber.d("[%s] onDestroy", TAG);
+ }
+
+ private void startMedia(MediaArgu media) {
+ if (media == null) {
+ notifyCallback(1, -1, "MediaArgu is null");
+ return;
+ }
+
+ // 璇ユ湇鍔″彧澶勭悊 USB 鎽勫儚澶�
+ if (!media.isUsedOutCamera()) {
+ notifyCallback(1, -4, "P2 service only supports USB camera");
+ return;
+ }
+
+ // 鍏堝仠姝㈠綋鍓�
+ stopCurrentManager();
+
+ if (media.isPush()) {
+ currentManagerType = ManagerType.USB_PUSH;
+ usbCameraPushManager.startPush(media);
+ } else {
+ currentManagerType = ManagerType.USB_RECORD;
+ usbCameraRecordManager.startRecord(media);
+ }
+ }
+
+ private void stopMedia(MediaArgu media) {
+ stopCurrentManager();
+ }
+
+ private void stopCurrentManager() {
+ switch (currentManagerType) {
+ case USB_PUSH:
+ if (usbCameraPushManager != null) {
+ usbCameraPushManager.stopPush();
+ }
+ break;
+ case USB_RECORD:
+ if (usbCameraRecordManager != null) {
+ usbCameraRecordManager.stopRecord();
+ }
+ break;
+ case NONE:
+ break;
+ }
+ currentManagerType = ManagerType.NONE;
+ }
+
+ private void notifyCallback(ResponseVO response) {
+ if (response == null) return;
+ int count = mCallbacks.beginBroadcast();
+ for (int i = 0; i < count; i++) {
+ try {
+ mCallbacks.getBroadcastItem(i).onResult(response);
+ } catch (RemoteException e) {
+ Timber.e(e, "[%s] Error notifying callback", TAG);
+ }
+ }
+ mCallbacks.finishBroadcast();
+ }
+
+ private void notifyCallback(int type, int errCode, String message) {
+ ResponseVO response = new ResponseVO();
+ response.setType(type);
+ response.setErrCode(errCode);
+ response.setMessage(message);
+ notifyCallback(response);
+ }
+}
+
diff --git a/app/src/main/java/com/safeluck/floatwindow/manager/UsbCameraPushManager.java b/app/src/main/java/com/safeluck/floatwindow/manager/UsbCameraPushManager.java
index c362f04..7a82c9f 100644
--- a/app/src/main/java/com/safeluck/floatwindow/manager/UsbCameraPushManager.java
+++ b/app/src/main/java/com/safeluck/floatwindow/manager/UsbCameraPushManager.java
@@ -591,9 +591,9 @@
int usbId = (mediaArgu != null) ? mediaArgu.getUsbCameraId() : 0;
int[] cameraIds;
if (usbId == 2) {
- cameraIds = new int[]{2};
+ cameraIds = new int[]{2,3};
} else if (usbId == 1) {
- cameraIds = new int[]{0};
+ cameraIds = new int[]{1,2};
} else {
cameraIds = new int[]{0, 2};
}
--
Gitblit v1.8.0