OpenShot Library | libopenshot  0.4.0
CacheBase.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #include "CacheBase.h"
14 
15 using namespace std;
16 using namespace openshot;
17 
18 // Default constructor, no max frames
19 CacheBase::CacheBase() : CacheBase::CacheBase(0) { }
20 
21 // Constructor that sets the max frames to cache
22 CacheBase::CacheBase(int64_t max_bytes) : max_bytes(max_bytes) {
23  // Init the mutex
24  cacheMutex = new std::recursive_mutex();
25 }
26 
27 // Set maximum bytes to a different amount based on a ReaderInfo struct
28 void CacheBase::SetMaxBytesFromInfo(int64_t number_of_frames, int width, int height, int sample_rate, int channels)
29 {
30  // n frames X height X width X 4 colors of chars X audio channels X 4 byte floats
31  int64_t bytes = number_of_frames * (height * width * 4 + (sample_rate * channels * 4));
32  SetMaxBytes(bytes);
33 }
34 
35 // Calculate ranges of frames
37  // Only calculate when something has changed
39 
40  // Create a scoped lock, to protect the cache from multiple threads
41  const std::lock_guard<std::recursive_mutex> lock(*cacheMutex);
42 
43  // Sort ordered frame #s, and calculate JSON ranges
44  std::sort(ordered_frame_numbers.begin(), ordered_frame_numbers.end());
45 
46  // Clear existing JSON variable
47  Json::Value ranges = Json::Value(Json::arrayValue);
48 
49  // Increment range version
50  range_version++;
51 
52  std::vector<int64_t>::iterator itr_ordered;
53 
54  int64_t starting_frame = 0;
55  int64_t ending_frame = 0;
56  if (ordered_frame_numbers.size() > 0) {
57  starting_frame = *ordered_frame_numbers.begin();
58  ending_frame = *ordered_frame_numbers.begin();
59 
60  // Loop through all known frames (in sequential order)
61  for (itr_ordered = ordered_frame_numbers.begin(); itr_ordered != ordered_frame_numbers.end(); ++itr_ordered) {
62  int64_t frame_number = *itr_ordered;
63  if (frame_number - ending_frame > 1) {
64  // End of range detected
65  Json::Value range;
66 
67  // Add JSON object with start/end attributes
68  // Use strings, since int64_ts are supported in JSON
69  range["start"] = std::to_string(starting_frame);
70  range["end"] = std::to_string(ending_frame);
71  ranges.append(range);
72 
73  // Set new starting range
74  starting_frame = frame_number;
75  }
76 
77  // Set current frame as end of range, and keep looping
78  ending_frame = frame_number;
79  }
80  }
81 
82  // APPEND FINAL VALUE
83  Json::Value range;
84 
85  // Add JSON object with start/end attributes
86  // Use strings, since int64_ts are not supported in JSON
87  range["start"] = std::to_string(starting_frame);
88  range["end"] = std::to_string(ending_frame);
89  ranges.append(range);
90 
91  // Cache range JSON as string
92  json_ranges = ranges.toStyledString();
93 
94  // Reset needs_range_processing
95  needs_range_processing = false;
96  }
97 }
98 
99 // Generate Json::Value for this object
100 Json::Value CacheBase::JsonValue() {
101 
102  // Create root json object
103  Json::Value root;
104  root["max_bytes"] = std::to_string(max_bytes);
105 
106  // return JsonValue
107  return root;
108 }
109 
110 // Load Json::Value into this object
111 void CacheBase::SetJsonValue(const Json::Value root) {
112 
113  // Set data from Json (if key is found)
114  if (!root["max_bytes"].isNull())
115  max_bytes = std::stoll(root["max_bytes"].asString());
116 }
openshot::CacheBase::needs_range_processing
bool needs_range_processing
Something has changed, and the range data needs to be re-calculated.
Definition: CacheBase.h:40
openshot::CacheBase::max_bytes
int64_t max_bytes
This is the max number of bytes to cache (0 = no limit)
Definition: CacheBase.h:38
openshot::CacheBase::ordered_frame_numbers
std::vector< int64_t > ordered_frame_numbers
Ordered list of frame numbers used by cache.
Definition: CacheBase.h:42
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::CacheBase::json_ranges
std::string json_ranges
JSON ranges of frame numbers.
Definition: CacheBase.h:41
openshot::CacheBase
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:34
CacheBase.h
Header file for CacheBase class.
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:28
openshot::CacheBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: CacheBase.cpp:111
openshot::CacheBase::JsonValue
virtual Json::Value JsonValue()=0
Generate Json::Value for this object.
Definition: CacheBase.cpp:100
openshot::CacheBase::CalculateRanges
void CalculateRanges()
Calculate ranges of frames.
Definition: CacheBase.cpp:36
openshot::CacheBase::SetMaxBytes
void SetMaxBytes(int64_t number_of_bytes)
Set maximum bytes to a different amount.
Definition: CacheBase.h:101
openshot::CacheBase::CacheBase
CacheBase()
Default constructor, no max bytes.
Definition: CacheBase.cpp:19
openshot::CacheBase::range_version
int64_t range_version
The version of the JSON range data (incremented with each change)
Definition: CacheBase.h:44
openshot::CacheBase::cacheMutex
std::recursive_mutex * cacheMutex
Mutex for multiple threads.
Definition: CacheBase.h:47