OpenShot Library | libopenshot  0.5.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  , preroll_on_next_fill(false)
33  , requested_display_frame(1)
34  , current_display_frame(1)
35  , cached_frame_count(0)
36  , min_frames_ahead(4)
37  , timeline_max_frame(0)
38  , reader(nullptr)
39  , force_directional_cache(false)
40  , last_cached_index(0)
41  {
42  }
43 
44  // Destructor
46  {
47  }
48 
49  // Is cache ready for playback (pre-roll)
51  {
52  if (!reader) {
53  return false;
54  }
55 
56  if (min_frames_ahead < 0) {
57  return true;
58  }
59 
60  int dir = computeDirection();
61  if (dir > 0) {
63  }
65  }
66 
67  void VideoCacheThread::setSpeed(int new_speed)
68  {
69  // Only update last_speed and last_dir when new_speed != 0
70  if (new_speed != 0) {
71  last_speed = new_speed;
72  last_dir = (new_speed > 0 ? 1 : -1);
73  }
74  speed = new_speed;
75  }
76 
77  // Get the size in bytes of a frame (rough estimate)
78  int64_t VideoCacheThread::getBytes(int width,
79  int height,
80  int sample_rate,
81  int channels,
82  float fps)
83  {
84  // RGBA video frame
85  int64_t bytes = static_cast<int64_t>(width) * height * sizeof(char) * 4;
86  // Approximate audio: (sample_rate * channels)/fps samples per frame
87  bytes += ((sample_rate * channels) / fps) * sizeof(float);
88  return bytes;
89  }
90 
93  {
94  // JUCE’s startThread() returns void, so we launch it and then check if
95  // the thread actually started:
96  startThread(Priority::high);
97  return isThreadRunning();
98  }
99 
101  bool VideoCacheThread::StopThread(int timeoutMs)
102  {
103  stopThread(timeoutMs);
104  return !isThreadRunning();
105  }
106 
107  void VideoCacheThread::Seek(int64_t new_position, bool start_preroll)
108  {
109  if (start_preroll) {
110  userSeeked = true;
111 
112  CacheBase* cache = reader ? reader->GetCache() : nullptr;
113 
114  if (cache && !cache->Contains(new_position))
115  {
116  // If user initiated seek, and current frame not found (
117  Timeline* timeline = static_cast<Timeline*>(reader);
118  timeline->ClearAllCache();
119  cached_frame_count = 0;
120  preroll_on_next_fill = true;
121  }
122  else if (cache)
123  {
124  cached_frame_count = cache->Count();
125  preroll_on_next_fill = false;
126  }
127  else {
128  preroll_on_next_fill = false;
129  }
130  }
131  requested_display_frame = new_position;
132  }
133 
134  void VideoCacheThread::Seek(int64_t new_position)
135  {
136  Seek(new_position, false);
137  }
138 
140  {
141  // If speed ≠ 0, use its sign; if speed==0, keep last_dir
142  return (speed != 0 ? (speed > 0 ? 1 : -1) : last_dir);
143  }
144 
145  void VideoCacheThread::handleUserSeek(int64_t playhead, int dir)
146  {
147  // Place last_cached_index just “behind” playhead in the given dir
148  last_cached_index = playhead - dir;
149  }
150 
152  int dir,
153  int64_t timeline_end,
154  int64_t preroll_frames)
155  {
156  int64_t preroll_start = playhead;
157  if (preroll_frames > 0) {
158  if (dir > 0) {
159  preroll_start = std::max<int64_t>(1, playhead - preroll_frames);
160  }
161  else {
162  preroll_start = std::min<int64_t>(timeline_end, playhead + preroll_frames);
163  }
164  }
165  last_cached_index = preroll_start - dir;
166  }
167 
168  int64_t VideoCacheThread::computePrerollFrames(const Settings* settings) const
169  {
170  if (!settings) {
171  return 0;
172  }
173  int64_t min_frames = settings->VIDEO_CACHE_MIN_PREROLL_FRAMES;
174  int64_t max_frames = settings->VIDEO_CACHE_MAX_PREROLL_FRAMES;
175  if (min_frames < 0) {
176  return 0;
177  }
178  if (max_frames > 0 && min_frames > max_frames) {
179  min_frames = max_frames;
180  }
181  return min_frames;
182  }
183 
185  bool paused,
186  CacheBase* cache)
187  {
188  if (paused && !cache->Contains(playhead)) {
189  // If paused and playhead not in cache, clear everything
190  Timeline* timeline = static_cast<Timeline*>(reader);
191  timeline->ClearAllCache();
192  cached_frame_count = 0;
193  return true;
194  }
195  return false;
196  }
197 
199  int dir,
200  int64_t ahead_count,
201  int64_t timeline_end,
202  int64_t& window_begin,
203  int64_t& window_end) const
204  {
205  if (dir > 0) {
206  // Forward window: [playhead ... playhead + ahead_count]
207  window_begin = playhead;
208  window_end = playhead + ahead_count;
209  }
210  else {
211  // Backward window: [playhead - ahead_count ... playhead]
212  window_begin = playhead - ahead_count;
213  window_end = playhead;
214  }
215  // Clamp to [1 ... timeline_end]
216  window_begin = std::max<int64_t>(window_begin, 1);
217  window_end = std::min<int64_t>(window_end, timeline_end);
218  }
219 
221  int64_t window_begin,
222  int64_t window_end,
223  int dir,
224  ReaderBase* reader)
225  {
226  bool window_full = true;
227  int64_t next_frame = last_cached_index + dir;
228 
229  // Advance from last_cached_index toward window boundary
230  while ((dir > 0 && next_frame <= window_end) ||
231  (dir < 0 && next_frame >= window_begin))
232  {
233  if (threadShouldExit()) {
234  break;
235  }
236  // If a Seek was requested mid-caching, bail out immediately
237  if (userSeeked) {
238  break;
239  }
240 
241  if (!cache->Contains(next_frame)) {
242  // Frame missing, fetch and add
243  try {
244  auto framePtr = reader->GetFrame(next_frame);
245  cache->Add(framePtr);
246  cached_frame_count = cache->Count();
247  }
248  catch (const OutOfBoundsFrame&) {
249  break;
250  }
251  window_full = false;
252  }
253  else {
254  cache->Touch(next_frame);
255  }
256 
257  last_cached_index = next_frame;
258  next_frame += dir;
259  }
260 
261  return window_full;
262  }
263 
265  {
266  using micro_sec = std::chrono::microseconds;
267  using double_micro_sec = std::chrono::duration<double, micro_sec::period>;
268 
269  while (!threadShouldExit()) {
270  Settings* settings = Settings::Instance();
271  CacheBase* cache = reader ? reader->GetCache() : nullptr;
272 
273  // If caching disabled or no reader, mark cache as ready and sleep briefly
274  if (!settings->ENABLE_PLAYBACK_CACHING || !cache) {
275  cached_frame_count = (cache ? cache->Count() : 0);
276  min_frames_ahead = -1;
277  std::this_thread::sleep_for(double_micro_sec(50000));
278  continue;
279  }
280 
281  // init local vars
283 
284  Timeline* timeline = static_cast<Timeline*>(reader);
285  int64_t timeline_end = timeline->GetMaxFrame();
286  int64_t playhead = requested_display_frame;
287  bool paused = (speed == 0);
288  int64_t preroll_frames = computePrerollFrames(settings);
289 
290  cached_frame_count = cache->Count();
291 
292  // Compute effective direction (±1)
293  int dir = computeDirection();
294  if (speed != 0) {
295  last_dir = dir;
296  }
297 
298  // Compute bytes_per_frame, max_bytes, and capacity once
299  int64_t bytes_per_frame = getBytes(
300  (timeline->preview_width ? timeline->preview_width : reader->info.width),
301  (timeline->preview_height ? timeline->preview_height : reader->info.height),
305  );
306  int64_t max_bytes = cache->GetMaxBytes();
307  int64_t capacity = 0;
308  if (max_bytes > 0 && bytes_per_frame > 0) {
309  capacity = max_bytes / bytes_per_frame;
310  if (capacity > settings->VIDEO_CACHE_MAX_FRAMES) {
311  capacity = settings->VIDEO_CACHE_MAX_FRAMES;
312  }
313  }
314 
315  // Handle a user-initiated seek
316  bool use_preroll = preroll_on_next_fill;
317  if (userSeeked) {
318  if (use_preroll) {
319  handleUserSeekWithPreroll(playhead, dir, timeline_end, preroll_frames);
320  }
321  else {
322  handleUserSeek(playhead, dir);
323  }
324  userSeeked = false;
325  preroll_on_next_fill = false;
326  }
327  else if (!paused && capacity >= 1) {
328  // In playback mode, check if last_cached_index drifted outside the new window
329  int64_t base_ahead = static_cast<int64_t>(capacity * settings->VIDEO_CACHE_PERCENT_AHEAD);
330 
331  int64_t window_begin, window_end;
333  playhead,
334  dir,
335  base_ahead,
336  timeline_end,
337  window_begin,
338  window_end
339  );
340 
341  bool outside_window =
342  (dir > 0 && last_cached_index > window_end) ||
343  (dir < 0 && last_cached_index < window_begin);
344  if (outside_window) {
345  handleUserSeek(playhead, dir);
346  }
347  }
348 
349  // If capacity is insufficient, sleep and retry
350  if (capacity < 1) {
351  std::this_thread::sleep_for(double_micro_sec(50000));
352  continue;
353  }
354  int64_t ahead_count = static_cast<int64_t>(capacity *
355  settings->VIDEO_CACHE_PERCENT_AHEAD);
356  int64_t window_size = ahead_count + 1;
357  if (window_size < 1) {
358  window_size = 1;
359  }
360  int64_t ready_target = window_size - 1;
361  if (ready_target < 0) {
362  ready_target = 0;
363  }
364  int64_t configured_min = settings->VIDEO_CACHE_MIN_PREROLL_FRAMES;
365  min_frames_ahead = std::min<int64_t>(configured_min, ready_target);
366 
367  // If paused and playhead is no longer in cache, clear everything
368  bool did_clear = clearCacheIfPaused(playhead, paused, cache);
369  if (did_clear) {
370  handleUserSeekWithPreroll(playhead, dir, timeline_end, preroll_frames);
371  }
372 
373  // Compute the current caching window
374  int64_t window_begin, window_end;
375  computeWindowBounds(playhead,
376  dir,
377  ahead_count,
378  timeline_end,
379  window_begin,
380  window_end);
381 
382  // Attempt to fill any missing frames in that window
383  bool window_full = prefetchWindow(cache, window_begin, window_end, dir, reader);
384 
385  // If paused and window was already full, keep playhead fresh
386  if (paused && window_full) {
387  cache->Touch(playhead);
388  }
389 
390  // Sleep a short fraction of a frame interval
391  int64_t sleep_us = static_cast<int64_t>(
392  1000000.0 / reader->info.fps.ToFloat() / 4.0
393  );
394  std::this_thread::sleep_for(double_micro_sec(sleep_us));
395  }
396  }
397 
398 } // 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:89
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:92
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:220
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: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::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:145
openshot::Timeline::ClearAllCache
void ClearAllCache(bool deep=false)
Definition: Timeline.cpp:1749
openshot::VideoCacheThread::computePrerollFrames
int64_t computePrerollFrames(const Settings *settings) const
Compute preroll frame count from settings.
Definition: VideoCacheThread.cpp:168
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:101
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:98
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:45
openshot::ReaderInfo::height
int height
The height of the video (in pixels)
Definition: ReaderBase.h:45
openshot::Settings::VIDEO_CACHE_MAX_PREROLL_FRAMES
int VIDEO_CACHE_MAX_PREROLL_FRAMES
Max number of frames (ahead of playhead) to cache during playback.
Definition: Settings.h:95
openshot::VideoCacheThread::last_speed
int last_speed
Last non-zero speed (for tracking).
Definition: VideoCacheThread.h:179
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:92
openshot::Timeline
This class represents a timeline.
Definition: Timeline.h:154
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::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:264
openshot::CacheBase::Count
virtual int64_t Count()=0
Count the frames in the queue.
openshot::VideoCacheThread::last_cached_index
int64_t last_cached_index
Index of the most recently cached frame.
Definition: VideoCacheThread.h:194
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:78
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::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::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::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:477
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::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:50
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.
openshot::VideoCacheThread::preroll_on_next_fill
bool preroll_on_next_fill
True if next cache rebuild should include preroll offset.
Definition: VideoCacheThread.h:182