|
OpenShot Library | libopenshot 1.1.1
|
Handles prefetching and caching of video/audio frames for smooth playback. More...
#include <Qt/VideoCacheThread.h>
Public Member Functions | |
| void | AcknowledgeAudioCacheMiss () |
| int64_t | CancellationAcknowledgementUs () const |
| void | EnableRequestDiagnostics (bool enabled) |
| int64_t | FirstCurrentFrameUs () const |
| int | getSpeed () const |
| bool | isReady () |
| Shared startup gate; bounded fallback resumes established video decoding. | |
| void | NotifyAudioCacheMiss () |
| void | NotifyPlaybackPosition (int64_t new_position) |
| Update playback position without triggering seek behavior or cache invalidation. | |
| int64_t | ObsoleteCompletionUs () const |
| void | Play () |
| Play method is unimplemented. | |
| void | Reader (ReaderBase *new_reader) |
| Attach a ReaderBase (e.g. Timeline, FFmpegReader) and begin caching. | |
| void | Seek (int64_t new_position) |
| Backward-compatible alias for playback position updates (no seek side effects). | |
| void | Seek (int64_t new_position, bool start_preroll) |
| Seek to a specific frame and optionally start a preroll (cache reset). | |
| void | setSpeed (int new_speed) |
| Set playback speed/direction. Positive = forward, negative = rewind, zero = pause. | |
| bool | StartThread () |
| Start the cache thread at high priority. Returns true if it’s actually running. | |
| void | Stop () |
| Stop method is unimplemented. | |
| bool | StopThread (int timeoutMs=0) |
| Stop the cache thread (wait up to timeoutMs ms). Returns true if it stopped. | |
| bool | UsesCachedAudio () const |
| Callback miss holds the shared gate until the player resynchronizes audio. | |
| VideoCacheThread () | |
| Constructor: initializes member variables and assumes forward direction on first launch. | |
| ~VideoCacheThread () override | |
Protected Member Functions | |
| void | acknowledgeRequest (int64_t started_us) |
| int64_t | clampToTimelineRange (int64_t frame, int64_t timeline_end) const |
| Clamp frame index to [1, timeline_end] when timeline_end is valid. | |
| bool | clearCacheIfPaused (int64_t playhead, bool paused, CacheBase *cache) |
| When paused and playhead is outside current cache, clear all frames. | |
| int | computeDirection () const |
| int64_t | computePrerollFrames (const Settings *settings) const |
| Compute preroll frame count from settings. | |
| 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. | |
| int64_t | getBytes (int width, int height, int sample_rate, int channels, float fps) |
| Estimate memory usage for a single frame (video + audio). | |
| void | handleUserSeek (int64_t playhead, int dir) |
| If userSeeked is true, reset last_cached_index just behind the playhead. | |
| 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. | |
| bool | prefetchWindow (CacheBase *cache, int64_t window_begin, int64_t window_end, int dir, ReaderBase *reader, int64_t max_frames_to_fetch=-1, uint64_t generation=UINT64_MAX) |
| Prefetch all missing frames in [window_begin ... window_end] or [window_end ... window_begin]. | |
| void | resetReadiness () |
| int64_t | resolveTimelineEnd () const |
| Resolve timeline end frame from reader/timeline metadata. | |
| void | run () override |
| Thread entry point: loops until threadShouldExit() is true. | |
| void | updateReadiness (CacheBase *cache, int64_t playhead, int dir) |
| Worker-only cache probing; never runs in the audio callback. | |
Static Protected Member Functions | |
| static int64_t | monotonicUs () |
Protected Attributes | |
| std::atomic< int64_t > | acknowledgement_us {0} |
| std::atomic< bool > | audio_cache_miss {false} |
| std::atomic< bool > | cache_audio_only {false} |
| std::atomic< int64_t > | cached_frame_count |
| Estimated count of frames currently stored in cache. | |
| std::atomic< bool > | clear_cache_on_next_fill |
| True if next cache loop should clear existing cache ranges. | |
| int64_t | contiguous_ahead = 0 |
| int64_t | current_display_frame |
| Currently displayed frame (unused here, reserved). | |
| std::atomic< bool > | diagnostics_enabled {false} |
| std::atomic< int64_t > | first_current_frame_us {0} |
| bool | force_directional_cache |
| (Reserved for future use). | |
| std::shared_ptr< Frame > | last_cached_frame |
| Last frame pointer added to cache. | |
| std::atomic< int64_t > | last_cached_index |
| Index of the most recently cached frame. | |
| std::atomic< int > | last_dir |
| Last direction sign (+1 forward, –1 backward). | |
| std::atomic< int > | last_speed |
| Last non-zero speed (for tracking). | |
| std::mutex | lifecycle_mutex |
| Start/stop/reader drain; never acquired by worker. | |
| std::atomic< int64_t > | min_frames_ahead |
| Minimum number of frames considered “ready” (pre-roll). | |
| int64_t | observed_frame_bytes = 0 |
| std::atomic< int64_t > | obsolete_completion_us {0} |
| std::atomic< bool > | preroll_on_next_fill |
| True if next cache rebuild should include preroll offset. | |
| AdaptivePreroll | preroll_policy |
| std::atomic< uint64_t > | processed_generation {UINT64_MAX} |
| Last request whose scheduling baseline was consumed. | |
| ReaderBase * | reader |
| The source reader (e.g., Timeline, FFmpegReader). | |
| int64_t | readiness_bytes_per_frame = 0 |
| int64_t | readiness_capacity = 0 |
| int | readiness_direction = 1 |
| uint64_t | readiness_epoch = 0 |
| uint64_t | readiness_generation = UINT64_MAX |
| Request covered by the readiness snapshot. | |
| int64_t | readiness_minimum_step = 0 |
| int64_t | readiness_physical_capacity = 0 |
| int64_t | readiness_playhead = 1 |
| int64_t | readiness_timeline_end = 0 |
| std::atomic< uint64_t > | request_generation {0} |
| Invalidates obsolete fills, including seeks retaining cache. | |
| std::atomic< int64_t > | request_time_us {0} |
| std::atomic< int64_t > | requested_display_frame |
| Frame index the user requested. | |
| std::atomic< bool > | scrub_active |
| True while user is dragging/scrubbing the playhead. | |
| std::mutex | seek_state_mutex |
| Protects coherent seek state updates/consumption. | |
| uint64_t | seen_timeline_cache_epoch |
| Last observed Timeline cache invalidation epoch. | |
| std::atomic< int > | speed |
| Current playback speed (0=paused, >0 forward, <0 backward). | |
| bool | timeline_cache_epoch_initialized |
| True once an initial epoch snapshot has been taken. | |
| int64_t | timeline_max_frame |
| Highest valid frame index in the timeline. | |
| std::atomic< bool > | userSeeked |
| True if Seek(..., true) was called (forces a cache reset). | |
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 38 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.
|
inline |
Definition at line 50 of file VideoCacheThread.h.
|
protected |
Definition at line 155 of file VideoCacheThread.cpp.
|
inline |
Definition at line 92 of file VideoCacheThread.h.
|
protected |
Clamp frame index to [1, timeline_end] when timeline_end is valid.
Definition at line 460 of file VideoCacheThread.cpp.
Referenced by clearCacheIfPaused(), isReady(), prefetchWindow(), 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 468 of file VideoCacheThread.cpp.
|
protected |
Definition at line 396 of file VideoCacheThread.cpp.
Referenced by isReady(), Reader(), run(), Seek(), and setSpeed().
|
protected |
Compute preroll frame count from settings.
Definition at line 429 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 488 of file VideoCacheThread.cpp.
Referenced by run().
|
inline |
Optional request timings in microseconds (zero until observed). No per-frame logging; clocks are sampled only when enabled.
Definition at line 91 of file VideoCacheThread.h.
|
inline |
Definition at line 94 of file VideoCacheThread.h.
|
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 136 of file VideoCacheThread.cpp.
Referenced by run().
|
inline |
Definition at line 68 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 406 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 412 of file VideoCacheThread.cpp.
Referenced by run().
| bool openshot::VideoCacheThread::isReady | ( | ) |
Shared startup gate; bounded fallback resumes established video decoding.
Definition at line 56 of file VideoCacheThread.cpp.
Referenced by openshot::AudioReaderSource::getNextAudioBlock().
|
staticprotected |
Definition at line 149 of file VideoCacheThread.cpp.
Referenced by acknowledgeRequest(), isReady(), prefetchWindow(), resetReadiness(), Seek(), and Stop().
|
inline |
Definition at line 49 of file VideoCacheThread.h.
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 376 of file VideoCacheThread.cpp.
Referenced by Seek().
|
inline |
Definition at line 93 of file VideoCacheThread.h.
|
inline |
Play method is unimplemented.
Definition at line 53 of file VideoCacheThread.h.
|
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 510 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 195 of file VideoCacheThread.cpp.
Referenced by openshot::QtPlayer::Reader().
|
protected |
Definition at line 94 of file VideoCacheThread.cpp.
Referenced by isReady(), Reader(), run(), Seek(), and setSpeed().
|
protected |
Resolve timeline end frame from reader/timeline metadata.
Definition at line 445 of file VideoCacheThread.cpp.
Referenced by clearCacheIfPaused(), prefetchWindow(), run(), and Seek().
|
overrideprotected |
Thread entry point: loops until threadShouldExit() is true.
Definition at line 604 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 371 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 219 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 112 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.
Definition at line 167 of file VideoCacheThread.cpp.
| void openshot::VideoCacheThread::Stop | ( | ) |
Stop method is unimplemented.
Definition at line 177 of file VideoCacheThread.cpp.
Referenced by Reader(), openshot::QtPlayer::Stop(), and StopThread().
| bool openshot::VideoCacheThread::StopThread | ( | int | timeoutMs = 0 | ) |
Stop the cache thread (wait up to timeoutMs ms). Returns true if it stopped.
Definition at line 186 of file VideoCacheThread.cpp.
Referenced by ~VideoCacheThread().
|
protected |
Worker-only cache probing; never runs in the audio callback.
Definition at line 100 of file VideoCacheThread.cpp.
Referenced by prefetchWindow(), run(), and Seek().
|
inline |
Callback miss holds the shared gate until the player resynchronizes audio.
Definition at line 48 of file VideoCacheThread.h.
Referenced by openshot::AudioReaderSource::getNextAudioBlock().
|
protected |
Definition at line 248 of file VideoCacheThread.h.
Referenced by acknowledgeRequest(), and CancellationAcknowledgementUs().
|
protected |
Definition at line 208 of file VideoCacheThread.h.
Referenced by AcknowledgeAudioCacheMiss(), isReady(), NotifyAudioCacheMiss(), and Reader().
|
protected |
Definition at line 209 of file VideoCacheThread.h.
Referenced by Reader(), run(), and UsesCachedAudio().
|
protected |
Estimated count of frames currently stored in cache.
Definition at line 233 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 228 of file VideoCacheThread.h.
|
protected |
Definition at line 205 of file VideoCacheThread.h.
Referenced by isReady(), resetReadiness(), run(), and updateReadiness().
|
protected |
Currently displayed frame (unused here, reserved).
Definition at line 232 of file VideoCacheThread.h.
|
protected |
Definition at line 246 of file VideoCacheThread.h.
Referenced by EnableRequestDiagnostics(), prefetchWindow(), Seek(), and Stop().
|
protected |
Definition at line 250 of file VideoCacheThread.h.
Referenced by acknowledgeRequest(), FirstCurrentFrameUs(), and prefetchWindow().
|
protected |
(Reserved for future use).
Definition at line 239 of file VideoCacheThread.h.
|
protected |
Last frame pointer added to cache.
Definition at line 221 of file VideoCacheThread.h.
|
protected |
Index of the most recently cached frame.
Definition at line 243 of file VideoCacheThread.h.
Referenced by handleUserSeek(), handleUserSeekWithPreroll(), prefetchWindow(), Reader(), run(), Seek(), and setSpeed().
|
protected |
Last direction sign (+1 forward, –1 backward).
Definition at line 225 of file VideoCacheThread.h.
Referenced by computeDirection(), and setSpeed().
|
protected |
Last non-zero speed (for tracking).
Definition at line 224 of file VideoCacheThread.h.
Referenced by setSpeed().
|
mutableprotected |
Start/stop/reader drain; never acquired by worker.
Definition at line 253 of file VideoCacheThread.h.
Referenced by Reader(), StartThread(), and StopThread().
|
protected |
Minimum number of frames considered “ready” (pre-roll).
Definition at line 235 of file VideoCacheThread.h.
Referenced by isReady(), prefetchWindow(), run(), and updateReadiness().
|
protected |
Definition at line 210 of file VideoCacheThread.h.
Referenced by prefetchWindow(), and run().
|
protected |
Definition at line 249 of file VideoCacheThread.h.
Referenced by acknowledgeRequest(), ObsoleteCompletionUs(), and prefetchWindow().
|
protected |
True if next cache rebuild should include preroll offset.
Definition at line 227 of file VideoCacheThread.h.
|
protected |
Definition at line 204 of file VideoCacheThread.h.
Referenced by isReady(), prefetchWindow(), resetReadiness(), and run().
|
protected |
Last request whose scheduling baseline was consumed.
Definition at line 244 of file VideoCacheThread.h.
|
protected |
The source reader (e.g., Timeline, FFmpegReader).
Definition at line 238 of file VideoCacheThread.h.
Referenced by clearCacheIfPaused(), isReady(), NotifyPlaybackPosition(), prefetchWindow(), Reader(), resolveTimelineEnd(), run(), and Seek().
|
protected |
Definition at line 214 of file VideoCacheThread.h.
Referenced by run().
|
protected |
Definition at line 211 of file VideoCacheThread.h.
Referenced by prefetchWindow(), and run().
|
protected |
Definition at line 207 of file VideoCacheThread.h.
Referenced by isReady(), and updateReadiness().
|
protected |
Definition at line 215 of file VideoCacheThread.h.
|
protected |
Request covered by the readiness snapshot.
Definition at line 216 of file VideoCacheThread.h.
Referenced by isReady(), and updateReadiness().
|
protected |
Definition at line 217 of file VideoCacheThread.h.
Referenced by isReady(), run(), and updateReadiness().
|
protected |
Definition at line 212 of file VideoCacheThread.h.
|
protected |
Definition at line 206 of file VideoCacheThread.h.
Referenced by isReady(), and updateReadiness().
|
protected |
Definition at line 213 of file VideoCacheThread.h.
|
protected |
Invalidates obsolete fills, including seeks retaining cache.
Definition at line 245 of file VideoCacheThread.h.
Referenced by acknowledgeRequest(), isReady(), prefetchWindow(), Reader(), run(), setSpeed(), and updateReadiness().
|
protected |
Definition at line 247 of file VideoCacheThread.h.
Referenced by acknowledgeRequest(), and prefetchWindow().
|
protected |
Frame index the user requested.
Definition at line 231 of file VideoCacheThread.h.
Referenced by isReady(), NotifyPlaybackPosition(), prefetchWindow(), Reader(), run(), Seek(), and setSpeed().
|
protected |
True while user is dragging/scrubbing the playhead.
Definition at line 229 of file VideoCacheThread.h.
Referenced by NotifyPlaybackPosition(), run(), Seek(), and setSpeed().
|
mutableprotected |
Protects coherent seek state updates/consumption.
Definition at line 254 of file VideoCacheThread.h.
Referenced by isReady(), NotifyPlaybackPosition(), prefetchWindow(), Reader(), run(), Seek(), setSpeed(), and Stop().
|
protected |
Last observed Timeline cache invalidation epoch.
Definition at line 240 of file VideoCacheThread.h.
|
protected |
Current playback speed (0=paused, >0 forward, <0 backward).
Definition at line 223 of file VideoCacheThread.h.
Referenced by computeDirection(), getSpeed(), prefetchWindow(), run(), Seek(), and setSpeed().
|
protected |
True once an initial epoch snapshot has been taken.
Definition at line 241 of file VideoCacheThread.h.
|
protected |
Highest valid frame index in the timeline.
Definition at line 236 of file VideoCacheThread.h.
|
protected |
True if Seek(..., true) was called (forces a cache reset).
Definition at line 226 of file VideoCacheThread.h.
Referenced by prefetchWindow(), Reader(), run(), Seek(), and setSpeed().