DummyReader.cpp
Go to the documentation of this file.
/**
* @file
* @brief Source file for DummyReader class
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @section LICENSE
*
* Copyright (c) 2008-2014 OpenShot Studios, LLC
* <http://www.openshotstudios.com/>. This file is part of
* OpenShot Library (libopenshot), an open-source project dedicated to
* delivering high quality video editing and animation solutions to the
* world. For more information visit <http://www.openshot.org/>.
*
* OpenShot Library (libopenshot) is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenShot Library (libopenshot) is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../include/DummyReader.h"
using namespace openshot;
// Blank constructor for DummyReader, with default settings.
DummyReader::DummyReader() {
// Call actual constructor with default values
DummyReader(Fraction(24,1), 1280, 768, 44100, 2, 30.0);
}
// Constructor for DummyReader. Pass a framerate and samplerate.
DummyReader::DummyReader(Fraction fps, int width, int height, int sample_rate, int channels, float duration) {
// Set key info settings
info.has_audio = false;
info.has_video = true;
info.file_size = width * height * sizeof(int);
info.vcodec = "raw";
info.fps = fps;
info.width = width;
info.height = height;
info.sample_rate = sample_rate;
info.channels = channels;
info.duration = duration;
info.video_length = duration * fps.ToFloat();
info.pixel_ratio.num = 1;
info.pixel_ratio.den = 1;
info.video_timebase = fps.Reciprocal();
info.acodec = "raw";
// Calculate the DAR (display aspect ratio)
Fraction size(info.width * info.pixel_ratio.num, info.height * info.pixel_ratio.den);
// Reduce size fraction
size.Reduce();
// Set the ratio based on the reduced fraction
info.display_ratio.num = size.num;
info.display_ratio.den = size.den;
// Open and Close the reader, to populate it's attributes (such as height, width, etc...)
Open();
Close();
}
// Open image file
void DummyReader::Open()
{
// Open reader if not already open
if (!is_open)
{
// Create or get frame object
image_frame = std::make_shared<Frame>(1, info.width, info.height, "#000000", info.sample_rate, info.channels);
// Mark as "open"
is_open = true;
}
}
// Close image file
void DummyReader::Close()
{
// Close all objects, if reader is 'open'
if (is_open)
{
// Mark as "closed"
is_open = false;
}
}
// Get an openshot::Frame object for a specific frame number of this reader.
std::shared_ptr<Frame> DummyReader::GetFrame(int64_t requested_frame)
{
// Check for open reader (or throw exception)
if (!is_open)
throw ReaderClosed("The ImageReader is closed. Call Open() before calling this method.", "dummy");
if (image_frame)
{
// Create a scoped lock, allowing only a single thread to run the following code at one time
const GenericScopedLock<CriticalSection> lock(getFrameCriticalSection);
// Always return same frame (regardless of which frame number was requested)
image_frame->number = requested_frame;
return image_frame;
}
else
// no frame loaded
throw InvalidFile("No frame could be created from this type of file.", "dummy");
}
// Generate JSON string of this object
string DummyReader::Json() {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::JsonValue for this object
Json::Value DummyReader::JsonValue() {
// Create root json object
Json::Value root = ReaderBase::JsonValue(); // get parent properties
root["type"] = "DummyReader";
// return JsonValue
return root;
}
// Load JSON string into this object
void DummyReader::SetJson(string value) {
// Parse JSON string into JSON objects
Json::Value root;
Json::Reader reader;
bool success = reader.parse( value, root );
if (!success)
// Raise exception
throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
try
{
// Set all values that match
SetJsonValue(root);
}
catch (exception e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
}
}
// Load Json::JsonValue into this object
void DummyReader::SetJsonValue(Json::Value root) {
// Set parent data
ReaderBase::SetJsonValue(root);
}
openshot::DummyReader::Json
string Json()
Get and Set JSON methods.
Definition: DummyReader.cpp:121
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:44
openshot::ReaderBase::getFrameCriticalSection
CriticalSection getFrameCriticalSection
Section lock for multiple threads.
Definition: ReaderBase.h:99
openshot::DummyReader::JsonValue
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: DummyReader.cpp:128
openshot::ReaderInfo::width
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:67
openshot::Fraction::ToFloat
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:41
openshot::ReaderInfo::duration
float duration
Length of time (in seconds)
Definition: ReaderBase.h:64
openshot::ReaderInfo::acodec
string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:79
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:71
OpenShot Wipe Tests.e
e
Definition: OpenShot Wipe Tests.py:28
openshot::Fraction::Reciprocal
Fraction Reciprocal()
Return the reciprocal as a Fraction.
Definition: Fraction.cpp:81
openshot::ReaderClosed
Exception when a reader is closed, and a frame is requested.
Definition: Exceptions.h:234
openshot::ReaderInfo::has_video
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:61
openshot::DummyReader::DummyReader
DummyReader()
Blank constructor for DummyReader, with default settings.
Definition: DummyReader.cpp:33
openshot::ReaderInfo::display_ratio
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
openshot::ReaderInfo::file_size
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:65
openshot::ReaderInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:62
openshot::ReaderInfo::video_length
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:74
openshot::ReaderInfo::height
int height
The height of the video (in pixels)
Definition: ReaderBase.h:66
openshot::DummyReader::Open
void Open()
Open File - which is called by the constructor automatically.
Definition: DummyReader.cpp:75
openshot::InvalidFile
Exception for files that can not be found or opened.
Definition: Exceptions.h:132
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:42
openshot::DummyReader::GetFrame
std::shared_ptr< Frame > GetFrame(int64_t requested_frame)
Definition: DummyReader.cpp:100
openshot::ReaderBase::JsonValue
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: ReaderBase.cpp:106
openshot::ReaderBase::SetJsonValue
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: ReaderBase.cpp:155
openshot::ReaderBase::info
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
openshot::DummyReader::Close
void Close()
Close File.
Definition: DummyReader.cpp:89
openshot::ReaderInfo::fps
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:69
openshot::ReaderInfo::video_timebase
Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:76
openshot::ReaderInfo::pixel_ratio
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: ReaderBase.h:71
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:45
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:152
openshot::ReaderInfo::vcodec
string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:73
openshot::DummyReader::SetJson
void SetJson(string value)
Load JSON string into this object.
Definition: DummyReader.cpp:139
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:45
openshot::ReaderInfo::channels
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:82
openshot::DummyReader::SetJsonValue
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: DummyReader.cpp:162
openshot::ReaderInfo::sample_rate
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:81