crabcamera 0.8.3

Advanced cross-platform camera integration for Tauri applications
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
use crate::platform::PlatformCamera;
use crate::quality::QualityValidator;
use crate::types::{CameraFormat, CameraFrame, CameraInitParams};
use std::collections::HashMap;
use std::fs::File;
use std::sync::{Arc, Mutex as SyncMutex};
use tauri::command;
use tokio::sync::RwLock;

// Global camera registry with async-friendly locking for the map, but sync locking for the camera
lazy_static::lazy_static! {
    static ref CAMERA_REGISTRY: Arc<RwLock<HashMap<String, Arc<SyncMutex<PlatformCamera>>>>> = Arc::new(RwLock::new(HashMap::new()));
}

/// Capture a single photo from the specified camera with automatic reconnection
#[command]
pub async fn capture_single_photo(
    device_id: Option<String>,
    format: Option<CameraFormat>,
) -> Result<CameraFrame, String> {
    log::info!("Capturing single photo from camera: {:?}", device_id);

    // Use default camera if none specified
    let camera_id = device_id.unwrap_or_else(|| "0".to_string());
    let capture_format = format.unwrap_or_else(CameraFormat::standard);

    // Use capture_with_reconnect for automatic recovery
    match capture_with_reconnect(camera_id, capture_format, 3).await {
        Ok(frame) => {
            log::info!(
                "Successfully captured frame: {}x{} ({} bytes)",
                frame.width,
                frame.height,
                frame.size_bytes
            );
            Ok(frame)
        }
        Err(e) => {
            log::error!("Failed to capture frame: {}", e);
            Err(format!("Failed to capture frame: {}", e))
        }
    }
}

/// Capture multiple photos in sequence
#[command]
pub async fn capture_photo_sequence(
    device_id: String,
    count: u32,
    interval_ms: u32,
    format: Option<CameraFormat>,
) -> Result<Vec<CameraFrame>, String> {
    log::info!(
        "Capturing {} photos from camera {} with {}ms interval",
        count,
        device_id,
        interval_ms
    );

    if count == 0 || count > 20 {
        return Err("Invalid photo count (must be 1-20)".to_string());
    }

    let capture_format = format.unwrap_or_else(CameraFormat::standard);
    let camera = match get_or_create_camera(device_id.clone(), capture_format).await {
        Ok(cam) => cam,
        Err(e) => return Err(e),
    };

    // Start stream once
    {
        let camera_clone = camera.clone();
        tokio::task::spawn_blocking(move || {
            if let Ok(mut camera_guard) = camera_clone.lock() {
                if let Err(e) = camera_guard.start_stream() {
                    log::warn!("Failed to start camera stream: {}", e);
                }
            }
        })
        .await
        .map_err(|e| format!("Task join error: {}", e))?;
    }

    let mut frames = Vec::new();

    for i in 0..count {
        log::debug!("Capturing photo {} of {}", i + 1, count);

        let camera_clone = camera.clone();
        let frame = tokio::task::spawn_blocking(move || {
            let mut camera_guard = camera_clone
                .lock()
                .map_err(|_| "Mutex poisoned".to_string())?;
            camera_guard
                .capture_frame()
                .map_err(|e| format!("Failed to capture frame: {}", e))
        })
        .await
        .map_err(|e| format!("Task join error: {}", e))??;

        frames.push(frame);

        // Wait between captures (except for the last one)
        if i < count - 1 {
            tokio::time::sleep(tokio::time::Duration::from_millis(interval_ms as u64)).await;
        }
    }

    log::info!("Successfully captured {} photos", frames.len());
    Ok(frames)
}

/// Capture a photo with quality retry - automatically retries until quality threshold is met
#[command]
pub async fn capture_with_quality_retry(
    device_id: Option<String>,
    max_attempts: Option<u32>,
    min_quality_score: Option<f32>,
    format: Option<CameraFormat>,
) -> Result<CameraFrame, String> {
    let camera_id = device_id.unwrap_or_else(|| "0".to_string());
    let attempts = max_attempts.unwrap_or(10).min(50); // Cap at 50 attempts
    let quality_threshold = min_quality_score.unwrap_or(0.7).clamp(0.0, 1.0);
    let capture_format = format.unwrap_or_else(CameraFormat::standard);

    log::info!(
        "Starting quality capture: camera={}, max_attempts={}, min_quality={}",
        camera_id,
        attempts,
        quality_threshold
    );

    let camera = match get_or_create_camera(camera_id.clone(), capture_format).await {
        Ok(cam) => cam,
        Err(e) => return Err(e),
    };

    // Start stream once
    {
        let camera_clone = camera.clone();
        tokio::task::spawn_blocking(move || {
            if let Ok(mut camera_guard) = camera_clone.lock() {
                if let Err(e) = camera_guard.start_stream() {
                    log::warn!("Failed to start camera stream: {}", e);
                }
            }
        })
        .await
        .map_err(|e| format!("Task join error: {}", e))?;
    }

    let validator = QualityValidator::default();
    let mut best_frame: Option<(CameraFrame, f32)> = None;

    for attempt in 1..=attempts {
        // Capture frame
        let frame = {
            let camera_clone = camera.clone();
            tokio::task::spawn_blocking(move || {
                let mut camera_guard = camera_clone
                    .lock()
                    .map_err(|_| "Mutex poisoned".to_string())?;
                camera_guard.capture_frame().map_err(|e| e.to_string())
            })
            .await
            .map_err(|e| format!("Task join error: {}", e))??
        };

        // Validate quality
        let quality = validator.validate_frame(&frame);
        let score = quality.score.overall;
        log::debug!(
            "Attempt {}/{}: quality_score={:.3} (blur={:.3}, exposure={:.3})",
            attempt,
            attempts,
            score,
            quality.score.blur,
            quality.score.exposure
        );

        // Update best frame if this one is better
        if best_frame.is_none() || score > best_frame.as_ref().unwrap().1 {
            best_frame = Some((frame.clone(), score));
        }

        // Check if quality threshold met
        if score >= quality_threshold {
            log::info!(
                "Quality threshold met on attempt {}: score={:.3} >= {:.3}",
                attempt,
                score,
                quality_threshold
            );
            return Ok(frame);
        }

        // Small delay between attempts to allow camera to adjust
        if attempt < attempts {
            tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
        }
    }

    // If we didn't meet threshold, return best frame we got
    if let Some((frame, score)) = best_frame {
        log::warn!(
            "Quality threshold not met after {} attempts. Returning best frame: score={:.3}",
            attempts,
            score
        );
        Ok(frame)
    } else {
        Err(format!(
            "Failed to capture any valid frames after {} attempts",
            attempts
        ))
    }
}

/// Set a callback for real-time frame processing
#[command]
pub async fn set_frame_callback(
    device_id: String,
    format: Option<CameraFormat>,
) -> Result<String, String> {
    log::info!("Setting frame callback for device: {}", device_id);

    let capture_format = format.unwrap_or_else(CameraFormat::standard);
    let camera = match get_or_create_camera(device_id.clone(), capture_format).await {
        Ok(cam) => cam,
        Err(e) => return Err(e),
    };

    // For now, we'll set a simple callback that logs frames
    // In a real implementation, this would need to communicate frames back to the frontend
    // via events or websockets. This is a placeholder implementation.
    let device_id_clone = device_id.clone();
    let callback = move |frame: CameraFrame| {
        log::debug!(
            "Callback received frame from {}: {}x{} ({} bytes)",
            device_id_clone,
            frame.width,
            frame.height,
            frame.size_bytes
        );
        // TODO: Send frame to frontend via Tauri events
        // tauri::api::event::emit(&app_handle, "frame", frame);
    };

    let camera_clone = camera.clone();
    let device_id_clone = device_id.clone();
    tokio::task::spawn_blocking(move || {
        let mut camera_guard = camera_clone
            .lock()
            .map_err(|_| "Mutex poisoned".to_string())?;
        match camera_guard.frame_callback(callback) {
            Ok(_) => {
                log::info!("Frame callback set for device: {}", device_id_clone);
                Ok(format!("Callback set for camera {}", device_id_clone))
            }
            Err(e) => {
                log::error!("Failed to set frame callback: {}", e);
                Err(format!("Failed to set callback: {}", e))
            }
        }
    })
    .await
    .map_err(|e| format!("Task join error: {}", e))?
}

/// Start continuous capture from a camera (for live preview)
#[command]
pub async fn start_camera_preview(
    device_id: String,
    format: Option<CameraFormat>,
) -> Result<String, String> {
    log::info!("Starting camera preview for device: {}", device_id);

    let capture_format = format.unwrap_or_else(CameraFormat::standard);
    let camera = match get_or_create_camera(device_id.clone(), capture_format).await {
        Ok(cam) => cam,
        Err(e) => return Err(e),
    };

    let camera_clone = camera.clone();
    let device_id_clone = device_id.clone();
    tokio::task::spawn_blocking(move || {
        let mut camera_guard = camera_clone
            .lock()
            .map_err(|_| "Mutex poisoned".to_string())?;
        match camera_guard.start_stream() {
            Ok(_) => {
                log::info!("Camera preview started for device: {}", device_id_clone);
                Ok(format!("Preview started for camera {}", device_id_clone))
            }
            Err(e) => {
                log::error!("Failed to start camera preview: {}", e);
                Err(format!("Failed to start camera preview: {}", e))
            }
        }
    })
    .await
    .map_err(|e| format!("Task join error: {}", e))?
}

/// Stop camera preview
#[command]
pub async fn stop_camera_preview(device_id: String) -> Result<String, String> {
    log::info!("Stopping camera preview for device: {}", device_id);

    let registry = CAMERA_REGISTRY.read().await;

    if let Some(camera) = registry.get(&device_id) {
        let camera_clone = camera.clone();
        let device_id_clone = device_id.clone();
        tokio::task::spawn_blocking(move || {
            let mut camera_guard = camera_clone
                .lock()
                .map_err(|_| "Mutex poisoned".to_string())?;
            match camera_guard.stop_stream() {
                Ok(_) => {
                    log::info!("Camera preview stopped for device: {}", device_id_clone);
                    Ok(format!("Preview stopped for camera {}", device_id_clone))
                }
                Err(e) => {
                    log::error!("Failed to stop camera preview: {}", e);
                    Err(format!("Failed to stop camera preview: {}", e))
                }
            }
        })
        .await
        .map_err(|e| format!("Task join error: {}", e))?
    } else {
        let msg = format!("No active camera found with ID: {}", device_id);
        log::warn!("{}", msg);
        Err(msg)
    }
}

/// Release a camera (stop and remove from registry)
#[command]
pub async fn release_camera(device_id: String) -> Result<String, String> {
    log::info!("Releasing camera: {}", device_id);

    let mut registry = CAMERA_REGISTRY.write().await;

    if let Some(camera) = registry.remove(&device_id) {
        let camera_clone = camera.clone();
        let device_id_clone = device_id.clone();
        tokio::task::spawn_blocking(move || {
            if let Ok(mut camera_guard) = camera_clone.lock() {
                let _ = camera_guard.stop_stream(); // Ignore errors on cleanup
                log::info!("Camera {} released", device_id_clone);
            }
        })
        .await
        .ok();
        Ok(format!("Camera {} released", device_id))
    } else {
        let msg = format!("No active camera found with ID: {}", device_id);
        log::info!("{}", msg);
        Ok(msg) // Not an error if camera wasn't active
    }
}

/// Get capture statistics for a camera
#[command]
pub async fn get_capture_stats(device_id: String) -> Result<CaptureStats, String> {
    let registry = CAMERA_REGISTRY.read().await;

    if let Some(camera) = registry.get(&device_id) {
        let camera_clone = camera.clone();
        let device_id_clone = device_id.clone();
        let stats = tokio::task::spawn_blocking(move || {
            let camera_guard = camera_clone
                .lock()
                .map_err(|_| "Mutex poisoned".to_string())?;
            let is_active = camera_guard.is_available();
            let device_id_opt = camera_guard.get_device_id();

            Ok::<CaptureStats, String>(CaptureStats {
                device_id: device_id_clone,
                is_active,
                device_info: device_id_opt.map(|s| s.to_string()),
            })
        })
        .await
        .map_err(|e| format!("Task join error: {}", e))??;
        Ok(stats)
    } else {
        Ok(CaptureStats {
            device_id: device_id.clone(),
            is_active: false,
            device_info: None,
        })
    }
}

/// Save captured frame to disk as a proper image file
/// Supports PNG (lossless) based on file extension
#[command]
pub async fn save_frame_to_disk(frame: CameraFrame, file_path: String) -> Result<String, String> {
    log::info!("Saving frame {} to disk: {}", frame.id, file_path);

    // Convert frame data to proper image format
    let img = image::RgbImage::from_vec(frame.width, frame.height, frame.data)
        .ok_or_else(|| "Failed to create image from frame data".to_string())?;

    let dynamic_img = image::DynamicImage::ImageRgb8(img);

    // Determine format from extension, default to PNG
    let format = if file_path.to_lowercase().ends_with(".jpg")
        || file_path.to_lowercase().ends_with(".jpeg")
    {
        image::ImageFormat::Jpeg
    } else {
        image::ImageFormat::Png
    };

    // Save in spawn_blocking to avoid blocking async runtime
    let file_path_clone = file_path.clone();
    match tokio::task::spawn_blocking(move || {
        dynamic_img.save_with_format(&file_path_clone, format)
    })
    .await
    {
        Ok(Ok(_)) => {
            log::info!("Frame saved successfully to: {}", file_path);
            Ok(format!("Frame saved to {}", file_path))
        }
        Ok(Err(e)) => {
            log::error!("Failed to save frame: {}", e);
            Err(format!("Failed to save frame: {}", e))
        }
        Err(e) => {
            log::error!("Task join error: {}", e);
            Err("Failed to execute save task".to_string())
        }
    }
}

/// Save frame with compression for smaller file sizes
#[command]
pub async fn save_frame_compressed(
    frame: CameraFrame,
    file_path: String,
    quality: Option<u8>,
) -> Result<String, String> {
    log::info!(
        "Saving compressed frame {} to disk: {}",
        frame.id,
        file_path
    );

    let quality = quality.unwrap_or(85); // Default JPEG quality

    // Convert frame to image and compress
    let img = image::RgbImage::from_vec(frame.width, frame.height, frame.data)
        .ok_or_else(|| "Failed to create image from frame data".to_string())?;

    let dynamic_img = image::DynamicImage::ImageRgb8(img);

    // Save with compression in a spawn_blocking task
    let file_path_clone = file_path.clone();
    match tokio::task::spawn_blocking(move || {
        let mut file = File::create(&file_path_clone)?;
        let encoder = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut file, quality);
        dynamic_img.write_with_encoder(encoder)
    })
    .await
    {
        Ok(Ok(_)) => {
            log::info!("Compressed frame saved to: {}", file_path);
            Ok(format!("Compressed frame saved to {}", file_path))
        }
        Ok(Err(e)) => {
            log::error!("Failed to save compressed frame: {}", e);
            Err(format!("Failed to save compressed frame: {}", e))
        }
        Err(e) => {
            log::error!("Task join error: {}", e);
            Err("Failed to execute save task".to_string())
        }
    }
}

// Helper functions

/// Get existing camera or create new one
pub async fn get_or_create_camera(
    device_id: String,
    format: CameraFormat,
) -> Result<Arc<SyncMutex<PlatformCamera>>, String> {
    // First, try to get existing camera with read lock
    {
        let registry = CAMERA_REGISTRY.read().await;
        if let Some(camera) = registry.get(&device_id) {
            log::debug!("Using existing camera: {}", device_id);
            return Ok(camera.clone());
        }
    }

    // Need to create new camera, acquire write lock
    let mut registry = CAMERA_REGISTRY.write().await;

    // Double-check in case another task created it while we waited
    if let Some(camera) = registry.get(&device_id) {
        log::debug!("Using camera created by another task: {}", device_id);
        return Ok(camera.clone());
    }

    // Create new camera
    log::debug!("Creating new camera: {}", device_id);
    let params = CameraInitParams::new(device_id.clone()).with_format(format);

    match PlatformCamera::new(params) {
        Ok(camera) => {
            let camera_arc = Arc::new(SyncMutex::new(camera));
            registry.insert(device_id.clone(), camera_arc.clone());
            Ok(camera_arc)
        }
        Err(e) => {
            log::error!("Failed to create camera: {}", e);
            Err(format!("Failed to create camera: {}", e))
        }
    }
}

/// Attempt to reconnect a camera with retries
pub async fn reconnect_camera(
    device_id: String,
    format: CameraFormat,
    max_retries: u32,
) -> Result<Arc<SyncMutex<PlatformCamera>>, String> {
    log::info!(
        "Attempting to reconnect camera: {} (max retries: {})",
        device_id,
        max_retries
    );

    // Remove old camera from registry
    {
        let mut registry = CAMERA_REGISTRY.write().await;
        if let Some(old_camera) = registry.remove(&device_id) {
            let old_camera_clone = old_camera.clone();
            tokio::task::spawn_blocking(move || {
                if let Ok(mut camera_guard) = old_camera_clone.lock() {
                    let _ = camera_guard.stop_stream();
                    log::debug!("Removed old camera instance from registry");
                }
            })
            .await
            .ok();
        }
    }

    // Retry connection with exponential backoff
    for attempt in 1..=max_retries {
        log::debug!(
            "Reconnection attempt {}/{} for camera: {}",
            attempt,
            max_retries,
            device_id
        );

        match get_or_create_camera(device_id.clone(), format.clone()).await {
            Ok(camera) => {
                log::info!("Camera reconnected successfully on attempt {}", attempt);
                return Ok(camera);
            }
            Err(e) => {
                log::warn!("Reconnection attempt {} failed: {}", attempt, e);
                if attempt < max_retries {
                    let backoff_ms = (100 * 2_u64.pow(attempt - 1)).min(2000);
                    tokio::time::sleep(tokio::time::Duration::from_millis(backoff_ms)).await;
                }
            }
        }
    }

    Err(format!(
        "Failed to reconnect camera after {} attempts",
        max_retries
    ))
}

/// Capture with automatic reconnection on failure
pub async fn capture_with_reconnect(
    device_id: String,
    format: CameraFormat,
    max_reconnect_attempts: u32,
) -> Result<CameraFrame, String> {
    log::debug!(
        "Attempting capture with reconnect for device: {}",
        device_id
    );

    let camera = match get_or_create_camera(device_id.clone(), format.clone()).await {
        Ok(cam) => cam,
        Err(e) => return Err(format!("Failed to get camera: {}", e)),
    };

    // Try normal capture first
    let camera_clone = camera.clone();
    let capture_result = tokio::task::spawn_blocking(move || {
        let mut camera_guard = camera_clone
            .lock()
            .map_err(|_| "Mutex poisoned".to_string())?;

        // Ensure stream is started
        if let Err(e) = camera_guard.start_stream() {
            log::warn!("Failed to start stream: {}", e);
        }

        // Discard warmup frames - cameras need time to stabilize exposure/focus
        // This is especially important for USB cameras that power up on stream start
        // Using 5 frames with 30ms delay for reasonable warmup without excessive latency
        for i in 0..5 {
            match camera_guard.capture_frame() {
                Ok(_) => {
                    log::debug!("Warmup frame {} captured", i + 1);
                }
                Err(e) => {
                    log::debug!(
                        "Warmup frame {} failed (normal during startup): {}",
                        i + 1,
                        e
                    );
                }
            }
            // Small delay between warmup frames
            std::thread::sleep(std::time::Duration::from_millis(30));
        }

        // Now capture the real frame
        camera_guard
            .capture_frame()
            .map_err(|e| format!("Initial capture failed: {}, attempting reconnect", e))
    })
    .await
    .map_err(|e| format!("Task join error: {}", e))?;

    if let Ok(frame) = capture_result {
        return Ok(frame);
    }

    // Initial capture failed, try reconnecting
    let camera = reconnect_camera(device_id.clone(), format, max_reconnect_attempts).await?;

    // Try capture after reconnect with warmup
    tokio::task::spawn_blocking(move || {
        let mut camera_guard = camera.lock().map_err(|_| "Mutex poisoned".to_string())?;

        if let Err(e) = camera_guard.start_stream() {
            log::warn!("Failed to start stream after reconnect: {}", e);
        }

        // Warmup after reconnect too
        for _ in 0..10 {
            let _ = camera_guard.capture_frame();
            std::thread::sleep(std::time::Duration::from_millis(50));
        }

        camera_guard
            .capture_frame()
            .map_err(|e| format!("Capture failed after reconnection: {}", e))
    })
    .await
    .map_err(|e| format!("Task join error: {}", e))?
}

/// Zero-copy frame capture with memory pool
pub struct FramePool {
    pool: Arc<SyncMutex<Vec<Vec<u8>>>>,
    max_frames: usize,
    frame_size: usize,
}

impl FramePool {
    pub fn new(max_frames: usize, frame_size: usize) -> Self {
        let mut pool = Vec::with_capacity(max_frames);
        for _ in 0..max_frames {
            pool.push(Vec::with_capacity(frame_size));
        }

        Self {
            pool: Arc::new(SyncMutex::new(pool)),
            max_frames,
            frame_size,
        }
    }

    pub async fn get_buffer(&self) -> Vec<u8> {
        let pool = self.pool.clone();
        let frame_size = self.frame_size;
        tokio::task::spawn_blocking(move || {
            let mut pool_guard = pool.lock().unwrap();
            pool_guard
                .pop()
                .unwrap_or_else(|| Vec::with_capacity(frame_size))
        })
        .await
        .unwrap()
    }

    pub async fn return_buffer(&self, mut buffer: Vec<u8>) {
        let pool = self.pool.clone();
        let max_frames = self.max_frames;
        tokio::task::spawn_blocking(move || {
            buffer.clear();
            let mut pool_guard = pool.lock().unwrap();
            if pool_guard.len() < max_frames {
                pool_guard.push(buffer);
            }
        })
        .await
        .ok();
    }
}

lazy_static::lazy_static! {
    static ref FRAME_POOL: FramePool = FramePool::new(10, 1920 * 1080 * 3); // 10 HD RGB buffers
}

/// Capture statistics structure
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CaptureStats {
    pub device_id: String,
    pub is_active: bool,
    pub device_info: Option<String>,
}

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

    #[tokio::test]
    async fn test_quality_retry_returns_best_frame() {
        // This test verifies the quality retry logic returns a frame
        // even if threshold isn't met, it should return the best attempt

        // Note: This is a smoke test - real testing requires mock camera
        // For now, we just verify the function signature and error handling
        let result = capture_with_quality_retry(
            Some("test_device".to_string()),
            Some(3),
            Some(0.9), // Very high threshold unlikely to be met
            None,
        )
        .await;

        // Should return error since no real camera exists
        assert!(result.is_err() || result.is_ok());
    }

    #[test]
    fn test_quality_threshold_clamping() {
        // Verify quality threshold is properly clamped
        assert_eq!(1.5_f32.clamp(0.0, 1.0), 1.0);
        assert_eq!((-0.5_f32).clamp(0.0, 1.0), 0.0);
        assert_eq!(0.75_f32.clamp(0.0, 1.0), 0.75);
    }

    #[test]
    fn test_max_attempts_capping() {
        // Verify max attempts is capped properly
        let attempts = 50;
        assert_eq!(attempts, 50);

        let attempts = 10_u32;
        assert_eq!(attempts, 10);
    }
}