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