GCC Code Coverage Report


Directory: ./
File: effects.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 237 306 77.5%
Functions: 21 23 91.3%
Branches: 256 494 51.8%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2022 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12
13 #include "fpp-pch.h"
14
15 #include "fpp-json.h"
16
17 #include "Warnings.h" // WarningHolder -- needed directly for NOPCH builds
18
19 #include <algorithm>
20 #include <array>
21 #include <atomic>
22 #include <cmath>
23 #include <cstdint>
24 #include <cstring>
25 #include <fnmatch.h>
26 #include <functional>
27 #include <list>
28 #include <memory>
29 #include <mutex>
30 #include <numeric>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string>
34 #include <utility>
35 #include <vector>
36
37 #include "Events.h"
38 #include "Sequence.h"
39 #include "common.h"
40 #include "log.h"
41 #include "settings.h"
42 #include "channeloutput/channeloutputthread.h"
43 #include "commands/Commands.h" // lines 58-58
44 #include "fseq/FSEQFile.h"
45
46 #include "effects.h"
47
48 #define MAX_EFFECTS 100
49
50 class FPPeffect {
51 public:
52 163 FPPeffect() :
53 163 fp(nullptr) {}
54 48 ~FPPeffect() {
55
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 10 times.
48 if (lastFrame)
56
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 delete lastFrame;
57
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 if (fp)
58
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 delete fp;
59 48 }
60
61 std::string name;
62 FSEQFile* fp;
63 int loop;
64 int background;
65
66 // An effect keeps its own clock, so it plays at its file's frame rate
67 // rather than one file frame per output frame: the output thread runs at
68 // the playing sequence's rate, and an effect at a different rate used to
69 // play at that one instead (a 50 ms effect over a 25 ms sequence ran
70 // twice as fast). elapsedUs is how far into the file the effect is; it
71 // advances only while the effect is drawn, so a background effect
72 // paused by pauseBackgroundEffects resumes where it was.
73 long long stepUs = 50000;
74 long long elapsedUs = 0;
75 long long lastDrawnUs = 0; // 0: not drawn since it (re)started or was paused
76
77 // The frame last read, kept for the output frames that show it again
78 // when the effect is slower than the output.
79 FSEQFile::FrameData* lastFrame = nullptr;
80 uint32_t lastFrameNum = 0;
81 };
82
83 static int effectCount = 0;
84 // Changed by the settings listener while the output thread reads it.
85 static std::atomic_int pauseBackgroundEffects{ 0 };
86 static std::array<FPPeffect*, MAX_EFFECTS> effects;
87 static std::list<std::pair<uint32_t, uint32_t>> clearRanges;
88 static std::mutex effectsLock;
89 // effectIDs in the order OverlayEffects() composites them: oldest (re)started
90 // first, most-recently (re)started last. Composited in this order, so the
91 // last entry here is drawn last and wins on any channel two effects share.
92 static std::vector<int> effectRenderOrder;
93
94 /*
95 * Initialize effects constructs
96 */
97 2651 int InitEffects(void) {
98
3/6
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2651 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2651 times.
✗ Branch 5 not taken.
2651 std::string localFilename(FPP_DIR_EFFECT("/background.eseq"));
99
100
3/4
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✓ Branch 3 taken 2590 times.
2712 if ((getFPPmode() == REMOTE_MODE) &&
101
11/22
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 61 times.
✓ Branch 8 taken 61 times.
✓ Branch 9 taken 2590 times.
✓ Branch 10 taken 61 times.
✓ Branch 11 taken 2590 times.
✓ Branch 12 taken 61 times.
✓ Branch 13 taken 2590 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 2651 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
2778 CheckForHostSpecificFile(getSetting("HostName").c_str(), localFilename)) {
102 ✗ localFilename = "background_";
103 ✗ localFilename += getSetting("HostName");
104
105 ✗ LogInfo(VB_EFFECT, "Automatically starting background effect "
106 "sequence %s\n",
107 localFilename.c_str());
108
109 ✗ StartEffect(localFilename.c_str(), 0, 1, true);
110
3/4
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2650 times.
2651 } else if (FileExists(localFilename)) {
111
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogInfo(VB_EFFECT, "Automatically starting background effect sequence "
112 "background.eseq\n");
113
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 StartEffect("background", 0, 1, true);
114 }
115
116
1/2
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
2651 pauseBackgroundEffects = getSettingInt("pauseBackgroundEffects");
117
3/6
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2651 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2651 times.
✗ Branch 5 not taken.
12665 registerSettingsListener("effects", "pauseBackgroundEffects",
118 295 [](const std::string& value) {
119 6 LogDebug(VB_SETTING, "Setting pauseBackgroundEffects to %s\n", value.c_str());
120 6 pauseBackgroundEffects = getSettingInt("pauseBackgroundEffects");
121 6 });
122
123 std::function<void(const std::string&, const std::string&)>
124 2651 effect_callback = [](const std::string& topic_in,
125 const std::string& payload) {
126 5 LogDebug(VB_CONTROL, "System Callback for %s\n", topic_in.c_str());
127
128
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 if (0 == topic_in.compare(topic_in.length() - 5, 5, "/stop")) {
129
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (payload == "") {
130 1 StopAllEffects();
131 } else {
132
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 StopEffect(payload.c_str());
133 }
134
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 } else if (0 == topic_in.compare(topic_in.length() - 6, 6, "/start")) {
135
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 StartEffect(payload.c_str(), 0);
136
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (0 == topic_in.compare(topic_in.length() - 8, 8, "/startbg")) {
137
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 StartEffect(payload.c_str(), 0, 0, true);
138 }
139 2656 };
140
141
2/4
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2651 times.
✗ Branch 3 not taken.
2651 Events::AddCallback("/set/effect/#", effect_callback);
142
143 2651 return 1;
144 2651 }
145
146 /*
147 * Close effects constructs
148 */
149 2620 void CloseEffects(void) {
150 2620 }
151
152 /*
153 * Get next available effect ID
154 *
155 * Assumes effectsLock is held already
156 */
157 163 int GetNextEffectID(void) {
158 163 int i = -1;
159
160
1/2
✓ Branch 0 taken 5129 times.
✗ Branch 1 not taken.
5129 for (i = 0; i < MAX_EFFECTS; i++) {
161
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 4966 times.
5129 if (!effects[i])
162 163 return i;
163 }
164
165 ✗ return -1;
166 }
167
168 /*
169 * Check to see if any effects are running
170 */
171 496988 int IsEffectRunning(void) {
172 496988 int result = 0;
173
1/2
✓ Branch 0 taken 496988 times.
✗ Branch 1 not taken.
496988 std::unique_lock<std::mutex> lock(effectsLock);
174 496988 result = effectCount;
175
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 496918 times.
496988 if (!clearRanges.empty()) {
176 70 ++result;
177 }
178 496988 return result;
179 496988 }
180
181 /*
182 * Assumes effectsLock is already held. Moves effectID to the most-recent end
183 * of effectRenderOrder (the back of the vector, since OverlayEffects() walks
184 * it front-to-back and composites last = wins), inserting it if not already
185 * present. Used by both a fresh StartEffect() allocation and a
186 * RestartEffect() re-trigger of an existing slot, so that whichever effect
187 * started or restarted most recently visually wins regardless of its array
188 * slot.
189 */
190 164 static void BringEffectToFront(int effectID) {
191
1/2
✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
164 auto it = std::find(effectRenderOrder.begin(), effectRenderOrder.end(), effectID);
192
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 163 times.
164 if (it != effectRenderOrder.end()) {
193
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 effectRenderOrder.erase(it);
194 }
195
1/2
✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
164 effectRenderOrder.push_back(effectID);
196 164 }
197
198 /*
199 * Assumes effectsLock is already held. Removes effectID from the render order if
200 * present.
201 */
202 48 static void RemoveEffectFromRenderOrder(int effectID) {
203
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 auto it = std::find(effectRenderOrder.begin(), effectRenderOrder.end(), effectID);
204
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 if (it != effectRenderOrder.end()) {
205
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 effectRenderOrder.erase(it);
206 }
207 48 }
208
209 213 int StartEffect(FSEQFile* fseq, const std::string& effectName, int loop, bool bg) {
210
1/2
✓ Branch 0 taken 213 times.
✗ Branch 1 not taken.
213 std::unique_lock<std::mutex> lock(effectsLock);
211
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 163 times.
213 if (effectCount >= MAX_EFFECTS) {
212
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 LogErr(VB_EFFECT, "Unable to start effect %s, maximum number of effects already running\n", effectName.c_str());
213 // This function takes ownership of fseq -- the effectID failure below
214 // deletes it, and so does StopEffect. Returning here without deleting
215 // leaked the whole FSEQFile, mapping and all, on every rejected start,
216 // which is exactly when a controller is already at the ceiling and
217 // retrying: one report leaked 48 of them before running out of address
218 // space to create the next read-ahead thread in.
219
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 delete fseq;
220 50 return -1;
221 }
222 163 int effectID = -1;
223 163 int frameTime = fseq->getStepTime();
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163 times.
163 if (frameTime <= 0) {
225 ✗ frameTime = 50;
226 }
227
228
1/2
✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
163 effectID = GetNextEffectID();
229
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163 times.
163 if (effectID < 0) {
231 ✗ LogErr(VB_EFFECT, "Unable to start effect %s, unable to determine next effect ID\n", effectName.c_str());
232 ✗ delete fseq;
233 ✗ return effectID;
234 }
235
236
2/4
✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 163 times.
163 effects[effectID] = new FPPeffect;
237
1/2
✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
163 effects[effectID]->name = effectName;
238 163 effects[effectID]->fp = fseq;
239 163 effects[effectID]->loop = loop;
240 163 effects[effectID]->background = bg;
241 163 effects[effectID]->stepUs = frameTime * 1000LL;
242
1/2
✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
163 BringEffectToFront(effectID);
243
244 163 effectCount++;
245 163 int tmpec = effectCount;
246
1/2
✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
163 lock.unlock();
247
248
1/2
✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
163 StartChannelOutputThread();
249
250
7/8
✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 155 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 115 times.
✓ Branch 5 taken 40 times.
✓ Branch 6 taken 41 times.
✓ Branch 7 taken 122 times.
278 if (!sequence->IsSequenceRunning() &&
251
3/4
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 114 times.
115 (tmpec == 1 || frameTime < 1000.0f / GetChannelOutputRefreshRate())) {
252 // No sequence running: output at the rate of the fastest effect, so
253 // none of them has frames dropped. Each plays at its own rate either
254 // way (see OverlayEffect()); this only sets how often they're sampled.
255
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 SetChannelOutputRefreshRate(1000.0f / frameTime);
256 }
257
258 163 return effectID;
259 213 }
260
261 163 int StartFSEQAsEffect(const std::string& fseqName, int loop, bool bg) {
262
1/2
✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
163 LogInfo(VB_EFFECT, "Starting FSEQ %s as effect\n", fseqName.c_str());
263
264
5/10
✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 163 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 163 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 163 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 163 times.
✗ Branch 9 not taken.
163 std::string filename = FPP_DIR_SEQUENCE("/" + fseqName + ".fseq");
265
266
1/2
✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
163 FSEQFile* fseq = FSEQFile::openFSEQFile(filename);
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163 times.
163 if (!fseq) {
268 ✗ LogErr(VB_EFFECT, "Unable to open effect: %s\n", filename.c_str());
269 ✗ WarningHolder::AddWarningTimeout(60, 49, "Effect could not be started: cannot open " + filename);
270 ✗ return -1;
271 }
272
1/2
✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
163 return StartEffect(fseq, fseqName, loop, bg);
273 163 }
274
275 /*
276 * Start a new effect offset at the specified channel number
277 */
278 56 int StartEffect(const std::string& effectName, int startChannel, int loop, bool bg) {
279
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 LogInfo(VB_EFFECT, "Starting effect %s at channel %d\n", effectName.c_str(), startChannel);
280
281
5/10
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 56 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 56 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 56 times.
✗ Branch 9 not taken.
56 std::string filename = FPP_DIR_EFFECT("/" + effectName + ".eseq");
282
283
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 FSEQFile* fseq = FSEQFile::openFSEQFile(filename);
284
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 50 times.
56 if (!fseq) {
285
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 LogErr(VB_EFFECT, "Unable to open effect: %s\n", filename.c_str());
286
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 WarningHolder::AddWarningTimeout(60, 49, "Effect could not be started: cannot open " + filename);
287 6 return -1;
288 }
289
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 V2FSEQFile* v2fseq = dynamic_cast<V2FSEQFile*>(fseq);
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (!v2fseq) {
291 ✗ delete fseq;
292 ✗ LogErr(VB_EFFECT, "Effect file not a correct eseq file: %s\n", filename.c_str());
293 ✗ WarningHolder::AddWarningTimeout(60, 49, "Effect could not be started: " + filename + " is not a valid eseq file");
294 ✗ return -1;
295 }
296
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (v2fseq->m_sparseRanges.size() == 0) {
298 ✗ LogErr(VB_EFFECT, "eseq file must have at least one model range.");
299 ✗ WarningHolder::AddWarningTimeout(60, 49, "Effect could not be started: " + filename + " has no model range");
300 ✗ delete fseq;
301 ✗ return -1;
302 }
303
304
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 47 times.
50 if (startChannel != 0) {
305 // This will need to change if/when we support multiple models per file
306 3 v2fseq->m_sparseRanges[0].first = startChannel - 1;
307 }
308
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 return StartEffect(v2fseq, effectName, loop, bg);
309 56 }
310
311 /*
312 * Queue a channel range to be zeroed by OverlayEffects(). The ranges come
313 * from the effect file's header (or a caller-supplied start channel), so they
314 * are clamped to the channel buffer here: readFrame() already ignores a range
315 * past FPPD_MAX_CHANNELS, but the memset() that clears it would not. Assumes
316 * effectsLock is already held.
317 */
318 79 static void AddClearRange(uint32_t start, uint32_t len) {
319
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 75 times.
79 if (start >= FPPD_MAX_CHANNELS) {
320 4 return;
321 }
322 75 len = std::min(len, (uint32_t)FPPD_MAX_CHANNELS - start);
323
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 2 times.
75 if (len) {
324
1/2
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
73 clearRanges.push_back(std::pair<uint32_t, uint32_t>(start, len));
325 }
326 }
327
328 /*
329 * Helper function to stop an effect, assumes effectsLock is already held
330 */
331 48 void StopEffectHelper(int effectID) {
332 48 FPPeffect* e = NULL;
333 // Slots are validated by all callers, but effect IDs also arrive parsed
334 // from sequence tracks and command arguments -- never trust the index.
335
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
48 if (effectID < 0 || effectID >= MAX_EFFECTS) {
336 ✗ return;
337 }
338 48 e = effects[effectID];
339
340
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 if (e->fp) {
341
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 V2FSEQFile* v2fseq = dynamic_cast<V2FSEQFile*>(e->fp);
342
5/6
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 41 times.
✓ Branch 5 taken 7 times.
48 if (v2fseq && v2fseq->m_sparseRanges.size() != 0) {
343
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 41 times.
82 for (auto& a : v2fseq->m_sparseRanges) {
344
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 AddClearRange(a.first, a.second);
345 }
346
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 41 times.
72 for (auto& a : v2fseq->m_rangesToRead) {
347
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 AddClearRange(a.first, a.second);
348 }
349 } else {
350 // not sparse and not eseq, entire range
351 7 AddClearRange(0, e->fp->getChannelCount());
352 }
353 }
354
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 delete e;
355 48 effects[effectID] = NULL;
356 48 effectCount--;
357 48 RemoveEffectFromRenderOrder(effectID);
358 }
359
360 /*
361 * Stop all effects named effectName
362 */
363 26 int StopEffect(const std::string& effectName) {
364
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 LogDebug(VB_EFFECT, "StopEffect(%s)\n", effectName.c_str());
365
366
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 std::unique_lock<std::mutex> lock(effectsLock);
367
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 std::vector<std::string> names = split(effectName, ',');
368
369
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 26 times.
53 for (int j = 0; j < names.size(); j++) {
370
2/2
✓ Branch 0 taken 2700 times.
✓ Branch 1 taken 27 times.
2727 for (int i = 0; i < MAX_EFFECTS; i++) {
371
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 2670 times.
2700 if (effects[i]) {
372
4/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 7 times.
39 if ((effects[i]->name == names[j]) ||
373
3/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 7 times.
9 (!fnmatch(names[j].c_str(), effects[i]->name.c_str(), FNM_PATHNAME))) {
374
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 StopEffectHelper(i);
375 }
376 }
377 }
378 }
379
380
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 lock.unlock();
381
382
5/6
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 20 times.
32 if ((!IsEffectRunning()) &&
383
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 (!sequence->IsSequenceRunning())) {
384
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 sequence->SendBlankingData();
385 }
386
387 26 return 1;
388 26 }
389
390 /*
391 * Stop a single effect
392 */
393 ✗ int StopEffect(int effectID) {
394 ✗ FPPeffect* e = NULL;
395
396 ✗ LogDebug(VB_EFFECT, "StopEffect(%d)\n", effectID);
397
398 ✗ std::unique_lock<std::mutex> lock(effectsLock);
399 ✗ if (effectID < 0 || effectID >= MAX_EFFECTS || !effects[effectID]) {
400 ✗ return 0;
401 }
402
403 ✗ StopEffectHelper(effectID);
404 ✗ lock.unlock();
405
406 ✗ if ((!IsEffectRunning()) &&
407 ✗ (!sequence->IsSequenceRunning())) {
408 ✗ sequence->SendBlankingData();
409 }
410
411 ✗ return 1;
412 ✗ }
413
414 /*
415 * Restart a single effect in place, i.e. jump it back to frame 0 without
416 * closing and reopening the underlying FSEQFile. This reuses the exact same
417 * mechanism OverlayEffect() already uses to wrap a looping effect back to
418 * the start (its clock goes back to zero), so it needs no new file
419 * open, no new read-ahead thread, and no teardown of the one already
420 * running - unlike a stop+start, it can't add to the concurrent-thread
421 * pressure that caused the crashes in issue #2815.
422 *
423 * Matches on both ID and name: effectID slots are recycled, so a caller
424 * that remembered an ID from an earlier point in time could otherwise end
425 * up restarting an unrelated effect that has since taken over that slot.
426 * Requiring the name to also match makes that mismatch a safe no-op.
427 */
428 1 int RestartEffect(int effectID, const std::string& effectName) {
429
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogDebug(VB_EFFECT, "RestartEffect(%d, %s)\n", effectID, effectName.c_str());
430
431
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::unique_lock<std::mutex> lock(effectsLock);
432
5/10
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 if (effectID < 0 || effectID >= MAX_EFFECTS || !effects[effectID] || effects[effectID]->name != effectName) {
433 ✗ return 0;
434 }
435
436 1 effects[effectID]->elapsedUs = 0;
437 1 effects[effectID]->lastDrawnUs = 0;
438
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 BringEffectToFront(effectID);
439 1 return 1;
440 1 }
441
442 /*
443 * Stop all effects
444 */
445 9 void StopAllEffects(void) {
446 int i;
447
448
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 LogDebug(VB_EFFECT, "Stopping all effects\n");
449
450
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 std::unique_lock<std::mutex> lock(effectsLock);
451
452
2/2
✓ Branch 0 taken 900 times.
✓ Branch 1 taken 9 times.
909 for (i = 0; i < MAX_EFFECTS; i++) {
453
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 890 times.
900 if (effects[i])
454
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 StopEffectHelper(i);
455 }
456
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 lock.unlock();
457
3/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9 times.
9 if ((!IsEffectRunning()) &&
458 ✗ (!sequence->IsSequenceRunning()))
459 ✗ sequence->SendBlankingData();
460 9 }
461
462 /*
463 * Overlay a single effect onto raw channel data
464 */
465 3248 int OverlayEffect(int effectID, char* channelData) {
466 3248 FPPeffect* e = NULL;
467
4/8
✓ Branch 0 taken 3248 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3248 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3248 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3248 times.
3248 if (effectID < 0 || effectID >= MAX_EFFECTS || !effects[effectID]) {
468 ✗ LogErr(VB_EFFECT, "Invalid Effect ID %d\n", effectID);
469 ✗ return 0;
470 }
471
472 3248 e = effects[effectID];
473
474 3248 long long now = GetMonotonicTimeMicros();
475
2/2
✓ Branch 0 taken 3090 times.
✓ Branch 1 taken 158 times.
3248 if (e->lastDrawnUs) {
476 3090 e->elapsedUs += now - e->lastDrawnUs;
477 }
478 3248 e->lastDrawnUs = now;
479
480 // Which file frame is showing now. The output samples the effect every
481 // output period, and wakes a little early or late each time; a frame
482 // boundary that fell right where a sample does would show a frame twice
483 // or skip one at random (at equal rates, nearly every frame). The
484 // samples land on multiples of gcd(output period, step) into a frame, so
485 // shifting the boundaries half that puts them as far from any sample as
486 // can be: at equal rates, half a frame either way.
487 3248 long long periodUs = std::lround(1000000.0 / GetChannelOutputRefreshRate());
488
1/2
✓ Branch 0 taken 3248 times.
✗ Branch 1 not taken.
3248 long long offsetUs = periodUs > 0 ? std::gcd(periodUs, e->stepUs) / 2 : 0;
489 3248 long long frame = (e->elapsedUs + offsetUs) / e->stepUs;
490
491 3248 uint32_t numFrames = e->fp->getNumFrames();
492
6/6
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 3204 times.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 29 times.
✓ Branch 5 taken 2 times.
3248 if (frame >= numFrames && e->loop && numFrames) {
493 29 long long loops = frame / numFrames;
494 29 frame -= loops * numFrames;
495 29 e->elapsedUs -= loops * numFrames * e->stepUs;
496 }
497
498 3248 FSEQFile::FrameData* d = nullptr;
499
2/2
✓ Branch 0 taken 3233 times.
✓ Branch 1 taken 15 times.
3248 if (frame < numFrames) {
500
4/4
✓ Branch 0 taken 3083 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 92 times.
✓ Branch 3 taken 2991 times.
3233 if (e->lastFrame && e->lastFrameNum == frame) {
501 92 d = e->lastFrame;
502 } else {
503 3141 d = e->fp->getFrame(frame);
504
1/2
✓ Branch 0 taken 3141 times.
✗ Branch 1 not taken.
3141 if (d) {
505
2/2
✓ Branch 0 taken 2991 times.
✓ Branch 1 taken 150 times.
3141 delete e->lastFrame;
506 3141 e->lastFrame = d;
507 3141 e->lastFrameNum = frame;
508 }
509 }
510 }
511
2/2
✓ Branch 0 taken 3233 times.
✓ Branch 1 taken 15 times.
3248 if (d) {
512 3233 d->readFrame((uint8_t*)channelData, FPPD_MAX_CHANNELS);
513 3233 return 1;
514 } else {
515 15 StopEffectHelper(effectID);
516
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 15 times.
32 for (auto& rng : clearRanges) {
517 17 memset(&channelData[rng.first], 0, rng.second);
518 }
519 15 clearRanges.clear();
520 }
521
522 15 return 0;
523 }
524
525 /*
526 * Overlay current effects onto raw channel data
527 */
528 1759 int OverlayEffects(char* channelData) {
529 1759 int dataRead = 0;
530
531
1/2
✓ Branch 0 taken 1759 times.
✗ Branch 1 not taken.
1759 std::unique_lock<std::mutex> lock(effectsLock);
532
533 // for effects that have been stopped, we need to clear the data
534
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 1759 times.
1808 for (auto& rng : clearRanges) {
535 49 memset(&channelData[rng.first], 0, rng.second);
536 }
537 1759 clearRanges.clear();
538
539
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 1738 times.
1759 if (effectCount == 0) {
540 21 return 0;
541 }
542
543 1738 int skipBackground = 0;
544
6/8
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1718 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 20 times.
✓ Branch 7 taken 1718 times.
1738 if (pauseBackgroundEffects && sequence->IsSequenceRunning()) {
545 20 skipBackground = 1;
546 }
547
548 // Snapshot into a reused scratch buffer (not a fresh local) so this
549 // doesn't malloc/free every frame: OverlayEffect() below can call
550 // StopEffectHelper() -> RemoveEffectFromRenderOrder() on natural EOF,
551 // mutating the live list mid-loop, so we still need a copy to iterate
552 // safely - just not a newly-allocated one each time. Safe as `static`
553 // even though OverlayEffects() can be entered from more than one thread
554 // (normally RunChannelOutputThread, but also directly from whatever
555 // thread calls Sequence::SendBlankingData() when the channel output
556 // thread isn't running) because the whole function body, including
557 // every access to `order`, runs under effectsLock - concurrent calls
558 // are serialized by the mutex, not by single-thread ownership.
559
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1706 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
1738 static std::vector<int> order;
560
1/2
✓ Branch 0 taken 1738 times.
✗ Branch 1 not taken.
1738 order = effectRenderOrder;
561
2/2
✓ Branch 0 taken 3268 times.
✓ Branch 1 taken 1738 times.
5006 for (int id : order) {
562
1/2
✓ Branch 0 taken 3268 times.
✗ Branch 1 not taken.
3268 if (effects[id]) {
563
5/6
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 3235 times.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3248 times.
✓ Branch 5 taken 20 times.
3301 if ((!skipBackground) ||
564
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 20 times.
33 (skipBackground && (!effects[id]->background))) {
565
1/2
✓ Branch 0 taken 3248 times.
✗ Branch 1 not taken.
3248 dataRead |= OverlayEffect(id, channelData);
566 } else {
567 // paused: its clock stops until it's drawn again
568 20 effects[id]->lastDrawnUs = 0;
569 }
570 }
571 }
572
573
1/2
✓ Branch 0 taken 1738 times.
✗ Branch 1 not taken.
1738 lock.unlock();
574
575 1756 if ((dataRead == 0) &&
576
7/8
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1720 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 1730 times.
1749 (!IsEffectRunning()) &&
577
3/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 3 times.
11 (!sequence->IsSequenceRunning())) {
578
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 sequence->SendBlankingData();
579 }
580
581 1738 return 1;
582 1759 }
583
584 385 Json::Value GetRunningEffectsJson() {
585
1/2
✓ Branch 0 taken 385 times.
✗ Branch 1 not taken.
385 Json::Value arr(Json::arrayValue);
586 int i;
587
1/2
✓ Branch 0 taken 385 times.
✗ Branch 1 not taken.
385 std::unique_lock<std::mutex> lock(effectsLock);
588
589
2/2
✓ Branch 0 taken 38500 times.
✓ Branch 1 taken 385 times.
38885 for (i = 0; i < MAX_EFFECTS; i++) {
590
2/2
✓ Branch 0 taken 718 times.
✓ Branch 1 taken 37782 times.
38500 if (effects[i]) {
591
1/2
✓ Branch 0 taken 718 times.
✗ Branch 1 not taken.
718 Json::Value obj;
592
2/4
✓ Branch 0 taken 718 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 718 times.
✗ Branch 3 not taken.
718 obj["id"] = i;
593
2/4
✓ Branch 0 taken 718 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 718 times.
✗ Branch 3 not taken.
718 obj["name"] = effects[i]->name;
594
1/2
✓ Branch 0 taken 718 times.
✗ Branch 1 not taken.
718 arr.append(obj);
595 718 }
596 }
597
598 385 return arr;
599 385 }
600
601 /*
602 * Get list of running effects and their IDs
603 *
604 * Format: [EFFECTID1,EFFECTNAME1[,EFFECTID2,EFFECTNAME2]...]
605 *
606 * NOTE: Caller is responsible for freeing string allocated
607 */
608 ✗ int GetRunningEffects(char* msg, char** result) {
609 ✗ int length = strlen(msg) + 2; // 1 for LF, 1 for NULL termination
610 ✗ int i = 0;
611
612 ✗ std::unique_lock<std::mutex> lock(effectsLock);
613
614 ✗ for (i = 0; i < MAX_EFFECTS; i++) {
615 ✗ if (effects[i]) {
616 // delimiters
617 ✗ length += 2;
618
619 // ID
620 ✗ length++;
621 ✗ if (i > 9)
622 ✗ length++;
623 ✗ if (i > 99)
624 ✗ length++;
625
626 // Name
627 ✗ length += strlen(effects[i]->name.c_str());
628 }
629 }
630
631 ✗ *result = (char*)malloc(length);
632 ✗ char* cptr = *result;
633 ✗ *cptr = '\0';
634
635 ✗ strcat(cptr, msg);
636 ✗ cptr += strlen(msg);
637
638 ✗ for (i = 0; i < MAX_EFFECTS; i++) {
639 ✗ if (effects[i]) {
640 ✗ strcat(cptr, ";");
641 ✗ cptr++;
642
643 ✗ cptr += snprintf(cptr, 4, "%d", i);
644
645 ✗ strcat(cptr, ",");
646 ✗ cptr++;
647
648 ✗ strcat(cptr, effects[i]->name.c_str());
649 ✗ cptr += strlen(effects[i]->name.c_str());
650 }
651 }
652
653 ✗ strcat(cptr, "\n");
654 ✗ return strlen(*result);
655 ✗ }
656