OpenShot Library | libopenshot  0.2.5
CacheDisk.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for CacheDisk 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_DISK_H
32 #define OPENSHOT_CACHE_DISK_H
33 
34 #include <map>
35 #include <deque>
36 #include <memory>
37 #include "CacheBase.h"
38 #include "Frame.h"
39 #include "Exceptions.h"
40 #include <QDir>
41 #include <QString>
42 #include <QTextStream>
43 
44 namespace openshot {
45 
46  /**
47  * @brief This class is a disk-based cache manager for Frame objects.
48  *
49  * It is used by the Timeline class, if enabled, to cache video and audio frames to disk, to cut down on CPU
50  * and memory utilization. This will thrash a user's disk, but save their memory and CPU. It's a trade off that
51  * sometimes makes perfect sense. You can also set the max number of bytes to cache.
52  */
53  class CacheDisk : public CacheBase {
54  private:
55  QDir path; ///< This is the folder path of the cache directory
56  std::map<int64_t, int64_t> frames; ///< This map holds the frame number and Frame objects
57  std::deque<int64_t> frame_numbers; ///< This queue holds a sequential list of cached Frame numbers
58  std::string image_format;
59  float image_quality;
60  float image_scale;
61 
62  int64_t frame_size_bytes; ///< The size of the cached frame in bytes
63  bool needs_range_processing; ///< Something has changed, and the range data needs to be re-calculated
64  std::string json_ranges; ///< JSON ranges of frame numbers
65  std::vector<int64_t> ordered_frame_numbers; ///< Ordered list of frame numbers used by cache
66  std::map<int64_t, int64_t> frame_ranges; ///< This map holds the ranges of frames, useful for quickly displaying the contents of the cache
67  int64_t range_version; ///< The version of the JSON range data (incremented with each change)
68 
69  /// Clean up cached frames that exceed the max number of bytes
70  void CleanUp();
71 
72  /// Init path directory
73  void InitPath(std::string cache_path);
74 
75  /// Calculate ranges of frames
76  void CalculateRanges();
77 
78  public:
79  /// @brief Default constructor, no max bytes
80  /// @param cache_path The folder path of the cache directory (empty string = /tmp/preview-cache/)
81  /// @param format The image format for disk caching (ppm, jpg, png)
82  /// @param quality The quality of the image (1.0=highest quality/slowest speed, 0.0=worst quality/fastest speed)
83  /// @param scale The scale factor for the preview images (1.0 = original size, 0.5=half size, 0.25=quarter size, etc...)
84  CacheDisk(std::string cache_path, std::string format, float quality, float scale);
85 
86  /// @brief Constructor that sets the max bytes to cache
87  /// @param cache_path The folder path of the cache directory (empty string = /tmp/preview-cache/)
88  /// @param format The image format for disk caching (ppm, jpg, png)
89  /// @param quality The quality of the image (1.0=highest quality/slowest speed, 0.0=worst quality/fastest speed)
90  /// @param scale The scale factor for the preview images (1.0 = original size, 0.5=half size, 0.25=quarter size, etc...)
91  /// @param max_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
92  CacheDisk(std::string cache_path, std::string format, float quality, float scale, int64_t max_bytes);
93 
94  // Default destructor
95  ~CacheDisk();
96 
97  /// @brief Add a Frame to the cache
98  /// @param frame The openshot::Frame object needing to be cached.
99  void Add(std::shared_ptr<openshot::Frame> frame);
100 
101  /// Clear the cache of all frames
102  void Clear();
103 
104  /// Count the frames in the queue
105  int64_t Count();
106 
107  /// @brief Get a frame from the cache
108  /// @param frame_number The frame number of the cached frame
109  std::shared_ptr<openshot::Frame> GetFrame(int64_t frame_number);
110 
111  /// Gets the maximum bytes value
112  int64_t GetBytes();
113 
114  /// Get the smallest frame number
115  std::shared_ptr<openshot::Frame> GetSmallestFrame();
116 
117  /// @brief Move frame to front of queue (so it lasts longer)
118  /// @param frame_number The frame number of the cached frame
119  void MoveToFront(int64_t frame_number);
120 
121  /// @brief Remove a specific frame
122  /// @param frame_number The frame number of the cached frame
123  void Remove(int64_t frame_number);
124 
125  /// @brief Remove a range of frames
126  /// @param start_frame_number The starting frame number of the cached frame
127  /// @param end_frame_number The ending frame number of the cached frame
128  void Remove(int64_t start_frame_number, int64_t end_frame_number);
129 
130  /// Get and Set JSON methods
131  std::string Json(); ///< Generate JSON string of this object
132  void SetJson(const std::string value); ///< Load JSON string into this object
133  Json::Value JsonValue(); ///< Generate Json::Value for this object
134  void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object
135  };
136 
137 }
138 
139 #endif
void Add(std::shared_ptr< openshot::Frame > frame)
Add a Frame to the cache.
Definition: CacheDisk.cpp:159
int64_t Count()
Count the frames in the queue.
Definition: CacheDisk.cpp:428
This class is a disk-based cache manager for Frame objects.
Definition: CacheDisk.h:53
int64_t GetBytes()
Gets the maximum bytes value.
Definition: CacheDisk.cpp:310
std::string Json()
Get and Set JSON methods.
Definition: CacheDisk.cpp:458
Header file for CacheBase class.
Header file for all Exception classes.
std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number)
Get a frame from the cache.
Definition: CacheDisk.cpp:218
Header file for Frame class.
void Remove(int64_t frame_number)
Remove a specific frame.
Definition: CacheDisk.cpp:326
Json::Value JsonValue()
Generate Json::Value for this object.
Definition: CacheDisk.cpp:465
void MoveToFront(int64_t frame_number)
Move frame to front of queue (so it lasts longer)
Definition: CacheDisk.cpp:381
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: CacheDisk.cpp:509
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:49
void Clear()
Clear the cache of all frames.
Definition: CacheDisk.cpp:407
This namespace is the default namespace for all code in the openshot library.
std::shared_ptr< openshot::Frame > GetSmallestFrame()
Get the smallest frame number.
Definition: CacheDisk.cpp:288
int64_t max_bytes
This is the max number of bytes to cache (0 = no limit)
Definition: CacheBase.h:53
void SetJson(const std::string value)
Load JSON string into this object.
Definition: CacheDisk.cpp:492
CacheDisk(std::string cache_path, std::string format, float quality, float scale)
Default constructor, no max bytes.
Definition: CacheDisk.cpp:37