49 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
50 int64_t frame_number = frame->number;
53 if (frames.count(frame_number))
60 frames[frame_number] = frame;
61 frame_numbers.push_front(frame_number);
72 if (frames.count(frame_number) > 0) {
83 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
86 if (frames.count(frame_number))
88 return frames[frame_number];
92 return std::shared_ptr<Frame>();
99 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
101 std::vector<std::shared_ptr<openshot::Frame>> all_frames;
102 std::vector<int64_t>::iterator itr_ordered;
105 int64_t frame_number = *itr_ordered;
106 all_frames.push_back(
GetFrame(frame_number));
116 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
119 std::deque<int64_t>::iterator itr;
120 int64_t smallest_frame = -1;
121 for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
123 if (*itr < smallest_frame || smallest_frame == -1)
124 smallest_frame = *itr;
128 if (smallest_frame != -1) {
129 return frames[smallest_frame];
139 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
141 int64_t total_bytes = 0;
144 std::deque<int64_t>::reverse_iterator itr;
145 for(itr = frame_numbers.rbegin(); itr != frame_numbers.rend(); ++itr)
147 total_bytes += frames[*itr]->GetBytes();
156 Remove(frame_number, frame_number);
163 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
166 std::deque<int64_t>::iterator itr;
167 for(itr = frame_numbers.begin(); itr != frame_numbers.end();)
169 if (*itr >= start_frame_number && *itr <= end_frame_number)
172 itr = frame_numbers.erase(itr);
178 std::vector<int64_t>::iterator itr_ordered;
181 if (*itr_ordered >= start_frame_number && *itr_ordered <= end_frame_number)
184 frames.erase(*itr_ordered);
198 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
201 if (frames.count(frame_number))
204 std::deque<int64_t>::iterator itr;
205 for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
207 if (*itr == frame_number)
210 frame_numbers.erase(itr);
213 frame_numbers.push_front(frame_number);
224 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
227 frame_numbers.clear();
228 frame_numbers.shrink_to_fit();
238 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
241 return frames.size();
245void CacheMemory::CleanUp()
251 const std::lock_guard<std::recursive_mutex> lock(*
cacheMutex);
256 int64_t frame_to_remove = frame_numbers.back();
287 root[
"ranges"] = ranges;
304 catch (
const std::exception& e)
307 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
320 if (!root[
"type"].isNull())
Header file for CacheMemory class.
Header file for all Exception classes.
Header file for Frame class.
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 MoveToFront(int64_t frame_number)
Move frame to front of queue (so it lasts longer)
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.
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.
const Json::Value stringToJson(const std::string value)