OpenShot Library | libopenshot  0.4.0
CacheBase.h
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #ifndef OPENSHOT_CACHE_BASE_H
14 #define OPENSHOT_CACHE_BASE_H
15 
16 #include <map>
17 #include <vector>
18 #include <memory>
19 #include <mutex>
20 #include <algorithm>
21 
22 #include "Json.h"
23 
24 namespace openshot {
25  class Frame;
26 
34  class CacheBase
35  {
36  protected:
37  std::string cache_type;
38  int64_t max_bytes;
39 
41  std::string json_ranges;
42  std::vector<int64_t> ordered_frame_numbers;
43  std::map<int64_t, int64_t> frame_ranges;
44  int64_t range_version;
45 
47  std::recursive_mutex *cacheMutex;
48 
50  void CalculateRanges();
51 
52  public:
54  CacheBase();
55 
58  CacheBase(int64_t max_bytes);
59 
62  virtual void Add(std::shared_ptr<openshot::Frame> frame) = 0;
63 
65  virtual void Clear() = 0;
66 
69  virtual bool Contains(int64_t frame_number) = 0;
70 
72  virtual int64_t Count() = 0;
73 
76  virtual std::shared_ptr<openshot::Frame> GetFrame(int64_t frame_number) = 0;
77 
79  virtual std::vector<std::shared_ptr<openshot::Frame>> GetFrames() = 0;
80 
82  virtual int64_t GetBytes() = 0;
83 
85  virtual std::shared_ptr<openshot::Frame> GetSmallestFrame() = 0;
86 
89  virtual void Remove(int64_t frame_number) = 0;
90 
94  virtual void Remove(int64_t start_frame_number, int64_t end_frame_number) = 0;
95 
97  int64_t GetMaxBytes() { return max_bytes; };
98 
101  void SetMaxBytes(int64_t number_of_bytes) { max_bytes = number_of_bytes; };
102 
109  void SetMaxBytesFromInfo(int64_t number_of_frames, int width, int height, int sample_rate, int channels);
110 
111  // Get and Set JSON methods
112  virtual std::string Json() = 0;
113  virtual void SetJson(const std::string value) = 0;
114  virtual Json::Value JsonValue() = 0;
115  virtual void SetJsonValue(const Json::Value root) = 0;
116  virtual ~CacheBase() = default;
117 
118  };
119 
120 }
121 
122 #endif
openshot::CacheBase::GetSmallestFrame
virtual std::shared_ptr< openshot::Frame > GetSmallestFrame()=0
Get the smallest frame number.
openshot::CacheBase::Clear
virtual void Clear()=0
Clear the cache of all frames.
openshot::CacheBase::GetFrame
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number)=0
Get a frame from the cache.
openshot::CacheBase::needs_range_processing
bool needs_range_processing
Something has changed, and the range data needs to be re-calculated.
Definition: CacheBase.h:40
openshot::CacheBase::max_bytes
int64_t max_bytes
This is the max number of bytes to cache (0 = no limit)
Definition: CacheBase.h:38
openshot::CacheBase::ordered_frame_numbers
std::vector< int64_t > ordered_frame_numbers
Ordered list of frame numbers used by cache.
Definition: CacheBase.h:42
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::CacheBase::Add
virtual void Add(std::shared_ptr< openshot::Frame > frame)=0
Add a Frame to the cache.
openshot::CacheBase::json_ranges
std::string json_ranges
JSON ranges of frame numbers.
Definition: CacheBase.h:41
openshot::CacheBase::~CacheBase
virtual ~CacheBase()=default
openshot::CacheBase
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:34
openshot::CacheBase::Remove
virtual void Remove(int64_t frame_number)=0
Remove a specific frame.
openshot::CacheBase::SetMaxBytesFromInfo
void SetMaxBytesFromInfo(int64_t number_of_frames, int width, int height, int sample_rate, int channels)
Set maximum bytes to a different amount based on a ReaderInfo struct.
Definition: CacheBase.cpp:30
openshot::CacheBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: CacheBase.cpp:115
openshot::CacheBase::GetFrames
virtual std::vector< std::shared_ptr< openshot::Frame > > GetFrames()=0
Get an vector of all Frames.
openshot::CacheBase::frame_ranges
std::map< int64_t, int64_t > frame_ranges
This map holds the ranges of frames, useful for quickly displaying the contents of the cache.
Definition: CacheBase.h:43
openshot::CacheBase::SetJson
virtual void SetJson(const std::string value)=0
Load JSON string into this object.
openshot::CacheBase::JsonValue
virtual Json::Value JsonValue()=0
Generate Json::Value for this object.
Definition: CacheBase.cpp:102
openshot::CacheBase::CalculateRanges
void CalculateRanges()
Calculate ranges of frames.
Definition: CacheBase.cpp:38
openshot::CacheBase::Count
virtual int64_t Count()=0
Count the frames in the queue.
openshot::CacheBase::cache_type
std::string cache_type
This is a friendly type name of the derived cache instance.
Definition: CacheBase.h:37
openshot::CacheBase::Json
virtual std::string Json()=0
Generate JSON string of this object.
openshot::CacheBase::GetMaxBytes
int64_t GetMaxBytes()
Gets the maximum bytes value.
Definition: CacheBase.h:97
openshot::CacheBase::SetMaxBytes
void SetMaxBytes(int64_t number_of_bytes)
Set maximum bytes to a different amount.
Definition: CacheBase.h:101
openshot::CacheBase::GetBytes
virtual int64_t GetBytes()=0
Gets the maximum bytes value.
openshot::CacheBase::Contains
virtual bool Contains(int64_t frame_number)=0
Check if frame is already contained in cache.
openshot::CacheBase::CacheBase
CacheBase()
Default constructor, no max bytes.
Definition: CacheBase.cpp:21
Json.h
Header file for JSON class.
openshot::CacheBase::range_version
int64_t range_version
The version of the JSON range data (incremented with each change)
Definition: CacheBase.h:44
openshot::CacheBase::cacheMutex
std::recursive_mutex * cacheMutex
Mutex for multiple threads.
Definition: CacheBase.h:47