cranpose 0.1.93

Cranpose runtime and UI facade
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
//! iOS live camera via `AVCaptureSession`.
//!
//! Registered as the platform camera (see
//! [`cranpose_services::set_platform_camera`]) by the iOS backend. A video data
//! output delivers BGRA frames on a background dispatch queue; each frame is
//! converted to tightly-packed RGBA and stored so the app's preview pump can
//! poll it (matching the desktop/Android live viewfinder).
#![allow(unsafe_code)]

use block2::RcBlock;
use cranpose_services::{set_platform_camera, Camera, CameraError, CameraFrame, CameraStill};
use dispatch2::{DispatchQueue, DispatchRetained};
use objc2::rc::Retained;
use objc2::runtime::{AnyObject, Bool, ProtocolObject};
use objc2::{define_class, msg_send, AllocAnyThread};
use objc2_av_foundation::{
    AVCaptureAutoFocusRangeRestriction, AVCaptureConnection, AVCaptureDevice, AVCaptureDeviceInput,
    AVCaptureDevicePosition, AVCaptureDeviceType, AVCaptureDeviceTypeBuiltInDualWideCamera,
    AVCaptureDeviceTypeBuiltInTripleCamera, AVCaptureFocusMode, AVCaptureOutput, AVCapturePhoto,
    AVCapturePhotoCaptureDelegate, AVCapturePhotoOutput, AVCapturePhotoSettings,
    AVCapturePrimaryConstituentDeviceRestrictedSwitchingBehaviorConditions,
    AVCapturePrimaryConstituentDeviceSwitchingBehavior, AVCaptureSession,
    AVCaptureSessionPresetPhoto, AVCaptureTorchMode, AVCaptureVideoDataOutput,
    AVCaptureVideoDataOutputSampleBufferDelegate, AVMediaType, AVMediaTypeVideo, AVVideoCodecKey,
    AVVideoCodecTypeJPEG,
};
use objc2_core_media::CMSampleBuffer;
use objc2_core_video::{
    kCVPixelBufferPixelFormatTypeKey, CVBuffer, CVPixelBuffer, CVPixelBufferGetBaseAddress,
    CVPixelBufferGetBytesPerRow, CVPixelBufferGetHeight, CVPixelBufferGetWidth,
    CVPixelBufferLockBaseAddress, CVPixelBufferLockFlags, CVPixelBufferUnlockBaseAddress,
};
use objc2_foundation::{NSDictionary, NSError, NSNumber, NSObject, NSObjectProtocol, NSString};
use std::sync::mpsc;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::OnceLock;
use std::time::Duration;

/// `'BGRA'` — 32-bit BGRA, the pixel format we request from the video output.
const PIXEL_FORMAT_32BGRA: u32 = 0x4247_5241;

fn latest() -> &'static Mutex<Option<CameraFrame>> {
    static SLOT: OnceLock<Mutex<Option<CameraFrame>>> = OnceLock::new();
    SLOT.get_or_init(|| Mutex::new(None))
}

/// Recycled RGBA buffers for [`frame_from_sample`]. The delegate replaces
/// [`latest`] ~25×/s; a fresh multi-MB `Vec` per frame fragments the app
/// heap against any concurrently running inference's transient buffers
/// (measured on a phone: each SAM encode grew the process footprint ~500MB
/// while frames interleaved, straight into a jetsam kill — with frame
/// delivery paused the identical workload stayed flat). Replaced frames
/// park their allocation here; steady state allocates nothing.
fn buffer_pool() -> &'static Mutex<Vec<Vec<u8>>> {
    static POOL: OnceLock<Mutex<Vec<Vec<u8>>>> = OnceLock::new();
    POOL.get_or_init(|| Mutex::new(Vec::new()))
}

/// Holds the running session alive. Its AVFoundation objects are only touched
/// from `start`/`stop` (the app's single preview-pump thread) and the frame
/// delegate (its own dispatch queue, which only writes [`latest`]); marking it
/// `Send` lets it live behind the session mutex.
struct SessionHolder {
    session: Retained<AVCaptureSession>,
    photo_output: Retained<AVCapturePhotoOutput>,
    /// The capture device, kept for mid-session reconfiguration (torch).
    device: Retained<AVCaptureDevice>,
    _delegate: Retained<FrameDelegate>,
    _queue: DispatchRetained<DispatchQueue>,
}
unsafe impl Send for SessionHolder {}

fn session_slot() -> &'static Mutex<Option<SessionHolder>> {
    static SLOT: OnceLock<Mutex<Option<SessionHolder>>> = OnceLock::new();
    SLOT.get_or_init(|| Mutex::new(None))
}

/// Installs the iOS camera as the platform camera.
pub(crate) fn register() {
    set_platform_camera(Arc::new(IosCamera));
}

struct IosCamera;

impl Camera for IosCamera {
    fn start(&self) -> Result<String, CameraError> {
        if session_slot().lock().map(|s| s.is_some()).unwrap_or(false) {
            return Ok("camera".into());
        }
        start_session()
    }

    fn latest_frame(&self) -> Option<CameraFrame> {
        latest().lock().ok().and_then(|f| f.clone())
    }

    fn capture_still(&self) -> Option<CameraStill> {
        capture_photo()
    }

    fn set_torch(&self, on: bool) -> bool {
        let Ok(slot) = session_slot().lock() else {
            return false;
        };
        let Some(holder) = slot.as_ref() else {
            return false;
        };
        let device = &holder.device;
        let mode = if on {
            AVCaptureTorchMode::On
        } else {
            AVCaptureTorchMode::Off
        };
        if !unsafe { device.hasTorch() } || !unsafe { device.isTorchModeSupported(mode) } {
            return false;
        }
        if unsafe { device.lockForConfiguration() }.is_err() {
            return false;
        }
        unsafe { device.setTorchMode(mode) };
        unsafe { device.unlockForConfiguration() };
        true
    }

    fn stop(&self) {
        if let Ok(mut slot) = session_slot().lock() {
            if let Some(holder) = slot.take() {
                unsafe { holder.session.stopRunning() };
            }
        }
        if let Ok(mut f) = latest().lock() {
            *f = None;
        }
    }
}

define_class!(
    #[unsafe(super(NSObject))]
    #[name = "CranposeCameraDelegate"]
    #[ivars = ()]
    struct FrameDelegate;

    unsafe impl NSObjectProtocol for FrameDelegate {}

    unsafe impl AVCaptureVideoDataOutputSampleBufferDelegate for FrameDelegate {
        #[unsafe(method(captureOutput:didOutputSampleBuffer:fromConnection:))]
        unsafe fn did_output(
            &self,
            _output: &AVCaptureOutput,
            sample_buffer: &CMSampleBuffer,
            _connection: &AVCaptureConnection,
        ) {
            if let Some(frame) = frame_from_sample(sample_buffer) {
                if let Ok(mut slot) = latest().lock() {
                    if let Some(old) = slot.replace(frame) {
                        if let Ok(mut pool) = buffer_pool().lock() {
                            if pool.len() < 3 {
                                pool.push(old.rgba);
                            }
                        }
                    }
                }
            }
        }
    }
);

impl FrameDelegate {
    fn new() -> Retained<Self> {
        let this = Self::alloc().set_ivars(());
        unsafe { msg_send![super(this), init] }
    }
}

/// Resolves one pending still capture: the encoded JPEG, or `None` on failure.
type PhotoSender = mpsc::Sender<Option<Vec<u8>>>;

/// The pending still capture's result channel. One capture runs at a time
/// (guarded by [`capture_photo`]'s lock); the photo delegate resolves it.
fn photo_result_slot() -> &'static Mutex<Option<PhotoSender>> {
    static SLOT: OnceLock<Mutex<Option<PhotoSender>>> = OnceLock::new();
    SLOT.get_or_init(|| Mutex::new(None))
}

define_class!(
    #[unsafe(super(NSObject))]
    #[name = "CranposePhotoDelegate"]
    #[ivars = ()]
    struct PhotoDelegate;

    unsafe impl NSObjectProtocol for PhotoDelegate {}

    unsafe impl AVCapturePhotoCaptureDelegate for PhotoDelegate {
        #[unsafe(method(captureOutput:didFinishProcessingPhoto:error:))]
        unsafe fn did_finish_photo(
            &self,
            _output: &AVCapturePhotoOutput,
            photo: &AVCapturePhoto,
            error: Option<&NSError>,
        ) {
            let jpeg = if error.is_some() {
                None
            } else {
                unsafe { photo.fileDataRepresentation() }.map(|data| data.to_vec())
            };
            if let Ok(mut slot) = photo_result_slot().lock() {
                if let Some(sender) = slot.take() {
                    let _ = sender.send(jpeg);
                }
            }
        }
    }
);

impl PhotoDelegate {
    fn new() -> Retained<Self> {
        let this = Self::alloc().set_ivars(());
        unsafe { msg_send![super(this), init] }
    }
}

/// Capture one full-resolution JPEG still through `AVCapturePhotoOutput`.
///
/// Blocks the calling (worker) thread until the photo pipeline delivers the
/// encoded image or a timeout passes. The EXIF orientation in the JPEG carries
/// the sensor rotation, matching the assumption the portrait viewfinder makes.
fn capture_photo() -> Option<CameraStill> {
    // Serialize captures: the delegate resolves the single pending channel.
    static CAPTURE_GATE: Mutex<()> = Mutex::new(());
    let _gate = CAPTURE_GATE.lock().ok()?;

    let (sender, receiver) = mpsc::channel::<Option<Vec<u8>>>();
    // The delegate must outlive the async capture; hold it until the callback
    // (or timeout) resolves the channel below.
    let delegate = PhotoDelegate::new();
    {
        let slot = session_slot().lock().ok()?;
        let holder = slot.as_ref()?;
        if let Ok(mut result) = photo_result_slot().lock() {
            *result = Some(sender);
        }
        let codec_key: &NSString = unsafe { AVVideoCodecKey }?;
        let codec_value: &NSString = unsafe { AVVideoCodecTypeJPEG }?;
        let codec: &AnyObject = codec_value.as_ref();
        let format = NSDictionary::from_slices(&[codec_key], &[codec]);
        let settings = unsafe { AVCapturePhotoSettings::photoSettingsWithFormat(Some(&format)) };
        // `maxPhotoDimensions` (iOS 16+) supersedes these, but the deprecated
        // switches still map onto it and keep the iOS 15 floor working.
        #[allow(deprecated)]
        unsafe {
            settings.setHighResolutionPhotoEnabled(true);
        }
        unsafe {
            holder
                .photo_output
                .capturePhotoWithSettings_delegate(&settings, ProtocolObject::from_ref(&*delegate));
        }
    }

    let jpeg = receiver.recv_timeout(Duration::from_secs(5)).ok().flatten();
    if jpeg.is_none() {
        // Timeout or failure: clear any unresolved channel so a later capture
        // starts clean.
        if let Ok(mut result) = photo_result_slot().lock() {
            *result = None;
        }
    }
    drop(delegate);
    jpeg.map(|jpeg| CameraStill { jpeg })
}

/// Convert one BGRA sample buffer to a tightly-packed RGBA frame.
fn frame_from_sample(sample: &CMSampleBuffer) -> Option<CameraFrame> {
    let image_buffer = unsafe { sample.image_buffer() }?;
    // CVImageBuffer and CVPixelBuffer are the same Core Video object.
    let pixel_buffer: &CVPixelBuffer =
        unsafe { &*((&*image_buffer as *const CVBuffer) as *const CVPixelBuffer) };

    let flags = CVPixelBufferLockFlags::ReadOnly;
    unsafe { CVPixelBufferLockBaseAddress(pixel_buffer, flags) };

    let width = CVPixelBufferGetWidth(pixel_buffer);
    let height = CVPixelBufferGetHeight(pixel_buffer);
    let bytes_per_row = CVPixelBufferGetBytesPerRow(pixel_buffer);
    let base = CVPixelBufferGetBaseAddress(pixel_buffer) as *const u8;

    // The sensor delivers landscape buffers; rotate 90° clockwise so the
    // in-app viewfinder is upright in portrait. Output is `height` x `width`.
    let out_w = height;
    let out_h = width;
    // Recycle a parked buffer when one fits (clear keeps capacity, so after
    // the first few frames this allocates nothing at all).
    let mut rgba = buffer_pool()
        .lock()
        .ok()
        .and_then(|mut pool| pool.pop())
        .unwrap_or_default();
    rgba.clear();
    rgba.resize(out_w * out_h * 4, 0);
    if !base.is_null() && bytes_per_row >= width * 4 {
        for sy in 0..height {
            let src_row = unsafe { base.add(sy * bytes_per_row) };
            let dx = height - 1 - sy;
            for sx in 0..width {
                let src = unsafe { src_row.add(sx * 4) };
                let (b, g, r, a) = unsafe { (*src, *src.add(1), *src.add(2), *src.add(3)) };
                // 90° clockwise: src(sx, sy) -> dst(height-1-sy, sx).
                let dst = (sx * out_w + dx) * 4;
                rgba[dst] = r;
                rgba[dst + 1] = g;
                rgba[dst + 2] = b;
                rgba[dst + 3] = a;
            }
        }
    }

    unsafe { CVPixelBufferUnlockBaseAddress(pixel_buffer, flags) };

    Some(CameraFrame {
        width: out_w as u32,
        height: out_h as u32,
        rgba,
    })
}

/// Picks the back camera, preferring an auto-switching virtual multi-camera
/// (triple, then dual-wide) so the system can drop to the ultra-wide constituent
/// for macro (close-up receipts, below the wide lens's minimum focus distance).
/// Falls back to the plain wide-angle camera on devices without a virtual one.
fn select_camera_device(media_type: &AVMediaType) -> Option<Retained<AVCaptureDevice>> {
    let virtual_types: [&AVCaptureDeviceType; 2] = unsafe {
        [
            AVCaptureDeviceTypeBuiltInTripleCamera,
            AVCaptureDeviceTypeBuiltInDualWideCamera,
        ]
    };
    for device_type in virtual_types {
        if let Some(device) = unsafe {
            AVCaptureDevice::defaultDeviceWithDeviceType_mediaType_position(
                device_type,
                Some(media_type),
                AVCaptureDevicePosition::Back,
            )
        } {
            return Some(device);
        }
    }
    unsafe { AVCaptureDevice::defaultDeviceWithMediaType(media_type) }
}

fn start_session() -> Result<String, CameraError> {
    let media_type = unsafe { AVMediaTypeVideo }
        .ok_or_else(|| CameraError::Failed("AVMediaTypeVideo unavailable".into()))?;

    // Trigger the permission prompt (first launch); frames flow once granted.
    let handler = RcBlock::new(|_granted: Bool| {});
    unsafe { AVCaptureDevice::requestAccessForMediaType_completionHandler(media_type, &handler) };

    let device = select_camera_device(media_type).ok_or(CameraError::Unsupported)?;
    let name = unsafe { device.localizedName() }.to_string();

    // Enable continuous autofocus so the viewfinder keeps documents sharp as the
    // user moves the phone (the default is a fixed lens position -> blurry
    // preview). Restrict the scan range to "near" when supported, since receipts
    // and pages are held close. On a virtual multi-camera device, allow the
    // system to auto-switch to the ultra-wide constituent so very close subjects
    // (macro, below the wide lens's minimum focus distance) stay sharp. Any of
    // these calls throw if unsupported, so they are all guarded.
    if unsafe { device.lockForConfiguration() }.is_ok() {
        if unsafe { device.isAutoFocusRangeRestrictionSupported() } {
            unsafe {
                device.setAutoFocusRangeRestriction(AVCaptureAutoFocusRangeRestriction::Near)
            };
        }
        if unsafe { device.isFocusModeSupported(AVCaptureFocusMode::ContinuousAutoFocus) } {
            unsafe { device.setFocusMode(AVCaptureFocusMode::ContinuousAutoFocus) };
        }
        if unsafe { device.primaryConstituentDeviceSwitchingBehavior() }
            != AVCapturePrimaryConstituentDeviceSwitchingBehavior::Unsupported
        {
            unsafe {
                device.setPrimaryConstituentDeviceSwitchingBehavior_restrictedSwitchingBehaviorConditions(
                    AVCapturePrimaryConstituentDeviceSwitchingBehavior::Auto,
                    AVCapturePrimaryConstituentDeviceRestrictedSwitchingBehaviorConditions(0),
                )
            };
        }
        unsafe { device.unlockForConfiguration() };
    }

    let input = unsafe { AVCaptureDeviceInput::deviceInputWithDevice_error(&device) }
        .map_err(|_| CameraError::Failed("could not open camera input".into()))?;

    let session = unsafe { AVCaptureSession::new() };
    unsafe { session.beginConfiguration() };
    // The photo preset unlocks full-sensor stills through the photo output;
    // the video data output then streams preview-resolution 4:3 frames
    // (device-dependent, ~1440x1080) instead of 720p.
    let preset = unsafe { AVCaptureSessionPresetPhoto };
    if unsafe { session.canSetSessionPreset(preset) } {
        unsafe { session.setSessionPreset(preset) };
    }
    if !unsafe { session.canAddInput(&input) } {
        return Err(CameraError::Failed("cannot add camera input".into()));
    }
    unsafe { session.addInput(&input) };

    let output = unsafe { AVCaptureVideoDataOutput::new() };
    let key: &NSString =
        unsafe { &*(kCVPixelBufferPixelFormatTypeKey as *const _ as *const NSString) };
    let value = NSNumber::numberWithUnsignedInt(PIXEL_FORMAT_32BGRA);
    let value_obj: &AnyObject = &value;
    let settings = NSDictionary::from_slices(&[key], &[value_obj]);
    unsafe { output.setVideoSettings(Some(&settings)) };
    unsafe { output.setAlwaysDiscardsLateVideoFrames(true) };

    let delegate = FrameDelegate::new();
    let queue = DispatchQueue::new("com.cranpose.camera", None);
    unsafe {
        output
            .setSampleBufferDelegate_queue(Some(ProtocolObject::from_ref(&*delegate)), Some(&queue))
    };
    if !unsafe { session.canAddOutput(&output) } {
        return Err(CameraError::Failed("cannot add camera output".into()));
    }
    unsafe { session.addOutput(&output) };

    // Dedicated photo pipeline for full-resolution stills (see
    // [`Camera::capture_still`]). High-resolution capture must be opted into
    // before the session starts.
    let photo_output = unsafe { AVCapturePhotoOutput::new() };
    if !unsafe { session.canAddOutput(&photo_output) } {
        return Err(CameraError::Failed("cannot add photo output".into()));
    }
    unsafe { session.addOutput(&photo_output) };
    // `maxPhotoDimensions` (iOS 16+) supersedes this, but the deprecated
    // switch still maps onto it and keeps the iOS 15 floor working.
    #[allow(deprecated)]
    unsafe {
        photo_output.setHighResolutionCaptureEnabled(true);
    }

    unsafe { session.commitConfiguration() };
    unsafe { session.startRunning() };

    if let Ok(mut slot) = session_slot().lock() {
        *slot = Some(SessionHolder {
            session,
            photo_output,
            device,
            _delegate: delegate,
            _queue: queue,
        });
    }
    Ok(name)
}