Loading...
Searching...
No Matches
CacheBase.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "CacheBase.h"
using namespace std;
using namespace openshot;
// Default constructor, no max frames
CacheBase::CacheBase() : CacheBase::CacheBase(0) { }
// Constructor that sets the max frames to cache
CacheBase::CacheBase(int64_t max_bytes) : max_bytes(max_bytes) {
// Init the mutex
cacheMutex = new std::recursive_mutex();
}
// Set maximum bytes to a different amount based on a ReaderInfo struct
void CacheBase::SetMaxBytesFromInfo(int64_t number_of_frames, int width, int height, int sample_rate, int channels)
{
// n frames X height X width X 4 colors of chars X audio channels X 4 byte floats
int64_t bytes = number_of_frames * (height * width * 4 + (sample_rate * channels * 4));
SetMaxBytes(bytes);
}
// Calculate ranges of frames
void CacheBase::CalculateRanges() {
// Only calculate when something has changed
if (needs_range_processing) {
// Create a scoped lock, to protect the cache from multiple threads
const std::lock_guard<std::recursive_mutex> lock(*cacheMutex);
// Sort ordered frame #s, and calculate JSON ranges
std::sort(ordered_frame_numbers.begin(), ordered_frame_numbers.end());
// Clear existing JSON variable
Json::Value ranges = Json::Value(Json::arrayValue);
// Increment range version
range_version++;
std::vector<int64_t>::iterator itr_ordered;
int64_t starting_frame = 0;
int64_t ending_frame = 0;
if (ordered_frame_numbers.size() > 0) {
starting_frame = *ordered_frame_numbers.begin();
ending_frame = *ordered_frame_numbers.begin();
// Loop through all known frames (in sequential order)
for (itr_ordered = ordered_frame_numbers.begin(); itr_ordered != ordered_frame_numbers.end(); ++itr_ordered) {
int64_t frame_number = *itr_ordered;
if (frame_number - ending_frame > 1) {
// End of range detected
Json::Value range;
// Add JSON object with start/end attributes
// Use strings, since int64_ts are supported in JSON
range["start"] = std::to_string(starting_frame);
range["end"] = std::to_string(ending_frame);
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
// Use strings, since int64_ts are not supported in JSON
range["start"] = std::to_string(starting_frame);
range["end"] = std::to_string(ending_frame);
ranges.append(range);
// Cache range JSON as string
json_ranges = ranges.toStyledString();
// Reset needs_range_processing
needs_range_processing = false;
}
}
// Generate Json::Value for this object
Json::Value CacheBase::JsonValue() {
// Create root json object
Json::Value root;
root["max_bytes"] = std::to_string(max_bytes);
// return JsonValue
return root;
}
// Load Json::Value into this object
void CacheBase::SetJsonValue(const Json::Value root) {
// Set data from Json (if key is found)
if (!root["max_bytes"].isNull())
max_bytes = std::stoll(root["max_bytes"].asString());
}
CacheBase.h
Header file for CacheBase class.
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::CacheBase
CacheBase()
Default constructor, no max bytes.
Definition CacheBase.cpp:19
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::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition CacheBase.cpp:111
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::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::cacheMutex
std::recursive_mutex * cacheMutex
Mutex for multiple threads.
Definition CacheBase.h:47
openshot::CacheBase::json_ranges
std::string json_ranges
JSON ranges of frame numbers.
Definition CacheBase.h:41
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::CacheBase::SetMaxBytes
void SetMaxBytes(int64_t number_of_bytes)
Set maximum bytes to a different amount.
Definition CacheBase.h:105
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29