OpenShot Library | libopenshot  0.4.0
VideoCacheThread.cpp
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 #include "VideoCacheThread.h"
14 #include "CacheBase.h"
15 #include "Exceptions.h"
16 #include "Frame.h"
17 #include "Settings.h"
18 #include "Timeline.h"
19 #include <thread>
20 #include <chrono>
21 #include <algorithm>
22 
23 namespace openshot
24 {
25  // Constructor
27  : Thread("video-cache")
28  , speed(0)
29  , last_speed(1)
30  , last_dir(1) // assume forward (+1) on first launch
31  , userSeeked(false)
32  , requested_display_frame(1)
33  , current_display_frame(1)
34  , cached_frame_count(0)
35  , min_frames_ahead(4)
36  , timeline_max_frame(0)
37  , reader(nullptr)
38  , force_directional_cache(false)
39  , last_cached_index(0)
40  {
41  }
42 
43  // Destructor
45  {
46  }
47 
48  // Is cache ready for playback (pre-roll)
50  {
52  }
53 
54  void VideoCacheThread::setSpeed(int new_speed)
55  {
56  // Only update last_speed and last_dir when new_speed != 0
57  if (new_speed != 0) {
58  last_speed = new_speed;
59  last_dir = (new_speed > 0 ? 1 : -1);
60  }
61  speed = new_speed;
62  }
63 
64  // Get the size in bytes of a frame (rough estimate)
65  int64_t VideoCacheThread::getBytes(int width,
66  int height,
67  int sample_rate,
68  int channels,
69  float fps)
70  {
71  // RGBA video frame
72  int64_t bytes = static_cast<int64_t>(width) * height * sizeof(char) * 4;
73  // Approximate audio: (sample_rate * channels)/fps samples per frame
74  bytes += ((sample_rate * channels) / fps) * sizeof(float);
75  return bytes;
76  }
77 
80  {
81  // JUCE’s startThread() returns void, so we launch it and then check if
82  // the thread actually started:
83  startThread(Priority::high);
84  return isThreadRunning();
85  }
86 
88  bool VideoCacheThread::StopThread(int timeoutMs)
89  {
90  stopThread(timeoutMs);
91  return !isThreadRunning();
92  }
93 
94  void VideoCacheThread::Seek(int64_t new_position, bool start_preroll)
95  {
96  if (start_preroll) {
97  userSeeked = true;
98 
99  if (!reader->GetCache()->Contains(new_position))
100  {
101  // If user initiated seek, and current frame not found (
102  Timeline* timeline = static_cast<Timeline*>(reader);
103  timeline->ClearAllCache();
104  }
105  }
106  requested_display_frame = new_position;
107  }
108 
109  void VideoCacheThread::Seek(int64_t new_position)
110  {
111  Seek(new_position, false);
112  }
113 
115  {
116  // If speed ≠ 0, use its sign; if speed==0, keep last_dir
117  return (speed != 0 ? (speed > 0 ? 1 : -1) : last_dir);
118  }
119 
120  void VideoCacheThread::handleUserSeek(int64_t playhead, int dir)
121  {
122  // Place last_cached_index just “behind” playhead in the given dir
123  last_cached_index = playhead - dir;
124  }
125 
127  bool paused,
128  CacheBase* cache)
129  {
130  if (paused && !cache->Contains(playhead)) {
131  // If paused and playhead not in cache, clear everything
132  Timeline* timeline = static_cast<Timeline*>(reader);
133  timeline->ClearAllCache();
134  return true;
135  }
136  return false;
137  }
138 
140  int dir,
141  int64_t ahead_count,
142  int64_t timeline_end,
143  int64_t& window_begin,
144  int64_t& window_end) const
145  {
146  if (dir > 0) {
147  // Forward window: [playhead ... playhead + ahead_count]
148  window_begin = playhead;
149  window_end = playhead + ahead_count;
150  }
151  else {
152  // Backward window: [playhead - ahead_count ... playhead]
153  window_begin = playhead - ahead_count;
154  window_end = playhead;
155  }
156  // Clamp to [1 ... timeline_end]
157  window_begin = std::max<int64_t>(window_begin, 1);
158  window_end = std::min<int64_t>(window_end, timeline_end);
159  }
160 
162  int64_t window_begin,
163  int64_t window_end,
164  int dir,
165  ReaderBase* reader)
166  {
167  bool window_full = true;
168  int64_t next_frame = last_cached_index + dir;
169 
170  // Advance from last_cached_index toward window boundary
171  while ((dir > 0 && next_frame <= window_end) ||
172  (dir < 0 && next_frame >= window_begin))
173  {
174  if (threadShouldExit()) {
175  break;
176  }
177  // If a Seek was requested mid-caching, bail out immediately
178  if (userSeeked) {
179  break;
180  }
181 
182  if (!cache->Contains(next_frame)) {
183  // Frame missing, fetch and add
184  try {
185  auto framePtr = reader->GetFrame(next_frame);
186  cache->Add(framePtr);
188  }
189  catch (const OutOfBoundsFrame&) {
190  break;
191  }
192  window_full = false;
193  }
194  else {
195  cache->Touch(next_frame);
196  }
197 
198  last_cached_index = next_frame;
199  next_frame += dir;
200  }
201 
202  return window_full;
203  }
204 
206  {
207  using micro_sec = std::chrono::microseconds;
208  using double_micro_sec = std::chrono::duration<double, micro_sec::period>;
209 
210  while (!threadShouldExit()) {
211  Settings* settings = Settings::Instance();
212  CacheBase* cache = reader ? reader->GetCache() : nullptr;
213 
214  // If caching disabled or no reader, sleep briefly
215  if (!settings->ENABLE_PLAYBACK_CACHING || !cache) {
216  std::this_thread::sleep_for(double_micro_sec(50000));
217  continue;
218  }
219 
220  // init local vars
222 
223  Timeline* timeline = static_cast<Timeline*>(reader);
224  int64_t timeline_end = timeline->GetMaxFrame();
225  int64_t playhead = requested_display_frame;
226  bool paused = (speed == 0);
227 
228  // Compute effective direction (±1)
229  int dir = computeDirection();
230  if (speed != 0) {
231  last_dir = dir;
232  }
233 
234  // Compute bytes_per_frame, max_bytes, and capacity once
235  int64_t bytes_per_frame = getBytes(
236  (timeline->preview_width ? timeline->preview_width : reader->info.width),
237  (timeline->preview_height ? timeline->preview_height : reader->info.height),
241  );
242  int64_t max_bytes = cache->GetMaxBytes();
243  int64_t capacity = 0;
244  if (max_bytes > 0 && bytes_per_frame > 0) {
245  capacity = max_bytes / bytes_per_frame;
246  if (capacity > settings->VIDEO_CACHE_MAX_FRAMES) {
247  capacity = settings->VIDEO_CACHE_MAX_FRAMES;
248  }
249  }
250 
251  // Handle a user-initiated seek
252  if (userSeeked) {
253  handleUserSeek(playhead, dir);
254  userSeeked = false;
255  }
256  else if (!paused && capacity >= 1) {
257  // In playback mode, check if last_cached_index drifted outside the new window
258  int64_t base_ahead = static_cast<int64_t>(capacity * settings->VIDEO_CACHE_PERCENT_AHEAD);
259 
260  int64_t window_begin, window_end;
262  playhead,
263  dir,
264  base_ahead,
265  timeline_end,
266  window_begin,
267  window_end
268  );
269 
270  bool outside_window =
271  (dir > 0 && last_cached_index > window_end) ||
272  (dir < 0 && last_cached_index < window_begin);
273  if (outside_window) {
274  handleUserSeek(playhead, dir);
275  }
276  }
277 
278  // If capacity is insufficient, sleep and retry
279  if (capacity < 1) {
280  std::this_thread::sleep_for(double_micro_sec(50000));
281  continue;
282  }
283  int64_t ahead_count = static_cast<int64_t>(capacity *
284  settings->VIDEO_CACHE_PERCENT_AHEAD);
285 
286  // If paused and playhead is no longer in cache, clear everything
287  bool did_clear = clearCacheIfPaused(playhead, paused, cache);
288  if (did_clear) {
289  handleUserSeek(playhead, dir);
290  }
291 
292  // Compute the current caching window
293  int64_t window_begin, window_end;
294  computeWindowBounds(playhead,
295  dir,
296  ahead_count,
297  timeline_end,
298  window_begin,
299  window_end);
300 
301  // Attempt to fill any missing frames in that window
302  bool window_full = prefetchWindow(cache, window_begin, window_end, dir, reader);
303 
304  // If paused and window was already full, keep playhead fresh
305  if (paused && window_full) {
306  cache->Touch(playhead);
307  }
308 
309  // Sleep a short fraction of a frame interval
310  int64_t sleep_us = static_cast<int64_t>(
311  1000000.0 / reader->info.fps.ToFloat() / 4.0
312  );
313  std::this_thread::sleep_for(double_micro_sec(sleep_us));
314  }
315  }
316 
317 } // namespace openshot
Settings.h
Header file for global Settings class.
openshot::ReaderInfo::sample_rate
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:60
openshot::VideoCacheThread::VideoCacheThread
VideoCacheThread()
Constructor: initializes member variables and assumes forward direction on first launch.
Definition: VideoCacheThread.cpp:26
openshot::Fraction::ToFloat
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:35
openshot::Settings::VIDEO_CACHE_PERCENT_AHEAD
float VIDEO_CACHE_PERCENT_AHEAD
Percentage of cache in front of the playhead (0.0 to 1.0)
Definition: Settings.h:86
openshot::TimelineBase::preview_width
int preview_width
Optional preview width of timeline image. If your preview window is smaller than the timeline,...
Definition: TimelineBase.h:44
openshot::VideoCacheThread::StartThread
bool StartThread()
Start the cache thread at high priority. Returns true if it’s actually running.
Definition: VideoCacheThread.cpp:79
openshot::ReaderBase::GetFrame
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
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:161
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::TimelineBase::preview_height
int preview_height
Optional preview width of timeline image. If your preview window is smaller than the timeline,...
Definition: TimelineBase.h:45
openshot::CacheBase::Add
virtual void Add(std::shared_ptr< openshot::Frame > frame)=0
Add a Frame to the cache.
openshot::VideoCacheThread::min_frames_ahead
int64_t min_frames_ahead
Minimum number of frames considered “ready” (pre-roll).
Definition: VideoCacheThread.h:171
openshot::VideoCacheThread::computeDirection
int computeDirection() const
Definition: VideoCacheThread.cpp:114
openshot::VideoCacheThread::reader
ReaderBase * reader
The source reader (e.g., Timeline, FFmpegReader).
Definition: VideoCacheThread.h:174
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:88
openshot::Settings
This class is contains settings used by libopenshot (and can be safely toggled at any point)
Definition: Settings.h:26
Timeline.h
Header file for Timeline class.
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:120
openshot::Timeline::ClearAllCache
void ClearAllCache(bool deep=false)
Definition: Timeline.cpp:1713
openshot::Settings::ENABLE_PLAYBACK_CACHING
bool ENABLE_PLAYBACK_CACHING
Enable/Disable the cache thread to pre-fetch and cache video frames before we need them.
Definition: Settings.h:98
openshot::ReaderInfo::width
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:46
openshot::CacheBase
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:34
openshot::Settings::VIDEO_CACHE_MAX_FRAMES
int VIDEO_CACHE_MAX_FRAMES
Max number of frames (when paused) to cache for playback.
Definition: Settings.h:95
CacheBase.h
Header file for CacheBase class.
openshot::OutOfBoundsFrame
Exception for frames that are out of bounds.
Definition: Exceptions.h:300
openshot::VideoCacheThread::~VideoCacheThread
~VideoCacheThread() override
Definition: VideoCacheThread.cpp:44
openshot::ReaderInfo::height
int height
The height of the video (in pixels)
Definition: ReaderBase.h:45
openshot::VideoCacheThread::last_speed
int last_speed
Last non-zero speed (for tracking).
Definition: VideoCacheThread.h:163
openshot::Settings::VIDEO_CACHE_MIN_PREROLL_FRAMES
int VIDEO_CACHE_MIN_PREROLL_FRAMES
Minimum number of frames to cache before playback begins.
Definition: Settings.h:89
openshot::Timeline
This class represents a timeline.
Definition: Timeline.h:148
openshot::VideoCacheThread::setSpeed
void setSpeed(int new_speed)
Set playback speed/direction. Positive = forward, negative = rewind, zero = pause.
Definition: VideoCacheThread.cpp:54
openshot::VideoCacheThread::userSeeked
bool userSeeked
True if Seek(..., true) was called (forces a cache reset).
Definition: VideoCacheThread.h:165
openshot::VideoCacheThread::speed
int speed
Current playback speed (0=paused, >0 forward, <0 backward).
Definition: VideoCacheThread.h:162
openshot::Settings::Instance
static Settings * Instance()
Create or get an instance of this logger singleton (invoke the class with this method)
Definition: Settings.cpp:23
openshot::CacheBase::Touch
virtual void Touch(int64_t frame_number)=0
Move frame to front of queue (so it lasts longer)
Frame.h
Header file for Frame class.
openshot::VideoCacheThread::run
void run() override
Thread entry point: loops until threadShouldExit() is true.
Definition: VideoCacheThread.cpp:205
openshot::VideoCacheThread::last_cached_index
int64_t last_cached_index
Index of the most recently cached frame.
Definition: VideoCacheThread.h:177
VideoCacheThread.h
Header file for VideoCacheThread class.
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:65
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:126
openshot::VideoCacheThread::last_dir
int last_dir
Last direction sign (+1 forward, –1 backward).
Definition: VideoCacheThread.h:164
openshot::VideoCacheThread::cached_frame_count
int64_t cached_frame_count
Count of frames currently added to cache.
Definition: VideoCacheThread.h:169
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:88
openshot::CacheBase::GetMaxBytes
int64_t GetMaxBytes()
Gets the maximum bytes value.
Definition: CacheBase.h:101
openshot::ReaderInfo::fps
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:48
openshot::CacheBase::Contains
virtual bool Contains(int64_t frame_number)=0
Check if frame is already contained in cache.
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:75
openshot::Timeline::GetMaxFrame
int64_t GetMaxFrame()
Look up the end frame number of the latest element on the timeline.
Definition: Timeline.cpp:469
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:139
openshot::VideoCacheThread::Seek
void Seek(int64_t new_position)
Seek to a specific frame (no preroll).
Definition: VideoCacheThread.cpp:109
openshot::VideoCacheThread::requested_display_frame
int64_t requested_display_frame
Frame index the user requested.
Definition: VideoCacheThread.h:167
openshot::ReaderInfo::channels
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:61
openshot::VideoCacheThread::isReady
bool isReady()
Definition: VideoCacheThread.cpp:49
openshot::ReaderBase::GetCache
virtual openshot::CacheBase * GetCache()=0
Get the cache object used by this reader (note: not all readers use cache)
Exceptions.h
Header file for all Exception classes.