OpenShot Library | libopenshot  0.2.6
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 "Json.h"
38 
39 namespace openshot {
40 
41  /**
42  * @brief All cache managers in libopenshot are based on this CacheBase class
43  *
44  * Cache is a very important element of video editing, and is required to achieve a high degree
45  * of performance. There are multiple derived cache objects based on this class, some which use
46  * memory, and some which use disk to store the cache.
47  */
48  class CacheBase
49  {
50  protected:
51  std::string cache_type; ///< This is a friendly type name of the derived cache instance
52  int64_t max_bytes; ///< This is the max number of bytes to cache (0 = no limit)
53 
54  /// Section lock for multiple threads
55  juce::CriticalSection *cacheCriticalSection;
56 
57 
58  public:
59  /// Default constructor, no max bytes
60  CacheBase();
61 
62  /// @brief Constructor that sets the max bytes to cache
63  /// @param max_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
64  CacheBase(int64_t max_bytes);
65 
66  /// @brief Add a Frame to the cache
67  /// @param frame The openshot::Frame object needing to be cached.
68  virtual void Add(std::shared_ptr<openshot::Frame> frame) = 0;
69 
70  /// Clear the cache of all frames
71  virtual void Clear() = 0;
72 
73  /// Count the frames in the queue
74  virtual int64_t Count() = 0;
75 
76  /// @brief Get a frame from the cache
77  /// @param frame_number The frame number of the cached frame
78  virtual std::shared_ptr<openshot::Frame> GetFrame(int64_t frame_number) = 0;
79 
80  /// Gets the maximum bytes value
81  virtual int64_t GetBytes() = 0;
82 
83  /// Get the smallest frame number
84  virtual std::shared_ptr<openshot::Frame> GetSmallestFrame() = 0;
85 
86  /// @brief Remove a specific frame
87  /// @param frame_number The frame number of the cached frame
88  virtual void Remove(int64_t frame_number) = 0;
89 
90  /// @brief Remove a range of frames
91  /// @param start_frame_number The starting frame number of the cached frame
92  /// @param end_frame_number The ending frame number of the cached frame
93  virtual void Remove(int64_t start_frame_number, int64_t end_frame_number) = 0;
94 
95  /// Gets the maximum bytes value
96  int64_t GetMaxBytes() { return max_bytes; };
97 
98  /// @brief Set maximum bytes to a different amount
99  /// @param number_of_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
100  void SetMaxBytes(int64_t number_of_bytes) { max_bytes = number_of_bytes; };
101 
102  /// @brief Set maximum bytes to a different amount based on a ReaderInfo struct
103  /// @param number_of_frames The maximum number of frames to hold in cache
104  /// @param width The width of the frame's image
105  /// @param height The height of the frame's image
106  /// @param sample_rate The sample rate of the frame's audio data
107  /// @param channels The number of audio channels in the frame
108  void SetMaxBytesFromInfo(int64_t number_of_frames, int width, int height, int sample_rate, int channels);
109 
110  // Get and Set JSON methods
111  virtual std::string Json() = 0; ///< Generate JSON string of this object
112  virtual void SetJson(const std::string value) = 0; ///< Load JSON string into this object
113  virtual Json::Value JsonValue() = 0; ///< Generate Json::Value for this object
114  virtual void SetJsonValue(const Json::Value root) = 0; ///< Load Json::Value into this object
115  virtual ~CacheBase() = default;
116 
117  };
118 
119 }
120 
121 #endif
juce::CriticalSection * cacheCriticalSection
Section lock for multiple threads.
Definition: CacheBase.h:55
virtual std::string Json()=0
Generate JSON string of this object.
virtual void SetJson(const std::string value)=0
Load JSON string into this object.
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
virtual ~CacheBase()=default
Header file for Frame class.
virtual void Remove(int64_t frame_number)=0
Remove a specific frame.
virtual int64_t Count()=0
Count the frames in the queue.
Header file for JSON class.
virtual std::shared_ptr< openshot::Frame > GetSmallestFrame()=0
Get the smallest frame number.
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number)=0
Get a frame from the cache.
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:48
CacheBase()
Default constructor, no max bytes.
Definition: CacheBase.cpp:37
virtual int64_t GetBytes()=0
Gets the maximum bytes value.
virtual Json::Value JsonValue()=0
Generate Json::Value for this object.
Definition: CacheBase.cpp:57
void SetMaxBytes(int64_t number_of_bytes)
Set maximum bytes to a different amount.
Definition: CacheBase.h:100
std::string cache_type
This is a friendly type name of the derived cache instance.
Definition: CacheBase.h:51
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
virtual void Clear()=0
Clear the cache of all frames.
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: CacheBase.cpp:70
int64_t GetMaxBytes()
Gets the maximum bytes value.
Definition: CacheBase.h:96
virtual void Add(std::shared_ptr< openshot::Frame > frame)=0
Add a Frame to the cache.
int64_t max_bytes
This is the max number of bytes to cache (0 = no limit)
Definition: CacheBase.h:52