Loading...
Searching...
No Matches
DummyReader.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "DummyReader.h"
#include "Exceptions.h"
#include "Frame.h"
using namespace openshot;
// Initialize variables used by constructor
void DummyReader::init(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 = static_cast<size_t>(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;
}
// Blank constructor for DummyReader, with default settings.
DummyReader::DummyReader() : dummy_cache(NULL), last_cached_frame(NULL), image_frame(NULL), is_open(false) {
// Initialize important variables
init(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) :
dummy_cache(NULL), last_cached_frame(NULL), image_frame(NULL), is_open(false) {
// Initialize important variables
init(fps, width, height, sample_rate, channels, duration);
}
// Constructor which also takes a cache object
DummyReader::DummyReader(Fraction fps, int width, int height, int sample_rate, int channels, float duration,
CacheBase* cache) : last_cached_frame(NULL), image_frame(NULL), is_open(false) {
// Initialize important variables
init(fps, width, height, sample_rate, channels, duration);
// Set cache object
dummy_cache = (CacheBase*) cache;
}
DummyReader::~DummyReader() {
}
// 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. It is either a blank frame
// or a custom frame added with passing a Cache object to the constructor.
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");
int dummy_cache_count = 0;
if (dummy_cache) {
dummy_cache_count = dummy_cache->Count();
}
if (dummy_cache_count == 0 && image_frame) {
// Create a scoped lock, allowing only a single thread to run the following code at one time
const std::lock_guard<std::recursive_mutex> lock(getFrameMutex);
// Always return same frame (regardless of which frame number was requested)
image_frame->number = requested_frame;
last_cached_frame = image_frame;
return image_frame;
} else if (dummy_cache_count > 0) {
// Create a scoped lock, allowing only a single thread to run the following code at one time
const std::lock_guard<std::recursive_mutex> lock(getFrameMutex);
// Get a frame from the dummy cache
std::shared_ptr<openshot::Frame> f = dummy_cache->GetFrame(requested_frame);
if (f) {
// return frame from cache (if found)
last_cached_frame = f;
return f;
} else if (last_cached_frame) {
// If available, return last cached frame
return last_cached_frame;
} else {
// No cached frame found
throw InvalidFile("Requested frame not found. You can only access Frame numbers that exist in the Cache object.", "dummy");
}
}
else
// no frame loaded
throw InvalidFile("No frame could be created from this type of file.", "dummy");
}
// Generate JSON string of this object
std::string DummyReader::Json() const {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::Value for this object
Json::Value DummyReader::JsonValue() const {
// 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(const std::string value) {
// Parse JSON string into JSON objects
try
{
const Json::Value root = openshot::stringToJson(value);
// Set all values that match
SetJsonValue(root);
}
catch (const std::exception& e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
}
}
// Load Json::Value into this object
void DummyReader::SetJsonValue(const Json::Value root) {
// Set parent data
ReaderBase::SetJsonValue(root);
}
DummyReader.h
Header file for DummyReader class.
Exceptions.h
Header file for all Exception classes.
Frame.h
Header file for Frame class.
openshot::CacheBase
All cache managers in libopenshot are based on this CacheBase class.
Definition CacheBase.h:35
openshot::CacheBase::GetFrame
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number)=0
Get a frame from the cache.
openshot::CacheBase::Count
virtual int64_t Count()=0
Count the frames in the queue.
openshot::DummyReader::DummyReader
DummyReader()
Blank constructor for DummyReader, with default settings.
Definition DummyReader.cpp:50
openshot::DummyReader::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Definition DummyReader.cpp:105
openshot::DummyReader::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition DummyReader.cpp:167
openshot::DummyReader::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition DummyReader.cpp:156
openshot::DummyReader::Json
std::string Json() const override
Generate JSON string of this object.
Definition DummyReader.cpp:149
openshot::DummyReader::~DummyReader
virtual ~DummyReader()
Definition DummyReader.cpp:75
openshot::DummyReader::Open
void Open() override
Open File - which is called by the constructor automatically.
Definition DummyReader.cpp:79
openshot::DummyReader::Close
void Close() override
Close File.
Definition DummyReader.cpp:93
openshot::DummyReader::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition DummyReader.cpp:184
openshot::Fraction
This class represents a fraction.
Definition Fraction.h:30
openshot::Fraction::num
int num
Numerator for the fraction.
Definition Fraction.h:32
openshot::Fraction::ToFloat
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition Fraction.cpp:35
openshot::Fraction::Reciprocal
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
Definition Fraction.cpp:78
openshot::Fraction::den
int den
Denominator for the fraction.
Definition Fraction.h:33
openshot::InvalidFile
Exception for files that can not be found or opened.
Definition Exceptions.h:188
openshot::InvalidJSON
Exception for invalid JSON.
Definition Exceptions.h:218
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition ReaderBase.h:88
openshot::ReaderBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition ReaderBase.cpp:162
openshot::ReaderBase::JsonValue
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition ReaderBase.cpp:107
openshot::ReaderBase::getFrameMutex
std::recursive_mutex getFrameMutex
Mutex for multiple threads.
Definition ReaderBase.h:79
openshot::ReaderClosed
Exception when a reader is closed, and a frame is requested.
Definition Exceptions.h:364
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
openshot::ReaderInfo::duration
float duration
Length of time (in seconds)
Definition ReaderBase.h:43
openshot::ReaderInfo::width
int width
The width of the video (in pixesl)
Definition ReaderBase.h:46
openshot::ReaderInfo::channels
int channels
The number of audio channels used in the audio stream.
Definition ReaderBase.h:61
openshot::ReaderInfo::fps
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition ReaderBase.h:48
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::height
int height
The height of the video (in pixels)
Definition ReaderBase.h:45
openshot::ReaderInfo::video_length
int64_t video_length
The number of frames in the video stream.
Definition ReaderBase.h:53
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::vcodec
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition ReaderBase.h:52
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::has_video
bool has_video
Determines if this file has a video stream.
Definition ReaderBase.h:40
openshot::ReaderInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition ReaderBase.h:41
openshot::ReaderInfo::video_timebase
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition ReaderBase.h:55
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::ReaderInfo::file_size
int64_t file_size
Size of file (in bytes)
Definition ReaderBase.h:44