FastLED 3.9.7
Loading...
Searching...
No Matches
video.cpp
1#include "fx/video.h"
2
3#include "crgb.h"
4#include "fx/video/pixel_stream.h"
5#include "fx/frame.h"
6#include "fx/video/frame_interpolator.h"
7#include "fl/dbg.h"
8#include "fl/str.h"
9#include "fl/warn.h"
10#include "fl/math_macros.h"
11#include "fx/video/video_impl.h"
12#include "fx/video/pixel_stream.h"
13#include "fl/bytestreammemory.h"
14
15#define DBG FASTLED_DBG
16
17using fl::FileHandlePtr;
18using fl::ByteStreamPtr;
19
20#include "fl/namespace.h"
21
22namespace fl {
23
24FASTLED_SMART_PTR(PixelStream);
25FASTLED_SMART_PTR(FrameInterpolator);
26FASTLED_SMART_PTR(Frame);
27
28Video::Video(): Fx1d(0) {
29}
30
31Video::Video(size_t pixelsPerFrame, float fps, size_t frame_history_count): Fx1d(pixelsPerFrame) {
32 mImpl = VideoImplPtr::New(pixelsPerFrame, fps, frame_history_count);
33}
34
35void Video::pause(uint32_t now) {
36 if (mImpl) {
37 mImpl->pause(now);
38 }
39}
40
41void Video::resume(uint32_t now) {
42 if (mImpl) {
43 mImpl->resume(now);
44 }
45}
46
47Video::~Video() = default;
48Video::Video(const Video &) = default;
49Video &Video::operator=(const Video &) = default;
50
51bool Video::begin(FileHandlePtr handle) {
52 if (!mImpl) {
53 FASTLED_WARN("Video::begin: mImpl is null, manually constructed videos must include full parameters.");
54 return false;
55 }
56 if (!handle) {
57 mError = "FileHandle is null";
58 FASTLED_DBG(mError.c_str());
59 return false;
60 }
61 if (mError.size()) {
62 FASTLED_DBG(mError.c_str());
63 return false;
64 }
65 mError.clear();
66 mImpl->begin(handle);
67 return true;
68}
69
70bool Video::beginStream(ByteStreamPtr bs) {
71 if (!bs) {
72 mError = "FileHandle is null";
73 FASTLED_DBG(mError.c_str());
74 return false;
75 }
76 if (mError.size()) {
77 FASTLED_DBG(mError.c_str());
78 return false;
79 }
80 mError.clear();
81 mImpl->beginStream(bs);
82 return true;
83}
84
85bool Video::draw(uint32_t now, CRGB *leds) {
86 if (!mImpl) {
87 FASTLED_WARN_IF(!mError.empty(), mError.c_str());
88 return false;
89 }
90 bool ok = mImpl->draw(now, leds);
91 if (!ok) {
92 // Interpret not being able to draw as a finished signal.
93 mFinished = true;
94 }
95 return ok;
96}
97
98void Video::draw(DrawContext context) {
99 if (!mImpl) {
100 FASTLED_WARN_IF(!mError.empty(), mError.c_str());
101 return;
102 }
103 mImpl->draw(context.now, context.leds);
104}
105
106Str Video::fxName() const {
107 return "Video";
108}
109
110bool Video::draw(uint32_t now, Frame *frame) {
111 if (!mImpl) {
112 return false;
113 }
114 return mImpl->draw(now, frame);
115}
116
117void Video::end() {
118 if (mImpl) {
119 mImpl->end();
120 }
121}
122
123void Video::setTimeScale(float timeScale) {
124 if (!mImpl) {
125 return;
126 }
127 mImpl->setTimeScale(timeScale);
128}
129
130float Video::timeScale() const {
131 if (!mImpl) {
132 return 1.0f;
133 }
134 return mImpl->timeScale();
135}
136
137Str Video::error() const {
138 return mError;
139}
140
141size_t Video::pixelsPerFrame() const {
142 if (!mImpl) {
143 return 0;
144 }
145 return mImpl->pixelsPerFrame();
146}
147
148bool Video::finished() {
149 if (!mImpl) {
150 return true;
151 }
152 return mFinished;
153}
154
155bool Video::rewind() {
156 if (!mImpl) {
157 return false;
158 }
159 return mImpl->rewind();
160}
161
162
163VideoFxWrapper::VideoFxWrapper(Ptr<Fx> fx) : Fx1d(fx->getNumLeds()), mFx(fx) {
164 if (!mFx->hasFixedFrameRate(&mFps)) {
165 FASTLED_WARN("VideoFxWrapper: Fx does not have a fixed frame rate, assuming 30fps.");
166 mFps = 30.0f;
167 }
168 mVideo = VideoImplPtr::New(fx->getNumLeds(), mFps, 2);
169 mByteStream = ByteStreamMemoryPtr::New(fx->getNumLeds() * sizeof(CRGB));
170 mVideo->beginStream(mByteStream);
171}
172
173VideoFxWrapper::~VideoFxWrapper() = default;
174
175Str VideoFxWrapper::fxName() const {
176 Str out = "video_fx_wrapper: ";
177 out.append(mFx->fxName());
178 return out;
179}
180
182 if (mVideo->needsFrame(context.now)) {
183 mFx->draw(context); // use the leds in the context as a tmp buffer.
184 mByteStream->writeCRGB(context.leds, mFx->getNumLeds()); // now write the leds to the byte stream.
185 }
186 bool ok = mVideo->draw(context.now, context.leds);
187 if (!ok) {
188 FASTLED_WARN("VideoFxWrapper: draw failed.");
189 }
190}
191
192
193} // namespace fl
Definition str.h:336
void draw(DrawContext context) override
Definition video.cpp:181
Defines the red, green, and blue (RGB) pixel struct.
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54