OpenShot Library | libopenshot  0.2.5
CacheMemory.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Cache 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 #include "../include/CacheMemory.h"
32 
33 using namespace std;
34 using namespace openshot;
35 
36 // Default constructor, no max bytes
37 CacheMemory::CacheMemory() : CacheBase(0) {
38  // Set cache type name
39  cache_type = "CacheMemory";
40  range_version = 0;
41  needs_range_processing = false;
42 }
43 
44 // Constructor that sets the max bytes to cache
45 CacheMemory::CacheMemory(int64_t max_bytes) : CacheBase(max_bytes) {
46  // Set cache type name
47  cache_type = "CacheMemory";
48  range_version = 0;
49  needs_range_processing = false;
50 }
51 
52 // Default destructor
54 {
55  frames.clear();
56  frame_numbers.clear();
57  ordered_frame_numbers.clear();
58 
59  // remove critical section
60  delete cacheCriticalSection;
61  cacheCriticalSection = NULL;
62 }
63 
64 
65 // Calculate ranges of frames
66 void CacheMemory::CalculateRanges() {
67  // Only calculate when something has changed
68  if (needs_range_processing) {
69 
70  // Create a scoped lock, to protect the cache from multiple threads
71  const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
72 
73  // Sort ordered frame #s, and calculate JSON ranges
74  std::sort(ordered_frame_numbers.begin(), ordered_frame_numbers.end());
75 
76  // Clear existing JSON variable
77  Json::Value ranges = Json::Value(Json::arrayValue);
78 
79  // Increment range version
80  range_version++;
81 
82  std::vector<int64_t>::iterator itr_ordered;
83  int64_t starting_frame = *ordered_frame_numbers.begin();
84  int64_t ending_frame = *ordered_frame_numbers.begin();
85 
86  // Loop through all known frames (in sequential order)
87  for (itr_ordered = ordered_frame_numbers.begin(); itr_ordered != ordered_frame_numbers.end(); ++itr_ordered) {
88  int64_t frame_number = *itr_ordered;
89  if (frame_number - ending_frame > 1) {
90  // End of range detected
91  Json::Value range;
92 
93  // Add JSON object with start/end attributes
94  // Use strings, since int64_ts are supported in JSON
95  range["start"] = std::to_string(starting_frame);
96  range["end"] = std::to_string(ending_frame);
97  ranges.append(range);
98 
99  // Set new starting range
100  starting_frame = frame_number;
101  }
102 
103  // Set current frame as end of range, and keep looping
104  ending_frame = frame_number;
105  }
106 
107  // APPEND FINAL VALUE
108  Json::Value range;
109 
110  // Add JSON object with start/end attributes
111  // Use strings, since int64_ts are not supported in JSON
112  range["start"] = std::to_string(starting_frame);
113  range["end"] = std::to_string(ending_frame);
114  ranges.append(range);
115 
116  // Cache range JSON as string
117  json_ranges = ranges.toStyledString();
118 
119  // Reset needs_range_processing
120  needs_range_processing = false;
121  }
122 }
123 
124 // Add a Frame to the cache
125 void CacheMemory::Add(std::shared_ptr<Frame> frame)
126 {
127  // Create a scoped lock, to protect the cache from multiple threads
128  const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
129  int64_t frame_number = frame->number;
130 
131  // Freshen frame if it already exists
132  if (frames.count(frame_number))
133  // Move frame to front of queue
134  MoveToFront(frame_number);
135 
136  else
137  {
138  // Add frame to queue and map
139  frames[frame_number] = frame;
140  frame_numbers.push_front(frame_number);
141  ordered_frame_numbers.push_back(frame_number);
142  needs_range_processing = true;
143 
144  // Clean up old frames
145  CleanUp();
146  }
147 }
148 
149 // Get a frame from the cache (or NULL shared_ptr if no frame is found)
150 std::shared_ptr<Frame> CacheMemory::GetFrame(int64_t frame_number)
151 {
152  // Create a scoped lock, to protect the cache from multiple threads
153  const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
154 
155  // Does frame exists in cache?
156  if (frames.count(frame_number))
157  // return the Frame object
158  return frames[frame_number];
159 
160  else
161  // no Frame found
162  return std::shared_ptr<Frame>();
163 }
164 
165 // Get the smallest frame number (or NULL shared_ptr if no frame is found)
166 std::shared_ptr<Frame> CacheMemory::GetSmallestFrame()
167 {
168  // Create a scoped lock, to protect the cache from multiple threads
169  const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
170  std::shared_ptr<openshot::Frame> f;
171 
172  // Loop through frame numbers
173  std::deque<int64_t>::iterator itr;
174  int64_t smallest_frame = -1;
175  for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
176  {
177  if (*itr < smallest_frame || smallest_frame == -1)
178  smallest_frame = *itr;
179  }
180 
181  // Return frame
182  f = GetFrame(smallest_frame);
183 
184  return f;
185 }
186 
187 // Gets the maximum bytes value
189 {
190  // Create a scoped lock, to protect the cache from multiple threads
191  const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
192 
193  int64_t total_bytes = 0;
194 
195  // Loop through frames, and calculate total bytes
196  std::deque<int64_t>::reverse_iterator itr;
197  for(itr = frame_numbers.rbegin(); itr != frame_numbers.rend(); ++itr)
198  {
199  total_bytes += frames[*itr]->GetBytes();
200  }
201 
202  return total_bytes;
203 }
204 
205 // Remove a specific frame
206 void CacheMemory::Remove(int64_t frame_number)
207 {
208  Remove(frame_number, frame_number);
209 }
210 
211 // Remove range of frames
212 void CacheMemory::Remove(int64_t start_frame_number, int64_t end_frame_number)
213 {
214  // Create a scoped lock, to protect the cache from multiple threads
215  const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
216 
217  // Loop through frame numbers
218  std::deque<int64_t>::iterator itr;
219  for(itr = frame_numbers.begin(); itr != frame_numbers.end();)
220  {
221  if (*itr >= start_frame_number && *itr <= end_frame_number)
222  {
223  // erase frame number
224  itr = frame_numbers.erase(itr);
225  }else
226  itr++;
227  }
228 
229  // Loop through ordered frame numbers
230  std::vector<int64_t>::iterator itr_ordered;
231  for(itr_ordered = ordered_frame_numbers.begin(); itr_ordered != ordered_frame_numbers.end();)
232  {
233  if (*itr_ordered >= start_frame_number && *itr_ordered <= end_frame_number)
234  {
235  // erase frame number
236  frames.erase(*itr_ordered);
237  itr_ordered = ordered_frame_numbers.erase(itr_ordered);
238  }else
239  itr_ordered++;
240  }
241 
242  // Needs range processing (since cache has changed)
243  needs_range_processing = true;
244 }
245 
246 // Move frame to front of queue (so it lasts longer)
247 void CacheMemory::MoveToFront(int64_t frame_number)
248 {
249  // Create a scoped lock, to protect the cache from multiple threads
250  const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
251 
252  // Does frame exists in cache?
253  if (frames.count(frame_number))
254  {
255  // Loop through frame numbers
256  std::deque<int64_t>::iterator itr;
257  for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
258  {
259  if (*itr == frame_number)
260  {
261  // erase frame number
262  frame_numbers.erase(itr);
263 
264  // add frame number to 'front' of queue
265  frame_numbers.push_front(frame_number);
266  break;
267  }
268  }
269  }
270 }
271 
272 // Clear the cache of all frames
274 {
275  // Create a scoped lock, to protect the cache from multiple threads
276  const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
277 
278  frames.clear();
279  frame_numbers.clear();
280  ordered_frame_numbers.clear();
281  needs_range_processing = true;
282 }
283 
284 // Count the frames in the queue
286 {
287  // Create a scoped lock, to protect the cache from multiple threads
288  const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
289 
290  // Return the number of frames in the cache
291  return frames.size();
292 }
293 
294 // Clean up cached frames that exceed the number in our max_bytes variable
295 void CacheMemory::CleanUp()
296 {
297  // Do we auto clean up?
298  if (max_bytes > 0)
299  {
300  // Create a scoped lock, to protect the cache from multiple threads
301  const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
302 
303  while (GetBytes() > max_bytes && frame_numbers.size() > 20)
304  {
305  // Get the oldest frame number.
306  int64_t frame_to_remove = frame_numbers.back();
307 
308  // Remove frame_number and frame
309  Remove(frame_to_remove);
310  }
311  }
312 }
313 
314 
315 // Generate JSON string of this object
316 std::string CacheMemory::Json() {
317 
318  // Return formatted string
319  return JsonValue().toStyledString();
320 }
321 
322 // Generate Json::Value for this object
323 Json::Value CacheMemory::JsonValue() {
324 
325  // Process range data (if anything has changed)
326  CalculateRanges();
327 
328  // Create root json object
329  Json::Value root = CacheBase::JsonValue(); // get parent properties
330  root["type"] = cache_type;
331 
332  root["version"] = std::to_string(range_version);
333 
334  // Parse and append range data (if any)
335  try {
336  const Json::Value ranges = openshot::stringToJson(json_ranges);
337  root["ranges"] = ranges;
338  } catch (...) { }
339 
340  // return JsonValue
341  return root;
342 }
343 
344 // Load JSON string into this object
345 void CacheMemory::SetJson(const std::string value) {
346 
347  try
348  {
349  // Parse string to Json::Value
350  const Json::Value root = openshot::stringToJson(value);
351  // Set all values that match
352  SetJsonValue(root);
353  }
354  catch (const std::exception& e)
355  {
356  // Error parsing JSON (or missing keys)
357  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
358  }
359 }
360 
361 // Load Json::Value into this object
362 void CacheMemory::SetJsonValue(const Json::Value root) {
363 
364  // Close timeline before we do anything (this also removes all open and closing clips)
365  Clear();
366 
367  // Set parent data
369 
370  if (!root["type"].isNull())
371  cache_type = root["type"].asString();
372 }
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
openshot::CacheMemory::Clear
void Clear()
Clear the cache of all frames.
Definition: CacheMemory.cpp:273
openshot::CacheMemory::Count
int64_t Count()
Count the frames in the queue.
Definition: CacheMemory.cpp:285
openshot::CacheMemory::GetBytes
int64_t GetBytes()
Gets the maximum bytes value.
Definition: CacheMemory.cpp:188
openshot::CacheMemory::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number)
Get a frame from the cache.
Definition: CacheMemory.cpp:150
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::CacheMemory::Add
void Add(std::shared_ptr< openshot::Frame > frame)
Add a Frame to the cache.
Definition: CacheMemory.cpp:125
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:38
openshot::CacheBase::cacheCriticalSection
juce::CriticalSection * cacheCriticalSection
Section lock for multiple threads.
Definition: CacheBase.h:56
openshot::CacheMemory::Remove
void Remove(int64_t frame_number)
Remove a specific frame.
Definition: CacheMemory.cpp:206
openshot::CacheMemory::JsonValue
Json::Value JsonValue()
Generate Json::Value for this object.
Definition: CacheMemory.cpp:323
openshot::CacheBase
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:49
openshot::CacheBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: CacheBase.cpp:70
openshot::CacheMemory::~CacheMemory
virtual ~CacheMemory()
Definition: CacheMemory.cpp:53
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:205
openshot::CacheMemory::Json
std::string Json()
Get and Set JSON methods.
Definition: CacheMemory.cpp:316
openshot::CacheBase::JsonValue
virtual Json::Value JsonValue()=0
Generate Json::Value for this object.
Definition: CacheBase.cpp:57
openshot::CacheMemory::GetSmallestFrame
std::shared_ptr< openshot::Frame > GetSmallestFrame()
Get the smallest frame number.
Definition: CacheMemory.cpp:166
openshot::CacheBase::cache_type
std::string cache_type
This is a friendly type name of the derived cache instance.
Definition: CacheBase.h:52
openshot::CacheMemory::CacheMemory
CacheMemory()
Default constructor, no max bytes.
Definition: CacheMemory.cpp:37
openshot::CacheMemory::MoveToFront
void MoveToFront(int64_t frame_number)
Move frame to front of queue (so it lasts longer)
Definition: CacheMemory.cpp:247
openshot::CacheMemory::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: CacheMemory.cpp:345
openshot::CacheMemory::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: CacheMemory.cpp:362
openshot::Frame::number
int64_t number
This is the frame number (starting at 1)
Definition: Frame.h:129