OpenShot Library | libopenshot  0.5.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  class Settings;
25  using juce::Thread;
26 
34  class VideoCacheThread : public Thread
35  {
36  public:
39  ~VideoCacheThread() override;
40 
42  bool isReady();
43 
45  void Play() {};
46 
48  void Stop() {};
49 
57  void setSpeed(int new_speed);
58 
60  int getSpeed() const { return speed; }
61 
63  void Seek(int64_t new_position);
64 
70  void Seek(int64_t new_position, bool start_preroll);
71 
73  bool StartThread();
74 
76  bool StopThread(int timeoutMs = 0);
77 
82  void Reader(ReaderBase* new_reader) { reader = new_reader; Play(); }
83 
84  protected:
86  void run() override;
87 
97  int64_t getBytes(int width, int height, int sample_rate, int channels, float fps);
98 
99  //---------- Helper functions, broken out for clarity & unit testing ----------
100 
102  int computeDirection() const;
103 
109  void handleUserSeek(int64_t playhead, int dir);
110 
118  void handleUserSeekWithPreroll(int64_t playhead,
119  int dir,
120  int64_t timeline_end,
121  int64_t preroll_frames);
122 
124  int64_t computePrerollFrames(const Settings* settings) const;
125 
133  bool clearCacheIfPaused(int64_t playhead, bool paused, CacheBase* cache);
134 
148  void computeWindowBounds(int64_t playhead,
149  int dir,
150  int64_t ahead_count,
151  int64_t timeline_end,
152  int64_t& window_begin,
153  int64_t& window_end) const;
154 
168  bool prefetchWindow(CacheBase* cache,
169  int64_t window_begin,
170  int64_t window_end,
171  int dir,
172  ReaderBase* reader);
173 
174  //---------- Internal state ----------
175 
176  std::shared_ptr<Frame> last_cached_frame;
177 
178  int speed;
180  int last_dir;
181  bool userSeeked;
183 
187 
190 
193 
195  };
196 
197 } // namespace openshot
198 
199 #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::StartThread
bool StartThread()
Start the cache thread at high priority. Returns true if it’s actually running.
Definition: VideoCacheThread.cpp:92
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:220
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:188
openshot::VideoCacheThread::computeDirection
int computeDirection() const
Definition: VideoCacheThread.cpp:139
openshot::VideoCacheThread::reader
ReaderBase * reader
The source reader (e.g., Timeline, FFmpegReader).
Definition: VideoCacheThread.h:191
openshot::Settings
This class is contains settings used by libopenshot (and can be safely toggled at any point)
Definition: Settings.h:26
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:145
openshot::VideoCacheThread::computePrerollFrames
int64_t computePrerollFrames(const Settings *settings) const
Compute preroll frame count from settings.
Definition: VideoCacheThread.cpp:168
openshot::VideoCacheThread::last_cached_frame
std::shared_ptr< Frame > last_cached_frame
Last frame pointer added to cache.
Definition: VideoCacheThread.h:176
openshot::CacheBase
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:34
openshot::VideoCacheThread::Play
void Play()
Play method is unimplemented.
Definition: VideoCacheThread.h:45
openshot::VideoCacheThread::~VideoCacheThread
~VideoCacheThread() override
Definition: VideoCacheThread.cpp:45
openshot::VideoCacheThread::timeline_max_frame
int64_t timeline_max_frame
Highest valid frame index in the timeline.
Definition: VideoCacheThread.h:189
openshot::VideoCacheThread
Handles prefetching and caching of video/audio frames for smooth playback.
Definition: VideoCacheThread.h:34
openshot::VideoCacheThread::last_speed
int last_speed
Last non-zero speed (for tracking).
Definition: VideoCacheThread.h:179
openshot::VideoCacheThread::setSpeed
void setSpeed(int new_speed)
Set playback speed/direction. Positive = forward, negative = rewind, zero = pause.
Definition: VideoCacheThread.cpp:67
openshot::VideoCacheThread::userSeeked
bool userSeeked
True if Seek(..., true) was called (forces a cache reset).
Definition: VideoCacheThread.h:181
openshot::VideoCacheThread::speed
int speed
Current playback speed (0=paused, >0 forward, <0 backward).
Definition: VideoCacheThread.h:178
openshot::VideoCacheThread::Reader
void Reader(ReaderBase *new_reader)
Attach a ReaderBase (e.g. Timeline, FFmpegReader) and begin caching.
Definition: VideoCacheThread.h:82
openshot::VideoCacheThread::Stop
void Stop()
Stop method is unimplemented.
Definition: VideoCacheThread.h:48
openshot::VideoCacheThread::run
void run() override
Thread entry point: loops until threadShouldExit() is true.
Definition: VideoCacheThread.cpp:264
openshot::VideoCacheThread::last_cached_index
int64_t last_cached_index
Index of the most recently cached frame.
Definition: VideoCacheThread.h:194
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:78
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:184
openshot::VideoCacheThread::getSpeed
int getSpeed() const
Definition: VideoCacheThread.h:60
openshot::VideoCacheThread::current_display_frame
int64_t current_display_frame
Currently displayed frame (unused here, reserved).
Definition: VideoCacheThread.h:185
openshot::VideoCacheThread::last_dir
int last_dir
Last direction sign (+1 forward, –1 backward).
Definition: VideoCacheThread.h:180
openshot::VideoCacheThread::cached_frame_count
int64_t cached_frame_count
Estimated count of frames currently stored in cache.
Definition: VideoCacheThread.h:186
openshot::VideoCacheThread::force_directional_cache
bool force_directional_cache
(Reserved for future use).
Definition: VideoCacheThread.h:192
openshot::VideoCacheThread::StopThread
bool StopThread(int timeoutMs=0)
Stop the cache thread (wait up to timeoutMs ms). Returns true if it stopped.
Definition: VideoCacheThread.cpp:101
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:198
openshot::VideoCacheThread::Seek
void Seek(int64_t new_position)
Seek to a specific frame (no preroll).
Definition: VideoCacheThread.cpp:134
openshot::VideoCacheThread::handleUserSeekWithPreroll
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.
Definition: VideoCacheThread.cpp:151
openshot::VideoCacheThread::requested_display_frame
int64_t requested_display_frame
Frame index the user requested.
Definition: VideoCacheThread.h:184
openshot::VideoCacheThread::isReady
bool isReady()
Definition: VideoCacheThread.cpp:50
openshot::VideoCacheThread::preroll_on_next_fill
bool preroll_on_next_fill
True if next cache rebuild should include preroll offset.
Definition: VideoCacheThread.h:182