camera-rs 0.5.1

Intent-driven cross-platform camera capture with bounded native frames
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
// NDK Camera2 Bridge Implementation
// Provides a C API for Rust FFI to interact with Android Camera2 NDK APIs.
// Designed for high-performance video streaming with minimal copies.

#include "ndk_camera2_bridge.h"
#include "camera2_metadata.h"

#include <camera/NdkCameraDevice.h>
#include <camera/NdkCameraError.h>
#include <camera/NdkCameraManager.h>
#include <camera/NdkCameraMetadataTags.h>
#include <camera/NdkCameraCaptureSession.h>
#include <media/NdkImageReader.h>
#include <android/log.h>

#include <string>
#include <vector>
#include <atomic>
#include <mutex>

#define LOG_TAG "ndk_camera2"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,  LOG_TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,  LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

// ============================================================================
// Internal camera device info
// ============================================================================
struct CameraDeviceEntry {
    std::string id;
    int32_t facing;
    int32_t orientation;
    bool available;
};

// ============================================================================
// Internal output config
// ============================================================================
struct OutputConfig {
    int32_t width;
    int32_t height;
    int32_t format;
    int32_t fps;
};

// ============================================================================
// NdkCamera2 implementation
// ============================================================================
struct NdkCamera2 {
    ACameraManager *camera_mgr = nullptr;

    // Enumerated devices
    std::vector<CameraDeviceEntry> devices;

    // Active streaming state
    std::atomic<bool> streaming{false};
    ACameraDevice *camera_device = nullptr;
    AImageReader *image_reader = nullptr;
    ANativeWindow *image_window = nullptr;
    ACaptureSessionOutputContainer *output_container = nullptr;
    ACaptureSessionOutput *session_output = nullptr;
    ACameraOutputTarget *output_target = nullptr;
    ACaptureRequest *capture_request = nullptr;
    ACameraCaptureSession *capture_session = nullptr;

    // Frame callback
    std::mutex callback_mutex;
    ndk_camera_frame_callback frame_cb = nullptr;
    void *cb_context = nullptr;

    // Current config
    NdkCameraConfig current_config = {};
    int32_t active_device_index = -1;
    int32_t output_image_format = AIMAGE_FORMAT_YUV_420_888;
    int32_t max_image_count = 4;
    ACameraDevice_request_template request_template = TEMPLATE_PREVIEW;

    // Cached configs per device
    std::vector<OutputConfig> cached_configs;
    int32_t cached_config_device = -1;

    // Mutex for session state changes
    std::mutex session_mutex;

    // Callback structs are stored as members to ensure they outlive the session
    ACameraDevice_StateCallbacks device_cbs = {};
    ACameraCaptureSession_stateCallbacks session_cbs = {};

    NdkCamera2() {
        camera_mgr = ACameraManager_create();
        if (camera_mgr) {
            enumerate_cameras();
        }
    }

    ~NdkCamera2() {
        stop_streaming();
        if (camera_mgr) {
            ACameraManager_delete(camera_mgr);
            camera_mgr = nullptr;
        }
    }

    void enumerate_cameras() {
        devices.clear();
        ACameraIdList *id_list = nullptr;
        if (ACameraManager_getCameraIdList(camera_mgr, &id_list) != ACAMERA_OK || !id_list) {
            LOGE("Failed to get camera id list");
            return;
        }

        for (int i = 0; i < id_list->numCameras; i++) {
            const char *id = id_list->cameraIds[i];
            ACameraMetadata *metadata = nullptr;
            if (ACameraManager_getCameraCharacteristics(camera_mgr, id, &metadata) != ACAMERA_OK) {
                continue;
            }

            CameraDeviceEntry entry;
            entry.id = id;
            entry.facing = 1; // default BACK (ACAMERA_LENS_FACING_BACK = 1)
            entry.orientation = 0;
            entry.available = true;

            ACameraMetadata_const_entry lens_entry;
            if (ACameraMetadata_getConstEntry(metadata, ACAMERA_LENS_FACING, &lens_entry) == ACAMERA_OK) {
                entry.facing = lens_entry.data.u8[0];
            }

            ACameraMetadata_const_entry orient_entry;
            if (ACameraMetadata_getConstEntry(metadata, ACAMERA_SENSOR_ORIENTATION, &orient_entry) == ACAMERA_OK) {
                entry.orientation = orient_entry.data.i32[0];
            }

            ACameraMetadata_free(metadata);
            devices.push_back(entry);
        }

        ACameraManager_deleteCameraIdList(id_list);
        LOGI("Enumerated %zu cameras", devices.size());
    }

    void query_output_configs(int32_t device_index) {
        if (cached_config_device == device_index && !cached_configs.empty()) {
            return; // already cached
        }
        cached_configs.clear();
        cached_config_device = device_index;

        if (device_index < 0 || device_index >= (int32_t)devices.size()) {
            return;
        }

        ACameraMetadata *metadata = nullptr;
        if (ACameraManager_getCameraCharacteristics(
                camera_mgr, devices[device_index].id.c_str(), &metadata) != ACAMERA_OK) {
            return;
        }

        ACameraMetadata_const_entry entry;
        if (ACameraMetadata_getConstEntry(metadata,
                ACAMERA_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, &entry) == ACAMERA_OK) {
            // Data format: format, width, height, input/output (0=output)
            for (uint32_t i = 0; i + 3 < entry.count; i += 4) {
                int32_t format = entry.data.i32[i + 0];
                int32_t width  = entry.data.i32[i + 1];
                int32_t height = entry.data.i32[i + 2];
                int32_t input  = entry.data.i32[i + 3];

                // Only output configurations, YUV_420_888 format
                if (input == 0 && format == AIMAGE_FORMAT_YUV_420_888) {
                    OutputConfig cfg;
                    cfg.width = width;
                    cfg.height = height;
                    cfg.format = format;
                    ACameraMetadata_const_entry rates;
                    if (ACameraMetadata_getConstEntry(metadata, ACAMERA_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, &rates) == ACAMERA_OK) {
                        for (uint32_t r = 0; r + 1 < rates.count; r += 2) {
                            cfg.fps = rates.data.i32[r + 1];
                            if (cfg.fps > 0) cached_configs.push_back(cfg);
                        }
                    }
                }
            }
        }

        ACameraMetadata_free(metadata);
        LOGI("Device %d has %zu YUV output configs", device_index, cached_configs.size());
    }

    // ========================================================================
    // Streaming
    // ========================================================================

    NdkCameraStatus start_streaming(int32_t device_index, const NdkCameraConfig *config,
                                     ndk_camera_frame_callback cb, void *ctx) {
        if (device_index < 0 || device_index >= (int32_t)devices.size()) {
            return NDK_CAMERA_ERROR_NOT_FOUND;
        }
        if (!config || !cb) {
            return NDK_CAMERA_ERROR_INVALID_PARAM;
        }

        std::lock_guard<std::mutex> lock(session_mutex);

        if (streaming.load()) {
            return NDK_CAMERA_ERROR_ALREADY_STREAMING;
        }

        cleanup_streaming();

        {
            std::lock_guard<std::mutex> callback_lock(callback_mutex);
            frame_cb = cb;
            cb_context = ctx;
        }
        current_config = *config;
        active_device_index = device_index;

        // 1. Create AImageReader.
        //
        // External Camera2 devices commonly expose only YUV_420_888 output.
        // Requesting RGBA_8888 can create the reader but fail later when the
        // capture session is configured, so use YUV and let Rust convert it.
        int32_t requested_format = AIMAGE_FORMAT_YUV_420_888;

        media_status_t mstatus = AImageReader_new(
            config->width, config->height,
            requested_format,
            max_image_count,
            &image_reader);

        if ((mstatus != AMEDIA_OK || !image_reader)
            && requested_format != AIMAGE_FORMAT_YUV_420_888) {
            LOGW("RGBA reader unavailable, fallback to YUV_420_888");
            image_reader = nullptr;
            requested_format = AIMAGE_FORMAT_YUV_420_888;
            mstatus = AImageReader_new(
                config->width,
                config->height,
                requested_format,
                max_image_count,
                &image_reader);
        }

        if (mstatus != AMEDIA_OK || !image_reader) {
            LOGE("Failed to create AImageReader: %d", mstatus);
            cleanup_streaming();
            return NDK_CAMERA_ERROR_INTERNAL;
        }
        output_image_format = requested_format;

        // Set image available callback
        AImageReader_ImageListener listener = {
            .context = this,
            .onImageAvailable = on_image_available,
        };
        AImageReader_setImageListener(image_reader, &listener);

        // Get native window from image reader
        AImageReader_getWindow(image_reader, &image_window);
        if (!image_window) {
            LOGE("Failed to get AImageReader window");
            cleanup_streaming();
            return NDK_CAMERA_ERROR_INTERNAL;
        }
        ANativeWindow_acquire(image_window);

        // 2. Open camera device
        // Callback structs are member variables (see above) so they outlive the session.
        // This also ensures 'this' is always the correct live instance.
        device_cbs.context = this;
        device_cbs.onDisconnected = on_device_disconnected;
        device_cbs.onError = on_device_error;

        camera_status_t status = ACameraManager_openCamera(
            camera_mgr, devices[device_index].id.c_str(),
            &device_cbs, &camera_device);
        if (status != ACAMERA_OK || !camera_device) {
            LOGE("Failed to open camera %s: %d", devices[device_index].id.c_str(), status);
            cleanup_streaming();
            return NDK_CAMERA_ERROR_OPEN_FAILED;
        }

        // 3. Create capture session output container
        ACaptureSessionOutputContainer_create(&output_container);

        // Create session output from image reader window
        ACaptureSessionOutput_create(image_window, &session_output);
        ACaptureSessionOutputContainer_add(output_container, session_output);

        // Create output target
        ACameraOutputTarget_create(image_window, &output_target);

        ACameraDevice_createCaptureRequest(camera_device, request_template, &capture_request);
        ACaptureRequest_addTarget(capture_request, output_target);

        // A target range must be one of the advertised pairs (many USB HALs
        // advertise [15,30] only). Never manufacture [requested,requested].
        ACameraMetadata *metadata = nullptr;
        ACameraMetadata_const_entry rates;
        int32_t fps_range[2];
        bool selected = ACameraManager_getCameraCharacteristics(camera_mgr,
            devices[device_index].id.c_str(), &metadata) == ACAMERA_OK &&
            ACameraMetadata_getConstEntry(metadata, ACAMERA_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, &rates) == ACAMERA_OK &&
            camera2_select_fps(rates.data.i32, rates.count, config->fps, fps_range);
        if (metadata) ACameraMetadata_free(metadata);
        if (!selected || ACaptureRequest_setEntry_i32(capture_request,
                ACAMERA_CONTROL_AE_TARGET_FPS_RANGE, 2, fps_range) != ACAMERA_OK) {
            cleanup_streaming();
            return NDK_CAMERA_ERROR_INVALID_PARAM;
        }
        current_config.fps = camera2_clamp_fps(config->fps, fps_range);

        // 4. Create capture session
        // Callback struct is a member variable to ensure correct lifetime and 'this'.
        session_cbs.context = this;
        session_cbs.onClosed = on_session_closed;
        session_cbs.onReady = on_session_ready;
        session_cbs.onActive = on_session_active;

        status = ACameraDevice_createCaptureSession(
            camera_device, output_container, &session_cbs, &capture_session);
        if (status != ACAMERA_OK || !capture_session) {
            LOGE("Failed to create capture session: %d", status);
            cleanup_streaming();
            return NDK_CAMERA_ERROR_SESSION_FAILED;
        }

        // 5. Start repeating request
        status = ACameraCaptureSession_setRepeatingRequest(
            capture_session, nullptr, 1, &capture_request, nullptr);
        if (status != ACAMERA_OK) {
            LOGE("Failed to start repeating request: %d", status);
            cleanup_streaming();
            return NDK_CAMERA_ERROR_SESSION_FAILED;
        }

        streaming.store(true);
        LOGI("Camera %s streaming at %dx%d @ %dfps",
             devices[device_index].id.c_str(),
             config->width, config->height, config->fps);
        return NDK_CAMERA_OK;
    }

    void stop_streaming() {
        bool was_streaming = streaming.exchange(false);
        LOGI("Stopping camera stream");

        std::lock_guard<std::mutex> lock(session_mutex);

        bool has_resources = capture_session || capture_request || output_target ||
            session_output || output_container || camera_device || image_window || image_reader;
        if (!was_streaming && !has_resources) {
            return;
        }

        if (capture_session) {
            ACameraCaptureSession_stopRepeating(capture_session);
            ACameraCaptureSession_close(capture_session);
            capture_session = nullptr;
        }

        cleanup_streaming();
        LOGI("Camera stream stopped");
    }

    void cleanup_streaming() {
        streaming.store(false);
        // Drain an active Rust callback before releasing native images. Never
        // hold this mutex while deleting the reader (which joins its thread).
        {
            std::lock_guard<std::mutex> callback_lock(callback_mutex);
            frame_cb = nullptr;
            cb_context = nullptr;
        }
        if (capture_session) {
            ACameraCaptureSession_stopRepeating(capture_session);
            ACameraCaptureSession_close(capture_session);
            capture_session = nullptr;
        }

        if (capture_request) {
            if (output_target) {
                ACaptureRequest_removeTarget(capture_request, output_target);
            }
            ACaptureRequest_free(capture_request);
            capture_request = nullptr;
        }

        if (output_target) {
            ACameraOutputTarget_free(output_target);
            output_target = nullptr;
        }

        if (session_output && output_container) {
            ACaptureSessionOutputContainer_remove(output_container, session_output);
        }
        if (session_output) {
            ACaptureSessionOutput_free(session_output);
            session_output = nullptr;
        }
        if (output_container) {
            ACaptureSessionOutputContainer_free(output_container);
            output_container = nullptr;
        }

        if (camera_device) {
            ACameraDevice_close(camera_device);
            camera_device = nullptr;
        }

        if (image_window) {
            ANativeWindow_release(image_window);
            image_window = nullptr;
        }

        if (image_reader) {
            AImageReader_delete(image_reader);
            image_reader = nullptr;
        }

        active_device_index = -1;
    }

    // ========================================================================
    // Camera controls - applied by updating capture request
    // ========================================================================

    NdkCameraStatus update_repeating_request() {
        if (!streaming.load() || !capture_session || !capture_request) {
            return NDK_CAMERA_ERROR_NOT_STREAMING;
        }
        // Stop and re-start repeating request with updated parameters
        ACameraCaptureSession_stopRepeating(capture_session);
        camera_status_t status = ACameraCaptureSession_setRepeatingRequest(
            capture_session, nullptr, 1, &capture_request, nullptr);
        if (status != ACAMERA_OK) {
            LOGE("Failed to update repeating request: %d", status);
            return NDK_CAMERA_ERROR_INTERNAL;
        }
        return NDK_CAMERA_OK;
    }

    NdkCameraStatus get_metadata_range_i32(int32_t device_index, uint32_t tag,
                                            int32_t *out_min, int32_t *out_max) {
        if (device_index < 0 || device_index >= (int32_t)devices.size()) {
            return NDK_CAMERA_ERROR_NOT_FOUND;
        }
        ACameraMetadata *metadata = nullptr;
        if (ACameraManager_getCameraCharacteristics(
                camera_mgr, devices[device_index].id.c_str(), &metadata) != ACAMERA_OK) {
            return NDK_CAMERA_ERROR_INTERNAL;
        }
        ACameraMetadata_const_entry entry;
        NdkCameraStatus ret = NDK_CAMERA_ERROR_INTERNAL;
        if (ACameraMetadata_getConstEntry(metadata, tag, &entry) == ACAMERA_OK && entry.count >= 2) {
            *out_min = entry.data.i32[0];
            *out_max = entry.data.i32[1];
            ret = NDK_CAMERA_OK;
        }
        ACameraMetadata_free(metadata);
        return ret;
    }

    // ========================================================================
    // Static callbacks
    // ========================================================================

    static void on_image_available(void *context, AImageReader *reader) {
        auto *self = reinterpret_cast<NdkCamera2*>(context);
        std::lock_guard<std::mutex> callback_lock(self->callback_mutex);
        if (!self->streaming.load() || !self->frame_cb) {
            // Drain the image
            AImage *image = nullptr;
            if (AImageReader_acquireLatestImage(reader, &image) == AMEDIA_OK && image) {
                AImage_delete(image);
            }
            return;
        }

        AImage *image = nullptr;
        // Use acquireLatestImage to skip stale frames for lower latency
        media_status_t status = AImageReader_acquireLatestImage(reader, &image);
        if (status != AMEDIA_OK || !image) {
            return;
        }

        int32_t width = 0, height = 0;
        AImage_getWidth(image, &width);
        AImage_getHeight(image, &height);

        uint8_t *rgb_data = nullptr;
        int32_t rgb_len = 0;
        int32_t rgb_row_stride = 0;

        uint8_t *y_data = nullptr;
        int32_t y_len = 0;
        uint8_t *uv_data = nullptr;
        int32_t uv_len = 0;
        uint8_t *v_data = nullptr;
        int32_t v_len = 0, v_row_stride = 0, v_pixel_stride = 0;
        int32_t y_row_stride = 0, uv_row_stride = 0, uv_pixel_stride = 0;
        int32_t pixel_format = NDK_CAMERA_PIXEL_FORMAT_UNKNOWN;

        if (self->output_image_format == AIMAGE_FORMAT_RGBA_8888) {
            AImage_getPlaneData(image, 0, &rgb_data, &rgb_len);
            AImage_getPlaneRowStride(image, 0, &rgb_row_stride);
            if (rgb_data && rgb_len > 0) {
                pixel_format = NDK_CAMERA_PIXEL_FORMAT_RGBA8888;
            }
        }

        if (pixel_format == NDK_CAMERA_PIXEL_FORMAT_UNKNOWN) {
            AImage_getPlaneData(image, 0, &y_data, &y_len);
            AImage_getPlaneData(image, 1, &uv_data, &uv_len);
            AImage_getPlaneData(image, 2, &v_data, &v_len);
            AImage_getPlaneRowStride(image, 2, &v_row_stride);
            AImage_getPlanePixelStride(image, 2, &v_pixel_stride);
            AImage_getPlaneRowStride(image, 0, &y_row_stride);
            AImage_getPlaneRowStride(image, 1, &uv_row_stride);
            AImage_getPlanePixelStride(image, 1, &uv_pixel_stride);
            pixel_format = NDK_CAMERA_PIXEL_FORMAT_YUV420;
        }

        int64_t timestamp = 0;
        AImage_getTimestamp(image, &timestamp);

        NdkFrameData frame = {};
        frame.rgb_data = rgb_data;
        frame.rgb_len = rgb_len;
        frame.y_data = y_data;
        frame.y_len = y_len;
        frame.uv_data = uv_data;
        frame.uv_len = uv_len;
        frame.v_data = v_data;
        frame.v_len = v_len;
        frame.row_stride_v = v_row_stride;
        frame.pixel_stride_v = v_pixel_stride;
        frame.width = width;
        frame.height = height;
        frame.row_stride_y = y_row_stride;
        frame.row_stride_uv = uv_row_stride;
        frame.pixel_stride_uv = uv_pixel_stride;
        frame.row_stride_rgb = rgb_row_stride;
        frame.pixel_format = pixel_format;
        frame.timestamp_ns = timestamp;

        self->frame_cb(self->cb_context, &frame);

        AImage_delete(image);
    }

    static void on_device_disconnected(void *context, ACameraDevice *device) {
        auto *self = reinterpret_cast<NdkCamera2*>(context);
        LOGW("Camera device disconnected");
        std::lock_guard<std::mutex> callback_lock(self->callback_mutex);
        if (self->streaming.exchange(false) && self->frame_cb) {
            self->frame_cb(self->cb_context, nullptr);
        }
    }

    static void on_device_error(void *context, ACameraDevice *device, int error) {
        auto *self = reinterpret_cast<NdkCamera2*>(context);
        LOGE("Camera device error: %d", error);
        std::lock_guard<std::mutex> callback_lock(self->callback_mutex);
        if (self->streaming.exchange(false) && self->frame_cb) {
            self->frame_cb(self->cb_context, nullptr);
        }
    }

    static void on_session_closed(void *context, ACameraCaptureSession *session) {
        LOGI("Capture session closed");
    }

    static void on_session_ready(void *context, ACameraCaptureSession *session) {
        LOGI("Capture session ready");
    }

    static void on_session_active(void *context, ACameraCaptureSession *session) {
        LOGI("Capture session active");
    }
};

// ============================================================================
// C API implementation
// ============================================================================

extern "C" {

NdkCamera2* ndk_camera2_create(void) {
    auto *cam = new (std::nothrow) NdkCamera2();
    if (!cam || !cam->camera_mgr) {
        delete cam;
        return nullptr;
    }
    return cam;
}

void ndk_camera2_destroy(NdkCamera2 *cam) {
    delete cam;
}

NdkCameraStatus ndk_camera2_set_options(NdkCamera2 *cam, int32_t max_images,
                                         int32_t request_template) {
    const bool known_template = request_template == TEMPLATE_PREVIEW ||
        request_template == TEMPLATE_STILL_CAPTURE || request_template == TEMPLATE_RECORD;
    if (!cam || cam->streaming.load() || max_images < 2 || max_images > 16 ||
        !known_template) {
        return NDK_CAMERA_ERROR_INVALID_PARAM;
    }
    cam->max_image_count = max_images;
    cam->request_template = static_cast<ACameraDevice_request_template>(request_template);
    return NDK_CAMERA_OK;
}

NdkCameraStatus ndk_camera2_get_device_count(NdkCamera2 *cam, int32_t *out_count) {
    if (!cam || !out_count) return NDK_CAMERA_ERROR_INVALID_PARAM;
    *out_count = (int32_t)cam->devices.size();
    return NDK_CAMERA_OK;
}

NdkCameraStatus ndk_camera2_get_device_info(NdkCamera2 *cam, int32_t index,
                                             NdkCameraDeviceInfo *out_info) {
    if (!cam || !out_info) return NDK_CAMERA_ERROR_INVALID_PARAM;
    if (index < 0 || index >= (int32_t)cam->devices.size()) return NDK_CAMERA_ERROR_NOT_FOUND;

    const auto &dev = cam->devices[index];
    out_info->id = dev.id.c_str();
    out_info->facing = dev.facing;
    out_info->orientation = dev.orientation;
    out_info->available = dev.available;
    return NDK_CAMERA_OK;
}

NdkCameraStatus ndk_camera2_get_config_count(NdkCamera2 *cam, int32_t device_index,
                                              int32_t *out_count) {
    if (!cam || !out_count) return NDK_CAMERA_ERROR_INVALID_PARAM;
    cam->query_output_configs(device_index);
    *out_count = (int32_t)cam->cached_configs.size();
    return NDK_CAMERA_OK;
}

NdkCameraStatus ndk_camera2_get_config(NdkCamera2 *cam, int32_t device_index,
                                        int32_t config_index, NdkCameraConfig *out_config) {
    if (!cam || !out_config) return NDK_CAMERA_ERROR_INVALID_PARAM;
    cam->query_output_configs(device_index);
    if (config_index < 0 || config_index >= (int32_t)cam->cached_configs.size()) {
        return NDK_CAMERA_ERROR_NOT_FOUND;
    }
    const auto &cfg = cam->cached_configs[config_index];
    out_config->width = cfg.width;
    out_config->height = cfg.height;
    out_config->format = cfg.format;
    out_config->fps = cfg.fps; // Advertised AE upper bound; exposure can lower delivery rate.
    return NDK_CAMERA_OK;
}

NdkCameraStatus ndk_camera2_start_stream(NdkCamera2 *cam, int32_t device_index,
                                          const NdkCameraConfig *config,
                                          ndk_camera_frame_callback frame_cb,
                                          void *callback_context) {
    if (!cam) return NDK_CAMERA_ERROR_INVALID_PARAM;
    return cam->start_streaming(device_index, config, frame_cb, callback_context);
}

NdkCameraStatus ndk_camera2_get_active_config(NdkCamera2 *cam, NdkCameraConfig *out) {
    if (!cam || !out) return NDK_CAMERA_ERROR_INVALID_PARAM;
    if (!cam->streaming.load()) return NDK_CAMERA_ERROR_NOT_STREAMING;
    *out = cam->current_config;
    return NDK_CAMERA_OK;
}

NdkCameraStatus ndk_camera2_stop_stream(NdkCamera2 *cam) {
    if (!cam) return NDK_CAMERA_ERROR_INVALID_PARAM;
    cam->stop_streaming();
    return NDK_CAMERA_OK;
}

bool ndk_camera2_is_streaming(NdkCamera2 *cam) {
    if (!cam) return false;
    return cam->streaming.load();
}



NdkCameraStatus ndk_camera2_get_control_modes(NdkCamera2 *cam, int32_t kind, uint32_t *mask) {
    if (!cam || !mask || kind < 0 || kind > 2) return NDK_CAMERA_ERROR_INVALID_PARAM;
    *mask = 0;
    int32_t idx = cam->active_device_index >= 0 ? cam->active_device_index : 0;
    if (idx >= (int32_t)cam->devices.size()) return NDK_CAMERA_ERROR_NOT_FOUND;
    ACameraMetadata *metadata = nullptr;
    if (ACameraManager_getCameraCharacteristics(cam->camera_mgr,cam->devices[idx].id.c_str(),&metadata) != ACAMERA_OK)
        return NDK_CAMERA_ERROR_INTERNAL;
    uint32_t tags[] = {ACAMERA_CONTROL_AE_AVAILABLE_MODES, ACAMERA_CONTROL_AF_AVAILABLE_MODES, ACAMERA_CONTROL_AWB_AVAILABLE_MODES};
    ACameraMetadata_const_entry entry;
    if (ACameraMetadata_getConstEntry(metadata,tags[kind],&entry) == ACAMERA_OK)
        for (uint32_t i = 0; i < entry.count; i++)
            if (entry.data.u8[i] < 32) *mask |= 1u << entry.data.u8[i];
    ACameraMetadata_free(metadata);
    return NDK_CAMERA_OK;
}

static NdkCameraStatus set_mode(NdkCamera2 *cam, int32_t kind, int32_t mode) {
    if (!cam || !cam->capture_request) return NDK_CAMERA_ERROR_NOT_STREAMING;
    uint32_t mask = 0;
    NdkCameraStatus result = ndk_camera2_get_control_modes(cam,kind,&mask);
    if (result != NDK_CAMERA_OK) return result;
    if (mode < 0 || mode >= 32 || !(mask & (1u << mode))) return NDK_CAMERA_ERROR_INVALID_PARAM;
    const uint32_t tags[] = {ACAMERA_CONTROL_AE_MODE, ACAMERA_CONTROL_AF_MODE, ACAMERA_CONTROL_AWB_MODE};
    uint8_t value = (uint8_t)mode;
    if (ACaptureRequest_setEntry_u8(cam->capture_request,tags[kind],1,&value) != ACAMERA_OK)
        return NDK_CAMERA_ERROR_INVALID_PARAM;
    if (kind == 1 && mode == ACAMERA_CONTROL_AF_MODE_AUTO) {
        uint8_t trigger = ACAMERA_CONTROL_AF_TRIGGER_START;
        if (ACaptureRequest_setEntry_u8(cam->capture_request,ACAMERA_CONTROL_AF_TRIGGER,1,&trigger) != ACAMERA_OK)
            return NDK_CAMERA_ERROR_INVALID_PARAM;
        camera_status_t status = ACameraCaptureSession_capture(cam->capture_session,nullptr,1,&cam->capture_request,nullptr);
        trigger = ACAMERA_CONTROL_AF_TRIGGER_IDLE;
        ACaptureRequest_setEntry_u8(cam->capture_request,ACAMERA_CONTROL_AF_TRIGGER,1,&trigger);
        if (status != ACAMERA_OK) return NDK_CAMERA_ERROR_SESSION_FAILED;
    }
    return cam->update_repeating_request();
}

NdkCameraStatus ndk_camera2_set_exposure_compensation(NdkCamera2 *cam, int32_t value) {
    if (!cam || !cam->capture_request) return NDK_CAMERA_ERROR_NOT_STREAMING;
    if (ACaptureRequest_setEntry_i32(cam->capture_request,
            ACAMERA_CONTROL_AE_EXPOSURE_COMPENSATION, 1, &value) != ACAMERA_OK)
        return NDK_CAMERA_ERROR_INVALID_PARAM;
    return cam->update_repeating_request();
}

NdkCameraStatus ndk_camera2_get_exposure_compensation_range(NdkCamera2 *cam,
                                                             int32_t *out_min,
                                                             int32_t *out_max) {
    if (!cam || !out_min || !out_max) return NDK_CAMERA_ERROR_INVALID_PARAM;
    int32_t idx = cam->active_device_index >= 0 ? cam->active_device_index : 0;
    return cam->get_metadata_range_i32(idx,
        ACAMERA_CONTROL_AE_COMPENSATION_RANGE, out_min, out_max);
}

NdkCameraStatus ndk_camera2_set_ae_mode(NdkCamera2 *cam, int32_t mode) {
    return set_mode(cam,0,mode);
}

NdkCameraStatus ndk_camera2_set_af_mode(NdkCamera2 *cam, int32_t mode) {
    return set_mode(cam,1,mode);
}

NdkCameraStatus ndk_camera2_set_awb_mode(NdkCamera2 *cam, int32_t mode) {
    return set_mode(cam,2,mode);
}

NdkCameraStatus ndk_camera2_set_zoom(NdkCamera2 *cam, int32_t zoom_100) {
    if (!cam || !cam->capture_request) return NDK_CAMERA_ERROR_NOT_STREAMING;

    // Use SCALER_CROP_REGION for zoom (compatible with API 24+)
    int32_t idx = cam->active_device_index;
    if (idx < 0 || idx >= (int32_t)cam->devices.size()) return NDK_CAMERA_ERROR_INTERNAL;

    ACameraMetadata *metadata = nullptr;
    if (ACameraManager_getCameraCharacteristics(
            cam->camera_mgr, cam->devices[idx].id.c_str(), &metadata) != ACAMERA_OK) {
        return NDK_CAMERA_ERROR_INTERNAL;
    }

    ACameraMetadata_const_entry sensor_entry;
    if (ACameraMetadata_getConstEntry(metadata,
            ACAMERA_SENSOR_INFO_ACTIVE_ARRAY_SIZE, &sensor_entry) != ACAMERA_OK ||
        sensor_entry.count < 4) {
        ACameraMetadata_free(metadata);
        return NDK_CAMERA_ERROR_INTERNAL;
    }

    int32_t crop_region[4];
    bool valid = camera2_zoom_crop(sensor_entry.data.i32, zoom_100, crop_region);
    ACameraMetadata_free(metadata);
    if (!valid || ACaptureRequest_setEntry_i32(cam->capture_request,
            ACAMERA_SCALER_CROP_REGION, 4, crop_region) != ACAMERA_OK)
        return NDK_CAMERA_ERROR_INVALID_PARAM;

    return cam->update_repeating_request();
}

NdkCameraStatus ndk_camera2_get_zoom_range(NdkCamera2 *cam, int32_t *out_min, int32_t *out_max) {
    if (!cam || !out_min || !out_max) return NDK_CAMERA_ERROR_INVALID_PARAM;
    int32_t idx = cam->active_device_index >= 0 ? cam->active_device_index : 0;
    if (idx >= (int32_t)cam->devices.size()) return NDK_CAMERA_ERROR_NOT_FOUND;

    ACameraMetadata *metadata = nullptr;
    if (ACameraManager_getCameraCharacteristics(
            cam->camera_mgr, cam->devices[idx].id.c_str(), &metadata) != ACAMERA_OK) {
        return NDK_CAMERA_ERROR_INTERNAL;
    }

    ACameraMetadata_const_entry entry;
    *out_min = 100; // 1.0x
    if (ACameraMetadata_getConstEntry(metadata,
            ACAMERA_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM, &entry) == ACAMERA_OK && entry.count >= 1) {
        *out_max = (int32_t)(entry.data.f[0] * 100.0f);
    } else {
        *out_max = 100; // no zoom
    }

    ACameraMetadata_free(metadata);
    return NDK_CAMERA_OK;
}

NdkCameraStatus ndk_camera2_get_sensitivity_range(NdkCamera2 *cam,
                                                    int32_t *out_min, int32_t *out_max) {
    if (!cam || !out_min || !out_max) return NDK_CAMERA_ERROR_INVALID_PARAM;
    int32_t idx = cam->active_device_index >= 0 ? cam->active_device_index : 0;
    return cam->get_metadata_range_i32(idx,
        ACAMERA_SENSOR_INFO_SENSITIVITY_RANGE, out_min, out_max);
}

NdkCameraStatus ndk_camera2_set_sensitivity(NdkCamera2 *cam, int32_t iso) {
    if (!cam || !cam->capture_request) return NDK_CAMERA_ERROR_NOT_STREAMING;
    // Must disable AE to set ISO manually
    uint8_t ae_off = ACAMERA_CONTROL_AE_MODE_OFF;
    uint32_t modes = 0;
    if (ndk_camera2_get_control_modes(cam, 0, &modes) != NDK_CAMERA_OK || !(modes & 1u))
        return NDK_CAMERA_ERROR_INVALID_PARAM;
    if (ACaptureRequest_setEntry_u8(cam->capture_request,
            ACAMERA_CONTROL_AE_MODE, 1, &ae_off) != ACAMERA_OK ||
        ACaptureRequest_setEntry_i32(cam->capture_request,
            ACAMERA_SENSOR_SENSITIVITY, 1, &iso) != ACAMERA_OK)
        return NDK_CAMERA_ERROR_INVALID_PARAM;
    return cam->update_repeating_request();
}

} // extern "C"