13#ifndef OPENSHOT_VIDEO_CACHE_THREAD_H
14#define OPENSHOT_VIDEO_CACHE_THREAD_H
20#include <juce_audio_basics/juce_audio_basics.h>
71 void Seek(int64_t new_position);
78 void Seek(int64_t new_position,
bool start_preroll);
115 int64_t
getBytes(
int width,
int height,
int sample_rate,
int channels,
float fps);
138 int64_t timeline_end,
139 int64_t preroll_frames);
175 int64_t timeline_end,
176 int64_t& window_begin,
177 int64_t& window_end)
const;
193 int64_t window_begin,
197 int64_t max_frames_to_fetch = -1,
198 uint64_t generation = UINT64_MAX);
Adaptive preview pre-roll policy.
Header file for ReaderBase class.
All cache managers in libopenshot are based on this CacheBase class.
This abstract class is the base class, used by all readers in libopenshot.
This class is contains settings used by libopenshot (and can be safely toggled at any point)
Handles prefetching and caching of video/audio frames for smooth playback.
std::atomic< bool > diagnostics_enabled
int64_t ObsoleteCompletionUs() const
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).
std::atomic< int64_t > acknowledgement_us
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.
~VideoCacheThread() override
int64_t resolveTimelineEnd() const
Resolve timeline end frame from reader/timeline metadata.
int64_t timeline_max_frame
Highest valid frame index in the timeline.
void Play()
Play method is unimplemented.
int64_t readiness_bytes_per_frame
int64_t clampToTimelineRange(int64_t frame, int64_t timeline_end) const
Clamp frame index to [1, timeline_end] when timeline_end is valid.
void Stop()
Stop method is unimplemented.
std::atomic< bool > preroll_on_next_fill
True if next cache rebuild should include preroll offset.
bool clearCacheIfPaused(int64_t playhead, bool paused, CacheBase *cache)
When paused and playhead is outside current cache, clear all frames.
std::atomic< int64_t > last_cached_index
Index of the most recently cached frame.
std::atomic< int > speed
Current playback speed (0=paused, >0 forward, <0 backward).
void updateReadiness(CacheBase *cache, int64_t playhead, int dir)
Worker-only cache probing; never runs in the audio callback.
void run() override
Thread entry point: loops until threadShouldExit() is true.
int64_t readiness_minimum_step
int64_t computePrerollFrames(const Settings *settings) const
Compute preroll frame count from settings.
std::atomic< uint64_t > processed_generation
Last request whose scheduling baseline was consumed.
bool isReady()
Shared startup gate; bounded fallback resumes established video decoding.
std::mutex seek_state_mutex
Protects coherent seek state updates/consumption.
uint64_t readiness_generation
Request covered by the readiness snapshot.
bool prefetchWindow(CacheBase *cache, int64_t window_begin, int64_t window_end, int dir, ReaderBase *reader, int64_t max_frames_to_fetch=-1, uint64_t generation=UINT64_MAX)
Prefetch all missing frames in [window_begin ... window_end] or [window_end ... window_begin].
std::atomic< bool > cache_audio_only
int64_t current_display_frame
Currently displayed frame (unused here, reserved).
void NotifyAudioCacheMiss()
VideoCacheThread()
Constructor: initializes member variables and assumes forward direction on first launch.
uint64_t seen_timeline_cache_epoch
Last observed Timeline cache invalidation epoch.
std::atomic< int64_t > requested_display_frame
Frame index the user requested.
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).
int64_t readiness_timeline_end
std::atomic< int64_t > first_current_frame_us
AdaptivePreroll preroll_policy
void Seek(int64_t new_position)
Backward-compatible alias for playback position updates (no seek side effects).
std::atomic< int64_t > request_time_us
void EnableRequestDiagnostics(bool enabled)
std::atomic< int64_t > cached_frame_count
Estimated count of frames currently stored in cache.
std::atomic< int > last_dir
Last direction sign (+1 forward, –1 backward).
int64_t readiness_physical_capacity
std::atomic< int64_t > min_frames_ahead
Minimum number of frames considered “ready” (pre-roll).
std::mutex lifecycle_mutex
Start/stop/reader drain; never acquired by worker.
std::atomic< bool > userSeeked
True if Seek(..., true) was called (forces a cache reset).
std::atomic< bool > audio_cache_miss
int64_t readiness_capacity
std::atomic< uint64_t > request_generation
Invalidates obsolete fills, including seeks retaining cache.
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 CancellationAcknowledgementUs() const
std::atomic< int64_t > obsolete_completion_us
void acknowledgeRequest(int64_t started_us)
static int64_t monotonicUs()
bool StartThread()
Start the cache thread at high priority. Returns true if it’s actually running.
bool timeline_cache_epoch_initialized
True once an initial epoch snapshot has been taken.
std::atomic< bool > scrub_active
True while user is dragging/scrubbing the playhead.
int computeDirection() const
bool UsesCachedAudio() const
Callback miss holds the shared gate until the player resynchronizes audio.
int64_t observed_frame_bytes
void NotifyPlaybackPosition(int64_t new_position)
Update playback position without triggering seek behavior or cache invalidation.
std::atomic< int > last_speed
Last non-zero speed (for tracking).
std::shared_ptr< Frame > last_cached_frame
Last frame pointer added to cache.
void AcknowledgeAudioCacheMiss()
std::atomic< bool > clear_cache_on_next_fill
True if next cache loop should clear existing cache ranges.
int64_t FirstCurrentFrameUs() const
int64_t readiness_playhead
This namespace is the default namespace for all code in the openshot library.