OpenShot Library | libopenshot  0.2.6
CacheBase.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for CacheBase class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #include "CacheBase.h"
32 
33 using namespace std;
34 using namespace openshot;
35 
36 // Default constructor, no max frames
37 CacheBase::CacheBase() : max_bytes(0) {
38  // Init the critical section
39  cacheCriticalSection = new CriticalSection();
40 }
41 
42 // Constructor that sets the max frames to cache
43 CacheBase::CacheBase(int64_t max_bytes) : max_bytes(max_bytes) {
44  // Init the critical section
45  cacheCriticalSection = new CriticalSection();
46 }
47 
48 // Set maximum bytes to a different amount based on a ReaderInfo struct
49 void CacheBase::SetMaxBytesFromInfo(int64_t number_of_frames, int width, int height, int sample_rate, int channels)
50 {
51  // n frames X height X width X 4 colors of chars X audio channels X 4 byte floats
52  int64_t bytes = number_of_frames * (height * width * 4 + (sample_rate * channels * 4));
53  SetMaxBytes(bytes);
54 }
55 
56 // Generate Json::Value for this object
57 Json::Value CacheBase::JsonValue() {
58 
59  // Create root json object
60  Json::Value root;
61  std::stringstream max_bytes_stream;
62  max_bytes_stream << max_bytes;
63  root["max_bytes"] = max_bytes_stream.str();
64 
65  // return JsonValue
66  return root;
67 }
68 
69 // Load Json::Value into this object
70 void CacheBase::SetJsonValue(const Json::Value root) {
71 
72  // Set data from Json (if key is found)
73  if (!root["max_bytes"].isNull())
74  max_bytes = std::stoll(root["max_bytes"].asString());
75 }
juce::CriticalSection * cacheCriticalSection
Section lock for multiple threads.
Definition: CacheBase.h:55
STL namespace.
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:49
Header file for CacheBase class.
CacheBase()
Default constructor, no max bytes.
Definition: CacheBase.cpp:37
virtual Json::Value JsonValue()=0
Generate Json::Value for this object.
Definition: CacheBase.cpp:57
void SetMaxBytes(int64_t number_of_bytes)
Set maximum bytes to a different amount.
Definition: CacheBase.h:100
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: CacheBase.cpp:70
int64_t max_bytes
This is the max number of bytes to cache (0 = no limit)
Definition: CacheBase.h:52