OpenShot Library | libopenshot  0.3.3
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 
17 using namespace openshot;
18 
19 // Initialize variables used by constructor
20 void 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();
33  info.pixel_ratio.num = 1;
34  info.pixel_ratio.den = 1;
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.
50 DummyReader::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.
57 DummyReader::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
65 DummyReader::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 
76 }
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.
105 std::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
149 std::string DummyReader::Json() const {
150 
151  // Return formatted string
152  return JsonValue().toStyledString();
153 }
154 
155 // Generate Json::Value for this object
156 Json::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
167 void 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
184 void DummyReader::SetJsonValue(const Json::Value root) {
185 
186  // Set parent data
188 
189 }
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::ReaderInfo::sample_rate
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:60
openshot::Fraction::ToFloat
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:35
openshot::ReaderBase::JsonValue
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:107
openshot::CacheBase::GetFrame
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number)=0
Get a frame from the cache.
openshot::ReaderBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:162
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:88
openshot::DummyReader::Open
void Open() override
Open File - which is called by the constructor automatically.
Definition: DummyReader.cpp:79
openshot::ReaderInfo::duration
float duration
Length of time (in seconds)
Definition: ReaderBase.h:43
openshot::ReaderInfo::has_video
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:40
openshot::ReaderInfo::width
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:46
openshot::CacheBase
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:34
openshot::DummyReader::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: DummyReader.cpp:167
openshot::ReaderInfo::video_length
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:53
openshot::ReaderInfo::height
int height
The height of the video (in pixels)
Definition: ReaderBase.h:45
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:32
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:33
openshot::Fraction::Reciprocal
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
Definition: Fraction.cpp:78
openshot::ReaderInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:41
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::ReaderInfo::file_size
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:44
openshot::DummyReader::~DummyReader
virtual ~DummyReader()
Definition: DummyReader.cpp:75
openshot::ReaderInfo::video_timebase
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:55
Frame.h
Header file for Frame class.
openshot::DummyReader::Close
void Close() override
Close File.
Definition: DummyReader.cpp:93
openshot::DummyReader::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: DummyReader.cpp:156
openshot::CacheBase::Count
virtual int64_t Count()=0
Count the frames in the queue.
openshot::InvalidFile
Exception for files that can not be found or opened.
Definition: Exceptions.h:187
openshot::ReaderInfo::vcodec
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:52
openshot::DummyReader::DummyReader
DummyReader()
Blank constructor for DummyReader, with default settings.
Definition: DummyReader.cpp:50
openshot::ReaderClosed
Exception when a reader is closed, and a frame is requested.
Definition: Exceptions.h:363
openshot::ReaderInfo::fps
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:48
openshot::DummyReader::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: DummyReader.cpp:184
openshot::ReaderInfo::pixel_ratio
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
openshot::ReaderInfo::acodec
std::string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:58
openshot::ReaderInfo::display_ratio
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
openshot::ReaderInfo::channels
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:61
openshot::DummyReader::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Definition: DummyReader.cpp:105
DummyReader.h
Header file for DummyReader class.
Exceptions.h
Header file for all Exception classes.
openshot::ReaderBase::getFrameMutex
std::recursive_mutex getFrameMutex
Mutex for multiple threads.
Definition: ReaderBase.h:79
openshot::DummyReader::Json
std::string Json() const override
Generate JSON string of this object.
Definition: DummyReader.cpp:149