| 
    OpenShot Library | libopenshot
    0.4.0
    
   | 
 
 
 
 
Go to the documentation of this file.
   27         : Thread(
"video-cache")
 
   32         , requested_display_frame(1)
 
   33         , current_display_frame(1)
 
   34         , cached_frame_count(0)
 
   36         , timeline_max_frame(0)
 
   38         , force_directional_cache(false)
 
   39         , last_cached_index(0)
 
   72         int64_t bytes = 
static_cast<int64_t
>(width) * height * 
sizeof(
char) * 4;
 
   74         bytes += ((sample_rate * channels) / fps) * 
sizeof(float);
 
   83         startThread(Priority::high);
 
   84         return isThreadRunning();
 
   90         stopThread(timeoutMs);
 
   91         return !isThreadRunning();
 
  111         Seek(new_position, 
false);
 
  130         if (paused && !cache->
Contains(playhead)) {
 
  142                                                int64_t timeline_end,
 
  143                                                int64_t& window_begin,
 
  144                                                int64_t& window_end)
 const 
  148             window_begin = playhead;
 
  149             window_end   = playhead + ahead_count;
 
  153             window_begin = playhead - ahead_count;
 
  154             window_end   = playhead;
 
  157         window_begin = std::max<int64_t>(window_begin, 1);
 
  158         window_end   = std::min<int64_t>(window_end, timeline_end);
 
  162                                           int64_t window_begin,
 
  167         bool window_full = 
true;
 
  171         while ((dir > 0 && next_frame <= window_end) ||
 
  172                (dir < 0 && next_frame >= window_begin))
 
  174             if (threadShouldExit()) {
 
  186                     cache->
Add(framePtr);
 
  195                 cache->
Touch(next_frame);
 
  207         using micro_sec        = std::chrono::microseconds;
 
  208         using double_micro_sec = std::chrono::duration<double, micro_sec::period>;
 
  210         while (!threadShouldExit()) {
 
  216                 std::this_thread::sleep_for(double_micro_sec(50000));
 
  226             bool     paused       = (
speed == 0);
 
  243             int64_t capacity  = 0;
 
  244             if (max_bytes > 0 && bytes_per_frame > 0) {
 
  245                 capacity = max_bytes / bytes_per_frame;
 
  256             else if (!paused && capacity >= 1) {
 
  260                 int64_t window_begin, window_end;
 
  270                 bool outside_window =
 
  273                 if (outside_window) {
 
  280                 std::this_thread::sleep_for(double_micro_sec(50000));
 
  283             int64_t ahead_count = 
static_cast<int64_t
>(capacity *
 
  293             int64_t window_begin, window_end;
 
  305             if (paused && window_full) {
 
  306                 cache->
Touch(playhead);
 
  310             int64_t sleep_us = 
static_cast<int64_t
>(
 
  313             std::this_thread::sleep_for(double_micro_sec(sleep_us));
 
  
Header file for global Settings class.
 
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
 
VideoCacheThread()
Constructor: initializes member variables and assumes forward direction on first launch.
 
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
 
float VIDEO_CACHE_PERCENT_AHEAD
Percentage of cache in front of the playhead (0.0 to 1.0)
 
int preview_width
Optional preview width of timeline image. If your preview window is smaller than the timeline,...
 
bool StartThread()
Start the cache thread at high priority. Returns true if it’s actually running.
 
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
 
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].
 
This namespace is the default namespace for all code in the openshot library.
 
int preview_height
Optional preview width of timeline image. If your preview window is smaller than the timeline,...
 
virtual void Add(std::shared_ptr< openshot::Frame > frame)=0
Add a Frame to the cache.
 
int64_t min_frames_ahead
Minimum number of frames considered “ready” (pre-roll).
 
int computeDirection() const
 
ReaderBase * reader
The source reader (e.g., Timeline, FFmpegReader).
 
openshot::ReaderInfo info
Information about the current media file.
 
This class is contains settings used by libopenshot (and can be safely toggled at any point)
 
Header file for Timeline class.
 
void handleUserSeek(int64_t playhead, int dir)
If userSeeked is true, reset last_cached_index just behind the playhead.
 
void ClearAllCache(bool deep=false)
 
bool ENABLE_PLAYBACK_CACHING
Enable/Disable the cache thread to pre-fetch and cache video frames before we need them.
 
int width
The width of the video (in pixesl)
 
All cache managers in libopenshot are based on this CacheBase class.
 
int VIDEO_CACHE_MAX_FRAMES
Max number of frames (when paused) to cache for playback.
 
Header file for CacheBase class.
 
Exception for frames that are out of bounds.
 
~VideoCacheThread() override
 
int height
The height of the video (in pixels)
 
int last_speed
Last non-zero speed (for tracking).
 
int VIDEO_CACHE_MIN_PREROLL_FRAMES
Minimum number of frames to cache before playback begins.
 
This class represents a timeline.
 
void setSpeed(int new_speed)
Set playback speed/direction. Positive = forward, negative = rewind, zero = pause.
 
bool userSeeked
True if Seek(..., true) was called (forces a cache reset).
 
int speed
Current playback speed (0=paused, >0 forward, <0 backward).
 
static Settings * Instance()
Create or get an instance of this logger singleton (invoke the class with this method)
 
virtual void Touch(int64_t frame_number)=0
Move frame to front of queue (so it lasts longer)
 
Header file for Frame class.
 
void run() override
Thread entry point: loops until threadShouldExit() is true.
 
int64_t last_cached_index
Index of the most recently cached frame.
 
Header file for VideoCacheThread class.
 
int64_t getBytes(int width, int height, int sample_rate, int channels, float fps)
Estimate memory usage for a single frame (video + audio).
 
bool clearCacheIfPaused(int64_t playhead, bool paused, CacheBase *cache)
When paused and playhead is outside current cache, clear all frames.
 
int last_dir
Last direction sign (+1 forward, –1 backward).
 
int64_t cached_frame_count
Count of frames currently added to cache.
 
bool StopThread(int timeoutMs=0)
Stop the cache thread (wait up to timeoutMs ms). Returns true if it stopped.
 
int64_t GetMaxBytes()
Gets the maximum bytes value.
 
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
 
virtual bool Contains(int64_t frame_number)=0
Check if frame is already contained in cache.
 
This abstract class is the base class, used by all readers in libopenshot.
 
int64_t GetMaxFrame()
Look up the end frame number of the latest element on the timeline.
 
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.
 
void Seek(int64_t new_position)
Seek to a specific frame (no preroll).
 
int64_t requested_display_frame
Frame index the user requested.
 
int channels
The number of audio channels used in the audio stream.
 
virtual openshot::CacheBase * GetCache()=0
Get the cache object used by this reader (note: not all readers use cache)
 
Header file for all Exception classes.