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
773
774
775
776
777
778
779
780
781
782
783
784
//! Comprehensive Audio Capture Tests for CrabCamera
//!
//! This test suite provides comprehensive coverage of audio capture functionality
//! including device enumeration, capture lifecycle, synchronization, error recovery,
//! and performance characteristics.
//!
//! Run with: cargo test --test audio_capture_test --features audio

#![cfg(feature = "audio")]

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};

use crabcamera::audio::{
    get_default_audio_device, list_audio_devices, AudioCapture, OpusEncoder, PTSClock,
};

// ═══════════════════════════════════════════════════════════════════════════
// AUDIO DEVICE TESTS
// ═══════════════════════════════════════════════════════════════════════════

/// Test audio device enumeration is safe and returns valid data
#[test]
fn test_device_enumeration_comprehensive() {
    match list_audio_devices() {
        Ok(devices) => {
            println!("Found {} audio devices", devices.len());

            // Validate each device
            for (i, device) in devices.iter().enumerate() {
                println!("Device {}: {} ({})", i, device.name, device.id);

                // Basic validation
                assert!(!device.id.is_empty(), "Device ID cannot be empty");
                assert!(!device.name.is_empty(), "Device name cannot be empty");
                assert!(device.sample_rate > 0, "Sample rate must be positive");
                assert!(
                    device.channels > 0 && device.channels <= 8,
                    "Channels must be 1-8"
                );

                // Sample rate should be reasonable
                assert!(
                    device.sample_rate >= 8000 && device.sample_rate <= 192000,
                    "Sample rate {} is unreasonable",
                    device.sample_rate
                );
            }

            // Check for default device
            let default_devices: Vec<_> = devices.iter().filter(|d| d.is_default).collect();
            assert!(
                default_devices.len() <= 1,
                "Cannot have more than one default device"
            );

            // If we have devices, at least one should be default OR we should be able to get default
            if !devices.is_empty() {
                if default_devices.is_empty() {
                    // Try to get default device explicitly
                    match get_default_audio_device() {
                        Ok(default) => {
                            println!("Default device: {}", default.name);
                            assert!(
                                default.is_default,
                                "Default device should be marked as default"
                            );
                        }
                        Err(e) => {
                            println!("Warning: No default device available: {}", e);
                        }
                    }
                }
            }
        }
        Err(e) => {
            println!(
                "Audio enumeration failed (may be expected on headless systems): {}",
                e
            );

            // Error should be descriptive
            let error_str = e.to_string();
            assert!(!error_str.is_empty(), "Error message should not be empty");
            assert!(error_str.len() > 10, "Error message should be descriptive");
        }
    }
}

/// Test getting default audio device
#[test]
fn test_default_device_handling() {
    match get_default_audio_device() {
        Ok(device) => {
            assert!(
                device.is_default,
                "Default device should be marked as default"
            );
            assert!(
                !device.id.is_empty(),
                "Default device ID should not be empty"
            );
            assert!(
                !device.name.is_empty(),
                "Default device name should not be empty"
            );

            println!(
                "Default audio device: {} (SR: {}Hz, CH: {})",
                device.name, device.sample_rate, device.channels
            );
        }
        Err(e) => {
            println!("No default audio device (expected on some systems): {}", e);
            // This is acceptable on headless systems
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// AUDIO CAPTURE LIFECYCLE TESTS
// ═══════════════════════════════════════════════════════════════════════════

/// Test audio capture creation and basic lifecycle
#[test]
fn test_capture_lifecycle_comprehensive() {
    let clock = PTSClock::new();

    // Test capture creation with different configurations
    let test_configs = vec![
        (None, 48000, 1),                        // Default device, mono
        (None, 48000, 2),                        // Default device, stereo
        (Some("default".to_string()), 48000, 2), // Explicit default, stereo
    ];

    for (device_id, sample_rate, channels) in test_configs {
        println!(
            "Testing config: device={:?}, sr={}, ch={}",
            device_id, sample_rate, channels
        );

        match AudioCapture::new(device_id.clone(), sample_rate, channels, clock.clone()) {
            Ok(mut capture) => {
                // Test initial state
                assert!(
                    !capture.is_running(),
                    "Capture should not be running initially"
                );
                assert_eq!(capture.sample_rate(), sample_rate);
                assert_eq!(capture.channels(), channels);

                // Test idempotent start
                assert!(capture.start().is_ok(), "First start should succeed");
                assert!(
                    capture.is_running(),
                    "Capture should be running after start"
                );
                assert!(capture.start().is_ok(), "Second start should be idempotent");
                assert!(capture.is_running(), "Capture should still be running");

                // Brief capture period
                thread::sleep(Duration::from_millis(100));

                // Test data availability
                let frames_before = capture.drain().len();
                thread::sleep(Duration::from_millis(50));
                let frames_after = capture.drain().len();
                println!(
                    "Captured {} frames before, {} after 50ms",
                    frames_before, frames_after
                );

                // Test idempotent stop
                assert!(capture.stop().is_ok(), "First stop should succeed");
                assert!(
                    !capture.is_running(),
                    "Capture should not be running after stop"
                );
                assert!(capture.stop().is_ok(), "Second stop should be idempotent");
                assert!(!capture.is_running(), "Capture should still be stopped");

                // Test restart
                assert!(capture.start().is_ok(), "Restart should succeed");
                assert!(
                    capture.is_running(),
                    "Capture should be running after restart"
                );

                // Final stop
                assert!(capture.stop().is_ok(), "Final stop should succeed");

                println!("✓ Lifecycle test passed for config");
            }
            Err(e) => {
                println!("Capture creation failed (may be expected): {}", e);
                // This is acceptable on systems without audio devices
            }
        }
    }
}

/// Test audio capture with different sample rates and channels
#[test]
fn test_capture_format_handling() {
    let clock = PTSClock::new();

    // Test different format configurations
    let formats = vec![
        (48000, 1), // Standard mono
        (48000, 2), // Standard stereo
        (44100, 2), // CD quality (should be resampled to 48kHz)
    ];

    for (requested_rate, channels) in formats {
        match AudioCapture::new(None, requested_rate, channels, clock.clone()) {
            Ok(capture) => {
                // The capture might adjust the format to what's actually supported
                let actual_rate = capture.sample_rate();
                let actual_channels = capture.channels();

                println!(
                    "Requested {}Hz/{}ch, got {}Hz/{}ch",
                    requested_rate, channels, actual_rate, actual_channels
                );

                // Should be reasonable values
                assert!(
                    actual_rate == 44100 || actual_rate == 48000,
                    "Sample rate should be standard: {}",
                    actual_rate
                );
                assert!(
                    actual_channels >= 1 && actual_channels <= 2,
                    "Channels should be 1 or 2: {}",
                    actual_channels
                );
            }
            Err(e) => {
                println!(
                    "Format {}Hz/{}ch not supported: {}",
                    requested_rate, channels, e
                );
            }
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// AUDIO FRAME TESTS
// ═══════════════════════════════════════════════════════════════════════════

/// Test audio frame structure and validation
#[test]
fn test_audio_frame_properties() {
    let clock = PTSClock::new();

    if let Ok(mut capture) = AudioCapture::new(None, 48000, 2, clock) {
        if capture.start().is_ok() {
            // Capture some frames
            thread::sleep(Duration::from_millis(100));
            let frames = capture.drain();

            if !frames.is_empty() {
                println!("Captured {} frames for validation", frames.len());

                for (i, frame) in frames.iter().take(5).enumerate() {
                    println!(
                        "Frame {}: {}Hz, {}ch, {:.3}s, {} samples",
                        i,
                        frame.sample_rate,
                        frame.channels,
                        frame.timestamp,
                        frame.samples.len()
                    );

                    // Validate frame properties
                    assert_eq!(
                        frame.sample_rate, 48000,
                        "Frame should have correct sample rate"
                    );
                    assert!(
                        frame.channels == 1 || frame.channels == 2,
                        "Frame should have 1-2 channels"
                    );
                    assert!(frame.timestamp >= 0.0, "Timestamp should be non-negative");
                    assert!(!frame.samples.is_empty(), "Frame should have samples");

                    // Sample count should be reasonable for the format
                    let expected_samples_per_channel =
                        frame.samples.len() / frame.channels as usize;
                    assert!(
                        expected_samples_per_channel > 0,
                        "Should have samples per channel"
                    );
                    assert!(
                        expected_samples_per_channel <= 4800,
                        "Sample count should be reasonable"
                    ); // Max ~100ms worth

                    // Samples should be valid float values
                    for (j, &sample) in frame.samples.iter().take(10).enumerate() {
                        assert!(
                            sample.is_finite(),
                            "Sample {} should be finite: {}",
                            j,
                            sample
                        );
                        assert!(
                            sample >= -2.0 && sample <= 2.0,
                            "Sample {} should be in reasonable range: {}",
                            j,
                            sample
                        );
                    }
                }

                // Check timestamp ordering
                for window in frames.windows(2) {
                    assert!(
                        window[1].timestamp >= window[0].timestamp,
                        "Timestamps should be non-decreasing: {} -> {}",
                        window[0].timestamp,
                        window[1].timestamp
                    );
                }
            } else {
                println!("No frames captured (may be expected on quiet systems)");
            }

            let _ = capture.stop();
        }
    }
}

/// Test PTS clock synchronization across multiple captures
#[test]
fn test_pts_clock_synchronization() {
    let shared_clock = PTSClock::new();
    let start_time = Instant::now();

    // Create multiple captures with the same clock
    let configs = vec![(None, 48000, 1), (None, 48000, 2)];

    let mut captures = Vec::new();
    for (device_id, sample_rate, channels) in configs {
        if let Ok(capture) =
            AudioCapture::new(device_id, sample_rate, channels, shared_clock.clone())
        {
            captures.push(capture);
        }
    }

    if captures.is_empty() {
        println!("No audio devices available for PTS sync test");
        return;
    }

    println!(
        "Testing PTS synchronization with {} captures",
        captures.len()
    );

    // Start all captures
    for capture in &mut captures {
        let _ = capture.start();
    }

    // Collect timestamps over time
    let mut all_timestamps = Vec::new();
    for i in 0..10 {
        thread::sleep(Duration::from_millis(20));
        let clock_pts = shared_clock.pts();
        all_timestamps.push((i, clock_pts, start_time.elapsed().as_secs_f64()));

        // Check that frame timestamps are close to clock PTS
        for (j, capture) in captures.iter().enumerate() {
            for frame in capture.drain() {
                let pts_diff = (frame.timestamp - clock_pts).abs();
                assert!(
                    pts_diff < 0.1,
                    "Frame PTS {} should be close to clock PTS {} (diff: {:.3}s) for capture {}",
                    frame.timestamp,
                    clock_pts,
                    pts_diff,
                    j
                );
            }
        }
    }

    // Stop all captures
    for capture in &mut captures {
        let _ = capture.stop();
    }

    // Verify clock progression
    println!("PTS progression over time:");
    for (i, pts, elapsed) in &all_timestamps {
        println!(
            "  Step {}: PTS={:.3}s, Elapsed={:.3}s, Diff={:.3}s",
            i,
            pts,
            elapsed,
            (pts - elapsed).abs()
        );
    }

    // Check that PTS closely tracks real time
    if let (Some((_, first_pts, first_elapsed)), Some((_, last_pts, last_elapsed))) =
        (all_timestamps.first(), all_timestamps.last())
    {
        let pts_duration = last_pts - first_pts;
        let real_duration = last_elapsed - first_elapsed;
        let drift = (pts_duration - real_duration).abs();

        assert!(drift < 0.1,
            "PTS should track real time within 100ms: PTS duration={:.3}s, real duration={:.3}s, drift={:.3}s",
            pts_duration, real_duration, drift);

        println!("✓ PTS synchronization test passed with {:.3}s drift", drift);
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// ERROR RECOVERY TESTS
// ═══════════════════════════════════════════════════════════════════════════

/// Test error handling with invalid device IDs
#[test]
fn test_invalid_device_handling() {
    let clock = PTSClock::new();

    let invalid_devices = vec![
        Some("nonexistent_device_12345".to_string()),
        Some("".to_string()), // Empty string
        Some("invalid/device\\name".to_string()),
    ];

    for device_id in invalid_devices {
        let result = AudioCapture::new(device_id.clone(), 48000, 2, clock.clone());
        match result {
            Ok(_) => {
                println!("Unexpectedly succeeded with device: {:?}", device_id);
                // This might happen if the system has very permissive device handling
            }
            Err(e) => {
                println!("Expected error for device {:?}: {}", device_id, e);

                // Error should be descriptive
                let error_str = e.to_string();
                assert!(!error_str.is_empty(), "Error message should not be empty");
                assert!(
                    error_str.contains("device")
                        || error_str.contains("Device")
                        || error_str.contains("audio")
                        || error_str.contains("Audio"),
                    "Error should mention device or audio: {}",
                    error_str
                );
            }
        }
    }
}

/// Test error handling with invalid sample rates and channels
#[test]
fn test_invalid_format_handling() {
    let clock = PTSClock::new();

    let invalid_formats = vec![
        (0, 2),      // Zero sample rate
        (1000, 2),   // Too low sample rate
        (500000, 2), // Too high sample rate
        (48000, 0),  // Zero channels
        (48000, 10), // Too many channels
    ];

    for (sample_rate, channels) in invalid_formats {
        println!("Testing invalid format: {}Hz, {}ch", sample_rate, channels);

        let result = AudioCapture::new(None, sample_rate, channels, clock.clone());
        match result {
            Ok(capture) => {
                // Some systems might be very permissive and adjust formats
                println!(
                    "Format adjusted to: {}Hz, {}ch",
                    capture.sample_rate(),
                    capture.channels()
                );

                // Adjusted format should be valid
                assert!(
                    capture.sample_rate() > 0,
                    "Adjusted sample rate should be positive"
                );
                assert!(
                    capture.channels() > 0,
                    "Adjusted channels should be positive"
                );
            }
            Err(e) => {
                println!("Expected error for {}Hz/{}ch: {}", sample_rate, channels, e);

                // Verify error is reasonable
                let error_str = e.to_string();
                assert!(!error_str.is_empty(), "Error should not be empty");
            }
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// PERFORMANCE AND RESOURCE TESTS
// ═══════════════════════════════════════════════════════════════════════════

/// Test audio capture performance and resource usage
#[test]
fn test_capture_performance() {
    let clock = PTSClock::new();

    if let Ok(mut capture) = AudioCapture::new(None, 48000, 2, clock) {
        if capture.start().is_ok() {
            let start_time = Instant::now();
            let mut total_frames = 0;
            let mut total_samples = 0;
            let mut max_frame_gap = 0.0f64;
            let mut last_timestamp = None;

            // Capture for 1 second
            while start_time.elapsed() < Duration::from_millis(1000) {
                let frames = capture.drain();
                total_frames += frames.len();

                for frame in frames {
                    total_samples += frame.samples.len();

                    if let Some(last_ts) = last_timestamp {
                        let gap = frame.timestamp - last_ts;
                        max_frame_gap = max_frame_gap.max(gap);
                    }
                    last_timestamp = Some(frame.timestamp);
                }

                thread::sleep(Duration::from_millis(10));
            }

            let elapsed = start_time.elapsed().as_secs_f64();

            println!("Performance metrics over {:.2}s:", elapsed);
            println!("  Total frames: {}", total_frames);
            println!("  Total samples: {}", total_samples);
            println!("  Frames per second: {:.1}", total_frames as f64 / elapsed);
            println!("  Max frame gap: {:.3}s", max_frame_gap);

            if total_frames > 0 {
                println!(
                    "  Avg samples per frame: {:.1}",
                    total_samples as f64 / total_frames as f64
                );

                // Performance assertions
                let fps = total_frames as f64 / elapsed;
                assert!(
                    fps >= 10.0,
                    "Should capture at least 10 frames per second, got {:.1}",
                    fps
                );
                assert!(
                    max_frame_gap < 0.5,
                    "Frame gaps should be < 500ms, got {:.3}s",
                    max_frame_gap
                );

                // Sample rate check
                let expected_samples = (48000.0 * 2.0 * elapsed) as usize; // 48kHz stereo
                let sample_ratio = total_samples as f64 / expected_samples as f64;
                assert!(
                    sample_ratio > 0.5 && sample_ratio < 2.0,
                    "Sample count should be reasonable: expected ~{}, got {} (ratio: {:.2})",
                    expected_samples,
                    total_samples,
                    sample_ratio
                );
            }

            let _ = capture.stop();
            println!("✓ Performance test passed");
        }
    } else {
        println!("No audio device available for performance test");
    }
}

/// Test memory usage and buffer management
#[test]
fn test_buffer_management() {
    let clock = PTSClock::new();

    if let Ok(mut capture) = AudioCapture::new(None, 48000, 2, clock) {
        if capture.start().is_ok() {
            println!("Testing buffer management with rapid draining");

            // Test rapid draining doesn't cause memory issues
            for i in 0..100 {
                let frames = capture.drain();
                if i % 20 == 0 {
                    println!("  Iteration {}: drained {} frames", i, frames.len());
                }
                thread::sleep(Duration::from_millis(5));
            }

            // Test that capture continues working after intensive draining
            thread::sleep(Duration::from_millis(50));
            let final_frames = capture.drain();
            println!("Final drain: {} frames", final_frames.len());

            let _ = capture.stop();
            println!("✓ Buffer management test passed");
        }
    }
}

/// Test concurrent access patterns
#[test]
fn test_concurrent_access_safety() {
    let clock = PTSClock::new();

    if let Ok(mut capture) = AudioCapture::new(None, 48000, 2, clock) {
        if capture.start().is_ok() {
            let stop_flag = Arc::new(AtomicBool::new(false));
            let stop_flag_clone = stop_flag.clone();

            // Spawn thread that continuously drains
            let drain_thread = thread::spawn(move || {
                let mut drain_count = 0;
                while !stop_flag_clone.load(Ordering::Relaxed) {
                    // Note: We can't share the capture between threads (not Sync)
                    // This tests that the internal buffering is thread-safe
                    thread::sleep(Duration::from_millis(1));
                    drain_count += 1;
                }
                drain_count
            });

            // Main thread also drains
            for _ in 0..50 {
                let frames = capture.drain();
                if !frames.is_empty() {
                    println!("Main thread drained {} frames", frames.len());
                }
                thread::sleep(Duration::from_millis(10));
            }

            stop_flag.store(true, Ordering::Relaxed);
            let drain_count = drain_thread.join().unwrap();
            println!("Background thread completed {} iterations", drain_count);

            let _ = capture.stop();
            println!("✓ Concurrent access test passed");
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// INTEGRATION TESTS WITH OPUS ENCODER
// ═══════════════════════════════════════════════════════════════════════════

/// Test full audio pipeline: capture -> encode
#[test]
fn test_capture_to_encode_pipeline() {
    let clock = PTSClock::new();

    if let Ok(mut capture) = AudioCapture::new(None, 48000, 2, clock) {
        if let Ok(mut encoder) = OpusEncoder::new(48000, 2, 128_000) {
            if capture.start().is_ok() {
                println!("Testing full capture -> encode pipeline");

                let mut total_input_samples = 0;
                let mut total_encoded_packets = 0;
                let mut total_encoded_bytes = 0;

                // Capture and encode for 500ms
                let start_time = Instant::now();
                while start_time.elapsed() < Duration::from_millis(500) {
                    let frames = capture.drain();

                    for frame in frames {
                        total_input_samples += frame.samples.len();

                        match encoder.encode(&frame) {
                            Ok(packets) => {
                                total_encoded_packets += packets.len();
                                for packet in packets {
                                    total_encoded_bytes += packet.data.len();

                                    // Validate packet
                                    assert!(
                                        !packet.data.is_empty(),
                                        "Encoded packet should not be empty"
                                    );
                                    assert!(
                                        packet.timestamp >= 0.0,
                                        "Packet timestamp should be non-negative"
                                    );
                                    assert!(
                                        packet.duration > 0.0,
                                        "Packet duration should be positive"
                                    );
                                    assert!(
                                        packet.duration <= 0.1,
                                        "Packet duration should be reasonable"
                                    );

                                    // Check Opus TOC byte
                                    let toc = packet.data[0];
                                    let config = (toc >> 3) & 0x1F;
                                    assert!(config < 32, "Opus config should be valid: {}", config);
                                }
                            }
                            Err(e) => {
                                println!("Encoding error: {}", e);
                            }
                        }
                    }

                    thread::sleep(Duration::from_millis(10));
                }

                // Flush remaining data
                match encoder.flush() {
                    Ok(packets) => {
                        total_encoded_packets += packets.len();
                        for packet in packets {
                            total_encoded_bytes += packet.data.len();
                        }
                    }
                    Err(e) => {
                        println!("Flush error: {}", e);
                    }
                }

                println!("Pipeline results:");
                println!("  Input samples: {}", total_input_samples);
                println!("  Encoded packets: {}", total_encoded_packets);
                println!("  Encoded bytes: {}", total_encoded_bytes);

                if total_input_samples > 0 {
                    println!(
                        "  Compression ratio: {:.2}x",
                        (total_input_samples * 4) as f64 / total_encoded_bytes as f64
                    );

                    // Should have produced some packets
                    assert!(
                        total_encoded_packets > 0,
                        "Should have produced encoded packets"
                    );
                    assert!(
                        total_encoded_bytes > 0,
                        "Should have produced encoded bytes"
                    );

                    // Compression should be reasonable (Opus should compress significantly)
                    let raw_bytes = total_input_samples * 4; // f32 samples
                    let compression_ratio = raw_bytes as f64 / total_encoded_bytes as f64;
                    assert!(
                        compression_ratio > 5.0,
                        "Opus should achieve significant compression: {:.2}x",
                        compression_ratio
                    );
                }

                let _ = capture.stop();
                println!("✓ Capture-to-encode pipeline test passed");
            }
        } else {
            println!("Could not create Opus encoder");
        }
    } else {
        println!("No audio device available for pipeline test");
    }
}