Files
VoelinClient/crates/tsc-media/tests/codec_roundtrip.rs
T
Claude e3269e3cc7 M8 (part 2): tsc-media integration tests and docs
- tests/codec_roundtrip.rs: synthetic pattern -> VP8 / VP9 -> decode
  (PSNR, rectangle and background colours, forced keyframes, bitrate
  change) and capture thread -> encoder -> decoder
- tests/x11_capture.rs: draws windows on a real X server ($DISPLAY, e.g.
  Xvfb), checks MIT-SHM and GetImage pixels, window listing, window
  capture -> VP8 -> decode colours, and cursor drawing; skipped without
  DISPLAY
- tests/openh264.rs: missing-library errors; with TSC_OPENH264_LIB a full
  H.264 roundtrip incl. SPS profile checks (High 100, Baseline 66); an
  ignored test downloads Cisco's binary and runs the roundtrip
- OpenH264: allow frame skipping so rate control holds the bitrate, no
  adaptive quantisation / background detection for screen content
- docs/media.md: API, backends, features, build requirements, licenses
  (OpenH264 runtime download and attribution), verification status

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
2026-09-26 05:02:38 +00:00

102 lines
3.7 KiB
Rust

//! Synthetic capture -> encoder -> decoder, through the public API.
#![cfg(feature = "vpx")]
use std::time::Duration;
use tsc_media::capture::synthetic::{BACKGROUND, RECT_COLOR, SyntheticScreen, TEXT_COLOR};
use tsc_media::{
CaptureOptions, Codec, Codecs, EncoderBackend, EncoderConfig, ScreenCapture, SourceId,
VideoFrame, convert,
};
const W: u32 = 320;
const H: u32 = 240;
const FPS: u32 = 30;
fn rgb_at(frame: &VideoFrame, x: u32, y: u32) -> [u8; 3] {
let rgba = convert::to_rgba_vec(frame).unwrap();
let i = ((y * frame.width + x) * 4) as usize;
[rgba[i], rgba[i + 1], rgba[i + 2]]
}
fn assert_close(actual: [u8; 3], expected: [u8; 3], tolerance: i32, what: &str) {
let ok = (0..3).all(|c| (i32::from(actual[c]) - i32::from(expected[c])).abs() <= tolerance);
assert!(ok, "{what}: got {actual:?}, expected {expected:?} ±{tolerance}");
}
/// Encode 30 frames of the pattern, force a keyframe midway, lower the
/// bitrate, and check every decoded frame.
fn roundtrip(codec: Codec) {
let codecs = Codecs::new();
let screen = SyntheticScreen::new(W, H);
let config = EncoderConfig { fps: FPS, bitrate_bps: 1_500_000, ..EncoderConfig::default() };
let mut encoder = codecs.new_encoder(codec, config).unwrap();
assert_eq!(encoder.backend(), EncoderBackend::Libvpx);
let mut decoder = codecs.new_decoder(codec).unwrap();
let mut worst = f64::INFINITY;
for n in 0..30 {
if n == 20 {
encoder.set_bitrate(600_000).unwrap();
}
let source = screen.frame(n, FPS);
let encoded = encoder.encode(&source, n == 15).unwrap();
assert_eq!(encoded.len(), 1, "{codec} frame {n}");
let encoded = &encoded[0];
assert_eq!(encoded.keyframe, n == 0 || n == 15, "{codec} frame {n}");
assert_eq!(encoded.pts_90khz, source.pts_90khz());
let decoded = decoder.decode(&encoded.data).unwrap().expect("a picture per frame");
assert_eq!((decoded.width, decoded.height), (W, H));
worst = worst.min(convert::psnr(&source, &decoded).unwrap());
// The rectangle is where the pattern put it, in its colour.
let (x, y, w, h) = screen.rect(n);
assert_close(rgb_at(&decoded, x + w / 2, y + h / 2), RECT_COLOR, 16, "rectangle");
assert_close(rgb_at(&decoded, W - 10, H - 10), BACKGROUND, 16, "background");
}
assert!(worst > 30.0, "{codec}: worst PSNR {worst:.1} dB");
}
#[test]
fn vp8_synthetic_roundtrip() {
roundtrip(Codec::Vp8);
}
#[test]
fn vp9_synthetic_roundtrip() {
roundtrip(Codec::Vp9);
}
/// Frames from the capture thread go through encode / decode in order.
#[tokio::test]
async fn capture_thread_to_decoder() {
let codecs = Codecs::new();
let mut screen = SyntheticScreen::new(W, H);
let options = CaptureOptions { fps: 60, cursor: false, queue: 8 };
let mut frames = screen.start(&SourceId::Synthetic, &options).await.unwrap();
let config = EncoderConfig { fps: 60, bitrate_bps: 1_000_000, ..EncoderConfig::default() };
let mut encoder = codecs.new_encoder(Codec::Vp8, config).unwrap();
let mut decoder = codecs.new_decoder(Codec::Vp8).unwrap();
let mut last_pts = None;
for _ in 0..10 {
let frame =
tokio::time::timeout(Duration::from_secs(5), frames.recv()).await.unwrap().unwrap();
let encoded = encoder.encode(&frame, false).unwrap();
for e in &encoded {
assert!(last_pts.is_none_or(|p| e.pts_90khz > p));
last_pts = Some(e.pts_90khz);
let decoded = decoder.decode(&e.data).unwrap().unwrap();
// The counter's first digit (3x5 cells of 5 px) sits at (10, 10).
let rgba = convert::to_rgba_vec(&decoded).unwrap();
let lit = (10..25u32)
.flat_map(|x| (10..35u32).map(move |y| (x, y)))
.filter(|&(x, y)| {
let p = &rgba[((y * W + x) * 4) as usize..][..3];
(0..3).all(|c| (i32::from(p[c]) - i32::from(TEXT_COLOR[c])).abs() < 60)
})
.count();
assert!(lit >= 25, "counter digit missing ({lit} lit pixels)");
}
}
screen.stop();
}