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 <atomic>
21 #include <cstdint>
22 #include <mutex>
23 #include <memory>
24 
25 namespace openshot
26 {
27  class Settings;
28  using juce::Thread;
29 
37  class VideoCacheThread : public Thread
38  {
39  public:
42  ~VideoCacheThread() override;
43 
45  bool isReady();
46 
48  void Play() {};
49 
51  void Stop() {};
52 
60  void setSpeed(int new_speed);
61 
63  int getSpeed() const { return speed.load(); }
64 
66  void Seek(int64_t new_position);
67 
73  void Seek(int64_t new_position, bool start_preroll);
74 
76  void NotifyPlaybackPosition(int64_t new_position);
77 
79  bool StartThread();
80 
82  bool StopThread(int timeoutMs = 0);
83 
88  void Reader(ReaderBase* new_reader);
89 
90  protected:
92  void run() override;
93 
103  int64_t getBytes(int width, int height, int sample_rate, int channels, float fps);
104 
105  //---------- Helper functions, broken out for clarity & unit testing ----------
106 
108  int computeDirection() const;
109 
115  void handleUserSeek(int64_t playhead, int dir);
116 
124  void handleUserSeekWithPreroll(int64_t playhead,
125  int dir,
126  int64_t timeline_end,
127  int64_t preroll_frames);
128 
130  int64_t computePrerollFrames(const Settings* settings) const;
131 
133  int64_t resolveTimelineEnd() const;
134 
136  int64_t clampToTimelineRange(int64_t frame, int64_t timeline_end) const;
137 
145  bool clearCacheIfPaused(int64_t playhead, bool paused, CacheBase* cache);
146 
160  void computeWindowBounds(int64_t playhead,
161  int dir,
162  int64_t ahead_count,
163  int64_t timeline_end,
164  int64_t& window_begin,
165  int64_t& window_end) const;
166 
180  bool prefetchWindow(CacheBase* cache,
181  int64_t window_begin,
182  int64_t window_end,
183  int dir,
185  int64_t max_frames_to_fetch = -1);
186 
187  //---------- Internal state ----------
188 
189  std::shared_ptr<Frame> last_cached_frame;
190 
191  std::atomic<int> speed;
192  std::atomic<int> last_speed;
193  std::atomic<int> last_dir;
194  std::atomic<bool> userSeeked;
195  std::atomic<bool> preroll_on_next_fill;
196  std::atomic<bool> clear_cache_on_next_fill;
197  std::atomic<bool> scrub_active;
198 
199  std::atomic<int64_t> requested_display_frame;
201  std::atomic<int64_t> cached_frame_count;
202 
203  std::atomic<int64_t> min_frames_ahead;
205 
210 
211  std::atomic<int64_t> last_cached_index;
212  mutable std::mutex seek_state_mutex;
213  };
214 
215 } // namespace openshot
216 
217 #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::seek_state_mutex
std::mutex seek_state_mutex
Protects coherent seek state updates/consumption.
Definition: VideoCacheThread.h:212
openshot::VideoCacheThread::StartThread
bool StartThread()
Start the cache thread at high priority. Returns true if it’s actually running.
Definition: VideoCacheThread.cpp:121
openshot::VideoCacheThread::last_speed
std::atomic< int > last_speed
Last non-zero speed (for tracking).
Definition: VideoCacheThread.h:192
openshot::VideoCacheThread::clampToTimelineRange
int64_t clampToTimelineRange(int64_t frame, int64_t timeline_end) const
Clamp frame index to [1, timeline_end] when timeline_end is valid.
Definition: VideoCacheThread.cpp:372
openshot::VideoCacheThread::preroll_on_next_fill
std::atomic< bool > preroll_on_next_fill
True if next cache rebuild should include preroll offset.
Definition: VideoCacheThread.h:195
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::VideoCacheThread::last_cached_index
std::atomic< int64_t > last_cached_index
Index of the most recently cached frame.
Definition: VideoCacheThread.h:211
openshot::VideoCacheThread::computeDirection
int computeDirection() const
Definition: VideoCacheThread.cpp:308
openshot::VideoCacheThread::reader
ReaderBase * reader
The source reader (e.g., Timeline, FFmpegReader).
Definition: VideoCacheThread.h:206
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:318
openshot::VideoCacheThread::computePrerollFrames
int64_t computePrerollFrames(const Settings *settings) const
Compute preroll frame count from settings.
Definition: VideoCacheThread.cpp:341
openshot::VideoCacheThread::last_cached_frame
std::shared_ptr< Frame > last_cached_frame
Last frame pointer added to cache.
Definition: VideoCacheThread.h:189
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:48
openshot::VideoCacheThread::cached_frame_count
std::atomic< int64_t > cached_frame_count
Estimated count of frames currently stored in cache.
Definition: VideoCacheThread.h:201
openshot::VideoCacheThread::clear_cache_on_next_fill
std::atomic< bool > clear_cache_on_next_fill
True if next cache loop should clear existing cache ranges.
Definition: VideoCacheThread.h:196
openshot::VideoCacheThread::prefetchWindow
bool prefetchWindow(CacheBase *cache, int64_t window_begin, int64_t window_end, int dir, ReaderBase *reader, int64_t max_frames_to_fetch=-1)
Prefetch all missing frames in [window_begin ... window_end] or [window_end ... window_begin].
Definition: VideoCacheThread.cpp:422
openshot::VideoCacheThread::~VideoCacheThread
~VideoCacheThread() override
Definition: VideoCacheThread.cpp:49
openshot::VideoCacheThread::timeline_max_frame
int64_t timeline_max_frame
Highest valid frame index in the timeline.
Definition: VideoCacheThread.h:204
openshot::VideoCacheThread
Handles prefetching and caching of video/audio frames for smooth playback.
Definition: VideoCacheThread.h:37
openshot::VideoCacheThread::resolveTimelineEnd
int64_t resolveTimelineEnd() const
Resolve timeline end frame from reader/timeline metadata.
Definition: VideoCacheThread.cpp:357
openshot::VideoCacheThread::userSeeked
std::atomic< bool > userSeeked
True if Seek(..., true) was called (forces a cache reset).
Definition: VideoCacheThread.h:194
openshot::VideoCacheThread::setSpeed
void setSpeed(int new_speed)
Set playback speed/direction. Positive = forward, negative = rewind, zero = pause.
Definition: VideoCacheThread.cpp:94
openshot::VideoCacheThread::seen_timeline_cache_epoch
uint64_t seen_timeline_cache_epoch
Last observed Timeline cache invalidation epoch.
Definition: VideoCacheThread.h:208
openshot::VideoCacheThread::speed
std::atomic< int > speed
Current playback speed (0=paused, >0 forward, <0 backward).
Definition: VideoCacheThread.h:191
openshot::VideoCacheThread::Reader
void Reader(ReaderBase *new_reader)
Attach a ReaderBase (e.g. Timeline, FFmpegReader) and begin caching.
Definition: VideoCacheThread.cpp:136
openshot::VideoCacheThread::requested_display_frame
std::atomic< int64_t > requested_display_frame
Frame index the user requested.
Definition: VideoCacheThread.h:199
openshot::VideoCacheThread::Stop
void Stop()
Stop method is unimplemented.
Definition: VideoCacheThread.h:51
openshot::VideoCacheThread::NotifyPlaybackPosition
void NotifyPlaybackPosition(int64_t new_position)
Update playback position without triggering seek behavior or cache invalidation.
Definition: VideoCacheThread.cpp:288
openshot::VideoCacheThread::timeline_cache_epoch_initialized
bool timeline_cache_epoch_initialized
True once an initial epoch snapshot has been taken.
Definition: VideoCacheThread.h:209
openshot::VideoCacheThread::run
void run() override
Thread entry point: loops until threadShouldExit() is true.
Definition: VideoCacheThread.cpp:475
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:107
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:380
openshot::VideoCacheThread::getSpeed
int getSpeed() const
Definition: VideoCacheThread.h:63
openshot::VideoCacheThread::current_display_frame
int64_t current_display_frame
Currently displayed frame (unused here, reserved).
Definition: VideoCacheThread.h:200
openshot::VideoCacheThread::last_dir
std::atomic< int > last_dir
Last direction sign (+1 forward, –1 backward).
Definition: VideoCacheThread.h:193
openshot::VideoCacheThread::force_directional_cache
bool force_directional_cache
(Reserved for future use).
Definition: VideoCacheThread.h:207
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:130
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:400
openshot::VideoCacheThread::scrub_active
std::atomic< bool > scrub_active
True while user is dragging/scrubbing the playhead.
Definition: VideoCacheThread.h:197
openshot::VideoCacheThread::min_frames_ahead
std::atomic< int64_t > min_frames_ahead
Minimum number of frames considered “ready” (pre-roll).
Definition: VideoCacheThread.h:203
openshot::VideoCacheThread::Seek
void Seek(int64_t new_position)
Backward-compatible alias for playback position updates (no seek side effects).
Definition: VideoCacheThread.cpp:283
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:324
openshot::VideoCacheThread::isReady
bool isReady()
Definition: VideoCacheThread.cpp:54