OpenShot Library | libopenshot 0.5.0
Loading...
Searching...
No Matches
VideoCacheThread.h
Go to the documentation of this file.
1
8
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
22namespace openshot
23{
24 using juce::Thread;
25
33 class VideoCacheThread : public Thread
34 {
35 public:
38 ~VideoCacheThread() override;
39
41 bool isReady();
42
44 void Play() {};
45
47 void Stop() {};
48
56 void setSpeed(int new_speed);
57
59 int getSpeed() const { return speed; }
60
62 void Seek(int64_t new_position);
63
69 void Seek(int64_t new_position, bool start_preroll);
70
72 bool StartThread();
73
75 bool StopThread(int timeoutMs = 0);
76
81 void Reader(ReaderBase* new_reader) { reader = new_reader; Play(); }
82
83 protected:
85 void run() override;
86
96 int64_t getBytes(int width, int height, int sample_rate, int channels, float fps);
97
98 //---------- Helper functions, broken out for clarity & unit testing ----------
99
101 int computeDirection() const;
102
108 void handleUserSeek(int64_t playhead, int dir);
109
117 bool clearCacheIfPaused(int64_t playhead, bool paused, CacheBase* cache);
118
132 void computeWindowBounds(int64_t playhead,
133 int dir,
134 int64_t ahead_count,
135 int64_t timeline_end,
136 int64_t& window_begin,
137 int64_t& window_end) const;
138
152 bool prefetchWindow(CacheBase* cache,
153 int64_t window_begin,
154 int64_t window_end,
155 int dir,
157
158 //---------- Internal state ----------
159
160 std::shared_ptr<Frame> last_cached_frame;
161
162 int speed;
166
170
173
176
178 };
179
180} // namespace openshot
181
182#endif // OPENSHOT_VIDEO_CACHE_THREAD_H
Header file for ReaderBase class.
All cache managers in libopenshot are based on this CacheBase class.
Definition CacheBase.h:35
This abstract class is the base class, used by all readers in libopenshot.
Definition ReaderBase.h:76
bool StopThread(int timeoutMs=0)
Stop the cache thread (wait up to timeoutMs ms). Returns true if it stopped.
void setSpeed(int new_speed)
Set playback speed/direction. Positive = forward, negative = rewind, zero = pause.
bool force_directional_cache
(Reserved for future use).
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].
int64_t requested_display_frame
Frame index the user requested.
int64_t timeline_max_frame
Highest valid frame index in the timeline.
void Play()
Play method is unimplemented.
void Stop()
Stop method is unimplemented.
bool clearCacheIfPaused(int64_t playhead, bool paused, CacheBase *cache)
When paused and playhead is outside current cache, clear all frames.
int64_t last_cached_index
Index of the most recently cached frame.
int64_t cached_frame_count
Estimated count of frames currently stored in cache.
void run() override
Thread entry point: loops until threadShouldExit() is true.
int last_speed
Last non-zero speed (for tracking).
int64_t current_display_frame
Currently displayed frame (unused here, reserved).
VideoCacheThread()
Constructor: initializes member variables and assumes forward direction on first launch.
void Reader(ReaderBase *new_reader)
Attach a ReaderBase (e.g. Timeline, FFmpegReader) and begin caching.
void handleUserSeek(int64_t playhead, int dir)
If userSeeked is true, reset last_cached_index just behind the playhead.
int64_t getBytes(int width, int height, int sample_rate, int channels, float fps)
Estimate memory usage for a single frame (video + audio).
ReaderBase * reader
The source reader (e.g., Timeline, FFmpegReader).
void Seek(int64_t new_position)
Seek to a specific frame (no preroll).
bool userSeeked
True if Seek(..., true) was called (forces a cache reset).
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.
int64_t min_frames_ahead
Minimum number of frames considered “ready” (pre-roll).
int last_dir
Last direction sign (+1 forward, –1 backward).
bool StartThread()
Start the cache thread at high priority. Returns true if it’s actually running.
int speed
Current playback speed (0=paused, >0 forward, <0 backward).
std::shared_ptr< Frame > last_cached_frame
Last frame pointer added to cache.
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29