OpenShot Library | libopenshot  0.1.9
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  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #include "../include/DummyReader.h"
29 
30 using namespace openshot;
31 
32 // Blank constructor for DummyReader, with default settings.
34 
35  // Call actual constructor with default values
36  DummyReader(Fraction(24,1), 1280, 768, 44100, 2, 30.0);
37 }
38 
39 // Constructor for DummyReader. Pass a framerate and samplerate.
40 DummyReader::DummyReader(Fraction fps, int width, int height, int sample_rate, int channels, float duration) {
41 
42  // Set key info settings
43  info.has_audio = false;
44  info.has_video = true;
45  info.file_size = width * height * sizeof(int);
46  info.vcodec = "raw";
47  info.fps = fps;
48  info.width = width;
49  info.height = height;
50  info.sample_rate = sample_rate;
51  info.channels = channels;
52  info.duration = duration;
53  info.video_length = duration * fps.ToFloat();
54  info.pixel_ratio.num = 1;
55  info.pixel_ratio.den = 1;
57  info.acodec = "raw";
58 
59  // Calculate the DAR (display aspect ratio)
61 
62  // Reduce size fraction
63  size.Reduce();
64 
65  // Set the ratio based on the reduced fraction
66  info.display_ratio.num = size.num;
67  info.display_ratio.den = size.den;
68 
69  // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
70  Open();
71  Close();
72 }
73 
74 // Open image file
76 {
77  // Open reader if not already open
78  if (!is_open)
79  {
80  // Create or get frame object
81  image_frame = std::make_shared<Frame>(1, info.width, info.height, "#000000", info.sample_rate, info.channels);
82 
83  // Mark as "open"
84  is_open = true;
85  }
86 }
87 
88 // Close image file
90 {
91  // Close all objects, if reader is 'open'
92  if (is_open)
93  {
94  // Mark as "closed"
95  is_open = false;
96  }
97 }
98 
99 // Get an openshot::Frame object for a specific frame number of this reader.
100 std::shared_ptr<Frame> DummyReader::GetFrame(int64_t requested_frame)
101 {
102  // Check for open reader (or throw exception)
103  if (!is_open)
104  throw ReaderClosed("The ImageReader is closed. Call Open() before calling this method.", "dummy");
105 
106  if (image_frame)
107  {
108  // Create a scoped lock, allowing only a single thread to run the following code at one time
109  const GenericScopedLock<CriticalSection> lock(getFrameCriticalSection);
110 
111  // Always return same frame (regardless of which frame number was requested)
112  image_frame->number = requested_frame;
113  return image_frame;
114  }
115  else
116  // no frame loaded
117  throw InvalidFile("No frame could be created from this type of file.", "dummy");
118 }
119 
120 // Generate JSON string of this object
122 
123  // Return formatted string
124  return JsonValue().toStyledString();
125 }
126 
127 // Generate Json::JsonValue for this object
128 Json::Value DummyReader::JsonValue() {
129 
130  // Create root json object
131  Json::Value root = ReaderBase::JsonValue(); // get parent properties
132  root["type"] = "DummyReader";
133 
134  // return JsonValue
135  return root;
136 }
137 
138 // Load JSON string into this object
139 void DummyReader::SetJson(string value) {
140 
141  // Parse JSON string into JSON objects
142  Json::Value root;
143  Json::Reader reader;
144  bool success = reader.parse( value, root );
145  if (!success)
146  // Raise exception
147  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
148 
149  try
150  {
151  // Set all values that match
152  SetJsonValue(root);
153  }
154  catch (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::JsonValue into this object
162 void DummyReader::SetJsonValue(Json::Value root) {
163 
164  // Set parent data
166 
167 }
string Json()
Get and Set JSON methods.
int num
Numerator for the fraction.
Definition: Fraction.h:44
CriticalSection getFrameCriticalSection
Section lock for multiple threads.
Definition: ReaderBase.h:99
Json::Value JsonValue()
Generate Json::JsonValue for this object.
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:67
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:41
float duration
Length of time (in seconds)
Definition: ReaderBase.h:64
string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:79
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:71
Fraction Reciprocal()
Return the reciprocal as a Fraction.
Definition: Fraction.cpp:81
Exception when a reader is closed, and a frame is requested.
Definition: Exceptions.h:234
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:61
DummyReader()
Blank constructor for DummyReader, with default settings.
Definition: DummyReader.cpp:33
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:72
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:65
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:62
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:74
int height
The height of the video (in pixels)
Definition: ReaderBase.h:66
void Open()
Open File - which is called by the constructor automatically.
Definition: DummyReader.cpp:75
Exception for files that can not be found or opened.
Definition: Exceptions.h:132
This class represents a fraction.
Definition: Fraction.h:42
std::shared_ptr< Frame > GetFrame(int64_t requested_frame)
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: ReaderBase.cpp:106
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: ReaderBase.cpp:155
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
void Close()
Close File.
Definition: DummyReader.cpp:89
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:69
Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:76
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: ReaderBase.h:71
This namespace is the default namespace for all code in the openshot library.
Exception for invalid JSON.
Definition: Exceptions.h:152
string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:73
void SetJson(string value)
Load JSON string into this object.
int den
Denominator for the fraction.
Definition: Fraction.h:45
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:82
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:81