50 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
51 int64_t frame_number = frame->number;
54 if (frames.count(frame_number))
61 frames[frame_number] = frame;
62 frame_numbers.push_front(frame_number);
73 if (frames.count(frame_number) > 0) {
84 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
87 if (frames.count(frame_number))
89 return frames[frame_number];
93 return std::shared_ptr<Frame>();
100 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
102 std::vector<std::shared_ptr<openshot::Frame>> all_frames;
103 std::vector<int64_t>::iterator itr_ordered;
106 int64_t frame_number = *itr_ordered;
107 all_frames.push_back(
GetFrame(frame_number));
117 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
120 std::deque<int64_t>::iterator itr;
121 int64_t smallest_frame = -1;
122 for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
124 if (*itr < smallest_frame || smallest_frame == -1)
125 smallest_frame = *itr;
129 if (smallest_frame != -1) {
130 return frames[smallest_frame];
140 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
142 int64_t total_bytes = 0;
145 std::deque<int64_t>::reverse_iterator itr;
146 for(itr = frame_numbers.rbegin(); itr != frame_numbers.rend(); ++itr)
148 total_bytes += frames[*itr]->GetBytes();
157 Remove(frame_number, frame_number);
164 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
165 int64_t removed_bytes = 0;
168 std::deque<int64_t>::iterator itr;
169 for(itr = frame_numbers.begin(); itr != frame_numbers.end();)
171 if (*itr >= start_frame_number && *itr <= end_frame_number)
174 itr = frame_numbers.erase(itr);
180 std::vector<int64_t>::iterator itr_ordered;
183 if (*itr_ordered >= start_frame_number && *itr_ordered <= end_frame_number)
186 if (frames.count(*itr_ordered))
187 removed_bytes += frames[*itr_ordered]->
GetBytes();
190 frames.erase(*itr_ordered);
196 if (removed_bytes > 0)
198 bytes_freed_since_trim += removed_bytes;
199 if (bytes_freed_since_trim >= TRIM_THRESHOLD_BYTES)
203 bytes_freed_since_trim = 0;
215 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
218 if (frames.count(frame_number))
221 std::deque<int64_t>::iterator itr;
222 for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
224 if (*itr == frame_number)
227 frame_numbers.erase(itr);
230 frame_numbers.push_front(frame_number);
241 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
244 frame_numbers.clear();
245 frame_numbers.shrink_to_fit();
249 bytes_freed_since_trim = 0;
259 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
262 return frames.size();
266void CacheMemory::CleanUp()
272 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
277 int64_t frame_to_remove = frame_numbers.back();
308 root[
"ranges"] = ranges;
325 catch (
const std::exception& e)
328 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
341 if (!root[
"type"].isNull())
Header file for CacheMemory class.
Header file for all Exception classes.
Header file for Frame class.
Cross-platform helper to encourage returning freed memory to the OS.
All cache managers in libopenshot are based on this CacheBase class.
int64_t range_version
The version of the JSON range data (incremented with each change)
virtual Json::Value JsonValue()=0
Generate Json::Value for this object.
std::string cache_type
This is a friendly type name of the derived cache instance.
void CalculateRanges()
Calculate ranges of frames.
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
bool needs_range_processing
Something has changed, and the range data needs to be re-calculated.
int64_t max_bytes
This is the max number of bytes to cache (0 = no limit)
std::recursive_mutex * cacheMutex
Mutex for multiple threads.
std::string json_ranges
JSON ranges of frame numbers.
std::vector< int64_t > ordered_frame_numbers
Ordered list of frame numbers used by cache.
CacheMemory()
Default constructor, no max bytes.
int64_t Count()
Count the frames in the queue.
void Add(std::shared_ptr< openshot::Frame > frame)
Add a Frame to the cache.
void SetJson(const std::string value)
Load JSON string into this object.
int64_t GetBytes()
Gets the maximum bytes value.
std::string Json()
Generate JSON string of this object.
std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number)
Get a frame from the cache.
std::vector< std::shared_ptr< openshot::Frame > > GetFrames()
Get an array of all Frames.
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
void Remove(int64_t frame_number)
Remove a specific frame.
void Clear()
Clear the cache of all frames.
Json::Value JsonValue()
Generate Json::Value for this object.
void Touch(int64_t frame_number)
Move frame to front of queue (so it lasts longer)
bool Contains(int64_t frame_number)
Check if frame is already contained in cache.
std::shared_ptr< openshot::Frame > GetSmallestFrame()
Get the smallest frame number.
Exception for invalid JSON.
This namespace is the default namespace for all code in the openshot library.
bool TrimMemoryToOS(bool force) noexcept
Attempt to return unused heap memory to the operating system.
const Json::Value stringToJson(const std::string value)