OpenShot Library | libopenshot
0.4.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 | Play () |
Set is_playing = true, so run() will begin caching/playback. More... | |
void | Reader (ReaderBase *new_reader) |
Attach a ReaderBase (e.g. Timeline, FFmpegReader) and begin caching. More... | |
void | Seek (int64_t new_position) |
Seek to a specific frame (no preroll). 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... | |
void | Stop () |
Set is_playing = false, effectively pausing playback (caching still runs). More... | |
VideoCacheThread () | |
Constructor: initializes member variables and assumes forward direction on first launch. More... | |
~VideoCacheThread () override | |
Protected Member Functions | |
bool | clearCacheIfPaused (int64_t playhead, bool paused, CacheBase *cache) |
When paused and playhead is outside current cache, clear all frames. More... | |
int | computeDirection () const |
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... | |
bool | prefetchWindow (CacheBase *cache, int64_t window_begin, int64_t window_end, int dir, ReaderBase *reader) |
Prefetch all missing frames in [window_begin ... window_end] or [window_end ... window_begin]. More... | |
void | run () override |
Thread entry point: loops until threadShouldExit() is true. More... | |
Protected Attributes | |
int64_t | cached_frame_count |
Count of frames currently added to cache. More... | |
int64_t | current_display_frame |
Currently displayed frame (unused here, reserved). More... | |
bool | force_directional_cache |
(Reserved for future use). More... | |
bool | is_playing |
True if playback is “running” (affects thread loop, not caching). More... | |
std::shared_ptr< Frame > | last_cached_frame |
Last frame pointer added to cache. More... | |
int64_t | last_cached_index |
Index of the most recently cached frame. More... | |
int | last_dir |
Last direction sign (+1 forward, –1 backward). More... | |
int | last_speed |
Last non-zero speed (for tracking). More... | |
int64_t | max_frames_ahead |
Maximum frames to attempt to cache (mem capped). More... | |
int64_t | min_frames_ahead |
Minimum number of frames considered “ready” (pre-roll). More... | |
ReaderBase * | reader |
The source reader (e.g., Timeline, FFmpegReader). More... | |
int64_t | requested_display_frame |
Frame index the user requested. More... | |
int | speed |
Current playback speed (0=paused, >0 forward, <0 backward). More... | |
int64_t | timeline_max_frame |
Highest valid frame index in the timeline. More... | |
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 33 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 46 of file VideoCacheThread.cpp.
|
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 117 of file VideoCacheThread.cpp.
Referenced by run().
|
protected |
Definition at line 105 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 130 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 79 of file VideoCacheThread.cpp.
Referenced by run().
|
inline |
Definition at line 59 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 111 of file VideoCacheThread.cpp.
Referenced by run().
bool openshot::VideoCacheThread::isReady | ( | ) |
Definition at line 63 of file VideoCacheThread.cpp.
Referenced by openshot::AudioReaderSource::getNextAudioBlock().
void openshot::VideoCacheThread::Play | ( | ) |
Set is_playing = true, so run() will begin caching/playback.
Definition at line 51 of file VideoCacheThread.cpp.
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 152 of file VideoCacheThread.cpp.
Referenced by run().
|
inline |
Attach a ReaderBase (e.g. Timeline, FFmpegReader) and begin caching.
new_reader |
Definition at line 75 of file VideoCacheThread.h.
Referenced by openshot::QtPlayer::Reader().
|
overrideprotected |
Thread entry point: loops until threadShouldExit() is true.
Definition at line 196 of file VideoCacheThread.cpp.
void openshot::VideoCacheThread::Seek | ( | int64_t | new_position | ) |
Seek to a specific frame (no preroll).
Definition at line 100 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 92 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 68 of file VideoCacheThread.cpp.
Referenced by openshot::QtPlayer::Speed().
void openshot::VideoCacheThread::Stop | ( | ) |
Set is_playing = false, effectively pausing playback (caching still runs).
Definition at line 57 of file VideoCacheThread.cpp.
Referenced by openshot::QtPlayer::Stop().
|
protected |
Count of frames currently added to cache.
Definition at line 165 of file VideoCacheThread.h.
Referenced by isReady(), and prefetchWindow().
|
protected |
Currently displayed frame (unused here, reserved).
Definition at line 164 of file VideoCacheThread.h.
|
protected |
(Reserved for future use).
Definition at line 172 of file VideoCacheThread.h.
|
protected |
True if playback is “running” (affects thread loop, not caching).
Definition at line 160 of file VideoCacheThread.h.
|
protected |
Last frame pointer added to cache.
Definition at line 154 of file VideoCacheThread.h.
|
protected |
Index of the most recently cached frame.
Definition at line 174 of file VideoCacheThread.h.
Referenced by handleUserSeek(), prefetchWindow(), and run().
|
protected |
Last direction sign (+1 forward, –1 backward).
Definition at line 158 of file VideoCacheThread.h.
Referenced by computeDirection(), run(), and setSpeed().
|
protected |
Last non-zero speed (for tracking).
Definition at line 157 of file VideoCacheThread.h.
Referenced by setSpeed().
|
protected |
Maximum frames to attempt to cache (mem capped).
Definition at line 168 of file VideoCacheThread.h.
|
protected |
Minimum number of frames considered “ready” (pre-roll).
Definition at line 167 of file VideoCacheThread.h.
Referenced by isReady().
|
protected |
The source reader (e.g., Timeline, FFmpegReader).
Definition at line 171 of file VideoCacheThread.h.
Referenced by clearCacheIfPaused(), prefetchWindow(), Reader(), and run().
|
protected |
Frame index the user requested.
Definition at line 163 of file VideoCacheThread.h.
|
protected |
Current playback speed (0=paused, >0 forward, <0 backward).
Definition at line 156 of file VideoCacheThread.h.
Referenced by computeDirection(), getSpeed(), run(), and setSpeed().
|
protected |
Highest valid frame index in the timeline.
Definition at line 169 of file VideoCacheThread.h.
|
protected |
True if Seek(..., true) was called (forces a cache reset).
Definition at line 161 of file VideoCacheThread.h.
Referenced by prefetchWindow(), run(), and Seek().