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