Skip to content

Commit

Permalink
Replacing WriteFrame() method with custom constructor which can accep…
Browse files Browse the repository at this point in the history
…t a CacheBase* pointer, for instances where a DummyReader needs some specific test Frame objects
  • Loading branch information
jonoomph committed Jun 6, 2020
1 parent d29027a commit 8b12c1f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 59 deletions.
33 changes: 18 additions & 15 deletions include/DummyReader.h
Expand Up @@ -48,18 +48,14 @@ namespace openshot
/**
* @brief This class is used as a simple, dummy reader, which can be very useful when writing
* unit tests. It can return a single blank frame or it can return custom frame objects
* which were added using the WriteFrame() method.
* which were passed into the constructor with a Cache object.
*
* A dummy reader can be created with any framerate or samplerate. This is useful in unit
* tests that need to test different framerates or samplerates.
*
* @code
* // Create a reader (Fraction fps, int width, int height, int sample_rate, int channels, float duration)
* openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 30.0);
* r.Open(); // Open the reader
*
* // Get a frame (which will be blank, since we haven't added any frames yet)
* std::shared_ptr<openshot::Frame> f = r.GetFrame(1);
* // Create cache object to store fake Frame objects
* CacheMemory cache;
*
* // Now let's create some test frames
* for (int64_t frame_number = 1; frame_number <= 30; frame_number++)
Expand All @@ -82,25 +78,33 @@ namespace openshot
* f->AddAudio(true, 0, 0, audio_buffer, sample_count, 1.0); // add channel 1
* f->AddAudio(true, 1, 0, audio_buffer, sample_count, 1.0); // add channel 2
*
* // Write test frame to dummy reader
* r.WriteFrame(f);
* // Add test frame to cache
* cache.Add(f);
* }
*
* // Create a reader (Fraction fps, int width, int height, int sample_rate, int channels, float duration, CacheBase* cache)
* openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 30.0, &cache);
* r.Open(); // Open the reader
*
* // Now let's verify our DummyReader works
* std::shared_ptr<openshot::Frame> f = r.GetFrame(1);
* // r.GetFrame(1)->GetAudioSamples(0)[1] should equal 1.00068033 based on our above calculations
*
* // Close the reader
* // Clean up
* r.Close();
* cache.Clear()
* @endcode
*/
class DummyReader : public ReaderBase
{
private:
CacheMemory dummy_cache;
CacheBase* dummy_cache;
std::shared_ptr<openshot::Frame> image_frame;
bool is_open;

/// Initialize variables used by constructor
void init(Fraction fps, int width, int height, int sample_rate, int channels, float duration);

public:

/// Blank constructor for DummyReader, with default settings.
Expand All @@ -109,6 +113,9 @@ namespace openshot
/// Constructor for DummyReader.
DummyReader(openshot::Fraction fps, int width, int height, int sample_rate, int channels, float duration);

/// Constructor for DummyReader which takes a frame cache object.
DummyReader(openshot::Fraction fps, int width, int height, int sample_rate, int channels, float duration, CacheBase* cache);

virtual ~DummyReader();

/// Close File
Expand Down Expand Up @@ -138,10 +145,6 @@ namespace openshot

/// Open File - which is called by the constructor automatically
void Open() override;

/// @brief Add a frame to the dummy reader. This is useful when constructing unit tests that require custom frames.
/// @param frame The openshot::Frame object to write to this image
void WriteFrame(std::shared_ptr<openshot::Frame> frame);
};

}
Expand Down
64 changes: 35 additions & 29 deletions src/DummyReader.cpp
Expand Up @@ -32,16 +32,8 @@

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) {

// 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;
Expand All @@ -68,10 +60,30 @@ DummyReader::DummyReader(Fraction fps, int width, int height, int sample_rate, i
// 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), 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), is_open(false) {

// Initialize important variables
init(fps, width, height, sample_rate, channels, duration);
}

// Open and Close the reader, to populate its attributes (such as height, width, etc...)
Open();
Close();
// Constructor which also takes a cache object
DummyReader::DummyReader(Fraction fps, int width, int height, int sample_rate, int channels, float duration, CacheBase* cache) : is_open(false) {

// Initialize important variables
init(fps, width, height, sample_rate, channels, duration);

// Set cache object
dummy_cache = (CacheBase*) cache;
}

DummyReader::~DummyReader() {
Expand Down Expand Up @@ -99,48 +111,42 @@ void DummyReader::Close()
{
// Mark as "closed"
is_open = false;

// Clear cache
dummy_cache.Clear();
}
}

// Add Frame objects to DummyReader
void DummyReader::WriteFrame(std::shared_ptr<openshot::Frame> frame)
{
if (frame) {
dummy_cache.Add(frame);
}
}

// 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 the WriteFrame() method.
// 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");

if (dummy_cache.Count() == 0 && image_frame) {
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 GenericScopedLock<CriticalSection> lock(getFrameCriticalSection);

// Always return same frame (regardless of which frame number was requested)
image_frame->number = requested_frame;
return image_frame;

} else if (dummy_cache.Count() > 0) {
} else if (dummy_cache_count > 0) {
// Create a scoped lock, allowing only a single thread to run the following code at one time
const GenericScopedLock<CriticalSection> lock(getFrameCriticalSection);

// Get a frame from the dummy cache
std::shared_ptr<openshot::Frame> f = dummy_cache.GetFrame(requested_frame);
std::shared_ptr<openshot::Frame> f = dummy_cache->GetFrame(requested_frame);
if (f) {
// return frame from cache (if found)
return f;
} else {
// No cached frame found
throw InvalidFile("Requested frame not found. You can only access Frame numbers added with WriteFrame().", "dummy");
throw InvalidFile("Requested frame not found. You can only access Frame numbers that exist in the Cache object.", "dummy");
}
}
else
Expand Down
58 changes: 43 additions & 15 deletions tests/DummyReader_Tests.cpp
Expand Up @@ -37,9 +37,24 @@
using namespace std;
using namespace openshot;

TEST (DummyReader_Basic_Constructor) {
// Create a default fraction (should be 1/1)
openshot::DummyReader r;
r.Open(); // Open the reader

// Check values
CHECK_EQUAL(1280, r.info.width);
CHECK_EQUAL(768, r.info.height);
CHECK_EQUAL(24, r.info.fps.num);
CHECK_EQUAL(1, r.info.fps.den);
CHECK_EQUAL(44100, r.info.sample_rate);
CHECK_EQUAL(2, r.info.channels);
CHECK_EQUAL(30.0, r.info.duration);
}

TEST (DummyReader_Constructor) {
// Create a default fraction (should be 1/1)
openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 30.0);
openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 60.0);
r.Open(); // Open the reader

// Check values
Expand All @@ -49,25 +64,25 @@ TEST (DummyReader_Constructor) {
CHECK_EQUAL(1, r.info.fps.den);
CHECK_EQUAL(44100, r.info.sample_rate);
CHECK_EQUAL(2, r.info.channels);
CHECK_EQUAL(30.0, r.info.duration);
CHECK_EQUAL(60.0, r.info.duration);
}

TEST (DummyReader_Blank_Frame) {
// Create a default fraction (should be 1/1)
openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 30.0);
r.Open(); // Open the reader

// Get a blank frame (because we have not added any frames using WriteFrame() yet)
// Get a blank frame (because we have not passed a Cache object (full of Frame objects) to the constructor
// Check values
CHECK_EQUAL(1, r.GetFrame(1)->number);
CHECK_EQUAL(1, r.GetFrame(1)->GetPixels(700)[700] == 0); // black pixel
CHECK_EQUAL(1, r.GetFrame(1)->GetPixels(701)[701] == 0); // black pixel
}

TEST (DummyReader_Fake_Frame) {
// Create a default fraction (should be 1/1)
openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 30.0);
r.Open(); // Open the reader

// Create cache object to hold test frames
CacheMemory cache;

// Let's create some test frames
for (int64_t frame_number = 1; frame_number <= 30; frame_number++) {
Expand All @@ -87,10 +102,14 @@ TEST (DummyReader_Fake_Frame) {
f->AddAudio(true, 0, 0, audio_buffer, sample_count, 1.0); // add channel 1
f->AddAudio(true, 1, 0, audio_buffer, sample_count, 1.0); // add channel 2

// Write test frame to dummy reader
r.WriteFrame(f);
// Add test frame to dummy reader
cache.Add(f);
}

// Create a default fraction (should be 1/1)
openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 30.0, &cache);
r.Open(); // Open the reader

// Verify our artificial audio sample data is correct
CHECK_EQUAL(1, r.GetFrame(1)->number);
CHECK_EQUAL(1, r.GetFrame(1)->GetAudioSamples(0)[0]);
Expand All @@ -99,23 +118,32 @@ TEST (DummyReader_Fake_Frame) {
CHECK_EQUAL(2, r.GetFrame(2)->GetAudioSamples(0)[0]);
CHECK_CLOSE(2.00068033, r.GetFrame(2)->GetAudioSamples(0)[1], 0.00001);
CHECK_CLOSE(2.00136054, r.GetFrame(2)->GetAudioSamples(0)[2], 0.00001);

// Clean up
cache.Clear();
r.Close();
}

TEST (DummyReader_Invalid_Fake_Frame) {
// Create a default fraction (should be 1/1)
openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 30.0);
r.Open();

// Create fake frames (with specific frame #, samples, and channels)
std::shared_ptr<openshot::Frame> f1(new openshot::Frame(1, 1470, 2));
std::shared_ptr<openshot::Frame> f2(new openshot::Frame(2, 1470, 2));

// Write test frames to dummy reader
r.WriteFrame(f1);
r.WriteFrame(f2);
// Add test frames to cache object
CacheMemory cache;
cache.Add(f1);
cache.Add(f2);

// Create a default fraction (should be 1/1)
openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 30.0, &cache);
r.Open();

// Verify exception
CHECK_EQUAL(1, r.GetFrame(1)->number);
CHECK_EQUAL(2, r.GetFrame(2)->number);
CHECK_THROW(r.GetFrame(3)->number, InvalidFile);

// Clean up
cache.Clear();
r.Close();
}

0 comments on commit 8b12c1f

Please sign in to comment.