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 "fx/frame.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"
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
27
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";
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
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
125 if (mImpl) {
126 mImpl->end();
127 }
128}
129
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
145 return mError;
146}
147
148size_t Video::pixelsPerFrame() const {
149 if (!mImpl) {
150 return 0;
151 }
152 return mImpl->pixelsPerFrame();
153}
154
156 if (!mImpl) {
157 return true;
158 }
159 return mFinished;
160}
161
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
182
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
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:102
Str & append(const char *str)
Definition str.h:409
Definition str.h:368
const char * c_str() const
Definition str.h:247
void pause(uint32_t now) override
Definition video.cpp:39
Str error() const
Definition video.cpp:144
bool finished()
Definition video.cpp:155
void draw(DrawContext context) override
Definition video.cpp:98
float timeScale() const
Definition video.cpp:137
void end()
Definition video.cpp:124
void setFade(uint32_t fadeInTime, uint32_t fadeOutTime)
Definition video.cpp:35
bool begin(fl::FileHandlePtr h)
Str fxName() const override
Definition video.cpp:113
size_t pixelsPerFrame() const
Definition video.cpp:148
bool mFinished
Definition video.h:75
bool beginStream(fl::ByteStreamPtr s)
Str mError
Definition video.h:77
void setTimeScale(float timeScale)
Definition video.cpp:130
Video & operator=(const Video &)
void resume(uint32_t now) override
Definition video.cpp:43
VideoImplPtr mImpl
Definition video.h:76
bool rewind()
Definition video.cpp:162
int32_t durationMicros() const
Definition video.cpp:106
Str fxName() const override
Definition video.cpp:183
VideoImplPtr mVideo
Definition video.h:98
VideoFxWrapper(FxPtr fx)
ByteStreamMemoryPtr mByteStream
Definition video.h:99
void draw(DrawContext context) override
Definition video.cpp:189
~VideoFxWrapper() override
void setFade(uint32_t fadeInTime, uint32_t fadeOutTime)
Definition video.cpp:200
Defines the red, green, and blue (RGB) pixel struct.
#define FASTLED_DBG(X)
Definition dbg.h:60
#define FASTLED_SMART_PTR(type)
Definition ptr.h:17
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
#define FASTLED_WARN
Definition warn.h:7
#define FASTLED_WARN_IF
Definition warn.h:8