|
OpenShot Library | libopenshot
0.5.0
|
Handles prefetching and caching of video/audio frames for smooth playback. More...
#include <Qt/VideoCacheThread.h>
Public Member Functions | |
| int | getSpeed () const |
| bool | isReady () |
| void | NotifyPlaybackPosition (int64_t new_position) |
| Update playback position without triggering seek behavior or cache invalidation. More... | |
| void | Play () |
| Play method is unimplemented. More... | |
| void | Reader (ReaderBase *new_reader) |
| Attach a ReaderBase (e.g. Timeline, FFmpegReader) and begin caching. More... | |
| void | Seek (int64_t new_position) |
| Backward-compatible alias for playback position updates (no seek side effects). More... | |
| void | Seek (int64_t new_position, bool start_preroll) |
| Seek to a specific frame and optionally start a preroll (cache reset). More... | |
| void | setSpeed (int new_speed) |
| Set playback speed/direction. Positive = forward, negative = rewind, zero = pause. More... | |
| bool | StartThread () |
| Start the cache thread at high priority. Returns true if it’s actually running. More... | |
| void | Stop () |
| Stop method is unimplemented. More... | |
| bool | StopThread (int timeoutMs=0) |
| Stop the cache thread (wait up to timeoutMs ms). Returns true if it stopped. More... | |
| VideoCacheThread () | |
| Constructor: initializes member variables and assumes forward direction on first launch. More... | |
| ~VideoCacheThread () override | |
Protected Member Functions | |
| int64_t | clampToTimelineRange (int64_t frame, int64_t timeline_end) const |
| Clamp frame index to [1, timeline_end] when timeline_end is valid. More... | |
| bool | clearCacheIfPaused (int64_t playhead, bool paused, CacheBase *cache) |
| When paused and playhead is outside current cache, clear all frames. More... | |
| int | computeDirection () const |
| int64_t | computePrerollFrames (const Settings *settings) const |
| Compute preroll frame count from settings. More... | |
| void | computeWindowBounds (int64_t playhead, int dir, int64_t ahead_count, int64_t timeline_end, int64_t &window_begin, int64_t &window_end) const |
| Compute the “window” of frames to cache around playhead. More... | |
| int64_t | getBytes (int width, int height, int sample_rate, int channels, float fps) |
| Estimate memory usage for a single frame (video + audio). More... | |
| void | handleUserSeek (int64_t playhead, int dir) |
| If userSeeked is true, reset last_cached_index just behind the playhead. More... | |
| void | handleUserSeekWithPreroll (int64_t playhead, int dir, int64_t timeline_end, int64_t preroll_frames) |
| Reset last_cached_index to start caching with a directional preroll offset. More... | |
| bool | prefetchWindow (CacheBase *cache, int64_t window_begin, int64_t window_end, int dir, ReaderBase *reader, int64_t max_frames_to_fetch=-1) |
| Prefetch all missing frames in [window_begin ... window_end] or [window_end ... window_begin]. More... | |
| int64_t | resolveTimelineEnd () const |
| Resolve timeline end frame from reader/timeline metadata. More... | |
| void | run () override |
| Thread entry point: loops until threadShouldExit() is true. More... | |
Protected Attributes | |
| std::atomic< int64_t > | cached_frame_count |
| Estimated count of frames currently stored in cache. More... | |
| std::atomic< bool > | clear_cache_on_next_fill |
| True if next cache loop should clear existing cache ranges. More... | |
| int64_t | current_display_frame |
| Currently displayed frame (unused here, reserved). More... | |
| bool | force_directional_cache |
| (Reserved for future use). More... | |
| std::shared_ptr< Frame > | last_cached_frame |
| Last frame pointer added to cache. More... | |
| std::atomic< int64_t > | last_cached_index |
| Index of the most recently cached frame. More... | |
| std::atomic< int > | last_dir |
| Last direction sign (+1 forward, –1 backward). More... | |
| std::atomic< int > | last_speed |
| Last non-zero speed (for tracking). More... | |
| std::atomic< int64_t > | min_frames_ahead |
| Minimum number of frames considered “ready” (pre-roll). More... | |
| std::atomic< bool > | preroll_on_next_fill |
| True if next cache rebuild should include preroll offset. More... | |
| ReaderBase * | reader |
| The source reader (e.g., Timeline, FFmpegReader). More... | |
| std::atomic< int64_t > | requested_display_frame |
| Frame index the user requested. More... | |
| std::atomic< bool > | scrub_active |
| True while user is dragging/scrubbing the playhead. More... | |
| std::mutex | seek_state_mutex |
| Protects coherent seek state updates/consumption. More... | |
| uint64_t | seen_timeline_cache_epoch |
| Last observed Timeline cache invalidation epoch. More... | |
| std::atomic< int > | speed |
| Current playback speed (0=paused, >0 forward, <0 backward). More... | |
| bool | timeline_cache_epoch_initialized |
| True once an initial epoch snapshot has been taken. More... | |
| int64_t | timeline_max_frame |
| Highest valid frame index in the timeline. More... | |
| std::atomic< bool > | userSeeked |
| True if Seek(..., true) was called (forces a cache reset). More... | |
Handles prefetching and caching of video/audio frames for smooth playback.
This thread continuously maintains a “window” of cached frames in the current playback direction (forward or backward). When paused, it continues to fill that same window; when seeking, it resets to cache around the new position.
Definition at line 37 of file VideoCacheThread.h.
| openshot::VideoCacheThread::VideoCacheThread | ( | ) |
Constructor: initializes member variables and assumes forward direction on first launch.
Definition at line 26 of file VideoCacheThread.cpp.
|
override |
Definition at line 49 of file VideoCacheThread.cpp.
|
protected |
Clamp frame index to [1, timeline_end] when timeline_end is valid.
Definition at line 372 of file VideoCacheThread.cpp.
Referenced by clearCacheIfPaused(), isReady(), run(), and Seek().
|
protected |
When paused and playhead is outside current cache, clear all frames.
| playhead | Current requested_display_frame |
| paused | True if speed == 0 |
| cache | Pointer to CacheBase |
Definition at line 380 of file VideoCacheThread.cpp.
Referenced by run().
|
protected |
Definition at line 308 of file VideoCacheThread.cpp.
|
protected |
Compute preroll frame count from settings.
Definition at line 341 of file VideoCacheThread.cpp.
Referenced by run().
|
protected |
Compute the “window” of frames to cache around playhead.
| playhead | Current requested_display_frame | |
| dir | Effective direction (±1) | |
| ahead_count | Number of frames ahead/back to cache | |
| timeline_end | Last valid frame index | |
| [out] | window_begin | Lower bound (inclusive) of caching window |
| [out] | window_end | Upper bound (inclusive) of caching window |
If dir > 0: window = [playhead ... playhead + ahead_count] If dir < 0: window = [playhead – ahead_count ... playhead] Always clamps to [1 ... timeline_end].
Definition at line 400 of file VideoCacheThread.cpp.
Referenced by run().
|
protected |
Estimate memory usage for a single frame (video + audio).
| width | Frame width (pixels) |
| height | Frame height (pixels) |
| sample_rate | Audio sample rate (e.g. 48000) |
| channels | Number of audio channels |
| fps | Frames per second |
Definition at line 107 of file VideoCacheThread.cpp.
Referenced by run().
|
inline |
Definition at line 63 of file VideoCacheThread.h.
|
protected |
If userSeeked is true, reset last_cached_index just behind the playhead.
| playhead | Current requested_display_frame |
| dir | Effective direction (±1) |
Definition at line 318 of file VideoCacheThread.cpp.
Referenced by run().
|
protected |
Reset last_cached_index to start caching with a directional preroll offset.
| playhead | Current requested_display_frame |
| dir | Effective direction (±1) |
| timeline_end | Last valid frame index |
| preroll_frames | Number of frames to offset the cache start |
Definition at line 324 of file VideoCacheThread.cpp.
Referenced by run().
| bool openshot::VideoCacheThread::isReady | ( | ) |
Definition at line 54 of file VideoCacheThread.cpp.
Referenced by openshot::AudioReaderSource::getNextAudioBlock().
| void openshot::VideoCacheThread::NotifyPlaybackPosition | ( | int64_t | new_position | ) |
Update playback position without triggering seek behavior or cache invalidation.
Definition at line 288 of file VideoCacheThread.cpp.
Referenced by Seek().
|
inline |
Play method is unimplemented.
Definition at line 48 of file VideoCacheThread.h.
Referenced by Reader().
|
protected |
Prefetch all missing frames in [window_begin ... window_end] or [window_end ... window_begin].
| cache | Pointer to CacheBase |
| window_begin | Inclusive lower bound of the window |
| window_end | Inclusive upper bound of the window |
| dir | Effective direction (±1) |
| reader | Pointer to ReaderBase to call GetFrame() |
Internally, this method iterates from last_cached_index + dir toward window_end (or window_begin) and calls GetFrame()/Add() for each missing frame until hitting the window boundary or an OOB. It also breaks early if threadShouldExit() or userSeeked becomes true.
Definition at line 422 of file VideoCacheThread.cpp.
Referenced by run().
| void openshot::VideoCacheThread::Reader | ( | ReaderBase * | new_reader | ) |
Attach a ReaderBase (e.g. Timeline, FFmpegReader) and begin caching.
| new_reader |
Definition at line 136 of file VideoCacheThread.cpp.
Referenced by openshot::QtPlayer::Reader().
|
protected |
Resolve timeline end frame from reader/timeline metadata.
Definition at line 357 of file VideoCacheThread.cpp.
Referenced by clearCacheIfPaused(), run(), and Seek().
|
overrideprotected |
Thread entry point: loops until threadShouldExit() is true.
Definition at line 475 of file VideoCacheThread.cpp.
| void openshot::VideoCacheThread::Seek | ( | int64_t | new_position | ) |
Backward-compatible alias for playback position updates (no seek side effects).
Definition at line 283 of file VideoCacheThread.cpp.
Referenced by openshot::QtPlayer::Seek().
| void openshot::VideoCacheThread::Seek | ( | int64_t | new_position, |
| bool | start_preroll | ||
| ) |
Seek to a specific frame and optionally start a preroll (cache reset).
| new_position | Frame index to jump to. |
| start_preroll | If true, forces cache to rebuild around new_position. |
Definition at line 145 of file VideoCacheThread.cpp.
| void openshot::VideoCacheThread::setSpeed | ( | int | new_speed | ) |
Set playback speed/direction. Positive = forward, negative = rewind, zero = pause.
| new_speed | If new_speed != 0, last_speed and last_dir are updated. If new_speed == 0, last_dir is left unchanged so that pausing does not flip direction. |
Definition at line 94 of file VideoCacheThread.cpp.
Referenced by openshot::QtPlayer::Speed().
| bool openshot::VideoCacheThread::StartThread | ( | ) |
Start the cache thread at high priority. Returns true if it’s actually running.
Start the cache thread at high priority, and return true if it’s actually running.
Definition at line 121 of file VideoCacheThread.cpp.
|
inline |
Stop method is unimplemented.
Definition at line 51 of file VideoCacheThread.h.
Referenced by openshot::QtPlayer::Stop().
| bool openshot::VideoCacheThread::StopThread | ( | int | timeoutMs = 0 | ) |
Stop the cache thread (wait up to timeoutMs ms). Returns true if it stopped.
Stop the cache thread, waiting up to timeoutMs ms. Returns true if it actually stopped.
Definition at line 130 of file VideoCacheThread.cpp.
|
protected |
Estimated count of frames currently stored in cache.
Definition at line 201 of file VideoCacheThread.h.
Referenced by clearCacheIfPaused(), NotifyPlaybackPosition(), prefetchWindow(), run(), and Seek().
|
protected |
True if next cache loop should clear existing cache ranges.
Definition at line 196 of file VideoCacheThread.h.
|
protected |
Currently displayed frame (unused here, reserved).
Definition at line 200 of file VideoCacheThread.h.
|
protected |
(Reserved for future use).
Definition at line 207 of file VideoCacheThread.h.
|
protected |
Last frame pointer added to cache.
Definition at line 189 of file VideoCacheThread.h.
|
protected |
Index of the most recently cached frame.
Definition at line 211 of file VideoCacheThread.h.
Referenced by handleUserSeek(), handleUserSeekWithPreroll(), isReady(), prefetchWindow(), run(), and Seek().
|
protected |
Last direction sign (+1 forward, –1 backward).
Definition at line 193 of file VideoCacheThread.h.
Referenced by computeDirection(), run(), and setSpeed().
|
protected |
Last non-zero speed (for tracking).
Definition at line 192 of file VideoCacheThread.h.
Referenced by setSpeed().
|
protected |
Minimum number of frames considered “ready” (pre-roll).
Definition at line 203 of file VideoCacheThread.h.
|
protected |
True if next cache rebuild should include preroll offset.
Definition at line 195 of file VideoCacheThread.h.
|
protected |
The source reader (e.g., Timeline, FFmpegReader).
Definition at line 206 of file VideoCacheThread.h.
Referenced by clearCacheIfPaused(), isReady(), NotifyPlaybackPosition(), prefetchWindow(), Reader(), resolveTimelineEnd(), run(), and Seek().
|
protected |
Frame index the user requested.
Definition at line 199 of file VideoCacheThread.h.
Referenced by isReady(), NotifyPlaybackPosition(), run(), and Seek().
|
protected |
True while user is dragging/scrubbing the playhead.
Definition at line 197 of file VideoCacheThread.h.
Referenced by NotifyPlaybackPosition(), run(), Seek(), and setSpeed().
|
mutableprotected |
Protects coherent seek state updates/consumption.
Definition at line 212 of file VideoCacheThread.h.
Referenced by NotifyPlaybackPosition(), Reader(), run(), and Seek().
|
protected |
Last observed Timeline cache invalidation epoch.
Definition at line 208 of file VideoCacheThread.h.
|
protected |
Current playback speed (0=paused, >0 forward, <0 backward).
Definition at line 191 of file VideoCacheThread.h.
Referenced by computeDirection(), getSpeed(), run(), Seek(), and setSpeed().
|
protected |
True once an initial epoch snapshot has been taken.
Definition at line 209 of file VideoCacheThread.h.
|
protected |
Highest valid frame index in the timeline.
Definition at line 204 of file VideoCacheThread.h.
|
protected |
True if Seek(..., true) was called (forces a cache reset).
Definition at line 194 of file VideoCacheThread.h.
Referenced by prefetchWindow(), run(), and Seek().
1.8.17