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  , is_playing(false)
32  , userSeeked(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  // Play the video
51  {
52  is_playing = true;
53  }
54 
55  // Stop the video
57  {
58  is_playing = false;
59  }
60 
61  // Is cache ready for playback (pre-roll)
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  requested_display_frame = new_position;
113  }
114 
115  void VideoCacheThread::Seek(int64_t new_position)
116  {
117  Seek(new_position, false);
118  }
119 
121  {
122  // If speed ≠ 0, use its sign; if speed==0, keep last_dir
123  return (speed != 0 ? (speed > 0 ? 1 : -1) : last_dir);
124  }
125 
126  void VideoCacheThread::handleUserSeek(int64_t playhead, int dir)
127  {
128  // Place last_cached_index just “behind” playhead in the given dir
129  last_cached_index = playhead - dir;
130  }
131 
133  bool paused,
134  CacheBase* cache)
135  {
136  if (paused && !cache->Contains(playhead)) {
137  // If paused and playhead not in cache, clear everything
138  Timeline* timeline = static_cast<Timeline*>(reader);
139  timeline->ClearAllCache();
140  return true;
141  }
142  return false;
143  }
144 
146  int dir,
147  int64_t ahead_count,
148  int64_t timeline_end,
149  int64_t& window_begin,
150  int64_t& window_end) const
151  {
152  if (dir > 0) {
153  // Forward window: [playhead ... playhead + ahead_count]
154  window_begin = playhead;
155  window_end = playhead + ahead_count;
156  }
157  else {
158  // Backward window: [playhead - ahead_count ... playhead]
159  window_begin = playhead - ahead_count;
160  window_end = playhead;
161  }
162  // Clamp to [1 ... timeline_end]
163  window_begin = std::max<int64_t>(window_begin, 1);
164  window_end = std::min<int64_t>(window_end, timeline_end);
165  }
166 
168  int64_t window_begin,
169  int64_t window_end,
170  int dir,
171  ReaderBase* reader)
172  {
173  bool window_full = true;
174  int64_t next_frame = last_cached_index + dir;
175 
176  // Advance from last_cached_index toward window boundary
177  while ((dir > 0 && next_frame <= window_end) ||
178  (dir < 0 && next_frame >= window_begin))
179  {
180  if (threadShouldExit()) {
181  break;
182  }
183  // If a Seek was requested mid-caching, bail out immediately
184  if (userSeeked) {
185  break;
186  }
187 
188  if (!cache->Contains(next_frame)) {
189  // Frame missing, fetch and add
190  try {
191  auto framePtr = reader->GetFrame(next_frame);
192  cache->Add(framePtr);
194  }
195  catch (const OutOfBoundsFrame&) {
196  break;
197  }
198  window_full = false;
199  }
200  else {
201  cache->Touch(next_frame);
202  }
203 
204  last_cached_index = next_frame;
205  next_frame += dir;
206  }
207 
208  return window_full;
209  }
210 
212  {
213  using micro_sec = std::chrono::microseconds;
214  using double_micro_sec = std::chrono::duration<double, micro_sec::period>;
215 
216  while (!threadShouldExit()) {
217  Settings* settings = Settings::Instance();
218  CacheBase* cache = reader ? reader->GetCache() : nullptr;
219 
220  // If caching disabled or no reader, sleep briefly
221  if (!settings->ENABLE_PLAYBACK_CACHING || !cache || !is_playing) {
222  std::this_thread::sleep_for(double_micro_sec(50000));
223  continue;
224  }
225 
226  // init local vars
228 
229  Timeline* timeline = static_cast<Timeline*>(reader);
230  int64_t timeline_end = timeline->GetMaxFrame();
231  int64_t playhead = requested_display_frame;
232  bool paused = (speed == 0);
233 
234  // Compute effective direction (±1)
235  int dir = computeDirection();
236  if (speed != 0) {
237  last_dir = dir;
238  }
239 
240  // Compute bytes_per_frame, max_bytes, and capacity once
241  int64_t bytes_per_frame = getBytes(
242  (timeline->preview_width ? timeline->preview_width : reader->info.width),
243  (timeline->preview_height ? timeline->preview_height : reader->info.height),
247  );
248  int64_t max_bytes = cache->GetMaxBytes();
249  int64_t capacity = 0;
250  if (max_bytes > 0 && bytes_per_frame > 0) {
251  capacity = max_bytes / bytes_per_frame;
252  if (capacity > settings->VIDEO_CACHE_MAX_FRAMES) {
253  capacity = settings->VIDEO_CACHE_MAX_FRAMES;
254  }
255  }
256 
257  // Handle a user-initiated seek
258  if (userSeeked) {
259  handleUserSeek(playhead, dir);
260  userSeeked = false;
261  }
262  else if (!paused && capacity >= 1) {
263  // In playback mode, check if last_cached_index drifted outside the new window
264  int64_t base_ahead = static_cast<int64_t>(capacity * settings->VIDEO_CACHE_PERCENT_AHEAD);
265 
266  int64_t window_begin, window_end;
268  playhead,
269  dir,
270  base_ahead,
271  timeline_end,
272  window_begin,
273  window_end
274  );
275 
276  bool outside_window =
277  (dir > 0 && last_cached_index > window_end) ||
278  (dir < 0 && last_cached_index < window_begin);
279  if (outside_window) {
280  handleUserSeek(playhead, dir);
281  }
282  }
283 
284  // If capacity is insufficient, sleep and retry
285  if (capacity < 1) {
286  std::this_thread::sleep_for(double_micro_sec(50000));
287  continue;
288  }
289  int64_t ahead_count = static_cast<int64_t>(capacity *
290  settings->VIDEO_CACHE_PERCENT_AHEAD);
291 
292  // If paused and playhead is no longer in cache, clear everything
293  bool did_clear = clearCacheIfPaused(playhead, paused, cache);
294  if (did_clear) {
295  handleUserSeek(playhead, dir);
296  }
297 
298  // Compute the current caching window
299  int64_t window_begin, window_end;
300  computeWindowBounds(playhead,
301  dir,
302  ahead_count,
303  timeline_end,
304  window_begin,
305  window_end);
306 
307  // Attempt to fill any missing frames in that window
308  bool window_full = prefetchWindow(cache, window_begin, window_end, dir, reader);
309 
310  // If paused and window was already full, keep playhead fresh
311  if (paused && window_full) {
312  cache->Touch(playhead);
313  }
314 
315  // Sleep a short fraction of a frame interval
316  int64_t sleep_us = static_cast<int64_t>(
317  1000000.0 / reader->info.fps.ToFloat() / 4.0
318  );
319  std::this_thread::sleep_for(double_micro_sec(sleep_us));
320  }
321  }
322 
323 } // 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: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:167
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:173
openshot::VideoCacheThread::computeDirection
int computeDirection() const
Definition: VideoCacheThread.cpp:120
openshot::VideoCacheThread::reader
ReaderBase * reader
The source reader (e.g., Timeline, FFmpegReader).
Definition: VideoCacheThread.h:176
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:126
openshot::Timeline::ClearAllCache
void ClearAllCache(bool deep=false)
Definition: Timeline.cpp:1715
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::VideoCacheThread::Play
void Play()
Set is_playing = true, so run() will begin caching/playback.
Definition: VideoCacheThread.cpp:50
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:45
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:67
openshot::VideoCacheThread::userSeeked
bool userSeeked
True if Seek(..., true) was called (forces a cache reset).
Definition: VideoCacheThread.h:167
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::VideoCacheThread::Stop
void Stop()
Set is_playing = false, effectively pausing playback (caching still runs).
Definition: VideoCacheThread.cpp:56
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:211
openshot::VideoCacheThread::last_cached_index
int64_t last_cached_index
Index of the most recently cached frame.
Definition: VideoCacheThread.h:179
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:132
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:171
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: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:145
openshot::VideoCacheThread::is_playing
bool is_playing
True if playback is “running” (affects thread loop, not caching).
Definition: VideoCacheThread.h:166
openshot::VideoCacheThread::Seek
void Seek(int64_t new_position)
Seek to a specific frame (no preroll).
Definition: VideoCacheThread.cpp:115
openshot::VideoCacheThread::requested_display_frame
int64_t requested_display_frame
Frame index the user requested.
Definition: VideoCacheThread.h:169
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:62
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.