OpenShot Library | libopenshot  0.2.5
CacheBase.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for CacheBase class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #ifndef OPENSHOT_CACHE_BASE_H
32 #define OPENSHOT_CACHE_BASE_H
33 
34 #include <memory>
35 #include <cstdlib>
36 #include "Frame.h"
37 #include "Exceptions.h"
38 #include "Json.h"
39 
40 namespace openshot {
41 
42  /**
43  * @brief All cache managers in libopenshot are based on this CacheBase class
44  *
45  * Cache is a very important element of video editing, and is required to achieve a high degree
46  * of performance. There are multiple derived cache objects based on this class, some which use
47  * memory, and some which use disk to store the cache.
48  */
49  class CacheBase
50  {
51  protected:
52  std::string cache_type; ///< This is a friendly type name of the derived cache instance
53  int64_t max_bytes; ///< This is the max number of bytes to cache (0 = no limit)
54 
55  /// Section lock for multiple threads
56  juce::CriticalSection *cacheCriticalSection;
57 
58 
59  public:
60  /// Default constructor, no max bytes
61  CacheBase();
62 
63  /// @brief Constructor that sets the max bytes to cache
64  /// @param max_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
65  CacheBase(int64_t max_bytes);
66 
67  /// @brief Add a Frame to the cache
68  /// @param frame The openshot::Frame object needing to be cached.
69  virtual void Add(std::shared_ptr<openshot::Frame> frame) = 0;
70 
71  /// Clear the cache of all frames
72  virtual void Clear() = 0;
73 
74  /// Count the frames in the queue
75  virtual int64_t Count() = 0;
76 
77  /// @brief Get a frame from the cache
78  /// @param frame_number The frame number of the cached frame
79  virtual std::shared_ptr<openshot::Frame> GetFrame(int64_t frame_number) = 0;
80 
81  /// Gets the maximum bytes value
82  virtual int64_t GetBytes() = 0;
83 
84  /// Get the smallest frame number
85  virtual std::shared_ptr<openshot::Frame> GetSmallestFrame() = 0;
86 
87  /// @brief Remove a specific frame
88  /// @param frame_number The frame number of the cached frame
89  virtual void Remove(int64_t frame_number) = 0;
90 
91  /// @brief Remove a range of frames
92  /// @param start_frame_number The starting frame number of the cached frame
93  /// @param end_frame_number The ending frame number of the cached frame
94  virtual void Remove(int64_t start_frame_number, int64_t end_frame_number) = 0;
95 
96  /// Gets the maximum bytes value
97  int64_t GetMaxBytes() { return max_bytes; };
98 
99  /// @brief Set maximum bytes to a different amount
100  /// @param number_of_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
101  void SetMaxBytes(int64_t number_of_bytes) { max_bytes = number_of_bytes; };
102 
103  /// @brief Set maximum bytes to a different amount based on a ReaderInfo struct
104  /// @param number_of_frames The maximum number of frames to hold in cache
105  /// @param width The width of the frame's image
106  /// @param height The height of the frame's image
107  /// @param sample_rate The sample rate of the frame's audio data
108  /// @param channels The number of audio channels in the frame
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; ///< Generate JSON string of this object
113  virtual void SetJson(const std::string value) = 0; ///< Load JSON string into this object
114  virtual Json::Value JsonValue() = 0; ///< Generate Json::Value for this object
115  virtual void SetJsonValue(const Json::Value root) = 0; ///< Load Json::Value into this object
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::max_bytes
int64_t max_bytes
This is the max number of bytes to cache (0 = no limit)
Definition: CacheBase.h:53
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:38
openshot::CacheBase::Add
virtual void Add(std::shared_ptr< openshot::Frame > frame)=0
Add a Frame to the cache.
openshot::CacheBase::~CacheBase
virtual ~CacheBase()=default
openshot::CacheBase::cacheCriticalSection
juce::CriticalSection * cacheCriticalSection
Section lock for multiple threads.
Definition: CacheBase.h:56
openshot::CacheBase
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:49
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:49
openshot::CacheBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: CacheBase.cpp:70
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:57
Frame.h
Header file for Frame class.
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:52
openshot::CacheBase::Json
virtual std::string Json()=0
Get and Set JSON methods.
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::CacheBase
CacheBase()
Default constructor, no max bytes.
Definition: CacheBase.cpp:37
Json.h
Header file for JSON class.
Exceptions.h
Header file for all Exception classes.