OpenShot Library | libopenshot 0.2.7
DummyReader.cpp
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Source file for DummyReader 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 "DummyReader.h"
32#include "Exceptions.h"
33
34using namespace openshot;
35
36// Initialize variables used by constructor
37void DummyReader::init(Fraction fps, int width, int height, int sample_rate, int channels, float duration) {
38 // Set key info settings
39 info.has_audio = false;
40 info.has_video = true;
41 info.file_size = width * height * sizeof(int);
42 info.vcodec = "raw";
43 info.fps = fps;
44 info.width = width;
45 info.height = height;
46 info.sample_rate = sample_rate;
47 info.channels = channels;
48 info.duration = duration;
49 info.video_length = duration * fps.ToFloat();
53 info.acodec = "raw";
54
55 // Calculate the DAR (display aspect ratio)
57
58 // Reduce size fraction
59 size.Reduce();
60
61 // Set the ratio based on the reduced fraction
62 info.display_ratio.num = size.num;
63 info.display_ratio.den = size.den;
64}
65
66// Blank constructor for DummyReader, with default settings.
67DummyReader::DummyReader() : dummy_cache(NULL), is_open(false) {
68
69 // Initialize important variables
70 init(Fraction(24,1), 1280, 768, 44100, 2, 30.0);
71}
72
73// Constructor for DummyReader. Pass a framerate and samplerate.
74DummyReader::DummyReader(Fraction fps, int width, int height, int sample_rate, int channels, float duration) : dummy_cache(NULL), is_open(false) {
75
76 // Initialize important variables
77 init(fps, width, height, sample_rate, channels, duration);
78}
79
80// Constructor which also takes a cache object
81DummyReader::DummyReader(Fraction fps, int width, int height, int sample_rate, int channels, float duration, CacheBase* cache) : is_open(false) {
82
83 // Initialize important variables
84 init(fps, width, height, sample_rate, channels, duration);
85
86 // Set cache object
87 dummy_cache = (CacheBase*) cache;
88}
89
91}
92
93// Open image file
95{
96 // Open reader if not already open
97 if (!is_open)
98 {
99 // Create or get frame object
100 image_frame = std::make_shared<Frame>(1, info.width, info.height, "#000000", info.sample_rate, info.channels);
101
102 // Mark as "open"
103 is_open = true;
104 }
105}
106
107// Close image file
109{
110 // Close all objects, if reader is 'open'
111 if (is_open)
112 {
113 // Mark as "closed"
114 is_open = false;
115 }
116}
117
118// Get an openshot::Frame object for a specific frame number of this reader. It is either a blank frame
119// or a custom frame added with passing a Cache object to the constructor.
120std::shared_ptr<Frame> DummyReader::GetFrame(int64_t requested_frame)
121{
122 // Check for open reader (or throw exception)
123 if (!is_open)
124 throw ReaderClosed("The ImageReader is closed. Call Open() before calling this method.", "dummy");
125
126 int dummy_cache_count = 0;
127 if (dummy_cache) {
128 dummy_cache_count = dummy_cache->Count();
129 }
130
131 if (dummy_cache_count == 0 && image_frame) {
132 // Create a scoped lock, allowing only a single thread to run the following code at one time
133 const GenericScopedLock<CriticalSection> lock(getFrameCriticalSection);
134
135 // Always return same frame (regardless of which frame number was requested)
136 image_frame->number = requested_frame;
137 return image_frame;
138
139 } else if (dummy_cache_count > 0) {
140 // Create a scoped lock, allowing only a single thread to run the following code at one time
141 const GenericScopedLock<CriticalSection> lock(getFrameCriticalSection);
142
143 // Get a frame from the dummy cache
144 std::shared_ptr<openshot::Frame> f = dummy_cache->GetFrame(requested_frame);
145 if (f) {
146 // return frame from cache (if found)
147 return f;
148 } else {
149 // No cached frame found
150 throw InvalidFile("Requested frame not found. You can only access Frame numbers that exist in the Cache object.", "dummy");
151 }
152 }
153 else
154 // no frame loaded
155 throw InvalidFile("No frame could be created from this type of file.", "dummy");
156}
157
158// Generate JSON string of this object
159std::string DummyReader::Json() const {
160
161 // Return formatted string
162 return JsonValue().toStyledString();
163}
164
165// Generate Json::Value for this object
166Json::Value DummyReader::JsonValue() const {
167
168 // Create root json object
169 Json::Value root = ReaderBase::JsonValue(); // get parent properties
170 root["type"] = "DummyReader";
171
172 // return JsonValue
173 return root;
174}
175
176// Load JSON string into this object
177void DummyReader::SetJson(const std::string value) {
178
179 // Parse JSON string into JSON objects
180 try
181 {
182 const Json::Value root = openshot::stringToJson(value);
183 // Set all values that match
184 SetJsonValue(root);
185 }
186 catch (const std::exception& e)
187 {
188 // Error parsing JSON (or missing keys)
189 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
190 }
191}
192
193// Load Json::Value into this object
194void DummyReader::SetJsonValue(const Json::Value root) {
195
196 // Set parent data
198
199}
Header file for DummyReader class.
Header file for all Exception classes.
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:49
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number)=0
Get a frame from the cache.
virtual int64_t Count()=0
Count the frames in the queue.
DummyReader()
Blank constructor for DummyReader, with default settings.
Definition: DummyReader.cpp:67
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
void SetJson(const std::string value) override
Load JSON string into this object.
Json::Value JsonValue() const override
Generate Json::Value for this object.
std::string Json() const override
Generate JSON string of this object.
void Open() override
Open File - which is called by the constructor automatically.
Definition: DummyReader.cpp:94
void Close() override
Close File.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
This class represents a fraction.
Definition: Fraction.h:48
int num
Numerator for the fraction.
Definition: Fraction.h:50
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:54
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
Definition: Fraction.cpp:94
int den
Denominator for the fraction.
Definition: Fraction.h:51
Exception for files that can not be found or opened.
Definition: Exceptions.h:174
Exception for invalid JSON.
Definition: Exceptions.h:206
juce::CriticalSection getFrameCriticalSection
Section lock for multiple threads.
Definition: ReaderBase.h:101
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:171
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:116
Exception when a reader is closed, and a frame is requested.
Definition: Exceptions.h:338
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
float duration
Length of time (in seconds)
Definition: ReaderBase.h:65
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:68
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:83
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:70
openshot::Fraction display_ratio
The ratio of width to height of the video stream (i.e. 640x480 has a ratio of 4/3)
Definition: ReaderBase.h:73
int height
The height of the video (in pixels)
Definition: ReaderBase.h:67
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:75
std::string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:80
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:74
openshot::Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square)
Definition: ReaderBase.h:72
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:62
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:63
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:77
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:82
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:66