OpenShot Library | libopenshot  0.4.0
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
openshot::VideoCacheThread Class Reference

Handles prefetching and caching of video/audio frames for smooth playback. More...

#include <Qt/VideoCacheThread.h>

Inheritance diagram for openshot::VideoCacheThread:
[legend]
Collaboration diagram for openshot::VideoCacheThread:
[legend]

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< Framelast_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...
 
ReaderBasereader
 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...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ VideoCacheThread()

openshot::VideoCacheThread::VideoCacheThread ( )

Constructor: initializes member variables and assumes forward direction on first launch.

Definition at line 26 of file VideoCacheThread.cpp.

◆ ~VideoCacheThread()

openshot::VideoCacheThread::~VideoCacheThread ( )
override

Definition at line 46 of file VideoCacheThread.cpp.

Member Function Documentation

◆ clearCacheIfPaused()

bool openshot::VideoCacheThread::clearCacheIfPaused ( int64_t  playhead,
bool  paused,
CacheBase cache 
)
protected

When paused and playhead is outside current cache, clear all frames.

Parameters
playheadCurrent requested_display_frame
pausedTrue if speed == 0
cachePointer to CacheBase
Returns
True if ClearAllCache() was invoked.

Definition at line 117 of file VideoCacheThread.cpp.

Referenced by run().

◆ computeDirection()

int openshot::VideoCacheThread::computeDirection ( ) const
protected
Returns
Effective playback direction (+1 forward, –1 backward), preserving last_dir if speed == 0.

Definition at line 105 of file VideoCacheThread.cpp.

Referenced by run().

◆ computeWindowBounds()

void openshot::VideoCacheThread::computeWindowBounds ( int64_t  playhead,
int  dir,
int64_t  ahead_count,
int64_t  timeline_end,
int64_t &  window_begin,
int64_t &  window_end 
) const
protected

Compute the “window” of frames to cache around playhead.

Parameters
playheadCurrent requested_display_frame
dirEffective direction (±1)
ahead_countNumber of frames ahead/back to cache
timeline_endLast valid frame index
[out]window_beginLower bound (inclusive) of caching window
[out]window_endUpper 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().

◆ getBytes()

int64_t openshot::VideoCacheThread::getBytes ( int  width,
int  height,
int  sample_rate,
int  channels,
float  fps 
)
protected

Estimate memory usage for a single frame (video + audio).

Parameters
widthFrame width (pixels)
heightFrame height (pixels)
sample_rateAudio sample rate (e.g. 48000)
channelsNumber of audio channels
fpsFrames per second
Returns
Approximate size in bytes for one frame

Definition at line 79 of file VideoCacheThread.cpp.

Referenced by run().

◆ getSpeed()

int openshot::VideoCacheThread::getSpeed ( ) const
inline
Returns
The current speed (1=normal, 2=fast, –1=rewind, etc.)

Definition at line 59 of file VideoCacheThread.h.

◆ handleUserSeek()

void openshot::VideoCacheThread::handleUserSeek ( int64_t  playhead,
int  dir 
)
protected

If userSeeked is true, reset last_cached_index just behind the playhead.

Parameters
playheadCurrent requested_display_frame
dirEffective direction (±1)

Definition at line 111 of file VideoCacheThread.cpp.

Referenced by run().

◆ isReady()

bool openshot::VideoCacheThread::isReady ( )
Returns
True if at least min_frames_ahead frames have been cached.

Definition at line 63 of file VideoCacheThread.cpp.

Referenced by openshot::AudioReaderSource::getNextAudioBlock().

◆ Play()

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().

◆ prefetchWindow()

bool openshot::VideoCacheThread::prefetchWindow ( CacheBase cache,
int64_t  window_begin,
int64_t  window_end,
int  dir,
ReaderBase reader 
)
protected

Prefetch all missing frames in [window_begin ... window_end] or [window_end ... window_begin].

Parameters
cachePointer to CacheBase
window_beginInclusive lower bound of the window
window_endInclusive upper bound of the window
dirEffective direction (±1)
readerPointer to ReaderBase to call GetFrame()
Returns
True if the window was already full (no new frames added)

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().

◆ Reader()

void openshot::VideoCacheThread::Reader ( ReaderBase new_reader)
inline

Attach a ReaderBase (e.g. Timeline, FFmpegReader) and begin caching.

Parameters
new_reader

Definition at line 75 of file VideoCacheThread.h.

Referenced by openshot::QtPlayer::Reader().

◆ run()

void openshot::VideoCacheThread::run ( )
overrideprotected

Thread entry point: loops until threadShouldExit() is true.

Definition at line 196 of file VideoCacheThread.cpp.

◆ Seek() [1/2]

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().

◆ Seek() [2/2]

void openshot::VideoCacheThread::Seek ( int64_t  new_position,
bool  start_preroll 
)

Seek to a specific frame and optionally start a preroll (cache reset).

Parameters
new_positionFrame index to jump to.
start_prerollIf true, forces cache to rebuild around new_position.

Definition at line 92 of file VideoCacheThread.cpp.

◆ setSpeed()

void openshot::VideoCacheThread::setSpeed ( int  new_speed)

Set playback speed/direction. Positive = forward, negative = rewind, zero = pause.

Parameters
new_speedIf 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().

◆ Stop()

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().

Member Data Documentation

◆ cached_frame_count

int64_t openshot::VideoCacheThread::cached_frame_count
protected

Count of frames currently added to cache.

Definition at line 165 of file VideoCacheThread.h.

Referenced by isReady(), and prefetchWindow().

◆ current_display_frame

int64_t openshot::VideoCacheThread::current_display_frame
protected

Currently displayed frame (unused here, reserved).

Definition at line 164 of file VideoCacheThread.h.

◆ force_directional_cache

bool openshot::VideoCacheThread::force_directional_cache
protected

(Reserved for future use).

Definition at line 172 of file VideoCacheThread.h.

◆ is_playing

bool openshot::VideoCacheThread::is_playing
protected

True if playback is “running” (affects thread loop, not caching).

Definition at line 160 of file VideoCacheThread.h.

Referenced by Play(), and Stop().

◆ last_cached_frame

std::shared_ptr<Frame> openshot::VideoCacheThread::last_cached_frame
protected

Last frame pointer added to cache.

Definition at line 154 of file VideoCacheThread.h.

◆ last_cached_index

int64_t openshot::VideoCacheThread::last_cached_index
protected

Index of the most recently cached frame.

Definition at line 174 of file VideoCacheThread.h.

Referenced by handleUserSeek(), prefetchWindow(), and run().

◆ last_dir

int openshot::VideoCacheThread::last_dir
protected

Last direction sign (+1 forward, –1 backward).

Definition at line 158 of file VideoCacheThread.h.

Referenced by computeDirection(), run(), and setSpeed().

◆ last_speed

int openshot::VideoCacheThread::last_speed
protected

Last non-zero speed (for tracking).

Definition at line 157 of file VideoCacheThread.h.

Referenced by setSpeed().

◆ max_frames_ahead

int64_t openshot::VideoCacheThread::max_frames_ahead
protected

Maximum frames to attempt to cache (mem capped).

Definition at line 168 of file VideoCacheThread.h.

◆ min_frames_ahead

int64_t openshot::VideoCacheThread::min_frames_ahead
protected

Minimum number of frames considered “ready” (pre-roll).

Definition at line 167 of file VideoCacheThread.h.

Referenced by isReady().

◆ reader

ReaderBase* openshot::VideoCacheThread::reader
protected

The source reader (e.g., Timeline, FFmpegReader).

Definition at line 171 of file VideoCacheThread.h.

Referenced by clearCacheIfPaused(), prefetchWindow(), Reader(), and run().

◆ requested_display_frame

int64_t openshot::VideoCacheThread::requested_display_frame
protected

Frame index the user requested.

Definition at line 163 of file VideoCacheThread.h.

Referenced by run(), and Seek().

◆ speed

int openshot::VideoCacheThread::speed
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().

◆ timeline_max_frame

int64_t openshot::VideoCacheThread::timeline_max_frame
protected

Highest valid frame index in the timeline.

Definition at line 169 of file VideoCacheThread.h.

◆ userSeeked

bool openshot::VideoCacheThread::userSeeked
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().


The documentation for this class was generated from the following files: