spice-client 0.2.0

A pure Rust SPICE client library with native and WebAssembly support
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
#![allow(dead_code)]

use super::*;
use crate::error::SpiceError;

#[derive(Debug, Clone)]
pub struct VideoFrame {
    pub width: u32,
    pub height: u32,
    pub format: VideoFormat,
    pub data: Vec<u8>,
    pub timestamp: u64,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VideoFormat {
    Rgb32,
    Rgba32,
    Bgr32,
    Bgra32,
    Yuv420,
}

impl VideoFrame {
    pub fn from_display_surface(surface: &DisplaySurface, timestamp: u64) -> Result<Self> {
        let format = match surface.format {
            0x0 => VideoFormat::Rgb32,
            0x1 => VideoFormat::Rgba32,
            0x2 => VideoFormat::Bgr32,
            0x3 => VideoFormat::Bgra32,
            _ => {
                return Err(SpiceError::Protocol(format!(
                    "Unsupported format: {}",
                    surface.format
                )))
            }
        };

        Ok(VideoFrame {
            width: surface.width,
            height: surface.height,
            format,
            data: surface.data.clone(),
            timestamp,
        })
    }

    pub fn to_base64_data_url(&self) -> Result<String> {
        let image_data = match self.format {
            VideoFormat::Rgba32 | VideoFormat::Bgra32 => self.data.clone(),
            VideoFormat::Rgb32 => self.convert_rgb_to_rgba(),
            VideoFormat::Bgr32 => self.convert_bgr_to_rgba(),
            VideoFormat::Yuv420 => {
                return Err(SpiceError::Protocol(
                    "YUV420 conversion not implemented".to_string(),
                ))
            }
        };

        let png_data = self.encode_as_png(&image_data)?;
        use base64::Engine;
        let base64 = base64::engine::general_purpose::STANDARD.encode(&png_data);
        Ok(format!("data:image/png;base64,{base64}"))
    }

    fn convert_rgb_to_rgba(&self) -> Vec<u8> {
        let mut rgba_data = Vec::with_capacity((self.width * self.height * 4) as usize);
        for chunk in self.data.chunks(3) {
            rgba_data.extend_from_slice(chunk);
            rgba_data.push(255); // Alpha channel
        }
        rgba_data
    }

    fn convert_bgr_to_rgba(&self) -> Vec<u8> {
        let mut rgba_data = Vec::with_capacity((self.width * self.height * 4) as usize);
        for chunk in self.data.chunks(3) {
            rgba_data.push(chunk[2]); // R
            rgba_data.push(chunk[1]); // G
            rgba_data.push(chunk[0]); // B
            rgba_data.push(255); // A
        }
        rgba_data
    }

    fn encode_as_png(&self, rgba_data: &[u8]) -> Result<Vec<u8>> {
        use std::io::Cursor;

        let mut png_data = Vec::new();
        let mut encoder = png::Encoder::new(Cursor::new(&mut png_data), self.width, self.height);
        encoder.set_color(png::ColorType::Rgba);
        encoder.set_depth(png::BitDepth::Eight);

        let mut writer = encoder
            .write_header()
            .map_err(|e| SpiceError::Protocol(format!("PNG header error: {e}")))?;
        writer
            .write_image_data(rgba_data)
            .map_err(|e| SpiceError::Protocol(format!("PNG write error: {e}")))?;
        writer
            .finish()
            .map_err(|e| SpiceError::Protocol(format!("PNG finish error: {e}")))?;

        Ok(png_data)
    }
}

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

    #[test]
    fn test_video_frame_from_display_surface() {
        let surface = DisplaySurface {
            width: 100,
            height: 50,
            format: 0x1,                   // RGBA32
            data: vec![255; 100 * 50 * 4], // White image
        };

        let frame = VideoFrame::from_display_surface(&surface, 1000).unwrap();
        assert_eq!(frame.width, 100);
        assert_eq!(frame.height, 50);
        assert_eq!(frame.format, VideoFormat::Rgba32);
        assert_eq!(frame.timestamp, 1000);
        assert_eq!(frame.data.len(), surface.data.len());
    }

    #[test]
    fn test_video_frame_unsupported_format() {
        let surface = DisplaySurface {
            width: 100,
            height: 50,
            format: 0xFF, // Invalid format
            data: vec![0; 100 * 50 * 4],
        };

        let result = VideoFrame::from_display_surface(&surface, 1000);
        assert!(result.is_err());
    }

    #[test]
    fn test_base64_data_url_generation() {
        let surface = DisplaySurface {
            width: 2,
            height: 2,
            format: 0x1, // RGBA32
            data: vec![
                255, 0, 0, 255, // Red
                0, 255, 0, 255, // Green
                0, 0, 255, 255, // Blue
                255, 255, 255, 255, // White
            ],
        };

        let frame = VideoFrame::from_display_surface(&surface, 1000).unwrap();
        let data_url = frame.to_base64_data_url().unwrap();

        assert!(data_url.starts_with("data:image/png;base64,"));
        assert!(data_url.len() > 22); // Has actual base64 data
    }

    #[test]
    fn test_rgb_to_rgba_conversion() {
        let surface = DisplaySurface {
            width: 2,
            height: 1,
            format: 0x0,                      // RGB32
            data: vec![255, 0, 0, 0, 255, 0], // Red, Green
        };

        let frame = VideoFrame::from_display_surface(&surface, 1000).unwrap();
        let rgba_data = frame.convert_rgb_to_rgba();

        assert_eq!(
            rgba_data,
            vec![
                255, 0, 0, 255, // Red with alpha
                0, 255, 0, 255 // Green with alpha
            ]
        );
    }

    #[test]
    fn test_bgr_to_rgba_conversion() {
        let surface = DisplaySurface {
            width: 2,
            height: 1,
            format: 0x2,                      // BGR32
            data: vec![0, 0, 255, 0, 255, 0], // Red (BGR), Green (BGR)
        };

        let frame = VideoFrame::from_display_surface(&surface, 1000).unwrap();
        let rgba_data = frame.convert_bgr_to_rgba();

        assert_eq!(
            rgba_data,
            vec![
                255, 0, 0, 255, // Red with alpha
                0, 255, 0, 255 // Green with alpha
            ]
        );
    }

    #[test]
    fn test_video_frame_memory_size() {
        let sizes = vec![(640, 480), (1920, 1080), (3840, 2160)];

        for (width, height) in sizes {
            let surface = DisplaySurface {
                width,
                height,
                format: 0x1, // RGBA32
                data: vec![0; (width * height * 4) as usize],
            };

            let frame = VideoFrame::from_display_surface(&surface, 1000).unwrap();
            assert_eq!(frame.data.len(), (width * height * 4) as usize);
        }
    }
}

#[derive(Debug)]
pub struct VideoFrameBuffer {
    frames: Vec<VideoFrame>,
    max_frames: usize,
    target_fps: u32,
    last_frame_time: Option<u64>,
}

impl VideoFrameBuffer {
    pub fn new(max_frames: usize, target_fps: u32) -> Self {
        Self {
            frames: Vec::with_capacity(max_frames),
            max_frames,
            target_fps,
            last_frame_time: None,
        }
    }

    pub fn should_accept_frame(&self, timestamp: u64) -> bool {
        match self.last_frame_time {
            None => true,
            Some(last) => {
                if timestamp < last {
                    return false; // Timestamp went backwards
                }
                let min_interval = 1_000_000 / self.target_fps as u64; // microseconds
                timestamp - last >= min_interval
            }
        }
    }

    pub fn add_frame(&mut self, frame: VideoFrame) -> bool {
        if !self.should_accept_frame(frame.timestamp) {
            return false;
        }

        if self.frames.len() >= self.max_frames {
            self.frames.remove(0); // Drop oldest frame
        }

        self.last_frame_time = Some(frame.timestamp);
        self.frames.push(frame);
        true
    }

    pub fn get_latest_frame(&self) -> Option<&VideoFrame> {
        self.frames.last()
    }

    pub fn get_frame_count(&self) -> usize {
        self.frames.len()
    }

    pub fn clear(&mut self) {
        self.frames.clear();
        self.last_frame_time = None;
    }

    pub fn get_memory_usage(&self) -> usize {
        self.frames
            .iter()
            .map(|f| f.data.len() + std::mem::size_of::<VideoFrame>())
            .sum()
    }
}

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

    #[test]
    fn test_frame_rate_limiting() {
        let mut buffer = VideoFrameBuffer::new(10, 30); // 30 FPS target

        let surface = DisplaySurface {
            width: 100,
            height: 100,
            format: 0x1,
            data: vec![0; 100 * 100 * 4],
        };

        // First frame should be accepted
        let frame1 = VideoFrame::from_display_surface(&surface, 0).unwrap();
        assert!(buffer.add_frame(frame1));

        // Frame too soon should be rejected (10 microseconds later)
        let frame2 = VideoFrame::from_display_surface(&surface, 10).unwrap();
        assert!(!buffer.add_frame(frame2));

        // Frame after sufficient time should be accepted (33334 microseconds = ~30 FPS)
        let frame3 = VideoFrame::from_display_surface(&surface, 33334).unwrap();
        assert!(buffer.add_frame(frame3));

        assert_eq!(buffer.get_frame_count(), 2);
    }

    #[test]
    fn test_buffer_overflow_handling() {
        let mut buffer = VideoFrameBuffer::new(3, 60);

        let surface = DisplaySurface {
            width: 10,
            height: 10,
            format: 0x1,
            data: vec![0; 10 * 10 * 4],
        };

        // Add 4 frames, should only keep last 3
        for i in 0..4 {
            let frame = VideoFrame::from_display_surface(&surface, i * 20_000).unwrap();
            buffer.add_frame(frame);
        }

        assert_eq!(buffer.get_frame_count(), 3);
    }

    #[test]
    fn test_memory_usage_tracking() {
        let mut buffer = VideoFrameBuffer::new(10, 30);

        let surface = DisplaySurface {
            width: 100,
            height: 100,
            format: 0x1,
            data: vec![0; 100 * 100 * 4],
        };

        let frame = VideoFrame::from_display_surface(&surface, 0).unwrap();
        let frame_size = frame.data.len() + std::mem::size_of::<VideoFrame>();
        buffer.add_frame(frame);

        assert_eq!(buffer.get_memory_usage(), frame_size);

        // Add another frame
        let frame2 = VideoFrame::from_display_surface(&surface, 40_000).unwrap();
        buffer.add_frame(frame2);

        assert_eq!(buffer.get_memory_usage(), frame_size * 2);
    }

    #[test]
    fn test_buffer_clear() {
        let mut buffer = VideoFrameBuffer::new(10, 30);

        let surface = DisplaySurface {
            width: 10,
            height: 10,
            format: 0x1,
            data: vec![0; 10 * 10 * 4],
        };

        let frame = VideoFrame::from_display_surface(&surface, 0).unwrap();
        buffer.add_frame(frame);

        assert_eq!(buffer.get_frame_count(), 1);
        assert!(buffer.get_latest_frame().is_some());

        buffer.clear();

        assert_eq!(buffer.get_frame_count(), 0);
        assert!(buffer.get_latest_frame().is_none());
        assert_eq!(buffer.get_memory_usage(), 0);
    }
}

#[derive(Debug)]
pub struct VideoStreamer {
    buffer: VideoFrameBuffer,
    frames_encoded: u64,
    frames_dropped: u64,
    total_encoding_time: u64,
    quality_level: u8, // 0-100
}

impl VideoStreamer {
    pub fn new(buffer_size: usize, target_fps: u32) -> Self {
        Self {
            buffer: VideoFrameBuffer::new(buffer_size, target_fps),
            frames_encoded: 0,
            frames_dropped: 0,
            total_encoding_time: 0,
            quality_level: 100,
        }
    }

    pub async fn process_frame(
        &mut self,
        surface: &DisplaySurface,
        timestamp: u64,
    ) -> Result<Option<String>> {
        let start_time = instant::Instant::now();

        // Create video frame
        let frame = VideoFrame::from_display_surface(surface, timestamp)?;

        // Check if we should accept the frame based on frame rate
        if !self.buffer.should_accept_frame(timestamp) {
            self.frames_dropped += 1;
            return Ok(None);
        }

        // Simulate encoding with quality adjustment
        let encoded = if self.quality_level < 100 {
            self.encode_with_quality(&frame, self.quality_level)?
        } else {
            frame.to_base64_data_url()?
        };

        // Track encoding time
        let encoding_time = start_time.elapsed().as_micros() as u64;
        self.total_encoding_time += encoding_time;

        // Add frame to buffer
        if self.buffer.add_frame(frame) {
            self.frames_encoded += 1;
            Ok(Some(encoded))
        } else {
            self.frames_dropped += 1;
            Ok(None)
        }
    }

    fn encode_with_quality(&self, frame: &VideoFrame, quality: u8) -> Result<String> {
        // Quality affects both resolution scaling and PNG compression
        // For better performance at lower quality, we can downsample the image
        let scale = if quality < 80 {
            (quality as f32 / 80.0).max(0.5) // Scale down for quality < 80
        } else {
            1.0 // Full resolution for quality >= 80
        };

        let new_width = (frame.width as f32 * scale) as u32;
        let new_height = (frame.height as f32 * scale) as u32;

        // If scaling is needed, resample the image
        let (width, height, image_data) = if scale < 1.0 {
            let resampled = self.downsample_frame(frame, new_width, new_height)?;
            (new_width, new_height, resampled)
        } else {
            // Use original data
            let rgba_data = match frame.format {
                VideoFormat::Rgba32 | VideoFormat::Bgra32 => frame.data.clone(),
                VideoFormat::Rgb32 => frame.convert_rgb_to_rgba(),
                VideoFormat::Bgr32 => frame.convert_bgr_to_rgba(),
                VideoFormat::Yuv420 => {
                    return Err(SpiceError::Protocol(
                        "YUV420 conversion not implemented".to_string(),
                    ))
                }
            };
            (frame.width, frame.height, rgba_data)
        };

        // Encode as PNG with appropriate compression level
        let compression = if quality >= 90 {
            png::Compression::Best
        } else if quality >= 70 {
            png::Compression::Default
        } else {
            png::Compression::Fast
        };

        let png_data =
            self.encode_as_png_with_compression(&image_data, width, height, compression)?;

        use base64::Engine;
        let base64 = base64::engine::general_purpose::STANDARD.encode(&png_data);
        Ok(format!("data:image/png;base64,{base64}"))
    }

    pub fn adjust_quality_for_load(&mut self, target_encode_time_us: u64) {
        let avg_encode_time = if self.frames_encoded > 0 {
            self.total_encoding_time / self.frames_encoded
        } else {
            0
        };

        if avg_encode_time > target_encode_time_us && self.quality_level > 25 {
            self.quality_level = (self.quality_level - 5).max(25);
        } else if avg_encode_time < target_encode_time_us / 2 && self.quality_level < 100 {
            self.quality_level = (self.quality_level + 5).min(100);
        }
    }

    pub fn get_stats(&self) -> StreamingStats {
        StreamingStats {
            frames_encoded: self.frames_encoded,
            frames_dropped: self.frames_dropped,
            drop_rate: if self.frames_encoded + self.frames_dropped > 0 {
                self.frames_dropped as f32 / (self.frames_encoded + self.frames_dropped) as f32
            } else {
                0.0
            },
            avg_encoding_time_us: if self.frames_encoded > 0 {
                self.total_encoding_time / self.frames_encoded
            } else {
                0
            },
            quality_level: self.quality_level,
            buffer_usage: self.buffer.get_frame_count(),
            memory_usage: self.buffer.get_memory_usage(),
        }
    }

    pub fn reset_stats(&mut self) {
        self.frames_encoded = 0;
        self.frames_dropped = 0;
        self.total_encoding_time = 0;
    }

    fn downsample_frame(
        &self,
        frame: &VideoFrame,
        new_width: u32,
        new_height: u32,
    ) -> Result<Vec<u8>> {
        // Simple nearest-neighbor downsampling
        let mut result = Vec::with_capacity((new_width * new_height * 4) as usize);

        let x_ratio = frame.width as f32 / new_width as f32;
        let y_ratio = frame.height as f32 / new_height as f32;

        for y in 0..new_height {
            for x in 0..new_width {
                let src_x = (x as f32 * x_ratio) as u32;
                let src_y = (y as f32 * y_ratio) as u32;

                let src_offset = ((src_y * frame.width + src_x) * 4) as usize;

                if src_offset + 3 < frame.data.len() {
                    result.push(frame.data[src_offset]); // R
                    result.push(frame.data[src_offset + 1]); // G
                    result.push(frame.data[src_offset + 2]); // B
                    result.push(frame.data[src_offset + 3]); // A
                } else {
                    // Fallback for out-of-bounds
                    result.extend_from_slice(&[0, 0, 0, 255]);
                }
            }
        }

        Ok(result)
    }

    fn encode_as_png_with_compression(
        &self,
        rgba_data: &[u8],
        width: u32,
        height: u32,
        compression: png::Compression,
    ) -> Result<Vec<u8>> {
        use std::io::Cursor;

        let mut png_data = Vec::new();
        let mut encoder = png::Encoder::new(Cursor::new(&mut png_data), width, height);
        encoder.set_color(png::ColorType::Rgba);
        encoder.set_depth(png::BitDepth::Eight);
        encoder.set_compression(compression);

        let mut writer = encoder
            .write_header()
            .map_err(|e| SpiceError::Protocol(format!("PNG header error: {e}")))?;
        writer
            .write_image_data(rgba_data)
            .map_err(|e| SpiceError::Protocol(format!("PNG write error: {e}")))?;
        writer
            .finish()
            .map_err(|e| SpiceError::Protocol(format!("PNG finish error: {e}")))?;

        Ok(png_data)
    }
}

#[derive(Debug, Clone)]
pub struct StreamingStats {
    pub frames_encoded: u64,
    pub frames_dropped: u64,
    pub drop_rate: f32,
    pub avg_encoding_time_us: u64,
    pub quality_level: u8,
    pub buffer_usage: usize,
    pub memory_usage: usize,
}

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

    #[tokio::test]
    async fn test_continuous_frame_updates() {
        let mut streamer = VideoStreamer::new(10, 30);

        let surface = DisplaySurface {
            width: 640,
            height: 480,
            format: 0x1,
            data: vec![0; 640 * 480 * 4],
        };

        // Simulate continuous updates at 30 FPS (33.33ms intervals)
        let mut timestamp = 0;
        let mut successful_frames = 0;

        for _ in 0..60 {
            // 2 seconds worth of frames
            let result = streamer.process_frame(&surface, timestamp).await.unwrap();
            if result.is_some() {
                successful_frames += 1;
            }
            timestamp += 33_333; // microseconds
        }

        let stats = streamer.get_stats();
        assert_eq!(stats.frames_encoded, successful_frames);
        assert_eq!(stats.frames_encoded, 60); // Should encode all frames at correct interval
        assert_eq!(stats.frames_dropped, 0);
        assert_eq!(stats.drop_rate, 0.0);
    }

    #[tokio::test]
    async fn test_frame_dropping_under_load() {
        let mut streamer = VideoStreamer::new(5, 60); // 60 FPS target, small buffer

        let surface = DisplaySurface {
            width: 1920,
            height: 1080,
            format: 0x1,
            data: vec![0; 1920 * 1080 * 4],
        };

        // Simulate frames coming in too fast (every 10ms instead of 16.67ms)
        let mut timestamp = 0;
        for _ in 0..100 {
            streamer.process_frame(&surface, timestamp).await.unwrap();
            timestamp += 10_000; // 10ms intervals (100 FPS input)
        }

        let stats = streamer.get_stats();
        assert!(
            stats.frames_dropped > 0,
            "Should drop frames when input is too fast"
        );
        assert!(
            stats.drop_rate > 0.0 && stats.drop_rate < 1.0,
            "Drop rate should be between 0 and 1"
        );
        assert!(
            stats.frames_encoded < 100,
            "Should encode fewer frames than input"
        );
    }

    #[tokio::test]
    async fn test_quality_degradation_under_load() {
        let mut streamer = VideoStreamer::new(10, 30);

        let surface = DisplaySurface {
            width: 3840,
            height: 2160,
            format: 0x1,
            data: vec![0; 3840 * 2160 * 4], // 4K resolution
        };

        // Process frames and simulate high encoding time
        let mut timestamp = 0;
        for i in 0..30 {
            streamer.process_frame(&surface, timestamp).await.unwrap();
            timestamp += 33_333;

            // Simulate adjusting quality based on encoding time
            // Target: 20ms encoding time
            if i % 5 == 0 {
                streamer.total_encoding_time = streamer.frames_encoded * 25_000; // Simulate 25ms avg
                streamer.adjust_quality_for_load(20_000);
            }
        }

        let stats = streamer.get_stats();
        assert!(
            stats.quality_level < 100,
            "Quality should degrade under load"
        );
        assert!(
            stats.quality_level >= 25,
            "Quality should not go below minimum"
        );
    }

    #[tokio::test]
    async fn test_encoding_performance_benchmark() {
        use instant::Instant;

        let mut streamer = VideoStreamer::new(30, 30);

        let sizes = vec![
            (640, 480, "VGA"),
            (1280, 720, "720p"),
            (1920, 1080, "1080p"),
            (3840, 2160, "4K"),
        ];

        let mut results = Vec::new();

        for (width, height, name) in sizes {
            let surface = DisplaySurface {
                width,
                height,
                format: 0x1,
                data: vec![128; (width * height * 4) as usize],
            };

            streamer.reset_stats();

            let start = Instant::now();
            let mut timestamp = 0;

            // Process 30 frames
            for _ in 0..30 {
                streamer.process_frame(&surface, timestamp).await.unwrap();
                timestamp += 33_333;
            }

            let duration = start.elapsed();
            let stats = streamer.get_stats();

            results.push((
                name,
                width * height,
                duration.as_millis(),
                stats.avg_encoding_time_us / 1000, // Convert to ms
            ));
        }

        // Verify performance scales reasonably with resolution
        for i in 1..results.len() {
            let (_, pixels_prev, _, _) = results[i - 1];
            let (_, pixels_curr, _, time_curr) = results[i];

            // Encoding time should increase with resolution, but not linearly
            let pixel_ratio = pixels_curr as f32 / pixels_prev as f32;
            assert!(pixel_ratio > 1.0, "Each resolution should be larger");

            // Basic sanity check - encoding shouldn't take more than 100ms even for 4K
            assert!(time_curr < 100, "Encoding time should be reasonable");
        }
    }

    #[tokio::test]
    async fn test_streaming_memory_management() {
        let mut streamer = VideoStreamer::new(5, 30); // Small buffer

        let surface = DisplaySurface {
            width: 1920,
            height: 1080,
            format: 0x1,
            data: vec![0; 1920 * 1080 * 4],
        };

        // Fill buffer
        let mut timestamp = 0;
        for _ in 0..10 {
            // More than buffer size
            streamer.process_frame(&surface, timestamp).await.unwrap();
            timestamp += 33_333;
        }

        let stats = streamer.get_stats();
        assert!(stats.buffer_usage <= 5, "Buffer should not exceed max size");

        // Calculate expected memory usage
        let frame_size = 1920 * 1080 * 4 + std::mem::size_of::<VideoFrame>();
        let expected_max_memory = frame_size * 5;
        assert!(
            stats.memory_usage <= expected_max_memory,
            "Memory usage should be bounded"
        );
    }
}