camera-rs-android 0.5.0

Android USB permission and JNI adapter for camera
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
//! Android USB permission integration and JNI adapter. Camera2 capture in the
//! core `camera` crate does not require this package or a Java context.
#![warn(missing_docs)]
#![warn(unsafe_op_in_unsafe_fn)]
pub mod android;
/// Stable Android adapter error types and JNI boundary payloads.
pub mod error;
mod owned_fd;
use camera::{
    BackendId, BackendPolicy, CameraSystem, CaptureRequest, ControlId, ControlMode, ControlValue,
    Device, DeviceSelector, Frame, FrameRate, FrameReceiver, Session, SubscriptionOptions,
};
#[cfg(feature = "convert-rgb")]
use camera::{ConversionRequest, PixelFormat, RgbConverter};
use error::{CameraError, Result};
use jni::{
    objects::{JByteBuffer, JClass, JObject, JString},
    sys::{jboolean, jint, jlong, jstring, JNI_FALSE, JNI_TRUE},
    JNIEnv,
};
use serde_json::{json, Value};
#[cfg(feature = "uvc")]
use std::os::fd::AsFd;
use std::{
    collections::HashMap,
    sync::{
        atomic::{AtomicBool, AtomicI64, Ordering},
        Arc, Mutex, OnceLock,
    },
    time::Duration,
};

#[derive(Clone, Debug)]
/// Description of a USB video device discovered through Android's `UsbManager`.
pub struct UsbDeviceInfo {
    /// Stable index within the current scan result.
    pub index: u32,
    /// User-facing device name.
    pub name: String,
    /// Additional device description supplied by Android.
    pub description: String,
    /// USB vendor identifier, when Android exposes it.
    pub vendor_id: Option<u16>,
    /// USB product identifier, when Android exposes it.
    pub product_id: Option<u16>,
    /// USB serial number, when the application has permission to read it.
    pub serial_number: Option<String>,
    /// Android USB device path used by the permission and open calls.
    pub device_path: Option<String>,
}
struct Reader {
    session: Arc<Session>,
    frames: FrameReceiver,
    pending: Option<Frame>,
    #[cfg(feature = "convert-rgb")]
    converter: RgbConverter,
    #[cfg(feature = "convert-rgb")]
    rgb: Vec<u8>,
}
struct Handle {
    closed: AtomicBool,
    lifecycle: Mutex<()>,
    camera: tokio::sync::Mutex<Device>,
    session: Mutex<Option<Arc<Session>>>,
    reader: tokio::sync::Mutex<Option<Reader>>,
    native_output: AtomicBool,
}
static HANDLES: OnceLock<Mutex<HashMap<i64, Arc<Handle>>>> = OnceLock::new();
static NEXT: AtomicI64 = AtomicI64::new(1);
fn registry() -> &'static Mutex<HashMap<i64, Arc<Handle>>> {
    HANDLES.get_or_init(|| Mutex::new(HashMap::new()))
}
fn runtime() -> &'static tokio::runtime::Runtime {
    static RT: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
    RT.get_or_init(|| {
        tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .expect("JNI camera runtime")
    })
}
fn invalid(message: impl Into<String>) -> CameraError {
    CameraError::adapter(camera::CameraErrorKind::InvalidState, message)
}
fn handle(id: i64) -> Result<Arc<Handle>> {
    registry()
        .lock()
        .map_err(|_| invalid("Handle registry poisoned"))?
        .get(&id)
        .cloned()
        .ok_or_else(|| invalid("Unknown camera handle"))
}
fn session(handle: &Handle) -> Result<Arc<Session>> {
    handle
        .session
        .lock()
        .map_err(|_| invalid("Session state poisoned"))?
        .clone()
        .ok_or_else(|| invalid("Camera is not streaming"))
}
fn store(camera: Device) -> Result<i64> {
    let id = NEXT.fetch_add(1, Ordering::Relaxed);
    if id <= 0 {
        return Err(invalid("Handle space exhausted"));
    }
    registry()
        .lock()
        .map_err(|_| invalid("Handle registry poisoned"))?
        .insert(
            id,
            Arc::new(Handle {
                closed: AtomicBool::new(false),
                lifecycle: Mutex::new(()),
                camera: tokio::sync::Mutex::new(camera),
                session: Mutex::new(None),
                reader: tokio::sync::Mutex::new(None),
                native_output: AtomicBool::new(true),
            }),
        );
    Ok(id)
}
fn backend(name: &str) -> Result<Option<BackendId>> {
    Ok(match name.to_ascii_lowercase().as_str() {
        "camera2" => Some(BackendId::CAMERA2),
        "uvc" => Some(BackendId::UVC),
        "v4l2" => Some(BackendId::V4L2),
        "auto" => None,
        _ => return Err(invalid("Backend must be auto, camera2, uvc, or v4l2")),
    })
}

fn capture_format(name: &str) -> Result<camera::CaptureFormat> {
    use camera::CaptureFormat;
    match name.trim().to_ascii_lowercase().as_str() {
        "mjpeg" | "mjpg" => Ok(CaptureFormat::Mjpeg),
        "yuyv" | "yuy2" => Ok(CaptureFormat::Yuyv),
        "uyvy" => Ok(CaptureFormat::Uyvy),
        "nv12" => Ok(CaptureFormat::Nv12),
        "rgb" | "rgb8" => Ok(CaptureFormat::Rgb8),
        "h264" => Ok(CaptureFormat::H264),
        "gray" | "gray8" => Ok(CaptureFormat::Gray8),
        _ => Err(invalid("Unknown capture format")),
    }
}

fn frame_interval_payload(interval: camera::FrameInterval) -> Value {
    json!({"numerator":interval.numerator,"denominator":interval.denominator})
}

fn capability_payload(capabilities: camera::DeviceCapabilities) -> Value {
    let (knowledge, reason, modes) = match capabilities.capture_modes {
        camera::CapabilityKnowledge::Known(modes) => ("known", None, modes),
        camera::CapabilityKnowledge::Representative { values, reason } => {
            ("representative", Some(reason), values)
        }
        camera::CapabilityKnowledge::Unknown { reason } => ("unknown", Some(reason), Vec::new()),
    };
    let modes = modes
        .into_iter()
        .map(|mode| {
            json!({
                "format":format!("{:?}",mode.format),
                "width":mode.width,
                "height":mode.height,
                "frameRateNumerator":mode.frame_rate.numerator(),
                "frameRateDenominator":mode.frame_rate.denominator(),
                "framesPerSecond":mode.frame_rate.as_f64()
            })
        })
        .collect::<Vec<_>>();
    let ranges = capabilities
        .capture_mode_ranges
        .into_iter()
        .map(|range| {
            json!({
                "format":format!("{:?}",range.format),
                "kind":format!("{:?}",range.kind),
                "width":{"min":range.width.minimum,"max":range.width.maximum,"step":range.width.step},
                "height":{"min":range.height.minimum,"max":range.height.maximum,"step":range.height.step},
                "frameIntervals":range.frame_intervals.into_iter().map(|interval|json!({
                    "kind":format!("{:?}",interval.kind),
                    "minimum":frame_interval_payload(interval.minimum),
                    "maximum":frame_interval_payload(interval.maximum),
                    "step":interval.step.map(frame_interval_payload),
                    "atResolution":{"width":interval.at_resolution.0,"height":interval.at_resolution.1}
                })).collect::<Vec<_>>()
            })
        })
        .collect::<Vec<_>>();
    let conversions = capabilities
        .conversions
        .into_iter()
        .map(|conversion| {
            json!({
                "input":format!("{:?}",conversion.input),
                "output":format!("{:?}",conversion.output),
                "available":conversion.available,
                "requirement":conversion.requirement
            })
        })
        .collect::<Vec<_>>();
    json!({
        "knowledge":knowledge,
        "reason":reason,
        "modes":modes,
        "ranges":ranges,
        "nativeFormats":capabilities.native_formats.into_iter().map(|format|format!("{format:?}")).collect::<Vec<_>>(),
        "conversions":conversions,
        "limitations":capabilities.limitations
    })
}

fn system_for(backend: Option<BackendId>) -> Result<CameraSystem> {
    let policy = backend
        .map(BackendPolicy::Require)
        .unwrap_or(BackendPolicy::PlatformDefault);
    Ok(CameraSystem::builder().backend_policy(policy).build()?)
}

#[cfg(feature = "uvc")]
fn usb_devices() -> Result<Vec<Value>> {
    Ok(android::usb_manager_scan()?
        .into_iter()
        .map(|d| json!({"id":d.device_path,"index":d.index,"name":d.name,"description":d.description,"vendorId":d.vendor_id,"productId":d.product_id}))
        .collect())
}
#[cfg(not(feature = "uvc"))]
fn usb_devices() -> Result<Vec<Value>> {
    Err(camera::CameraError::backend_not_compiled(BackendId::UVC).into())
}

#[cfg(feature = "uvc")]
fn open_usb_path(path: &str) -> Result<Device> {
    let fd = android::usb_device_open(path)?;
    Ok(runtime().block_on(system_for(Some(BackendId::UVC))?.open_usb_fd(fd.as_fd()))?)
}
#[cfg(not(feature = "uvc"))]
fn open_usb_path(_path: &str) -> Result<Device> {
    Err(camera::CameraError::backend_not_compiled(BackendId::UVC).into())
}

#[cfg(feature = "uvc")]
fn open_usb_descriptor(fd: jint) -> Result<Device> {
    let owned = owned_fd::duplicate(fd)?;
    Ok(runtime().block_on(system_for(Some(BackendId::UVC))?.open_usb_fd(owned.as_fd()))?)
}
#[cfg(not(feature = "uvc"))]
fn open_usb_descriptor(_fd: jint) -> Result<Device> {
    Err(camera::CameraError::backend_not_compiled(BackendId::UVC).into())
}
fn boundary<T: Default>(
    env: &mut JNIEnv<'_>,
    operation: impl FnOnce(&mut JNIEnv<'_>) -> Result<T>,
) -> T {
    match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| operation(env))) {
        Ok(Ok(value)) => value,
        Ok(Err(error)) => {
            let _ = env.throw_new(
                "com/medivh/camera/CameraException",
                error.boundary_payload(),
            );
            T::default()
        }
        Err(_) => {
            let payload =
                CameraError::adapter(camera::CameraErrorKind::Internal, "Camera adapter panicked")
                    .boundary_payload();
            let _ = env.throw_new("com/medivh/camera/CameraException", payload);
            T::default()
        }
    }
}
fn output(env: &mut JNIEnv<'_>, value: Value) -> Result<jstring> {
    Ok(env.new_string(value.to_string())?.into_raw())
}
fn text(env: &mut JNIEnv<'_>, value: JString<'_>) -> Result<String> {
    Ok(env.get_string(&value)?.into())
}

#[no_mangle]
/// JNI entry point that stores the application context used for USB access.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeInitWithContext(
    mut env: JNIEnv,
    class: JClass,
    context: JObject,
) {
    let _ = class;
    boundary(&mut env, |env| {
        let vm = env.get_java_vm()?;
        let context = env.new_global_ref(context)?;
        android::init_android_context(vm, context);
        Ok(())
    });
}
#[no_mangle]
/// JNI entry point that enumerates devices for the requested backend.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeDevices(
    mut env: JNIEnv,
    _class: JClass,
    name: JString,
) -> jstring {
    boundary(&mut env, |env| {
        let selected = backend(&text(env, name)?)?;
        let devices = if selected.as_ref() == Some(&BackendId::UVC) {
            usb_devices()?
        } else {
            runtime().block_on(system_for(selected)?.devices())?.into_iter().map(|d|json!({"id":d.id.native_id(),"persistentId":d.id.to_persistent_string(),"index":d.index,"name":d.name,"description":d.description,"facing":format!("{:?}",d.facing)})).collect()
        };
        output(env, json!(devices))
    })
}

#[no_mangle]
/// JNI entry point that returns the device capability matrix as JSON.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeCapabilities(
    mut env: JNIEnv,
    _class: JClass,
    name: JString,
    id: JString,
) -> jstring {
    boundary(&mut env, |env| {
        let selected = backend(&text(env, name)?)?;
        let native_id = text(env, id)?;
        let capabilities = if selected.as_ref() == Some(&BackendId::UVC) {
            let camera = open_usb_path(&native_id)?;
            runtime().block_on(camera.capabilities())?
        } else {
            let system = system_for(selected)?;
            let device = runtime()
                .block_on(system.devices())?
                .into_iter()
                .find(|device| device.id.native_id() == native_id)
                .ok_or_else(|| camera::CameraError::device_not_found(native_id.clone()))?;
            runtime().block_on(system.capabilities(&device.id))?
        };
        output(env, capability_payload(capabilities))
    })
}
#[no_mangle]
/// JNI entry point that reports whether a USB device path is authorized.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeHasPermission(
    mut env: JNIEnv,
    _class: JClass,
    path: JString,
) -> jboolean {
    boundary(&mut env, |env| {
        Ok(if android::usb_device_has_permission(&text(env, path)?)? {
            JNI_TRUE
        } else {
            JNI_FALSE
        })
    })
}
#[no_mangle]
/// JNI entry point that starts Android's USB permission flow.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeRequestPermission(
    mut env: JNIEnv,
    _class: JClass,
    path: JString,
) {
    boundary(&mut env, |env| {
        android::usb_device_request_permission(&text(env, path)?)
    })
}
#[no_mangle]
/// JNI entry point that opens a camera by backend name and native device ID.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeOpen(
    mut env: JNIEnv,
    _class: JClass,
    name: JString,
    id: JString,
) -> jlong {
    boundary(&mut env, |env| {
        let selected = backend(&text(env, name)?)?;
        let id = text(env, id)?;
        let camera = if selected.as_ref() == Some(&BackendId::UVC) {
            open_usb_path(&id)?
        } else {
            let system = system_for(selected)?;
            runtime().block_on(async {
                let device = system
                    .devices()
                    .await?
                    .into_iter()
                    .find(|d| d.id.native_id() == id)
                    .ok_or_else(|| camera::CameraError::device_not_found(id.clone()))?;
                system.open(DeviceSelector::Id(device.id)).await
            })?
        };
        store(camera)
    })
}
#[no_mangle]
/// JNI entry point that opens UVC capture from an authorized USB descriptor.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeOpenUsbFd(
    mut env: JNIEnv,
    _class: JClass,
    fd: jint,
) -> jlong {
    boundary(&mut env, |_| {
        if fd < 0 {
            return Err(invalid("Negative USB descriptor"));
        }
        store(open_usb_descriptor(fd)?)
    })
}

fn start_handle(
    id: jlong,
    width: jint,
    height: jint,
    numerator: jint,
    denominator: jint,
    format: Option<camera::CaptureFormat>,
    native_output: bool,
) -> Result<Value> {
    if width <= 0 || height <= 0 || numerator <= 0 || denominator <= 0 {
        return Err(invalid("Dimensions and frame rate must be positive"));
    }
    #[cfg(not(feature = "convert-rgb"))]
    if !native_output {
        return Err(invalid(
            "RGB delivery was requested but camera-android/convert-rgb is not enabled",
        ));
    }
    let handle = handle(id)?;
    let mut builder = CaptureRequest::builder()
        .preferred_frame_rate(FrameRate::new(numerator as u32, denominator as u32)?);
    if let Some(format) = format {
        builder = builder
            .exact_resolution(width as u32, height as u32)
            .preferred_formats([format]);
    } else {
        builder = builder.preferred_resolution(width as u32, height as u32);
    }
    let request = builder.build()?;
    let _gate = handle
        .lifecycle
        .lock()
        .map_err(|_| invalid("Lifecycle state poisoned"))?;
    if handle.closed.load(Ordering::Acquire) {
        return Err(invalid("Camera is closed"));
    }
    let session = runtime().block_on(async {
        let mut camera = handle.camera.lock().await;
        camera.start(request).await
    })?;
    handle.native_output.store(native_output, Ordering::Release);
    let config = session.negotiated();
    let result = json!({"width":config.capture.width,"height":config.capture.height,"frameRateNumerator":config.capture.frame_rate.numerator(),"frameRateDenominator":config.capture.frame_rate.denominator(),"captureFormat":format!("{:?}",config.capture.format),"output":if native_output {"Native"} else {"Rgb8"},"adjustments":config.adjustments});
    *handle
        .session
        .lock()
        .map_err(|_| invalid("Session state poisoned"))? = Some(Arc::new(session));
    Ok(result)
}

#[no_mangle]
/// JNI entry point that starts capture using automatic format negotiation.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeStart(
    mut env: JNIEnv,
    _class: JClass,
    id: jlong,
    width: jint,
    height: jint,
    numerator: jint,
    denominator: jint,
    native: jboolean,
) -> jstring {
    boundary(&mut env, |env| {
        output(
            env,
            start_handle(
                id,
                width,
                height,
                numerator,
                denominator,
                None,
                native == JNI_TRUE,
            )?,
        )
    })
}

#[no_mangle]
/// JNI entry point that starts capture with an explicitly requested format.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeStartWithFormat(
    mut env: JNIEnv,
    _class: JClass,
    id: jlong,
    width: jint,
    height: jint,
    numerator: jint,
    denominator: jint,
    format: JString,
    native: jboolean,
) -> jstring {
    boundary(&mut env, |env| {
        let format = capture_format(&text(env, format)?)?;
        output(
            env,
            start_handle(
                id,
                width,
                height,
                numerator,
                denominator,
                Some(format),
                native == JNI_TRUE,
            )?,
        )
    })
}
#[cfg(feature = "convert-rgb")]
fn contiguous_rgb(frame: &Frame) -> Option<&[u8]> {
    let layout = frame.layout();
    let plane = layout.planes.first()?;
    let row = usize::try_from(layout.width).ok()?.checked_mul(3)?;
    let length = row.checked_mul(usize::try_from(layout.height).ok()?)?;
    if layout.format != PixelFormat::Rgb8
        || layout.bottom_up
        || layout.planes.len() != 1
        || plane.pixel_stride != 3
        || plane.row_stride != row
        || plane.length < length
    {
        return None;
    }
    frame
        .bytes()
        .get(plane.offset..plane.offset.checked_add(length)?)
}

#[cfg(feature = "convert-rgb")]
fn rgb_to_android_argb(rgb: &[u8], output: &mut [u8]) -> Result<()> {
    if !rgb.len().is_multiple_of(3) || output.len() < rgb.len() / 3 * 4 {
        return Err(invalid("Invalid RGB/ARGB buffer lengths"));
    }
    let (source_pixels, _) = rgb.as_chunks::<3>();
    let (target_pixels, _) = output.as_chunks_mut::<4>();
    for (source, target) in source_pixels.iter().zip(target_pixels) {
        let argb = 0xff00_0000
            | (u32::from(source[0]) << 16)
            | (u32::from(source[1]) << 8)
            | u32::from(source[2]);
        target.copy_from_slice(&argb.to_ne_bytes());
    }
    Ok(())
}

fn next_frame_into(
    id: jlong,
    pointer: *mut u8,
    capacity: usize,
    timeout_ms: jint,
    android_argb: bool,
) -> Result<Value> {
    let handle = handle(id)?;
    let current = session(&handle)?;
    let native_output = handle.native_output.load(Ordering::Acquire);
    if android_argb && native_output {
        return Err(invalid(
            "Android ARGB output requires an RGB capture session",
        ));
    }
    let (frame, byte_length) = runtime().block_on(async {
        let mut reader = handle.reader.lock().await;
        if reader
            .as_ref()
            .is_none_or(|r| !Arc::ptr_eq(&r.session, &current))
        {
            *reader = Some(Reader {
                frames: current.subscribe(SubscriptionOptions::latest())?,
                session: current,
                pending: None,
                #[cfg(feature = "convert-rgb")]
                converter: RgbConverter::new(),
                #[cfg(feature = "convert-rgb")]
                rgb: Vec::new(),
            });
        }
        let reader = reader.as_mut().unwrap();
        if reader.pending.is_none() {
            reader.pending = Some(
                reader
                    .frames
                    .next_timeout(Duration::from_millis(timeout_ms as u64))
                    .await?,
            );
        }
        let frame = reader.pending.as_ref().unwrap().clone();
        let byte_length = if native_output {
            let required = frame.bytes().len();
            if capacity < required {
                return Err(invalid(format!("Direct buffer needs {required} bytes")));
            }
            unsafe { std::ptr::copy_nonoverlapping(frame.bytes().as_ptr(), pointer, required) };
            required
        } else {
            #[cfg(feature = "convert-rgb")]
            {
                let request = ConversionRequest::for_layout(frame.layout());
                let rgb_length = request.output_len()?;
                let required = if android_argb {
                    rgb_length
                        .checked_div(3)
                        .and_then(|pixels| pixels.checked_mul(4))
                        .ok_or_else(|| invalid("ARGB frame size overflow"))?
                } else {
                    rgb_length
                };
                if capacity < required {
                    return Err(invalid(format!("Direct buffer needs {required} bytes")));
                }
                let output = unsafe { std::slice::from_raw_parts_mut(pointer, required) };
                if let Some(rgb) = contiguous_rgb(&frame) {
                    if android_argb {
                        rgb_to_android_argb(rgb, output)?;
                    } else {
                        output.copy_from_slice(rgb);
                    }
                } else {
                    reader.rgb.resize(rgb_length, 0);
                    let Reader { converter, rgb, .. } = &mut *reader;
                    converter.convert_into(&frame, request, rgb)?;
                    if android_argb {
                        rgb_to_android_argb(rgb, output)?;
                    } else {
                        output.copy_from_slice(rgb);
                    }
                }
                required
            }
            #[cfg(not(feature = "convert-rgb"))]
            unreachable!("RGB start is rejected when conversion is not compiled");
        };
        reader.pending.take();
        Ok((frame, byte_length))
    })?;
    let layout = frame.layout();
    let (pixel_format, planes) = if native_output {
        (
            format!("{:?}", layout.format),
            layout.planes.iter().map(|p|json!({"offset":p.offset,"length":p.length,"rowStride":p.row_stride,"pixelStride":p.pixel_stride})).collect::<Vec<_>>(),
        )
    } else {
        let pixel_stride = if android_argb { 4 } else { 3 };
        (
            if android_argb { "Argb8" } else { "Rgb8" }.to_owned(),
            vec![
                json!({"offset":0,"length":byte_length,"rowStride":layout.width as usize * pixel_stride,"pixelStride":pixel_stride}),
            ],
        )
    };
    Ok(
        json!({"session":frame.key.session,"sequence":frame.key.sequence,"byteLength":byte_length,"width":layout.width,"height":layout.height,"pixelFormat":pixel_format,"planes":planes,"colorMatrix":format!("{:?}",layout.color.matrix),"colorRange":format!("{:?}",layout.color.range),"sourceTimestampNs":frame.source_timestamp_ns,"clock":frame.source_timestamp().map(|t|format!("{:?}",t.clock)),"rotation":layout.orientation.rotation_degrees,"mirrored":layout.orientation.mirrored,"bottomUp":if native_output {layout.bottom_up} else {false}}),
    )
}

fn next_frame_boundary(
    env: &mut JNIEnv<'_>,
    id: jlong,
    buffer: JByteBuffer,
    timeout_ms: jint,
    android_argb: bool,
) -> Result<jstring> {
    if !(0..=60000).contains(&timeout_ms) {
        return Err(invalid("Frame timeout must be 0..60000 ms"));
    }
    if env.call_method(&buffer, "isReadOnly", "()Z", &[])?.z()? {
        return Err(invalid("Frame buffer is read-only"));
    }
    let capacity = env.get_direct_buffer_capacity(&buffer)?;
    let pointer = env.get_direct_buffer_address(&buffer)?;
    output(
        env,
        next_frame_into(id, pointer, capacity, timeout_ms, android_argb)?,
    )
}

/// The caller exclusively owns the direct buffer for the duration of this call.
/// The adapter copies one frame and returns metadata in JSON. A small buffer
/// leaves the frame pending, so callers may resize and retry without losing it.
#[no_mangle]
/// JNI entry point that copies the next frame into a direct byte buffer.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeNextFrame(
    mut env: JNIEnv,
    _class: JClass,
    id: jlong,
    buffer: JByteBuffer,
    timeout_ms: jint,
) -> jstring {
    boundary(&mut env, |env| {
        next_frame_boundary(env, id, buffer, timeout_ms, false)
    })
}

/// Writes native-endian ARGB words suitable for Bitmap.Config.ARGB_8888.
#[no_mangle]
/// JNI entry point that copies the next frame as Android-compatible ARGB pixels.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeNextFrameArgb(
    mut env: JNIEnv,
    _class: JClass,
    id: jlong,
    buffer: JByteBuffer,
    timeout_ms: jint,
) -> jstring {
    boundary(&mut env, |env| {
        next_frame_boundary(env, id, buffer, timeout_ms, true)
    })
}
#[no_mangle]
/// JNI entry point that returns capture and delivery metrics as JSON.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeMetrics(
    mut env: JNIEnv,
    _class: JClass,
    id: jlong,
) -> jstring {
    boundary(&mut env, |env| {
        let handle = handle(id)?;
        let session = session(&handle)?;
        let m = session.metrics();
        let dropped = runtime().block_on(async {
            handle
                .reader
                .lock()
                .await
                .as_ref()
                .map(|r| r.frames.dropped_frames())
                .unwrap_or(0)
        });
        output(
            env,
            json!({"received":m.received,"published":m.published,
            "poolDrops":m.pool_drops,"conversionErrors":m.conversion_errors,
            "allocatedBuffers":m.allocated_buffers,"allocatedBytes":m.allocated_bytes,
            "retainedBuffers":m.retained_buffers,"conversionTotalNs":m.conversion_total_ns,
            "conversionP50Ns":m.conversion_p50_ns,"conversionP95Ns":m.conversion_p95_ns,
            "subscriberDrops":dropped,
            "firstFrameLatencyMs":session.negotiated().first_frame_latency.as_secs_f64()*1000.0}),
        )
    })
}

#[no_mangle]
/// JNI entry point that stops capture while retaining the camera handle.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeStop(
    mut env: JNIEnv,
    _class: JClass,
    id: jlong,
) {
    boundary(&mut env, |_| {
        let handle = handle(id)?;
        let _gate = handle
            .lifecycle
            .lock()
            .map_err(|_| invalid("Lifecycle state poisoned"))?;
        let session = handle
            .session
            .lock()
            .map_err(|_| invalid("Session state poisoned"))?
            .take();
        if let Some(session) = session {
            runtime().block_on(session.close())?;
        }
        Ok(())
    })
}
#[no_mangle]
/// JNI entry point that stops capture and releases the camera handle.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeClose(
    mut env: JNIEnv,
    _class: JClass,
    id: jlong,
) {
    boundary(&mut env, |_| {
        let handle = registry()
            .lock()
            .map_err(|_| invalid("Handle registry poisoned"))?
            .remove(&id);
        if let Some(handle) = handle {
            handle.closed.store(true, Ordering::Release);
            let _gate = handle
                .lifecycle
                .lock()
                .map_err(|_| invalid("Lifecycle state poisoned"))?;
            let session = handle
                .session
                .lock()
                .map_err(|_| invalid("Session state poisoned"))?
                .take();
            if let Some(session) = session {
                runtime().block_on(session.close())?;
            }
        }
        Ok(())
    })
}

fn control_value_payload(value: ControlValue) -> Value {
    match value {
        ControlValue::Number(value) => json!(value),
        ControlValue::Mode(value) => json!(format!("{value:?}")),
    }
}

fn control_payload(control: camera::ControlDescriptor) -> Value {
    json!({
        "id":format!("{:?}",control.id),
        "unit":format!("{:?}",control.unit),
        "readable":control.readable,
        "writable":control.writable,
        "modes":control.modes.into_iter().map(|mode|format!("{mode:?}")).collect::<Vec<_>>(),
        "range":control.range.map(|range|json!({
            "min":range.min,
            "max":range.max,
            "step":range.step,
            "default":range.default.map(control_value_payload)
        })),
        "requiresManual":control.requires_manual.map(|id|format!("{id:?}"))
    })
}

#[no_mangle]
/// JNI entry point that returns supported controls as JSON.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeControls(
    mut env: JNIEnv,
    _class: JClass,
    id: jlong,
) -> jstring {
    boundary(&mut env, |env| {
        let session = session(&*handle(id)?)?;
        let controls = runtime().block_on(session.controls())?;
        output(
            env,
            json!(controls
                .into_iter()
                .map(control_payload)
                .collect::<Vec<_>>()),
        )
    })
}
#[no_mangle]
/// JNI entry point that reads a control value and its readback status.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeControl(
    mut env: JNIEnv,
    _class: JClass,
    id: jlong,
    name: JString,
) -> jstring {
    boundary(&mut env, |env| {
        let control = control_id(&text(env, name)?)?;
        let value = runtime().block_on(session(&*handle(id)?)?.control(control))?;
        let (kind, value) = match value {
            camera::ControlReadback::Actual(value) => ("Actual", Some(value)),
            camera::ControlReadback::Requested(value) => ("Requested", Some(value)),
            camera::ControlReadback::Unknown => ("Unknown", None),
        };
        let value = value.map(control_value_payload);
        output(env, json!({"kind":kind,"value":value}))
    })
}
fn control_id(name: &str) -> Result<ControlId> {
    use ControlId::*;
    [
        ExposureMode,
        ExposureTime,
        ExposureCompensation,
        FocusMode,
        FocusPosition,
        WhiteBalanceMode,
        WhiteBalanceTemperature,
        Zoom,
        Brightness,
        Contrast,
        Hue,
        Saturation,
        Sharpness,
        Gamma,
        BacklightCompensation,
        Gain,
        Pan,
        Tilt,
        Iris,
    ]
    .into_iter()
    .find(|v| format!("{v:?}") == name)
    .ok_or_else(|| invalid("Unknown control ID"))
}
#[no_mangle]
/// JNI entry point that applies a numeric value or mode to a camera control.
pub extern "system" fn Java_com_medivh_camera_MedivhCameraBridge_nativeSetControl(
    mut env: JNIEnv,
    _class: JClass,
    id: jlong,
    name: JString,
    value: JString,
) {
    boundary(&mut env, |env| {
        let control = control_id(&text(env, name)?)?;
        let value: Value =
            serde_json::from_str(&text(env, value)?).map_err(|e| invalid(e.to_string()))?;
        let value = if let Some(n) = value.as_f64() {
            ControlValue::Number(n)
        } else {
            let mode = match value.as_str() {
                Some("Manual") => ControlMode::Manual,
                Some("Automatic") => ControlMode::Automatic,
                Some("Locked") => ControlMode::Locked,
                Some("Single") => ControlMode::Single,
                Some("Continuous") => ControlMode::Continuous,
                _ => return Err(invalid("Control expects a JSON number or mode string")),
            };
            ControlValue::Mode(mode)
        };
        runtime().block_on(session(&*handle(id)?)?.set_control(control, value))?;
        Ok(())
    })
}

#[cfg(test)]
mod demo_contract_tests {
    use super::*;

    #[test]
    fn demo_supports_every_android_backend_and_capture_format() {
        assert_eq!(backend("camera2").unwrap(), Some(BackendId::CAMERA2));
        assert_eq!(backend("uvc").unwrap(), Some(BackendId::UVC));
        assert_eq!(backend("v4l2").unwrap(), Some(BackendId::V4L2));
        assert_eq!(
            capture_format("Mjpeg").unwrap(),
            camera::CaptureFormat::Mjpeg
        );
        assert_eq!(capture_format("Yuyv").unwrap(), camera::CaptureFormat::Yuyv);
        assert_eq!(capture_format("Nv12").unwrap(), camera::CaptureFormat::Nv12);
        assert!(capture_format("not-a-format").is_err());
    }

    #[test]
    fn capability_payload_exposes_format_resolution_and_fractional_frame_rate() {
        let capabilities = camera::DeviceCapabilities::single_native(
            camera::CaptureFormat::Nv12,
            1920,
            1080,
            FrameRate::new(30_000, 1_001).unwrap(),
        );

        let payload = capability_payload(capabilities);
        let mode = &payload["modes"][0];
        assert_eq!(payload["knowledge"], "known");
        assert_eq!(mode["format"], "Nv12");
        assert_eq!(mode["width"], 1920);
        assert_eq!(mode["height"], 1080);
        assert_eq!(mode["frameRateNumerator"], 30_000);
        assert_eq!(mode["frameRateDenominator"], 1_001);
    }

    #[test]
    fn control_payload_includes_the_reset_default() {
        let descriptor = camera::ControlDescriptor {
            id: ControlId::Brightness,
            unit: camera::ControlUnit::Native,
            readable: true,
            writable: Some(true),
            range: Some(camera::ControlRange {
                min: 0.0,
                max: 255.0,
                step: Some(1.0),
                default: Some(ControlValue::Number(128.0)),
            }),
            modes: Vec::new(),
            requires_manual: None,
        };

        assert_eq!(control_payload(descriptor)["range"]["default"], 128.0);
    }

    #[test]
    fn rgb_to_argb_uses_android_argb8888_native_words() {
        let mut output = [0u8; 8];
        rgb_to_android_argb(&[0x11, 0x22, 0x33, 0xaa, 0xbb, 0xcc], &mut output).unwrap();

        assert_eq!(
            u32::from_ne_bytes(output[0..4].try_into().unwrap()),
            0xff11_2233
        );
        assert_eq!(
            u32::from_ne_bytes(output[4..8].try_into().unwrap()),
            0xffaa_bbcc
        );
    }
}