Skip to content

Commit c29174f

Browse files
authored
Merge pull request #527 from OpenShot/dummy_reader_improvements
Adding some new functionality and documentation to DummyReader
2 parents 2834e77 + 8b12c1f commit c29174f

File tree

5 files changed

+254
-17
lines changed

5 files changed

+254
-17
lines changed

include/DummyReader.h

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,65 @@
4646
namespace openshot
4747
{
4848
/**
49-
* @brief This class is used as a simple, dummy reader, which always returns a blank frame.
49+
* @brief This class is used as a simple, dummy reader, which can be very useful when writing
50+
* unit tests. It can return a single blank frame or it can return custom frame objects
51+
* which were passed into the constructor with a Cache object.
5052
*
5153
* A dummy reader can be created with any framerate or samplerate. This is useful in unit
5254
* tests that need to test different framerates or samplerates.
55+
*
56+
* @code
57+
* // Create cache object to store fake Frame objects
58+
* CacheMemory cache;
59+
*
60+
* // Now let's create some test frames
61+
* for (int64_t frame_number = 1; frame_number <= 30; frame_number++)
62+
* {
63+
* // Create blank frame (with specific frame #, samples, and channels)
64+
* // Sample count should be 44100 / 30 fps = 1470 samples per frame
65+
* int sample_count = 1470;
66+
* std::shared_ptr<openshot::Frame> f(new openshot::Frame(frame_number, sample_count, 2));
67+
*
68+
* // Create test samples with incrementing value
69+
* float *audio_buffer = new float[sample_count];
70+
* for (int64_t sample_number = 0; sample_number < sample_count; sample_number++)
71+
* {
72+
* // Generate an incrementing audio sample value (just as an example)
73+
* audio_buffer[sample_number] = float(frame_number) + (float(sample_number) / float(sample_count));
74+
* }
75+
*
76+
* // Add custom audio samples to Frame (bool replaceSamples, int destChannel, int destStartSample, const float* source,
77+
* // int numSamples, float gainToApplyToSource = 1.0f)
78+
* f->AddAudio(true, 0, 0, audio_buffer, sample_count, 1.0); // add channel 1
79+
* f->AddAudio(true, 1, 0, audio_buffer, sample_count, 1.0); // add channel 2
80+
*
81+
* // Add test frame to cache
82+
* cache.Add(f);
83+
* }
84+
*
85+
* // Create a reader (Fraction fps, int width, int height, int sample_rate, int channels, float duration, CacheBase* cache)
86+
* openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 30.0, &cache);
87+
* r.Open(); // Open the reader
88+
*
89+
* // Now let's verify our DummyReader works
90+
* std::shared_ptr<openshot::Frame> f = r.GetFrame(1);
91+
* // r.GetFrame(1)->GetAudioSamples(0)[1] should equal 1.00068033 based on our above calculations
92+
*
93+
* // Clean up
94+
* r.Close();
95+
* cache.Clear()
96+
* @endcode
5397
*/
5498
class DummyReader : public ReaderBase
5599
{
56100
private:
101+
CacheBase* dummy_cache;
57102
std::shared_ptr<openshot::Frame> image_frame;
58103
bool is_open;
59104

105+
/// Initialize variables used by constructor
106+
void init(Fraction fps, int width, int height, int sample_rate, int channels, float duration);
107+
60108
public:
61109

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

116+
/// Constructor for DummyReader which takes a frame cache object.
117+
DummyReader(openshot::Fraction fps, int width, int height, int sample_rate, int channels, float duration, CacheBase* cache);
118+
68119
virtual ~DummyReader();
69120

70121
/// Close File

src/DummyReader.cpp

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,8 @@
3232

3333
using namespace openshot;
3434

35-
// Blank constructor for DummyReader, with default settings.
36-
DummyReader::DummyReader() {
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-
35+
// Initialize variables used by constructor
36+
void DummyReader::init(Fraction fps, int width, int height, int sample_rate, int channels, float duration) {
4537
// Set key info settings
4638
info.has_audio = false;
4739
info.has_video = true;
@@ -68,10 +60,30 @@ DummyReader::DummyReader(Fraction fps, int width, int height, int sample_rate, i
6860
// Set the ratio based on the reduced fraction
6961
info.display_ratio.num = size.num;
7062
info.display_ratio.den = size.den;
63+
}
64+
65+
// Blank constructor for DummyReader, with default settings.
66+
DummyReader::DummyReader() : dummy_cache(NULL), is_open(false) {
67+
68+
// Initialize important variables
69+
init(Fraction(24,1), 1280, 768, 44100, 2, 30.0);
70+
}
71+
72+
// Constructor for DummyReader. Pass a framerate and samplerate.
73+
DummyReader::DummyReader(Fraction fps, int width, int height, int sample_rate, int channels, float duration) : dummy_cache(NULL), is_open(false) {
74+
75+
// Initialize important variables
76+
init(fps, width, height, sample_rate, channels, duration);
77+
}
7178

72-
// Open and Close the reader, to populate its attributes (such as height, width, etc...)
73-
Open();
74-
Close();
79+
// Constructor which also takes a cache object
80+
DummyReader::DummyReader(Fraction fps, int width, int height, int sample_rate, int channels, float duration, CacheBase* cache) : is_open(false) {
81+
82+
// Initialize important variables
83+
init(fps, width, height, sample_rate, channels, duration);
84+
85+
// Set cache object
86+
dummy_cache = (CacheBase*) cache;
7587
}
7688

7789
DummyReader::~DummyReader() {
@@ -102,21 +114,40 @@ void DummyReader::Close()
102114
}
103115
}
104116

105-
// Get an openshot::Frame object for a specific frame number of this reader.
117+
// Get an openshot::Frame object for a specific frame number of this reader. It is either a blank frame
118+
// or a custom frame added with passing a Cache object to the constructor.
106119
std::shared_ptr<Frame> DummyReader::GetFrame(int64_t requested_frame)
107120
{
108121
// Check for open reader (or throw exception)
109122
if (!is_open)
110123
throw ReaderClosed("The ImageReader is closed. Call Open() before calling this method.", "dummy");
111124

112-
if (image_frame)
113-
{
125+
int dummy_cache_count = 0;
126+
if (dummy_cache) {
127+
dummy_cache_count = dummy_cache->Count();
128+
}
129+
130+
if (dummy_cache_count == 0 && image_frame) {
114131
// Create a scoped lock, allowing only a single thread to run the following code at one time
115132
const GenericScopedLock<CriticalSection> lock(getFrameCriticalSection);
116133

117134
// Always return same frame (regardless of which frame number was requested)
118135
image_frame->number = requested_frame;
119136
return image_frame;
137+
138+
} else if (dummy_cache_count > 0) {
139+
// Create a scoped lock, allowing only a single thread to run the following code at one time
140+
const GenericScopedLock<CriticalSection> lock(getFrameCriticalSection);
141+
142+
// Get a frame from the dummy cache
143+
std::shared_ptr<openshot::Frame> f = dummy_cache->GetFrame(requested_frame);
144+
if (f) {
145+
// return frame from cache (if found)
146+
return f;
147+
} else {
148+
// No cached frame found
149+
throw InvalidFile("Requested frame not found. You can only access Frame numbers that exist in the Cache object.", "dummy");
150+
}
120151
}
121152
else
122153
// no frame loaded

src/Frame.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,11 @@ const unsigned char* Frame::GetPixels()
480480
// Get pixel data (for only a single scan-line)
481481
const unsigned char* Frame::GetPixels(int row)
482482
{
483+
// Check for blank image
484+
if (!image)
485+
// Fill with black
486+
AddColor(width, height, color);
487+
483488
// Return array of pixel packets
484489
return image->constScanLine(row);
485490
}

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ set(OPENSHOT_TEST_FILES
109109
Clip_Tests.cpp
110110
Color_Tests.cpp
111111
Coordinate_Tests.cpp
112+
DummyReader_Tests.cpp
112113
ReaderBase_Tests.cpp
113114
ImageWriter_Tests.cpp
114115
FFmpegReader_Tests.cpp

tests/DummyReader_Tests.cpp

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/**
2+
* @file
3+
* @brief Unit tests for openshot::DummyReader
4+
* @author Jonathan Thomas <[email protected]>
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 "UnitTest++.h"
32+
// Prevent name clashes with juce::UnitTest
33+
#define DONT_SET_USING_JUCE_NAMESPACE 1
34+
35+
#include "../include/OpenShot.h"
36+
37+
using namespace std;
38+
using namespace openshot;
39+
40+
TEST (DummyReader_Basic_Constructor) {
41+
// Create a default fraction (should be 1/1)
42+
openshot::DummyReader r;
43+
r.Open(); // Open the reader
44+
45+
// Check values
46+
CHECK_EQUAL(1280, r.info.width);
47+
CHECK_EQUAL(768, r.info.height);
48+
CHECK_EQUAL(24, r.info.fps.num);
49+
CHECK_EQUAL(1, r.info.fps.den);
50+
CHECK_EQUAL(44100, r.info.sample_rate);
51+
CHECK_EQUAL(2, r.info.channels);
52+
CHECK_EQUAL(30.0, r.info.duration);
53+
}
54+
55+
TEST (DummyReader_Constructor) {
56+
// Create a default fraction (should be 1/1)
57+
openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 60.0);
58+
r.Open(); // Open the reader
59+
60+
// Check values
61+
CHECK_EQUAL(1920, r.info.width);
62+
CHECK_EQUAL(1080, r.info.height);
63+
CHECK_EQUAL(30, r.info.fps.num);
64+
CHECK_EQUAL(1, r.info.fps.den);
65+
CHECK_EQUAL(44100, r.info.sample_rate);
66+
CHECK_EQUAL(2, r.info.channels);
67+
CHECK_EQUAL(60.0, r.info.duration);
68+
}
69+
70+
TEST (DummyReader_Blank_Frame) {
71+
// Create a default fraction (should be 1/1)
72+
openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 30.0);
73+
r.Open(); // Open the reader
74+
75+
// Get a blank frame (because we have not passed a Cache object (full of Frame objects) to the constructor
76+
// Check values
77+
CHECK_EQUAL(1, r.GetFrame(1)->number);
78+
CHECK_EQUAL(1, r.GetFrame(1)->GetPixels(700)[700] == 0); // black pixel
79+
CHECK_EQUAL(1, r.GetFrame(1)->GetPixels(701)[701] == 0); // black pixel
80+
}
81+
82+
TEST (DummyReader_Fake_Frame) {
83+
84+
// Create cache object to hold test frames
85+
CacheMemory cache;
86+
87+
// Let's create some test frames
88+
for (int64_t frame_number = 1; frame_number <= 30; frame_number++) {
89+
// Create blank frame (with specific frame #, samples, and channels)
90+
// Sample count should be 44100 / 30 fps = 1470 samples per frame
91+
int sample_count = 1470;
92+
std::shared_ptr<openshot::Frame> f(new openshot::Frame(frame_number, sample_count, 2));
93+
94+
// Create test samples with incrementing value
95+
float *audio_buffer = new float[sample_count];
96+
for (int64_t sample_number = 0; sample_number < sample_count; sample_number++) {
97+
// Generate an incrementing audio sample value (just as an example)
98+
audio_buffer[sample_number] = float(frame_number) + (float(sample_number) / float(sample_count));
99+
}
100+
101+
// Add custom audio samples to Frame (bool replaceSamples, int destChannel, int destStartSample, const float* source,
102+
f->AddAudio(true, 0, 0, audio_buffer, sample_count, 1.0); // add channel 1
103+
f->AddAudio(true, 1, 0, audio_buffer, sample_count, 1.0); // add channel 2
104+
105+
// Add test frame to dummy reader
106+
cache.Add(f);
107+
}
108+
109+
// Create a default fraction (should be 1/1)
110+
openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 30.0, &cache);
111+
r.Open(); // Open the reader
112+
113+
// Verify our artificial audio sample data is correct
114+
CHECK_EQUAL(1, r.GetFrame(1)->number);
115+
CHECK_EQUAL(1, r.GetFrame(1)->GetAudioSamples(0)[0]);
116+
CHECK_CLOSE(1.00068033, r.GetFrame(1)->GetAudioSamples(0)[1], 0.00001);
117+
CHECK_CLOSE(1.00136054, r.GetFrame(1)->GetAudioSamples(0)[2], 0.00001);
118+
CHECK_EQUAL(2, r.GetFrame(2)->GetAudioSamples(0)[0]);
119+
CHECK_CLOSE(2.00068033, r.GetFrame(2)->GetAudioSamples(0)[1], 0.00001);
120+
CHECK_CLOSE(2.00136054, r.GetFrame(2)->GetAudioSamples(0)[2], 0.00001);
121+
122+
// Clean up
123+
cache.Clear();
124+
r.Close();
125+
}
126+
127+
TEST (DummyReader_Invalid_Fake_Frame) {
128+
// Create fake frames (with specific frame #, samples, and channels)
129+
std::shared_ptr<openshot::Frame> f1(new openshot::Frame(1, 1470, 2));
130+
std::shared_ptr<openshot::Frame> f2(new openshot::Frame(2, 1470, 2));
131+
132+
// Add test frames to cache object
133+
CacheMemory cache;
134+
cache.Add(f1);
135+
cache.Add(f2);
136+
137+
// Create a default fraction (should be 1/1)
138+
openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 30.0, &cache);
139+
r.Open();
140+
141+
// Verify exception
142+
CHECK_EQUAL(1, r.GetFrame(1)->number);
143+
CHECK_EQUAL(2, r.GetFrame(2)->number);
144+
CHECK_THROW(r.GetFrame(3)->number, InvalidFile);
145+
146+
// Clean up
147+
cache.Clear();
148+
r.Close();
149+
}

0 commit comments

Comments
 (0)