Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Route SeedVR2 exports through workers

Bitti1c4db3fb4110
diff --git a/src-tauri/core/src/lib.rs b/src-tauri/core/src/lib.rs
index d0ea370..caebaed 100644
--- a/src-tauri/core/src/lib.rs
+++ b/src-tauri/core/src/lib.rs
@@ -12,5 +12,6 @@ pub mod slopfab;
pub mod storage;
pub mod weights;
pub mod worker;
+pub mod upscale_stream;
pub use slopfab::{default_dll_path, generate_reference_icon_batch, ReferenceIconBatchConfig, ReferenceIconSpec};
diff --git a/src-tauri/core/src/upscale_stream.rs b/src-tauri/core/src/upscale_stream.rs
new file mode 100644
index 0000000..9d01507
--- /dev/null
+++ b/src-tauri/core/src/upscale_stream.rs
@@ -0,0 +1,110 @@
+//! Bounded RGBA transport shared by desktop exports and LAN workers.
+use std::sync::{atomic::{AtomicBool, Ordering}, mpsc::{self, Receiver, SyncSender}, Arc, Mutex};
+use std::time::{Duration, Instant};
+
+enum Output { Frame(Vec<u8>), Done(Result<u64, String>) }
+pub struct Session {
+ input: Mutex<SyncSender<Option<Vec<u8>>>>,
+ output: Mutex<Receiver<Output>>,
+ pub stopped: Arc<AtomicBool>,
+ frame_bytes: usize,
+ touched: Mutex<Instant>,
+}
+
+pub fn frame_bytes(width: u32, height: u32) -> Result<usize, String> {
+ let bytes = width as u64 * height as u64 * 4;
+ if width == 0 || height == 0 || width > 16384 || height > 16384 || bytes > 512 * 1024 * 1024 {
+ return Err("Invalid upscale frame dimensions (maximum 512 MB per frame).".into());
+ }
+ Ok(bytes as usize)
+}
+
+fn send<T>(sender: &SyncSender<T>, mut value: T, stop: &AtomicBool) -> Result<(), String> {
+ loop {
+ if stop.load(Ordering::Relaxed) { return Err("Upscaling cancelled.".into()); }
+ match sender.try_send(value) {
+ Ok(()) => return Ok(()),
+ Err(mpsc::TrySendError::Full(pending)) => { value = pending; std::thread::sleep(Duration::from_millis(10)); }
+ Err(_) => return Err("Upscaler stream closed.".into()),
+ }
+ }
+}
+
+impl Session {
+ pub fn start<F>(input: (u32, u32), output: (u32, u32), run: F) -> Result<Arc<Self>, String>
+ where F: FnOnce(&AtomicBool, &mut (dyn FnMut() -> Result<Option<Vec<u8>>, String> + Send), &mut (dyn FnMut(Vec<u8>) -> Result<(), String> + Send)) -> Result<u64, String> + Send + 'static {
+ let frame_bytes = frame_bytes(input.0, input.1)?;
+ let output_bytes = self::frame_bytes(output.0, output.1)?;
+ let (input_tx, input_rx) = mpsc::sync_channel(2);
+ let (output_tx, output_rx) = mpsc::sync_channel(2);
+ let stopped = Arc::new(AtomicBool::new(false));
+ let session = Arc::new(Self { input: Mutex::new(input_tx), output: Mutex::new(output_rx), stopped: stopped.clone(), frame_bytes, touched: Mutex::new(Instant::now()) });
+ std::thread::spawn(move || {
+ let read_stop = stopped.clone();
+ let mut read = move || loop {
+ if read_stop.load(Ordering::Relaxed) { return Err("Upscaling cancelled.".into()); }
+ match input_rx.recv_timeout(Duration::from_millis(50)) {
+ Ok(frame) => return Ok(frame),
+ Err(mpsc::RecvTimeoutError::Timeout) => continue,
+ Err(_) => return Err("Upscaler input closed.".into()),
+ }
+ };
+ let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(&stopped, &mut read, &mut |frame| {
+ if frame.len() != output_bytes { return Err("Upscaler returned invalid frame dimensions.".into()); }
+ send(&output_tx, Output::Frame(frame), &stopped)
+ }))).unwrap_or_else(|_| Err("Upscale worker panicked.".into()));
+ let _ = send(&output_tx, Output::Done(result), &stopped);
+ });
+ Ok(session)
+ }
+ pub fn touch(&self) { if let Ok(mut at) = self.touched.lock() { *at = Instant::now(); } }
+ pub fn idle_for(&self) -> Duration { self.touched.lock().map(|at| at.elapsed()).unwrap_or_default() }
+ pub fn push(&self, bytes: Vec<u8>) -> Result<(), String> {
+ self.touch();
+ if bytes.len() != self.frame_bytes { return Err("Invalid upscale frame size.".into()); }
+ send(&*self.input.lock().map_err(|e| e.to_string())?, Some(bytes), &self.stopped)
+ }
+ pub fn finish(&self) -> Result<(), String> {
+ self.touch();
+ send(&*self.input.lock().map_err(|e| e.to_string())?, None, &self.stopped)
+ }
+ pub fn read(&self) -> Result<Vec<u8>, String> {
+ self.touch();
+ let receiver = self.output.lock().map_err(|e| e.to_string())?;
+ loop {
+ if self.stopped.load(Ordering::Relaxed) { return Err("Upscaling cancelled.".into()); }
+ match receiver.recv_timeout(Duration::from_millis(50)) {
+ Ok(Output::Frame(bytes)) => return Ok(bytes),
+ Ok(Output::Done(result)) => return result.map(|_| Vec::new()),
+ Err(mpsc::RecvTimeoutError::Timeout) => continue,
+ Err(_) => return Err("Upscaler output closed.".into()),
+ }
+ }
+ }
+ pub fn cancel(&self) { self.stopped.store(true, Ordering::Relaxed); }
+}
+impl Drop for Session { fn drop(&mut self) { self.cancel(); } }
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ #[test]
+ fn streams_frames_and_finishes() {
+ let session = Session::start((1, 1), (1, 1), |_, read, write| {
+ let mut count = 0;
+ while let Some(frame) = read()? { write(frame)?; count += 1; }
+ Ok(count)
+ }).unwrap();
+ session.push(vec![1, 2, 3, 4]).unwrap(); session.finish().unwrap();
+ assert_eq!(session.read().unwrap(), vec![1, 2, 3, 4]);
+ assert!(session.read().unwrap().is_empty());
+ }
+ #[test]
+ fn cancellation_releases_blocked_reader() {
+ let session = Session::start((1, 1), (1, 1), |_, read, _| { read()?; Ok(0) }).unwrap();
+ let waiting = session.clone();
+ let reader = std::thread::spawn(move || waiting.read());
+ session.cancel();
+ assert!(reader.join().unwrap().unwrap_err().contains("cancelled"));
+ }
+}
diff --git a/src-tauri/core/src/worker/protocol.rs b/src-tauri/core/src/worker/protocol.rs
index c129240..2aaf81e 100644
--- a/src-tauri/core/src/worker/protocol.rs
+++ b/src-tauri/core/src/worker/protocol.rs
@@ -9,9 +9,21 @@ use std::collections::BTreeMap;
pub const SERVICE_TYPE: &str = "_slopus-worker._tcp.local.";
/// Bumped on any incompatible change to the routes or bodies below.
-// v3 carries inverted image masks; older workers would silently edit the
-// preserved original instead of the extension if they ignored this field.
-pub const PROTOCOL_VERSION: u32 = 3;
+// v4 adds bounded SeedVR2 export streams. v3 added inverted image masks.
+pub const PROTOCOL_VERSION: u32 = 4;
+
+/// Streaming SeedVR2 restoration; model paths are resolved on the worker.
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct StartUpscale {
+ pub input_width: u32,
+ pub input_height: u32,
+ pub width: u32,
+ pub height: u32,
+ pub segment_frames: u32,
+}
+pub const SEEDVR2_MODEL: &str = "https://huggingface.co/Comfy-Org/SeedVR2/resolve/main/diffusion_models/seedvr2_3b_int8_convrot.safetensors";
+pub const SEEDVR2_VAE: &str = "https://huggingface.co/Comfy-Org/SeedVR2/resolve/main/vae/seedvr2_ema_vae_fp16.safetensors";
pub const DEFAULT_PORT: u16 = 47321;
pub const TOKEN_HEADER: &str = "x-slopus-token";
diff --git a/src-tauri/core/src/worker/server.rs b/src-tauri/core/src/worker/server.rs
index c615896..8a90f0a 100644
--- a/src-tauri/core/src/worker/server.rs
+++ b/src-tauri/core/src/worker/server.rs
@@ -156,6 +156,7 @@ struct Worker {
runtime: slopfab::SlopfabRuntime,
jobs: Mutex<HashMap<String, Arc<JobLog>>>,
preparations: Mutex<HashMap<String, Arc<Preparation>>>,
+ upscales: Mutex<HashMap<String, Arc<crate::upscale_stream::Session>>>,
}
struct HttpError(u16, String);
@@ -285,8 +286,21 @@ pub fn run(options: WorkerOptions) -> Result<(), String> {
runtime: slopfab::SlopfabRuntime::default(),
jobs: Mutex::default(),
preparations: Mutex::default(),
+ upscales: Mutex::default(),
});
let server = Server::http(("0.0.0.0", options.port)).map_err(|error| format!("Could not listen on port {}: {error}", options.port))?;
+ let cleanup = Arc::downgrade(&worker);
+ std::thread::spawn(move || loop {
+ std::thread::sleep(Duration::from_secs(60));
+ let Some(worker) = cleanup.upgrade() else { break };
+ if let Ok(mut sessions) = worker.upscales.lock() {
+ sessions.retain(|_, session| {
+ let keep = session.idle_for() < Duration::from_secs(30 * 60);
+ if !keep { session.cancel(); }
+ keep
+ });
+ };
+ });
let info = worker.info();
println!("Slopus worker '{}' ({}) listening on port {}.", info.name, info.id, options.port);
println!("Engine: {} {}", info.runtime.state, info.runtime.version.as_deref().unwrap_or(""));
@@ -373,6 +387,46 @@ impl Worker {
}
match (method, &parts[1..]) {
(Method::Get, ["info"]) => json(&self.info()),
+ (Method::Post, ["upscales", id]) => {
+ generated_file_stem(id)?;
+ let options: StartUpscale = body(request)?;
+ if !(1..=5).contains(&options.segment_frames) || options.width < 16 || options.height < 16 {
+ return Err("Invalid SeedVR2 dimensions or segment size.".into());
+ }
+ if self.vulkan { return Err("SeedVR2 requires a CUDA worker.".into()); }
+ let mut sessions = self.upscales.lock().map_err(|_| "Lock failed.")?;
+ sessions.retain(|_, session| {
+ let keep = session.idle_for() < Duration::from_secs(30 * 60);
+ if !keep { session.cancel(); }
+ keep
+ });
+ if !sessions.is_empty() { return Err("Another upscale export is running on this worker.".into()); }
+ let store = self.store.clone();
+ let session = crate::upscale_stream::Session::start((options.input_width, options.input_height), (options.width, options.height), move |stop, read, write| {
+ let files = [FileRef::Url { url: SEEDVR2_MODEL.into(), lora: false }, FileRef::Url { url: SEEDVR2_VAE.into(), lora: false }];
+ store.prepare(&files, stop, |_| {})?;
+ let config = slopfab::upscale::UpscaleConfig { method: slopfab::upscale::UpscaleMethod::Seedvr2,
+ model_path: store.resolve(&files[0])?.to_string_lossy().into_owned(),
+ vae_path: Some(store.resolve(&files[1])?.to_string_lossy().into_owned()) };
+ slopfab::upscale::run(&config, (options.input_width, options.input_height), (options.width, options.height), options.segment_frames as i32, stop, read, write)
+ })?;
+ sessions.insert(id.to_string(), session);
+ json(&true)
+ }
+ (Method::Post, ["upscales", id, "frames"]) => {
+ let session = self.upscale(id)?;
+ let mut pixels = Vec::new();
+ request.as_reader().take(512 * 1024 * 1024 + 1).read_to_end(&mut pixels).map_err(|e| e.to_string())?;
+ session.push(pixels)?;
+ json(&true)
+ }
+ (Method::Post, ["upscales", id, "finish"]) => { self.upscale(id)?.finish()?; json(&true) }
+ (Method::Get, ["upscales", id, "frames"]) => Ok(bytes(self.upscale(id)?.read()?, "application/octet-stream")),
+ (Method::Get, ["upscales", id]) => { self.upscale(id)?.touch(); json(&true) }
+ (Method::Delete, ["upscales", id]) => {
+ if let Some(session) = self.upscales.lock().map_err(|_| "Lock failed.")?.remove(*id) { session.cancel(); }
+ json(&true)
+ }
(Method::Post, ["uploads", "missing"]) => {
let files: Vec<FileRef> = body(request)?;
json(&MissingUploads { missing: self.store.missing(&files)? })
@@ -501,6 +555,10 @@ impl Worker {
self.jobs.lock().map_err(|_| "Lock failed.")?.get(id).cloned().ok_or(HttpError(404, "No job with this id.".into()))
}
+ fn upscale(&self, id: &str) -> Result<Arc<crate::upscale_stream::Session>, HttpError> {
+ self.upscales.lock().map_err(|_| "Lock failed.")?.get(id).cloned().ok_or(HttpError(404, "Upscale session has ended.".into()))
+ }
+
fn start_preparation(&self, id: &str, files: Vec<FileRef>) -> Result<(), String> {
generated_file_stem(id)?;
let preparation = Arc::new(Preparation {
diff --git a/src-tauri/src/app.rs b/src-tauri/src/app.rs
index 1c8bf55..7e3dc1e 100644
--- a/src-tauri/src/app.rs
+++ b/src-tauri/src/app.rs
@@ -187,6 +187,8 @@ pub fn run() {
commands::character_sheet::discard_character_sheet_views,
commands::artifacts::open_image_source,
commands::artifacts::export_generated_image,
+ commands::artifacts::choose_image_export_destination,
+ commands::artifacts::cancel_image_export,
crate::upscale::start_export_upscale,
crate::upscale::push_export_upscale,
crate::upscale::finish_export_upscale,
diff --git a/src-tauri/src/commands/artifacts.rs b/src-tauri/src/commands/artifacts.rs
index 861f831..0cb52af 100644
--- a/src-tauri/src/commands/artifacts.rs
+++ b/src-tauri/src/commands/artifacts.rs
@@ -4,7 +4,7 @@ use crate::{app_paths, diagnostics, reference_icons, rendered};
use std::fs;
use tauri::{
ipc::{InvokeBody, Request},
- AppHandle,
+ AppHandle, Emitter, State,
};
pub(crate) const GENERATED_FOLDER_HEADER: &str = "x-generated-folder";
pub(crate) const GENERATED_JOB_HEADER: &str = "x-generated-job";
@@ -268,22 +268,13 @@ pub(crate) enum ImageExportFormat {
const GENERATED_JPEG_QUALITY: u8 = 95;
#[tauri::command]
-pub(crate) async fn export_generated_image(
+pub(crate) async fn choose_image_export_destination(
app: AppHandle,
- folder_path: String,
- relative_path: String,
options: Option<ImageExportOptions>,
-) -> Result<bool, String> {
+) -> Result<Option<String>, String> {
tauri::async_runtime::spawn_blocking(move || {
use tauri_plugin_dialog::DialogExt;
let options = options.unwrap_or_default();
- let root = ProjectRoot::open(&folder_path)?;
- let source = root.existing(&relative_path)?;
- if !relative_path.starts_with("media/generated/")
- || !matches!(source.extension().and_then(|s| s.to_str()), Some("jpg" | "jpeg" | "png"))
- {
- return Err("Choose a generated JPEG or PNG image to export.".into());
- }
let dialog = app.dialog().file().set_title("Export image");
let dialog = match options.format {
Some(ImageExportFormat::Jpg) => dialog.add_filter("JPEG image", &["jpg", "jpeg"]).set_file_name("image.jpg"),
@@ -294,7 +285,7 @@ pub(crate) async fn export_generated_image(
.set_file_name("image"),
};
let Some(destination) = dialog.blocking_save_file() else {
- return Ok(false);
+ return Ok(None);
};
let destination = destination
.into_path()
@@ -305,13 +296,53 @@ pub(crate) async fn export_generated_image(
Some(ImageExportFormat::Jpg) if !has_extension(&destination, &["jpg", "jpeg"]) => destination.with_extension("jpg"),
_ => destination,
};
- export_image_file_with(&source, &destination, options)?;
- Ok(true)
+ IMAGE_DESTINATIONS.lock().map_err(|e| e.to_string())?.insert(destination.clone());
+ Ok(Some(destination.to_string_lossy().into_owned()))
})
.await
.map_err(|error| error.to_string())?
}
+static IMAGE_DESTINATIONS: std::sync::LazyLock<std::sync::Mutex<std::collections::HashSet<std::path::PathBuf>>> = std::sync::LazyLock::new(Default::default);
+static IMAGE_EXPORTS: std::sync::LazyLock<std::sync::Mutex<std::collections::HashMap<String, std::sync::Arc<std::sync::atomic::AtomicBool>>>> = std::sync::LazyLock::new(Default::default);
+
+#[tauri::command]
+pub(crate) async fn export_generated_image(app: AppHandle, workers: State<'_, crate::worker::Workers>,
+ job_id: String, folder_path: String, relative_path: String, destination: String, options: Option<ImageExportOptions>) -> Result<u64, String> {
+ let destination = std::path::PathBuf::from(destination);
+ if !IMAGE_DESTINATIONS.lock().map_err(|e| e.to_string())?.contains(&destination) {
+ return Err("Choose the image destination with the save dialog first.".into());
+ }
+ let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
+ {
+ let mut jobs = IMAGE_EXPORTS.lock().map_err(|e| e.to_string())?;
+ if jobs.contains_key(&job_id) { return Err("This image export is already running.".into()); }
+ jobs.insert(job_id.clone(), stop.clone());
+ }
+ let id = job_id.clone();
+ let workers = workers.inner().clone();
+ let result = tauri::async_runtime::spawn_blocking(move || {
+ let root = ProjectRoot::open(&folder_path)?;
+ let source = root.existing(&relative_path)?;
+ let progress = |phase: &str, detail: &str| {
+ let _ = app.emit("export-progress", serde_json::json!({ "jobId": id, "phase": phase,
+ "framesDone": if phase == "writing" { 1 } else { 0 }, "frameCount": 1, "detail": detail }));
+ };
+ export_image_file_running(&source, &destination, options.unwrap_or_default(), &workers, &id, &stop, &progress)?;
+ fs::metadata(destination).map(|info| info.len()).map_err(|e| e.to_string())
+ }).await.map_err(|e| e.to_string()).and_then(|result| result);
+ IMAGE_EXPORTS.lock().map_err(|e| e.to_string())?.remove(&job_id);
+ result
+}
+
+#[tauri::command]
+pub(crate) fn cancel_image_export(job_id: String) -> Result<(), String> {
+ if let Some(stop) = IMAGE_EXPORTS.lock().map_err(|e| e.to_string())?.get(&job_id) {
+ stop.store(true, std::sync::atomic::Ordering::Relaxed);
+ }
+ Ok(())
+}
+
fn has_extension(path: &std::path::Path, extensions: &[&str]) -> bool {
path.extension()
.and_then(|value| value.to_str())
@@ -331,11 +362,20 @@ pub(crate) fn export_image_file(
/// Copy the file untouched when nothing would change (same format, same size,
/// and for JPEG no quality below the one it was written at); otherwise decode,
/// resize with Lanczos3 and encode. File extensions always match the bytes.
+#[cfg(test)]
pub(crate) fn export_image_file_with(
source: &std::path::Path,
destination: &std::path::Path,
options: ImageExportOptions,
) -> Result<(), String> {
+ export_image_file_running(source, destination, options, &crate::worker::Workers::default(), "image-test",
+ &std::sync::atomic::AtomicBool::new(false), &|_, _| {})
+}
+
+fn export_image_file_running(source: &std::path::Path, destination: &std::path::Path, options: ImageExportOptions,
+ workers: &crate::worker::Workers, id: &str, stop: &std::sync::atomic::AtomicBool, progress: &dyn Fn(&str, &str)) -> Result<(), String> {
+ if stop.load(std::sync::atomic::Ordering::Relaxed) { return Err("Export cancelled.".into()); }
+ progress("preparing", "Reading image…");
let destination = if destination.extension().is_none() {
destination.with_extension("jpg")
} else {
@@ -375,9 +415,10 @@ pub(crate) fn export_image_file_with(
let original = image.to_rgba8();
let mut input = Some(original.clone().into_raw());
let mut output = None;
- crate::slopfab::upscale::run(config, (source_width, source_height), (width, height),
+ progress("rendering", if config.method == crate::slopfab::upscale::UpscaleMethod::Seedvr2 { "Upscaling with SeedVR2 on worker…" } else { "Upscaling with Real-ESRGAN…" });
+ crate::upscale::run(workers, id, config, (source_width, source_height), (width, height),
1,
- &std::sync::atomic::AtomicBool::new(false), &mut || Ok(input.take()),
+ stop, &mut || Ok(input.take()),
&mut |bytes| { output = Some(bytes); Ok(()) })?;
let mut restored = image::RgbaImage::from_raw(width, height, output.ok_or("Upscaler returned no image.")?)
.ok_or("Upscaler returned invalid image dimensions.")?;
@@ -395,6 +436,8 @@ pub(crate) fn export_image_file_with(
}
encoded.into_inner()
};
+ if stop.load(std::sync::atomic::Ordering::Relaxed) { return Err("Export cancelled.".into()); }
+ progress("writing", "Writing image…");
crate::storage::atomic::write_atomically(&destination, &bytes)
}
diff --git a/src-tauri/src/tests/images.rs b/src-tauri/src/tests/images.rs
index 449771a..1a92a75 100644
--- a/src-tauri/src/tests/images.rs
+++ b/src-tauri/src/tests/images.rs
@@ -160,7 +160,7 @@ fn image_export_does_not_silently_skip_a_selected_upscaler() {
upscale: Some(UpscaleConfig { method, model_path: folder.path().join("missing.safetensors").to_string_lossy().into(), vae_path: None }),
..Default::default()
}).unwrap_err();
- assert!(error.contains("weights are missing"));
+ assert!(error.contains(if method == UpscaleMethod::Seedvr2 { "Select a worker" } else { "weights are missing" }));
assert!(!destination.exists());
assert_eq!(fs::read(&source).unwrap(), original);
}
diff --git a/src-tauri/src/upscale.rs b/src-tauri/src/upscale.rs
index 75e587f..f0afca0 100644
--- a/src-tauri/src/upscale.rs
+++ b/src-tauri/src/upscale.rs
@@ -1,102 +1,36 @@
-use crate::slopfab::upscale::{self, UpscaleConfig};
-use std::{
- collections::HashMap,
- sync::{
- atomic::{AtomicBool, Ordering},
- mpsc::{self, Receiver, SyncSender},
- Arc, LazyLock, Mutex,
- },
- time::Duration,
-};
-use tauri::ipc::{InvokeBody, Request, Response};
+use crate::{slopfab::upscale::{self, UpscaleConfig, UpscaleMethod}, worker::Workers};
+use slopus_core::{upscale_stream::Session, worker::protocol::StartUpscale};
+use std::{collections::HashMap, sync::{atomic::AtomicBool, Arc, LazyLock, Mutex}};
+use tauri::{ipc::{InvokeBody, Request, Response}, State};
-enum Output {
- Frame(Vec<u8>),
- Done(Result<u64, String>),
-}
-struct Session {
- input: Mutex<SyncSender<Option<Vec<u8>>>>,
- output: Mutex<Receiver<Output>>,
- stopped: Arc<AtomicBool>,
- frame_bytes: usize,
-}
static SESSIONS: LazyLock<Mutex<HashMap<String, Arc<Session>>>> = LazyLock::new(Default::default);
fn session(id: &str) -> Result<Arc<Session>, String> {
- SESSIONS
- .lock()
- .map_err(|e| e.to_string())?
- .get(id)
- .cloned()
- .ok_or("Upscale session has ended.".into())
+ SESSIONS.lock().map_err(|e| e.to_string())?.get(id).cloned().ok_or("Upscale session has ended.".into())
}
-fn send<T>(sender: &SyncSender<T>, mut value: T, stop: &AtomicBool) -> Result<(), String> {
- loop {
- if stop.load(Ordering::Relaxed) {
- return Err("Upscaling cancelled.".into());
- }
- match sender.try_send(value) {
- Ok(()) => return Ok(()),
- Err(mpsc::TrySendError::Full(pending)) => {
- value = pending;
- std::thread::sleep(Duration::from_millis(10));
- }
- Err(mpsc::TrySendError::Disconnected(_)) => {
- return Err("Upscaler stream closed.".into())
- }
- }
+
+pub(crate) fn run(workers: &Workers, id: &str, config: &UpscaleConfig,
+ input: (u32, u32), output: (u32, u32), segment_frames: u32, stop: &AtomicBool,
+ read: &mut (dyn FnMut() -> Result<Option<Vec<u8>>, String> + Send),
+ write: &mut (dyn FnMut(Vec<u8>) -> Result<(), String> + Send)) -> Result<u64, String> {
+ if config.method == UpscaleMethod::Seedvr2 {
+ let worker = workers.active()?.ok_or("Select a worker in Settings → Workers to use SeedVR2 upscaling.")?;
+ worker.upscale(id, &StartUpscale { input_width: input.0, input_height: input.1, width: output.0, height: output.1, segment_frames }, stop, read, write)
+ } else {
+ upscale::run(config, input, output, segment_frames as i32, stop, read, write)
}
}
#[tauri::command]
-pub(crate) fn start_export_upscale(
- id: String,
- config: UpscaleConfig,
- input_width: u32,
- input_height: u32,
- width: u32,
- height: u32,
-) -> Result<(), String> {
- upscale::validate(&config, (input_width, input_height), (width, height))?;
+pub(crate) fn start_export_upscale(workers: State<'_, Workers>, id: String, config: UpscaleConfig,
+ input_width: u32, input_height: u32, width: u32, height: u32) -> Result<(), String> {
let mut sessions = SESSIONS.lock().map_err(|e| e.to_string())?;
- if !sessions.is_empty() {
- return Err("Another upscale export is already running.".into());
- }
- let (input_tx, input_rx) = mpsc::sync_channel(2);
- let (output_tx, output_rx) = mpsc::sync_channel(2);
- let stopped = Arc::new(AtomicBool::new(false));
- sessions.insert(
- id,
- Arc::new(Session {
- input: Mutex::new(input_tx),
- output: Mutex::new(output_rx),
- stopped: stopped.clone(),
- frame_bytes: input_width as usize * input_height as usize * 4,
- }),
- );
- std::thread::spawn(move || {
- let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
- upscale::run(
- &config,
- (input_width, input_height),
- (width, height),
- 5,
- &stopped,
- &mut || loop {
- if stopped.load(Ordering::Relaxed) {
- return Err("Upscaling cancelled.".into());
- }
- match input_rx.recv_timeout(Duration::from_millis(50)) {
- Ok(frame) => return Ok(frame),
- Err(mpsc::RecvTimeoutError::Timeout) => continue,
- Err(_) => return Err("Upscaler input closed.".into()),
- }
- },
- &mut |frame| send(&output_tx, Output::Frame(frame), &stopped),
- )
- }))
- .unwrap_or_else(|_| Err("Upscale worker panicked.".into()));
- let _ = send(&output_tx, Output::Done(result), &stopped);
- });
+ if !sessions.is_empty() { return Err("Another upscale export is already running.".into()); }
+ let workers = workers.inner().clone();
+ let job_id = id.clone();
+ let session = Session::start((input_width, input_height), (width, height), move |stop, read, write| {
+ run(&workers, &job_id, &config, (input_width, input_height), (width, height), 5, stop, read, write)
+ })?;
+ sessions.insert(id, session);
Ok(())
}
@@ -104,76 +38,34 @@ pub(crate) fn start_export_upscale(
pub(crate) async fn push_export_upscale(request: Request<'_>) -> Result<(), String> {
let id = crate::commands::binary::decoded_header(&request, "x-upscale-id")?;
let session = session(&id)?;
- let InvokeBody::Raw(bytes) = request.body() else {
- return Err("Expected raw RGBA pixels.".into());
- };
- if bytes.len() != session.frame_bytes {
- return Err("Invalid upscale frame size.".into());
- }
+ let InvokeBody::Raw(bytes) = request.body() else { return Err("Expected raw RGBA pixels.".into()); };
let bytes = bytes.clone();
- tauri::async_runtime::spawn_blocking(move || {
- send(
- &*session.input.lock().map_err(|e| e.to_string())?,
- Some(bytes),
- &session.stopped,
- )
- })
- .await
- .map_err(|e| e.to_string())?
+ tauri::async_runtime::spawn_blocking(move || session.push(bytes)).await.map_err(|e| e.to_string())?
}
#[tauri::command]
pub(crate) async fn finish_export_upscale(id: String) -> Result<(), String> {
let session = session(&id)?;
- tauri::async_runtime::spawn_blocking(move || {
- send(
- &*session.input.lock().map_err(|e| e.to_string())?,
- None,
- &session.stopped,
- )
- })
- .await
- .map_err(|e| e.to_string())?
+ tauri::async_runtime::spawn_blocking(move || session.finish()).await.map_err(|e| e.to_string())?
}
#[tauri::command]
pub(crate) async fn read_export_upscale(id: String) -> Result<Response, String> {
let session = session(&id)?;
- tauri::async_runtime::spawn_blocking(move || {
- let receiver = session.output.lock().map_err(|e| e.to_string())?;
- loop {
- if session.stopped.load(Ordering::Relaxed) {
- return Err("Upscaling cancelled.".into());
- }
- match receiver.recv_timeout(Duration::from_millis(50)) {
- Ok(Output::Frame(bytes)) => return Ok(Response::new(bytes)),
- Ok(Output::Done(result)) => return result.map(|_| Response::new(Vec::<u8>::new())),
- Err(mpsc::RecvTimeoutError::Timeout) => continue,
- Err(_) => return Err("Upscaler output closed.".into()),
- }
- }
- })
- .await
- .map_err(|e| e.to_string())?
+ tauri::async_runtime::spawn_blocking(move || session.read().map(Response::new)).await.map_err(|e| e.to_string())?
}
#[tauri::command]
pub(crate) fn cancel_export_upscale(id: String) -> Result<(), String> {
- if let Some(session) = SESSIONS.lock().map_err(|e| e.to_string())?.remove(&id) {
- session.stopped.store(true, Ordering::Relaxed);
- }
+ if let Some(session) = SESSIONS.lock().map_err(|e| e.to_string())?.remove(&id) { session.cancel(); }
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
-
#[test]
- fn cancellation_releases_a_producer_blocked_by_backpressure() {
- let (sender, _receiver) = mpsc::sync_channel(1);
- sender.send(1).unwrap();
- let stop = Arc::new(AtomicBool::new(false));
- let producer_stop = stop.clone();
- let producer = std::thread::spawn(move || send(&sender, 2, &producer_stop));
- stop.store(true, Ordering::Relaxed);
- assert!(producer.join().unwrap().unwrap_err().contains("cancelled"));
+ fn seedvr2_requires_a_worker_instead_of_running_locally() {
+ let config = UpscaleConfig { method: UpscaleMethod::Seedvr2, model_path: String::new(), vae_path: None };
+ let result = run(&Workers::default(), "test", &config, (16, 16), (32, 32), 1,
+ &AtomicBool::new(false), &mut || Ok(None), &mut |_| Ok(()));
+ assert!(result.unwrap_err().contains("Select a worker"));
}
}
diff --git a/src-tauri/src/worker/client.rs b/src-tauri/src/worker/client.rs
index ca1bc63..c7c0f25 100644
--- a/src-tauri/src/worker/client.rs
+++ b/src-tauri/src/worker/client.rs
@@ -156,6 +156,59 @@ fn worker_error(name: &str, response: Response) -> String {
}
impl Connection {
+ pub(crate) fn upscale(&self, id: &str, options: &StartUpscale, stop: &AtomicBool,
+ read: &mut (dyn FnMut() -> Result<Option<Vec<u8>>, String> + Send),
+ write: &mut (dyn FnMut(Vec<u8>) -> Result<(), String> + Send)) -> Result<u64, String> {
+ if !self.info.runtime.cuda_available { return Err(format!("Worker {} needs CUDA for SeedVR2.", self.name)); }
+ self.post::<bool>(&format!("/v1/upscales/{id}"), options)?;
+ let finished = AtomicBool::new(false);
+ let failed = AtomicBool::new(false);
+ let result = std::thread::scope(|scope| {
+ let monitor = scope.spawn(|| {
+ let mut ticks = 0;
+ while !finished.load(Ordering::Relaxed) {
+ if stop.load(Ordering::Relaxed) || failed.load(Ordering::Relaxed) {
+ let _ = self.send(self.request(reqwest::Method::DELETE, &format!("/v1/upscales/{id}")).timeout(Duration::from_secs(5)));
+ break;
+ }
+ ticks += 1;
+ if ticks % 50 == 0 {
+ let _ = self.send(self.request(reqwest::Method::GET, &format!("/v1/upscales/{id}")).timeout(Duration::from_secs(5)));
+ }
+ std::thread::sleep(Duration::from_millis(100));
+ }
+ });
+ let output = scope.spawn(|| -> Result<u64, String> {
+ let result = (|| {
+ let mut count = 0;
+ loop {
+ let bytes = self.bytes(&format!("/v1/upscales/{id}/frames"))?;
+ if bytes.is_empty() { return Ok(count); }
+ write(bytes)?;
+ count += 1;
+ }
+ })();
+ if result.is_err() { failed.store(true, Ordering::Relaxed); }
+ result
+ });
+ let input = (|| -> Result<(), String> {
+ while let Some(frame) = read()? {
+ if stop.load(Ordering::Relaxed) || failed.load(Ordering::Relaxed) { return Err("Upscaler stream stopped.".into()); }
+ self.post_bytes::<bool>(&format!("/v1/upscales/{id}/frames"), &[], frame)?;
+ }
+ self.post::<bool>(&format!("/v1/upscales/{id}/finish"), &())?;
+ Ok(())
+ })();
+ if input.is_err() { failed.store(true, Ordering::Relaxed); }
+ let output = output.join().unwrap_or_else(|_| Err("Worker upscale reader panicked.".into()));
+ finished.store(true, Ordering::Relaxed);
+ let _ = monitor.join();
+ output.and_then(|count| input.map(|_| count))
+ });
+ let _ = self.send(self.request(reqwest::Method::DELETE, &format!("/v1/upscales/{id}")).timeout(Duration::from_secs(5)));
+ result
+ }
+
fn request(&self, method: reqwest::Method, path: &str) -> reqwest::blocking::RequestBuilder {
let builder = self.http.request(method, format!("{}{path}", self.base));
match &self.token {
@@ -887,6 +940,53 @@ mod tests {
pub(super) static FAILED: Mutex<Vec<String>> = Mutex::new(Vec::new());
+ #[test]
+ fn upscale_stream_sends_and_receives_frames_with_bounded_backpressure() {
+ let server = tiny_http::Server::http("127.0.0.1:0").unwrap();
+ let address = server.server_addr().to_ip().unwrap().to_string();
+ let session = slopus_core::upscale_stream::Session::start((1, 1), (1, 1), |_, read, write| {
+ // Like SeedVR2, wait for a temporal segment before writing output.
+ let mut frames = Vec::new();
+ while let Some(frame) = read()? { frames.push(frame); }
+ let count = frames.len() as u64;
+ for frame in frames { write(frame)?; }
+ Ok(count)
+ }).unwrap();
+ let server_thread = std::thread::spawn(move || {
+ while let Some(mut request) = server.recv_timeout(Duration::from_secs(5)).unwrap() {
+ let session = session.clone();
+ let remove = request.method() == &tiny_http::Method::Delete;
+ std::thread::spawn(move || {
+ assert!(request.headers().iter().any(|header| header.field.equiv(TOKEN_HEADER) && header.value.as_str() == "test-token"));
+ let response = match (request.method().clone(), request.url()) {
+ (tiny_http::Method::Post, "/v1/upscales/test") => b"true".to_vec(),
+ (tiny_http::Method::Post, "/v1/upscales/test/frames") => {
+ let mut bytes = Vec::new(); request.as_reader().read_to_end(&mut bytes).unwrap();
+ session.push(bytes).unwrap(); b"true".to_vec()
+ }
+ (tiny_http::Method::Post, "/v1/upscales/test/finish") => { session.finish().unwrap(); b"true".to_vec() }
+ (tiny_http::Method::Get, "/v1/upscales/test/frames") => session.read().unwrap(),
+ (tiny_http::Method::Delete, "/v1/upscales/test") => { session.cancel(); b"true".to_vec() }
+ _ => panic!("Unexpected upscale request"),
+ };
+ request.respond(tiny_http::Response::from_data(response)).unwrap();
+ });
+ if remove { break; }
+ }
+ });
+ let info = WorkerInfo { id: "00112233aabbccdd".into(), name: "Stub".into(), version: "0".into(), protocol: PROTOCOL_VERSION,
+ requires_token: true, gpus: Vec::new(), runtime: WorkerRuntime { state: "ready".into(), version: None,
+ platform: None, cuda_available: true, cuda_device_names: Vec::new(), detail: String::new() } };
+ let connection = Connection { base: format!("http://{address}"), token: Some("test-token".into()), name: "Stub".into(), address, info, http: http_client(Some(Duration::from_secs(5))) };
+ let mut source = vec![vec![1; 4], vec![2; 4], vec![3; 4], vec![4; 4], vec![5; 4]].into_iter();
+ let mut frames = Vec::new();
+ let count = connection.upscale("test", &StartUpscale { input_width: 1, input_height: 1, width: 1, height: 1, segment_frames: 5 },
+ &AtomicBool::new(false), &mut || Ok(source.next()), &mut |frame| { frames.push(frame); Ok(()) }).unwrap();
+ assert_eq!(count, 5);
+ assert_eq!(frames, vec![vec![1; 4], vec![2; 4], vec![3; 4], vec![4; 4], vec![5; 4]]);
+ server_thread.join().unwrap();
+ }
+
#[test]
fn addresses_get_the_default_port_and_reject_urls_with_paths() {
assert_eq!(normalize_address("192.168.1.20").unwrap(), format!("192.168.1.20:{DEFAULT_PORT}"));