OpenShot Library | libopenshot 0.3.3
Loading...
Searching...
No Matches
CacheBase.h
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#ifndef OPENSHOT_CACHE_BASE_H
14#define OPENSHOT_CACHE_BASE_H
15
16#include <map>
17#include <vector>
18#include <memory>
19#include <mutex>
20#include <algorithm>
21
22#include "Json.h"
23
24namespace openshot {
25 class Frame;
26
35 {
36 protected:
37 std::string cache_type;
38 int64_t max_bytes;
39
41 std::string json_ranges;
42 std::vector<int64_t> ordered_frame_numbers;
43 std::map<int64_t, int64_t> frame_ranges;
44 int64_t range_version;
45
47 std::recursive_mutex *cacheMutex;
48
50 void CalculateRanges();
51
52 public:
54 CacheBase();
55
58 CacheBase(int64_t max_bytes);
59
62 virtual void Add(std::shared_ptr<openshot::Frame> frame) = 0;
63
65 virtual void Clear() = 0;
66
69 virtual bool Contains(int64_t frame_number) = 0;
70
72 virtual int64_t Count() = 0;
73
76 virtual std::shared_ptr<openshot::Frame> GetFrame(int64_t frame_number) = 0;
77
79 virtual std::vector<std::shared_ptr<openshot::Frame>> GetFrames() = 0;
80
82 virtual int64_t GetBytes() = 0;
83
85 virtual std::shared_ptr<openshot::Frame> GetSmallestFrame() = 0;
86
89 virtual void Remove(int64_t frame_number) = 0;
90
94 virtual void Remove(int64_t start_frame_number, int64_t end_frame_number) = 0;
95
97 int64_t GetMaxBytes() { return max_bytes; };
98
101 void SetMaxBytes(int64_t number_of_bytes) { max_bytes = number_of_bytes; };
102
109 void SetMaxBytesFromInfo(int64_t number_of_frames, int width, int height, int sample_rate, int channels);
110
111 // Get and Set JSON methods
112 virtual std::string Json() = 0;
113 virtual void SetJson(const std::string value) = 0;
114 virtual Json::Value JsonValue() = 0;
115 virtual void SetJsonValue(const Json::Value root) = 0;
116 virtual ~CacheBase() = default;
117
118 };
119
120}
121
122#endif
Header file for JSON class.
All cache managers in libopenshot are based on this CacheBase class.
Definition CacheBase.h:35
int64_t range_version
The version of the JSON range data (incremented with each change)
Definition CacheBase.h:44
CacheBase()
Default constructor, no max bytes.
Definition CacheBase.cpp:21
virtual void SetJson(const std::string value)=0
Load JSON string into this object.
virtual int64_t GetBytes()=0
Gets the maximum bytes value.
virtual std::shared_ptr< openshot::Frame > GetSmallestFrame()=0
Get the smallest frame number.
virtual void Clear()=0
Clear the cache of all frames.
virtual bool Contains(int64_t frame_number)=0
Check if frame is already contained in cache.
virtual std::vector< std::shared_ptr< openshot::Frame > > GetFrames()=0
Get an vector of all Frames.
virtual Json::Value JsonValue()=0
Generate Json::Value for this object.
std::map< int64_t, int64_t > frame_ranges
This map holds the ranges of frames, useful for quickly displaying the contents of the cache.
Definition CacheBase.h:43
std::string cache_type
This is a friendly type name of the derived cache instance.
Definition CacheBase.h:37
virtual void Remove(int64_t start_frame_number, int64_t end_frame_number)=0
Remove a range of frames.
void CalculateRanges()
Calculate ranges of frames.
Definition CacheBase.cpp:38
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
virtual void Remove(int64_t frame_number)=0
Remove a specific frame.
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number)=0
Get a frame from the cache.
bool needs_range_processing
Something has changed, and the range data needs to be re-calculated.
Definition CacheBase.h:40
virtual ~CacheBase()=default
virtual int64_t Count()=0
Count the frames in the queue.
virtual void Add(std::shared_ptr< openshot::Frame > frame)=0
Add a Frame to the cache.
virtual std::string Json()=0
Generate JSON string of this object.
int64_t max_bytes
This is the max number of bytes to cache (0 = no limit)
Definition CacheBase.h:38
int64_t GetMaxBytes()
Gets the maximum bytes value.
Definition CacheBase.h:97
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
std::recursive_mutex * cacheMutex
Mutex for multiple threads.
Definition CacheBase.h:47
std::string json_ranges
JSON ranges of frame numbers.
Definition CacheBase.h:41
std::vector< int64_t > ordered_frame_numbers
Ordered list of frame numbers used by cache.
Definition CacheBase.h:42
void SetMaxBytes(int64_t number_of_bytes)
Set maximum bytes to a different amount.
Definition CacheBase.h:101
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29