OpenShot Library | libopenshot  0.2.6
DummyReader.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for DummyReader class
4  * @author Jonathan Thomas <jonathan@openshot.org>
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 #ifndef OPENSHOT_DUMMY_READER_H
32 #define OPENSHOT_DUMMY_READER_H
33 
34 #include "ReaderBase.h"
35 
36 #include <cmath>
37 #include <ctime>
38 #include <iostream>
39 #include <omp.h>
40 #include <stdio.h>
41 #include <memory>
42 #include "CacheMemory.h"
43 #include "Fraction.h"
44 
45 namespace openshot
46 {
47  /**
48  * @brief This class is used as a simple, dummy reader, which can be very useful when writing
49  * unit tests. It can return a single blank frame or it can return custom frame objects
50  * which were passed into the constructor with a Cache object.
51  *
52  * A dummy reader can be created with any framerate or samplerate. This is useful in unit
53  * tests that need to test different framerates or samplerates.
54  *
55  * @note Timeline does buffering by requesting more frames than it
56  * strictly needs. Thus if you use this DummyReader with a custom
57  * cache in a Timeline, make sure it has enough
58  * frames. Specifically you need some frames after the last frame
59  * you plan to access through the Timeline.
60  *
61  * @code
62  * // Create cache object to store fake Frame objects
63  * CacheMemory cache;
64  *
65  * // Now let's create some test frames
66  * for (int64_t frame_number = 1; frame_number <= 30; frame_number++)
67  * {
68  * // Create blank frame (with specific frame #, samples, and channels)
69  * // Sample count should be 44100 / 30 fps = 1470 samples per frame
70  * int sample_count = 1470;
71  * auto f = std::make_shared<openshot::Frame>(frame_number, sample_count, 2);
72  *
73  * // Create test samples with incrementing value
74  * float *audio_buffer = new float[sample_count];
75  * for (int64_t sample_number = 0; sample_number < sample_count; sample_number++)
76  * {
77  * // Generate an incrementing audio sample value (just as an example)
78  * audio_buffer[sample_number] = float(frame_number) + (float(sample_number) / float(sample_count));
79  * }
80  *
81  * // Add custom audio samples to Frame (bool replaceSamples, int destChannel, int destStartSample, const float* source,
82  * // int numSamples, float gainToApplyToSource = 1.0f)
83  * f->AddAudio(true, 0, 0, audio_buffer, sample_count, 1.0); // add channel 1
84  * f->AddAudio(true, 1, 0, audio_buffer, sample_count, 1.0); // add channel 2
85  *
86  * // Add test frame to cache
87  * cache.Add(f);
88  * }
89  *
90  * // Create a reader (Fraction fps, int width, int height, int sample_rate, int channels, float duration, CacheBase* cache)
91  * openshot::DummyReader r(openshot::Fraction(30, 1), 1920, 1080, 44100, 2, 30.0, &cache);
92  * r.Open(); // Open the reader
93  *
94  * // Now let's verify our DummyReader works
95  * std::shared_ptr<openshot::Frame> f = r.GetFrame(1);
96  * // r.GetFrame(1)->GetAudioSamples(0)[1] should equal 1.00068033 based on our above calculations
97  *
98  * // Clean up
99  * r.Close();
100  * cache.Clear()
101  * @endcode
102  */
103  class DummyReader : public ReaderBase
104  {
105  private:
106  CacheBase* dummy_cache;
107  std::shared_ptr<openshot::Frame> image_frame;
108  bool is_open;
109 
110  /// Initialize variables used by constructor
111  void init(Fraction fps, int width, int height, int sample_rate, int channels, float duration);
112 
113  public:
114 
115  /// Blank constructor for DummyReader, with default settings.
116  DummyReader();
117 
118  /// Constructor for DummyReader.
119  DummyReader(openshot::Fraction fps, int width, int height, int sample_rate, int channels, float duration);
120 
121  /// Constructor for DummyReader which takes a frame cache object.
122  DummyReader(openshot::Fraction fps, int width, int height, int sample_rate, int channels, float duration, CacheBase* cache);
123 
124  virtual ~DummyReader();
125 
126  /// Close File
127  void Close() override;
128 
129  /// Get the cache object used by this reader (always returns NULL for this reader)
130  CacheMemory* GetCache() override { return NULL; };
131 
132  /// Get an openshot::Frame object for a specific frame number of this reader. All numbers
133  /// return the same Frame, since they all share the same image data.
134  ///
135  /// @returns The requested frame (containing the image)
136  /// @param requested_frame The frame number that is requested.
137  std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame) override;
138 
139  /// Determine if reader is open or closed
140  bool IsOpen() override { return is_open; };
141 
142  /// Return the type name of the class
143  std::string Name() override { return "DummyReader"; };
144 
145  // Get and Set JSON methods
146  std::string Json() const override; ///< Generate JSON string of this object
147  void SetJson(const std::string value) override; ///< Load JSON string into this object
148  Json::Value JsonValue() const override; ///< Generate Json::Value for this object
149  void SetJsonValue(const Json::Value root) override; ///< Load Json::Value into this object
150 
151  /// Open File - which is called by the constructor automatically
152  void Open() override;
153  };
154 
155 }
156 
157 #endif
Header file for Fraction class.
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
std::string Name() override
Return the type name of the class.
Definition: DummyReader.h:143
Header file for ReaderBase class.
This class is used as a simple, dummy reader, which can be very useful when writing unit tests...
Definition: DummyReader.h:103
bool IsOpen() override
Determine if reader is open or closed.
Definition: DummyReader.h:140
void Open() override
Open File - which is called by the constructor automatically.
Definition: DummyReader.cpp:94
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:97
std::string Json() const override
Generate JSON string of this object.
DummyReader()
Blank constructor for DummyReader, with default settings.
Definition: DummyReader.cpp:67
void Close() override
Close File.
Header file for CacheMemory class.
void SetJson(const std::string value) override
Load JSON string into this object.
This class represents a fraction.
Definition: Fraction.h:48
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:48
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
CacheMemory * GetCache() override
Get the cache object used by this reader (always returns NULL for this reader)
Definition: DummyReader.h:130
Json::Value JsonValue() const override
Generate Json::Value for this object.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:50