capture quality settings, storage cap enforcement, WGC downscaling, SQLite migrations

This commit is contained in:
Alvin-Zilverstand committed 2026-09-14 11:36:24 +02:00
1 parent 7be5517d18
commit 64e058ad2e
6 files changed
+424 -59

No files matched your search

+141 -36
View File
@@ -3,6 +3,44 @@ use rusqlite::{params, Connection};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
const CURRENT_SCHEMA_VERSION: u32 = MIGRATIONS.len() as u32;
const MIGRATIONS: &[&str] = &[
// v1: initial clips table (also used to repair databases predating versioned migrations).
"
CREATE TABLE IF NOT EXISTS clips (
id TEXT PRIMARY KEY NOT NULL,
title TEXT,
session_id TEXT NOT NULL,
game_id TEXT NOT NULL,
path TEXT NOT NULL,
thumbnail_path TEXT,
created_at_ms INTEGER NOT NULL,
duration_ms INTEGER NOT NULL,
source TEXT NOT NULL,
event_type TEXT,
tags_json TEXT NOT NULL,
upload_url TEXT,
upload_provider TEXT
);
CREATE INDEX IF NOT EXISTS clips_created_at_idx ON clips(created_at_ms DESC);
CREATE INDEX IF NOT EXISTS clips_game_id_idx ON clips(game_id);
",
// v2: upload history bookkeeping.
"
CREATE TABLE IF NOT EXISTS upload_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
clip_id TEXT NOT NULL,
provider TEXT NOT NULL,
status TEXT NOT NULL,
url TEXT,
error TEXT,
attempted_at_ms INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS upload_history_clip_id_idx ON upload_history(clip_id);
",
];
#[derive(Debug)]
pub struct ClipDatabase {
connection: Connection,
@@ -22,42 +60,32 @@ impl ClipDatabase {
}
pub fn init(&self) -> rusqlite::Result<()> {
self.connection.execute_batch(
"
CREATE TABLE IF NOT EXISTS clips (
id TEXT PRIMARY KEY NOT NULL,
title TEXT,
session_id TEXT NOT NULL,
game_id TEXT NOT NULL,
path TEXT NOT NULL,
thumbnail_path TEXT,
created_at_ms INTEGER NOT NULL,
duration_ms INTEGER NOT NULL,
source TEXT NOT NULL,
event_type TEXT,
tags_json TEXT NOT NULL,
upload_url TEXT,
upload_provider TEXT
);
CREATE INDEX IF NOT EXISTS clips_created_at_idx ON clips(created_at_ms DESC);
CREATE INDEX IF NOT EXISTS clips_game_id_idx ON clips(game_id);
CREATE TABLE IF NOT EXISTS upload_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
clip_id TEXT NOT NULL,
provider TEXT NOT NULL,
status TEXT NOT NULL,
url TEXT,
error TEXT,
attempted_at_ms INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS upload_history_clip_id_idx ON upload_history(clip_id);
",
)?;
self.migrate()?;
if !self.column_exists("clips", "title")? {
self.connection
.execute("ALTER TABLE clips ADD COLUMN title TEXT", [])?;
}
self.connection.pragma_update(None, "user_version", 2)
if self.user_version()? != CURRENT_SCHEMA_VERSION {
self.connection
.pragma_update(None, "user_version", CURRENT_SCHEMA_VERSION)?;
}
Ok(())
}
fn migrate(&self) -> rusqlite::Result<()> {
let mut version = self.user_version()?;
while (version as usize) < MIGRATIONS.len() {
self.connection.execute_batch(MIGRATIONS[version as usize])?;
version += 1;
self.connection
.pragma_update(None, "user_version", version)?;
}
Ok(())
}
fn user_version(&self) -> rusqlite::Result<u32> {
self.connection
.query_row("PRAGMA user_version", [], |row| row.get(0))
}
pub fn load_clips(&self) -> rusqlite::Result<Vec<Clip>> {
@@ -281,12 +309,89 @@ fn millis_to_system_time(millis: i64) -> SystemTime {
mod tests {
use super::*;
#[test]
fn round_trips_clip_records() {
let db_path = std::env::temp_dir().join(format!(
"clipforge-db-{}.sqlite",
fn temp_database_path(name: &str) -> PathBuf {
let path = std::env::temp_dir().join(format!(
"clipforge-db-{name}-{}.sqlite",
system_time_to_millis(SystemTime::now())
));
let _ = std::fs::remove_file(&path);
path
}
#[test]
fn fresh_database_walks_to_current_version() {
let db_path = temp_database_path("fresh");
let database = ClipDatabase::open(&db_path).expect("database");
assert_eq!(database.user_version().expect("version"), CURRENT_SCHEMA_VERSION);
database.connection.execute("SELECT 1 FROM upload_history", []).expect("v2 table exists");
let _ = std::fs::remove_file(db_path);
}
#[test]
fn opening_is_idempotent_and_does_not_downgrade() {
let db_path = temp_database_path("idempotent");
let first = ClipDatabase::open(&db_path).expect("database");
let second = ClipDatabase::open(&db_path).expect("database re-open");
assert_eq!(
first.user_version().expect("version"),
CURRENT_SCHEMA_VERSION
);
assert_eq!(
second.user_version().expect("version"),
CURRENT_SCHEMA_VERSION
);
let _ = std::fs::remove_file(db_path);
}
#[test]
fn legacy_database_is_repaired_and_upgraded() {
let db_path = temp_database_path("legacy");
{
let connection = Connection::open(&db_path).expect("raw connection");
connection
.execute_batch(
"
CREATE TABLE clips (
id TEXT PRIMARY KEY NOT NULL,
session_id TEXT NOT NULL,
game_id TEXT NOT NULL,
path TEXT NOT NULL,
thumbnail_path TEXT,
created_at_ms INTEGER NOT NULL,
duration_ms INTEGER NOT NULL,
source TEXT NOT NULL,
event_type TEXT,
tags_json TEXT NOT NULL,
upload_url TEXT,
upload_provider TEXT
);
",
)
.expect("legacy clips table");
connection
.pragma_update(None, "user_version", 0)
.expect("version 0");
}
let database = ClipDatabase::open(&db_path).expect("open with repair");
assert_eq!(database.user_version().expect("version"), CURRENT_SCHEMA_VERSION);
assert!(database.column_exists("clips", "title").expect("column lookup"));
assert!(database.column_exists("clips", "id").expect("column lookup"));
database
.insert_upload_history(
"clip-old",
"catbox",
"uploaded",
Some("https://example.test/old.mp4"),
None,
SystemTime::now(),
)
.expect("v2 table usable");
let _ = std::fs::remove_file(db_path);
}
#[test]
fn round_trips_clip_records() {
let db_path = temp_database_path("roundtrip");
let database = ClipDatabase::open(&db_path).expect("database");
let clip = Clip {
id: "clip-1".to_string(),
+63 -13
View File
@@ -1,6 +1,33 @@
use crate::capture::{CaptureBackend, CaptureConfig, CaptureError};
use std::path::PathBuf;
pub(crate) fn downscale_to_cap(
width: u32,
height: u32,
cap_width: u32,
cap_height: u32,
) -> (u32, u32) {
if width == 0 || height == 0 || cap_width == 0 || cap_height == 0 {
return (even_capped_dimension(width, cap_width), even_capped_dimension(height, cap_height));
}
let scale = (cap_width as f32 / width as f32).min(cap_height as f32 / height as f32);
if scale >= 1.0 {
return (
even_capped_dimension(width, cap_width),
even_capped_dimension(height, cap_height),
);
}
(
even_capped_dimension((width as f32 * scale).round() as u32, cap_width),
even_capped_dimension((height as f32 * scale).round() as u32, cap_height),
)
}
fn even_capped_dimension(value: u32, cap: u32) -> u32 {
let value = value.min(cap).max(2);
value - (value % 2)
}
#[cfg(target_os = "windows")]
mod windows_impl {
use super::*;
@@ -488,10 +515,6 @@ mod windows_impl {
}
}
fn even_dimension(value: u32) -> u32 {
value.saturating_sub(value % 2).max(2)
}
fn find_window_for_source(
source: &crate::capture::CaptureSource,
) -> Result<Option<Window>, HandlerError> {
@@ -526,25 +549,24 @@ mod windows_impl {
Ok(None)
}
fn window_size(window: Window, fallback_width: u32, fallback_height: u32) -> (u32, u32) {
fn window_size(window: Window, cap_width: u32, cap_height: u32) -> (u32, u32) {
let width = window
.width()
.ok()
.and_then(|value| u32::try_from(value.max(0)).ok())
.unwrap_or(fallback_width);
.unwrap_or(cap_width);
let height = window
.height()
.ok()
.and_then(|value| u32::try_from(value.max(0)).ok())
.unwrap_or(fallback_height);
(even_dimension(width), even_dimension(height))
.unwrap_or(cap_height);
downscale_to_cap(width, height, cap_width, cap_height)
}
fn monitor_size(monitor: Monitor, fallback_width: u32, fallback_height: u32) -> (u32, u32) {
(
even_dimension(monitor.width().unwrap_or(fallback_width)),
even_dimension(monitor.height().unwrap_or(fallback_height)),
)
fn monitor_size(monitor: Monitor, cap_width: u32, cap_height: u32) -> (u32, u32) {
let width = monitor.width().unwrap_or(cap_width);
let height = monitor.height().unwrap_or(cap_height);
downscale_to_cap(width, height, cap_width, cap_height)
}
fn recording_settings<T: TryInto<GraphicsCaptureItemType> + Send + 'static>(
@@ -669,6 +691,34 @@ impl CaptureBackend for NativeWgcReplayCaptureBackend {
}
}
#[cfg(test)]
mod downscale_tests {
use super::downscale_to_cap;
#[test]
fn keeps_resolution_at_or_below_the_cap() {
assert_eq!(downscale_to_cap(2560, 1440, 1280, 720), (1280, 720));
assert_eq!(downscale_to_cap(1920, 1080, 1280, 720), (1280, 720));
assert_eq!(downscale_to_cap(3840, 2160, 1920, 1080), (1920, 1080));
}
#[test]
fn leaves_smaller_sources_unchanged() {
assert_eq!(downscale_to_cap(800, 600, 1280, 720), (800, 600));
assert_eq!(downscale_to_cap(1280, 720, 1280, 720), (1280, 720));
}
#[test]
fn preserves_aspect_ratio_and_even_dimensions_when_downscaling() {
let (width, height) = downscale_to_cap(2000, 1000, 1000, 600);
assert_eq!(width % 2, 0);
assert_eq!(height % 2, 0);
assert!(width <= 1000);
assert!(height <= 600);
assert_eq!(width as f32 / height as f32, 2000.0 / 1000.0);
}
}
#[cfg(all(test, target_os = "windows"))]
mod tests {
use super::*;