FastLED 3.9.12
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::setFade(uint32_t fadeInTime, uint32_t fadeOutTime) {
36 mImpl->setFade(fadeInTime, fadeOutTime);
37}
38
39void Video::pause(uint32_t now) {
40 mImpl->pause(now);
41}
42
43void Video::resume(uint32_t now) {
44 mImpl->resume(now);
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
106int32_t Video::durationMicros() const {
107 if (!mImpl) {
108 return -1;
109 }
110 return mImpl->durationMicros();
111}
112
113Str Video::fxName() const {
114 return "Video";
115}
116
117bool Video::draw(uint32_t now, Frame *frame) {
118 if (!mImpl) {
119 return false;
120 }
121 return mImpl->draw(now, frame);
122}
123
124void Video::end() {
125 if (mImpl) {
126 mImpl->end();
127 }
128}
129
130void Video::setTimeScale(float timeScale) {
131 if (!mImpl) {
132 return;
133 }
134 mImpl->setTimeScale(timeScale);
135}
136
137float Video::timeScale() const {
138 if (!mImpl) {
139 return 1.0f;
140 }
141 return mImpl->timeScale();
142}
143
144Str Video::error() const {
145 return mError;
146}
147
148size_t Video::pixelsPerFrame() const {
149 if (!mImpl) {
150 return 0;
151 }
152 return mImpl->pixelsPerFrame();
153}
154
155bool Video::finished() {
156 if (!mImpl) {
157 return true;
158 }
159 return mFinished;
160}
161
162bool Video::rewind() {
163 if (!mImpl) {
164 return false;
165 }
166 return mImpl->rewind();
167}
168
169
170VideoFxWrapper::VideoFxWrapper(Ptr<Fx> fx) : Fx1d(fx->getNumLeds()), mFx(fx) {
171 if (!mFx->hasFixedFrameRate(&mFps)) {
172 FASTLED_WARN("VideoFxWrapper: Fx does not have a fixed frame rate, assuming 30fps.");
173 mFps = 30.0f;
174 }
175 mVideo = VideoImplPtr::New(mFx->getNumLeds(), mFps, 2);
176 mByteStream = ByteStreamMemoryPtr::New(mFx->getNumLeds() * sizeof(CRGB));
177 mVideo->beginStream(mByteStream);
178}
179
180
181VideoFxWrapper::~VideoFxWrapper() = default;
182
183Str VideoFxWrapper::fxName() const {
184 Str out = "video_fx_wrapper: ";
185 out.append(mFx->fxName());
186 return out;
187}
188
190 if (mVideo->needsFrame(context.now)) {
191 mFx->draw(context); // use the leds in the context as a tmp buffer.
192 mByteStream->writeCRGB(context.leds, mFx->getNumLeds()); // now write the leds to the byte stream.
193 }
194 bool ok = mVideo->draw(context.now, context.leds);
195 if (!ok) {
196 FASTLED_WARN("VideoFxWrapper: draw failed.");
197 }
198}
199
200void VideoFxWrapper::setFade(uint32_t fadeInTime, uint32_t fadeOutTime) {
201 mVideo->setFade(fadeInTime, fadeOutTime);
202}
203
204
205} // namespace fl
void draw(DrawContext context) override
Definition video.cpp:189
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