OpenShot Library | libopenshot  0.3.2
CacheBase.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #include <sstream>
14 
15 #include "CacheBase.h"
16 
17 using namespace std;
18 using namespace openshot;
19 
20 // Default constructor, no max frames
21 CacheBase::CacheBase() : CacheBase::CacheBase(0) { }
22 
23 // Constructor that sets the max frames to cache
24 CacheBase::CacheBase(int64_t max_bytes) : max_bytes(max_bytes) {
25  // Init the mutex
26  cacheMutex = new std::recursive_mutex();
27 }
28 
29 // Set maximum bytes to a different amount based on a ReaderInfo struct
30 void CacheBase::SetMaxBytesFromInfo(int64_t number_of_frames, int width, int height, int sample_rate, int channels)
31 {
32  // n frames X height X width X 4 colors of chars X audio channels X 4 byte floats
33  int64_t bytes = number_of_frames * (height * width * 4 + (sample_rate * channels * 4));
34  SetMaxBytes(bytes);
35 }
36 
37 // Calculate ranges of frames
39  // Only calculate when something has changed
41 
42  // Create a scoped lock, to protect the cache from multiple threads
43  const std::lock_guard<std::recursive_mutex> lock(*cacheMutex);
44 
45  // Sort ordered frame #s, and calculate JSON ranges
46  std::sort(ordered_frame_numbers.begin(), ordered_frame_numbers.end());
47 
48  // Clear existing JSON variable
49  Json::Value ranges = Json::Value(Json::arrayValue);
50 
51  // Increment range version
52  range_version++;
53 
54  std::vector<int64_t>::iterator itr_ordered;
55 
56  int64_t starting_frame = 0;
57  int64_t ending_frame = 0;
58  if (ordered_frame_numbers.size() > 0) {
59  starting_frame = *ordered_frame_numbers.begin();
60  ending_frame = *ordered_frame_numbers.begin();
61 
62  // Loop through all known frames (in sequential order)
63  for (itr_ordered = ordered_frame_numbers.begin(); itr_ordered != ordered_frame_numbers.end(); ++itr_ordered) {
64  int64_t frame_number = *itr_ordered;
65  if (frame_number - ending_frame > 1) {
66  // End of range detected
67  Json::Value range;
68 
69  // Add JSON object with start/end attributes
70  // Use strings, since int64_ts are supported in JSON
71  range["start"] = std::to_string(starting_frame);
72  range["end"] = std::to_string(ending_frame);
73  ranges.append(range);
74 
75  // Set new starting range
76  starting_frame = frame_number;
77  }
78 
79  // Set current frame as end of range, and keep looping
80  ending_frame = frame_number;
81  }
82  }
83 
84  // APPEND FINAL VALUE
85  Json::Value range;
86 
87  // Add JSON object with start/end attributes
88  // Use strings, since int64_ts are not supported in JSON
89  range["start"] = std::to_string(starting_frame);
90  range["end"] = std::to_string(ending_frame);
91  ranges.append(range);
92 
93  // Cache range JSON as string
94  json_ranges = ranges.toStyledString();
95 
96  // Reset needs_range_processing
97  needs_range_processing = false;
98  }
99 }
100 
101 // Generate Json::Value for this object
102 Json::Value CacheBase::JsonValue() {
103 
104  // Create root json object
105  Json::Value root;
106  std::stringstream max_bytes_stream;
107  max_bytes_stream << max_bytes;
108  root["max_bytes"] = max_bytes_stream.str();
109 
110  // return JsonValue
111  return root;
112 }
113 
114 // Load Json::Value into this object
115 void CacheBase::SetJsonValue(const Json::Value root) {
116 
117  // Set data from Json (if key is found)
118  if (!root["max_bytes"].isNull())
119  max_bytes = std::stoll(root["max_bytes"].asString());
120 }
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::ordered_frame_numbers
std::vector< int64_t > ordered_frame_numbers
Ordered list of frame numbers used by cache.
Definition: CacheBase.h:42
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::CacheBase::json_ranges
std::string json_ranges
JSON ranges of frame numbers.
Definition: CacheBase.h:41
openshot::CacheBase
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:34
CacheBase.h
Header file for CacheBase class.
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:30
openshot::CacheBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: CacheBase.cpp:115
openshot::CacheBase::JsonValue
virtual Json::Value JsonValue()=0
Generate Json::Value for this object.
Definition: CacheBase.cpp:102
openshot::CacheBase::CalculateRanges
void CalculateRanges()
Calculate ranges of frames.
Definition: CacheBase.cpp:38
openshot::CacheBase::SetMaxBytes
void SetMaxBytes(int64_t number_of_bytes)
Set maximum bytes to a different amount.
Definition: CacheBase.h:101
openshot::CacheBase::CacheBase
CacheBase()
Default constructor, no max bytes.
Definition: CacheBase.cpp:21
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::cacheMutex
std::recursive_mutex * cacheMutex
Mutex for multiple threads.
Definition: CacheBase.h:47