| 1 | |
| 2 | |
| 3 | use engine::render_cache; |
| 4 | use serde_json::{json, Value}; |
| 5 | |
| 6 | use crate::parse::{session_end, session_fps}; |
| 7 | use crate::server::{opt_bool, tri, Reply, Server, Tool}; |
| 8 | use crate::tools::export::parse_range; |
| 9 | |
| 10 | pub fn tools() -> Vec<Tool> { |
| 11 | vec![Tool { |
| 12 | name: "render_preview", |
| 13 | description: "Pre-render part of the timeline into the render cache, so playback over it reads finished frames instead of compositing them: use it over heavy sections (stacked effects, titles over video, colour, speed, multicam) that drop frames live. `range` is \"all\" (the default, the whole timeline) or {from, to} as export takes it. The render runs in the background; project_status shows its progress and the cached ranges. Any edit inside a cached range drops that render, and playback composes live again there. `cancel: true` stops the render running now and removes its partial file. `clear: true` deletes every cached render file on disk (finished or half written) and says how many went; it is refused while a render is running. One render runs at a time. The sound is not rendered: the live mix always plays.", |
| 14 | schema, |
| 15 | run: render_preview, |
| 16 | }] |
| 17 | } |
| 18 | |
| 19 | fn schema() -> Value { |
| 20 | json!({ |
| 21 | "type": "object", |
| 22 | "properties": { |
| 23 | "range": { |
| 24 | "type": ["string", "object"], |
| 25 | "description": "\"all\" for the whole timeline (the default), or {\"from\": \"<time>\", \"to\": \"<time>\"}.", |
| 26 | "examples": [{"from": "2s", "to": "5s"}] |
| 27 | }, |
| 28 | "cancel": { "type": "boolean", "description": "true cancels the render running now." }, |
| 29 | "clear": { "type": "boolean", "description": "true deletes every render file in the render cache, as File > Delete render files does." } |
| 30 | } |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | fn render_preview(srv: &mut Server, args: &Value) -> Reply { |
| 35 | if opt_bool(args, "clear").unwrap_or(false) { |
| 36 | return clear_renders(srv); |
| 37 | } |
| 38 | if opt_bool(args, "cancel").unwrap_or(false) { |
| 39 | return match srv.render.as_ref().filter(|(job, _)| !job.is_finished()) { |
| 40 | Some((job, _)) => { |
| 41 | job.cancel(); |
| 42 | Reply::ok("render cancelled; its partial file is removed") |
| 43 | } |
| 44 | None => Reply::ok("no render is running"), |
| 45 | }; |
| 46 | } |
| 47 | if srv.render.as_ref().is_some_and(|(job, _)| !job.is_finished()) { |
| 48 | return Reply::err("a render preview is already running: poll project_status, or stop it with render_preview {cancel: true}"); |
| 49 | } |
| 50 | |
| 51 | let mut note = String::new(); |
| 52 | if let Some(Err(e)) = srv.render.take().and_then(|(job, _)| job.outcome()) { |
| 53 | note = format!("the last render failed: {e}\n"); |
| 54 | } |
| 55 | let s = tri!(srv.session_ref()); |
| 56 | let fps = session_fps(s); |
| 57 | let end = session_end(s); |
| 58 | let (from, to) = match args.get("range") { |
| 59 | None | Some(Value::Null) => (0, end), |
| 60 | Some(Value::String(word)) if word == "all" => (0, end), |
| 61 | Some(_) => match parse_range(args, fps, end, end) { |
| 62 | Ok(Some(range)) => range, |
| 63 | Ok(None) => (0, end), |
| 64 | Err(e) => return Reply::err(e), |
| 65 | }, |
| 66 | }; |
| 67 | let (project, meta) = s.export_snapshot(); |
| 68 | let size = engine::proxy::size_for(&meta); |
| 69 | match render_cache::start(project, meta, from, to) { |
| 70 | Ok(job) => { |
| 71 | let done = job.is_finished(); |
| 72 | srv.render = Some((job, (from, to))); |
| 73 | let secs = |f: u32| format!("{:.1}s", f64::from(f) / fps); |
| 74 | let what = match done { |
| 75 | true => "is already cached".to_string(), |
| 76 | false => format!("is rendering at {}x{}", size.0, size.1), |
| 77 | }; |
| 78 | Reply::ok(format!("{note}frames {}-{} {what}; project_status shows it", secs(from), secs(to))) |
| 79 | } |
| 80 | Err(e) => Reply::err(format!("{note}{e}")), |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | fn clear_renders(srv: &mut Server) -> Reply { |
| 87 | if srv.render.as_ref().is_some_and(|(job, _)| !job.is_finished()) { |
| 88 | return Reply::err("a render preview is running: stop it with render_preview {cancel: true} before clearing the render files"); |
| 89 | } |
| 90 | srv.render = None; |
| 91 | match render_cache::delete_all() { |
| 92 | Ok(0) => Reply::ok("no render files to delete").with(json!({"deleted": 0})), |
| 93 | Ok(n) => Reply::ok(format!("deleted {n} render file{}", if n == 1 { "" } else { "s" })).with(json!({"deleted": n})), |
| 94 | Err(e) => Reply::err(format!("could not delete the render files: {e}")), |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | pub fn status_line(server: &Server) -> Option<String> { |
| 100 | let s = server.session_ref().ok()?; |
| 101 | let fps = session_fps(s); |
| 102 | let secs = |f: u32| format!("{:.1}s", f64::from(f) / fps); |
| 103 | let mut parts = Vec::new(); |
| 104 | if let Some((job, (from, to))) = server.render.as_ref().filter(|(job, _)| !job.is_finished()) { |
| 105 | parts.push(format!("rendering {}-{} {:.0}%", secs(*from), secs(*to), job.progress() * 100.0)); |
| 106 | } |
| 107 | let (project, meta) = s.export_snapshot(); |
| 108 | let cached = render_cache::segments(&project, &meta); |
| 109 | if cached.is_empty() { |
| 110 | parts.push("nothing cached".to_string()); |
| 111 | } else { |
| 112 | let ranges: Vec<String> = cached.iter().map(|c| format!("{}-{}", secs(c.from), secs(c.to))).collect(); |
| 113 | parts.push(format!("cached {}", ranges.join(", "))); |
| 114 | } |
| 115 | Some(format!("render preview: {}", parts.join(" · "))) |
| 116 | } |