Create Kaleido Desktop, a cinematic Plex client for Windows
Tauri 2 shell with a React/TypeScript interface and libmpv playback composed beneath the web view (DirectComposition on Windows). Shared code with per-OS platform modules and configs so macOS and Linux can follow. - Plex sign-in (browser, plex.tv/link or manual), server selection, Plex Home - Home, Movies and TV Shows (Discover, Library, Collections), search, people - Title pages with logos, ratings, cast, extras, seasons and episodes - Direct play with Plex conversion fallback, tracks, markers, autoplay - Metadata editing, artwork, fix match, refresh, analyze and deletion - Resumable offline downloads - App settings and Plex server administration with a live dashboard - Signed updater feed and Forgejo CI Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
00d72e70f4
121 changed files with 25315 additions and 0 deletions
207
src-tauri/src/player.rs
Normal file
207
src-tauri/src/player.rs
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
//! 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")?;
|
||||
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(())
|
||||
}
|
||||
|
||||
#[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());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue