From 8b004066992c3ff21348a7bfa44042d44d0b7088 Mon Sep 17 00:00:00 2001
From: Dana <Dana_Lee1016@126.com>
Date: 星期三, 28 一月 2026 17:30:09 +0800
Subject: [PATCH] 1.p2摄像头 跨进程服务,未测试

---
 app/src/main/java/com/safeluck/floatwindow/FloatingService.java |  108 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 107 insertions(+), 1 deletions(-)

diff --git a/app/src/main/java/com/safeluck/floatwindow/FloatingService.java b/app/src/main/java/com/safeluck/floatwindow/FloatingService.java
index d634ab8..c714a00 100644
--- a/app/src/main/java/com/safeluck/floatwindow/FloatingService.java
+++ b/app/src/main/java/com/safeluck/floatwindow/FloatingService.java
@@ -1,7 +1,9 @@
 package com.safeluck.floatwindow;
 
 import android.app.Service;
+import android.content.ComponentName;
 import android.content.Context;
+import android.content.ServiceConnection;
 import android.content.Intent;
 import android.os.IBinder;
 import android.os.RemoteCallbackList;
@@ -32,6 +34,50 @@
     
     // 褰撳墠浣跨敤鐨勭鐞嗗櫒绫诲瀷
     private ManagerType currentManagerType = ManagerType.NONE;
+
+    // P2 璺ㄨ繘绋嬫湇鍔★紙鐢ㄤ簬 usbCameraId == 2锛�
+    private IMediaAidlInterface p2Service;
+    private boolean p2Bound = false;
+    private MediaArgu pendingP2StartMedia;
+
+    private final IMyCallback p2Callback = new IMyCallback.Stub() {
+        @Override
+        public void onResult(ResponseVO re) throws RemoteException {
+            // 灏� P2 杩涚▼鍥炶皟杞彂缁欏鎴风
+            notifyCallback(re);
+        }
+    };
+
+    private final ServiceConnection p2Connection = new ServiceConnection() {
+        @Override
+        public void onServiceConnected(ComponentName name, IBinder service) {
+            p2Service = IMediaAidlInterface.Stub.asInterface(service);
+            p2Bound = true;
+            Timber.d("P2UsbCameraVideoService connected");
+            try {
+                p2Service.registerCallback(p2Callback);
+            } catch (RemoteException e) {
+                Timber.e(e, "Failed to register p2Callback");
+            }
+
+            // 濡傛灉鏈� pending startMedia锛岃繛鎺ュ悗绔嬪埢鎵ц
+            if (pendingP2StartMedia != null) {
+                try {
+                    p2Service.startMedia(pendingP2StartMedia);
+                    pendingP2StartMedia = null;
+                } catch (RemoteException e) {
+                    Timber.e(e, "Failed to startMedia on P2 service");
+                }
+            }
+        }
+
+        @Override
+        public void onServiceDisconnected(ComponentName name) {
+            Timber.w("P2UsbCameraVideoService disconnected");
+            p2Bound = false;
+            p2Service = null;
+        }
+    };
     
     /**
      * 绠$悊鍣ㄧ被鍨嬫灇涓�
@@ -40,7 +86,9 @@
         NONE,
         USB_PUSH,
         USB_RECORD,
-        ANDROID_RECORD
+        ANDROID_RECORD,
+        P2_USB_PUSH,
+        P2_USB_RECORD
     }
     
     // AIDL Binder
@@ -116,6 +164,16 @@
         
         Timber.d("FloatingService onCreate");
     }
+
+    private void ensureP2Bound() {
+        if (p2Bound) return;
+        Intent intent = new Intent(this, P2UsbCameraVideoService.class);
+        try {
+            bindService(intent, p2Connection, Context.BIND_AUTO_CREATE);
+        } catch (Exception e) {
+            Timber.e(e, "bindService P2UsbCameraVideoService failed");
+        }
+    }
     
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
@@ -129,6 +187,26 @@
         if (media == null) {
             Timber.w("startMedia: media is null");
             notifyCallback(1, -1, "MediaArgu is null");
+            return;
+        }
+
+        // usbCameraId == 2锛氳蛋 P2 璺ㄨ繘绋嬫湇鍔★紝鏀寔涓よ矾 USB 鍚屾椂宸ヤ綔
+        if (media.isUsedOutCamera() && media.getUsbCameraId() == 2) {
+            stopCurrentManager();
+            ensureP2Bound();
+            if (p2Service != null) {
+                try {
+                    p2Service.startMedia(media);
+                    currentManagerType = media.isPush() ? ManagerType.P2_USB_PUSH : ManagerType.P2_USB_RECORD;
+                } catch (RemoteException e) {
+                    Timber.e(e, "startMedia forward to P2 failed");
+                    notifyCallback(1, -3, "鍚姩P2鏈嶅姟澶辫触: " + e.getMessage());
+                }
+            } else {
+                // 绛夊緟杩炴帴瀹屾垚鍚庢墽琛�
+                pendingP2StartMedia = media;
+                currentManagerType = media.isPush() ? ManagerType.P2_USB_PUSH : ManagerType.P2_USB_RECORD;
+            }
             return;
         }
         
@@ -183,6 +261,18 @@
                     androidCameraRecordManager.stopRecord();
                 }
                 break;
+            case P2_USB_PUSH:
+            case P2_USB_RECORD:
+                if (p2Service != null) {
+                    try {
+                        p2Service.stopMedia();
+                    } catch (RemoteException e) {
+                        Timber.e(e, "stopMedia forward to P2 failed");
+                    }
+                } else {
+                    pendingP2StartMedia = null;
+                }
+                break;
             case NONE:
                 break;
         }
@@ -208,6 +298,22 @@
         super.onDestroy();
         stopMedia();
         mCallbacks.kill();
+        if (p2Bound) {
+            try {
+                if (p2Service != null) {
+                    p2Service.unregisterCallback(p2Callback);
+                }
+            } catch (RemoteException e) {
+                Timber.e(e, "Failed to unregister p2Callback");
+            }
+            try {
+                unbindService(p2Connection);
+            } catch (Exception e) {
+                Timber.w(e, "unbindService P2 failed");
+            }
+            p2Bound = false;
+            p2Service = null;
+        }
         Timber.d("FloatingService onDestroy");
     }
     

--
Gitblit v1.8.0