GCC Code Coverage Report


Directory: ./
File: WLEDAudioSync.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 212 219 96.8%
Functions: 17 17 100.0%
Branches: 185 310 59.7%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2026 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 "Warnings.h" // WarningHolder -- needed directly for NOPCH builds
16
17 #include <arpa/inet.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <ifaddrs.h>
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <sys/socket.h>
24 #include <sys/time.h>
25 #include <unistd.h>
26
27 #include <algorithm>
28 #include <chrono>
29 #include <cmath>
30 #include <cstring>
31
32 #include "WLEDAudioSync.h"
33
34 #include "common.h"
35 #include "log.h"
36 #include "settings.h"
37
38 #include "overlays/wled/wled.h" // AudioFrame, fetchAudioSamples, computeAudioFrame
39 #include "SysSocket.h"
40
41 WLEDAudioSync WLEDAudioSync::INSTANCE;
42
43 namespace {
44
45 // WLED V2 audio-sync packet on the wire. 44 bytes, big-endian floats?
46 // No — WLED runs on little-endian ESP32/ESP8266 and emits the bytes
47 // raw, so we transmit native floats. All fields are documented in
48 // /opt/WLED/usermods/audioreactive/audio_reactive.cpp around line 794.
49 #pragma pack(push, 1)
50 struct AudioSyncPacketV2 {
51 char header[6]; // 6 "00002\0"
52 uint8_t reserved1[2]; // 2 unused, present for compiler padding parity
53 float sampleRaw; // 4
54 float sampleSmth; // 4
55 uint8_t samplePeak; // 1 0=no peak, >=1=beat
56 uint8_t reserved2; // 1
57 uint8_t fftResult[16]; // 16 16 freq bins, 0-255 each
58 uint16_t reserved3; // 2
59 float FFT_Magnitude; // 4
60 float FFT_MajorPeak; // 4
61 }; // 44 total
62 #pragma pack(pop)
63 static_assert(sizeof(AudioSyncPacketV2) == 44,
64 "WLED audiosync V2 packet must be exactly 44 bytes");
65
66 constexpr const char* WLED_HEADER_V2 = "00002";
67
68 3114 uint64_t nowMs() {
69 using namespace std::chrono;
70
1/2
✓ Branch 0 taken 3114 times.
✗ Branch 1 not taken.
3114 return duration_cast<milliseconds>(
71 6228 steady_clock::now().time_since_epoch()).count();
72 }
73
74 } // namespace
75
76
1/2
✓ Branch 0 taken 3252 times.
✗ Branch 1 not taken.
9756 WLEDAudioSync::WLEDAudioSync() = default;
77
78 600 WLEDAudioSync::~WLEDAudioSync() {
79 600 Cleanup();
80 600 }
81
82 2651 void WLEDAudioSync::Initialize() {
83 // Live-reload: any of the three settings can be edited at runtime
84 // (e.g. via the FPP settings UI) and we'll reconfigure without an
85 // fppd restart. The handler funnels into Reconfigure() which is
86 // mutex-protected against concurrent edits.
87 308 auto cb = [this](const std::string&) { Reconfigure(); };
88
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.
13255 registerSettingsListener("WLEDAudioSync", "WLEDAudioSyncMode", cb);
89
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.
12960 registerSettingsListener("WLEDAudioSync", "WLEDAudioSyncDest", cb);
90
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("WLEDAudioSync", "WLEDAudioSyncPort", cb);
91
92
1/2
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
2651 Reconfigure();
93 2651 }
94
95 3220 void WLEDAudioSync::Cleanup() {
96 // Idempotent: main() calls this during shutdown, and ~WLEDAudioSync() calls
97 // it again at static-destruction time. The second call must be a no-op --
98 // unregisterSettingsListener() reaches into the global SettingsConfig, which
99 // may already be destroyed by then (cross-TU static destruction order),
100 // and locking its destroyed mutex throws EINVAL out of the noexcept dtor.
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3220 times.
3220 if (m_cleanedUp.exchange(true)) {
102 ✗ return;
103 }
104
3/6
✓ Branch 0 taken 3220 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3220 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3220 times.
✗ Branch 5 not taken.
12880 unregisterSettingsListener("WLEDAudioSync", "WLEDAudioSyncMode");
105
3/6
✓ Branch 0 taken 3220 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3220 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3220 times.
✗ Branch 5 not taken.
12880 unregisterSettingsListener("WLEDAudioSync", "WLEDAudioSyncDest");
106
3/6
✓ Branch 0 taken 3220 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3220 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3220 times.
✗ Branch 5 not taken.
9660 unregisterSettingsListener("WLEDAudioSync", "WLEDAudioSyncPort");
107 // Under the lock Reconfigure() holds: a settings callback already in
108 // flight could otherwise be starting threads (and opening sockets) while
109 // this stops them. Reconfigure() sees m_cleanedUp and won't restart.
110
1/2
✓ Branch 0 taken 3220 times.
✗ Branch 1 not taken.
3220 std::unique_lock<std::mutex> lock(m_reconfigureLock);
111
1/2
✓ Branch 0 taken 3220 times.
✗ Branch 1 not taken.
3220 StopAllThreads();
112 3220 }
113
114 2664 void WLEDAudioSync::Reconfigure() {
115
1/2
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
2664 std::unique_lock<std::mutex> lock(m_reconfigureLock);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2664 times.
2664 if (m_cleanedUp) {
117 ✗ return; // shutting down; Cleanup() has stopped (or will stop) everything
118 }
119
120 2664 Mode newMode = Off;
121
1/2
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
2664 std::string modeStr = getSetting("WLEDAudioSyncMode");
122
9/14
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2655 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 2655 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2655 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2655 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2655 times.
✓ Branch 12 taken 9 times.
✓ Branch 13 taken 2655 times.
2664 if (modeStr == "send" || modeStr == "Send" || modeStr == "1") newMode = Send;
123
9/14
✓ Branch 0 taken 2655 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2638 times.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 2638 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2638 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2638 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2638 times.
✓ Branch 12 taken 17 times.
✓ Branch 13 taken 2638 times.
2655 else if (modeStr == "receive" || modeStr == "Receive" || modeStr == "2") newMode = Receive;
124
125 2664 int newPort = 11988;
126
1/2
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
2664 int p = getSettingInt("WLEDAudioSyncPort", 0);
127
2/4
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2664 times.
✗ Branch 3 not taken.
2664 if (p > 0 && p < 65536) newPort = p;
128
129
1/2
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
2664 std::string newDest = getSetting("WLEDAudioSyncDest");
130
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2664 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2664 if (newDest.empty()) newDest = "239.0.0.1";
131
132 // Tear down anything currently running before re-applying. Cheap
133 // and unambiguous; sockets reopen in <50 ms.
134
1/2
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
2664 StopAllThreads();
135
136 // Threads are stopped, so nothing can re-raise these — clear any stale
137 // warnings from a previous configuration. The (re)attempt below re-raises
138 // the relevant one if it still fails.
139
3/6
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2664 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2664 times.
✗ Branch 5 not taken.
10656 WarningHolder::RemoveWarning(46, "WLED Audio Sync: could not open the send socket");
140
3/6
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2664 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2664 times.
✗ Branch 5 not taken.
10656 WarningHolder::RemoveWarning(46, "WLED Audio Sync: could not open the receive socket");
141
3/6
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2664 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2664 times.
✗ Branch 5 not taken.
7992 WarningHolder::RemoveWarning(46, "WLED Audio Sync: invalid destination address");
142
143 // m_mode is what the effects read, so it only says Receive (or Send) once
144 // that socket is open: set early, a failed bind left a window in which
145 // InjectCachedFrame() answered for a receiver that never existed.
146 2664 m_mode = Off;
147 2664 m_port = newPort;
148
1/2
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
2664 m_destAddr = newDest;
149
150
2/2
✓ Branch 0 taken 2638 times.
✓ Branch 1 taken 26 times.
2664 if (newMode == Off) {
151
1/2
✓ Branch 0 taken 2638 times.
✗ Branch 1 not taken.
2638 LogInfo(VB_GENERAL, "WLEDAudioSync: disabled\n");
152 2638 return;
153 }
154
155
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 CacheLocalIPs();
156 26 m_running = true;
157
158
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 17 times.
26 if (newMode == Send) {
159
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
9 if (!OpenSendSocket()) {
160 ✗ LogErr(VB_GENERAL, "WLEDAudioSync: send socket open failed\n");
161 ✗ WarningHolder::AddWarning(46, "WLED Audio Sync: could not open the send socket");
162 ✗ m_running = false;
163 ✗ return;
164 }
165 9 m_mode = Send;
166
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
18 m_sendThread = new std::thread([this]() {
167
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 SetThreadName("FPP-WLEDAudio-S");
168 9 SendLoop();
169
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
18 });
170
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 LogInfo(VB_GENERAL, "WLEDAudioSync: SEND mode, %s:%d\n",
171 m_destAddr.c_str(), m_port);
172
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 } else if (newMode == Receive) {
173
3/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 16 times.
17 if (!OpenReceiveSocket()) {
174
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogErr(VB_GENERAL, "WLEDAudioSync: receive socket open failed\n");
175
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
3 WarningHolder::AddWarning(46, "WLED Audio Sync: could not open the receive socket");
176 1 m_running = false;
177 1 return;
178 }
179 16 m_mode = Receive;
180
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
32 m_recvThread = new std::thread([this]() {
181
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 SetThreadName("FPP-WLEDAudio-R");
182 16 ReceiveLoop();
183
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
32 });
184
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 LogInfo(VB_GENERAL, "WLEDAudioSync: RECEIVE mode, port %d\n", m_port);
185 }
186
6/6
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2639 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 2639 times.
✓ Branch 4 taken 25 times.
✓ Branch 5 taken 2639 times.
7942 }
187
188 5884 void WLEDAudioSync::StopAllThreads() {
189 5884 m_running = false;
190
191
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 5868 times.
5884 if (m_recvSocket >= 0) {
192 // shutdown() wakes the listener's blocking recvfrom() (same
193 // lesson as the WLED API responder — close alone doesn't).
194 16 ::shutdown(m_recvSocket, SHUT_RDWR);
195 }
196
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 5868 times.
5884 if (m_recvThread) {
197
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (m_recvThread->joinable()) m_recvThread->join();
198
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 delete m_recvThread;
199 16 m_recvThread = nullptr;
200 }
201 // Closed only once the listener has been joined: closed under it, the
202 // descriptor number could be reused by another socket that its next
203 // recvfrom() would then read from.
204
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 5868 times.
5884 if (m_recvSocket >= 0) {
205 16 ::close(m_recvSocket);
206 16 m_recvSocket = -1;
207 }
208 // A cached frame belongs to the listener that received it. Kept, it
209 // stayed live for kFrameTtlMs after the port moved or receive was
210 // switched off and on, and drew the old source's packets.
211 {
212
1/2
✓ Branch 0 taken 5884 times.
✗ Branch 1 not taken.
5884 std::unique_lock<std::mutex> lock(m_cacheLock);
213 5884 m_cacheTimestampMs = 0;
214 5884 }
215
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 5875 times.
5884 if (m_sendThread) {
216
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (m_sendThread->joinable()) m_sendThread->join();
217
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 delete m_sendThread;
218 9 m_sendThread = nullptr;
219 }
220
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 5875 times.
5884 if (m_sendSocket >= 0) {
221 9 ::close(m_sendSocket);
222 9 m_sendSocket = -1;
223 }
224 5884 }
225
226 9 bool WLEDAudioSync::OpenSendSocket() {
227 9 m_sendSocket = OpenSocket(AF_INET, SOCK_DGRAM, 0);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (m_sendSocket < 0) return false;
229
230 // Enable broadcast (allows 255.255.255.255 / x.x.x.255 destinations).
231 9 int yes = 1;
232 9 ::setsockopt(m_sendSocket, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes));
233
234 // Multicast TTL of 1 keeps multicast packets link-local; same
235 // semantics WLED uses on its own audiosync transmitter.
236 9 unsigned char ttl = 1;
237 9 ::setsockopt(m_sendSocket, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
238
239 9 return true;
240 }
241
242 17 bool WLEDAudioSync::OpenReceiveSocket() {
243 17 m_recvSocket = OpenSocket(AF_INET, SOCK_DGRAM, 0);
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (m_recvSocket < 0) return false;
245
246 17 int yes = 1;
247 17 ::setsockopt(m_recvSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
248 // SO_REUSEPORT intentionally NOT set: it triggers the kernel's
249 // load-balancing path even with a single binder, and on some
250 // kernels causes UDP packets to be silently dropped when the
251 // sender and receiver are both on loopback. SO_REUSEADDR alone
252 // gives us the bind-restart-without-TIME_WAIT semantics we want.
253
254 // Belt and braces for StopAllThreads(): not every platform wakes a
255 // recvfrom() on an unconnected UDP socket at shutdown(), so the
256 // listener also rechecks m_running at least this often.
257 17 struct timeval tv = { 0, 250000 };
258 17 ::setsockopt(m_recvSocket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
259
260 17 sockaddr_in addr{};
261 17 addr.sin_family = AF_INET;
262 17 addr.sin_addr.s_addr = htonl(INADDR_ANY);
263 17 addr.sin_port = htons(m_port);
264
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 16 times.
17 if (::bind(m_recvSocket, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) {
265
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 LogErr(VB_GENERAL, "WLEDAudioSync: bind(%d) failed: %s\n",
266 m_port, FPPstrerror(errno));
267
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ::close(m_recvSocket);
268 1 m_recvSocket = -1;
269 1 return false;
270 }
271
272 // If the destination address is multicast, join the group so
273 // multicast packets are delivered to us. Unicast/broadcast needs
274 // no extra setup beyond the bind above.
275 16 in_addr a{};
276 16 if (inet_pton(AF_INET, m_destAddr.c_str(), &a) == 1
277
5/6
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 2 times.
16 && IN_MULTICAST(ntohl(a.s_addr))) {
278 14 ip_mreq mreq{};
279 14 mreq.imr_multiaddr = a;
280 14 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
281 14 if (::setsockopt(m_recvSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP,
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 &mreq, sizeof(mreq)) < 0) {
283 ✗ LogWarn(VB_GENERAL, "WLEDAudioSync: IP_ADD_MEMBERSHIP %s failed: %s\n",
284 m_destAddr.c_str(), FPPstrerror(errno));
285 }
286 }
287 16 return true;
288 }
289
290 26 void WLEDAudioSync::CacheLocalIPs() {
291 26 m_localIPs.clear();
292 26 ifaddrs* ifa = nullptr;
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (getifaddrs(&ifa) != 0) return;
294
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 26 times.
156 for (auto* p = ifa; p; p = p->ifa_next) {
295
3/4
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✓ Branch 3 taken 52 times.
130 if (!p->ifa_addr || p->ifa_addr->sa_family != AF_INET) continue;
296 52 auto* in = reinterpret_cast<sockaddr_in*>(p->ifa_addr);
297
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 m_localIPs.insert(in->sin_addr.s_addr);
298 }
299 26 freeifaddrs(ifa);
300 }
301
302 3822 bool WLEDAudioSync::IsLocalAddress(uint32_t ipv4_n) const {
303 3822 return m_localIPs.count(ipv4_n) > 0;
304 }
305
306 // Standalone broadcast loop. Runs whenever WLEDAudioSync is in Send
307 // mode, independent of any local reactive effect — so FPP can be a
308 // pure audio source for a network of WLED reactive devices even with
309 // no overlay effects active locally. Cadence matches WLED's own
310 // audioreactive transmit rate (10ms / 100Hz).
311 9 void WLEDAudioSync::SendLoop() {
312 9 sockaddr_in dst{};
313 9 dst.sin_family = AF_INET;
314 9 dst.sin_port = htons(m_port);
315
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (inet_pton(AF_INET, m_destAddr.c_str(), &dst.sin_addr) != 1) {
316
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogErr(VB_GENERAL, "WLEDAudioSync: invalid destination '%s'\n",
317 m_destAddr.c_str());
318
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
6 WarningHolder::AddWarning(46, "WLED Audio Sync: invalid destination address");
319 2 return;
320 }
321
322 std::array<float, NUM_SAMPLES> samples;
323 7 AudioFrame frame{};
324
325
2/2
✓ Branch 0 taken 2881 times.
✓ Branch 1 taken 7 times.
2888 while (m_running) {
326 2881 int sampleRate = 0;
327
3/4
✓ Branch 0 taken 2881 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 405 times.
✓ Branch 3 taken 2476 times.
2881 if (!fetchAudioSamples(samples, sampleRate)) {
328 // No audio source producing right now (nothing playing,
329 // mic unconfigured, etc.). Wait a bit and re-check rather
330 // than spinning.
331
1/2
✓ Branch 0 taken 405 times.
✗ Branch 1 not taken.
405 std::this_thread::sleep_for(std::chrono::milliseconds(50));
332 405 continue;
333 }
334
335
1/2
✓ Branch 0 taken 2476 times.
✗ Branch 1 not taken.
2476 computeAudioFrame(samples, sampleRate, frame);
336
337 2476 AudioSyncPacketV2 pkt{};
338 2476 std::memcpy(pkt.header, WLED_HEADER_V2, 5); // header[6] keeps the trailing NUL
339 2476 pkt.sampleRaw = static_cast<float>(frame.volumeRaw);
340 2476 pkt.sampleSmth = frame.volumeSmth;
341 2476 pkt.samplePeak = frame.samplePeak;
342 2476 std::memcpy(pkt.fftResult, frame.fftResult, 16);
343 2476 pkt.FFT_Magnitude = frame.FFT_Magnitude;
344 2476 pkt.FFT_MajorPeak = frame.FFT_MajorPeak;
345
346
1/2
✓ Branch 0 taken 2476 times.
✗ Branch 1 not taken.
2476 ::sendto(m_sendSocket, &pkt, sizeof(pkt), 0,
347 reinterpret_cast<sockaddr*>(&dst), sizeof(dst));
348
349
1/2
✓ Branch 0 taken 2476 times.
✗ Branch 1 not taken.
2476 std::this_thread::sleep_for(std::chrono::milliseconds(10));
350 }
351 }
352
353 16 void WLEDAudioSync::ReceiveLoop() {
354 AudioSyncPacketV2 pkt;
355 uint8_t buf[sizeof(AudioSyncPacketV2) + 1]; // one spare byte: a longer datagram reads 45 and is dropped
356
357 16 sockaddr_in from{};
358 16 socklen_t fromLen = sizeof(from);
359 16 bool firstPacketLogged = false;
360
361
2/2
✓ Branch 0 taken 3948 times.
✓ Branch 1 taken 1 times.
3949 while (m_running) {
362 3948 fromLen = sizeof(from);
363
1/2
✓ Branch 0 taken 3948 times.
✗ Branch 1 not taken.
3948 ssize_t n = ::recvfrom(m_recvSocket, buf, sizeof(buf), 0,
364 reinterpret_cast<sockaddr*>(&from), &fromLen);
365
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 3822 times.
3948 if (n <= 0) {
366 // Socket closed by Cleanup(), or transient error — bail
367 // out cleanly when we're shutting down.
368
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 111 times.
126 if (!m_running) break;
369 1347 continue;
370 }
371
372 // Loopback suppression: ignore packets from ourselves. Without
373 // this, a node in `send` mode that hears its own multicast back
374 // via host routing would inject its own FFT data on top of the
375 // locally-computed FFT, oscillating.
376
3/4
✓ Branch 0 taken 3822 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 197 times.
✓ Branch 3 taken 3625 times.
3822 if (IsLocalAddress(from.sin_addr.s_addr)) continue;
377
378 // V1 ("00001", 88-byte) packets exist for backwards-compat with
379 // very old WLED firmware; we accept the V2 form only since that's
380 // what every current WLED build emits.
381
2/2
✓ Branch 0 taken 296 times.
✓ Branch 1 taken 3329 times.
3625 if (n != sizeof(AudioSyncPacketV2)) continue;
382 3329 std::memcpy(&pkt, buf, sizeof(pkt));
383
2/2
✓ Branch 0 taken 197 times.
✓ Branch 1 taken 3132 times.
3329 if (std::memcmp(pkt.header, WLED_HEADER_V2, 5) != 0) continue;
384
385 // The floats come from anyone on the LAN and go straight into the
386 // WLED effects: an Inf major peak never leaves Rocktaves'
387 // "while (f > 249) f /= 2" (holding the WLED lock), and NaN or huge
388 // values are undefined in the effects' integer casts. Drop
389 // non-finite packets and clamp the rest to what WLED itself sends.
390 // (Copied out first: the packet is packed, so its floats can't bind
391 // to std::clamp's references.)
392 3132 float sampleRaw = pkt.sampleRaw;
393 3132 float sampleSmth = pkt.sampleSmth;
394 3132 float fftMagnitude = pkt.FFT_Magnitude;
395 3132 float fftMajorPeak = pkt.FFT_MajorPeak;
396
2/2
✓ Branch 0 taken 2934 times.
✓ Branch 1 taken 99 times.
6165 if (!std::isfinite(sampleRaw) || !std::isfinite(sampleSmth) ||
397
8/8
✓ Branch 0 taken 3033 times.
✓ Branch 1 taken 99 times.
✓ Branch 2 taken 2835 times.
✓ Branch 3 taken 99 times.
✓ Branch 4 taken 249 times.
✓ Branch 5 taken 2586 times.
✓ Branch 6 taken 546 times.
✓ Branch 7 taken 2586 times.
6165 !std::isfinite(fftMagnitude) || !std::isfinite(fftMajorPeak)) {
398 546 continue;
399 }
400
1/2
✓ Branch 0 taken 2586 times.
✗ Branch 1 not taken.
2586 sampleRaw = std::clamp(sampleRaw, 0.0f, 65535.0f);
401
1/2
✓ Branch 0 taken 2586 times.
✗ Branch 1 not taken.
2586 sampleSmth = std::clamp(sampleSmth, 0.0f, 255.0f);
402
1/2
✓ Branch 0 taken 2586 times.
✗ Branch 1 not taken.
2586 fftMagnitude = std::clamp(fftMagnitude, 0.0f, 1.0e7f);
403
1/2
✓ Branch 0 taken 2586 times.
✗ Branch 1 not taken.
2586 fftMajorPeak = std::clamp(fftMajorPeak, 1.0f, 11025.0f); // FX.cpp MAX_FREQUENCY
404
405
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2574 times.
2586 if (!firstPacketLogged) {
406 12 char fromBuf[INET_ADDRSTRLEN] = {};
407 12 inet_ntop(AF_INET, &from.sin_addr, fromBuf, sizeof(fromBuf));
408 // Use raw fprintf rather than LogInfo: observed that the
409 // FPPLogger may swallow messages from a brand-new thread on
410 // its very first call.
411
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fprintf(stderr, "WLEDAudioSync: receiving audio sync from %s\n",
412 fromBuf);
413
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fflush(stderr);
414 12 firstPacketLogged = true;
415 }
416
417
1/2
✓ Branch 0 taken 2586 times.
✗ Branch 1 not taken.
2586 std::unique_lock<std::mutex> lock(m_cacheLock);
418
1/2
✓ Branch 0 taken 2586 times.
✗ Branch 1 not taken.
2586 m_cacheTimestampMs = nowMs();
419 2586 std::memcpy(m_cachedFFT.data(), pkt.fftResult, 16);
420 2586 m_cachedVolumeSmth = sampleSmth;
421 2586 m_cachedVolumeRaw = static_cast<uint16_t>(sampleRaw);
422 2586 m_cachedSamplePeak = pkt.samplePeak;
423 2586 m_cachedFFTMagnitude = fftMagnitude;
424 2586 m_cachedFFTMajorPeak = fftMajorPeak;
425 2586 }
426 16 }
427
428 1560 bool WLEDAudioSync::InjectCachedFrame(uint8_t fftResult[16],
429 float& volumeSmth,
430 uint16_t& volumeRaw,
431 uint8_t& samplePeak,
432 float& FFT_Magnitude,
433 float& FFT_MajorPeak) {
434
2/2
✓ Branch 0 taken 929 times.
✓ Branch 1 taken 631 times.
1560 if (m_mode != Receive) return false;
435
436
1/2
✓ Branch 0 taken 631 times.
✗ Branch 1 not taken.
631 std::unique_lock<std::mutex> lock(m_cacheLock);
437
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 528 times.
631 if (m_cacheTimestampMs == 0) return false;
438
3/4
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 526 times.
528 if (nowMs() - m_cacheTimestampMs > kFrameTtlMs) return false;
439
440 526 std::memcpy(fftResult, m_cachedFFT.data(), 16);
441 526 volumeSmth = m_cachedVolumeSmth;
442 526 volumeRaw = m_cachedVolumeRaw;
443 526 samplePeak = m_cachedSamplePeak;
444 526 FFT_Magnitude = m_cachedFFTMagnitude;
445 526 FFT_MajorPeak = m_cachedFFTMajorPeak;
446 526 return true;
447 631 }
448