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);
74 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
76 if (frames.count(frame_number) > 0) {
87 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
90 if (frames.count(frame_number))
92 return frames[frame_number];
96 return std::shared_ptr<Frame>();
103 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
105 std::vector<std::shared_ptr<openshot::Frame>> all_frames;
106 std::vector<int64_t>::iterator itr_ordered;
109 int64_t frame_number = *itr_ordered;
110 all_frames.push_back(
GetFrame(frame_number));
120 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
123 std::deque<int64_t>::iterator itr;
124 int64_t smallest_frame = -1;
125 for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
127 if (*itr < smallest_frame || smallest_frame == -1)
128 smallest_frame = *itr;
132 if (smallest_frame != -1) {
133 return frames[smallest_frame];
143 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
145 int64_t total_bytes = 0;
148 std::deque<int64_t>::reverse_iterator itr;
149 for(itr = frame_numbers.rbegin(); itr != frame_numbers.rend(); ++itr)
151 total_bytes += frames[*itr]->GetBytes();
160 Remove(frame_number, frame_number);
167 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
169 std::deque<int64_t>::iterator itr;
170 for(itr = frame_numbers.begin(); itr != frame_numbers.end();)
172 if (*itr >= start_frame_number && *itr <= end_frame_number)
175 itr = frame_numbers.erase(itr);
181 std::vector<int64_t>::iterator itr_ordered;
184 if (*itr_ordered >= start_frame_number && *itr_ordered <= end_frame_number)
187 frames.erase(*itr_ordered);
201 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
204 if (frames.count(frame_number))
207 std::deque<int64_t>::iterator itr;
208 for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
210 if (*itr == frame_number)
213 frame_numbers.erase(itr);
216 frame_numbers.push_front(frame_number);
227 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
230 frame_numbers.clear();
231 frame_numbers.shrink_to_fit();
243 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
246 return frames.size();
250void CacheMemory::CleanUp()
256 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
261 int64_t frame_to_remove = frame_numbers.back();
292 root[
"ranges"] = ranges;
309 catch (
const std::exception& e)
312 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
325 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() noexcept
Attempt to return unused heap memory to the operating system.
const Json::Value stringToJson(const std::string value)