2011-10-11 08:44:27 -05:00
|
|
|
/**
|
2013-09-08 23:09:54 -05:00
|
|
|
* @file
|
|
|
|
|
* @brief Source file for Cache class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* LICENSE
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2019-06-11 06:48:32 -04:00
|
|
|
* Copyright (c) 2008-2019 OpenShot Studios, LLC
|
2014-03-29 18:49:22 -05:00
|
|
|
* <http://www.openshotstudios.com/>. This file is part of
|
|
|
|
|
* OpenShot Library (libopenshot), an open-source project dedicated to
|
|
|
|
|
* delivering high quality video editing and animation solutions to the
|
|
|
|
|
* world. For more information visit <http://www.openshot.org/>.
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is free software: you can redistribute it
|
2014-07-11 16:52:14 -05:00
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* as published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is distributed in the hope that it will be
|
|
|
|
|
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2014-07-11 16:52:14 -05:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2014-07-11 16:52:14 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
2011-10-11 08:44:27 -05:00
|
|
|
*/
|
|
|
|
|
|
2020-10-18 07:43:37 -04:00
|
|
|
#include "CacheMemory.h"
|
2021-01-26 10:52:04 -05:00
|
|
|
#include "Exceptions.h"
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2016-08-31 02:02:54 -05:00
|
|
|
// Default constructor, no max bytes
|
2016-08-31 23:57:06 -05:00
|
|
|
CacheMemory::CacheMemory() : CacheBase(0) {
|
2016-09-07 00:40:01 -05:00
|
|
|
// Set cache type name
|
|
|
|
|
cache_type = "CacheMemory";
|
|
|
|
|
range_version = 0;
|
|
|
|
|
needs_range_processing = false;
|
2019-12-27 01:01:48 -05:00
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2016-08-31 02:02:54 -05:00
|
|
|
// Constructor that sets the max bytes to cache
|
2017-09-28 16:03:01 -05:00
|
|
|
CacheMemory::CacheMemory(int64_t max_bytes) : CacheBase(max_bytes) {
|
2016-09-07 00:40:01 -05:00
|
|
|
// Set cache type name
|
|
|
|
|
cache_type = "CacheMemory";
|
|
|
|
|
range_version = 0;
|
|
|
|
|
needs_range_processing = false;
|
2019-12-27 01:01:48 -05:00
|
|
|
}
|
2015-06-01 00:20:14 -07:00
|
|
|
|
|
|
|
|
// Default destructor
|
2016-08-31 23:57:06 -05:00
|
|
|
CacheMemory::~CacheMemory()
|
2015-06-01 00:20:14 -07:00
|
|
|
{
|
|
|
|
|
frames.clear();
|
|
|
|
|
frame_numbers.clear();
|
2016-09-07 00:40:01 -05:00
|
|
|
ordered_frame_numbers.clear();
|
2015-06-01 00:20:14 -07:00
|
|
|
|
|
|
|
|
// remove critical section
|
|
|
|
|
delete cacheCriticalSection;
|
|
|
|
|
cacheCriticalSection = NULL;
|
|
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2016-09-07 00:40:01 -05:00
|
|
|
|
|
|
|
|
// Calculate ranges of frames
|
|
|
|
|
void CacheMemory::CalculateRanges() {
|
|
|
|
|
// Only calculate when something has changed
|
|
|
|
|
if (needs_range_processing) {
|
|
|
|
|
|
|
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
|
|
|
|
|
|
|
|
|
// Sort ordered frame #s, and calculate JSON ranges
|
|
|
|
|
std::sort(ordered_frame_numbers.begin(), ordered_frame_numbers.end());
|
|
|
|
|
|
|
|
|
|
// Clear existing JSON variable
|
2016-09-08 22:17:27 -05:00
|
|
|
Json::Value ranges = Json::Value(Json::arrayValue);
|
2016-09-07 00:40:01 -05:00
|
|
|
|
|
|
|
|
// Increment range version
|
|
|
|
|
range_version++;
|
|
|
|
|
|
2019-08-04 22:34:24 -04:00
|
|
|
std::vector<int64_t>::iterator itr_ordered;
|
2017-09-28 16:03:01 -05:00
|
|
|
int64_t starting_frame = *ordered_frame_numbers.begin();
|
|
|
|
|
int64_t ending_frame = *ordered_frame_numbers.begin();
|
2016-09-07 00:40:01 -05:00
|
|
|
|
|
|
|
|
// Loop through all known frames (in sequential order)
|
|
|
|
|
for (itr_ordered = ordered_frame_numbers.begin(); itr_ordered != ordered_frame_numbers.end(); ++itr_ordered) {
|
2017-09-28 16:03:01 -05:00
|
|
|
int64_t frame_number = *itr_ordered;
|
2016-09-07 00:40:01 -05:00
|
|
|
if (frame_number - ending_frame > 1) {
|
|
|
|
|
// End of range detected
|
|
|
|
|
Json::Value range;
|
|
|
|
|
|
|
|
|
|
// Add JSON object with start/end attributes
|
2017-09-28 16:03:01 -05:00
|
|
|
// Use strings, since int64_ts are supported in JSON
|
2019-12-27 08:51:51 -05:00
|
|
|
range["start"] = std::to_string(starting_frame);
|
|
|
|
|
range["end"] = std::to_string(ending_frame);
|
2016-09-07 00:40:01 -05:00
|
|
|
ranges.append(range);
|
|
|
|
|
|
|
|
|
|
// Set new starting range
|
|
|
|
|
starting_frame = frame_number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set current frame as end of range, and keep looping
|
|
|
|
|
ending_frame = frame_number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// APPEND FINAL VALUE
|
|
|
|
|
Json::Value range;
|
|
|
|
|
|
|
|
|
|
// Add JSON object with start/end attributes
|
2017-09-28 16:03:01 -05:00
|
|
|
// Use strings, since int64_ts are not supported in JSON
|
2019-12-27 08:51:51 -05:00
|
|
|
range["start"] = std::to_string(starting_frame);
|
|
|
|
|
range["end"] = std::to_string(ending_frame);
|
2016-09-07 00:40:01 -05:00
|
|
|
ranges.append(range);
|
|
|
|
|
|
2016-09-08 22:17:27 -05:00
|
|
|
// Cache range JSON as string
|
|
|
|
|
json_ranges = ranges.toStyledString();
|
|
|
|
|
|
2016-09-07 00:40:01 -05:00
|
|
|
// Reset needs_range_processing
|
|
|
|
|
needs_range_processing = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
// Add a Frame to the cache
|
2017-08-20 17:37:39 -05:00
|
|
|
void CacheMemory::Add(std::shared_ptr<Frame> frame)
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
2017-09-28 16:03:01 -05:00
|
|
|
int64_t frame_number = frame->number;
|
2015-06-01 00:20:14 -07:00
|
|
|
|
2016-05-08 19:14:19 -04:00
|
|
|
// Freshen frame if it already exists
|
2015-08-05 23:40:58 -05:00
|
|
|
if (frames.count(frame_number))
|
2012-10-12 16:41:23 -05:00
|
|
|
// Move frame to front of queue
|
|
|
|
|
MoveToFront(frame_number);
|
2015-08-05 23:40:58 -05:00
|
|
|
|
2012-10-12 16:41:23 -05:00
|
|
|
else
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
|
|
|
|
// Add frame to queue and map
|
|
|
|
|
frames[frame_number] = frame;
|
2011-10-26 00:34:48 -05:00
|
|
|
frame_numbers.push_front(frame_number);
|
2016-09-07 00:40:01 -05:00
|
|
|
ordered_frame_numbers.push_back(frame_number);
|
|
|
|
|
needs_range_processing = true;
|
2012-10-11 17:30:32 -05:00
|
|
|
|
|
|
|
|
// Clean up old frames
|
|
|
|
|
CleanUp();
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-05 23:40:58 -05:00
|
|
|
// Get a frame from the cache (or NULL shared_ptr if no frame is found)
|
2017-09-28 16:03:01 -05:00
|
|
|
std::shared_ptr<Frame> CacheMemory::GetFrame(int64_t frame_number)
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
// Does frame exists in cache?
|
2015-08-05 23:40:58 -05:00
|
|
|
if (frames.count(frame_number))
|
2011-10-11 08:44:27 -05:00
|
|
|
// return the Frame object
|
|
|
|
|
return frames[frame_number];
|
2015-08-05 23:40:58 -05:00
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
else
|
2015-08-05 23:40:58 -05:00
|
|
|
// no Frame found
|
2017-08-20 17:37:39 -05:00
|
|
|
return std::shared_ptr<Frame>();
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
|
2015-08-05 23:40:58 -05:00
|
|
|
// Get the smallest frame number (or NULL shared_ptr if no frame is found)
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> CacheMemory::GetSmallestFrame()
|
2011-10-24 08:22:21 -05:00
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<openshot::Frame> f;
|
2013-02-12 01:28:48 -06:00
|
|
|
|
2016-05-08 19:14:19 -04:00
|
|
|
// Loop through frame numbers
|
2019-08-04 22:34:24 -04:00
|
|
|
std::deque<int64_t>::iterator itr;
|
2017-09-28 16:03:01 -05:00
|
|
|
int64_t smallest_frame = -1;
|
2016-05-08 19:14:19 -04:00
|
|
|
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
|
|
|
|
|
{
|
|
|
|
|
if (*itr < smallest_frame || smallest_frame == -1)
|
|
|
|
|
smallest_frame = *itr;
|
|
|
|
|
}
|
2011-10-26 00:34:48 -05:00
|
|
|
|
2016-05-08 19:14:19 -04:00
|
|
|
// Return frame
|
|
|
|
|
f = GetFrame(smallest_frame);
|
2013-02-12 01:28:48 -06:00
|
|
|
|
2016-05-08 19:14:19 -04:00
|
|
|
return f;
|
2011-10-26 00:34:48 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
// Gets the maximum bytes value
|
2017-09-28 16:03:01 -05:00
|
|
|
int64_t CacheMemory::GetBytes()
|
2015-06-01 00:20:14 -07:00
|
|
|
{
|
|
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
|
|
|
|
|
2017-09-28 16:03:01 -05:00
|
|
|
int64_t total_bytes = 0;
|
2015-06-01 00:20:14 -07:00
|
|
|
|
|
|
|
|
// Loop through frames, and calculate total bytes
|
2019-08-04 22:34:24 -04:00
|
|
|
std::deque<int64_t>::reverse_iterator itr;
|
2015-06-01 00:20:14 -07:00
|
|
|
for(itr = frame_numbers.rbegin(); itr != frame_numbers.rend(); ++itr)
|
|
|
|
|
{
|
|
|
|
|
total_bytes += frames[*itr]->GetBytes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return total_bytes;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 00:34:48 -05:00
|
|
|
// Remove a specific frame
|
2017-09-28 16:03:01 -05:00
|
|
|
void CacheMemory::Remove(int64_t frame_number)
|
2016-09-07 00:40:01 -05:00
|
|
|
{
|
|
|
|
|
Remove(frame_number, frame_number);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove range of frames
|
2017-09-28 16:03:01 -05:00
|
|
|
void CacheMemory::Remove(int64_t start_frame_number, int64_t end_frame_number)
|
2011-10-26 00:34:48 -05:00
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
2012-10-11 17:30:32 -05:00
|
|
|
|
2011-10-26 00:34:48 -05:00
|
|
|
// Loop through frame numbers
|
2019-08-04 22:34:24 -04:00
|
|
|
std::deque<int64_t>::iterator itr;
|
2016-09-08 00:58:12 -05:00
|
|
|
for(itr = frame_numbers.begin(); itr != frame_numbers.end();)
|
2012-07-05 00:01:42 -05:00
|
|
|
{
|
2016-09-07 00:40:01 -05:00
|
|
|
if (*itr >= start_frame_number && *itr <= end_frame_number)
|
2012-07-05 00:01:42 -05:00
|
|
|
{
|
|
|
|
|
// erase frame number
|
2016-09-08 00:58:12 -05:00
|
|
|
itr = frame_numbers.erase(itr);
|
2016-09-07 00:40:01 -05:00
|
|
|
}else
|
2016-09-08 00:58:12 -05:00
|
|
|
itr++;
|
2012-07-05 00:01:42 -05:00
|
|
|
}
|
2011-10-26 00:34:48 -05:00
|
|
|
|
2016-09-07 00:40:01 -05:00
|
|
|
// Loop through ordered frame numbers
|
2019-08-04 22:34:24 -04:00
|
|
|
std::vector<int64_t>::iterator itr_ordered;
|
2016-09-08 00:58:12 -05:00
|
|
|
for(itr_ordered = ordered_frame_numbers.begin(); itr_ordered != ordered_frame_numbers.end();)
|
2016-09-07 00:40:01 -05:00
|
|
|
{
|
|
|
|
|
if (*itr_ordered >= start_frame_number && *itr_ordered <= end_frame_number)
|
|
|
|
|
{
|
|
|
|
|
// erase frame number
|
|
|
|
|
frames.erase(*itr_ordered);
|
2016-09-08 00:58:12 -05:00
|
|
|
itr_ordered = ordered_frame_numbers.erase(itr_ordered);
|
2016-09-07 00:40:01 -05:00
|
|
|
}else
|
2016-09-08 00:58:12 -05:00
|
|
|
itr_ordered++;
|
2016-09-07 00:40:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Needs range processing (since cache has changed)
|
|
|
|
|
needs_range_processing = true;
|
2011-10-24 08:22:21 -05:00
|
|
|
}
|
|
|
|
|
|
2012-10-12 16:41:23 -05:00
|
|
|
// Move frame to front of queue (so it lasts longer)
|
2017-09-28 16:03:01 -05:00
|
|
|
void CacheMemory::MoveToFront(int64_t frame_number)
|
2012-10-12 16:41:23 -05:00
|
|
|
{
|
2017-08-20 17:37:39 -05:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
|
|
|
|
|
2012-10-12 16:41:23 -05:00
|
|
|
// Does frame exists in cache?
|
2015-08-05 23:40:58 -05:00
|
|
|
if (frames.count(frame_number))
|
2012-10-12 16:41:23 -05:00
|
|
|
{
|
|
|
|
|
// Loop through frame numbers
|
2019-08-04 22:34:24 -04:00
|
|
|
std::deque<int64_t>::iterator itr;
|
2012-10-12 16:41:23 -05:00
|
|
|
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
|
|
|
|
|
{
|
|
|
|
|
if (*itr == frame_number)
|
|
|
|
|
{
|
|
|
|
|
// erase frame number
|
|
|
|
|
frame_numbers.erase(itr);
|
|
|
|
|
|
|
|
|
|
// add frame number to 'front' of queue
|
|
|
|
|
frame_numbers.push_front(frame_number);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
// Clear the cache of all frames
|
2016-08-31 23:57:06 -05:00
|
|
|
void CacheMemory::Clear()
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
2012-08-15 17:27:14 -05:00
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
frames.clear();
|
|
|
|
|
frame_numbers.clear();
|
2016-09-07 00:40:01 -05:00
|
|
|
ordered_frame_numbers.clear();
|
2017-03-10 00:51:08 -06:00
|
|
|
needs_range_processing = true;
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Count the frames in the queue
|
2017-09-28 16:03:01 -05:00
|
|
|
int64_t CacheMemory::Count()
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
// Return the number of frames in the cache
|
|
|
|
|
return frames.size();
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-11 17:30:32 -05:00
|
|
|
// Clean up cached frames that exceed the number in our max_bytes variable
|
2016-08-31 23:57:06 -05:00
|
|
|
void CacheMemory::CleanUp()
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
2012-10-10 17:27:46 -05:00
|
|
|
// Do we auto clean up?
|
2012-10-11 17:30:32 -05:00
|
|
|
if (max_bytes > 0)
|
2012-10-10 15:21:33 -05:00
|
|
|
{
|
2016-09-08 15:10:58 -05:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
|
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
while (GetBytes() > max_bytes && frame_numbers.size() > 20)
|
2012-10-10 17:27:46 -05:00
|
|
|
{
|
2016-05-08 19:14:19 -04:00
|
|
|
// Get the oldest frame number.
|
2017-09-28 16:03:01 -05:00
|
|
|
int64_t frame_to_remove = frame_numbers.back();
|
2012-07-05 00:01:42 -05:00
|
|
|
|
2012-10-10 17:27:46 -05:00
|
|
|
// Remove frame_number and frame
|
|
|
|
|
Remove(frame_to_remove);
|
2012-07-05 00:01:42 -05:00
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-09-07 00:40:01 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generate JSON string of this object
|
2019-08-04 22:34:24 -04:00
|
|
|
std::string CacheMemory::Json() {
|
2016-09-07 00:40:01 -05:00
|
|
|
|
|
|
|
|
// Return formatted string
|
|
|
|
|
return JsonValue().toStyledString();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 08:51:51 -05:00
|
|
|
// Generate Json::Value for this object
|
2016-09-07 00:40:01 -05:00
|
|
|
Json::Value CacheMemory::JsonValue() {
|
|
|
|
|
|
2018-01-06 02:22:05 -06:00
|
|
|
// Process range data (if anything has changed)
|
2016-09-07 00:40:01 -05:00
|
|
|
CalculateRanges();
|
|
|
|
|
|
|
|
|
|
// Create root json object
|
|
|
|
|
Json::Value root = CacheBase::JsonValue(); // get parent properties
|
|
|
|
|
root["type"] = cache_type;
|
|
|
|
|
|
2019-12-27 08:51:51 -05:00
|
|
|
root["version"] = std::to_string(range_version);
|
2016-09-07 00:40:01 -05:00
|
|
|
|
2016-09-08 22:17:27 -05:00
|
|
|
// Parse and append range data (if any)
|
2019-12-27 08:51:51 -05:00
|
|
|
try {
|
|
|
|
|
const Json::Value ranges = openshot::stringToJson(json_ranges);
|
2016-09-08 22:17:27 -05:00
|
|
|
root["ranges"] = ranges;
|
2019-12-27 08:51:51 -05:00
|
|
|
} catch (...) { }
|
2016-09-08 22:17:27 -05:00
|
|
|
|
2016-09-07 00:40:01 -05:00
|
|
|
// return JsonValue
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load JSON string into this object
|
2019-12-27 08:51:51 -05:00
|
|
|
void CacheMemory::SetJson(const std::string value) {
|
2016-09-07 00:40:01 -05:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-12-27 08:51:51 -05:00
|
|
|
// Parse string to Json::Value
|
|
|
|
|
const Json::Value root = openshot::stringToJson(value);
|
2016-09-07 00:40:01 -05:00
|
|
|
// Set all values that match
|
|
|
|
|
SetJsonValue(root);
|
|
|
|
|
}
|
2019-07-03 12:58:02 -04:00
|
|
|
catch (const std::exception& e)
|
2016-09-07 00:40:01 -05:00
|
|
|
{
|
|
|
|
|
// Error parsing JSON (or missing keys)
|
2019-08-27 15:47:39 -04:00
|
|
|
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
|
2016-09-07 00:40:01 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 08:51:51 -05:00
|
|
|
// Load Json::Value into this object
|
|
|
|
|
void CacheMemory::SetJsonValue(const Json::Value root) {
|
2016-09-07 00:40:01 -05:00
|
|
|
|
|
|
|
|
// Close timeline before we do anything (this also removes all open and closing clips)
|
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
|
|
// Set parent data
|
|
|
|
|
CacheBase::SetJsonValue(root);
|
|
|
|
|
|
|
|
|
|
if (!root["type"].isNull())
|
|
|
|
|
cache_type = root["type"].asString();
|
2017-10-26 18:44:35 -05:00
|
|
|
}
|