From d243db8aaa44319f851e8665f08bb0d2f2d9c3db Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Sat, 25 Jun 2022 17:32:36 -0500 Subject: [PATCH] Adding new GetFrames method to CacheBase, to return an ordered vector of Frame objects. Useful when needing to iterate through the current cached frames. --- src/CacheBase.h | 3 +++ src/CacheDisk.cpp | 17 +++++++++++++++++ src/CacheDisk.h | 3 +++ src/CacheMemory.cpp | 17 +++++++++++++++++ src/CacheMemory.h | 3 +++ 5 files changed, 43 insertions(+) diff --git a/src/CacheBase.h b/src/CacheBase.h index 4377a292..f7d69f3c 100644 --- a/src/CacheBase.h +++ b/src/CacheBase.h @@ -64,6 +64,9 @@ namespace openshot { /// @param frame_number The frame number of the cached frame virtual std::shared_ptr GetFrame(int64_t frame_number) = 0; + /// @brief Get an vector of all Frames + virtual std::vector> GetFrames() = 0; + /// Gets the maximum bytes value virtual int64_t GetBytes() = 0; diff --git a/src/CacheDisk.cpp b/src/CacheDisk.cpp index aa4d56a6..ce04092f 100644 --- a/src/CacheDisk.cpp +++ b/src/CacheDisk.cpp @@ -279,6 +279,23 @@ std::shared_ptr CacheDisk::GetFrame(int64_t frame_number) return std::shared_ptr(); } +// @brief Get an array of all Frames +std::vector> CacheDisk::GetFrames() +{ + // Create a scoped lock, to protect the cache from multiple threads + const std::lock_guard lock(*cacheMutex); + + std::vector> all_frames; + std::vector::iterator itr_ordered; + for(itr_ordered = ordered_frame_numbers.begin(); itr_ordered != ordered_frame_numbers.end(); ++itr_ordered) + { + int64_t frame_number = *itr_ordered; + all_frames.push_back(GetFrame(frame_number)); + } + + return all_frames; +} + // Get the smallest frame number (or NULL shared_ptr if no frame is found) std::shared_ptr CacheDisk::GetSmallestFrame() { diff --git a/src/CacheDisk.h b/src/CacheDisk.h index 7611728c..d8ae325d 100644 --- a/src/CacheDisk.h +++ b/src/CacheDisk.h @@ -93,6 +93,9 @@ namespace openshot { /// @param frame_number The frame number of the cached frame std::shared_ptr GetFrame(int64_t frame_number); + /// @brief Get an array of all Frames + std::vector> GetFrames(); + /// Gets the maximum bytes value int64_t GetBytes(); diff --git a/src/CacheMemory.cpp b/src/CacheMemory.cpp index bff20464..463ec0bb 100644 --- a/src/CacheMemory.cpp +++ b/src/CacheMemory.cpp @@ -152,6 +152,23 @@ std::shared_ptr CacheMemory::GetFrame(int64_t frame_number) return std::shared_ptr(); } +// @brief Get an array of all Frames +std::vector> CacheMemory::GetFrames() +{ + // Create a scoped lock, to protect the cache from multiple threads + const std::lock_guard lock(*cacheMutex); + + std::vector> all_frames; + std::vector::iterator itr_ordered; + for(itr_ordered = ordered_frame_numbers.begin(); itr_ordered != ordered_frame_numbers.end(); ++itr_ordered) + { + int64_t frame_number = *itr_ordered; + all_frames.push_back(GetFrame(frame_number)); + } + + return all_frames; +} + // Get the smallest frame number (or NULL shared_ptr if no frame is found) std::shared_ptr CacheMemory::GetSmallestFrame() { diff --git a/src/CacheMemory.h b/src/CacheMemory.h index 7e5913f0..552a6045 100644 --- a/src/CacheMemory.h +++ b/src/CacheMemory.h @@ -76,6 +76,9 @@ namespace openshot { /// @param frame_number The frame number of the cached frame std::shared_ptr GetFrame(int64_t frame_number); + /// @brief Get an array of all Frames + std::vector> GetFrames(); + /// Gets the maximum bytes value int64_t GetBytes();