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