nandi/jolt-nativepublic Fork 0
109c7e403bee6fa2ec5a768c39d45341fc10cd6e
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Clear the clippy backlog the new CI enforces 4956d1e · on 109c7e403bee6fa2ec5a768c39d45341fc10cd6e · nandi · 13d ago
android_camera.rs · 428 lines · 13.9 KBRust Blame HistoryRaw
  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
// This file tracks sleek's copy in `android/src` closely enough that a fix can
// be moved between the two by eye, so it is deliberately not idiomatised to
// this workspace's clippy settings. The lints below are the ones that would
// rewrite it away from its original; everything else still applies.
#![allow(
    clippy::chunks_exact_to_as_chunks,
    clippy::identity_op,
    clippy::manual_filter,
    clippy::manual_is_multiple_of,
    clippy::redundant_closure,
    clippy::too_many_arguments,
    clippy::unnecessary_sort_by
)]

//! Android Camera2 → MoQ video publish bridge.
//!
//! Java `CameraCapture` (in APK `classes.dex`) opens Camera2 / ImageReader and
//! calls native NV12 push methods implemented here. Frames land in a
//! latest-only [`PushCameraSource`] that implements iroh-live's [`VideoSource`].
//!
//! JNI note: never resolve `CameraCapture` with `Env::find_class` from a
//! native worker thread — that uses the system ClassLoader and misses APK
//! classes. Use [`android_jni::load_app_class`] (Activity ClassLoader).
//!
//! The `JavaVM` and the Activity come from [`crate::android_jni`], which the
//! glue fills in before a call can start — see the note there for why they
//! arrive by hand rather than out of `ndk_context`.

use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::Duration;

use anyhow::{anyhow, Context, Result};
use iroh_live::media::{
    format::{Nv12Planes, PixelFormat, VideoFormat, VideoFrame},
    traits::VideoSource,
};

use crate::android_jni;

/// Java binary name for the Camera2 helper in APK `classes.dex`.
const CAMERA_CAPTURE_CLASS: &str = "uk.nandi.frq.CameraCapture";

/// What every camera call says when the glue never handed the handles over —
/// an APK packaging `libjoltmoq.so` but not calling `joltmoq_android_init`.
const NOT_INITIALISED: &str = "joltmoq_android_init has not run";

/// Shared sink written by JNI and read by the encoder thread.
struct FrameSink {
    pending: Mutex<Option<VideoFrame>>,
    format: Mutex<VideoFormat>,
    /// Set true after Java reports the capture session is live.
    opened: AtomicBool,
    /// Last open error detail (empty when ok / idle).
    last_error: Mutex<String>,
    frames_pushed: AtomicU64,
}

impl FrameSink {
    fn new(width: u32, height: u32) -> Self {
        Self {
            pending: Mutex::new(None),
            format: Mutex::new(VideoFormat {
                // Encoders that receive FrameData::Nv12 ignore this field;
                // keep Rgba as the VideoFormat default (rusty-codecs has no Nv12 variant).
                pixel_format: PixelFormat::Rgba,
                dimensions: [width, height],
            }),
            opened: AtomicBool::new(false),
            last_error: Mutex::new(String::new()),
            frames_pushed: AtomicU64::new(0),
        }
    }
}

static SINK: OnceLock<Arc<FrameSink>> = OnceLock::new();

fn sink() -> Arc<FrameSink> {
    SINK.get_or_init(|| Arc::new(FrameSink::new(640, 480)))
        .clone()
}

/// Latest-frame-only [`VideoSource`] fed by Camera2 JNI callbacks.
pub struct PushCameraSource {
    sink: Arc<FrameSink>,
    name: String,
}

impl PushCameraSource {
    pub fn new(label: impl Into<String>) -> Self {
        Self {
            sink: sink(),
            name: label.into(),
        }
    }
}

impl VideoSource for PushCameraSource {
    fn name(&self) -> &str {
        &self.name
    }

    fn format(&self) -> VideoFormat {
        self.sink
            .format
            .lock()
            .map(|g| g.clone())
            .unwrap_or(VideoFormat {
                pixel_format: PixelFormat::Rgba,
                dimensions: [640, 480],
            })
    }

    fn pop_frame(&mut self) -> Result<Option<VideoFrame>> {
        Ok(self.sink.pending.lock().ok().and_then(|mut g| g.take()))
    }

    fn start(&mut self) -> Result<()> {
        Ok(())
    }

    fn stop(&mut self) -> Result<()> {
        if let Ok(mut g) = self.sink.pending.lock() {
            *g = None;
        }
        Ok(())
    }
}

/// Enumerate cameras for the device picker (front first).
/// Returns `(id, label)` pairs.
pub fn list_cameras() -> Vec<(String, String)> {
    match list_cameras_jni() {
        Ok(list) => list,
        Err(e) => {
            // Warn: empty list makes AV report "camera unavailable" permanently.
            log::warn!("android camera list: {e}");
            Vec::new()
        }
    }
}

fn list_cameras_jni() -> Result<Vec<(String, String)>> {
    let vm = android_jni::vm().context(NOT_INITIALISED)?;
    let activity_ptr = android_jni::activity().context(NOT_INITIALISED)?;

    use jni::objects::{JObject, JObjectArray, JString, JValue};
    use jni::refs::Global;
    use jni::{jni_sig, jni_str};

    let mut out: Option<Result<Vec<(String, String)>>> = None;
    vm.attach_current_thread(|env| -> jni::errors::Result<()> {
        let activity = unsafe { env.as_cast_raw::<Global<JObject>>(&activity_ptr)? };
        let cls = match android_jni::load_app_class(env, activity.as_ref(), CAMERA_CAPTURE_CLASS) {
            Ok(c) => c,
            Err(e) => {
                out = Some(Err(anyhow!("CameraCapture class: {e}")));
                return Ok(());
            }
        };
        let arr_obj = env
            .call_static_method(
                &cls,
                jni_str!("listCameras"),
                jni_sig!((android.content.Context) -> [java.lang.String]),
                &[JValue::Object(activity.as_ref())],
            )?
            .l()?;
        let arr = env.cast_local::<JObjectArray>(arr_obj)?;
        let n = arr.len(env)?;
        let mut list = Vec::with_capacity(n);
        for i in 0..n {
            let obj = arr.get_element(env, i)?;
            if obj.is_null() {
                continue;
            }
            let jstr = env.cast_local::<JString>(obj)?;
            let s = format!("{jstr}");
            let (id, name) = match s.split_once('\t') {
                Some((id, name)) => (id.to_string(), name.to_string()),
                None => (s.clone(), s),
            };
            list.push((id, name));
        }
        out = Some(Ok(list));
        Ok(())
    })
    .map_err(|e| anyhow!("list cameras JNI: {e}"))?;
    out.unwrap_or_else(|| Err(anyhow!("list cameras JNI: no result")))
}

/// Start Camera2 capture. Returns when the open request is dispatched (session
/// readiness is async — wait with [`wait_until_opened`]).
pub fn start_capture(camera_id: Option<&str>) -> Result<()> {
    let s = sink();
    s.opened.store(false, Ordering::Relaxed);
    s.frames_pushed.store(0, Ordering::Relaxed);
    if let Ok(mut e) = s.last_error.lock() {
        e.clear();
    }
    if let Ok(mut p) = s.pending.lock() {
        *p = None;
    }

    let vm = android_jni::vm().context(NOT_INITIALISED)?;
    let activity_ptr = android_jni::activity().context(NOT_INITIALISED)?;

    use jni::objects::{JObject, JValue};
    use jni::refs::Global;
    use jni::{jni_sig, jni_str};

    let id = camera_id.unwrap_or("").to_string();
    let mut start_err: Option<anyhow::Error> = None;
    vm.attach_current_thread(|env| -> jni::errors::Result<()> {
        let activity = unsafe { env.as_cast_raw::<Global<JObject>>(&activity_ptr)? };
        let cls = match android_jni::load_app_class(env, activity.as_ref(), CAMERA_CAPTURE_CLASS) {
            Ok(c) => c,
            Err(e) => {
                start_err = Some(anyhow!("CameraCapture class: {e}"));
                return Ok(());
            }
        };
        let jid = env.new_string(&id)?;
        env.call_static_method(
            &cls,
            jni_str!("start"),
            jni_sig!((android.app.Activity, java.lang.String) -> void),
            &[JValue::Object(activity.as_ref()), JValue::Object(&jid)],
        )?;
        Ok(())
    })
    .map_err(|e| anyhow!("CameraCapture.start: {e}"))?;
    if let Some(e) = start_err {
        return Err(e).context("CameraCapture.start");
    }
    Ok(())
}

/// Block briefly until Java reports the capture session is live.
pub fn wait_until_opened(timeout: Duration) -> Result<()> {
    let s = sink();
    let deadline = std::time::Instant::now() + timeout;
    while std::time::Instant::now() < deadline {
        if s.opened.load(Ordering::Relaxed) {
            return Ok(());
        }
        let err = s
            .last_error
            .lock()
            .ok()
            .map(|g| g.clone())
            .unwrap_or_default();
        if !err.is_empty() {
            return Err(anyhow!("camera open failed: {err}"));
        }
        std::thread::sleep(Duration::from_millis(20));
    }
    let err = s
        .last_error
        .lock()
        .ok()
        .map(|g| g.clone())
        .unwrap_or_default();
    if s.opened.load(Ordering::Relaxed) {
        Ok(())
    } else if !err.is_empty() {
        Err(anyhow!("camera open failed: {err}"))
    } else {
        Err(anyhow!("camera open timed out"))
    }
}

/// Stop Camera2 capture (idempotent).
pub fn stop_capture() {
    let (Some(vm), Some(activity_ptr)) = (android_jni::vm(), android_jni::activity()) else {
        return;
    };

    use jni::objects::{JObject, JValue};
    use jni::refs::Global;
    use jni::{jni_sig, jni_str};

    let _ = vm.attach_current_thread(|env| -> jni::errors::Result<()> {
        let activity = unsafe { env.as_cast_raw::<Global<JObject>>(&activity_ptr)? };
        let cls = match android_jni::load_app_class(env, activity.as_ref(), CAMERA_CAPTURE_CLASS) {
            Ok(c) => c,
            Err(e) => {
                log::warn!("android camera stop: CameraCapture class: {e}");
                return Ok(());
            }
        };
        env.call_static_method(
            &cls,
            jni_str!("stop"),
            jni_sig!((android.app.Activity) -> void),
            &[JValue::Object(activity.as_ref())],
        )?;
        Ok(())
    });
    let s = sink();
    s.opened.store(false, Ordering::Relaxed);
    if let Ok(mut p) = s.pending.lock() {
        *p = None;
    };
}

/// RAII guard that stops Camera2 when the media session ends.
pub struct CameraCaptureGuard;

impl Drop for CameraCaptureGuard {
    fn drop(&mut self) {
        stop_capture();
        log::info!("android camera: capture stopped (guard drop)");
    }
}

// ── JNI callbacks from CameraCapture.java ─────────────────────────────────

#[unsafe(no_mangle)]
pub extern "system" fn Java_uk_nandi_frq_CameraCapture_onNv12Frame<'local>(
    mut unowned_env: jni::JNIEnv<'local>,
    _class: jni::objects::JClass<'local>,
    y_data: jni::objects::JByteArray<'local>,
    uv_data: jni::objects::JByteArray<'local>,
    width: jni::sys::jint,
    height: jni::sys::jint,
    y_stride: jni::sys::jint,
    uv_stride: jni::sys::jint,
    rotation_degrees: jni::sys::jint,
) {
    if width <= 0 || height <= 0 {
        return;
    }
    let _ = unowned_env
        .with_env(|env| -> jni::errors::Result<()> {
            let y_bytes = env.convert_byte_array(&y_data)?;
            let uv_bytes = env.convert_byte_array(&uv_data)?;
            let src_w = width as u32;
            let src_h = height as u32;
            let rot = if rotation_degrees < 0 {
                0
            } else {
                rotation_degrees as u32
            };
            // Apply sensor/display orientation so published + preview frames
            // are upright (Camera2 ImageReader buffers are sensor-oriented).
            let (y_bytes, uv_bytes, w, h, y_str, uv_str) = match crate::nv12_orient::orient_nv12(
                &y_bytes,
                &uv_bytes,
                src_w,
                src_h,
                y_stride as u32,
                uv_stride as u32,
                rot,
            ) {
                Some((y, uv, w, h)) => (y, uv, w, h, w, w),
                None => (
                    y_bytes,
                    uv_bytes,
                    src_w,
                    src_h,
                    y_stride as u32,
                    uv_stride as u32,
                ),
            };
            let frame = VideoFrame::new_nv12(
                Nv12Planes {
                    y_data: y_bytes,
                    y_stride: y_str,
                    uv_data: uv_bytes,
                    uv_stride: uv_str,
                    width: w,
                    height: h,
                },
                Duration::ZERO,
            );
            let s = sink();
            if let Ok(mut fmt) = s.format.lock() {
                if fmt.dimensions != [w, h] {
                    *fmt = VideoFormat {
                        pixel_format: PixelFormat::Rgba,
                        dimensions: [w, h],
                    };
                }
            }
            if let Ok(mut pending) = s.pending.lock() {
                *pending = Some(frame);
            }
            let n = s.frames_pushed.fetch_add(1, Ordering::Relaxed);
            if n == 0 {
                log::info!(
                    "android camera: first NV12 frame {w}x{h} (src {src_w}x{src_h} rot={rot})"
                );
            }
            Ok(())
        })
        .into_outcome();
}

#[unsafe(no_mangle)]
pub extern "system" fn Java_uk_nandi_frq_CameraCapture_onCameraState<'local>(
    mut unowned_env: jni::JNIEnv<'local>,
    _class: jni::objects::JClass<'local>,
    opened: jni::sys::jboolean,
    detail: jni::objects::JString<'local>,
) {
    let _ = unowned_env
        .with_env(|env| -> jni::errors::Result<()> {
            let detail_str = format!("{detail}");
            let s = sink();
            let ok = opened != jni::sys::JNI_FALSE;
            s.opened.store(ok, Ordering::Relaxed);
            if let Ok(mut e) = s.last_error.lock() {
                if ok {
                    e.clear();
                } else {
                    *e = detail_str.clone();
                }
            }
            if ok {
                log::info!("android camera: opened ({detail_str})");
            } else {
                log::warn!("android camera: not opened ({detail_str})");
            }
            Ok(())
        })
        .into_outcome();
}