FastLED 3.9.15
Loading...
Searching...
No Matches
video.cpp.hpp
Go to the documentation of this file.
1#include "fl/fx/video.h"
2
3#include "crgb.h"
5#include "fl/log/log.h"
6#include "fl/math/math.h"
7#include "fl/stl/string.h"
8#include "fl/log/log.h"
9#include "fl/fx/frame.h"
12#include "fl/video/video_impl.h"
13#include "fl/stl/noexcept.h"
14
15#define DBG FL_DBG
16
17namespace fl {
18
20
21Video::Video(size_t pixelsPerFrame, float fps, size_t frame_history_count)
23 mImpl = fl::make_shared<VideoImpl>(pixelsPerFrame, fps, frame_history_count);
24}
25
26void Video::setFade(fl::u32 fadeInTime, fl::u32 fadeOutTime) {
27 mImpl->setFade(fadeInTime, fadeOutTime);
28}
29
30void Video::pause(fl::u32 now) { mImpl->pause(now); }
31
32void Video::resume(fl::u32 now) { mImpl->resume(now); }
33
34Video::~Video() FL_NOEXCEPT = default;
35Video::Video(const Video &) = default;
36Video &Video::operator=(const Video &) FL_NOEXCEPT = default;
37
38bool Video::begin(filebuf_ptr handle) {
39 if (!mImpl) {
40 FL_WARN("Video::begin: mImpl is null, manually constructed videos "
41 "must include full parameters.");
42 return false;
43 }
44 if (!handle) {
45 mError = "filebuf is null";
46 FL_DBG(mError.c_str());
47 return false;
48 }
49 if (mError.size()) {
50 FL_DBG(mError.c_str());
51 return false;
52 }
53 mError.clear();
54 mImpl->begin(handle);
55 return true;
56}
57
58bool Video::draw(fl::u32 now, fl::span<CRGB> leds) {
59 if (!mImpl) {
60 FL_WARN_IF(!mError.empty(), mError.c_str());
61 return false;
62 }
63 bool ok = mImpl->draw(now, leds);
64 if (!ok) {
65 // Interpret not being able to draw as a finished signal.
66 mFinished = true;
67 }
68 return ok;
69}
70
71void Video::draw(DrawContext context) {
72 if (!mImpl) {
73 FL_WARN_IF(!mError.empty(), mError.c_str());
74 return;
75 }
76 mImpl->draw(context.now, context.leds);
77}
78
80 if (!mImpl) {
81 return -1;
82 }
83 return mImpl->durationMicros();
84}
85
86string Video::fxName() const { return "Video"; }
87
88bool Video::draw(fl::u32 now, Frame *frame) {
89 if (!mImpl) {
90 return false;
91 }
92 return mImpl->draw(now, frame);
93}
94
95void Video::end() {
96 if (mImpl) {
97 mImpl->end();
98 }
99}
100
102 if (!mImpl) {
103 return;
104 }
105 mImpl->setTimeScale(timeScale);
106}
107
108float Video::timeScale() const {
109 if (!mImpl) {
110 return 1.0f;
111 }
112 return mImpl->timeScale();
113}
114
115string Video::error() const { return mError; }
116
117size_t Video::pixelsPerFrame() const {
118 if (!mImpl) {
119 return 0;
120 }
121 return mImpl->pixelsPerFrame();
122}
123
125 if (!mImpl) return false;
126 return mImpl->hasEmbeddedScreenMap();
127}
128
130 static const fl::string kEmpty;
131 if (!mImpl) return kEmpty;
132 return mImpl->embeddedScreenMapJson();
133}
134
136 if (!mImpl) {
137 return true;
138 }
139 return mFinished;
140}
141
143 if (!mImpl) {
144 return false;
145 }
146 return mImpl->rewind();
147}
148
149VideoFxWrapper::VideoFxWrapper(fl::shared_ptr<Fx> fx) : Fx1d(fx->getNumLeds()), mFx(fx) {
150 if (!mFx->hasFixedFrameRate(&mFps)) {
151 FL_WARN("VideoFxWrapper: Fx does not have a fixed frame rate, "
152 "assuming 30fps.");
153 mFps = 30.0f;
154 }
155 mVideo = fl::make_shared<VideoImpl>(mFx->getNumLeds(), mFps, 2);
156 mByteStream = fl::make_shared<memorybuf>(mFx->getNumLeds() * sizeof(CRGB));
157 mVideo->begin(mByteStream);
158}
159
161
162string VideoFxWrapper::fxName() const {
163 string out = "video_fx_wrapper: ";
164 out.append(mFx->fxName());
165 return out;
166}
167
169 if (mVideo->needsFrame(context.now)) {
170 mFx->draw(context); // use the leds in the context as a tmp buffer.
171 mByteStream->writeCRGB(
172 context.leds.data(),
173 mFx->getNumLeds()); // now write the leds to the byte stream.
174 }
175 bool ok = mVideo->draw(context.now, context.leds);
176 if (!ok) {
177 FL_WARN("VideoFxWrapper: draw failed.");
178 }
179}
180
181void VideoFxWrapper::setFade(fl::u32 fadeInTime, fl::u32 fadeOutTime) {
182 mVideo->setFade(fadeInTime, fadeOutTime);
183}
184
185} // namespace fl
fl::CRGB leds[NUM_LEDS]
Fx1d(u16 numLeds)
Definition fx1d.h:12
bool finished()
void draw(DrawContext context) override
Definition video.cpp.hpp:71
float timeScale() const
void end()
Definition video.cpp.hpp:95
bool begin(fl::filebuf_ptr h)
Definition video.cpp.hpp:38
Video() FL_NOEXCEPT
Definition video.cpp.hpp:19
void pause(fl::u32 now) override
Definition video.cpp.hpp:30
string fxName() const override
Definition video.cpp.hpp:86
size_t pixelsPerFrame() const
string mError
Definition video.h:91
bool mFinished
Definition video.h:89
~Video() FL_NOEXCEPT
void setFade(fl::u32 fadeInTime, fl::u32 fadeOutTime)
Definition video.cpp.hpp:26
string error() const
void setTimeScale(float timeScale)
VideoImplPtr mImpl
Definition video.h:90
bool hasEmbeddedScreenMap() const FL_NOEXCEPT
void resume(fl::u32 now) override
Definition video.cpp.hpp:32
bool rewind()
const fl::string & embeddedScreenMapJson() const FL_NOEXCEPT
i32 durationMicros() const
Definition video.cpp.hpp:79
void setFade(fl::u32 fadeInTime, fl::u32 fadeOutTime)
~VideoFxWrapper() FL_NOEXCEPT override
VideoImplPtr mVideo
Definition video.h:110
VideoFxWrapper(FxPtr fx)
string fxName() const override
memorybufPtr mByteStream
Definition video.h:111
void draw(DrawContext context) override
string & append(const bitset_fixed< N > &bs) FL_NOEXCEPT
Definition string.h:284
#define FL_WARN(X)
Definition log.h:276
#define FL_WARN_IF(COND, MSG)
Definition log.h:277
#define FL_DBG
Definition log.h:388
Centralized logging categories for FastLED hardware interfaces and subsystems.
shared_ptr< T > make_shared(Args &&... args) FL_NOEXCEPT
Definition shared_ptr.h:414
fl::shared_ptr< filebuf > filebuf_ptr
Definition idecoder.h:15
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
fl::span< CRGB > leds