You've already forked libopenshot
mirror of
https://github.com/OpenShot/libopenshot.git
synced 2026-03-02 08:53:52 -08:00
More changes looking for build server breakage
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
#include <tr1/memory>
|
||||
#include "Frame.h"
|
||||
#include "Exceptions.h"
|
||||
#include "Json.h"
|
||||
|
||||
namespace openshot {
|
||||
|
||||
@@ -44,7 +45,8 @@ namespace openshot {
|
||||
class CacheBase
|
||||
{
|
||||
protected:
|
||||
int64 max_bytes; ///< This is the max number of bytes to cache (0 = no limit)
|
||||
string cache_type; ///< This is a friendly type name of the derived cache instance
|
||||
long long int max_bytes; ///< This is the max number of bytes to cache (0 = no limit)
|
||||
|
||||
/// Section lock for multiple threads
|
||||
CriticalSection *cacheCriticalSection;
|
||||
@@ -56,7 +58,7 @@ namespace openshot {
|
||||
|
||||
/// @brief Constructor that sets the max bytes to cache
|
||||
/// @param max_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
|
||||
CacheBase(int64 max_bytes);
|
||||
CacheBase(long long int max_bytes);
|
||||
|
||||
/// @brief Add a Frame to the cache
|
||||
/// @param frame The openshot::Frame object needing to be cached.
|
||||
@@ -73,7 +75,7 @@ namespace openshot {
|
||||
virtual tr1::shared_ptr<Frame> GetFrame(long int frame_number) = 0;
|
||||
|
||||
/// Gets the maximum bytes value
|
||||
virtual int64 GetBytes() = 0;
|
||||
virtual long long int GetBytes() = 0;
|
||||
|
||||
/// Get the smallest frame number
|
||||
virtual tr1::shared_ptr<Frame> GetSmallestFrame() = 0;
|
||||
@@ -83,11 +85,11 @@ namespace openshot {
|
||||
virtual void Remove(long int frame_number) = 0;
|
||||
|
||||
/// Gets the maximum bytes value
|
||||
int64 GetMaxBytes() { return max_bytes; };
|
||||
long long int GetMaxBytes() { return max_bytes; };
|
||||
|
||||
/// @brief Set maximum bytes to a different amount
|
||||
/// @param number_of_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
|
||||
void SetMaxBytes(int64 number_of_bytes) { max_bytes = number_of_bytes; };
|
||||
void SetMaxBytes(long long int number_of_bytes) { max_bytes = number_of_bytes; };
|
||||
|
||||
/// @brief Set maximum bytes to a different amount based on a ReaderInfo struct
|
||||
/// @param number_of_frames The maximum number of frames to hold in cache
|
||||
@@ -97,6 +99,11 @@ namespace openshot {
|
||||
/// @param channels The number of audio channels in the frame
|
||||
void SetMaxBytesFromInfo(long int number_of_frames, int width, int height, int sample_rate, int channels);
|
||||
|
||||
/// Get and Set JSON methods
|
||||
virtual string Json() = 0; ///< Generate JSON string of this object
|
||||
virtual void SetJson(string value) throw(InvalidJSON) = 0; ///< Load JSON string into this object
|
||||
virtual Json::Value JsonValue() = 0; ///< Generate Json::JsonValue for this object
|
||||
virtual void SetJsonValue(Json::Value root) = 0; ///< Load Json::JsonValue into this object
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -25,8 +25,8 @@
|
||||
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef OPENSHOT_CACHE_H
|
||||
#define OPENSHOT_CACHE_H
|
||||
#ifndef OPENSHOT_CACHE_MEMORY_H
|
||||
#define OPENSHOT_CACHE_MEMORY_H
|
||||
|
||||
#include <map>
|
||||
#include <deque>
|
||||
@@ -38,7 +38,7 @@
|
||||
namespace openshot {
|
||||
|
||||
/**
|
||||
* @brief This class is a cache manager for Frame objects.
|
||||
* @brief This class is a memory-based cache manager for Frame objects.
|
||||
*
|
||||
* It is used by FileReaders (such as FFmpegReader) to cache recently accessed frames. Due to the
|
||||
* high cost of decoding streams, once a frame is decoded, converted to RGB, and a Frame object is created,
|
||||
@@ -50,9 +50,17 @@ namespace openshot {
|
||||
map<long int, tr1::shared_ptr<Frame> > frames; ///< This map holds the frame number and Frame objects
|
||||
deque<long int> frame_numbers; ///< This queue holds a sequential list of cached Frame numbers
|
||||
|
||||
bool needs_range_processing; ///< Something has changed, and the range data needs to be re-calculated
|
||||
Json::Value ranges; ///< JSON ranges of frame numbers
|
||||
vector<long int> ordered_frame_numbers; ///< Ordered list of frame numbers used by cache
|
||||
map<long int, long int> frame_ranges; ///< This map holds the ranges of frames, useful for quickly displaying the contents of the cache
|
||||
long int range_version; ///< The version of the JSON range data (incremented with each change)
|
||||
|
||||
/// Clean up cached frames that exceed the max number of bytes
|
||||
void CleanUp();
|
||||
|
||||
/// Calculate ranges of frames
|
||||
void CalculateRanges();
|
||||
|
||||
public:
|
||||
/// Default constructor, no max bytes
|
||||
@@ -60,7 +68,7 @@ namespace openshot {
|
||||
|
||||
/// @brief Constructor that sets the max bytes to cache
|
||||
/// @param max_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
|
||||
CacheMemory(int64 max_bytes);
|
||||
CacheMemory(long long int max_bytes);
|
||||
|
||||
// Default destructor
|
||||
~CacheMemory();
|
||||
@@ -80,7 +88,7 @@ namespace openshot {
|
||||
tr1::shared_ptr<Frame> GetFrame(long int frame_number);
|
||||
|
||||
/// Gets the maximum bytes value
|
||||
int64 GetBytes();
|
||||
long long int GetBytes();
|
||||
|
||||
/// Get the smallest frame number
|
||||
tr1::shared_ptr<Frame> GetSmallestFrame();
|
||||
@@ -93,6 +101,11 @@ namespace openshot {
|
||||
/// @param frame_number The frame number of the cached frame
|
||||
void Remove(long int frame_number);
|
||||
|
||||
/// Get and Set JSON methods
|
||||
string Json(); ///< Generate JSON string of this object
|
||||
void SetJson(string value) throw(InvalidJSON); ///< Load JSON string into this object
|
||||
Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
|
||||
void SetJsonValue(Json::Value root) throw(InvalidFile, ReaderClosed); ///< Load Json::JsonValue into this object
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ CacheBase::CacheBase() : max_bytes(0) {
|
||||
};
|
||||
|
||||
// Constructor that sets the max frames to cache
|
||||
CacheBase::CacheBase(int64 max_bytes) : max_bytes(max_bytes) {
|
||||
CacheBase::CacheBase(long long int max_bytes) : max_bytes(max_bytes) {
|
||||
// Init the critical section
|
||||
cacheCriticalSection = new CriticalSection();
|
||||
};
|
||||
@@ -46,7 +46,27 @@ CacheBase::CacheBase(int64 max_bytes) : max_bytes(max_bytes) {
|
||||
void CacheBase::SetMaxBytesFromInfo(long int 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 bytes = number_of_frames * (height * width * 4 + (sample_rate * channels * 4));
|
||||
long long int bytes = number_of_frames * (height * width * 4 + (sample_rate * channels * 4));
|
||||
SetMaxBytes(bytes);
|
||||
}
|
||||
|
||||
// Generate Json::JsonValue for this object
|
||||
Json::Value CacheBase::JsonValue() {
|
||||
|
||||
// Create root json object
|
||||
Json::Value root;
|
||||
stringstream max_bytes_stream;
|
||||
max_bytes_stream << max_bytes;
|
||||
root["max_bytes"] = max_bytes_stream.str();
|
||||
|
||||
// return JsonValue
|
||||
return root;
|
||||
}
|
||||
|
||||
// Load Json::JsonValue into this object
|
||||
void CacheBase::SetJsonValue(Json::Value root) {
|
||||
|
||||
// Set data from Json (if key is found)
|
||||
if (!root["max_bytes"].isNull())
|
||||
max_bytes = atoll(root["max_bytes"].asString().c_str());
|
||||
}
|
||||
@@ -32,12 +32,18 @@ using namespace openshot;
|
||||
|
||||
// Default constructor, no max bytes
|
||||
CacheMemory::CacheMemory() : CacheBase(0) {
|
||||
|
||||
// Set cache type name
|
||||
cache_type = "CacheMemory";
|
||||
range_version = 0;
|
||||
needs_range_processing = false;
|
||||
};
|
||||
|
||||
// Constructor that sets the max bytes to cache
|
||||
CacheMemory::CacheMemory(int64 max_bytes) : CacheBase(max_bytes) {
|
||||
|
||||
CacheMemory::CacheMemory(long long int max_bytes) : CacheBase(max_bytes) {
|
||||
// Set cache type name
|
||||
cache_type = "CacheMemory";
|
||||
range_version = 0;
|
||||
needs_range_processing = false;
|
||||
};
|
||||
|
||||
// Default destructor
|
||||
@@ -45,12 +51,78 @@ CacheMemory::~CacheMemory()
|
||||
{
|
||||
frames.clear();
|
||||
frame_numbers.clear();
|
||||
ordered_frame_numbers.clear();
|
||||
|
||||
// remove critical section
|
||||
delete cacheCriticalSection;
|
||||
cacheCriticalSection = NULL;
|
||||
}
|
||||
|
||||
// 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
|
||||
ranges.clear();
|
||||
ranges = Json::Value(Json::arrayValue);
|
||||
|
||||
// Increment range version
|
||||
range_version++;
|
||||
|
||||
vector<long int>::iterator itr_ordered;
|
||||
long int starting_frame = *ordered_frame_numbers.begin();
|
||||
long int 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) {
|
||||
long int 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 long ints are supported in JSON
|
||||
stringstream start_str;
|
||||
start_str << starting_frame;
|
||||
stringstream end_str;
|
||||
end_str << ending_frame;
|
||||
range["start"] = start_str.str();
|
||||
range["end"] = end_str.str();
|
||||
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 long ints are supported in JSON
|
||||
stringstream start_str;
|
||||
start_str << starting_frame;
|
||||
stringstream end_str;
|
||||
end_str << ending_frame;
|
||||
range["start"] = start_str.str();
|
||||
range["end"] = end_str.str();
|
||||
ranges.append(range);
|
||||
|
||||
// Reset needs_range_processing
|
||||
needs_range_processing = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Add a Frame to the cache
|
||||
void CacheMemory::Add(tr1::shared_ptr<Frame> frame)
|
||||
{
|
||||
@@ -68,6 +140,8 @@ void CacheMemory::Add(tr1::shared_ptr<Frame> frame)
|
||||
// Add frame to queue and map
|
||||
frames[frame_number] = frame;
|
||||
frame_numbers.push_front(frame_number);
|
||||
ordered_frame_numbers.push_back(frame_number);
|
||||
needs_range_processing = true;
|
||||
|
||||
// Clean up old frames
|
||||
CleanUp();
|
||||
@@ -113,12 +187,12 @@ tr1::shared_ptr<Frame> CacheMemory::GetSmallestFrame()
|
||||
}
|
||||
|
||||
// Gets the maximum bytes value
|
||||
int64 CacheMemory::GetBytes()
|
||||
long long int CacheMemory::GetBytes()
|
||||
{
|
||||
// Create a scoped lock, to protect the cache from multiple threads
|
||||
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
||||
|
||||
int64 total_bytes = 0;
|
||||
long long int total_bytes = 0;
|
||||
|
||||
// Loop through frames, and calculate total bytes
|
||||
deque<long int>::reverse_iterator itr;
|
||||
@@ -161,7 +235,7 @@ void CacheMemory::MoveToFront(long int frame_number)
|
||||
// Does frame exists in cache?
|
||||
/* FIXME if the frame number isn't present, the loop will do nothing, so why protect it?
|
||||
* Is it to save time by avoiding a loop?
|
||||
* Do we really need to optmize the case where we've been given a nonexisting frame_number? */
|
||||
* Do we really need to optimize the case where we've been given a nonexisting frame_number? */
|
||||
if (frames.count(frame_number))
|
||||
{
|
||||
// Loop through frame numbers
|
||||
@@ -189,6 +263,7 @@ void CacheMemory::Clear()
|
||||
|
||||
frames.clear();
|
||||
frame_numbers.clear();
|
||||
ordered_frame_numbers.clear();
|
||||
}
|
||||
|
||||
// Count the frames in the queue
|
||||
@@ -220,3 +295,67 @@ void CacheMemory::CleanUp()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Generate JSON string of this object
|
||||
string CacheMemory::Json() {
|
||||
|
||||
// Return formatted string
|
||||
return JsonValue().toStyledString();
|
||||
}
|
||||
|
||||
// Generate Json::JsonValue for this object
|
||||
Json::Value CacheMemory::JsonValue() {
|
||||
|
||||
// Proccess range data (if anything has changed)
|
||||
CalculateRanges();
|
||||
|
||||
// Create root json object
|
||||
Json::Value root = CacheBase::JsonValue(); // get parent properties
|
||||
root["type"] = cache_type;
|
||||
root["ranges"] = ranges;
|
||||
|
||||
Json::Value version;
|
||||
stringstream range_version_str;
|
||||
range_version_str << range_version;
|
||||
root["version"] = range_version_str.str();
|
||||
|
||||
// return JsonValue
|
||||
return root;
|
||||
}
|
||||
|
||||
// Load JSON string into this object
|
||||
void CacheMemory::SetJson(string value) throw(InvalidJSON) {
|
||||
|
||||
// Parse JSON string into JSON objects
|
||||
Json::Value root;
|
||||
Json::Reader reader;
|
||||
bool success = reader.parse( value, root );
|
||||
if (!success)
|
||||
// Raise exception
|
||||
throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
|
||||
|
||||
try
|
||||
{
|
||||
// Set all values that match
|
||||
SetJsonValue(root);
|
||||
}
|
||||
catch (exception e)
|
||||
{
|
||||
// Error parsing JSON (or missing keys)
|
||||
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
|
||||
}
|
||||
}
|
||||
|
||||
// Load Json::JsonValue into this object
|
||||
void CacheMemory::SetJsonValue(Json::Value root) throw(InvalidFile, ReaderClosed) {
|
||||
|
||||
// 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();
|
||||
}
|
||||
Reference in New Issue
Block a user