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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
//! Comprehensive Encoder Pipeline Tests for CrabCamera
//!
//! This test suite provides comprehensive coverage of the encoding pipeline
//! including H.264 video encoding, Opus audio encoding, format validation,
//! performance characteristics, and error recovery.
//!
//! Run with: cargo test --test encoder_test --features "recording,audio"

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

use std::time::{Duration, Instant};
use tempfile::tempdir;

use crabcamera::recording::{H264Encoder, Recorder, RecordingConfig};
use crabcamera::types::CameraFrame;

#[cfg(feature = "audio")]
use crabcamera::audio::{AudioFrame, EncodedAudio, OpusEncoder};

// ═══════════════════════════════════════════════════════════════════════════
// H.264 VIDEO ENCODER TESTS
// ═══════════════════════════════════════════════════════════════════════════

/// Test H.264 encoder creation with various configurations
#[test]
fn test_h264_encoder_creation_comprehensive() {
    // Test valid configurations
    let valid_configs = vec![
        (320, 240, 15.0, 500_000),      // Low quality
        (640, 480, 30.0, 1_000_000),    // Standard quality
        (1280, 720, 30.0, 2_000_000),   // HD quality
        (1920, 1080, 30.0, 5_000_000),  // Full HD quality
        (3840, 2160, 30.0, 10_000_000), // 4K quality (if supported)
    ];

    for (width, height, fps, bitrate) in valid_configs {
        println!(
            "Testing encoder config: {}x{} @ {}fps, {}bps",
            width, height, fps, bitrate
        );

        match H264Encoder::new(width, height, fps, bitrate) {
            Ok(encoder) => {
                println!("✓ Successfully created {}x{} encoder", width, height);
                assert_eq!(
                    encoder.frame_count(),
                    0,
                    "New encoder should have zero frame count"
                );
                assert!(
                    !encoder.last_was_keyframe(),
                    "New encoder should not have last keyframe flag"
                );
            }
            Err(e) => {
                println!("Failed to create encoder for {}x{}: {}", width, height, e);
                // High resolutions might not be supported on all systems
                if width <= 1920 && height <= 1080 {
                    panic!(
                        "Standard resolution should be supported: {}x{}",
                        width, height
                    );
                }
            }
        }
    }
}

/// Test H.264 encoder frame encoding with different content types
#[test]
fn test_h264_frame_encoding_comprehensive() {
    let mut encoder =
        H264Encoder::new(640, 480, 30.0, 1_000_000).expect("Encoder creation should succeed");

    let width = 640u32;
    let height = 480u32;
    let frame_size = (width * height * 3) as usize;

    // Test different frame types
    let test_frames = vec![
        ("solid_black", vec![0u8; frame_size]),
        ("solid_white", vec![255u8; frame_size]),
        ("solid_gray", vec![128u8; frame_size]),
        ("gradient", create_gradient_frame(width, height)),
        ("checkerboard", create_checkerboard_frame(width, height)),
        // Note: noise frames can sometimes cause encoder issues, skip in automated tests
        // ("noise", create_noise_frame(width, height, 12345)),
    ];

    for (frame_type, rgb_data) in test_frames {
        println!("Testing {} frame encoding", frame_type);

        let result = encoder.encode_rgb(&rgb_data);
        match result {
            Ok(encoded) => {
                println!(
                    "{} frame: {} bytes, keyframe: {}",
                    frame_type,
                    encoded.data.len(),
                    encoded.is_keyframe
                );

                // Validate encoded frame
                assert!(!encoded.data.is_empty(), "Encoded data should not be empty");

                // Check H.264 Annex B format
                assert!(
                    encoded.data.starts_with(&[0, 0, 0, 1]) || encoded.data.starts_with(&[0, 0, 1]),
                    "Encoded data should start with Annex B prefix"
                );

                // First frame should be keyframe
                if encoder.frame_count() == 1 {
                    assert!(encoded.is_keyframe, "First frame should be keyframe");
                }

                // Check frame count progression
                let expected_count = encoder.frame_count();
                assert!(expected_count > 0, "Frame count should increment");

                // Validate NAL unit structure
                validate_h264_nal_units(&encoded.data, encoded.is_keyframe);
            }
            Err(e) => {
                panic!("Encoding {} frame failed: {}", frame_type, e);
            }
        }
    }

    println!("Total frames encoded: {}", encoder.frame_count());
    assert_eq!(encoder.frame_count(), 5, "Should have encoded 5 frames");
}

/// Test H.264 keyframe forcing and GOP structure
#[test]
fn test_h264_keyframe_control() {
    let mut encoder =
        H264Encoder::new(320, 240, 30.0, 500_000).expect("Encoder creation should succeed");

    let rgb_frame = vec![128u8; 320 * 240 * 3];
    let mut keyframe_positions = Vec::new();

    // Encode 30 frames, forcing keyframes at specific intervals
    for i in 0..30 {
        // Force keyframe every 10 frames after the first
        if i > 0 && i % 10 == 0 {
            encoder.force_keyframe();
            println!("Forced keyframe at frame {}", i);
        }

        let encoded = encoder
            .encode_rgb(&rgb_frame)
            .expect("Encoding should succeed");

        if encoded.is_keyframe {
            keyframe_positions.push(i);
            println!("Keyframe detected at position {}", i);
        }
    }

    println!("Keyframes at positions: {:?}", keyframe_positions);

    // Should have keyframes at expected positions
    assert!(
        !keyframe_positions.is_empty(),
        "Should have at least one keyframe"
    );
    assert_eq!(keyframe_positions[0], 0, "First frame should be keyframe");

    // Should have forced keyframes
    assert!(
        keyframe_positions.len() >= 3,
        "Should have forced keyframes: {:?}",
        keyframe_positions
    );

    // Verify forced keyframes are at expected intervals
    for &pos in &keyframe_positions[1..] {
        assert!(
            pos % 10 == 0 || pos == 0,
            "Forced keyframe should be at 10-frame intervals: {}",
            pos
        );
    }
}

/// Test H.264 encoder error handling
#[test]
fn test_h264_encoder_error_handling() {
    let mut encoder =
        H264Encoder::new(640, 480, 30.0, 1_000_000).expect("Encoder creation should succeed");

    // Test invalid frame sizes
    let invalid_frames = vec![
        (vec![0u8; 100], "too small"),
        (vec![0u8; 640 * 480 * 3 + 1], "too large"),
        (vec![0u8; 640 * 480 * 2], "wrong format (2 bytes per pixel)"),
        (vec![0u8; 320 * 240 * 3], "wrong dimensions"),
    ];

    for (invalid_frame, description) in invalid_frames {
        println!("Testing error handling for {}", description);

        let result = encoder.encode_rgb(&invalid_frame);
        match result {
            Ok(_) => {
                panic!("Expected error for {}, but encoding succeeded", description);
            }
            Err(e) => {
                println!("✓ Expected error for {}: {}", description, 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");
            }
        }
    }

    // Encoder should still work after errors
    let valid_frame = vec![128u8; 640 * 480 * 3];
    let result = encoder.encode_rgb(&valid_frame);
    assert!(
        result.is_ok(),
        "Encoder should still work after error conditions"
    );
}

/// Test H.264 encoding performance
#[test]
fn test_h264_encoding_performance() {
    let mut encoder =
        H264Encoder::new(1280, 720, 30.0, 2_000_000).expect("HD encoder creation should succeed");

    let frame_size = 1280 * 720 * 3;
    let test_frame = create_noise_frame(1280, 720, 54321);

    println!(
        "Testing H.264 encoding performance with {}x720 frames",
        1280
    );

    let start_time = Instant::now();
    let mut total_encoded_bytes = 0;
    let frame_count = 100;

    for i in 0..frame_count {
        // Vary the frame content slightly to simulate real video
        let mut frame = test_frame.clone();
        for pixel in frame.iter_mut().step_by(100) {
            *pixel = ((i * 17) % 256) as u8;
        }

        let encoded = encoder.encode_rgb(&frame).expect("Encoding should succeed");
        total_encoded_bytes += encoded.data.len();

        if i % 20 == 0 {
            println!(
                "  Frame {}: {} bytes, keyframe: {}",
                i,
                encoded.data.len(),
                encoded.is_keyframe
            );
        }
    }

    let encoding_time = start_time.elapsed();
    let fps = frame_count as f64 / encoding_time.as_secs_f64();
    let megabytes_per_sec =
        (frame_count * frame_size) as f64 / encoding_time.as_secs_f64() / 1_000_000.0;
    let compression_ratio = (frame_count * frame_size) as f64 / total_encoded_bytes as f64;

    println!("Performance results:");
    println!(
        "  Encoded {} frames in {:.2}s",
        frame_count,
        encoding_time.as_secs_f64()
    );
    println!("  Encoding FPS: {:.1}", fps);
    println!("  Input data rate: {:.1} MB/s", megabytes_per_sec);
    println!("  Total encoded bytes: {}", total_encoded_bytes);
    println!("  Compression ratio: {:.1}x", compression_ratio);

    // Performance assertions
    assert!(fps > 8.0, "Should encode at least 8 FPS, got {:.1}", fps);
    assert!(
        compression_ratio > 5.0,
        "Should achieve reasonable compression: {:.1}x",
        compression_ratio
    );
    assert!(total_encoded_bytes > 0, "Should produce encoded output");
}

// ═══════════════════════════════════════════════════════════════════════════
// OPUS AUDIO ENCODER TESTS
// ═══════════════════════════════════════════════════════════════════════════

#[cfg(feature = "audio")]
mod opus_tests {
    use super::*;

    /// Test Opus encoder creation with various configurations
    #[test]
    fn test_opus_encoder_creation_comprehensive() {
        // Valid configurations
        let valid_configs = vec![
            (48000, 1, 64_000),  // Mono low bitrate
            (48000, 1, 128_000), // Mono standard bitrate
            (48000, 2, 128_000), // Stereo standard bitrate
            (48000, 2, 256_000), // Stereo high bitrate
        ];

        for (sample_rate, channels, bitrate) in valid_configs {
            println!(
                "Testing Opus config: {}Hz, {}ch, {}bps",
                sample_rate, channels, bitrate
            );

            let result = OpusEncoder::new(sample_rate, channels, bitrate);
            match result {
                Ok(encoder) => {
                    println!("✓ Successfully created Opus encoder");
                    assert_eq!(encoder.sample_rate(), sample_rate);
                    assert_eq!(encoder.channels(), channels);
                }
                Err(e) => {
                    panic!("Failed to create Opus encoder: {}", e);
                }
            }
        }

        // Invalid configurations
        let invalid_configs = vec![
            (44100, 2, 128_000), // Wrong sample rate
            (48000, 3, 128_000), // Too many channels
            (48000, 0, 128_000), // Zero channels
        ];

        for (sample_rate, channels, bitrate) in invalid_configs {
            println!(
                "Testing invalid Opus config: {}Hz, {}ch, {}bps",
                sample_rate, channels, bitrate
            );

            let result = OpusEncoder::new(sample_rate, channels, bitrate);
            match result {
                Ok(_) => {
                    panic!(
                        "Expected error for invalid config: {}Hz, {}ch",
                        sample_rate, channels
                    );
                }
                Err(e) => {
                    println!("✓ Expected error: {}", e);
                }
            }
        }
    }

    /// Test Opus encoding with different audio patterns
    #[test]
    fn test_opus_encoding_comprehensive() {
        let mut encoder =
            OpusEncoder::new(48000, 2, 128_000).expect("Opus encoder creation should succeed");

        // Create different types of audio frames
        let frame_size = 960 * 2; // 20ms stereo @ 48kHz
        let test_frames = vec![
            ("silence", vec![0.0f32; frame_size]),
            ("sine_wave", create_sine_wave_frame(frame_size, 440.0)),
            ("white_noise", create_white_noise_frame(frame_size, 111)),
            ("stereo_test", create_stereo_test_frame(frame_size)),
        ];

        for (frame_type, samples) in test_frames {
            println!("Testing {} encoding", frame_type);

            let audio_frame = AudioFrame {
                samples,
                sample_rate: 48000,
                channels: 2,
                timestamp: 0.0,
            };

            let result = encoder.encode(&audio_frame);
            match result {
                Ok(packets) => {
                    println!("{} encoded: {} packets", frame_type, packets.len());

                    if !packets.is_empty() {
                        for (i, packet) in packets.iter().enumerate() {
                            println!(
                                "  Packet {}: {} bytes, {:.3}s timestamp, {:.3}s duration",
                                i,
                                packet.data.len(),
                                packet.timestamp,
                                packet.duration
                            );

                            validate_opus_packet(packet);
                        }
                    }
                }
                Err(e) => {
                    panic!("Encoding {} failed: {}", frame_type, e);
                }
            }
        }
    }

    /// Test Opus encoder buffering and frame accumulation
    #[test]
    fn test_opus_buffering_behavior() {
        let mut encoder =
            OpusEncoder::new(48000, 2, 128_000).expect("Opus encoder creation should succeed");

        // Test partial frames (smaller than 960 samples)
        let partial_sizes = vec![100, 200, 500, 659]; // Sum = 1459, so we should get 1 packet
        let mut total_samples_sent = 0;

        for (i, size) in partial_sizes.iter().enumerate() {
            println!("Sending partial frame {}: {} samples", i, size);

            let audio_frame = AudioFrame {
                samples: vec![0.1f32; size * 2], // stereo
                sample_rate: 48000,
                channels: 2,
                timestamp: total_samples_sent as f64 / 48000.0,
            };

            total_samples_sent += size;

            let packets = encoder
                .encode(&audio_frame)
                .expect("Encoding should succeed");

            if packets.is_empty() {
                println!("  No output (buffering)");
            } else {
                println!("  Produced {} packets", packets.len());
                for packet in &packets {
                    validate_opus_packet(packet);
                }
            }
        }

        // Flush remaining data
        let flush_packets = encoder.flush().expect("Flush should succeed");
        println!("Flush produced {} packets", flush_packets.len());

        for packet in &flush_packets {
            validate_opus_packet(packet);
        }
    }

    /// Test Opus encoder performance with continuous encoding
    #[test]
    fn test_opus_encoding_performance() {
        let mut encoder =
            OpusEncoder::new(48000, 2, 128_000).expect("Opus encoder creation should succeed");

        let frame_size = 960 * 2; // 20ms stereo
        let frame_count = 250; // 5 seconds worth
        let test_samples = create_sine_wave_frame(frame_size, 1000.0);

        println!(
            "Testing Opus encoding performance with {} frames",
            frame_count
        );

        let start_time = Instant::now();
        let mut total_packets = 0;
        let mut total_bytes = 0;

        for i in 0..frame_count {
            let audio_frame = AudioFrame {
                samples: test_samples.clone(),
                sample_rate: 48000,
                channels: 2,
                timestamp: i as f64 * 0.020, // 20ms per frame
            };

            let packets = encoder
                .encode(&audio_frame)
                .expect("Encoding should succeed");
            total_packets += packets.len();

            for packet in packets {
                total_bytes += packet.data.len();

                if i % 50 == 0 && !packet.data.is_empty() {
                    validate_opus_packet(&packet);
                }
            }
        }

        let encoding_time = start_time.elapsed();
        let real_time_ratio = 5.0 / encoding_time.as_secs_f64(); // 5 seconds of audio
        let bitrate_actual = (total_bytes * 8) as f64 / 5.0; // bits per second

        println!("Opus performance results:");
        println!(
            "  Encoded {} frames in {:.3}s",
            frame_count,
            encoding_time.as_secs_f64()
        );
        println!("  Real-time ratio: {:.1}x", real_time_ratio);
        println!("  Total packets: {}", total_packets);
        println!("  Total bytes: {}", total_bytes);
        println!(
            "  Actual bitrate: {:.0} bps (target: 128000)",
            bitrate_actual
        );

        // Performance assertions
        assert!(
            real_time_ratio > 10.0,
            "Should encode much faster than real-time: {:.1}x",
            real_time_ratio
        );
        assert!(
            total_packets > 200,
            "Should produce reasonable number of packets: {}",
            total_packets
        );
        assert!(
            (bitrate_actual - 128_000.0).abs() < 50_000.0,
            "Bitrate should be close to target: {:.0} vs 128000",
            bitrate_actual
        );
    }

    /// Validate Opus packet structure
    fn validate_opus_packet(packet: &EncodedAudio) {
        assert!(!packet.data.is_empty(), "Packet data should not be empty");
        assert!(packet.timestamp >= 0.0, "Timestamp should be non-negative");
        assert!(packet.duration > 0.0, "Duration should be positive");
        assert!(
            packet.duration <= 0.120,
            "Duration should be reasonable (≤120ms)"
        );

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

        // Check frame count (c field in TOC)
        let c = toc & 0x03;
        assert!(c <= 3, "Opus frame count should be valid: {}", c);
    }

    /// Create sine wave audio for testing
    fn create_sine_wave_frame(samples: usize, frequency: f64) -> Vec<f32> {
        let mut frame = Vec::with_capacity(samples);
        for i in 0..samples {
            let t = i as f64 / 48000.0;
            let sample = (2.0 * std::f64::consts::PI * frequency * t).sin() as f32 * 0.5;
            frame.push(sample);
        }
        frame
    }

    /// Create white noise audio for testing
    fn create_white_noise_frame(samples: usize, seed: u64) -> Vec<f32> {
        let mut rng = seed;
        let mut frame = Vec::with_capacity(samples);
        for _ in 0..samples {
            rng = rng.wrapping_mul(1103515245).wrapping_add(12345);
            let sample = ((rng >> 16) as f32 / 32768.0 - 1.0) * 0.3;
            frame.push(sample);
        }
        frame
    }

    /// Create stereo test signal (left/right channel identification)
    fn create_stereo_test_frame(samples: usize) -> Vec<f32> {
        let mut frame = Vec::with_capacity(samples);
        for i in 0..(samples / 2) {
            let t = i as f64 / 48000.0;
            // Left channel: 440 Hz
            let left = (2.0 * std::f64::consts::PI * 440.0 * t).sin() as f32 * 0.3;
            // Right channel: 880 Hz
            let right = (2.0 * std::f64::consts::PI * 880.0 * t).sin() as f32 * 0.3;
            frame.push(left);
            frame.push(right);
        }
        frame
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// INTEGRATED ENCODING PIPELINE TESTS
// ═══════════════════════════════════════════════════════════════════════════

/// Test complete A/V encoding pipeline integration
#[test]
#[cfg(feature = "audio")]
fn test_integrated_av_encoding_pipeline() {
    use crabcamera::recording::AudioConfig;

    let dir = tempdir().expect("Create temp dir");
    let output = dir.path().join("integrated_test.mp4");

    // Create recorder with A/V configuration
    let config = RecordingConfig::new(640, 480, 30.0).with_audio(AudioConfig {
        device_id: None,
        sample_rate: 48000,
        channels: 2,
        bitrate: 128_000,
    });

    let mut recorder = Recorder::new(&output, config).expect("Recorder creation should succeed");

    println!("Testing integrated A/V encoding pipeline");

    let frame_count = 90; // 3 seconds at 30 fps
    let mut video_frames_written = 0;

    for i in 0..frame_count {
        // Create test video frame
        let frame = create_test_camera_frame(640, 480, i);

        match recorder.write_frame(&frame) {
            Ok(_) => {
                video_frames_written += 1;
                if i % 30 == 0 {
                    println!("  Written video frame {}", i);
                }
            }
            Err(e) => {
                println!("Video frame {} write error: {}", i, e);
            }
        }

        // Small delay to simulate real recording timing
        std::thread::sleep(Duration::from_millis(33));
    }

    let stats = recorder.finish().expect("Recording finish should succeed");

    println!("Integrated encoding results:");
    println!("  Video frames attempted: {}", frame_count);
    println!("  Video frames written: {}", video_frames_written);
    println!("  Final video frames: {}", stats.video_frames);
    println!("  Audio frames: {}", stats.audio_frames);
    println!("  Duration: {:.2}s", stats.duration_secs);
    println!("  File size: {} bytes", stats.bytes_written);

    // Verify results
    assert!(stats.video_frames > 0, "Should have video frames");
    assert!(stats.bytes_written > 0, "Should have written data");
    assert!(stats.duration_secs > 0.0, "Should have duration");

    // File should exist and have content
    let file_metadata = std::fs::metadata(&output).expect("Output file should exist");
    assert!(file_metadata.len() > 0, "Output file should have content");
    assert!(
        file_metadata.len() as u64 <= stats.bytes_written + 1000,
        "File size should match stats"
    );

    println!("✓ Integrated A/V encoding pipeline test passed");
}

/// Test encoder behavior under stress conditions
#[test]
fn test_encoder_stress_conditions() {
    let mut encoder =
        H264Encoder::new(320, 240, 30.0, 500_000).expect("Encoder creation should succeed");

    println!("Testing encoder under stress conditions");

    // Rapid encoding with varying content
    let start_time = Instant::now();
    let mut successful_encodes = 0;
    let mut total_bytes = 0;

    for i in 0..1000 {
        let frame = create_rapidly_changing_frame(320, 240, i);

        match encoder.encode_rgb(&frame) {
            Ok(encoded) => {
                successful_encodes += 1;
                total_bytes += encoded.data.len();

                if i % 100 == 0 {
                    println!(
                        "  Frame {}: {} bytes, keyframe: {}",
                        i,
                        encoded.data.len(),
                        encoded.is_keyframe
                    );
                }
            }
            Err(e) => {
                println!("Encoding error at frame {}: {}", i, e);
            }
        }
    }

    let encoding_duration = start_time.elapsed();
    let fps = successful_encodes as f64 / encoding_duration.as_secs_f64();

    println!("Stress test results:");
    println!("  Successful encodes: {}/1000", successful_encodes);
    println!("  Encoding FPS: {:.1}", fps);
    println!("  Total encoded bytes: {}", total_bytes);
    println!("  Time taken: {:.2}s", encoding_duration.as_secs_f64());

    // Should handle stress reasonably
    assert!(
        successful_encodes >= 950,
        "Should handle most frames under stress: {}",
        successful_encodes
    );
    assert!(
        fps > 50.0,
        "Should maintain reasonable FPS under stress: {:.1}",
        fps
    );
}

/// Test encoder memory usage patterns
#[test]
fn test_encoder_memory_patterns() {
    println!("Testing encoder memory usage patterns");

    // Create multiple encoders to test memory isolation
    let configs = vec![(320, 240), (640, 480), (1280, 720)];

    let mut encoders = Vec::new();
    for (width, height) in configs {
        match H264Encoder::new(width, height, 30.0, 1_000_000) {
            Ok(encoder) => {
                println!("Created {}x{} encoder", width, height);
                encoders.push((encoder, width, height));
            }
            Err(e) => {
                println!("Failed to create {}x{} encoder: {}", width, height, e);
            }
        }
    }

    // Encode frames with each encoder
    for (encoder, width, height) in &mut encoders {
        let frame = vec![128u8; (*width * *height * 3) as usize];

        for i in 0..10 {
            match encoder.encode_rgb(&frame) {
                Ok(encoded) => {
                    assert!(!encoded.data.is_empty(), "Frame should produce output");

                    if i == 0 {
                        println!(
                            "  {}x{} first frame: {} bytes",
                            width,
                            height,
                            encoded.data.len()
                        );
                    }
                }
                Err(e) => {
                    panic!("Encoding failed for {}x{}: {}", width, height, e);
                }
            }
        }
    }

    println!(
        "✓ Memory patterns test completed with {} encoders",
        encoders.len()
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// HELPER FUNCTIONS FOR TEST DATA GENERATION
// ═══════════════════════════════════════════════════════════════════════════

/// Create a gradient test frame
fn create_gradient_frame(width: u32, height: u32) -> Vec<u8> {
    let mut frame = Vec::with_capacity((width * height * 3) as usize);

    for y in 0..height {
        for x in 0..width {
            let r = ((x as f32 / width as f32) * 255.0) as u8;
            let g = ((y as f32 / height as f32) * 255.0) as u8;
            let b = 128u8;

            frame.push(r);
            frame.push(g);
            frame.push(b);
        }
    }

    frame
}

/// Create a checkerboard test frame
fn create_checkerboard_frame(width: u32, height: u32) -> Vec<u8> {
    let mut frame = Vec::with_capacity((width * height * 3) as usize);
    let square_size = 32;

    for y in 0..height {
        for x in 0..width {
            let checker_x = (x / square_size) % 2;
            let checker_y = (y / square_size) % 2;
            let is_white = (checker_x + checker_y) % 2 == 0;

            let color = if is_white { 255 } else { 0 };
            frame.push(color);
            frame.push(color);
            frame.push(color);
        }
    }

    frame
}

/// Create a noise test frame
fn create_noise_frame(width: u32, height: u32, seed: u64) -> Vec<u8> {
    let mut frame = Vec::with_capacity((width * height * 3) as usize);
    let mut rng = seed;

    for _ in 0..(width * height) {
        rng = rng.wrapping_mul(1103515245).wrapping_add(12345);
        let r = ((rng >> 16) & 0xFF) as u8;
        rng = rng.wrapping_mul(1103515245).wrapping_add(12345);
        let g = ((rng >> 16) & 0xFF) as u8;
        rng = rng.wrapping_mul(1103515245).wrapping_add(12345);
        let b = ((rng >> 16) & 0xFF) as u8;

        frame.push(r);
        frame.push(g);
        frame.push(b);
    }

    frame
}

/// Create rapidly changing frame for stress testing
fn create_rapidly_changing_frame(width: u32, height: u32, frame_index: usize) -> Vec<u8> {
    let mut frame = Vec::with_capacity((width * height * 3) as usize);
    let pattern = frame_index % 16;

    for _y in 0..height {
        for _x in 0..width {
            let base_color = match pattern {
                0..=3 => (255, 0, 0),  // Red phases
                4..=7 => (0, 255, 0),  // Green phases
                8..=11 => (0, 0, 255), // Blue phases
                _ => (255, 255, 255),  // White
            };

            let intensity = ((frame_index * 17) % 256) as u8;
            let r = ((base_color.0 as u16 * intensity as u16) / 255) as u8;
            let g = ((base_color.1 as u16 * intensity as u16) / 255) as u8;
            let b = ((base_color.2 as u16 * intensity as u16) / 255) as u8;

            frame.push(r);
            frame.push(g);
            frame.push(b);
        }
    }

    frame
}

/// Create test camera frame
fn create_test_camera_frame(width: u32, height: u32, frame_index: usize) -> CameraFrame {
    let gray = ((frame_index * 7) % 256) as u8;
    let data = vec![gray; (width * height * 3) as usize];

    CameraFrame::new(data, width, height, "test_camera".to_string())
}

/// Validate H.264 NAL unit structure
fn validate_h264_nal_units(data: &[u8], is_keyframe: bool) {
    assert!(!data.is_empty(), "NAL data should not be empty");

    // Should start with Annex B start code
    assert!(
        data.starts_with(&[0, 0, 0, 1]) || data.starts_with(&[0, 0, 1]),
        "Should start with Annex B start code"
    );

    // Find NAL units
    let mut nal_units = Vec::new();
    let mut i = 0;

    while i < data.len() {
        // Look for start code
        if i + 3 < data.len() {
            if data[i..i + 4] == [0, 0, 0, 1] {
                nal_units.push(i + 4);
                i += 4;
            } else if data[i..i + 3] == [0, 0, 1] {
                nal_units.push(i + 3);
                i += 3;
            } else {
                i += 1;
            }
        } else {
            break;
        }
    }

    assert!(!nal_units.is_empty(), "Should find at least one NAL unit");

    // Check first NAL unit type
    if !nal_units.is_empty() && nal_units[0] < data.len() {
        let nal_type = data[nal_units[0]] & 0x1F;

        if is_keyframe {
            // Keyframes should contain SPS/PPS or IDR
            assert!(
                nal_type == 5 || nal_type == 7 || nal_type == 8, // IDR, SPS, or PPS
                "Keyframe should contain IDR/SPS/PPS NAL unit, got type {}",
                nal_type
            );
        }

        println!("  Found NAL unit type: {}", nal_type);
    }
}