android / android/ndk-samples

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

Open
#1,170 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
10.5k
Forks
4.3k
PR merge metrics
No merged PRs in 30d

Description

### **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:

```cpp
// 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?

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the ALooper_prepare and ALooper_pollAll loop and the NDK Camera2 callback setup shown in the issue. Investigate whether the standalone process, shell/root identity, and missing Java surface affect callback delivery, then document the required conditions or limitations for callbacks to fire.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, cpp
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.