OpenShot Library | libopenshot  0.4.0
VideoCacheThread.h
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2025 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #ifndef OPENSHOT_VIDEO_CACHE_THREAD_H
14 #define OPENSHOT_VIDEO_CACHE_THREAD_H
15 
16 #include "ReaderBase.h"
17 
18 #include <AppConfig.h>
19 #include <juce_audio_basics/juce_audio_basics.h>
20 #include <memory>
21 
22 namespace openshot
23 {
24  using juce::Thread;
25 
33  class VideoCacheThread : public Thread
34  {
35  public:
38  ~VideoCacheThread() override;
39 
41  bool isReady();
42 
44  void Play();
45 
47  void Stop();
48 
56  void setSpeed(int new_speed);
57 
59  int getSpeed() const { return speed; }
60 
62  void Seek(int64_t new_position);
63 
69  void Seek(int64_t new_position, bool start_preroll);
70 
75  void Reader(ReaderBase* new_reader) { reader = new_reader; Play(); }
76 
77  protected:
79  void run() override;
80 
90  int64_t getBytes(int width, int height, int sample_rate, int channels, float fps);
91 
92  //---------- Helper functions, broken out for clarity & unit testing ----------
93 
95  int computeDirection() const;
96 
102  void handleUserSeek(int64_t playhead, int dir);
103 
111  bool clearCacheIfPaused(int64_t playhead, bool paused, CacheBase* cache);
112 
126  void computeWindowBounds(int64_t playhead,
127  int dir,
128  int64_t ahead_count,
129  int64_t timeline_end,
130  int64_t& window_begin,
131  int64_t& window_end) const;
132 
146  bool prefetchWindow(CacheBase* cache,
147  int64_t window_begin,
148  int64_t window_end,
149  int dir,
150  ReaderBase* reader);
151 
152  //---------- Internal state ----------
153 
154  std::shared_ptr<Frame> last_cached_frame;
155 
156  int speed;
158  int last_dir;
159 
160  bool is_playing;
161  bool userSeeked;
162 
166 
170 
173 
175  };
176 
177 } // namespace openshot
178 
179 #endif // OPENSHOT_VIDEO_CACHE_THREAD_H
openshot::VideoCacheThread::VideoCacheThread
VideoCacheThread()
Constructor: initializes member variables and assumes forward direction on first launch.
Definition: VideoCacheThread.cpp:26
openshot::VideoCacheThread::prefetchWindow
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].
Definition: VideoCacheThread.cpp:152
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::VideoCacheThread::min_frames_ahead
int64_t min_frames_ahead
Minimum number of frames considered “ready” (pre-roll).
Definition: VideoCacheThread.h:167
openshot::VideoCacheThread::computeDirection
int computeDirection() const
Definition: VideoCacheThread.cpp:105
openshot::VideoCacheThread::reader
ReaderBase * reader
The source reader (e.g., Timeline, FFmpegReader).
Definition: VideoCacheThread.h:171
openshot::VideoCacheThread::handleUserSeek
void handleUserSeek(int64_t playhead, int dir)
If userSeeked is true, reset last_cached_index just behind the playhead.
Definition: VideoCacheThread.cpp:111
openshot::VideoCacheThread::last_cached_frame
std::shared_ptr< Frame > last_cached_frame
Last frame pointer added to cache.
Definition: VideoCacheThread.h:154
openshot::CacheBase
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:34
openshot::VideoCacheThread::Play
void Play()
Set is_playing = true, so run() will begin caching/playback.
Definition: VideoCacheThread.cpp:51
openshot::VideoCacheThread::~VideoCacheThread
~VideoCacheThread() override
Definition: VideoCacheThread.cpp:46
openshot::VideoCacheThread::timeline_max_frame
int64_t timeline_max_frame
Highest valid frame index in the timeline.
Definition: VideoCacheThread.h:169
openshot::VideoCacheThread
Handles prefetching and caching of video/audio frames for smooth playback.
Definition: VideoCacheThread.h:33
openshot::VideoCacheThread::last_speed
int last_speed
Last non-zero speed (for tracking).
Definition: VideoCacheThread.h:157
openshot::VideoCacheThread::setSpeed
void setSpeed(int new_speed)
Set playback speed/direction. Positive = forward, negative = rewind, zero = pause.
Definition: VideoCacheThread.cpp:68
openshot::VideoCacheThread::userSeeked
bool userSeeked
True if Seek(..., true) was called (forces a cache reset).
Definition: VideoCacheThread.h:161
openshot::VideoCacheThread::speed
int speed
Current playback speed (0=paused, >0 forward, <0 backward).
Definition: VideoCacheThread.h:156
openshot::VideoCacheThread::Reader
void Reader(ReaderBase *new_reader)
Attach a ReaderBase (e.g. Timeline, FFmpegReader) and begin caching.
Definition: VideoCacheThread.h:75
openshot::VideoCacheThread::Stop
void Stop()
Set is_playing = false, effectively pausing playback (caching still runs).
Definition: VideoCacheThread.cpp:57
openshot::VideoCacheThread::run
void run() override
Thread entry point: loops until threadShouldExit() is true.
Definition: VideoCacheThread.cpp:196
openshot::VideoCacheThread::last_cached_index
int64_t last_cached_index
Index of the most recently cached frame.
Definition: VideoCacheThread.h:174
openshot::VideoCacheThread::getBytes
int64_t getBytes(int width, int height, int sample_rate, int channels, float fps)
Estimate memory usage for a single frame (video + audio).
Definition: VideoCacheThread.cpp:79
ReaderBase.h
Header file for ReaderBase class.
openshot::VideoCacheThread::clearCacheIfPaused
bool clearCacheIfPaused(int64_t playhead, bool paused, CacheBase *cache)
When paused and playhead is outside current cache, clear all frames.
Definition: VideoCacheThread.cpp:117
openshot::VideoCacheThread::getSpeed
int getSpeed() const
Definition: VideoCacheThread.h:59
openshot::VideoCacheThread::max_frames_ahead
int64_t max_frames_ahead
Maximum frames to attempt to cache (mem capped).
Definition: VideoCacheThread.h:168
openshot::VideoCacheThread::current_display_frame
int64_t current_display_frame
Currently displayed frame (unused here, reserved).
Definition: VideoCacheThread.h:164
openshot::VideoCacheThread::last_dir
int last_dir
Last direction sign (+1 forward, –1 backward).
Definition: VideoCacheThread.h:158
openshot::VideoCacheThread::cached_frame_count
int64_t cached_frame_count
Count of frames currently added to cache.
Definition: VideoCacheThread.h:165
openshot::VideoCacheThread::force_directional_cache
bool force_directional_cache
(Reserved for future use).
Definition: VideoCacheThread.h:172
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:75
openshot::VideoCacheThread::computeWindowBounds
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.
Definition: VideoCacheThread.cpp:130
openshot::VideoCacheThread::is_playing
bool is_playing
True if playback is “running” (affects thread loop, not caching).
Definition: VideoCacheThread.h:160
openshot::VideoCacheThread::Seek
void Seek(int64_t new_position)
Seek to a specific frame (no preroll).
Definition: VideoCacheThread.cpp:100
openshot::VideoCacheThread::requested_display_frame
int64_t requested_display_frame
Frame index the user requested.
Definition: VideoCacheThread.h:163
openshot::VideoCacheThread::isReady
bool isReady()
Definition: VideoCacheThread.cpp:63