| ... | ... | @@ -21,8 +21,8 @@ use crate::text_engine; |
| 21 | 21 | use model::{Node, default_main_input_port}; |
| 22 | 22 | use model::{ClipSpec, ClsEntry, Comp, NodeId, PolyPoint}; |
| 23 | 23 | #[cfg(not(target_arch = "wasm32"))] |
| 24 | | use std::collections::VecDeque; |
| 25 | | use std::collections::{HashMap, HashSet}; |
| 24 | use std::collections::{HashSet, VecDeque}; |
| 25 | use std::collections::HashMap; |
| 26 | 26 | use std::path::{Path, PathBuf}; |
| 27 | 27 | use std::process::Command; |
| 28 | 28 | #[cfg(not(target_arch = "wasm32"))] |
| ... | ... | @@ -195,7 +195,7 @@ struct ImageCacheKey { |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | struct VideoDecoderSlot { |
| 198 | | info: Option<VideoInfo>, |
| 198 | info: VideoInfo, |
| 199 | 199 | decoder: Arc<Mutex<Option<Box<dyn VideoDecoder>>>>, |
| 200 | 200 | last_used: u64, |
| 201 | 201 | } |
| ... | ... | @@ -212,6 +212,7 @@ struct MediaState { |
| 212 | 212 | in_flight: HashSet<ImageCacheKey>, |
| 213 | 213 | #[cfg(not(target_arch = "wasm32"))] |
| 214 | 214 | opening_videos: HashSet<PathBuf>, |
| 215 | #[cfg(not(target_arch = "wasm32"))] |
| 215 | 216 | known_existing_paths: HashSet<PathBuf>, |
| 216 | 217 | } |
| 217 | 218 | |
| ... | ... | @@ -229,10 +230,30 @@ impl MediaState { |
| 229 | 230 | in_flight: HashSet::new(), |
| 230 | 231 | #[cfg(not(target_arch = "wasm32"))] |
| 231 | 232 | opening_videos: HashSet::new(), |
| 233 | #[cfg(not(target_arch = "wasm32"))] |
| 232 | 234 | known_existing_paths: HashSet::new(), |
| 233 | 235 | } |
| 234 | 236 | } |
| 235 | 237 | |
| 238 | fn record_existing_path(&mut self, path: &Path) { |
| 239 | #[cfg(not(target_arch = "wasm32"))] |
| 240 | { |
| 241 | if self.known_existing_paths.len() > 8192 { |
| 242 | self.known_existing_paths.clear(); |
| 243 | } |
| 244 | self.known_existing_paths.insert(path.to_path_buf()); |
| 245 | } |
| 246 | #[cfg(target_arch = "wasm32")] |
| 247 | let _ = path; |
| 248 | } |
| 249 | |
| 250 | fn evict_existing_path(&mut self, path: &Path) { |
| 251 | #[cfg(not(target_arch = "wasm32"))] |
| 252 | self.known_existing_paths.remove(path); |
| 253 | #[cfg(target_arch = "wasm32")] |
| 254 | let _ = path; |
| 255 | } |
| 256 | |
| 236 | 257 | fn insert_cached(&mut self, cache_key: ImageCacheKey, decoded: DecodedImage) { |
| 237 | 258 | let bytes = decoded.byte_size(); |
| 238 | 259 | self.tick += 1; |
| ... | ... | @@ -256,16 +277,20 @@ impl MediaState { |
| 256 | 277 | fn put_video_decoder( |
| 257 | 278 | &mut self, |
| 258 | 279 | path: PathBuf, |
| 259 | | info: Option<VideoInfo>, |
| 280 | info: VideoInfo, |
| 260 | 281 | decoder: Arc<Mutex<Option<Box<dyn VideoDecoder>>>>, |
| 261 | | ) { |
| 282 | ) -> Vec<VideoDecoderSlot> { |
| 283 | let mut evicted = Vec::new(); |
| 262 | 284 | self.tick += 1; |
| 263 | 285 | let now_tick = self.tick; |
| 286 | let new_slot = VideoDecoderSlot { |
| 287 | info, |
| 288 | decoder, |
| 289 | last_used: now_tick, |
| 290 | }; |
| 264 | 291 | if let Some(slot) = self.video_decoders.get_mut(&path) { |
| 265 | | slot.info = info; |
| 266 | | slot.decoder = decoder; |
| 267 | | slot.last_used = now_tick; |
| 268 | | return; |
| 292 | evicted.push(std::mem::replace(slot, new_slot)); |
| 293 | return evicted; |
| 269 | 294 | } |
| 270 | 295 | while self.video_decoders.len() >= MAX_VIDEO_DECODERS { |
| 271 | 296 | let Some(oldest_path) = self |
| ... | ... | @@ -276,16 +301,12 @@ impl MediaState { |
| 276 | 301 | else { |
| 277 | 302 | break; |
| 278 | 303 | }; |
| 279 | | self.video_decoders.remove(&oldest_path); |
| 280 | | } |
| 281 | | self.video_decoders.insert( |
| 282 | | path, |
| 283 | | VideoDecoderSlot { |
| 284 | | info, |
| 285 | | decoder, |
| 286 | | last_used: now_tick, |
| 287 | | }, |
| 288 | | ); |
| 304 | if let Some(removed) = self.video_decoders.remove(&oldest_path) { |
| 305 | evicted.push(removed); |
| 306 | } |
| 307 | } |
| 308 | self.video_decoders.insert(path, new_slot); |
| 309 | evicted |
| 289 | 310 | } |
| 290 | 311 | } |
| 291 | 312 | |
| ... | ... | @@ -308,18 +329,25 @@ impl MediaShared { |
| 308 | 329 | if virtual_bytes(path).is_some() { |
| 309 | 330 | return true; |
| 310 | 331 | } |
| 311 | | if let Ok(state) = self.state.lock() { |
| 312 | | if state.known_existing_paths.contains(path) || state.video_decoders.contains_key(path) { |
| 313 | | return true; |
| 332 | #[cfg(not(target_arch = "wasm32"))] |
| 333 | { |
| 334 | if let Ok(state) = self.state.lock() { |
| 335 | if state.known_existing_paths.contains(path) || state.video_decoders.contains_key(path) { |
| 336 | return true; |
| 337 | } |
| 314 | 338 | } |
| 315 | | } |
| 316 | | if path.exists() { |
| 317 | | if let Ok(mut state) = self.state.lock() { |
| 318 | | state.known_existing_paths.insert(path.to_path_buf()); |
| 339 | if path.exists() { |
| 340 | if let Ok(mut state) = self.state.lock() { |
| 341 | state.record_existing_path(path); |
| 342 | } |
| 343 | true |
| 344 | } else { |
| 345 | false |
| 319 | 346 | } |
| 320 | | true |
| 321 | | } else { |
| 322 | | false |
| 347 | } |
| 348 | #[cfg(target_arch = "wasm32")] |
| 349 | { |
| 350 | path.exists() |
| 323 | 351 | } |
| 324 | 352 | } |
| 325 | 353 | |
| ... | ... | @@ -341,7 +369,7 @@ impl MediaShared { |
| 341 | 369 | let now_tick = guard.tick; |
| 342 | 370 | if let Some(slot) = guard.video_decoders.get_mut(path) { |
| 343 | 371 | slot.last_used = now_tick; |
| 344 | | return slot.info.map(|info| (info, Arc::clone(&slot.decoder))); |
| 372 | return Some((slot.info, Arc::clone(&slot.decoder))); |
| 345 | 373 | } |
| 346 | 374 | |
| 347 | 375 | #[cfg(not(target_arch = "wasm32"))] |
| ... | ... | @@ -353,37 +381,51 @@ impl MediaShared { |
| 353 | 381 | |
| 354 | 382 | struct OpenGuard<'a> { |
| 355 | 383 | shared: &'a MediaShared, |
| 356 | | path: PathBuf, |
| 384 | path: Option<PathBuf>, |
| 357 | 385 | } |
| 358 | 386 | impl Drop for OpenGuard<'_> { |
| 359 | 387 | fn drop(&mut self) { |
| 360 | 388 | #[cfg(not(target_arch = "wasm32"))] |
| 361 | | if let Ok(mut state) = self.shared.state.lock() { |
| 362 | | state.opening_videos.remove(&self.path); |
| 363 | | self.shared.cv.notify_all(); |
| 389 | if let Some(path) = self.path.take() { |
| 390 | if let Ok(mut state) = self.shared.state.lock() { |
| 391 | state.opening_videos.remove(&path); |
| 392 | self.shared.cv.notify_all(); |
| 393 | } |
| 364 | 394 | } |
| 365 | 395 | #[cfg(target_arch = "wasm32")] |
| 366 | 396 | let _ = (&self.shared, &self.path); |
| 367 | 397 | } |
| 368 | 398 | } |
| 369 | | let _open_guard = OpenGuard { |
| 399 | let mut open_guard = OpenGuard { |
| 370 | 400 | shared: self, |
| 371 | | path: path.to_path_buf(), |
| 401 | path: Some(path.to_path_buf()), |
| 372 | 402 | }; |
| 373 | 403 | |
| 374 | 404 | let decoder = open_video_decoder(path); |
| 375 | 405 | let info = decoder.as_ref().map(|d| d.info()); |
| 376 | 406 | let slot_arc = Arc::new(Mutex::new(decoder)); |
| 377 | 407 | |
| 408 | let mut evicted_decoders = Vec::new(); |
| 378 | 409 | if let Ok(mut state) = self.state.lock() { |
| 379 | 410 | #[cfg(not(target_arch = "wasm32"))] |
| 380 | 411 | state.opening_videos.remove(path); |
| 381 | | if state.generation == cache_gen { |
| 382 | | state.put_video_decoder(path.to_path_buf(), info, Arc::clone(&slot_arc)); |
| 412 | open_guard.path = None; |
| 413 | if let Some(dec_info) = info { |
| 414 | if state.generation == cache_gen { |
| 415 | state.record_existing_path(path); |
| 416 | evicted_decoders = state.put_video_decoder( |
| 417 | path.to_path_buf(), |
| 418 | dec_info, |
| 419 | Arc::clone(&slot_arc), |
| 420 | ); |
| 421 | } |
| 422 | } else { |
| 423 | state.evict_existing_path(path); |
| 383 | 424 | } |
| 384 | 425 | #[cfg(not(target_arch = "wasm32"))] |
| 385 | 426 | self.cv.notify_all(); |
| 386 | 427 | } |
| 428 | drop(evicted_decoders); |
| 387 | 429 | info.map(|i| (i, slot_arc)) |
| 388 | 430 | } |
| 389 | 431 | } |
| ... | ... | @@ -550,6 +592,10 @@ impl UploadStagingPool { |
| 550 | 592 | rx: None, |
| 551 | 593 | }); |
| 552 | 594 | } |
| 595 | |
| 596 | pub(crate) fn release_failed_upload(&mut self, capacity: u64) { |
| 597 | self.total_bytes = self.total_bytes.saturating_sub(capacity); |
| 598 | } |
| 553 | 599 | } |
| 554 | 600 | |
| 555 | 601 | pub struct MediaCache { |
| ... | ... | @@ -591,14 +637,17 @@ impl MediaCache { |
| 591 | 637 | } |
| 592 | 638 | |
| 593 | 639 | pub fn clear(&mut self) { |
| 640 | let mut cleared_decoders = Vec::new(); |
| 594 | 641 | if let Ok(mut state) = self.shared.state.lock() { |
| 595 | 642 | state.images.clear(); |
| 596 | 643 | state.lru.clear(); |
| 597 | | state.video_decoders.clear(); |
| 644 | cleared_decoders.extend(state.video_decoders.drain().map(|(_, slot)| slot)); |
| 598 | 645 | state.total_bytes = 0; |
| 599 | 646 | state.generation = state.generation.wrapping_add(1); |
| 647 | #[cfg(not(target_arch = "wasm32"))] |
| 600 | 648 | state.known_existing_paths.clear(); |
| 601 | 649 | } |
| 650 | drop(cleared_decoders); |
| 602 | 651 | #[cfg(not(target_arch = "wasm32"))] |
| 603 | 652 | if let Ok(mut pool) = self.staging_pool.lock() { |
| 604 | 653 | pool.clear(); |
| ... | ... | @@ -609,16 +658,22 @@ impl MediaCache { |
| 609 | 658 | pub(crate) fn acquire_upload_staging(&self, device: &wgpu::Device, needed: u64) -> Option<wgpu::Buffer> { |
| 610 | 659 | self.staging_pool |
| 611 | 660 | .lock() |
| 612 | | .expect("staging pool lock") |
| 661 | .ok()? |
| 613 | 662 | .acquire(device, needed) |
| 614 | 663 | } |
| 615 | 664 | |
| 616 | 665 | #[cfg(not(target_arch = "wasm32"))] |
| 617 | 666 | pub(crate) fn recycle_upload_staging(&self, buffer: wgpu::Buffer) { |
| 618 | | self.staging_pool |
| 619 | | .lock() |
| 620 | | .expect("staging pool lock") |
| 621 | | .finish_upload(buffer); |
| 667 | if let Ok(mut pool) = self.staging_pool.lock() { |
| 668 | pool.finish_upload(buffer); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | #[cfg(not(target_arch = "wasm32"))] |
| 673 | pub(crate) fn release_failed_upload_staging(&self, capacity: u64) { |
| 674 | if let Ok(mut pool) = self.staging_pool.lock() { |
| 675 | pool.release_failed_upload(capacity); |
| 676 | } |
| 622 | 677 | } |
| 623 | 678 | |
| 624 | 679 | /// Marks submitted upload staging buffers ready to begin asynchronous write-mapping |
| ... | ... | @@ -800,9 +855,15 @@ impl MediaCache { |
| 800 | 855 | exr_channels[2], |
| 801 | 856 | exr_channels[3], |
| 802 | 857 | false, |
| 803 | | )? |
| 858 | ) |
| 804 | 859 | } else { |
| 805 | | decode_raster_image(&resolved, post_multiply, true)? |
| 860 | decode_raster_image(&resolved, post_multiply, true) |
| 861 | }; |
| 862 | let Some(decoded) = decoded else { |
| 863 | if let Ok(mut state) = self.shared.state.lock() { |
| 864 | state.evict_existing_path(&resolved); |
| 865 | } |
| 866 | return None; |
| 806 | 867 | }; |
| 807 | 868 | |
| 808 | 869 | if let Ok(mut state) = self.shared.state.lock() { |
| ... | ... | @@ -812,7 +873,7 @@ impl MediaCache { |
| 812 | 873 | in_flight_guard.key = None; |
| 813 | 874 | } |
| 814 | 875 | if state.generation == cache_gen { |
| 815 | | state.known_existing_paths.insert(resolved.clone()); |
| 876 | state.record_existing_path(&resolved); |
| 816 | 877 | state.insert_cached(cache_key, decoded.clone()); |
| 817 | 878 | } |
| 818 | 879 | #[cfg(not(target_arch = "wasm32"))] |
| ... | ... | @@ -848,9 +909,6 @@ impl MediaCache { |
| 848 | 909 | } |
| 849 | 910 | #[cfg(not(target_arch = "wasm32"))] |
| 850 | 911 | { |
| 851 | | if let Ok(mut pool) = self.staging_pool.lock() { |
| 852 | | pool.mark_frame_start(); |
| 853 | | } |
| 854 | 912 | let mut visited = HashSet::new(); |
| 855 | 913 | let mut jobs = MissingJobs::default(); |
| 856 | 914 | collect_missing_loader_jobs_shared( |
| ... | ... | @@ -933,17 +991,8 @@ impl MediaCache { |
| 933 | 991 | shared: &self.shared, |
| 934 | 992 | key: Some(job.cache_key.clone()), |
| 935 | 993 | }; |
| 936 | | if let Some(decoded) = decode_job(&job, true) { |
| 937 | | if let Ok(mut state) = self.shared.state.lock() { |
| 938 | | state.in_flight.remove(&job.cache_key); |
| 939 | | guard.key = None; |
| 940 | | if state.generation == cache_gen { |
| 941 | | state.known_existing_paths.insert(job.cache_key.path.clone()); |
| 942 | | state.insert_cached(job.cache_key, decoded); |
| 943 | | } |
| 944 | | self.shared.cv.notify_all(); |
| 945 | | } |
| 946 | | } |
| 994 | let decoded = decode_job(&job, true); |
| 995 | finish_raster_job(&self.shared, job.cache_key, cache_gen, &mut guard, decoded); |
| 947 | 996 | return; |
| 948 | 997 | } |
| 949 | 998 | self.decode_jobs_parallel(jobs); |
| ... | ... | @@ -1065,17 +1114,14 @@ impl MediaCache { |
| 1065 | 1114 | shared: &self.shared, |
| 1066 | 1115 | key: Some(job.cache_key.clone()), |
| 1067 | 1116 | }; |
| 1068 | | if let Some(decoded) = decode_job(job, false) { |
| 1069 | | if let Ok(mut state) = self.shared.state.lock() { |
| 1070 | | state.in_flight.remove(&job.cache_key); |
| 1071 | | guard.key = None; |
| 1072 | | if state.generation == cache_gen { |
| 1073 | | state.known_existing_paths.insert(job.cache_key.path.clone()); |
| 1074 | | state.insert_cached(job.cache_key.clone(), decoded); |
| 1075 | | } |
| 1076 | | self.shared.cv.notify_all(); |
| 1077 | | } |
| 1078 | | } |
| 1117 | let decoded = decode_job(job, false); |
| 1118 | finish_raster_job( |
| 1119 | &self.shared, |
| 1120 | job.cache_key.clone(), |
| 1121 | cache_gen, |
| 1122 | &mut guard, |
| 1123 | decoded, |
| 1124 | ); |
| 1079 | 1125 | } |
| 1080 | 1126 | }); |
| 1081 | 1127 | } |
| ... | ... | @@ -1083,6 +1129,29 @@ impl MediaCache { |
| 1083 | 1129 | } |
| 1084 | 1130 | } |
| 1085 | 1131 | |
| 1132 | #[cfg(not(target_arch = "wasm32"))] |
| 1133 | fn finish_raster_job( |
| 1134 | shared: &MediaShared, |
| 1135 | key: ImageCacheKey, |
| 1136 | cache_gen: u64, |
| 1137 | guard: &mut InFlightGuard<'_>, |
| 1138 | decoded: Option<DecodedImage>, |
| 1139 | ) { |
| 1140 | if let Ok(mut state) = shared.state.lock() { |
| 1141 | state.in_flight.remove(&key); |
| 1142 | guard.key = None; |
| 1143 | if let Some(decoded) = decoded { |
| 1144 | if state.generation == cache_gen { |
| 1145 | state.record_existing_path(&key.path); |
| 1146 | state.insert_cached(key, decoded); |
| 1147 | } |
| 1148 | } else { |
| 1149 | state.evict_existing_path(&key.path); |
| 1150 | } |
| 1151 | shared.cv.notify_all(); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1086 | 1155 | #[cfg(not(target_arch = "wasm32"))] |
| 1087 | 1156 | struct PrefetchQueueState { |
| 1088 | 1157 | raster: VecDeque<(usize, PreloadJob)>, |
| ... | ... | @@ -1094,6 +1163,28 @@ struct PrefetchQueueState { |
| 1094 | 1163 | cancelled: bool, |
| 1095 | 1164 | } |
| 1096 | 1165 | |
| 1166 | #[cfg(not(target_arch = "wasm32"))] |
| 1167 | impl PrefetchQueueState { |
| 1168 | fn prune_stale(&mut self) { |
| 1169 | let cur = self.current_seq; |
| 1170 | if self.raster.iter().any(|&(seq, _)| seq < cur) { |
| 1171 | let mut kept = VecDeque::with_capacity(self.raster.len()); |
| 1172 | while let Some((seq, job)) = self.raster.pop_front() { |
| 1173 | if seq >= cur { |
| 1174 | kept.push_back((seq, job)); |
| 1175 | } else { |
| 1176 | self.queued_raster_keys.remove(&job.cache_key); |
| 1177 | } |
| 1178 | } |
| 1179 | self.raster = kept; |
| 1180 | } |
| 1181 | self.videos.retain(|_, vq| { |
| 1182 | vq.retain(|&(seq, _)| seq >= cur); |
| 1183 | !vq.is_empty() |
| 1184 | }); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1097 | 1188 | #[cfg(not(target_arch = "wasm32"))] |
| 1098 | 1189 | struct PrefetchQueue { |
| 1099 | 1190 | state: Mutex<PrefetchQueueState>, |
| ... | ... | @@ -1154,6 +1245,7 @@ impl MediaPrefetchHandle { |
| 1154 | 1245 | pub fn set_current_seq(&self, seq_idx: usize) { |
| 1155 | 1246 | if let Ok(mut q) = self.queue.state.lock() { |
| 1156 | 1247 | q.current_seq = seq_idx; |
| 1248 | q.prune_stale(); |
| 1157 | 1249 | self.queue.cv.notify_all(); |
| 1158 | 1250 | } |
| 1159 | 1251 | } |
| ... | ... | @@ -1184,7 +1276,7 @@ impl MediaPrefetchHandle { |
| 1184 | 1276 | return; |
| 1185 | 1277 | } |
| 1186 | 1278 | if let Ok(mut q) = self.queue.state.lock() { |
| 1187 | | if q.cancelled { |
| 1279 | if q.cancelled || seq_idx < q.current_seq { |
| 1188 | 1280 | return; |
| 1189 | 1281 | } |
| 1190 | 1282 | for job in jobs.raster { |
| ... | ... | @@ -1195,7 +1287,24 @@ impl MediaPrefetchHandle { |
| 1195 | 1287 | for (path, reqs) in jobs.videos { |
| 1196 | 1288 | let vq = q.videos.entry(path).or_default(); |
| 1197 | 1289 | for req in reqs { |
| 1198 | | vq.push_back((seq_idx, req)); |
| 1290 | if let Some((existing_seq, _)) = vq.iter_mut().find(|(_, existing)| { |
| 1291 | existing.post_multiply == req.post_multiply |
| 1292 | && match (existing.resolved_frame_idx, req.resolved_frame_idx) { |
| 1293 | (Some(a), Some(b)) => a == b, |
| 1294 | _ => { |
| 1295 | existing.frame_i == req.frame_i |
| 1296 | && existing.clip.global_start == req.clip.global_start |
| 1297 | && existing.clip.trim_in == req.clip.trim_in |
| 1298 | && existing.clip.trim_out == req.clip.trim_out |
| 1299 | && existing.clip.reverse == req.clip.reverse |
| 1300 | && existing.clip.loop_clip == req.clip.loop_clip |
| 1301 | } |
| 1302 | } |
| 1303 | }) { |
| 1304 | *existing_seq = (*existing_seq).min(seq_idx); |
| 1305 | } else { |
| 1306 | vq.push_back((seq_idx, req)); |
| 1307 | } |
| 1199 | 1308 | } |
| 1200 | 1309 | } |
| 1201 | 1310 | self.queue.cv.notify_all(); |
| ... | ... | @@ -1224,23 +1333,31 @@ impl MediaPrefetchHandle { |
| 1224 | 1333 | if q.cancelled { |
| 1225 | 1334 | return; |
| 1226 | 1335 | } |
| 1227 | | let raster_seq = q.raster.front().map(|(seq, _)| *seq); |
| 1228 | | let mut best_video: Option<(PathBuf, usize)> = None; |
| 1336 | q.prune_stale(); |
| 1337 | let best_raster = q |
| 1338 | .raster |
| 1339 | .iter() |
| 1340 | .enumerate() |
| 1341 | .min_by_key(|&(_, (seq, _))| *seq) |
| 1342 | .map(|(idx, (seq, _))| (idx, *seq)); |
| 1343 | let mut best_video: Option<(PathBuf, usize, usize)> = None; |
| 1229 | 1344 | for (path, vq) in &q.videos { |
| 1230 | 1345 | if q.active_videos.contains(path) { |
| 1231 | 1346 | continue; |
| 1232 | 1347 | } |
| 1233 | | if let Some(&(seq, _)) = vq.front() { |
| 1234 | | if best_video.as_ref().is_none_or(|&(_, bseq)| seq < bseq) { |
| 1235 | | best_video = Some((path.clone(), seq)); |
| 1348 | if let Some((v_idx, &(seq, _))) = |
| 1349 | vq.iter().enumerate().min_by_key(|&(_, (seq, _))| *seq) |
| 1350 | { |
| 1351 | if best_video.as_ref().is_none_or(|&(_, _, bseq)| seq < bseq) { |
| 1352 | best_video = Some((path.clone(), v_idx, seq)); |
| 1236 | 1353 | } |
| 1237 | 1354 | } |
| 1238 | 1355 | } |
| 1239 | 1356 | |
| 1240 | | let chosen_seq = match (raster_seq, &best_video) { |
| 1241 | | (Some(rs), Some((_, vs))) => rs.min(*vs), |
| 1242 | | (Some(rs), None) => rs, |
| 1243 | | (None, Some((_, vs))) => *vs, |
| 1357 | let chosen_seq = match (best_raster, &best_video) { |
| 1358 | (Some((_, rs)), Some((_, _, vs))) => rs.min(*vs), |
| 1359 | (Some((_, rs)), None) => rs, |
| 1360 | (None, Some((_, _, vs))) => *vs, |
| 1244 | 1361 | (None, None) => { |
| 1245 | 1362 | q = match queue.cv.wait(q) { |
| 1246 | 1363 | Ok(g) => g, |
| ... | ... | @@ -1271,9 +1388,9 @@ impl MediaPrefetchHandle { |
| 1271 | 1388 | } |
| 1272 | 1389 | } |
| 1273 | 1390 | |
| 1274 | | if let Some((path, vs)) = best_video { |
| 1275 | | if raster_seq.is_none_or(|rs| vs <= rs) { |
| 1276 | | if let Some((_, req)) = q.videos.get_mut(&path).and_then(|vq| vq.pop_front()) { |
| 1391 | if let Some((path, v_idx, vs)) = best_video { |
| 1392 | if best_raster.is_none_or(|(_, rs)| vs <= rs) { |
| 1393 | if let Some((_, req)) = q.videos.get_mut(&path).and_then(|vq| vq.remove(v_idx)) { |
| 1277 | 1394 | if q.videos.get(&path).is_some_and(VecDeque::is_empty) { |
| 1278 | 1395 | q.videos.remove(&path); |
| 1279 | 1396 | } |
| ... | ... | @@ -1282,9 +1399,11 @@ impl MediaPrefetchHandle { |
| 1282 | 1399 | } |
| 1283 | 1400 | } |
| 1284 | 1401 | } |
| 1285 | | if let Some((_, job)) = q.raster.pop_front() { |
| 1286 | | q.queued_raster_keys.remove(&job.cache_key); |
| 1287 | | break PrefetchWorkItem::Raster(job); |
| 1402 | if let Some((r_idx, _)) = best_raster { |
| 1403 | if let Some((_, job)) = q.raster.remove(r_idx) { |
| 1404 | q.queued_raster_keys.remove(&job.cache_key); |
| 1405 | break PrefetchWorkItem::Raster(job); |
| 1406 | } |
| 1288 | 1407 | } |
| 1289 | 1408 | } |
| 1290 | 1409 | }; |
| ... | ... | @@ -1306,17 +1425,8 @@ impl MediaPrefetchHandle { |
| 1306 | 1425 | shared: &shared, |
| 1307 | 1426 | key: Some(job.cache_key.clone()), |
| 1308 | 1427 | }; |
| 1309 | | if let Some(decoded) = decode_job(&job, false) { |
| 1310 | | if let Ok(mut state) = shared.state.lock() { |
| 1311 | | state.in_flight.remove(&job.cache_key); |
| 1312 | | guard.key = None; |
| 1313 | | if state.generation == cache_gen { |
| 1314 | | state.known_existing_paths.insert(job.cache_key.path.clone()); |
| 1315 | | state.insert_cached(job.cache_key, decoded); |
| 1316 | | } |
| 1317 | | shared.cv.notify_all(); |
| 1318 | | } |
| 1319 | | } |
| 1428 | let decoded = decode_job(&job, false); |
| 1429 | finish_raster_job(&shared, job.cache_key, cache_gen, &mut guard, decoded); |
| 1320 | 1430 | } |
| 1321 | 1431 | PrefetchWorkItem::Video(path, req) => { |
| 1322 | 1432 | struct ActiveVideoGuard<'a> { |
| ... | ... | @@ -1719,14 +1829,18 @@ fn collect_missing_loader_jobs_shared( |
| 1719 | 1829 | if video_frame_in_range(&effective_clip, frame_i) |
| 1720 | 1830 | && shared.path_exists_cached(base) |
| 1721 | 1831 | { |
| 1832 | let mut resolved_frame_idx = effective_clip |
| 1833 | .length |
| 1834 | .map(|_| compute_video_frame_idx(&effective_clip, frame_i, 1)); |
| 1722 | 1835 | let need_decode = shared.state.lock().is_ok_and(|state| { |
| 1723 | 1836 | match state.video_decoders.get(base) { |
| 1724 | | Some(slot) => slot.info.is_some_and(|info| { |
| 1837 | Some(slot) => { |
| 1725 | 1838 | let idx = compute_video_frame_idx( |
| 1726 | 1839 | &effective_clip, |
| 1727 | 1840 | frame_i, |
| 1728 | | info.nb_frames, |
| 1841 | slot.info.nb_frames, |
| 1729 | 1842 | ); |
| 1843 | resolved_frame_idx = Some(idx); |
| 1730 | 1844 | let cache_key = ImageCacheKey { |
| 1731 | 1845 | path: base.to_path_buf(), |
| 1732 | 1846 | frame_idx: idx, |
| ... | ... | @@ -1735,7 +1849,7 @@ fn collect_missing_loader_jobs_shared( |
| 1735 | 1849 | }; |
| 1736 | 1850 | !state.images.contains_key(&cache_key) |
| 1737 | 1851 | && !state.in_flight.contains(&cache_key) |
| 1738 | | }), |
| 1852 | } |
| 1739 | 1853 | None => true, |
| 1740 | 1854 | } |
| 1741 | 1855 | }); |
| ... | ... | @@ -1747,6 +1861,7 @@ fn collect_missing_loader_jobs_shared( |
| 1747 | 1861 | clip: effective_clip, |
| 1748 | 1862 | frame_i, |
| 1749 | 1863 | post_multiply: post_mult, |
| 1864 | resolved_frame_idx, |
| 1750 | 1865 | }); |
| 1751 | 1866 | } |
| 1752 | 1867 | } |
| ... | ... | @@ -1877,6 +1992,7 @@ struct VideoFrameRequest { |
| 1877 | 1992 | clip: ClipSpec, |
| 1878 | 1993 | frame_i: i64, |
| 1879 | 1994 | post_multiply: bool, |
| 1995 | resolved_frame_idx: Option<i64>, |
| 1880 | 1996 | } |
| 1881 | 1997 | |
| 1882 | 1998 | #[cfg(not(target_arch = "wasm32"))] |
| ... | ... | @@ -2910,4 +3026,114 @@ mod tests { |
| 2910 | 3026 | |
| 2911 | 3027 | let _ = std::fs::remove_dir_all(&dir); |
| 2912 | 3028 | } |
| 3029 | |
| 3030 | #[cfg(not(target_arch = "wasm32"))] |
| 3031 | #[test] |
| 3032 | fn failed_opens_are_not_cached_and_evict_known_existing_paths() { |
| 3033 | let dir = std::env::temp_dir().join(format!("fission_evict_test_{}", std::process::id())); |
| 3034 | let _ = std::fs::remove_dir_all(&dir); |
| 3035 | std::fs::create_dir_all(&dir).unwrap(); |
| 3036 | |
| 3037 | let bad_vid = dir.join("corrupt.mp4"); |
| 3038 | std::fs::write(&bad_vid, b"not a valid mp4").unwrap(); |
| 3039 | let mut cache = MediaCache::new(64); |
| 3040 | assert!(cache.shared.path_exists_cached(&bad_vid)); |
| 3041 | assert!(cache.shared.get_or_open_video_decoder(&bad_vid).is_none()); |
| 3042 | assert_eq!(cache.video_decoder_count(), 0); |
| 3043 | assert!(!cache.shared.state.lock().unwrap().known_existing_paths.contains(&bad_vid)); |
| 3044 | |
| 3045 | let img_path = dir.join("gone.png"); |
| 3046 | image::RgbaImage::from_pixel(2, 2, image::Rgba([10, 20, 30, 255])) |
| 3047 | .save(&img_path) |
| 3048 | .unwrap(); |
| 3049 | assert!(cache.shared.path_exists_cached(&img_path)); |
| 3050 | std::fs::remove_file(&img_path).unwrap(); |
| 3051 | let clip = ClipSpec { |
| 3052 | filename: img_path.to_string_lossy().into_owned(), |
| 3053 | alternate_filename: None, |
| 3054 | start_frame: 0, |
| 3055 | length: Some(1), |
| 3056 | trim_in: 0, |
| 3057 | trim_out: 0, |
| 3058 | global_start: 0, |
| 3059 | global_end: 0, |
| 3060 | extend_first: 0, |
| 3061 | extend_last: 0, |
| 3062 | loop_clip: false, |
| 3063 | reverse: false, |
| 3064 | }; |
| 3065 | assert!(cache.load_clip(&clip, 0, false, [None; 4]).is_none()); |
| 3066 | assert!(!cache.shared.path_exists_cached(&img_path)); |
| 3067 | |
| 3068 | let _ = std::fs::remove_dir_all(&dir); |
| 3069 | } |
| 3070 | |
| 3071 | #[cfg(not(target_arch = "wasm32"))] |
| 3072 | #[test] |
| 3073 | fn prefetch_queue_prunes_stale_and_avoids_hol_blocking() { |
| 3074 | let dir = std::env::temp_dir().join(format!("fission_hol_prefetch_{}", std::process::id())); |
| 3075 | let _ = std::fs::remove_dir_all(&dir); |
| 3076 | std::fs::create_dir_all(&dir).unwrap(); |
| 3077 | |
| 3078 | for i in 0..=10 { |
| 3079 | let p = dir.join(format!("seq_{i:04}.png")); |
| 3080 | image::RgbaImage::from_pixel(2, 2, image::Rgba([i as u8 * 20, 10, 200, 255])) |
| 3081 | .save(&p) |
| 3082 | .unwrap(); |
| 3083 | } |
| 3084 | let esc = |p: &Path| p.to_string_lossy().replace('\\', "\\\\"); |
| 3085 | let p0 = dir.join("seq_0000.png"); |
| 3086 | let comp_src = format!( |
| 3087 | r#"Composition {{ |
| 3088 | CurrentTime = 0, |
| 3089 | RenderRange = {{ 0, 10 }}, |
| 3090 | GlobalRange = {{ 0, 10 }}, |
| 3091 | Tools = ordered() {{ |
| 3092 | LD = Loader {{ |
| 3093 | Clips = {{ |
| 3094 | Clip {{ |
| 3095 | ID = "Clip1", |
| 3096 | Filename = "{}", |
| 3097 | FormatID = "PNGFormat", |
| 3098 | StartFrame = 0, |
| 3099 | Length = 11, |
| 3100 | GlobalStart = 0, |
| 3101 | GlobalEnd = 10, |
| 3102 | }}, |
| 3103 | }}, |
| 3104 | }}, |
| 3105 | }}, |
| 3106 | }}"#, |
| 3107 | esc(&p0) |
| 3108 | ); |
| 3109 | let comp = Comp::from_fusion_str(&comp_src).expect("parse hol prefetch comp"); |
| 3110 | let target = comp.find_node("LD").unwrap(); |
| 3111 | let clip = comp.node(target).clips[0].clone(); |
| 3112 | let expr = model::ExprEngine::new(); |
| 3113 | let mut cache = MediaCache::new(64); |
| 3114 | |
| 3115 | let mut handle = cache.start_frame_prefetch(2); |
| 3116 | // Enqueue far-future seq=10 first (outside lookahead=2), then seq=1 (within lookahead). |
| 3117 | handle.enqueue_frame(&comp, target, 10, 10.0, &expr, false); |
| 3118 | handle.enqueue_frame(&comp, target, 1, 1.0, &expr, false); |
| 3119 | |
| 3120 | let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); |
| 3121 | while cache.entry_count() < 1 && std::time::Instant::now() < deadline { |
| 3122 | std::thread::sleep(std::time::Duration::from_millis(5)); |
| 3123 | } |
| 3124 | assert_eq!(cache.entry_count(), 1); |
| 3125 | let (_, _, img1, from_disk1) = cache.load_clip(&clip, 1, false, [None; 4]).unwrap(); |
| 3126 | assert!(!from_disk1, "seq=1 must not be head-of-line blocked by seq=10"); |
| 3127 | assert_eq!(img1.rgba[0], 20); |
| 3128 | |
| 3129 | // Advancing current_seq past 10 prunes seq=10 cleanly. |
| 3130 | handle.set_current_seq(11); |
| 3131 | { |
| 3132 | let q = handle.queue.state.lock().unwrap(); |
| 3133 | assert!(q.raster.is_empty()); |
| 3134 | assert!(q.queued_raster_keys.is_empty()); |
| 3135 | } |
| 3136 | handle.cancel(); |
| 3137 | let _ = std::fs::remove_dir_all(&dir); |
| 3138 | } |
| 2913 | 3139 | } |