Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New camera feature issues on some devices #4392

Closed
2 tasks done
jgonyea opened this issue Nov 1, 2023 · 62 comments
Closed
2 tasks done

New camera feature issues on some devices #4392

jgonyea opened this issue Nov 1, 2023 · 62 comments

Comments

@jgonyea
Copy link

jgonyea commented Nov 1, 2023

  • I have read the FAQ.
  • I have searched in existing issues.

Environment

  • OS: Ubuntu 22.04
  • scrcpy version: 2.2
  • installation method: manual build
  • device model: OnePlus 8T
  • Android version: 13

Describe the bug
I'm attempting to use the new camera feature on my OnePlus. I used the pre-built server and then manually built the client. The main phone screen display works fine, but I get the following message when I attempt to use the camera mirroring feature:

scrcpy  --video-source=camera --camera-id=0           ──(Wed,Nov01)─┘
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: Camera video source: control disabled
INFO: Camera video source: microphone audio source selected
INFO: ADB device found:
INFO:     -->   (usb)  953a9722                        device  KB2005
/usr/local/share/scrcpy/scrcpy-server: 1 file pushed. 5.1 MB/s (64363 bytes in 0.012s)
[server] INFO: Device: [OnePlus] OnePlus KB2005 (Android 13)
INFO: Renderer: opengl
INFO: OpenGL version: 4.6 (Compatibility Profile) Mesa 23.0.4-0ubuntu1~22.04.1
INFO: Trilinear filtering enabled
[server] INFO: Using camera '0'
[server] ERROR: Exception on thread Thread[camera,5,main]
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Application.getApplicationContext()' on a null object reference
	at android.hardware.camera2.OplusCamera2StatisticsManager.addInfo(OplusCamera2StatisticsManager.java:69)
	at android.hardware.camera2.impl.CameraDeviceImplExtImpl.extendsetInfo(CameraDeviceImplExtImpl.java:47)
	at android.hardware.camera2.impl.CameraDeviceImpl$1.run(CameraDeviceImpl.java:190)
	at android.os.Handler.handleCallback(Handler.java:942)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loopOnce(Looper.java:240)
	at android.os.Looper.loop(Looper.java:351)
	at android.os.HandlerThread.run(HandlerThread.java:67)
INFO: Texture: 4000x3000

Attempting to use other camera id's presents similar console messages.


EDIT by @rom1v: it is now fixed, upgrade to version 2.3.

old EDIT by @rom1v: the problem will be fixed in the next release.

Meanwhile, you can download this file and replace it in the release of scrcpy v2.2:

  • scrcpy-server SHA-256: 97423bebd9e897f27ae4c0e81699e7cd04338c1a84d0fa9995d2ce29f9fc8bce

(built from branch camerafix)

Please tell us if it fixes your problem or not (or if it creates other issues).

@jgonyea
Copy link
Author

jgonyea commented Nov 1, 2023

scrcpy --list-cameras                                                                                                                              ──(Wed,Nov01)─┘
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: ADB device found:
INFO:     -->   (usb)  953a9722                        device  KB2005
/usr/local/share/scrcpy/scrcpy-server: 1 file pushed. 4.8 MB/s (64363 bytes in 0.013s)
[server] INFO: Device: [OnePlus] OnePlus KB2005 (Android 13)
[server] INFO: List of cameras:
    --video-source=camera --camera-id=0    (back, 4000x3000, fps=[15, 30])
    --video-source=camera --camera-id=1    (front, 2328x1748, fps=[15, 30])
    --video-source=camera --camera-id=2    (back, 4656x3496, fps=[15, 30])
    --video-source=camera --camera-id=3    (back, 2592x1944, fps=[15, 29])
    --video-source=camera --camera-id=4    (back, 1600x1200, fps=[15, 30])
    --video-source=camera --camera-id=5    (back, 8000x6000, fps=[15, 30])
    --video-source=camera --camera-id=6    (back, 8000x6000, fps=[15, 30])
    --video-source=camera --camera-id=7    (back, 8000x6000, fps=[15, 30])

@rom1v
Copy link
Collaborator

rom1v commented Nov 1, 2023

Please try to force mustFillAppContext to true in the source code:

boolean mustFillAppContext = false;

@stranno
Copy link

stranno commented Nov 1, 2023

I have tried the camera feature with a OnePlus 7 and a Fold 4. It works fine on the Fold, but got the same error on the OnePlus 7. I haven't compiled it yet with the change.

@zxc7895531
Copy link

zxc7895531 commented Nov 2, 2023

My device is Redmi Note 11T Pro(xaga), runing Android 12, and I tried build my self use boolean mustFillAppContext = true; and it's can't use too.

D:\Tools\scrcpy-win64>scrcpy.exe --video-source=camera

scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: Camera video source: control disabled
INFO: Camera video source: microphone audio source selected
INFO: ADB device found:
INFO:     --> (tcpip)     192.168.1.25:5555            device  22041216C
D:\Tools\scrcpy-win64\scrcpy-server: 1 file pushed, 0 skipped. 144.3 MB/s (64363 bytes in 0.000s)
[server] INFO: Device: [Xiaomi] Redmi 22041216C (Android 12)
[server] ERROR: Exception on thread Thread[video,5,main]
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.hardware.camera2.CameraManager.getCameraCharacteristics(CameraManager.java:491)
        at com.genymobile.scrcpy.CameraCapture.selectSize(CameraCapture.java:126)
        at com.genymobile.scrcpy.CameraCapture.init(CameraCapture.java:80)
        at com.genymobile.scrcpy.SurfaceEncoder.streamScreen(SurfaceEncoder.java:55)
        at com.genymobile.scrcpy.SurfaceEncoder.lambda$start$0$com-genymobile-scrcpy-SurfaceEncoder(SurfaceEncoder.java:253)
        at com.genymobile.scrcpy.SurfaceEncoder$$ExternalSyntheticLambda0.run(Unknown Source:4)
        at java.lang.Thread.run(Thread.java:920)
INFO: Renderer: direct3d
ERROR: Demuxer 'video': stream disabled due to connection error
ERROR: Demuxer 'audio': stream disabled due to connection error
ERROR: Demuxer error

@rom1v

@arunpt
Copy link

arunpt commented Nov 2, 2023

I've tried the same on Poco F4 (much) running on Evolution X A13 which is also having the same issue (traceback attached below).

Available cameras

O:\utils\scrcpy-win64-v2.2>scrcpy --list-cameras
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: ADB device found:
INFO:     -->   (usb)              57ca7a63            device  22021211RI
O:\utils\scrcpy-win64-v2.2\scrcpy-server: 1 file pushed, 0 skipped. 9.5 MB/s (64363 bytes in 0.006s)
[server] INFO: Device: [Xiaomi] POCO 22021211RI (Android 13)
[server] INFO: List of cameras:
    --video-source=camera --camera-id=0    (back, 4624x3472, fps=[15, 24, 25, 30])
    --video-source=camera --camera-id=1    (front, 2592x1952, fps=[15, 24, 25, 30])
    --video-source=camera --camera-id=2    (back, 3264x2448, fps=[15, 24, 25, 30])
    --video-source=camera --camera-id=3    (back, 1600x1200, fps=[15, 24, 25, 30])
    --video-source=camera --camera-id=4    (back, 4624x3472, fps=[15, 24, 25, 30])
    --video-source=camera --camera-id=5    (back, 4624x3472, fps=[15, 24, 25, 30])
    --video-source=camera --camera-id=6    (front, 2592x1952, fps=[15, 24, 25, 30])

Starting camera

O:\utils\scrcpy-win64-v2.2>scrcpy.exe --video-source=camera
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: Camera video source: control disabled
INFO: Camera video source: microphone audio source selected
INFO: ADB device found:
INFO:     -->   (usb)              57ca7a63            device  22021211RI
O:\utils\scrcpy-win64-v2.2\scrcpy-server: 1 file pushed, 0 skipped. 145.2 MB/s (64363 bytes in 0.000s)
[server] INFO: Device: [Xiaomi] POCO 22021211RI (Android 13)
[server] INFO: Using camera '0'
[server] ERROR: Exception on thread Thread[video,5,main]
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.hardware.camera2.impl.CameraDeviceImpl.checkPrivilegedAppList(CameraDeviceImpl.java:1536)
        at android.hardware.camera2.impl.CameraDeviceImpl.<init>(CameraDeviceImpl.java:311)
        at android.hardware.camera2.CameraManager.openCameraDeviceUserAsync(CameraManager.java:742)
        at android.hardware.camera2.CameraManager.openCameraForUid(CameraManager.java:1027)
        at android.hardware.camera2.CameraManager.openCameraForUid(CameraManager.java:1048)
        at android.hardware.camera2.CameraManager.openCamera(CameraManager.java:886)
        at com.genymobile.scrcpy.CameraCapture.openCamera(CameraCapture.java:241)
        at com.genymobile.scrcpy.CameraCapture.init(CameraCapture.java:86)
        at com.genymobile.scrcpy.SurfaceEncoder.streamScreen(SurfaceEncoder.java:55)
        at com.genymobile.scrcpy.SurfaceEncoder.lambda$start$0$com-genymobile-scrcpy-SurfaceEncoder(SurfaceEncoder.java:253)
        at com.genymobile.scrcpy.SurfaceEncoder$$ExternalSyntheticLambda0.run(Unknown Source:4)
        at java.lang.Thread.run(Thread.java:1012)
[server] ERROR: Audio capture error
java.lang.InterruptedException
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:2056)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2090)
        at java.util.concurrent.ArrayBlockingQueue.take(ArrayBlockingQueue.java:417)
        at com.genymobile.scrcpy.AudioEncoder.inputThread(AudioEncoder.java:94)
        at com.genymobile.scrcpy.AudioEncoder.lambda$encode$1$com-genymobile-scrcpy-AudioEncoder(AudioEncoder.java:198)
        at com.genymobile.scrcpy.AudioEncoder$$ExternalSyntheticLambda1.run(Unknown Source:4)
        at java.lang.Thread.run(Thread.java:1012)
INFO: Renderer: direct3d
ERROR: Demuxer 'video': stream disabled due to connection error
ERROR: Demuxer 'audio': stream disabled due to connection error
ERROR: Demuxer error

@atomofiron
Copy link

atomofiron commented Nov 2, 2023

OS: MacOS Ventura 13.4 (Intel CPU)
source: brew
device: Oneplus 7T PixelExperiance Android 12
device: Nothing Phone(1) Android 13
framework.jar: Google Drive (access by link)
terminal-oneplus-7t-pixel-experience-android-12.txt
terminal-nothing-phone-1-android-13.txt

@rom1v
Copy link
Collaborator

rom1v commented Nov 2, 2023

Thank you for your reports and stack traces 👍

So, that's great: each reported device here fails in a different way (different stack trace). 🙃

Please post your framework.jar:

adb pull /system/framework/framework.jar

@zxc7895531
Copy link

Thank you for your reports and stack traces 👍

So, that's great: each reported device here fails in a different way (different stack trace). 🙃

Please post your framework.jar:

adb pull /system/framework/framework.jar

framework.jar.zip
Hello, this is my framework.jar, from Redmi Note 11T Pro(xaga)

@rom1v
Copy link
Collaborator

rom1v commented Nov 2, 2023

@zxc7895531

Ahah, it seems they lie to pass the CTS 😆

                    String packageName = ActivityThread.currentOpPackageName();
                    String device_build_name = Build.DEVICE;
                    if (packageName.equals("com.android.cts.verifier") && device_build_name.contains("xaga")) {

But since packageName is null, you get:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.hardware.camera2.CameraManager.getCameraCharacteristics(CameraManager.java:491)

This might be the same problem in the stacktrace from @arunpt:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.hardware.camera2.impl.CameraDeviceImpl.checkPrivilegedAppList(CameraDeviceImpl.java:1536)

Could you please try with this quick-and-dirty patch:

diff --git a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java
index b8ee68ca5..a3330c160 100644
--- a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java
+++ b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java
@@ -123,6 +123,16 @@ public final class Workarounds {
             ApplicationInfo applicationInfo = new ApplicationInfo();
             applicationInfo.packageName = FakeContext.PACKAGE_NAME;
 
+            Application application = new Application() {
+                @Override
+                public String getOpPackageName() {
+                    return FakeContext.PACKAGE_NAME;
+                }
+            };
+            Field initialApplicationField = activityThreadClass.getDeclaredField("mInitialApplication");
+            initialApplicationField.setAccessible(true);
+            initialApplicationField.set(activityThread, application);
+
             // appBindData.appInfo = applicationInfo;
             Field appInfoField = appBindDataClass.getDeclaredField("appInfo");
             appInfoField.setAccessible(true);

@zxc7895531
Copy link

@zxc7895531

Ahah, it seems they lie to pass the CTS 😆

                    String packageName = ActivityThread.currentOpPackageName();
                    String device_build_name = Build.DEVICE;
                    if (packageName.equals("com.android.cts.verifier") && device_build_name.contains("xaga")) {

But since packageName is null, you get:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.hardware.camera2.CameraManager.getCameraCharacteristics(CameraManager.java:491)

This might be the same problem in the stacktrace from @arunpt:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.hardware.camera2.impl.CameraDeviceImpl.checkPrivilegedAppList(CameraDeviceImpl.java:1536)

Could you please try with this quick-and-dirty patch:

diff --git a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java
index b8ee68ca5..a3330c160 100644
--- a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java
+++ b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java
@@ -123,6 +123,16 @@ public final class Workarounds {
             ApplicationInfo applicationInfo = new ApplicationInfo();
             applicationInfo.packageName = FakeContext.PACKAGE_NAME;
 
+            Application application = new Application() {
+                @Override
+                public String getOpPackageName() {
+                    return FakeContext.PACKAGE_NAME;
+                }
+            };
+            Field initialApplicationField = activityThreadClass.getDeclaredField("mInitialApplication");
+            initialApplicationField.setAccessible(true);
+            initialApplicationField.set(activityThread, application);
+
             // appBindData.appInfo = applicationInfo;
             Field appInfoField = appBindDataClass.getDeclaredField("appInfo");
             appInfoField.setAccessible(true);

Hello, The --video-source and --camera-id args can be use now! But now there are still some issues. When I use the --list cameras args, it still encounters errors. This is the stacktrace:

D:\Tools\scrcpy-win64>scrcpy --list-cameras
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: ADB device found:
INFO:     --> (tcpip)     192.168.1.25:5555            device  22041216C
D:\Tools\scrcpy-win64\scrcpy-server: 1 file pushed, 0 skipped. 92.5 MB/s (64571 bytes in 0.001s)
[server] INFO: Device: [Xiaomi] Redmi 22041216C (Android 12)
[server] ERROR: android.app.AppOpsManager
java.lang.NoClassDefFoundError: android.app.AppOpsManager
        at android.os.BinderProxy.transact(BinderProxy.java:622)
        at android.os.IServiceManager$Stub$Proxy.checkService(IServiceManager.java:416)
        at android.os.ServiceManagerProxy.getService(ServiceManagerNative.java:63)
        at android.os.ServiceManager.rawGetService(ServiceManager.java:335)
        at android.os.ServiceManager.getService(ServiceManager.java:134)
        at android.hardware.camera2.CameraManager$CameraManagerGlobal.connectCameraServiceLocked(CameraManager.java:1398)
        at android.hardware.camera2.CameraManager$CameraManagerGlobal.getCameraIdList(CameraManager.java:1634)
        at android.hardware.camera2.CameraManager.getCameraIdList(CameraManager.java:125)
        at com.genymobile.scrcpy.LogUtils.buildCameraListMessage(LogUtils.java:91)
        at com.genymobile.scrcpy.Server.internalMain(Server.java:228)
        at com.genymobile.scrcpy.Server.main(Server.java:190)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:384)
Caused by: java.lang.ExceptionInInitializerError
        at android.os.BinderProxy.transact(BinderProxy.java:622)
        at android.os.IServiceManager$Stub$Proxy.checkService(IServiceManager.java:416)
        at android.os.ServiceManagerProxy.getService(ServiceManagerNative.java:63)
        at android.os.ServiceManager.rawGetService(ServiceManager.java:335)
        at android.os.ServiceManager.getService(ServiceManager.java:134)
        at android.hardware.display.DisplayManagerGlobal.getInstance(DisplayManagerGlobal.java:149)
        at android.app.ResourcesManager.getDisplayMetrics(ResourcesManager.java:352)
        at android.app.ResourcesManager.getDisplayMetrics(ResourcesManager.java:344)
        at android.app.ContextImpl.createSystemContext(ContextImpl.java:2985)
        at android.app.ActivityThread.getSystemContext(ActivityThread.java:2739)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.genymobile.scrcpy.Workarounds.fillBaseContext(Workarounds.java:176)
        at com.genymobile.scrcpy.Workarounds.apply(Workarounds.java:76)
        at com.genymobile.scrcpy.Server.internalMain(Server.java:227)
        ... 3 more
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
        at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:105)
        at android.app.AppOpsManager.getSystemAlertWindowDefault(AppOpsManager.java:10378)
        at android.app.AppOpsManager.<clinit>(AppOpsManager.java:2740)
        ... 17 more

And I find some other issues about --video-source=camera, I'll open another issue to discuss it.

@zxc7895531
Copy link

And this is my scrcpy-server

scrcpy-server.zip

@arunpt
Copy link

arunpt commented Nov 2, 2023

Thank you for your reports and stack traces 👍

So, that's great: each reported device here fails in a different way (different stack trace). 🙃

Please post your framework.jar:

adb pull /system/framework/framework.jar

framework.jar.munch.zip
This is the framework.jar from Poco F4 (munch) [Evox AOSP]

@rom1v rom1v changed the title New camera feature issue on OnePlus 8t New camera feature issues on some devices Nov 2, 2023
rom1v added a commit that referenced this issue Nov 3, 2023
@rom1v
Copy link
Collaborator

rom1v commented Nov 3, 2023

@zxc7895531

But now there are still some issues.

Some more quick-and-dirty hacks, please test branch fix4392zxc.1.

@zxc7895531
Copy link

@zxc7895531

But now there are still some issues.

Some more quick-and-dirty hacks, please test branch fix4392zxc.1.

Thanks, but there is error too.🙃

D:\Tools\scrcpy-win64>scrcpy --list-cameras
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: ADB device found:
INFO:     --> (tcpip)     192.168.1.25:5555            device  22041216C
D:\Tools\scrcpy-win64\scrcpy-server: 1 file pushed, 0 skipped. 141.7 MB/s (64687 bytes in 0.000s)
[server] INFO: Device: [Xiaomi] Redmi 22041216C (Android 12)
[server] ERROR: Cannot retrieve system context
java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at com.genymobile.scrcpy.Workarounds.retrieveSystemContext(Workarounds.java:330)
        at com.genymobile.scrcpy.FakeContext.<init>(FakeContext.java:21)
        at com.genymobile.scrcpy.FakeContext.<clinit>(FakeContext.java:14)
        at com.genymobile.scrcpy.FakeContext.get(FakeContext.java:17)
        at com.genymobile.scrcpy.wrappers.ServiceManager.getCameraManager(ServiceManager.java:143)
        at com.genymobile.scrcpy.LogUtils.buildCameraListMessage(LogUtils.java:89)
        at com.genymobile.scrcpy.Server.internalMain(Server.java:228)
        at com.genymobile.scrcpy.Server.main(Server.java:190)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:384)
Caused by: java.lang.NoClassDefFoundError: android.app.AppOpsManager
        at android.os.BinderProxy.transact(BinderProxy.java:622)
        at android.os.IServiceManager$Stub$Proxy.checkService(IServiceManager.java:416)
        at android.os.ServiceManagerProxy.getService(ServiceManagerNative.java:63)
        at android.os.ServiceManager.rawGetService(ServiceManager.java:335)
        at android.os.ServiceManager.getService(ServiceManager.java:134)
        at android.hardware.display.DisplayManagerGlobal.getInstance(DisplayManagerGlobal.java:149)
        at android.app.ResourcesManager.getDisplayMetrics(ResourcesManager.java:352)
        at android.app.ResourcesManager.getDisplayMetrics(ResourcesManager.java:344)
        at android.app.ContextImpl.createSystemContext(ContextImpl.java:2985)
        at android.app.ActivityThread.getSystemContext(ActivityThread.java:2739)
        ... 11 more
Caused by: java.lang.ExceptionInInitializerError
        at android.os.BinderProxy.transact(BinderProxy.java:622)
        at android.os.IServiceManager$Stub$Proxy.checkService(IServiceManager.java:416)
        at android.os.ServiceManagerProxy.getService(ServiceManagerNative.java:63)
        at android.os.ServiceManager.rawGetService(ServiceManager.java:335)
        at android.os.ServiceManager.getService(ServiceManager.java:134)
        at android.hardware.display.DisplayManagerGlobal.getInstance(DisplayManagerGlobal.java:149)
        at android.app.ResourcesManager.getDisplayMetrics(ResourcesManager.java:352)
        at android.app.ResourcesManager.getDisplayMetrics(ResourcesManager.java:344)
        at android.app.ContextImpl.createSystemContext(ContextImpl.java:2985)
        at android.app.ActivityThread.getSystemContext(ActivityThread.java:2739)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.genymobile.scrcpy.Workarounds.fillBaseContext(Workarounds.java:183)
        at com.genymobile.scrcpy.Workarounds.apply(Workarounds.java:82)
        at com.genymobile.scrcpy.Server.internalMain(Server.java:227)
        ... 3 more
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
        at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:105)
        at android.app.AppOpsManager.getSystemAlertWindowDefault(AppOpsManager.java:10378)
        at android.app.AppOpsManager.<clinit>(AppOpsManager.java:2740)
        ... 17 more
[server] ERROR: android.app.AppOpsManager
java.lang.NoClassDefFoundError: android.app.AppOpsManager
        at android.os.BinderProxy.transact(BinderProxy.java:622)
        at android.os.IServiceManager$Stub$Proxy.checkService(IServiceManager.java:416)
        at android.os.ServiceManagerProxy.getService(ServiceManagerNative.java:63)
        at android.os.ServiceManager.rawGetService(ServiceManager.java:335)
        at android.os.ServiceManager.getService(ServiceManager.java:134)
        at android.hardware.camera2.CameraManager$CameraManagerGlobal.connectCameraServiceLocked(CameraManager.java:1398)
        at android.hardware.camera2.CameraManager$CameraManagerGlobal.getCameraIdList(CameraManager.java:1634)
        at android.hardware.camera2.CameraManager.getCameraIdList(CameraManager.java:125)
        at com.genymobile.scrcpy.LogUtils.buildCameraListMessage(LogUtils.java:91)
        at com.genymobile.scrcpy.Server.internalMain(Server.java:228)
        at com.genymobile.scrcpy.Server.main(Server.java:190)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:384)
Caused by: java.lang.ExceptionInInitializerError
        at android.os.BinderProxy.transact(BinderProxy.java:622)
        at android.os.IServiceManager$Stub$Proxy.checkService(IServiceManager.java:416)
        at android.os.ServiceManagerProxy.getService(ServiceManagerNative.java:63)
        at android.os.ServiceManager.rawGetService(ServiceManager.java:335)
        at android.os.ServiceManager.getService(ServiceManager.java:134)
        at android.hardware.display.DisplayManagerGlobal.getInstance(DisplayManagerGlobal.java:149)
        at android.app.ResourcesManager.getDisplayMetrics(ResourcesManager.java:352)
        at android.app.ResourcesManager.getDisplayMetrics(ResourcesManager.java:344)
        at android.app.ContextImpl.createSystemContext(ContextImpl.java:2985)
        at android.app.ActivityThread.getSystemContext(ActivityThread.java:2739)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.genymobile.scrcpy.Workarounds.fillBaseContext(Workarounds.java:183)
        at com.genymobile.scrcpy.Workarounds.apply(Workarounds.java:82)
        at com.genymobile.scrcpy.Server.internalMain(Server.java:227)
        ... 3 more
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
        at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:105)
        at android.app.AppOpsManager.getSystemAlertWindowDefault(AppOpsManager.java:10378)
        at android.app.AppOpsManager.<clinit>(AppOpsManager.java:2740)
        ... 17 more

@rom1v
Copy link
Collaborator

rom1v commented Nov 3, 2023

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:105)

I'm very surprised that you get this error on that branch.

Could you please log what context you retrieve to pass to the FakeContext constructor, with this additional diff:

diff --git a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java
index 37240246e..e31b598d0 100644
--- a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java
+++ b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java
@@ -327,7 +327,9 @@ public final class Workarounds {
     static Context retrieveSystemContext() {
         try {
             Method getSystemContextMethod = activityThreadClass.getDeclaredMethod("getSystemContext");
-            return (Context) getSystemContextMethod.invoke(activityThread);
+            Context ctx = (Context) getSystemContextMethod.invoke(activityThread);
+            Ln.i("===== " + ctx);
+            return ctx;
         } catch (Exception e) {
             Ln.e("Cannot retrieve system context", e);
             return null;

@zxc7895531
Copy link

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:105)

I'm very surprised that you get this error on that branch.

Could you please log what context you retrieve to pass to the FakeContext constructor, with this additional diff:

diff --git a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java
index 37240246e..e31b598d0 100644
--- a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java
+++ b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java
@@ -327,7 +327,9 @@ public final class Workarounds {
     static Context retrieveSystemContext() {
         try {
             Method getSystemContextMethod = activityThreadClass.getDeclaredMethod("getSystemContext");
-            return (Context) getSystemContextMethod.invoke(activityThread);
+            Context ctx = (Context) getSystemContextMethod.invoke(activityThread);
+            Ln.i("===== " + ctx);
+            return ctx;
         } catch (Exception e) {
             Ln.e("Cannot retrieve system context", e);
             return null;

I just tried this diff, but the error message returned did not change anything. I don't know java but I guess the error may have occurred earlier.

@D3SOX
Copy link

D3SOX commented Nov 3, 2023

I got a different error which wasn't mentioned yet on my OnePlus 8T using Lineage for microG 20 (Android 13)

scrcpy --list-cameras
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: ADB device found:
INFO:     -->   (usb)  35311d0d                        device  KB2003
/usr/share/scrcpy/scrcpy-server: 1 file pushed, 0 skipped. 777.2 MB/s (64363 bytes in 0.000s)
[server] INFO: Device: [OnePlus] OnePlus KB2003 (Android 13)
[server] ERROR: Attempt to invoke virtual method 'android.content.res.Resources android.app.Application.getResources()' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.app.Application.getResources()' on a null object reference
        at android.hardware.Camera.shouldExposeAuxCamera(Camera.java:282)
        at android.hardware.camera2.CameraManager$CameraManagerGlobal.onStatusChangedLocked(CameraManager.java:2298)
        at android.hardware.camera2.CameraManager$CameraManagerGlobal.connectCameraServiceLocked(CameraManager.java:1713)
        at android.hardware.camera2.CameraManager$CameraManagerGlobal.getCameraIdList(CameraManager.java:1931)
        at android.hardware.camera2.CameraManager.getCameraIdList(CameraManager.java:246)
        at com.genymobile.scrcpy.LogUtils.buildCameraListMessage(LogUtils.java:91)
        at com.genymobile.scrcpy.Server.internalMain(Server.java:228)
        at com.genymobile.scrcpy.Server.main(Server.java:190)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:355)

Edit: Using branch fix4392zxc.3 doesn't change anything for me. Let me know if I should open a separate issue for this.

@rom1v
Copy link
Collaborator

rom1v commented Nov 3, 2023

@zxc7895531 I updated fix4392zxc.1, please test the new version 😉

EDIT: then please test fix4392zxc.2.

@zxc7895531
Copy link

@zxc7895531 I updated fix4392zxc.1, please test the new version 😉

I just tried now, --list-cameras can be use now, but --video-source=camera issues come back, even when I use 65d6bdb2

D:\Tools\scrcpy-win64>scrcpy --video-source=camera --camera-size=1920x1080
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: Camera video source: control disabled
INFO: Camera video source: microphone audio source selected
INFO: ADB device found:
INFO:     --> (tcpip)     192.168.1.25:5555            device  22041216C
D:\Tools\scrcpy-win64\scrcpy-server: 1 file pushed, 0 skipped. 99.7 MB/s (64687 bytes in 0.001s)
[server] INFO: Device: [Xiaomi] Redmi 22041216C (Android 12)
[server] INFO: Using camera '0'
[server] ERROR: Exception on thread Thread[video,5,main]
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.content.ContextWrapper.getPackageName(ContextWrapper.java:162)
        at android.hardware.CameraInjector.notifyCameraStateChange(CameraInjector.java:551)
        at android.hardware.CameraInjector.notifyCameraStateChange(CameraInjector.java:537)
        at android.hardware.camera2.impl.CameraDeviceImplInjector.initCamera(CameraDeviceImplInjector.java:140)
        at android.hardware.camera2.impl.CameraExtImplXiaoMi.initCameraDevice(CameraExtImplXiaoMi.java:58)
        at android.hardware.camera2.impl.CameraExtStub.initCameraDevice(CameraExtStub.java:103)
        at android.hardware.camera2.impl.CameraDeviceImpl.<init>(CameraDeviceImpl.java:304)
        at android.hardware.camera2.CameraManager.openCameraDeviceUserAsync(CameraManager.java:605)
        at android.hardware.camera2.CameraManager.openCameraForUid(CameraManager.java:890)
        at android.hardware.camera2.CameraManager.openCameraForUid(CameraManager.java:911)
        at android.hardware.camera2.CameraManager.openCamera(CameraManager.java:749)
        at com.genymobile.scrcpy.CameraCapture.openCamera(CameraCapture.java:241)
        at com.genymobile.scrcpy.CameraCapture.init(CameraCapture.java:86)
        at com.genymobile.scrcpy.SurfaceEncoder.streamScreen(SurfaceEncoder.java:55)
        at com.genymobile.scrcpy.SurfaceEncoder.lambda$start$0$com-genymobile-scrcpy-SurfaceEncoder(SurfaceEncoder.java:253)
        at com.genymobile.scrcpy.SurfaceEncoder$$ExternalSyntheticLambda0.run(Unknown Source:4)
        at java.lang.Thread.run(Thread.java:920)
INFO: Renderer: direct3d
ERROR: Demuxer 'video': stream disabled due to connection error
ERROR: Demuxer 'audio': stream disabled due to connection error
ERROR: Demuxer error

And there is another issues about --list-cameras. My phone has three back cameras, two main cameras(id=3, support 4k) and a macro camera(id=2). When I use fix version(3d93a09) to list my cameras, it cannot identify my macro camera, only show mix camera(idk how to say but display quality is better than id3 cameras) and front cameras.

D:\Tools\scrcpy-win64>scrcpy --list-cameras
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: ADB device found:
INFO:     --> (tcpip)     192.168.1.25:5555            device  22041216C
D:\Tools\scrcpy-win64\scrcpy-server: 1 file pushed, 0 skipped. 121.9 MB/s (64739 bytes in 0.001s)
[server] INFO: Device: [Xiaomi] Redmi 22041216C (Android 12)
[server] INFO: ===== android.app.ContextImpl@bbca705
[server] INFO: List of cameras:
    --video-source=camera --camera-id=0    (back, 4640x3472, fps=[10, 15, 20, 30])
    --video-source=camera --camera-id=1    (front, 2304x1728, fps=[10, 15, 20, 30])

@rom1v
Copy link
Collaborator

rom1v commented Nov 3, 2023

👍 and fix4392zxc.2 (probably won't solve the problem either, but worth trying)?

@zxc7895531
Copy link

👍 and fix4392zxc.2 (probably won't solve the problem either, but worth trying)?

It cannot use too😓

@rom1v
Copy link
Collaborator

rom1v commented Nov 3, 2023

What about fix4392zxc.3?

@zxc7895531
Copy link

zxc7895531 commented Nov 3, 2023

What about fix4392zxc.3?

D:\Tools\scrcpy-win64>scrcpy --camera-size=1920x1080 --video-source=camera
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: Camera video source: control disabled
INFO: Camera video source: microphone audio source selected
INFO: ADB device found:
INFO:     --> (tcpip)     192.168.1.25:5555            device  22041216C
D:\Tools\scrcpy-win64\scrcpy-server: 1 file pushed, 0 skipped. 100.4 MB/s (64787 bytes in 0.001s)
[server] INFO: Device: [Xiaomi] Redmi 22041216C (Android 12)
[server] INFO: ===== android.app.ContextImpl@156c5d4
[server] INFO: ===== android.app.ContextImpl@156c5d4
[server] INFO: Using camera '0'
[server] ERROR: Exception on thread Thread[video,5,main]
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.hardware.camera2.CameraManager.getCameraCharacteristics(CameraManager.java:491)
        at android.hardware.camera2.CameraManager.openCameraDeviceUserAsync(CameraManager.java:591)
        at android.hardware.camera2.CameraManager.openCameraForUid(CameraManager.java:890)
        at android.hardware.camera2.CameraManager.openCameraForUid(CameraManager.java:911)
        at android.hardware.camera2.CameraManager.openCamera(CameraManager.java:749)
        at com.genymobile.scrcpy.CameraCapture.openCamera(CameraCapture.java:241)
        at com.genymobile.scrcpy.CameraCapture.init(CameraCapture.java:86)
        at com.genymobile.scrcpy.SurfaceEncoder.streamScreen(SurfaceEncoder.java:55)
        at com.genymobile.scrcpy.SurfaceEncoder.lambda$start$0$com-genymobile-scrcpy-SurfaceEncoder(SurfaceEncoder.java:253)
        at com.genymobile.scrcpy.SurfaceEncoder$$ExternalSyntheticLambda0.run(Unknown Source:4)
        at java.lang.Thread.run(Thread.java:920)
INFO: Renderer: direct3d
ERROR: Demuxer 'video': stream disabled due to connection error
ERROR: Demuxer 'audio': stream disabled due to connection error
ERROR: Demuxer error
D:\Tools\scrcpy-win64>scrcpy --list-cameras
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: ADB device found:
INFO:     --> (tcpip)     192.168.1.25:5555            device  22041216C
D:\Tools\scrcpy-win64\scrcpy-server: 1 file pushed, 0 skipped. 108.8 MB/s (64787 bytes in 0.001s)
[server] INFO: Device: [Xiaomi] Redmi 22041216C (Android 12)
[server] INFO: ===== android.app.ContextImpl@42302c3
[server] INFO: ===== android.app.ContextImpl@42302c3
[server] ERROR: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.hardware.camera2.CameraManager.getCameraCharacteristics(CameraManager.java:491)
        at com.genymobile.scrcpy.LogUtils.buildCameraListMessage(LogUtils.java:97)
        at com.genymobile.scrcpy.Server.internalMain(Server.java:228)
        at com.genymobile.scrcpy.Server.main(Server.java:190)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:384)

@rom1v
Copy link
Collaborator

rom1v commented Nov 3, 2023

Let's rework the app/context/system context, and retry: fix4392zxc.4 (thank you for your tests 😉)

@Gizmodo
Copy link

Gizmodo commented Nov 3, 2023

Environment

  • OS: Windows
  • scrcpy version: 2.2
  • installation method: Windows release
  • device model: realme 9 Pro+ 256Gb
  • Android version: 13

Attempting to use other camera id's presents similar console messages.
Only mic works.

console
c:\Users\user\Downloads\scrcpy-win64-v2.2>scrcpy --video-source=camera
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: Camera video source: control disabled
INFO: Camera video source: microphone audio source selected
INFO: ADB device found:
INFO:     -->   (usb)      4DPFBYQO8PWGXKYT            device  RMX3393
c:\Users\user\Downloads\scrcpy-win64-v2.2\scrcpy-server: 1 file pushed, 0 skipped. 8.2 MB/s (64363 bytes in 0.007s)
[server] INFO: Device: [realme] realme RMX3393 (Android 13)
INFO: Renderer: direct3d
[server] INFO: Using camera '0'
[server] ERROR: Exception on thread Thread[camera,5,main]
INFO: Texture: 3840x2176
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Application.getApplicationContext()' on a null object reference
        at android.hardware.camera2.OplusCamera2StatisticsManager.addInfo(OplusCamera2StatisticsManager.java:69)
        at android.hardware.camera2.impl.CameraDeviceImplExtImpl.extendsetInfo(CameraDeviceImplExtImpl.java:47)
        at android.hardware.camera2.impl.CameraDeviceImpl$1.run(CameraDeviceImpl.java:190)
        at android.os.Handler.handleCallback(Handler.java:942)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:240)
        at android.os.Looper.loop(Looper.java:351)
        at android.os.HandlerThread.run(HandlerThread.java:67)


c:\Users\user\Downloads\scrcpy-win64-v2.2>scrcpy --list-cameras
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: ADB device found:
INFO:     -->   (usb)      4DPFBYQO8PWGXKYT            device  RMX3393
c:\Users\user\Downloads\scrcpy-win64-v2.2\scrcpy-server: 1 file pushed, 0 skipped. 272.2 MB/s (64363 bytes in 0.000s)
[server] INFO: Device: [realme] realme RMX3393 (Android 13)
[server] INFO: List of cameras:
    --video-source=camera --camera-id=0    (back, 4096x3072, fps=[10, 15, 20, 30])
    --video-source=camera --camera-id=1    (front, 2304x1728, fps=[10, 15, 20, 30])
    --video-source=camera --camera-id=2    (back, 3264x2448, fps=[10, 15, 20, 30])
    --video-source=camera --camera-id=3    (back, 1600x1200, fps=[10, 15, 20, 30])
    --video-source=camera --camera-id=4    (back, 4096x3072, fps=[10, 15, 20, 30])

c:\Users\user\Downloads\scrcpy-win64-v2.2>scrcpy --list-camera-sizes
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: ADB device found:
INFO:     -->   (usb)      4DPFBYQO8PWGXKYT            device  RMX3393
c:\Users\user\Downloads\scrcpy-win64-v2.2\scrcpy-server: 1 file pushed, 0 skipped. 277.0 MB/s (64363 bytes in 0.000s)
[server] INFO: Device: [realme] realme RMX3393 (Android 13)
[server] INFO: List of cameras:
    --video-source=camera --camera-id=0    (back, 4096x3072, fps=[10, 15, 20, 30])
        - 3840x2176
        - 3840x2160
        - 3264x2448
        - 3264x1840
        - 3264x1632
        - 3264x1572
        - 3264x1504
        - 3264x1472
        - 3216x1440
        - 2912x1344
        - 2560x1920
        - 2560x1080
        - 2400x1080
        - 2400x1028
        - 2340x1080
        - 2304x1728
        - 2280x1080
        - 2160x1080
        - 2080x960
        - 1920x1920
        - 1920x1440
        - 1920x1080
        - 1872x864
        - 1600x1200
        - 1560x720
        - 1560x702
        - 1440x1088
        - 1440x1080
        - 1280x960
        - 1280x768
        - 1280x720
        - 1088x1088
        - 960x544
        - 800x400
        - 720x480
        - 640x480
        - 352x288
        - 320x240
        - 192x144
        - 192x108
        - 176x144
        - 160x96
      High speed capture (--camera-high-speed):
        - 1280x720 (fps=[120])
        - 1920x1080 (fps=[120])
    --video-source=camera --camera-id=1    (front, 2304x1728, fps=[10, 15, 20, 30])
        - 2304x1728
        - 2304x1296
        - 2280x1080
        - 2160x1080
        - 2128x960
        - 2080x960
        - 1920x1440
        - 1920x1080
        - 1872x864
        - 1748x1748
        - 1600x1200
        - 1600x720
        - 1560x720
        - 1560x702
        - 1440x1088
        - 1440x1080
        - 1280x960
        - 1280x768
        - 1280x720
        - 1088x1088
        - 1024x768
        - 960x544
        - 800x400
        - 720x480
        - 640x480
        - 352x288
        - 320x240
        - 192x144
        - 192x108
        - 176x144
        - 160x96
      High speed capture (--camera-high-speed):
        - 1280x720 (fps=[120])
        - 1920x1080 (fps=[120])
    --video-source=camera --camera-id=2    (back, 3264x2448, fps=[10, 15, 20, 30])
        - 3264x2448
        - 3264x1840
        - 3264x1632
        - 3264x1572
        - 3264x1504
        - 3264x1472
        - 3168x1358
        - 3072x1728
        - 2912x1344
        - 2560x1920
        - 2560x1440
        - 2560x1080
        - 2448x2448
        - 2400x1080
        - 2400x1028
        - 2340x1728
        - 2340x1088
        - 2340x1080
        - 2340x1056
        - 2304x1728
        - 2304x1296
        - 2280x1080
        - 2196x1080
        - 2160x1080
        - 2080x960
        - 2080x944
        - 1920x1920
        - 1920x1440
        - 1920x1080
        - 1920x960
        - 1872x864
        - 1632x1224
        - 1600x1200
        - 1560x720
        - 1560x704
        - 1560x702
        - 1440x1088
        - 1440x1080
        - 1280x960
        - 1280x768
        - 1280x720
        - 1088x1088
        - 960x544
        - 800x400
        - 720x480
        - 640x480
        - 352x288
        - 320x240
        - 192x144
        - 192x108
        - 176x144
        - 160x96
      High speed capture (--camera-high-speed):
        - 1280x720 (fps=[120])
        - 1920x1080 (fps=[120])
    --video-source=camera --camera-id=3    (back, 1600x1200, fps=[10, 15, 20, 30])
        - 1600x1200
        - 1560x720
        - 1560x702
        - 1440x1088
        - 1440x1080
        - 1280x960
        - 1280x768
        - 1280x720
        - 1088x1088
        - 960x544
        - 800x400
        - 720x480
        - 640x480
        - 352x288
        - 320x240
        - 192x144
        - 192x108
        - 176x144
        - 160x96
      High speed capture (--camera-high-speed):
        - 1280x720 (fps=[120])
        - 1920x1080 (fps=[120])
    --video-source=camera --camera-id=4    (back, 4096x3072, fps=[10, 15, 20, 30])
        - 4096x3072
        - 4000x2256
        - 4000x1808
        - 3264x2448
        - 3264x1840
        - 3264x1632
        - 3264x1572
        - 3264x1504
        - 3264x1472
        - 2912x1344
        - 2560x1920
        - 2560x1080
        - 2400x1080
        - 2340x1080
        - 2304x1728
        - 2280x1080
        - 2160x1080
        - 2080x960
        - 1920x1920
        - 1920x1440
        - 1920x1080
        - 1872x864
        - 1600x1200
        - 1560x720
        - 1560x702
        - 1440x1088
        - 1440x1080
        - 1280x960
        - 1280x768
        - 1280x720
        - 1088x1088
        - 960x544
        - 800x400
        - 720x480
        - 640x480
        - 352x288
        - 320x240
        - 192x144
        - 192x108
        - 176x144
        - 160x96
      High speed capture (--camera-high-speed):
        - 1280x720 (fps=[120])
        - 1920x1080 (fps=[120])

@zxc7895531
Copy link

Let's rework the app/context/system context, and retry: fix4392zxc.4 (thank you for your tests 😉)

Finnaly, it's all work great👍

@rom1v
Copy link
Collaborator

rom1v commented Nov 4, 2023

@rom1v
Copy link
Collaborator

rom1v commented Nov 4, 2023

@zxc7895531 Could you please execute --list-cameras on branch cameraphy?

@RackYican
Copy link

@RackYican How did you install the new server provided here?

/usr/local/share/scrcpy/scrcpy-server

It's probably the one installed via install_release.sh, not the version with the fix.

Download the fixed version somewhere, and execute:

export SCRCPY_SERVER_PATH=path/to/the/scrcpy-server # adapt the path
scrcpy --list-cameras

I use Ubuntu, so my scrcpy version is not updated, so I install it via github

@rom1v
Copy link
Collaborator

rom1v commented Nov 4, 2023

I use Ubuntu, so my scrcpy version is not updated, so I install it via github

Yes, but download the file linked in the first post (with the fix attempt):

  • scrcpy-server SHA-256: 97423bebd9e897f27ae4c0e81699e7cd04338c1a84d0fa9995d2ce29f9fc8bce

And execute the commands I posted in my previous comment:

export SCRCPY_SERVER_PATH=path/to/the/scrcpy-server # adapt the path
scrcpy --list-cameras

@arunpt
Copy link

arunpt commented Nov 5, 2023

Starting from Android 9, camera API should only return one logical camera for all back-facing cameras

So the correct behavior is the new one IIUC?
Does it work if you pass --camera-id=4 for example?

My phone has same issues, three back cameras, scrcpy only can identity my main back camera and front camera. But my phone has a marco camera(id=3). And when i use scrcpy --video-source=camera --camera-id=3 it's work fine, but when i use --list-cameras it's not in showed list

I was able to reproduce the same. I passed camera ids 3 and 4 to open my wide angle and macro lenses, but the --list-camera command didn't return these IDs.

@rom1v
Copy link
Collaborator

rom1v commented Nov 5, 2023

@arunpt Could you also test cameraphy, please? #4392 (comment)

@rom1v
Copy link
Collaborator

rom1v commented Nov 5, 2023

@zxc7895531 @arunpt I just pushed a new branch cameraphy.2 for listing physical cameras. Here is a binary:

  • scrcpy-server SHA-256: 4264c944a1eebe815d0d8a0d947e4546beba88e63bad54c28846790a11116f5c

I could not test it myself, none of my device support logical multi-camera apparently. Please post the output you get in the console when you execute scrcpy --list-cameras.

@arunpt
Copy link

arunpt commented Nov 5, 2023

@zxc7895531 @arunpt I just pushed a new branch cameraphy.2 for listing physical cameras. Here is a binary:

  • scrcpy-server SHA-256: 4264c944a1eebe815d0d8a0d947e4546beba88e63bad54c28846790a11116f5c

I could not test it myself, none of my device support logical multi-camera apparently. Please post the output you get in the console when you execute scrcpy --list-cameras.

image
@rom1v This is after using the attached binary. Issue still persist ig

@rom1v
Copy link
Collaborator

rom1v commented Nov 5, 2023

@arunpt OK, I don't know how to list all your cameras then.

@yume-chan

but your ROM has special code to returns all physical cameras for apps in an allowlist (for example the built-in camera app). And when Scrcpy didn't supply its package name, the code also returns all physical cameras (hence the old behavior).

Where is this special code? After a quick look at the framework.jar provided, I didn't find it.

@chenxiaolong
Copy link
Contributor

chenxiaolong commented Nov 5, 2023

I just pushed a new branch cameraphy.2 for listing physical cameras.

@rom1v I think there are two small issues preventing this from working:

  1. --list-cameras doesn't show the physical camera IDs because Arrays.asList(...).contains(...) does not work for primitive int[] arrays (ugh, Java). Making a custom linear search function works fine.
  2. Physical camera IDs can't be used with --camera-id. The camera must be opened with the logical ID and then the physical camera ID is specified with outputConfig.setPhysicalCameraId(physicalId) at:
    OutputConfiguration outputConfig = new OutputConfiguration(surface);

After making the two changes (I just hardcoded the physical camera ID when testing), it's working fine on my Pixel 8 Pro. This is what the system exposes on the device:

  • Logical ID 0 (back)
    • Physical ID 2: Main camera
    • Physical ID 3: Ultrawide camera
    • Physical ID 4: Telephoto camera
    • Physical ID 5: Main camera with crop (for "zoom")
    • Physical ID 6: Telephoto camera with crop (for "zoom")
  • Logical ID 1 (front)
    • Physical ID 7: Front camera with a small crop
    • Physical ID 8: Front camera

If you decide to implement this, I think we'll need a --camera-physical-id. Or alternatively, if --camera-id can be a physical camera, then scrcpy would need to search for the corresponding logical ID to use for all calls outside of setPhysicalCameraId().

@rom1v
Copy link
Collaborator

rom1v commented Nov 5, 2023

--list-cameras doesn't show the physical camera IDs because Arrays.asList(...).contains(...) does not work for primitive int[] arrays

Oh, indeed, good catch 👍 Fixed on cameraphy.3.

@zxc7895531 What is the result of --list-cameras on that branch?

Indeed, on a Pixel 6a, I correctly get:

[server] INFO: List of cameras:
    --camera-id=0    (back, 4032x3024, fps=[15, 30, 60])
        Logical multi-camera, backed by physical cameras:
            --camera-id=2    (4032x3024)
            --camera-id=3    (4032x3016)
    --camera-id=1    (front, 3280x2464, fps=[15, 30])

Physical camera IDs can't be used with --camera-id.

Apparently, it can on some devices: #4392 (comment)
But indeed, on the Pixel 6a, it fails with:

java.io.IOException: android.hardware.camera2.CameraAccessException: CAMERA_DISCONNECTED (2): validateConnectLocked:1219: No camera device with ID "2" available

So I think the current branch camerafix (i.e. without showing physical cameras) is "working as expected", then.

We could add physical camera handling later (is it really important?), but maybe I could publish a v2.2.1 soon with the fixes of the branch camerafix.

@chenxiaolong
Copy link
Contributor

chenxiaolong commented Nov 5, 2023

Apparently, it can on some devices: #4392 (comment)

Thanks! I overlooked that comment. Looks like one more item to add to the list of things that don't follow the documented behavior 🙁

(From https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics#getPhysicalCameraIds())

Starting from API level 29, for each of the returned ID, if it's also returned by CameraManager#getCameraIdList, it can be used as a standalone camera by CameraManager#openCamera. Otherwise, the camera ID can only be used as part of the current logical camera.

EDIT:

We could add physical camera handling later (is it really important?)

The one situation I could think of where it might be a dealbreaker is if there's a device where the logical camera defaults to a physical camera that's not useful for normal use (eg. telephoto). I don't know if any device behaves like this in practice.

@chenxiaolong
Copy link
Contributor

I pushed a couple commits to my branch (commits cd6d9f6 and ea91d85) for a proof-of-concept implementation of selecting a physical camera. This branch allows --camera-id to specify either a logical or physical camera ID. On my devices, all camera IDs are unique (among both logical and physical cameras), but the documentation doesn't guarantee this, so this method might not be the best way to do things.

@yume-chan
Copy link
Contributor

yume-chan commented Nov 6, 2023

but your ROM has special code to returns all physical cameras for apps in an allowlist (for example the built-in camera app).

Where is this special code?

In framework.jar from #4392 (comment)

android.hardware.camera2.CameraManager.extractCameraIdListLocked() was modified

        private String[] extractCameraIdListLocked() {
            int size = Camera.shouldExposeAuxCamera() ? this.mDeviceStatus.size() : 2;
            int i = 0;
            for (int i2 = 0; i2 < size; i2++) {
                int intValue = this.mDeviceStatus.valueAt(i2).intValue();
                if (intValue != 0 && intValue != 2) {
                    i++;
                }
            }
            String[] strArr = new String[i];
            int i3 = 0;
            for (int i4 = 0; i4 < size; i4++) {
                int intValue2 = this.mDeviceStatus.valueAt(i4).intValue();
                if (intValue2 != 0 && intValue2 != 2) {
                    strArr[i3] = this.mDeviceStatus.keyAt(i4);
                    i3++;
                }
            }
            return strArr;
        }

android.hardware.Camera.shouldExposeAuxCamera():

    public static boolean shouldExposeAuxCamera() {
        String currentOpPackageName = ActivityThread.currentOpPackageName();
        if (currentOpPackageName == null) {
            return true;
        }
        ArrayList arrayList = new ArrayList(Arrays.asList(SystemProperties.get("vendor.camera.aux.packagelist", ",").split(",")));
        ArrayList arrayList2 = new ArrayList(Arrays.asList(SystemProperties.get("vendor.camera.aux.packageexcludelist", ",").split(",")));
        Resources resources = ActivityThread.currentApplication().getResources();
        arrayList.addAll(Arrays.asList(resources.getStringArray(C3639R.array.config_cameraAuxPackageAllowList)));
        arrayList2.addAll(Arrays.asList(resources.getStringArray(C3639R.array.config_cameraAuxPackageExcludeList)));
        return (arrayList.isEmpty() || arrayList.contains(currentOpPackageName)) && !arrayList2.contains(currentOpPackageName);
    }

That's why it requires ActivityThread.currentApplication() to work.

LineageOS has an almost identical version, I wonder who started it:

https://github.com/LineageOS/android_frameworks_base/blob/b71e63a6fcd996c7c63f9ffd3213ac683feffe70/core/java/android/hardware/camera2/CameraManager.java#L1752-L1774

        private String[] extractCameraIdListLocked() {
            String[] cameraIds = null;
            boolean exposeAuxCamera = Camera.shouldExposeAuxCamera();
            int idCount = 0;
            for (int i = 0; i < mDeviceStatus.size(); i++) {
                if (!exposeAuxCamera && i == 2) break;
                int status = mDeviceStatus.valueAt(i);
                if (status == ICameraServiceListener.STATUS_NOT_PRESENT
                        || status == ICameraServiceListener.STATUS_ENUMERATING) continue;
                idCount++;
            }
            cameraIds = new String[idCount];
            idCount = 0;
            for (int i = 0; i < mDeviceStatus.size(); i++) {
                if (!exposeAuxCamera && i == 2) break;
                int status = mDeviceStatus.valueAt(i);
                if (status == ICameraServiceListener.STATUS_NOT_PRESENT
                        || status == ICameraServiceListener.STATUS_ENUMERATING) continue;
                cameraIds[idCount] = mDeviceStatus.keyAt(i);
                idCount++;
            }
            return cameraIds;
        }

https://github.com/LineageOS/android_frameworks_base/blob/b71e63a6fcd996c7c63f9ffd3213ac683feffe70/core/java/android/hardware/Camera.java#L270-L290

    public static boolean shouldExposeAuxCamera() {
        /**
         * Force to expose only two cameras
         * if the package name does not falls in this bucket
         */
        String packageName = ActivityThread.currentOpPackageName();
        List<String> packageList = new ArrayList<>(Arrays.asList(
                SystemProperties.get("vendor.camera.aux.packagelist", ",").split(",")));
        List<String> packageExcludelist = new ArrayList<>(Arrays.asList(
                SystemProperties.get("vendor.camera.aux.packageexcludelist", ",").split(",")));


        // Append packages from lineage-sdk resources
        Resources res = ActivityThread.currentApplication().getResources();
        packageList.addAll(Arrays.asList(res.getStringArray(
                org.lineageos.platform.internal.R.array.config_cameraAuxPackageAllowList)));
        packageExcludelist.addAll(Arrays.asList(res.getStringArray(
                org.lineageos.platform.internal.R.array.config_cameraAuxPackageExcludeList)));


        return (packageList.isEmpty() || packageList.contains(packageName)) &&
                !packageExcludelist.contains(packageName);
    }

Using cameraphy.3 branch on my Mi 11:

> ./scrcpy -s 73294bba --list-cameras                                                                         
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: ADB device found:
INFO:     -->   (usb)              73294bba            device  M2011K2C
INFO:           (usb)  M01046406Y2031500412            device  mblu10
C:\Users\simon\Downloads\scrcpy-win64-v2.2\scrcpy-win64-v2.2\scrcpy-server: 1 file pushed, 0 skipped. 72.2 MB/s (65663 bytes in 0.001s)
[server] INFO: Device: [Xiaomi] Xiaomi M2011K2C (Android 13)
[server] INFO: List of cameras:
    --camera-id=0    (back, 6016x4512, fps=[10, 12, 15, 24, 30])
    --camera-id=1    (front, 2592x1940, fps=[10, 15, 24, 30])

@rom1v
Copy link
Collaborator

rom1v commented Nov 6, 2023

@yume-chan Thank you very much 👍

So the problem in #4392 (comment) is not related to physical cameras. It just happened to list all cameras when the package name was not set (for some reason), as you said.

We cannot write to system properties (permission denied), and I'm not very keen to create a custom Resources implementation to hack the return value if the key is org.lineageos.platform.internal.R.array.config_cameraAuxPackageAllowList or com.android.internal.R.array.config_cameraAuxPackageAllowList.

So I guess it will remain that way, sorry :/

@rom1v
Copy link
Collaborator

rom1v commented Nov 11, 2023

Fixes pushed to dev branch.

@rom1v rom1v closed this as completed Nov 11, 2023
@yume-chan
Copy link
Contributor

yume-chan commented Nov 12, 2023

I forgot I can get camera list from camera service directly: https://github.com/Genymobile/scrcpy/compare/cameraphy.3...yume-chan:feat/raw-camera-id?expand=1

Prebuilt: scrcpy-server.zip

> ./scrcpy --list-cameras
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: ADB device found:
INFO:     -->   (usb)              73294bba            device  M2011K2C
C:\Users\simon\Downloads\scrcpy-win64-v2.2\scrcpy-win64-v2...1 file pushed, 0 skipped. 0.7 MB/s (67251 bytes in 0.090s)
[server] INFO: Device: [Xiaomi] Xiaomi M2011K2C (Android 13)
[server] INFO: List of cameras:
    --camera-id=0    (back, 6016x4512, fps=[10, 12, 15, 24, 30])
    --camera-id=1    (front, 2592x1940, fps=[10, 15, 24, 30])
    --camera-id=2    (back, 4208x3120, fps=[15, 24, 30])
    --camera-id=3    (back, 2592x1944, fps=[24, 30])
    --camera-id=4    (back, 6016x4512, fps=[10, 12, 15, 24, 30])
        Logical multi-camera, backed by physical cameras:
            --camera-id=0    (6016x4512)
            --camera-id=2    (4208x3120)
    --camera-id=5    (back, 6016x4512, fps=[10, 12, 15, 24, 30])
        Logical multi-camera, backed by physical cameras:
            --camera-id=0    (6016x4512)
            --camera-id=2    (4208x3120)
    --camera-id=6    (back, 6016x4512, fps=[10, 12, 15, 24, 30])
        Logical multi-camera, backed by physical cameras:
            --camera-id=0    (6016x4512)
            --camera-id=3    (2592x1944)
    --camera-id=7    (front, 2592x1940, fps=[10, 15, 24, 30])
    --camera-id=8    (back, 6016x4512, fps=[10, 12, 15, 24, 30])
        Logical multi-camera, backed by physical cameras:
            --camera-id=0    (6016x4512)
            --camera-id=2    (4208x3120)

ID 0 and 1 was there previously and works, 2 and 3 weren't there but also works.

For ID 4 and 5

> ./scrcpy --video-source=camera --camera-id=4
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: Camera video source: control disabled
INFO: Camera video source: microphone audio source selected
INFO: ADB device found:
INFO:     -->   (usb)              73294bba            device  M2011K2C
C:\Users\simon\Downloads\scrcpy-win64-v2.2\scrcpy-win64-v2... file pushed, 0 skipped. 79.4 MB/s (67251 bytes in 0.001s)
[server] INFO: Device: [Xiaomi] Xiaomi M2011K2C (Android 13)
INFO: Renderer: direct3d
[server] INFO: Using camera '4'
INFO: Texture: 5920x2664
[server] ERROR: Video encoding error
java.io.IOException: android.hardware.camera2.CameraAccessException: The camera device is currently in the error state; no further calls to it will succeed.
        at com.genymobile.scrcpy.CameraCapture.start(CameraCapture.java:202)
        at com.genymobile.scrcpy.SurfaceEncoder.streamScreen(SurfaceEncoder.java:72)
        at com.genymobile.scrcpy.SurfaceEncoder.lambda$start$0$com-genymobile-scrcpy-SurfaceEncoder(SurfaceEncoder.java:253)
        at com.genymobile.scrcpy.SurfaceEncoder$$ExternalSyntheticLambda0.run(Unknown Source:4)
        at java.lang.Thread.run(Thread.java:1012)
Caused by: android.hardware.camera2.CameraAccessException: The camera device is currently in the error state; no further calls to it will succeed.
        at com.genymobile.scrcpy.CameraCapture$2.onConfigureFailed(CameraCapture.java:301)
        at android.hardware.camera2.impl.CallbackProxies$SessionStateCallbackProxy.lambda$onConfigureFailed$1$android-hardware-camera2-impl-CallbackProxies$SessionStateCallbackProxy(CallbackProxies.java:64)
        at android.hardware.camera2.impl.CallbackProxies$SessionStateCallbackProxy$$ExternalSyntheticLambda4.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:942)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:210)
        at android.os.Looper.loop(Looper.java:299)
        at android.os.HandlerThread.run(HandlerThread.java:67)

For ID 6 and 7

> ./scrcpy --video-source=camera --camera-id=6
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: Camera video source: control disabled
INFO: Camera video source: microphone audio source selected
INFO: ADB device found:
INFO:     -->   (usb)              73294bba            device  M2011K2C
C:\Users\simon\Downloads\scrcpy-win64-v2.2\scrcpy-win64-v2... file pushed, 0 skipped. 64.4 MB/s (67251 bytes in 0.001s)
[server] INFO: Device: [Xiaomi] Xiaomi M2011K2C (Android 13)
INFO: Renderer: direct3d
[server] INFO: Using camera '6'
INFO: Texture: 5920x2664

(It stops here)

For ID 8

> ./scrcpy --video-source=camera --camera-id=8
scrcpy v2.2 <https://github.com/Genymobile/scrcpy>
INFO: Camera video source: control disabled
INFO: Camera video source: microphone audio source selected
INFO: ADB device found:
INFO:     -->   (usb)              73294bba            device  M2011K2C
C:\Users\simon\Downloads\scrcpy-win64-v2.2\scrcpy-win64-v2... file pushed, 0 skipped. 83.6 MB/s (67251 bytes in 0.001s)
[server] INFO: Device: [Xiaomi] Xiaomi M2011K2C (Android 13)
INFO: Renderer: direct3d
[server] INFO: Using camera '8'
INFO: Texture: 5920x2664
[server] WARN: Camera capture failed: frame 0

It looks like none of those logical cameras works.

@christopher317
Copy link

christopher317 commented Apr 22, 2024

I just wanted to mention that in the 2.2 release, the --list-cameras used to list all the cameras on the device, including the wide-angle and macro lenses. However, the scrcpy-server with fixes (fix4392zxc.4) only listed two cameras, the front and rear ones.
-- aurnpt

is there anyway to undo this fix as a user? i want to list all the lenses on my device ( including the wide angle lens in my case ).
is there a option or would i be better off downgrading to 2.2 or another version?
( coming from #4452 )
( edit, just realized theres a new version, time to update from 2.3.1 )

@rom1v
Copy link
Collaborator

rom1v commented Dec 23, 2024

The missing cameras issue is fixed on dev #5669 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests