Kaleido-Desktop/src-tauri/src/player.rs
Elijah 9fd2b0603e Add a Quality upscaling tier and make enhancement verifiable
Quality runs ArtCNN C4F32 (its denoising model when Denoising is on),
KrigBilateral, debanding and CAS. Its network runs at any enlargement,
so 1080p in a maximized 1440p window is doubled and scaled down; Balanced
keeps FSRCNNX's 1.3x threshold.

The enhancement pipeline moves to player/enhance.ts. Mode changes are
serialized (latest wins). Shader errors in mpv's log, or network passes
missing from vo-passes, drop to the tier below with a notice, and the
chosen and running tiers are shown separately. HDR is judged from the
decoded video. Debanding, sharpening and denoising have their own
toggles, B compares with enhancement off, and the panel shows the
renderer's actual stages, GPU time and dropped frames.

Compiled shaders persist in the app cache (gpu-shader-cache-dir):
ArtCNN's first Direct3D compile takes about a minute, which the player
now announces, and later launches start immediately.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 19:37:32 -07:00

237 lines
8.2 KiB
Rust

//! Video playback: one long-lived libmpv instance whose output is composed
//! beneath the (transparent) web view, so the interface draws its controls
//! directly over the video. How the video reaches the screen is platform
//! specific; see `platform::VideoSurface`.
use std::sync::Arc;
use std::time::{Duration, Instant};
use parking_lot::Mutex;
use serde::Deserialize;
use serde_json::{json, Value};
use tauri::{AppHandle, Emitter, Manager, Runtime, WebviewWindow};
use crate::mpv::{self, Event, Mpv};
use crate::platform::VideoSurface;
#[derive(Default)]
pub struct PlayerState {
mpv: Mutex<Option<Arc<Mpv>>>,
surface: Mutex<Option<VideoSurface>>,
}
/// Properties streamed to the interface as they change.
const OBSERVED: &[&str] = &[
"time-pos",
"duration",
"pause",
"paused-for-cache",
"cache-buffering-state",
"demuxer-cache-time",
"eof-reached",
"idle-active",
"track-list",
"volume",
"mute",
"speed",
"sub-delay",
"audio-delay",
"video-params",
"hwdec-current",
"chapter-list",
"seeking",
];
#[derive(Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct InitOptions {
#[serde(default)]
pub hwdec: Option<String>,
#[serde(default)]
pub hdr: Option<bool>,
#[serde(default)]
pub extra: Vec<(String, String)>,
}
fn emit<R: Runtime>(app: &AppHandle<R>, payload: Value) {
let _ = app.emit("mpv", payload);
}
fn create_player<R: Runtime>(app: &AppHandle<R>, window: &WebviewWindow<R>, opts: &InitOptions) -> Result<Arc<Mpv>, String> {
let resource_dir = app.path().resource_dir().ok();
let mpv = Mpv::load(&mpv::library_candidates(resource_dir))?;
let surface = VideoSurface::create(window)?;
let render_options = surface.mpv_options();
*app.state::<PlayerState>().surface.lock() = Some(surface);
let set = |k: &str, v: &str| mpv.set_option(k, v);
for (k, v) in &render_options {
set(k, v)?;
}
set("hwdec", opts.hwdec.as_deref().unwrap_or("auto-safe"))?;
set("keep-open", "yes")?;
set("idle", "yes")?;
set("force-window", "yes")?;
set("osc", "no")?;
set("osd-level", "0")?;
set("input-default-bindings", "no")?;
set("input-vo-keyboard", "no")?;
set("input-cursor", "no")?;
set("cursor-autohide", "no")?;
set("terminal", "no")?;
set("ytdl", "no")?;
set("sub-auto", "no")?;
set("audio-client-name", "Kaleido")?;
set("title", "Kaleido")?;
set("cache", "yes")?;
set("demuxer-max-bytes", "300MiB")?;
set("demuxer-max-back-bytes", "100MiB")?;
set("demuxer-readahead-secs", "30")?;
set("network-timeout", "30")?;
set("hr-seek", "yes")?;
set("background-color", "#000000")?;
set("user-agent", "Kaleido")?;
// Keep compiled shaders between runs: translating the larger upscaling
// shaders for Direct3D can take seconds, and should only happen once.
if let Ok(dir) = app.path().app_cache_dir() {
let dir = dir.join("shader-cache");
if std::fs::create_dir_all(&dir).is_ok() {
let _ = set("gpu-shader-cache-dir", &dir.to_string_lossy());
}
}
if opts.hdr.unwrap_or(true) {
let _ = set("target-colorspace-hint", "yes");
}
for (k, v) in &opts.extra {
let _ = set(k, v);
}
mpv.request_log_messages("warn");
mpv.initialize()?;
for (i, name) in OBSERVED.iter().chain(crate::platform::SURFACE_PROPERTIES).enumerate() {
mpv.observe(i as u64 + 1, name);
}
let events = mpv.clone();
let app = app.clone();
std::thread::Builder::new()
.name("mpv-events".into())
.spawn(move || event_loop(app, events))
.map_err(|e| e.to_string())?;
Ok(mpv)
}
fn event_loop<R: Runtime>(app: AppHandle<R>, mpv: Arc<Mpv>) {
let mut last_time = Instant::now() - Duration::from_secs(1);
let mut last_pos = -1.0f64;
loop {
let Some(event) = mpv.wait_event(1.0) else { continue };
match event {
Event::Shutdown => break,
Event::StartFile => emit(&app, json!({"type": "start-file"})),
Event::FileLoaded => emit(&app, json!({"type": "file-loaded"})),
Event::PlaybackRestart => emit(&app, json!({"type": "playback-restart"})),
Event::Seek => emit(&app, json!({"type": "seek"})),
Event::EndFile { reason, error } => emit(&app, json!({"type": "end-file", "reason": reason, "error": error})),
Event::Property { name, value } => {
if crate::platform::SURFACE_PROPERTIES.contains(&name.as_str()) {
let state = app.state::<PlayerState>();
let mut surface = state.surface.lock();
if let Some(surface) = surface.as_mut() {
surface.on_property(&name, &value);
}
continue;
}
if name == "time-pos" {
// Positions change every frame; a few updates a second suffice.
let pos = value.as_f64().unwrap_or(-1.0);
let now = Instant::now();
if now.duration_since(last_time) < Duration::from_millis(200) && (pos - last_pos).abs() < 1.5 {
continue;
}
last_time = now;
last_pos = pos;
}
emit(&app, json!({"type": "property", "name": name, "value": value}));
}
Event::Log { level, prefix, text } => {
eprintln!("[mpv {level}] {prefix}: {text}");
emit(&app, json!({"type": "log", "level": level, "prefix": prefix, "text": text}));
}
Event::Other => {}
}
}
}
fn player<R: Runtime>(app: &AppHandle<R>) -> Result<Arc<Mpv>, String> {
app.state::<PlayerState>().mpv.lock().clone().ok_or_else(|| "The player has not started.".to_string())
}
#[tauri::command]
pub async fn player_init<R: Runtime>(app: AppHandle<R>, window: WebviewWindow<R>, options: Option<InitOptions>) -> Result<(), String> {
let state = app.state::<PlayerState>();
let mut guard = state.mpv.lock();
if guard.is_none() {
*guard = Some(create_player(&app, &window, &options.unwrap_or_default())?);
}
Ok(())
}
#[tauri::command]
pub async fn player_show<R: Runtime>(app: AppHandle<R>, window: WebviewWindow<R>, visible: bool) -> Result<(), String> {
let state = app.state::<PlayerState>();
let mpv = state.mpv.lock().clone();
let mut surface = state.surface.lock();
if let Some(surface) = surface.as_mut() {
surface.fit(&window, mpv.as_deref());
surface.show(visible);
}
Ok(())
}
#[derive(Deserialize)]
pub struct VideoRect {
x: f64,
y: f64,
width: f64,
height: f64,
}
/// Draws the video in a rectangle of the window (physical pixels), for the
/// miniplayer; `None` fills the window again.
#[tauri::command]
pub async fn player_rect<R: Runtime>(app: AppHandle<R>, window: WebviewWindow<R>, rect: Option<VideoRect>) -> Result<(), String> {
let state = app.state::<PlayerState>();
let mpv = state.mpv.lock().clone();
let mut surface = state.surface.lock();
if let Some(surface) = surface.as_mut() {
let rect = rect.map(|r| (r.x.round() as i32, r.y.round() as i32, r.width.round() as i32, r.height.round() as i32));
surface.set_rect(&window, mpv.as_deref(), rect);
}
Ok(())
}
#[tauri::command]
pub async fn player_command<R: Runtime>(app: AppHandle<R>, args: Vec<String>) -> Result<(), String> {
player(&app)?.command(&args)
}
#[tauri::command]
pub async fn player_set<R: Runtime>(app: AppHandle<R>, name: String, value: Value) -> Result<(), String> {
player(&app)?.set_property(&name, &value)
}
#[tauri::command]
pub async fn player_get<R: Runtime>(app: AppHandle<R>, name: String) -> Result<Value, String> {
player(&app)?.get_property(&name)
}
/// Keeps the video surface matched to the window's client area.
pub fn on_resize<R: Runtime>(window: &WebviewWindow<R>) {
let app = window.app_handle();
let state = app.state::<PlayerState>();
let mpv = state.mpv.lock().clone();
let mut surface = state.surface.lock();
if let Some(surface) = surface.as_mut() {
surface.fit(window, mpv.as_deref());
}
}