diff --git a/src/CacheBase.cpp b/src/CacheBase.cpp index 4ba49016..5ad9782c 100644 --- a/src/CacheBase.cpp +++ b/src/CacheBase.cpp @@ -34,6 +34,70 @@ void CacheBase::SetMaxBytesFromInfo(int64_t number_of_frames, int width, int hei 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 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::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() { diff --git a/src/CacheBase.h b/src/CacheBase.h index 33c25e30..2c31c9be 100644 --- a/src/CacheBase.h +++ b/src/CacheBase.h @@ -13,8 +13,11 @@ #ifndef OPENSHOT_CACHE_BASE_H #define OPENSHOT_CACHE_BASE_H +#include +#include #include #include +#include #include "Json.h" @@ -34,9 +37,17 @@ namespace openshot { std::string cache_type; ///< This is a friendly type name of the derived cache instance int64_t max_bytes; ///< This is the max number of bytes to cache (0 = no limit) + bool needs_range_processing; ///< Something has changed, and the range data needs to be re-calculated + std::string json_ranges; ///< JSON ranges of frame numbers + std::vector ordered_frame_numbers; ///< Ordered list of frame numbers used by cache + std::map frame_ranges; ///< This map holds the ranges of frames, useful for quickly displaying the contents of the cache + int64_t range_version; ///< The version of the JSON range data (incremented with each change) + /// Mutex for multiple threads std::recursive_mutex *cacheMutex; + /// Calculate ranges of frames + void CalculateRanges(); public: /// Default constructor, no max bytes diff --git a/src/CacheDisk.cpp b/src/CacheDisk.cpp index 337337e5..f969899b 100644 --- a/src/CacheDisk.cpp +++ b/src/CacheDisk.cpp @@ -75,63 +75,6 @@ void CacheDisk::InitPath(std::string cache_path) { path.mkpath(qpath); } -// Calculate ranges of frames -void CacheDisk::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 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++; - - int64_t starting_frame = *ordered_frame_numbers.begin(); - int64_t ending_frame = starting_frame; - - // Loop through all known frames (in sequential order) - for (const auto frame_number : ordered_frame_numbers) { - 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 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; - } -} - // Default destructor CacheDisk::~CacheDisk() { diff --git a/src/CacheDisk.h b/src/CacheDisk.h index 6a4fd8d1..50a2dc67 100644 --- a/src/CacheDisk.h +++ b/src/CacheDisk.h @@ -13,10 +13,6 @@ #ifndef OPENSHOT_CACHE_DISK_H #define OPENSHOT_CACHE_DISK_H -#include -#include -#include - #include "CacheBase.h" #include @@ -39,13 +35,7 @@ namespace openshot { std::string image_format; float image_quality; float image_scale; - int64_t frame_size_bytes; ///< The size of the cached frame in bytes - bool needs_range_processing; ///< Something has changed, and the range data needs to be re-calculated - std::string json_ranges; ///< JSON ranges of frame numbers - std::vector ordered_frame_numbers; ///< Ordered list of frame numbers used by cache - std::map frame_ranges; ///< This map holds the ranges of frames, useful for quickly displaying the contents of the cache - int64_t 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(); @@ -53,9 +43,6 @@ namespace openshot { /// Init path directory void InitPath(std::string cache_path); - /// Calculate ranges of frames - void CalculateRanges(); - public: /// @brief Default constructor, no max bytes /// @param cache_path The folder path of the cache directory (empty string = /tmp/preview-cache/) diff --git a/src/CacheMemory.cpp b/src/CacheMemory.cpp index 9f865b2d..ce30c077 100644 --- a/src/CacheMemory.cpp +++ b/src/CacheMemory.cpp @@ -42,66 +42,6 @@ CacheMemory::~CacheMemory() delete cacheMutex; } - -// 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 std::lock_guard 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::iterator itr_ordered; - int64_t starting_frame = *ordered_frame_numbers.begin(); - int64_t 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; - } -} - // Add a Frame to the cache void CacheMemory::Add(std::shared_ptr frame) { diff --git a/src/CacheMemory.h b/src/CacheMemory.h index c061acb6..03d3a415 100644 --- a/src/CacheMemory.h +++ b/src/CacheMemory.h @@ -13,10 +13,6 @@ #ifndef OPENSHOT_CACHE_MEMORY_H #define OPENSHOT_CACHE_MEMORY_H -#include -#include -#include - #include "CacheBase.h" namespace openshot { @@ -35,18 +31,9 @@ namespace openshot { std::map > frames; ///< This map holds the frame number and Frame objects std::deque 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 - std::string json_ranges; ///< JSON ranges of frame numbers - std::vector ordered_frame_numbers; ///< Ordered list of frame numbers used by cache - std::map frame_ranges; ///< This map holds the ranges of frames, useful for quickly displaying the contents of the cache - int64_t 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 CacheMemory();