Skip to content

[Bug/Question] NDK Camera2 callbacks (onImageAvailable) not firing in standalone C++ executable (No JNI/Java) #1170

@sitjac

Description

@sitjac

Issue Description:

1. Problem Statement

I am developing a pure C++ tool using the NDK Camera2 API (NdkCameraManager, NdkCameraDevice, AImageReader) without any Java or JNI glue code. The goal is to capture frames and save them to the local filesystem from the shell.

The binary initializes successfully, and ACameraManager_openCamera returns ACAMERA_OK. However, none of the callbacks are being triggered:

  • ACameraDevice_StateCallbacks (e.g., onOpened) are not called.
  • ACameraCaptureSession_stateCallbacks (e.g., onReady) are not called.
  • AImageReader_ImageCallback (onImageAvailable) is never triggered.

The program enters a loop but stays idle until the timeout, with no error messages in logcat related to camera service denials.

2. Minimal Code Snippet

I am using ALooper to handle asynchronous callbacks as follows:

// In main thread
ALooper* looper = ALooper_prepare(0);

camera.startCapture(1920, 1080, 10);

int64_t start_time = time(nullptr);
while (time(nullptr) - start_time < 10) {
    int ident = ALooper_pollAll(1000, nullptr, nullptr, nullptr);
    if (ident == ALOOPER_POLL_TIMEOUT) {
    }
}

3. What I Have Tried

  • Root Access: Ran the binary as root.
  • SELinux: Set setenforce 0 to rule out SELinux policy issues.
  • Looper: Added ALooper_prepare and ALooper_pollAll in the main thread to ensure the event loop is running.
  • Verification: Verified that cmd camera list-cameras shows the cameras are available.

4. Questions

  1. Is it possible to use NDK Camera2 API in a standalone binary without a View or SurfaceTexture from the Java side?
  2. Does the shell UID (2000) or root UID (0) have inherent restrictions for accessing CameraService via NDK even if SELinux is disabled?
  3. Am I missing any specific initialization required for the ALooper to correctly route CameraService callbacks to a native process?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions