| 420 | } |
| 421 | |
| 422 | bool VideoCacheThread::prefetchWindow(CacheBase* cache, |
| 423 | int64_t window_begin, |
| 424 | int64_t window_end, |
| 425 | int dir, |
| 426 | ReaderBase* reader, |
| 427 | int64_t max_frames_to_fetch) |
| 428 | { |
| 429 | bool window_full = true; |
| 430 | int64_t next_frame = last_cached_index.load() + dir; |
| 431 | int64_t fetched_this_pass = 0; |
| 432 | |
| 433 | // Advance from last_cached_index toward window boundary |
| 434 | while ((dir > 0 && next_frame <= window_end) || |
| 435 | (dir < 0 && next_frame >= window_begin)) |
| 436 | { |
| 437 | if (threadShouldExit()) { |
| 438 | break; |
| 439 | } |
| 440 | // If a Seek was requested mid-caching, bail out immediately |
| 441 | if (userSeeked.load()) { |
| 442 | break; |
| 443 | } |
| 444 | |
| 445 | if (!cache->Contains(next_frame)) { |
| 446 | // Frame missing, fetch and add |
| 447 | try { |
| 448 | auto framePtr = reader->GetFrame(next_frame); |
| 449 | cache->Add(framePtr); |
| 450 | cached_frame_count.store(cache->Count()); |
| 451 | ++fetched_this_pass; |
| 452 | } |
| 453 | catch (const OutOfBoundsFrame&) { |
| 454 | break; |
| 455 | } |
| 456 | window_full = false; |
| 457 | } |
| 458 | else { |
| 459 | cache->Touch(next_frame); |
| 460 | } |
| 461 | |
| 462 | last_cached_index.store(next_frame); |
| 463 | next_frame += dir; |
| 464 | |
| 465 | // In active playback, avoid long uninterrupted prefetch bursts |
| 466 | // that can delay player thread frame retrieval. |
| 467 | if (max_frames_to_fetch > 0 && fetched_this_pass >= max_frames_to_fetch) { |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | return window_full; |
| 473 | } |
| 474 | |
| 475 | void VideoCacheThread::run() |
| 476 | { |