| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use crate::*; |
| 8 | use crate::ui::type_scale::{self, Typeset}; |
| 9 | use engine::project::{Lane, MulticamUnder}; |
| 10 | use std::sync::Mutex; |
| 11 | use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; |
| 12 | |
| 13 | |
| 14 | |
| 15 | pub(crate) type ShotKey = (Lane, usize, u32, usize, usize, usize); |
| 16 | |
| 17 | |
| 18 | #[derive(Default)] |
| 19 | pub(crate) struct AngleShots { |
| 20 | key: Option<ShotKey>, |
| 21 | images: Vec<Option<Arc<RenderImage>>>, |
| 22 | busy: bool, |
| 23 | } |
| 24 | |
| 25 | |
| 26 | pub(crate) struct McamRun { |
| 27 | at: f64, |
| 28 | progress: Arc<AtomicU32>, |
| 29 | cancel: Arc<AtomicBool>, |
| 30 | result: Arc<Mutex<Option<Result<engine::MulticamPlan, String>>>>, |
| 31 | } |
| 32 | |
| 33 | |
| 34 | const TILE_W: u32 = 384; |
| 35 | |
| 36 | impl Player { |
| 37 | |
| 38 | pub(crate) fn multicam_here(&self) -> Option<MulticamUnder> { |
| 39 | let session = self.session.as_ref()?; |
| 40 | session.multicam_under(frame_at(session.now(), self.fps)) |
| 41 | } |
| 42 | |
| 43 | |
| 44 | |
| 45 | pub(crate) fn picked_sources(&self) -> Vec<usize> { |
| 46 | let Some(session) = &self.session else { |
| 47 | return Vec::new(); |
| 48 | }; |
| 49 | let mut rows = self.asset_picks.clone(); |
| 50 | if rows.is_empty() { |
| 51 | rows.extend(self.selected_asset.clone()); |
| 52 | } |
| 53 | let mut out = Vec::new(); |
| 54 | for (path, stream) in rows { |
| 55 | let found = session |
| 56 | .sources() |
| 57 | .iter() |
| 58 | .position(|s| s.path == path && s.audio_stream == stream) |
| 59 | .or_else(|| session.sources().iter().position(|s| s.path == path)); |
| 60 | if let Some(i) = found |
| 61 | && !out.contains(&i) |
| 62 | { |
| 63 | out.push(i); |
| 64 | } |
| 65 | } |
| 66 | out |
| 67 | } |
| 68 | |
| 69 | |
| 70 | pub(crate) fn toggle_asset_pick(&mut self, path: PathBuf, stream: usize) { |
| 71 | let row = (path, stream); |
| 72 | toggle_pick(&mut self.asset_picks, self.selected_asset.clone(), row.clone()); |
| 73 | self.selected_asset = Some(row); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | pub(crate) fn pick_one_asset(&mut self, path: PathBuf, stream: usize) { |
| 78 | self.asset_picks.clear(); |
| 79 | self.selected_asset = Some((path, stream)); |
| 80 | } |
| 81 | |
| 82 | pub(crate) fn toggle_angle_monitor(&mut self, cx: &mut Context<Self>) { |
| 83 | self.angle_monitor = !self.angle_monitor; |
| 84 | self.angle_shots = AngleShots::default(); |
| 85 | cx.notify(); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | pub(crate) fn create_multicam_selected(&mut self, cx: &mut Context<Self>) { |
| 93 | if self.exporting().is_some() { |
| 94 | return; |
| 95 | } |
| 96 | if self.mcam_run.is_some() { |
| 97 | self.notify_user("Angles are already being synced".into()); |
| 98 | cx.notify(); |
| 99 | return; |
| 100 | } |
| 101 | let picks = self.picked_sources(); |
| 102 | if picks.len() < 2 { |
| 103 | self.notify_user("Multicam needs two or more media clips picked".into()); |
| 104 | cx.notify(); |
| 105 | return; |
| 106 | } |
| 107 | if self.lane_locked_refusal(Lane::V1) { |
| 108 | cx.notify(); |
| 109 | return; |
| 110 | } |
| 111 | let Some(session) = self.session.as_ref() else { |
| 112 | return; |
| 113 | }; |
| 114 | let at = session.now(); |
| 115 | let job = match session.multicam_job(&picks, engine::MulticamSync::Audio) { |
| 116 | Ok(job) => job, |
| 117 | Err(e) => { |
| 118 | self.notify_user(format!("Not made — {e}").into()); |
| 119 | cx.notify(); |
| 120 | return; |
| 121 | } |
| 122 | }; |
| 123 | let run = McamRun { |
| 124 | at, |
| 125 | progress: Arc::new(AtomicU32::new(0)), |
| 126 | cancel: Arc::new(AtomicBool::new(false)), |
| 127 | result: Arc::new(Mutex::new(None)), |
| 128 | }; |
| 129 | let (progress, cancel, result) = (run.progress.clone(), run.cancel.clone(), run.result.clone()); |
| 130 | std::thread::spawn(move || { |
| 131 | let r = job.run(&progress, &cancel).map_err(|e| e.to_string()); |
| 132 | if let Ok(mut slot) = result.lock() { |
| 133 | *slot = Some(r); |
| 134 | } |
| 135 | }); |
| 136 | self.mcam_run = Some(run); |
| 137 | cx.notify(); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | pub(crate) fn mcam_words(&self) -> Option<String> { |
| 142 | let run = self.mcam_run.as_ref()?; |
| 143 | Some(format!("Syncing angles {}%", run.progress.load(Ordering::Relaxed) / 10)) |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | pub(crate) fn poll_multicam(&mut self) { |
| 149 | let Some(run) = &self.mcam_run else { return }; |
| 150 | let Some(outcome) = run.result.lock().ok().and_then(|mut r| r.take()) else { return }; |
| 151 | let run = self.mcam_run.take().expect("checked above"); |
| 152 | let plan = match outcome { |
| 153 | Ok(plan) => plan, |
| 154 | Err(e) => { |
| 155 | self.notify_user(format!("Not made — {e}").into()); |
| 156 | return; |
| 157 | } |
| 158 | }; |
| 159 | let Some(session) = self.session.as_mut() else { |
| 160 | return; |
| 161 | }; |
| 162 | match session.apply_multicam(&plan, None, Lane::V1, run.at) { |
| 163 | Ok(report) => { |
| 164 | let names = session.angle_names(report.made.sequence).unwrap_or_default(); |
| 165 | let synced = report.how.iter().filter(|h| matches!(h, engine::AngleSync::Audio { .. })).count(); |
| 166 | let loose = report |
| 167 | .how |
| 168 | .iter() |
| 169 | .filter(|h| matches!(h, engine::AngleSync::Weak { .. } | engine::AngleSync::NoSound)) |
| 170 | .count(); |
| 171 | let how = if loose > 0 { |
| 172 | format!("{synced} synced by sound, {loose} aligned at the start") |
| 173 | } else if synced > 0 { |
| 174 | format!("synced by sound ({synced} matched)") |
| 175 | } else { |
| 176 | "aligned at the start".to_string() |
| 177 | }; |
| 178 | self.selected.clear(); |
| 179 | if let Some(&first) = report.made.clips.first() { |
| 180 | self.selected.set_one(first); |
| 181 | } |
| 182 | self.angle_monitor = true; |
| 183 | self.angle_shots = AngleShots::default(); |
| 184 | self.mark_dirty(); |
| 185 | self.notify_user(format!("Multicam clip of {} angles — {how}", names.len()).into()); |
| 186 | } |
| 187 | Err(e) => self.notify_user(format!("Not made — {e}").into()), |
| 188 | } |
| 189 | self.reset_after_reseek(); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | pub(crate) fn flatten_multicam_selected(&mut self, cx: &mut Context<Self>) { |
| 194 | if self.exporting().is_some() { |
| 195 | return; |
| 196 | } |
| 197 | let mut picks = self.marks().0; |
| 198 | if picks.is_empty() { |
| 199 | picks.extend(self.selected.anchor()); |
| 200 | } |
| 201 | if let Some(under) = self.multicam_here() { |
| 202 | picks.push((under.lane, under.idx)); |
| 203 | } |
| 204 | let Some(session) = self.session.as_mut() else { |
| 205 | return; |
| 206 | }; |
| 207 | let Some(&(lane, idx)) = picks.iter().find(|&&(l, i)| session.multicam_at(l, i).is_some()) else { |
| 208 | self.notify_user("No multicam clip selected".into()); |
| 209 | cx.notify(); |
| 210 | return; |
| 211 | }; |
| 212 | match session.flatten_multicam(lane, idx) { |
| 213 | Ok(_) => { |
| 214 | self.selected.clear(); |
| 215 | self.angle_shots = AngleShots::default(); |
| 216 | self.mark_dirty(); |
| 217 | self.notify_user("Multicam clip flattened to plain clips".into()); |
| 218 | } |
| 219 | Err(e) => self.notify_user(format!("Not flattened — {e}").into()), |
| 220 | } |
| 221 | self.reset_after_reseek(); |
| 222 | cx.notify(); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | |
| 227 | pub(crate) fn switch_angle_here(&mut self, n: usize, cx: &mut Context<Self>) { |
| 228 | if self.exporting().is_some() { |
| 229 | return; |
| 230 | } |
| 231 | let Some(under) = self.multicam_here() else { |
| 232 | self.notify_user("No multicam clip under the playhead".into()); |
| 233 | cx.notify(); |
| 234 | return; |
| 235 | }; |
| 236 | if self.lane_locked_refusal(under.lane) { |
| 237 | cx.notify(); |
| 238 | return; |
| 239 | } |
| 240 | if n == 0 || n > under.names.len() { |
| 241 | self.notify_user(format!("This multicam clip has {} angles", under.names.len()).into()); |
| 242 | cx.notify(); |
| 243 | return; |
| 244 | } |
| 245 | if under.shown == n - 1 { |
| 246 | return; |
| 247 | } |
| 248 | let Some(session) = self.session.as_mut() else { |
| 249 | return; |
| 250 | }; |
| 251 | let at = session.now(); |
| 252 | match session.switch_angle_at(None, at, Some((n - 1) as u8), None) { |
| 253 | Ok(_) => { |
| 254 | self.mark_dirty(); |
| 255 | self.angle_shots = AngleShots::default(); |
| 256 | self.notify_user(format!("Angle {n} — {}", under.names[n - 1]).into()); |
| 257 | } |
| 258 | Err(e) => self.notify_user(format!("Not switched — {e}").into()), |
| 259 | } |
| 260 | self.reset_after_reseek(); |
| 261 | cx.notify(); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | |
| 266 | fn refresh_angle_shots(&mut self, under: &MulticamUnder, cx: &mut Context<Self>) { |
| 267 | let Some(session) = &self.session else { |
| 268 | return; |
| 269 | }; |
| 270 | let frame = frame_at(session.now(), self.fps); |
| 271 | let key = (under.lane, under.idx, frame, session.undo_depth(), session.redo_depth(), under.shown); |
| 272 | if self.angle_shots.busy || Some(key) == self.angle_shots.key { |
| 273 | return; |
| 274 | } |
| 275 | let Some((_, views, meta)) = session.angle_views(frame) else { |
| 276 | return; |
| 277 | }; |
| 278 | self.angle_shots.busy = true; |
| 279 | let work = cx.background_executor().spawn(async move { |
| 280 | let w = TILE_W; |
| 281 | let h = (u64::from(w) * u64::from(meta.height) / u64::from(meta.width.max(1))).max(2) as u32; |
| 282 | views |
| 283 | .iter() |
| 284 | .map(|p| { |
| 285 | let f = engine::export::compose_at(p, &meta, frame, w, h).ok()?; |
| 286 | let buf = image::RgbaImage::from_raw(f.width, f.height, f.bgra)?; |
| 287 | Some(Arc::new(RenderImage::new(vec![image::Frame::new(buf)]))) |
| 288 | }) |
| 289 | .collect::<Vec<_>>() |
| 290 | }); |
| 291 | cx.spawn(async move |this, cx| { |
| 292 | let images = work.await; |
| 293 | this.update(cx, |this, cx| { |
| 294 | this.angle_shots = AngleShots { key: Some(key), images, busy: false }; |
| 295 | cx.notify(); |
| 296 | }) |
| 297 | .ok(); |
| 298 | }) |
| 299 | .detach(); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | pub(crate) fn angle_monitor_view(&mut self, cx: &mut Context<Self>, max_cols: usize) -> Option<AnyElement> { |
| 307 | let Some(under) = self.multicam_here() else { |
| 308 | |
| 309 | |
| 310 | use crate::ui::icons::{Icon, IconSize}; |
| 311 | let t = crate::ui::theme::tokens(); |
| 312 | return Some( |
| 313 | div() |
| 314 | .id("angle-monitor") |
| 315 | .flex_1() |
| 316 | .min_w(px(0.)) |
| 317 | .min_h(px(0.)) |
| 318 | .flex() |
| 319 | .flex_col() |
| 320 | .items_center() |
| 321 | .justify_center() |
| 322 | .gap(px(8.)) |
| 323 | .px(px(16.)) |
| 324 | .child(Icon::Layers.el(IconSize::Hero, t.ink3)) |
| 325 | .child( |
| 326 | div() |
| 327 | .type_style(type_scale::title()) |
| 328 | .text_color(rgb(t.ink2)) |
| 329 | .child("No multicam clip under the playhead"), |
| 330 | ) |
| 331 | .child( |
| 332 | div() |
| 333 | .type_style(type_scale::body()) |
| 334 | .text_color(rgb(t.ink3)) |
| 335 | .child("To make one, pick two or more clips in Media (Ctrl-click), then right-click one and choose Create multicam."), |
| 336 | ) |
| 337 | .into_any_element(), |
| 338 | ); |
| 339 | }; |
| 340 | self.refresh_angle_shots(&under, cx); |
| 341 | let shell = div().id("angle-monitor").flex_1().min_w(px(0.)).min_h(px(0.)).flex().flex_col().gap(px(6.)); |
| 342 | let n = under.names.len(); |
| 343 | let rows = monitor_rows_within(n, max_cols); |
| 344 | let mut grid = shell; |
| 345 | let mut first = 0; |
| 346 | for tiles in rows { |
| 347 | let mut row = div().flex_1().min_h(px(0.)).flex().gap(px(6.)); |
| 348 | for i in first..first + tiles { |
| 349 | let active = i == under.shown; |
| 350 | let image = self.angle_shots.images.get(i).cloned().flatten(); |
| 351 | let label = format!("{} · {}", i + 1, under.names[i]); |
| 352 | row = row.child( |
| 353 | div() |
| 354 | .id(SharedString::from(format!("angle-tile-{}", i + 1))) |
| 355 | .flex_1() |
| 356 | .min_w(px(0.)) |
| 357 | .min_h(px(0.)) |
| 358 | .flex() |
| 359 | .flex_col() |
| 360 | .bg(rgb(DARK_PANEL())) |
| 361 | .border_2() |
| 362 | |
| 363 | |
| 364 | .border_color(if active { rgb(ACCENT_PRIMARY()) } else { rgba(DARK_SEAM()) }) |
| 365 | .cursor_pointer() |
| 366 | .hover(|s| s.bg(rgb(DARK_RAISED()))) |
| 367 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| this.switch_angle_here(i + 1, cx))) |
| 368 | .child( |
| 369 | div() |
| 370 | .flex_1() |
| 371 | .min_h(px(0.)) |
| 372 | .relative() |
| 373 | .bg(rgb(DARK_CANVAS())) |
| 374 | .children(image.map(letterboxed_image)), |
| 375 | ) |
| 376 | .child( |
| 377 | div() |
| 378 | .flex_none() |
| 379 | .px(px(6.)) |
| 380 | .py(px(2.)) |
| 381 | .when(active, |d| d.bg(rgba(ACCENT_WASH()))) |
| 382 | .text_size(px(type_scale::FLOOR_PX)) |
| 383 | .text_color(rgb(if active { INK1() } else { INK2() })) |
| 384 | .overflow_hidden() |
| 385 | .whitespace_nowrap() |
| 386 | .child(label), |
| 387 | ), |
| 388 | ); |
| 389 | } |
| 390 | first += tiles; |
| 391 | grid = grid.child(row); |
| 392 | } |
| 393 | Some(grid.into_any_element()) |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | |
| 398 | |
| 399 | pub(crate) fn toggle_pick(picks: &mut Vec<(PathBuf, usize)>, selected: Option<(PathBuf, usize)>, row: (PathBuf, usize)) { |
| 400 | if picks.is_empty() { |
| 401 | picks.extend(selected); |
| 402 | } |
| 403 | match picks.iter().position(|r| *r == row) { |
| 404 | Some(at) => { |
| 405 | picks.remove(at); |
| 406 | } |
| 407 | None => picks.push(row), |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | #[cfg(test)] |
| 415 | pub(crate) fn monitor_rows(n: usize) -> Vec<usize> { |
| 416 | monitor_rows_within(n, 3) |
| 417 | } |
| 418 | |
| 419 | |
| 420 | |
| 421 | pub(crate) fn monitor_rows_within(n: usize, max_cols: usize) -> Vec<usize> { |
| 422 | let cols = match n { |
| 423 | 0..=2 => 1, |
| 424 | 3..=4 => 2, |
| 425 | _ => 3, |
| 426 | } |
| 427 | .min(max_cols.max(1)); |
| 428 | let mut rows = vec![cols; n / cols]; |
| 429 | if !n.is_multiple_of(cols) { |
| 430 | rows.push(n % cols); |
| 431 | } |
| 432 | rows |
| 433 | } |