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