GCC Code Coverage Report


Directory: ./
File: boot/FPPINIT_Audio.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 1332 1491 89.3%
Functions: 53 54 98.1%
Branches: 2628 5106 51.5%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2024 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 <algorithm>
14 #include "fpp-json.h"
15 #include <chrono>
16 #include <climits>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <dirent.h>
20 #include <map>
21 #include <regex>
22 #include <set>
23 #include <sstream>
24 #include <sys/stat.h>
25 #include <thread>
26 #include <unistd.h>
27 #include <vector>
28
29 #include "FPPINIT.h"
30
31 // Resolve ALSA card number to stable card ID (e.g., 3 -> "S3", 0 -> "ICUSBAUDIO7D")
32 // Reads /proc/asound/cards: " 3 [S3 ]: USB-Audio - ..."
33 // Falls back to the card number as string if not found.
34 // Read the USB product name for an ALSA card from sysfs (e.g. "USB Sound Device").
35 // Falls back to the ALSA card type name if sysfs is unavailable.
36 47 static std::string getAlsaCardProductName(int cardNum, const std::string& fallback) {
37
2/4
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
47 std::string devicePath = "/sys/class/sound/card" + std::to_string(cardNum) + "/device";
38 char resolved[PATH_MAX];
39
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 20 times.
47 if (realpath(devicePath.c_str(), resolved)) {
40 // The ALSA device sysfs node points to a USB interface.
41 // The USB product string is in the parent USB device node.
42
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 std::string parentDir = resolved;
43 27 auto pos = parentDir.rfind('/');
44
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (pos != std::string::npos) {
45
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 std::string productFile = parentDir.substr(0, pos) + "/product";
46
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 std::string product = GetFileContents(productFile);
47
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 TrimWhiteSpace(product);
48 // It is written into PipeWire's SPA-JSON config as a quoted
49 // string, and the device chose it: a '"' or '\\' would end the
50 // string early, and a config PipeWire can't parse brings audio
51 // down at every boot.
52
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
27 std::string safe;
53
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 27 times.
111 for (unsigned char ch : product) {
54
2/4
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 84 times.
84 if (ch == '"' || ch == '\\') {
55 ✗ safe += '\'';
56
2/4
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
84 } else if (ch >= 0x20 && ch != 0x7f) {
57
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 safe += (char)ch;
58 }
59 }
60
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 TrimWhiteSpace(safe);
61
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 23 times.
27 if (!safe.empty()) {
62 4 return safe;
63 }
64
4/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 4 times.
54 }
65
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 4 times.
27 }
66
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 return fallback;
67 47 }
68
69 // Written at boot by setupAudio(); see the comment where it is written.
70 static const char* const EFFECTIVE_AUDIO_OUTPUT_PATH = "/run/fppd/audio-output.json";
71
72 531 static std::string getAlsaCardId(int cardNum) {
73
2/4
✓ Branch 0 taken 531 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 531 times.
✗ Branch 3 not taken.
531 std::string cardsContent = GetFileContents("/proc/asound/cards");
74
2/2
✓ Branch 0 taken 437 times.
✓ Branch 1 taken 94 times.
531 if (!cardsContent.empty()) {
75
1/2
✓ Branch 0 taken 437 times.
✗ Branch 1 not taken.
437 std::istringstream iss(cardsContent);
76 437 std::string line;
77
3/6
✓ Branch 0 taken 833 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 833 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 833 times.
✗ Branch 5 not taken.
833 while (std::getline(iss, line)) {
78 // Match: " 3 [S3 ]: ..."
79 833 auto bracket = line.find('[');
80 833 auto closeBracket = line.find(']');
81
4/6
✓ Branch 0 taken 629 times.
✓ Branch 1 taken 204 times.
✓ Branch 2 taken 629 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 629 times.
✗ Branch 5 not taken.
833 if (bracket != std::string::npos && closeBracket != std::string::npos && closeBracket > bracket) {
82
1/2
✓ Branch 0 taken 629 times.
✗ Branch 1 not taken.
629 std::string numStr = line.substr(0, bracket);
83
1/2
✓ Branch 0 taken 629 times.
✗ Branch 1 not taken.
629 TrimWhiteSpace(numStr);
84 try {
85
1/2
✓ Branch 0 taken 629 times.
✗ Branch 1 not taken.
629 int num = std::stoi(numStr);
86
2/2
✓ Branch 0 taken 437 times.
✓ Branch 1 taken 192 times.
629 if (num == cardNum) {
87
1/4
✓ Branch 0 taken 437 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
437 std::string cardId = line.substr(bracket + 1, closeBracket - bracket - 1);
88
1/2
✓ Branch 0 taken 437 times.
✗ Branch 1 not taken.
437 TrimWhiteSpace(cardId);
89
1/2
✓ Branch 0 taken 437 times.
✗ Branch 1 not taken.
437 if (!cardId.empty()) {
90 437 return cardId;
91 }
92 ✗ }
93 ✗ } catch (...) {}
94
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 437 times.
629 }
95 }
96
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 437 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 437 times.
874 }
97 94 return std::to_string(cardNum);
98 531 }
99
100 // Inverse of getAlsaCardId: resolve a stable ALSA card ID (e.g. "S3",
101 // "bcm2835ALSA") to its current card number by scanning /proc/asound/cards.
102 // Returns -1 if no currently-present card matches that ID.
103 428 static int getAlsaCardNumForId(const std::string& cardId) {
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 428 times.
428 if (cardId.empty()) {
105 ✗ return -1;
106 }
107
3/6
✓ Branch 0 taken 428 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 428 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 428 times.
✗ Branch 5 not taken.
428 std::istringstream iss(GetFileContents("/proc/asound/cards"));
108 428 std::string line;
109
4/6
✓ Branch 0 taken 789 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 789 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 529 times.
✓ Branch 5 taken 260 times.
789 while (std::getline(iss, line)) {
110 529 auto bracket = line.find('[');
111 529 auto closeBracket = line.find(']');
112
4/6
✓ Branch 0 taken 237 times.
✓ Branch 1 taken 292 times.
✓ Branch 2 taken 237 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 237 times.
✗ Branch 5 not taken.
529 if (bracket != std::string::npos && closeBracket != std::string::npos && closeBracket > bracket) {
113
1/2
✓ Branch 0 taken 237 times.
✗ Branch 1 not taken.
237 std::string id = line.substr(bracket + 1, closeBracket - bracket - 1);
114
1/2
✓ Branch 0 taken 237 times.
✗ Branch 1 not taken.
237 TrimWhiteSpace(id);
115
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 69 times.
237 if (id == cardId) {
116
1/2
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
168 std::string numStr = line.substr(0, bracket);
117
1/2
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
168 TrimWhiteSpace(numStr);
118 try {
119
1/2
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
168 return std::stoi(numStr);
120 ✗ } catch (...) {}
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 }
122
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 168 times.
237 }
123 }
124 260 return -1;
125 428 }
126
127 // Number of registered cards that aren't the synthetic snd-dummy. Reads
128 // /proc/asound/cards, which is pure kernel state -- unlike "aplay -l", it needs
129 // no /dev/snd node and so is visible the instant the card registers.
130 153 static int countRealAlsaCards() {
131 153 int count = 0;
132
3/6
✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 153 times.
✗ Branch 5 not taken.
153 std::istringstream iss(GetFileContents("/proc/asound/cards"));
133 153 std::string line;
134
4/6
✓ Branch 0 taken 261 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 261 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✓ Branch 5 taken 153 times.
261 while (std::getline(iss, line)) {
135 108 auto bracket = line.find('[');
136 108 auto closeBracket = line.find(']');
137
4/6
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 99 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
108 if (bracket != std::string::npos && closeBracket != std::string::npos && closeBracket > bracket) {
138
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 std::string id = line.substr(bracket + 1, closeBracket - bracket - 1);
139
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 TrimWhiteSpace(id);
140
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 if (id != "Dummy") {
141 9 ++count;
142 }
143 9 }
144 }
145 153 return count;
146 153 }
147
148 // True if ALSA card <num> is a USB audio device. snd-usb-audio, and only it,
149 // gives each card a /proc/asound/cardN/usbid entry.
150 84 static bool alsaCardIsUsb(int num) {
151
9/22
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 84 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 84 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 81 times.
✓ Branch 10 taken 84 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 84 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 84 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
84 return num >= 0 && FileExists("/proc/asound/card" + std::to_string(num) + "/usbid");
152 }
153
154 // True if the device tree declares a sound card -- i.e. this board is *expected*
155 // to have audio, whether or not the driver has bound yet. Used to decide whether
156 // "no cards yet" means "still coming" or "there is genuinely no audio here", so
157 // only the ambiguous case ever waits. A board with no sound node (a BeagleBone
158 // with no audio cape) costs one failed opendir.
159 53 static bool deviceTreeDeclaresSoundCard() {
160 53 DIR* dp = opendir("/proc/device-tree");
161
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 6 times.
53 if (!dp) {
162 47 return false;
163 }
164 6 bool found = false;
165 6 struct dirent* ep = nullptr;
166
6/6
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 19 times.
✓ Branch 5 taken 6 times.
25 while (!found && (ep = readdir(dp)) != nullptr) {
167
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
31 std::string name = ep->d_name;
168 // the node is "sound" or, when addressed, "sound@<unit>"
169
6/8
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 13 times.
19 if (name == "sound" || name.starts_with("sound@")) {
170 // Some boards ship the node with status "disabled" (an audio cape
171 // overlay flips it to "okay"). A disabled node can never bind, so
172 // it must not count as "audio is expected". No status property
173 // means enabled per the DT spec.
174
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 std::string status = GetFileContents("/proc/device-tree/" + name + "/status");
175
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 TrimWhiteSpace(status);
176
3/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 1 times.
6 found = status.empty() || status.starts_with("ok");
177 6 }
178 19 }
179 6 closedir(dp);
180 6 return found;
181 }
182
183 // True if a USB audio device is plugged in, whether or not snd-usb-audio has
184 // bound to it yet. A USB DAC has no device-tree node, so it needs its own check:
185 // the device appears in sysfs at enumeration, then udev autoloads snd-usb-audio,
186 // then the card registers -- and setupAudio can land in that gap.
187 //
188 // Read from sysfs rather than lsusb: only "lsusb -v" reports interface class, and
189 // it costs ~200ms on a BeagleBone (it walks and wakes every device) against a
190 // handful of open() calls here.
191 //
192 // bInterfaceClass 01 is USB Audio. Subclass 03 is MIDIStreaming, which yields a
193 // card with no playback device, so it can never satisfy the probe below -- skip
194 // it and don't wait on a MIDI controller.
195 48 static bool usbAudioDevicePresent() {
196 48 DIR* dp = opendir("/sys/bus/usb/devices");
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (!dp) {
198 ✗ return false;
199 }
200 48 bool found = false;
201 48 struct dirent* ep = nullptr;
202
5/6
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 96 times.
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 96 times.
✓ Branch 5 taken 48 times.
144 while (!found && (ep = readdir(dp)) != nullptr) {
203
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 std::string dev = ep->d_name;
204
6/10
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 48 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 96 times.
✗ Branch 9 not taken.
96 if (dev == "." || dev == "..") {
205 96 continue;
206 }
207 ✗ std::string base = "/sys/bus/usb/devices/" + dev;
208 ✗ std::string cls = GetFileContents(base + "/bInterfaceClass");
209 ✗ TrimWhiteSpace(cls);
210 ✗ if (cls != "01") {
211 ✗ continue;
212 }
213 ✗ std::string sub = GetFileContents(base + "/bInterfaceSubClass");
214 ✗ TrimWhiteSpace(sub);
215 ✗ if (sub != "03") {
216 ✗ found = true;
217 }
218
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 96 times.
96 }
219 48 closedir(dp);
220 48 return found;
221 }
222
223 // An ASoC card cannot bind until *every* component it references is present --
224 // the codec, the CPU DAI (e.g. McASP) and the machine driver (simple-audio-card).
225 // A cape EEPROM only names the codec, and the rest are device-tree platform
226 // devices whose drivers udev autoloads from their modalias. On a slow single-core
227 // board udev's coldplug can land tens of seconds into boot, long after we would
228 // have given up and taken snd-dummy -- measured ~35s after the codec on a
229 // BeagleBone, with the card arriving 10s behind snd-dummy.
230 //
231 // So do udev's job for the audio devices, early: modprobe the modalias of any
232 // still-unbound platform device whose compatible looks audio-related. Driving it
233 // off the kernel's own modalias means no module or SoC name is hardcoded, so this
234 // works for any cape/codec/CPU-DAI combination.
235 5 static void loadPendingSoundDrivers() {
236
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 const std::string base = "/sys/bus/platform/devices";
237
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 DIR* dp = opendir(base.c_str());
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!dp) {
239 ✗ return;
240 }
241 5 struct dirent* ep = nullptr;
242
3/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 5 times.
41 while ((ep = readdir(dp)) != nullptr) {
243
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
56 std::string dev = ep->d_name;
244
8/10
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 26 times.
✓ Branch 8 taken 10 times.
✓ Branch 9 taken 26 times.
36 if (dev == "." || dev == "..") {
245 10 continue;
246 }
247
2/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
26 std::string path = base + "/" + dev;
248
4/6
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 12 times.
26 if (FileExists(path + "/driver")) {
249 14 continue; // a driver is already bound
250 }
251
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 std::string modalias = GetFileContents(path + "/modalias");
252
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 TrimWhiteSpace(modalias);
253 // the modalias embeds the DT compatible, so it is also what we match on.
254 // A quote would let the string escape the shell below; nothing the kernel
255 // emits contains one, but refuse rather than trust that.
256
3/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
12 if (modalias.empty() || modalias.find('\'') != std::string::npos) {
257 ✗ continue;
258 }
259
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 std::string lower = modalias;
260 12 std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
261
7/10
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 8 times.
12 if (!lower.contains("audio") && !lower.contains("sound") && !lower.contains("mcasp") && !lower.contains("i2s")) {
262 4 continue;
263 }
264
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 printf("FPP - Loading sound driver for %s (%s)\n", dev.c_str(), modalias.c_str());
265
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 exec("/sbin/modprobe '" + modalias + "' > /dev/null 2>&1");
266
8/8
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 18 times.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 28 times.
62 }
267
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 closedir(dp);
268
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 }
269
270 // Load a PipeWire audio-groups config (pipewire-audio-groups[-simple].json).
271 // The UI writes these, but fppinit reads them at boot with no one to restart
272 // it, so every field the readers below touch is checked for the type they read
273 // it as: a file that fails is reported unusable, which each caller already
274 // treats as a missing or stale config (regenerate it, or skip it), instead of
275 // throwing out of fppinit on a wrong-typed value.
276 326 static bool loadAudioGroupsConfig(const std::string& path, Json::Value& root) {
277
1/2
✓ Branch 0 taken 326 times.
✗ Branch 1 not taken.
326 root = Json::Value();
278
10/16
✓ Branch 0 taken 326 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 238 times.
✓ Branch 3 taken 88 times.
✓ Branch 4 taken 238 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 238 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 238 times.
✓ Branch 10 taken 238 times.
✓ Branch 11 taken 88 times.
✓ Branch 12 taken 88 times.
✓ Branch 13 taken 238 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
326 if (!FileExists(path) || !LoadJsonFromString(GetFileContents(path), root, JsonRoot::Object)) {
279 88 return false;
280 }
281 1632 auto fits = [](const Json::Value& o, const char* key, Json::ValueType t) {
282
3/4
✓ Branch 0 taken 1396 times.
✓ Branch 1 taken 236 times.
✓ Branch 2 taken 1396 times.
✗ Branch 3 not taken.
1632 return !o.isMember(key) || o[key].isConvertibleTo(t);
283 };
284
1/2
✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
238 const Json::Value& groups = root["groups"];
285
7/10
✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 238 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 238 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 236 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 236 times.
238 if (!groups.isNull() && !groups.isArray()) {
286 2 return false;
287 }
288
7/12
✓ Branch 0 taken 236 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 236 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 236 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 232 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 468 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 236 times.
✓ Branch 11 taken 232 times.
468 for (const auto& grp : groups) {
289
5/10
✓ Branch 0 taken 236 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 234 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 234 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 234 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 234 times.
✗ Branch 9 not taken.
470 if (!grp.isObject() || !fits(grp, "enabled", Json::booleanValue) || !fits(grp, "id", Json::intValue) ||
290
8/12
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 234 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 234 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 234 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 234 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 234 times.
470 !fits(grp, "volume", Json::intValue) || !fits(grp, "name", Json::stringValue)) {
291 4 return false;
292 }
293
1/2
✓ Branch 0 taken 234 times.
✗ Branch 1 not taken.
234 const Json::Value& members = grp["members"];
294
7/10
✓ Branch 0 taken 234 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 234 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 234 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 232 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 232 times.
234 if (!members.isNull() && !members.isArray()) {
295 2 return false;
296 }
297
7/12
✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 232 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 232 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 232 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 464 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 232 times.
✓ Branch 11 taken 232 times.
464 for (const auto& mbr : members) {
298
3/6
✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 232 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 232 times.
✗ Branch 5 not taken.
464 if (!mbr.isObject() || !fits(mbr, "cardId", Json::stringValue) ||
299
6/12
✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 232 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 232 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 232 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 232 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 232 times.
464 !fits(mbr, "nodeTarget", Json::stringValue) || !fits(mbr, "volume", Json::intValue)) {
300 ✗ return false;
301 }
302 }
303 }
304 232 return true;
305 }
306
307 // Returns true if every ALSA card referenced by the PipeWire audio-groups JSON
308 // is currently present (by stable card ID, as listed in /proc/asound/cards).
309 // Used to decide whether the cached PipeWire group config is still valid for the
310 // current hardware without forking the (expensive) regeneration PHP. A removed
311 // or swapped card -> some referenced cardId is absent -> returns false so the
312 // regen runs and strips/re-resolves it. The regen resolves by stable card ID
313 // (not card number), so a card that's still present but renumbered keeps a valid
314 // config -- which is why presence-by-ID is a sufficient check.
315 77 static bool pipewireConfigCardsPresent(const std::string& jsonPath) {
316
1/2
✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
77 Json::Value root;
317
3/4
✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 74 times.
77 if (!loadAudioGroupsConfig(jsonPath, root)) {
318 3 return false;
319 }
320 74 std::vector<std::string> present;
321
3/6
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 74 times.
✗ Branch 5 not taken.
74 std::istringstream iss(GetFileContents("/proc/asound/cards"));
322 74 std::string line;
323
4/6
✓ Branch 0 taken 288 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 288 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 214 times.
✓ Branch 5 taken 74 times.
288 while (std::getline(iss, line)) {
324 214 auto b = line.find('[');
325 214 auto e = line.find(']');
326
4/6
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 103 times.
✓ Branch 2 taken 111 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 111 times.
✗ Branch 5 not taken.
214 if (b != std::string::npos && e != std::string::npos && e > b) {
327
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 std::string id = line.substr(b + 1, e - b - 1);
328
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 TrimWhiteSpace(id);
329
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 if (!id.empty()) {
330
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 present.push_back(id);
331 }
332 111 }
333 }
334
8/14
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 74 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 74 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 73 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 147 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 74 times.
✓ Branch 13 taken 73 times.
147 for (const auto& grp : root["groups"]) {
335
8/14
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 74 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 74 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 73 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 147 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 74 times.
✓ Branch 13 taken 73 times.
147 for (const auto& mbr : grp["members"]) {
336
3/6
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 74 times.
✗ Branch 5 not taken.
74 std::string cid = mbr.get("cardId", "").asString();
337
6/8
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 73 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 73 times.
74 if (!cid.empty() && std::find(present.begin(), present.end(), cid) == present.end()) {
338 1 return false;
339 }
340
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 1 times.
74 }
341 }
342 73 return true;
343 77 }
344
345 // Normalise an ALSA card ID for use in a PipeWire node name: lowercase, with
346 // every non-alphanumeric (and non-underscore) character replaced by '_'. Must
347 // match the convention used by the 95-fpp-alsa-sink.conf generation above and
348 // by GeneratePipeWireGroupsConfig() in pipewire.php.
349 334 static std::string normalizeCardIdForNode(const std::string& cardId) {
350 334 std::string n = cardId;
351 334 std::transform(n.begin(), n.end(), n.begin(), ::tolower);
352
2/2
✓ Branch 0 taken 3349 times.
✓ Branch 1 taken 334 times.
3683 for (auto& ch : n) {
353
4/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 3306 times.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 8 times.
3349 if (!std::isalnum(static_cast<unsigned char>(ch)) && ch != '_') ch = '_';
354 }
355 334 return n;
356 }
357
358 // Returns the first output-group member whose cached nodeTarget is a WirePlumber
359 // name for a card FPP owns (ownedCids: normalised IDs of the cards the boot conf
360 // covers), or "" if there is none.
361 //
362 // Every such card has its WirePlumber device disabled by
363 // 50-fpp-suppress-alsa-dupes.conf, so that node never exists: the group conf
364 // built from it links its filter-chain to nothing and plays silently, with no
365 // error anywhere. It gets there when the card was hot-plugged (WirePlumber's
366 // node was the only one when the group was applied) or the group predates the
367 // suppression rule. The card-presence check cannot see it -- the card IS
368 // present -- so without this the cached conf is re-validated every boot.
369 67 static std::string pipewireConfigSuppressedTarget(const std::string& jsonPath,
370 const std::set<std::string>& ownedCids) {
371
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 Json::Value root;
372
2/4
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 67 times.
67 if (!loadAudioGroupsConfig(jsonPath, root)) {
373 ✗ return "";
374 }
375
8/14
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 67 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 67 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 65 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 132 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 67 times.
✓ Branch 13 taken 65 times.
132 for (const auto& grp : root["groups"]) {
376
8/14
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 67 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 67 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 65 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 132 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 67 times.
✓ Branch 13 taken 65 times.
132 for (const auto& mbr : grp["members"]) {
377
3/6
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 67 times.
✗ Branch 5 not taken.
67 std::string cid = mbr.get("cardId", "").asString();
378
3/6
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 67 times.
✗ Branch 5 not taken.
67 std::string target = mbr.get("nodeTarget", "").asString();
379
11/18
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 63 times.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 4 times.
✓ Branch 11 taken 63 times.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 65 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
144 if (!cid.empty() && !target.empty() && !startsWith(target, "fpp_alsa_") &&
380
7/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 65 times.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 63 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
69 ownedCids.count(normalizeCardIdForNode(cid))) {
381 2 return target;
382 }
383
4/4
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 65 times.
✓ Branch 3 taken 2 times.
69 }
384 }
385
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
130 return "";
386 67 }
387
388 // Known multi-channel I2S cards whose drivers advertise a continuous channel
389 // range (e.g. "CHANNELS: [2 8]") that the non-USB range heuristic below would
390 // clamp to stereo, and whose live /proc hw_params may show a stale 2-channel
391 // negotiation left over from a previous config. Returns the minimum channel
392 // count the card must be configured with, or 0 when no quirk applies. Matched
393 // against the card's full `aplay -l` line (issue #2620).
394 // Mirrored in DetectAlsaCardMaxChannels() in www/api/controllers/pipewire.php.
395 61 static int quirkMinChannelsForCard(const std::string& aplayLine) {
396 static const std::pair<const char*, int> quirks[] = {
397 { "hifiberry_dac8x", 8 }, // HiFiBerry DAC8x / Raspiaudio 8xOUT: 4x stereo DACs on one I2S bus
398 };
399
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 std::string lineLc = aplayLine;
400 61 std::transform(lineLc.begin(), lineLc.end(), lineLc.begin(), ::tolower);
401
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 58 times.
119 for (const auto& [needle, ch] : quirks) {
402
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 58 times.
61 if (lineLc.find(needle) != std::string::npos) {
403 3 return ch;
404 }
405 }
406 58 return 0;
407 61 }
408
409 // Return the single-group member cardId recorded in a Simple-mode groups JSON,
410 // or "" if the file is absent/empty/unparseable or has no member. Simple mode
411 // always has exactly one group with one member (the selected output card).
412 23 static std::string simpleConfigCardId(const std::string& jsonPath) {
413
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 Json::Value root;
414
2/6
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 23 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
23 if (!loadAudioGroupsConfig(jsonPath, root)) return "";
415
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 const Json::Value& groups = root["groups"];
416
5/12
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 23 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 23 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
23 if (!groups.isArray() || groups.empty()) return "";
417
2/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
23 const Json::Value& members = groups[0]["members"];
418
5/12
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 23 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 23 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
23 if (!members.isArray() || members.empty()) return "";
419
4/8
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 23 times.
✗ Branch 7 not taken.
23 return members[0].get("cardId", "").asString();
420 23 }
421
422 // Open the device in one PCM format and report the sample rate the driver
423 // actually settled on -- 0 if the format is unusable or the probe could not run.
424 //
425 // This has to be an open. `aplay --dump-hw-params` reports the unrefined
426 // capability space and ignores both -f and -r, so a fixed-bit-clock I2S card
427 // (TI McASP driving a PCM5102A on a BeagleBone cape) lists "FORMAT: S16_LE
428 // S32_LE" and "RATE: [11025 44100]" while in fact only S16_LE reaches 44100:
429 // the bit clock is the constant, not the rate, so 32 bits/frame x 44100 ==
430 // 64 bits/frame x 22050. Nothing in the advertised space shows that coupling.
431 //
432 // Feeding 8KB of zeros opens the device, negotiates, and exits in ~250ms. The
433 // payload is silence, so nothing audible is emitted.
434 //
435 // Two different failures have to be told apart, and neither can be read off the
436 // banner: aplay prints "Playing raw data ..." from header() *before*
437 // set_params() negotiates anything, so it appears even for a format the card
438 // then rejects. An unusable format exits non-zero ("Sample format non
439 // available"); a rate the driver refines to something else only warns
440 // ("rate is not accurate (requested = X, got = Y)") and still exits 0. So:
441 // judge usability by exit status, and take the achieved rate from the warning.
442 // Keep in sync with PipeWireProbeFormatRate() in www/api/controllers/pipewire.php.
443 static constexpr char kProbeRcTag[] = "fpp-audio-probe-rc:";
444
445 34 static int achievedRateForFormat(const std::string& alsaPath, const std::string& pwFmt,
446 int rate, int channels) {
447 34 std::string alsaFmt;
448
3/4
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 26 times.
34 if (pwFmt == "S32LE") {
449
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 alsaFmt = "S32_LE";
450
2/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
26 } else if (pwFmt == "S24_32LE") {
451 ✗ alsaFmt = "S24_LE";
452
2/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
26 } else if (pwFmt == "S24LE") {
453 ✗ alsaFmt = "S24_3LE";
454
2/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
26 } else if (pwFmt == "S16LE") {
455
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 alsaFmt = "S16_LE";
456 } else {
457 ✗ return 0;
458 }
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (rate <= 0) rate = 44100;
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (channels <= 0) channels = 2;
461 // $? after the pipeline is aplay's own status; echo it so the exit code
462 // survives execAndReturn(), which only hands back stdout.
463 34 std::string out = execAndReturn("head -c 8192 /dev/zero | timeout 2 /usr/bin/aplay -D " +
464
4/8
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 34 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 34 times.
✗ Branch 7 not taken.
88 alsaPath + " -t raw -f " + alsaFmt + " -r " +
465
3/6
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 34 times.
✗ Branch 5 not taken.
116 std::to_string(rate) + " -c " + std::to_string(channels) +
466
4/8
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 34 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 34 times.
✗ Branch 7 not taken.
34 " - 2>&1; echo \"" + kProbeRcTag + "$?\"");
467 // Unverifiable (device busy, probe timed out, aplay missing, shell never ran).
468
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 const size_t tagPos = out.rfind(kProbeRcTag);
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (tagPos == std::string::npos) {
470 ✗ return 0;
471 }
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (std::strtol(out.c_str() + tagPos + (sizeof(kProbeRcTag) - 1), nullptr, 10) != 0) {
473 ✗ return 0;
474 }
475 34 std::smatch m;
476
3/6
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 34 times.
34 if (std::regex_search(out, m, std::regex(R"(not accurate \(requested = \d+Hz, got = (\d+)Hz\))"))) {
477 try {
478 ✗ return std::stoi(m[1].str());
479 ✗ } catch (...) {
480 ✗ }
481 }
482 34 return rate; // negotiated exactly what was asked for
483 34 }
484
485 // Widest first, so the first match in a FORMAT line is the best one. ALSA spells
486 // these with an underscore before LE, PipeWire without.
487 static const std::pair<const char*, const char*> kPcmFormatNames[] = {
488 { "S32_LE", "S32LE" },
489 { "S24_LE", "S24_32LE" }, // 24-bit in a 32-bit container
490 { "S24_3LE", "S24LE" }, // packed 24-bit (3 bytes)
491 { "S16_LE", "S16LE" },
492 };
493
494 // ALSA's spelling of a single PCM format -> PipeWire's. Anything unrecognised
495 // (an exotic or big-endian format) uses the legacy S16LE fallback.
496 ✗ static std::string pipewireFormatName(const std::string& alsaFmt) {
497 ✗ for (const auto& [alsaName, pwName] : kPcmFormatNames) {
498 ✗ if (alsaFmt == alsaName) {
499 ✗ return pwName;
500 }
501 }
502 ✗ return "S16LE";
503 }
504
505 // Pick the widest PCM format the card advertises that costs no sample rate
506 // relative to S16LE when the card supports it.
507 //
508 // The question is NOT "does this format hold the rate we asked for". A card can
509 // be unable to deliver the requested rate in ANY format -- an AM62x PCM5102A cape
510 // clocks no lower than 88200, so a 44100 request is refined upward for S16_LE and
511 // S32_LE alike. Treating any refinement as a rejection there throws away S32 for
512 // a card that pays nothing to provide it. Meanwhile the AM335x cape refines
513 // S32_LE from 44100 down to 22050 while S16_LE holds 44100 exactly, and taking
514 // S32 there is what makes PipeWire fail adapter creation, abort context creation,
515 // exit, and get restarted by systemd every few seconds forever (issue #2811).
516 //
517 // Both are the same rule once the comparison is made against what the fallback
518 // actually achieves rather than against what we asked for: widen only when it is
519 // free. Measured on both capes; see the achievedRateForFormat() probe.
520 29 static std::string bestFormatForRate(const std::string& fmtLine, const std::string& alsaPath,
521 int rate, int channels) {
522 // Nothing wider is even advertised -- skip the baseline probe entirely.
523 29 bool anyWider = false;
524
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 29 times.
232 for (size_t i = 0; i + 1 < std::size(kPcmFormatNames); ++i) {
525 87 anyWider |= fmtLine.find(kPcmFormatNames[i].first) != std::string::npos;
526 }
527
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 4 times.
29 if (!anyWider) {
528
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
50 return "S16LE";
529 }
530 4 const bool hasS16 = fmtLine.find("S16_LE") != std::string::npos;
531 // The rate to beat when S16 is available. If that advertised baseline cannot
532 // be established (device busy, probe timed out), decline to widen: a
533 // needlessly narrow format costs only bit depth, a wrongly wide one costs all
534 // audio. Cards that do not advertise S16 must use one of their real formats.
535 const int baselineRate = hasS16
536
6/12
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
10 ? achievedRateForFormat(alsaPath, "S16LE", rate, channels)
537
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 : 0;
538
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
4 if (hasS16 && baselineRate <= 0) {
539 ✗ return "S16LE";
540 }
541 4 std::string advertisedFallback;
542 // Stop before the last entry: S16LE is the fallback, already probed above.
543
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 for (size_t i = 0; i + 1 < std::size(kPcmFormatNames); ++i) {
544 4 const auto& [alsaName, pwName] = kPcmFormatNames[i];
545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (fmtLine.find(alsaName) == std::string::npos) {
546 ✗ continue;
547 }
548
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 advertisedFallback = pwName;
549
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
6 const int achievedRate = achievedRateForFormat(alsaPath, pwName, rate, channels);
550
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
4 if ((!hasS16 && achievedRate > 0) ||
551
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 (hasS16 && achievedRate >= baselineRate)) {
552
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
10 return pwName;
553 }
554 }
555 ✗ return hasS16 ? "S16LE" : advertisedFallback;
556 4 }
557
558 // Version of the rules the ALSA probe below applies when it derives an adapter
559 // from a card (format, rate, channels, headroom, quirks).
560 //
561 // 95-fpp-alsa-sink.conf lives in /etc and so survives every upgrade, and the
562 // "adapters already match present cards" fast path re-validates only which cards
563 // it names -- not how it described them. Without a version, an install keeps a
564 // conf that FPP's own code would no longer generate, forever: fixing a probe rule
565 // only ever helps boxes flashed after the fix, never the ones already broken by
566 // it. That is not hypothetical. Generation 1 (untagged) could pick S32LE for a
567 // fixed-bit-clock I2S cape that only reaches S16LE at the target rate; PipeWire
568 // then failed adapter creation, aborted context creation and exited, and systemd
569 // restarted it every few seconds indefinitely -- thousands of restarts and no
570 // audio on a box that had already installed the fix (issue #2811).
571 //
572 // Bump this whenever the probe can produce a different conf for unchanged
573 // hardware. Costs one extra probe on the first boot after the upgrade.
574 static constexpr int kAlsaSinkConfGeneration = 8;
575
576 238 static std::string alsaSinkConfGenerationTag() {
577
2/4
✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 238 times.
✗ Branch 3 not taken.
238 return "# FPP ALSA sink adapters (generation " + std::to_string(kAlsaSinkConfGeneration) + ")";
578 }
579
580 // Marker recording the rate the AudioFormat setting asked for when the conf was
581 // written, so the boot-time cache check can tell that the setting has changed.
582 //
583 // It cannot read that back off default.clock.rate: since the graph clock started
584 // following the hardware, that field holds the rate the card *achieved*, which a
585 // fixed-bit-clock cape refines upward (an AM62x PCM5102A asks for 44100 and lands
586 // on 88200). Comparing it against the configured rate therefore mismatched on
587 // every boot for exactly the capes that refine, forcing a full re-probe each time
588 // -- several seconds of aplay --dump-hw-params -- only to regenerate a
589 // byte-identical file. Nor can audio.rate serve: the capture adapters emit a
590 // fixed 44100, so a plain search finds the wrong one on a card with a mic.
591 129 static std::string alsaSinkConfRateTag(int configuredRate) {
592
1/2
✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
129 return "# configured rate: " + std::to_string(configuredRate);
593 }
594
595 // Marker recording the AudioPeriodSize the conf was written with.
596 //
597 // Same job as alsaSinkConfRateTag(), for the same reason: perSize is consumed
598 // only where the adapters are written, so without a tag to compare, changing
599 // AudioPeriodSize left a conf that still declared the old api.alsa.period-size
600 // and every boot logged "already match present cards; skipping probe". The
601 // setting read as applied in the UI while the graph never saw it.
602 //
603 // It cannot be read back off api.alsa.period-size: the capture adapters do not
604 // carry one, and a plain search would find whichever sink adapter came first
605 // rather than the value this boot would write.
606 129 static std::string alsaSinkConfPeriodSizeTag(int perSize) {
607
1/2
✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
129 return "# period size: " + std::to_string(perSize);
608 }
609
610 // Marker recording the cards the probe *considered*, which is not the same as
611 // the cards it ended up writing an adapter for.
612 //
613 // The validity check used to compare the adapters in the conf against the
614 // present candidate cards, but the probe drops a candidate whenever the device
615 // turns out to be unusable -- an IEC958-only HDMI card, or one that will not
616 // open. Those cards are still present and still candidates on the next boot,
617 // so the two sets could never agree and the fast path was dead: a Pi with a
618 // connected display re-ran the whole ALSA probe, several seconds of aplay
619 // --dump-hw-params, on every single boot, and silently -- none of the three
620 // checks that print a reason was the one rejecting it.
621 //
622 // Comparing the candidate sets instead asks the question that actually matters:
623 // the hardware on offer is unchanged, so the probe would reach the same
624 // conclusions, skips included. A card that was merely busy at probe time gets
625 // its skip cached too, which the no-sinks recovery at the end of runAudioSetup()
626 // already exists to catch, and a probe-rule change is what the generation tag
627 // above is for.
628 238 static std::string alsaSinkConfCardsTag(const std::set<std::string>& cids) {
629
1/2
✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
238 std::string tag = "# cards:";
630
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 238 times.
404 for (const auto& c : cids) {
631
2/4
✓ Branch 0 taken 166 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 166 times.
✗ Branch 3 not taken.
166 tag += " " + c;
632 }
633 238 return tag;
634 ✗ }
635
636 // api.alsa.headroom for a USB card, which is decided by whether the card can end
637 // up *following* someone else's clock.
638 //
639 // A USB card's oscillator is its own, unrelated to whatever drives the PipeWire
640 // graph, so when several sinks share one driver graph the followers need slack
641 // for the adaptive resampler to absorb the drift in -- without it the resampler
642 // hard-resyncs, which is audible as crackle and pitch wobble. That is what
643 // kUsbHeadroomSharedGroup buys.
644 //
645 // A card that is the only sink in its group has nobody to follow: PipeWire makes
646 // it the graph driver (pw-top shows it as the one node with a non-zero quantum),
647 // so the graph clock *is* that card's oscillator and there is no drift to absorb.
648 // Every frame of headroom is then pure added output latency.
649 //
650 // Measured through fppd/GStreamer on a Pi 5 with a full-speed USB dongle at
651 // 44.1 kHz, reading the card's own queue depth from
652 // /proc/asound/cardN/pcm0p/sub0/status (the same figure GStreamerOut.cpp uses to
653 // correct reported position): 156 ms of delay at 4096, 109 ms at 2048, 86 ms at
654 // 1024 -- with zero xruns on the sink node through sustained playback under a
655 // fully loaded CPU at each value. Below 1024 PipeWire's own minimum buffer fill
656 // dominates and further cuts stop paying: 512 saves only 13 ms more than 1024,
657 // and 256 only 3 ms beyond that, for a steadily thinner scheduling margin. 1024
658 // is that knee, and it is what a non-drifting card actually needs.
659 static constexpr int kUsbHeadroomSoleSink = 1024;
660 static constexpr int kUsbHeadroomSharedGroup = 4096;
661 static constexpr int kHeadroomNonUsb = 256;
662
663 // How many members the largest audio group containing each card has, keyed by
664 // stable ALSA card ID -- i.e. how many sinks that card may have to share a
665 // driver graph with. Cards absent from the result are in no group at all: they
666 // still get an adapter, but nothing links to it, so they can only ever drive
667 // their own (idle) graph and count as sole sinks.
668 //
669 // Read straight from the groups JSON rather than from the live graph because
670 // this runs while 95-fpp-alsa-sink.conf is being written, before any of those
671 // nodes exist. The file persists across reboots, so the answer is available at
672 // the point the decision has to be made.
673 127 static std::map<std::string, int> pipewireGroupSizeByCard(const std::string& jsonPath) {
674 127 std::map<std::string, int> sizes;
675
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 Json::Value root;
676
7/10
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 91 times.
✓ Branch 4 taken 36 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 36 times.
✓ Branch 8 taken 91 times.
✓ Branch 9 taken 36 times.
127 if (!loadAudioGroupsConfig(jsonPath, root) || !root.isMember("groups")) {
677 91 return sizes;
678 }
679
8/14
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 36 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 36 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 72 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 36 times.
✓ Branch 13 taken 36 times.
72 for (const auto& grp : root["groups"]) {
680 // A disabled group is not built into the graph, so its members are not
681 // sharing anything and must not be charged the follower's headroom.
682
6/12
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 36 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 36 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 36 times.
36 if (grp.isMember("enabled") && !grp["enabled"].asBool()) {
683 ✗ continue;
684 }
685
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 const int count = static_cast<int>(grp["members"].size());
686
8/14
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 36 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 36 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 72 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 36 times.
✓ Branch 13 taken 36 times.
72 for (const auto& mbr : grp["members"]) {
687
3/6
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
✗ Branch 5 not taken.
36 const std::string cid = mbr.get("cardId", "").asString();
688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (cid.empty()) {
689 ✗ continue;
690 }
691
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 if (count > sizes[cid]) {
692
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 sizes[cid] = count;
693 }
694
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 }
695 }
696 36 return sizes;
697 127 }
698
699 // Marker recording which cards the conf charged the shared-group USB headroom.
700 //
701 // Group membership changes without the set of present cards changing -- adding a
702 // second card to a group in the UI is exactly that -- so the cards tag above
703 // cannot see it, and the fast path would keep serving a conf whose headroom no
704 // longer matches the graph. Recording the decision itself makes the check able
705 // to notice. Empty (the common single-card case) is a meaningful value, not a
706 // missing one, so the tag is always written.
707 238 static std::string alsaSinkConfUsbHeadroomTag(const std::set<std::string>& sharedCids) {
708
1/2
✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
238 std::string tag = "# usb shared-group cards:";
709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 238 times.
238 for (const auto& c : sharedCids) {
710 ✗ tag += " " + c;
711 }
712 238 return tag;
713 ✗ }
714
715 // Every context.objects entry PipeWire creates is mandatory by default: if one
716 // fails, the daemon aborts context creation and exits (234). systemd then
717 // restarts it, and because fpp-pipewire.service sets StartLimitIntervalSec=0 it
718 // never gives up -- so one un-openable object crash-loops the whole media stack
719 // forever, taking audio, video routing, AES67, Opus RTP and RTSP with it.
720 //
721 // That is not hypothetical for the objects below. They are backed by real
722 // hardware, which can be absent for reasons entirely outside FPP: a USB adapter
723 // unplugged between boots, a card another process already holds, or -- the case
724 // this was found on -- an HDMI monitor that is switched off, whose vc4 audio
725 // device is still registered in /proc/asound/cards but returns ENOTSUPP on open
726 // because the powered-down display supplies no ELD. A show controller's HDMI
727 // monitor being off is the normal state, not a fault.
728 //
729 // nofail makes such a failure skip just that object: the node is not created,
730 // the reason is still logged, and everything else in the graph comes up. The
731 // missing node is then visible on the Troubleshooting page rather than being
732 // inferred from a daemon that will not start.
733 static constexpr const char* kNoFailFlag = "flags = [ nofail ]";
734
735 // priority.session for the Simple-mode sink fppd plays to. KEEP IN SYNC with
736 // FPPD_SINK_SESSION_PRIORITY in www/api/controllers/pipewire.php.
737 static constexpr int kFppdSinkSessionPriority = 2000;
738
739 // Build the contents of 97-fpp-audio-groups.conf for the Simple-mode synthetic
740 // group: a single 2-channel "Default" group whose one member is the selected
741 // sound card. This reproduces, byte-for-byte for the simple case, what
742 // GeneratePipeWireGroupsConfig() in www/api/controllers/pipewire.php emits:
743 //
744 // * an optional context.objects ALSA adapter — only when the card has no
745 // boot-time fpp_alsa_* node from 95-fpp-alsa-sink.conf (the snd-dummy
746 // fallback, which is detected after that conf was written),
747 // * a per-member delay filter-chain (delay always present at 0s so the UI
748 // can adjust it live during calibration — see $hasDelay = true in the PHP),
749 // * a combine-stream group sink named fpp_group_default targeting the
750 // filter-chain's virtual sink.
751 //
752 // Generating this here avoids forking PHP (apply_pipewire_simple_config /
753 // regenerate_pipewire_groups) on first boot, which costs ~10-30s of
754 // interpreter + common.php/pipewire.php startup on a single-core SBC.
755 50 static std::string buildSimplePipeWireGroupsConf(int card, const std::string& cId,
756 bool cardHasBootAdapter, int perSize,
757 int pipewireSampleRate) {
758
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 const std::string cidNorm = normalizeCardIdForNode(cId);
759
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 const std::string nodeName = "fpp_alsa_" + cidNorm; // adapter (boot-time 95 conf or custom below)
760
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 const std::string fxNode = "fpp_fx_g1_" + cidNorm; // filter-chain virtual sink
761
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 const std::string fxOut = fxNode + "_out";
762
763
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 std::ostringstream c;
764
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 c << "# Auto-generated by FPP - PipeWire Audio Output Groups\n";
765
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 c << "# Do not edit manually - managed via FPP UI\n\n";
766
767 // No real sound card: the selection resolved to the synthetic snd-dummy.
768 // Building the normal graph here (hw:Dummy adapter -> filter-chain ->
769 // combine-stream) pins the dummy PCM open, and because the filter-chain's
770 // playback link is not passive the sink never reaches idle, so PipeWire
771 // cycles the graph at the quantum rate (~47 wakeups/s) forever -- measured
772 // at 4% of a core on a single-core K8-PB that can never produce sound.
773 //
774 // fppd still needs *a* sink: media playback drives sequence position, and
775 // with no sink at all pipewiresink fails with "no target node available"
776 // and returns instantly instead of playing in real time, which would break
777 // sequence timing on exactly these boards. (WirePlumber ships
778 // scripts/fallback-sink.lua for this, but it is not registered in any
779 // profile, so no auto_null is ever created.)
780 //
781 // A null-audio-sink satisfies both: it consumes in real time so timing is
782 // preserved, and it suspends when idle so the graph parks completely.
783 // It must be wrapped in the `adapter` factory (as WirePlumber's own
784 // fallback-sink does) -- created via spa-node-factory directly it gets no
785 // usable ports and linking fails the same way as having no sink at all.
786
3/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 42 times.
50 if (cId == "Dummy") {
787
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << "# No sound card detected - a null sink keeps media playback (and so\n";
788
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << "# sequence timing) working while letting the graph park when idle.\n";
789
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << "context.objects = [\n";
790
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << " { factory = adapter\n";
791
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << " args = {\n";
792
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << " factory.name = support.null-audio-sink\n";
793
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << " node.name = \"fpp_group_default\"\n";
794
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << " node.description = \"FPP Audio (no sound card)\"\n";
795
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << " media.class = \"Audio/Sink\"\n";
796
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 c << " audio.rate = " << pipewireSampleRate << "\n";
797
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << " audio.channels = 2\n";
798
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << " audio.position = [ FL FR ]\n";
799
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << " monitor.channel-volumes = true\n";
800
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << " }\n";
801
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 c << " " << kNoFailFlag << "\n";
802
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << " }\n";
803
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c << "]\n";
804
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 return c.str();
805 }
806
807
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 23 times.
42 if (!cardHasBootAdapter) {
808 // No boot-time adapter exists for this card (snd-dummy: the dummy is
809 // loaded after 95-fpp-alsa-sink.conf is generated, so it never gets a
810 // node there). Create the adapter inline so the filter-chain/combine
811 // playback has a real sink to target. Detect the best PCM format the
812 // device advertises, using S16LE only when no PCM format is available.
813 // Capture the advertised format list here, but defer choosing one until
814 // alsaPath is resolved below: the rate-holding probe has to open the
815 // device we will actually configure (hw: or sysdefault:), not a guess.
816
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 std::string fmt = "S16LE";
817 19 std::string fmtLine;
818
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
38 std::string hwParams = execAndReturn("timeout 2 /usr/bin/aplay -D hw:" + cId +
819
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
19 " --dump-hw-params /dev/zero 2>&1 | head -40");
820 19 std::smatch fmtMatch;
821
4/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 18 times.
19 if (std::regex_search(hwParams, fmtMatch, std::regex(R"(FORMAT[^:]*:\s+(.+))"))) {
822
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 fmtLine = fmtMatch[1].str();
823 }
824 // Some cards expose only IEC958_SUBFRAME_LE passthrough on raw hw: with
825 // no standard PCM format (e.g. the Pi Zero W2 / Pi 3 vc4-hdmi card under
826 // KMS). PipeWire's SPA ALSA plugin cannot open a passthrough-only hw:
827 // device, so no sink is created and the fpp_group_default combine-stream
828 // ends up with no target, making GStreamer fail with "Failed to connect".
829 // sysdefault: routes through ALSA's dmix/plug layer (which downconverts
830 // IEC958 to normal PCM), so use it whenever hw: lacks a PCM format but
831 // sysdefault: provides one.
832
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 std::string alsaPath = "hw:" + cId;
833
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
38 bool hasPcmFmt = hwParams.find("S16_LE") != std::string::npos ||
834
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
38 hwParams.find("S24_LE") != std::string::npos ||
835 19 hwParams.find("S32_LE") != std::string::npos;
836
5/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 18 times.
19 if (!hasPcmFmt && hwParams.find("IEC958_SUBFRAME_LE") != std::string::npos) {
837
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 std::string sysParams = execAndReturn("timeout 2 /usr/bin/aplay -D sysdefault:" + cId +
838
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 " --dump-hw-params /dev/zero 2>&1 | head -40");
839
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
1 if (sysParams.find("S16_LE") != std::string::npos ||
840
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 sysParams.find("S24_LE") != std::string::npos ||
841 ✗ sysParams.find("S32_LE") != std::string::npos) {
842
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 alsaPath = "sysdefault:" + cId;
843 }
844 1 }
845 // Now that the target device is known, pick the widest advertised format
846 // that can actually hold the configured rate on it.
847
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18 times.
19 if (!fmtLine.empty()) {
848
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 fmt = bestFormatForRate(fmtLine, alsaPath, pipewireSampleRate, 2);
849 }
850
4/8
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 19 times.
✗ Branch 7 not taken.
19 const std::string desc = getAlsaCardProductName(card, cId) + " (" + cId + ")";
851
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 c << "# Custom FPP ALSA adapter nodes\n";
852
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 c << "# These provide sinks for cards with no WirePlumber node or needing extra channels\n";
853
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 c << "context.objects = [\n";
854
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 c << " { factory = adapter\n";
855
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 c << " args = {\n";
856
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 c << " factory.name = api.alsa.pcm.sink\n";
857
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 c << " node.name = \"" << nodeName << "\"\n";
858
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 c << " node.description = \"" << desc << "\"\n";
859
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 c << " media.class = \"Audio/Sink\"\n";
860
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 c << " api.alsa.path = \"" << alsaPath << "\"\n";
861
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 c << " api.alsa.period-size = " << perSize << "\n";
862
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 c << " api.alsa.headroom = 256\n";
863
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 c << " audio.format = \"" << fmt << "\"\n";
864
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 c << " audio.rate = " << pipewireSampleRate << "\n";
865
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 c << " audio.channels = 2\n";
866
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 c << " audio.position = [ FL FR ]\n";
867
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 c << " }\n";
868
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 c << " " << kNoFailFlag << "\n";
869
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 c << " }\n";
870
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 c << "]\n\n";
871 19 }
872
873 // Passive links let the graph reach idle so PipeWire can suspend the card.
874 // A non-passive playback link keeps the ALSA sink running forever, so the
875 // whole chain (combine -> filter-chain -> sink, plus any resampling) is
876 // recomputed every quantum on a box that is playing nothing -- measured at
877 // 4-5% of a core on single-core boards. Marking these links passive means
878 // only a real stream starts the graph. Settable so it can be turned off if
879 // a card misbehaves coming out of suspend.
880
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 const bool passiveSinks = getRawSettingInt("PipeWirePassiveSinks", 1) != 0;
881
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << "context.modules = [\n";
882 // Delay filter-chain (0s) — always emitted, matching the PHP, so the
883 // running graph has the fpp_fx_g1_* nodes the UI adjusts during calibration.
884
3/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
42 c << " # Filter chain (Delay) for: " << cId << " (Group 1)\n";
885
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " { name = libpipewire-module-filter-chain\n";
886
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " args = {\n";
887
3/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
42 c << " node.description = \"Delay: " << cId << "\"\n";
888
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " filter.graph = {\n";
889
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " nodes = [\n";
890
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " { type = builtin label = delay name = delay_l config = { \"max-delay\" = 5 } control = { \"Delay (s)\" = 0 } }\n";
891
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " { type = builtin label = delay name = delay_r config = { \"max-delay\" = 5 } control = { \"Delay (s)\" = 0 } }\n";
892
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " ]\n";
893
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " links = [\n";
894
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " ]\n";
895
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " inputs = [ \"delay_l:In\" \"delay_r:In\" ]\n";
896
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " outputs = [ \"delay_l:Out\" \"delay_r:Out\" ]\n";
897
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " }\n";
898
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " capture.props = {\n";
899
3/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
42 c << " node.name = \"" << fxNode << "\"\n";
900
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " media.class = Audio/Sink\n";
901
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " audio.channels = 2\n";
902
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " audio.position = [ FL FR ]\n";
903
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " }\n";
904
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " playback.props = {\n";
905
3/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
42 c << " node.name = \"" << fxOut << "\"\n";
906
3/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
42 c << " node.target = \"" << nodeName << "\"\n";
907
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (passiveSinks) {
908
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " node.passive = true\n";
909 }
910
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " stream.dont-remix = true\n";
911
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " audio.channels = 2\n";
912
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " audio.position = [ FL FR ]\n";
913
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " }\n";
914
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " }\n";
915
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " }\n";
916 // combine-stream group sink (the node fppd targets via PipeWireSinkName).
917
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " { name = libpipewire-module-combine-stream\n";
918
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " args = {\n";
919
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " combine.mode = sink\n";
920
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " node.name = \"fpp_group_default\"\n";
921
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " node.description = \"Default\"\n";
922
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " combine.latency-compensate = false\n";
923
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " combine.props = {\n";
924
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " audio.position = [ FL FR ]\n";
925 // Outranks every other sink (WirePlumber's own ALSA nodes reach ~1000;
926 // FPP's adapters carry none), so WirePlumber picks fppd's sink as the
927 // default itself when it has no stored choice -- see
928 // syncPipeWireDefaultSink(). A stored choice still wins (20000+).
929
3/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
42 c << " priority.session = " << kFppdSinkSessionPriority << "\n";
930
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " }\n";
931
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " stream.props = {\n";
932
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (passiveSinks) {
933
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " node.passive = true\n";
934 }
935
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " stream.dont-remix = true\n";
936
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " }\n";
937
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " stream.rules = [\n";
938
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " { matches = [\n";
939
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " { media.class = \"Audio/Sink\"\n";
940
3/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
42 c << " node.name = \"" << fxNode << "\"\n";
941
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " }\n";
942
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " ]\n";
943
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " actions = {\n";
944
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " create-stream = {\n";
945
3/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
42 c << " node.target = \"" << fxNode << "\"\n";
946
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " combine.audio.position = [ FL FR ]\n";
947
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " audio.position = [ FL FR ]\n";
948
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " }\n";
949
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " }\n";
950
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " }\n";
951
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " ]\n";
952
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " }\n";
953
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << " }\n";
954
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 c << "]\n";
955
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 return c.str();
956 50 }
957
958 // Ensure the WirePlumber "block combine fallback" hook is installed. It is a
959 // static, hardware-independent script shipped in the repo under /opt/fpp/etc;
960 // copy it into WirePlumber's search paths if absent. Without it, combine-stream
961 // output nodes can get rogue links to the default sink (doubled audio). A fresh
962 // image does not yet have it in /etc + /usr/share, so the Simple-mode C++ path
963 // installs it here rather than forking the PHP just to bootstrap it.
964 // Returns true if the hook is present (or was just installed from the shipped
965 // copy), false only if the shipped source is missing (a broken checkout — the
966 // files are committed to the repo, so this normally can't happen) so the caller
967 // can fall back to the PHP apply path for the rest of the work.
968 57 static bool ensureWirePlumberLinkingHook() {
969
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
114 const std::string luaSrc = "/opt/fpp/etc/wireplumber/scripts/linking/fpp-block-combine-fallback.lua";
970
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
114 const std::string luaDst = "/usr/share/wireplumber/scripts/linking/fpp-block-combine-fallback.lua";
971
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
114 const std::string confSrc = "/opt/fpp/etc/wireplumber/wireplumber.conf.d/60-fpp-block-combine-fallback.conf";
972
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57 const std::string confDst = "/etc/wireplumber/wireplumber.conf.d/60-fpp-block-combine-fallback.conf";
973
5/10
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 57 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 57 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 57 times.
57 if (!FileExists(luaSrc) || !FileExists(confSrc)) {
974 ✗ return false; // shipped copies not present (pre-upgrade install) — let PHP handle it
975 }
976
3/4
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 26 times.
57 if (!FileExists(luaDst)) {
977
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
31 exec("/bin/mkdir -p /usr/share/wireplumber/scripts/linking");
978
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 CopyFileContents(luaSrc, luaDst);
979 31 chmod(luaDst.c_str(), 0644); // world-readable to match the PHP installer (WirePlumber may run non-root)
980 }
981
3/4
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 26 times.
57 if (!FileExists(confDst)) {
982
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
31 exec("/bin/mkdir -p /etc/wireplumber/wireplumber.conf.d");
983
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 CopyFileContents(confSrc, confDst);
984 31 chmod(confDst.c_str(), 0644);
985 }
986 57 return true;
987 57 }
988
989 // Bounded wait for FPP's sinks to exist in the graph. pactl against a graph that
990 // is up but has not finished loading its modules silently sets nothing, so the
991 // volume restore needs the nodes to be there first. Replaces a flat sleep on the
992 // paths that skip the regeneration: normally returns on the first poll, and it
993 // cannot outlast maxSeconds if the graph never comes up.
994 //
995 // Returns false on timeout, which is also the audio stack's failure signal: see
996 // the recovery block at the end of runAudioSetup().
997 150 static bool waitForPipeWireSinks(int maxSeconds) {
998 const std::string cmd = "PIPEWIRE_RUNTIME_DIR=/run/pipewire-fpp XDG_RUNTIME_DIR=/run/pipewire-fpp "
999 "PULSE_RUNTIME_PATH=/run/pipewire-fpp/pulse "
1000
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 "/usr/bin/pactl list short sinks 2>/dev/null";
1001
2/2
✓ Branch 0 taken 2167 times.
✓ Branch 1 taken 23 times.
2190 for (int i = 0; i < maxSeconds * 4; i++) {
1002
3/4
✓ Branch 0 taken 2167 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✓ Branch 3 taken 2040 times.
2167 if (execAndReturn(cmd).find("fpp_") != std::string::npos) {
1003 127 return true;
1004 }
1005
1/2
✓ Branch 0 taken 2040 times.
✗ Branch 1 not taken.
2040 std::this_thread::sleep_for(std::chrono::milliseconds(250));
1006 }
1007 23 return false;
1008 150 }
1009
1010 15 static void setPipeWireSinkMute(const std::string& node, bool mute) {
1011
3/6
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
15 if (node.empty() || node.find('\'') != std::string::npos) {
1012 ✗ return;
1013 }
1014
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 exec("PIPEWIRE_RUNTIME_DIR=/run/pipewire-fpp XDG_RUNTIME_DIR=/run/pipewire-fpp "
1015 "PULSE_RUNTIME_PATH=/run/pipewire-fpp/pulse "
1016 15 "/usr/bin/pactl set-sink-mute '" +
1017
7/10
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 10 times.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 15 times.
✗ Branch 9 not taken.
45 node + "' " + (mute ? "1" : "0") + " 2>/dev/null");
1018 }
1019
1020 53 static void setPipeWireSinkVolume(const std::string& node, int volPct) {
1021 // Node names are generated by normalizeCardIdForNode() (alphanumerics and
1022 // underscore only), but nodeTarget comes straight out of the user's JSON --
1023 // refuse anything that could escape the single quotes rather than handing it
1024 // to a shell.
1025
3/6
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 53 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 53 times.
53 if (node.empty() || node.find('\'') != std::string::npos) {
1026 ✗ return;
1027 }
1028
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (volPct < 0) volPct = 0;
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (volPct > 200) volPct = 200;
1030
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 exec("PIPEWIRE_RUNTIME_DIR=/run/pipewire-fpp XDG_RUNTIME_DIR=/run/pipewire-fpp "
1031 "PULSE_RUNTIME_PATH=/run/pipewire-fpp/pulse "
1032 53 "/usr/bin/pactl set-sink-volume '" +
1033
4/8
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 53 times.
✗ Branch 7 not taken.
159 node + "' " + std::to_string(volPct) + "% 2>/dev/null");
1034 }
1035
1036 // Make the sink fppd plays to (PipeWireSinkName) the graph's default sink, so a
1037 // client that names no target -- a plugin's player, anything speaking plain
1038 // PulseAudio -- takes the same route as show audio (issue #3063). The UI's
1039 // applies already do this, but a boot never did: what WirePlumber remembered
1040 // from the last apply stood, and with nothing remembered (a first boot, whose
1041 // Simple config is generated here in C++) every FPP sink ties on priority and
1042 // the first adapter wins -- card 0, whichever card AudioOutput selected.
1043 //
1044 // pactl rather than the metadata directly: pipewire-pulse refuses a sink that
1045 // is not in the graph, so a name that never came up leaves the old default
1046 // rather than pointing everything at nothing.
1047 //
1048 // It has to wait for WirePlumber. The "default" metadata belongs to it, and
1049 // pipewire and wireplumber start together: a set made as WirePlumber starts
1050 // (the same second, measured on a Pi 3B+) is overwritten by its own pick and
1051 // never reaches its state file, so the default fell back to card 0 within
1052 // minutes of the log saying it was set. WirePlumber publishing
1053 // default.audio.sink is the sign its default-nodes logic is running. A set
1054 // made after that is stored (wireplumber/default-nodes) and restored by every
1055 // later restart of the stack, so it holds without being re-run.
1056 //
1057 // KEEP IN SYNC with the pactl set-default-sink calls in
1058 // www/api/controllers/pipewire.php, which choose the same sink.
1059 1859 static std::string pipeWireDefaultMetadata(const std::string& key) {
1060 return execAndReturn("PIPEWIRE_RUNTIME_DIR=/run/pipewire-fpp XDG_RUNTIME_DIR=/run/pipewire-fpp "
1061
3/6
✓ Branch 0 taken 1859 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1859 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1859 times.
✗ Branch 5 not taken.
1859 "timeout 2 /usr/bin/pw-metadata -n default 0 " + key + " 2>/dev/null");
1062 }
1063
1064 59 static void syncPipeWireDefaultSink() {
1065 59 std::string target;
1066
2/4
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 59 times.
✗ Branch 3 not taken.
59 getRawSetting("PipeWireSinkName", target);
1067
1/2
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
59 TrimWhiteSpace(target);
1068 // Node names FPP generates are slugged to [a-z0-9_]; refuse anything that
1069 // could escape the shell's quotes.
1070
5/6
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 49 times.
59 if (target.empty() || target.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-") != std::string::npos) {
1071 10 return;
1072 }
1073 const std::string env = "PIPEWIRE_RUNTIME_DIR=/run/pipewire-fpp XDG_RUNTIME_DIR=/run/pipewire-fpp "
1074
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 "PULSE_RUNTIME_PATH=/run/pipewire-fpp/pulse ";
1075
1076 49 int waited = 0;
1077
11/22
✓ Branch 0 taken 1809 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1809 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1804 times.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 1760 times.
✓ Branch 7 taken 44 times.
✓ Branch 8 taken 1809 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1809 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 1809 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 1760 times.
✓ Branch 15 taken 49 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.
5427 while (pipeWireDefaultMetadata("default.audio.sink").find("value:") == std::string::npos && waited < 40) {
1078
1/2
✓ Branch 0 taken 1760 times.
✗ Branch 1 not taken.
1760 std::this_thread::sleep_for(std::chrono::milliseconds(250));
1079 1760 ++waited;
1080 }
1081
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 5 times.
49 if (waited >= 40) {
1082
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 printf("FPP - WirePlumber published no default sink within 10s; setting %s anyway\n", target.c_str());
1083 }
1084
1085 // What WirePlumber stores, as well as what is in effect: an effective
1086 // default that matches only by WirePlumber's own pick is not stored, and
1087 // the next restart can pick differently.
1088
2/4
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
49 const std::string storedValue = "{\"name\":\"" + target + "\"}";
1089 50 auto isStored = [&]() {
1090
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
100 return pipeWireDefaultMetadata("default.configured.audio.sink").find(storedValue) != std::string::npos;
1091 49 };
1092 60 auto effective = [&]() {
1093
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
60 std::string s = execAndReturn(env + "/usr/bin/pactl get-default-sink 2>/dev/null");
1094
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 TrimWhiteSpace(s);
1095 60 return s;
1096 ✗ };
1097
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 std::string current = effective();
1098
6/8
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 48 times.
49 if (current == target && isStored()) {
1099 1 return;
1100 }
1101 // "<id>\t<name>\t..." -- match the whole name, not a prefix of a longer one.
1102
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
48 const std::string sinks = execAndReturn(env + "/usr/bin/pactl list short sinks 2>/dev/null");
1103
4/6
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 37 times.
✓ Branch 5 taken 11 times.
48 if (sinks.find("\t" + target + "\t") == std::string::npos) {
1104
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 printf("FPP - PipeWire default sink left as '%s': %s is not in the graph\n", current.c_str(), target.c_str());
1105 37 return;
1106 }
1107
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 printf("FPP - PipeWire default sink: %s (was '%s')\n", target.c_str(), current.c_str());
1108 // Re-checked rather than trusted: WirePlumber can still be settling its
1109 // first pick when the metadata appears.
1110
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 9 times.
58 for (int attempt = 0; attempt < 5; attempt++) {
1111
4/8
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 49 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 49 times.
✗ Branch 7 not taken.
49 exec(env + "/usr/bin/pactl set-default-sink '" + target + "' 2>/dev/null");
1112
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 std::this_thread::sleep_for(std::chrono::milliseconds(500));
1113
9/14
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 47 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 47 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 47 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
49 if (isStored() && effective() == target) {
1114 2 return;
1115 }
1116 }
1117
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 printf("FPP - PipeWire default sink did not hold at %s (now '%s')\n", target.c_str(), effective().c_str());
1118
10/10
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 40 times.
✓ Branch 6 taken 9 times.
✓ Branch 7 taken 40 times.
✓ Branch 8 taken 9 times.
✓ Branch 9 taken 50 times.
218 }
1119
1120 // A playback stream's level, as a linear gain set on the running node with
1121 // pw-cli -- the stage pactl and the UI's faders drive (channelVolumes). The
1122 // generated config's channelmix.volume is not applied by PipeWire, and a
1123 // stream node is no sink for pactl set-sink-volume.
1124 // KEEP IN SYNC with pw_set_node_volume_linear() in pipewire.php.
1125 8 static void setPipeWireNodeVolumeLinear(int nodeId, int volPct, int channels) {
1126 8 volPct = std::max(0, std::min(100, volPct));
1127 8 std::string vals;
1128
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 8 times.
28 for (int i = 0; i < std::max(1, channels); i++) {
1129 char v[16];
1130
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 8 times.
20 snprintf(v, sizeof(v), "%s%.4g", i ? ", " : "", volPct / 100.0);
1131
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 vals += v;
1132 }
1133
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 exec("PIPEWIRE_RUNTIME_DIR=/run/pipewire-fpp XDG_RUNTIME_DIR=/run/pipewire-fpp "
1134 8 "/usr/bin/pw-cli set-param " +
1135
4/8
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
24 std::to_string(nodeId) + " Props '{ channelVolumes: [ " + vals + " ] }' >/dev/null 2>&1");
1136 8 }
1137
1138 // A node in the running graph, as the input group levels below look for it.
1139 struct PipeWireStreamNode {
1140 int id = 0;
1141 int channels = 2;
1142 std::string name;
1143 std::string target;
1144 };
1145
1146 // Every node in the running graph, from one pw-dump: its name, what it was
1147 // told to connect to, and how many channels its volume has (from the
1148 // channelVolumes it reports; stereo if it reports none).
1149 // KEEP IN SYNC with pw_node_channel_count() in pipewire.php.
1150 13 static std::vector<PipeWireStreamNode> pipeWireStreamNodes() {
1151 13 std::vector<PipeWireStreamNode> nodes;
1152
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
26 const std::string out = execAndReturn("PIPEWIRE_RUNTIME_DIR=/run/pipewire-fpp XDG_RUNTIME_DIR=/run/pipewire-fpp "
1153
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 "/usr/bin/pw-dump 2>/dev/null");
1154
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Json::Value dump;
1155
6/8
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 11 times.
✓ Branch 7 taken 2 times.
13 if (out.find('[') == std::string::npos || !LoadJsonFromString(out, dump, JsonRoot::Array)) {
1156 11 return nodes;
1157 }
1158 46 auto str = [](const Json::Value& o, const char* key) {
1159
3/4
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 4 times.
46 return o.isObject() && o[key].isString() ? o[key].asString() : std::string();
1160 };
1161
7/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 16 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 18 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 16 times.
✓ Branch 11 taken 2 times.
18 for (const auto& obj : dump) {
1162
13/24
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 14 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 14 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 14 times.
✓ Branch 18 taken 16 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 2 times.
✓ Branch 21 taken 14 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
16 if (str(obj, "type") != "PipeWire:Interface:Node" || !obj["id"].isIntegral() || !obj["info"].isObject()) {
1163 2 continue;
1164 }
1165
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 const Json::Value& info = obj["info"];
1166 14 PipeWireStreamNode n;
1167
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 n.id = obj["id"].asInt();
1168
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 n.name = str(info["props"], "node.name");
1169
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 n.target = str(info["props"], "node.target");
1170
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 if (n.target.empty()) {
1171
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 n.target = str(info["props"], "target.object");
1172 }
1173
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 const Json::Value& params = info["params"];
1174
6/12
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 14 times.
✗ Branch 11 not taken.
14 if (params.isObject() && params["Props"].isArray()) {
1175
6/14
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 14 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 14 times.
✗ Branch 13 not taken.
14 for (const auto& p : params["Props"]) {
1176
9/18
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 14 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 14 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 14 times.
✗ Branch 17 not taken.
14 if (p.isObject() && p["channelVolumes"].isArray() && !p["channelVolumes"].empty()) {
1177
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 n.channels = p["channelVolumes"].size();
1178 14 break;
1179 }
1180 }
1181 }
1182
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 nodes.push_back(n);
1183 14 }
1184 2 return nodes;
1185 13 }
1186
1187 // The levels inside the input groups: each member's send into its bus (the
1188 // playback half, output.<name>, of its loopback) and each routing path from a
1189 // bus to an output group (the bus's combine-stream output to that group).
1190 // They are rebuilt at full level on every start like the buses. Every saved
1191 // level is set, 100% included: WirePlumber restores a stream's last live level
1192 // when it comes back, which need not be the saved one. A muted member or path
1193 // has no node -- the generated config leaves it out.
1194 //
1195 // KEEP IN SYNC with RestoreInputGroupSendAndPathLevels() in
1196 // www/api/controllers/pipewire.php, and the node names with
1197 // GeneratePipeWireInputGroupsConfig().
1198 13 static void restoreInputGroupSendAndPathLevels(const Json::Value& inputGroups) {
1199 13 std::map<int, std::string> outputNodes;
1200
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Json::Value og;
1201
3/6
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
13 if (loadAudioGroupsConfig(FPP_MEDIA_DIR + "/config/pipewire-audio-groups.json", og)) {
1202
8/14
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 26 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 13 times.
✓ Branch 13 taken 13 times.
26 for (const auto& grp : og["groups"]) {
1203
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 if (grp.isMember("id")) {
1204
8/16
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 13 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 13 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 13 times.
✗ Branch 15 not taken.
13 outputNodes[grp["id"].asInt()] = "fpp_group_" + normalizeCardIdForNode(grp.get("name", "Group").asString());
1205 }
1206 }
1207 }
1208 // A level as the PHP's intval() reads it; anything that is not a number is 100.
1209 8 auto level = [](const Json::Value& o) {
1210 8 const Json::Value& v = o["volume"];
1211
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 return v.isNumeric() ? (int)std::max(0.0, std::min(100.0, v.asDouble())) : 100;
1212 };
1213 8 auto muted = [](const Json::Value& o) {
1214
3/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6 times.
8 return o["mute"].isConvertibleTo(Json::booleanValue) && o["mute"].asBool();
1215 };
1216
1217 13 std::vector<PipeWireStreamNode> nodes;
1218 13 bool dumped = false;
1219
7/12
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 17 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 15 times.
✓ Branch 11 taken 2 times.
17 for (const auto& ig : inputGroups) {
1220
9/18
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 15 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 15 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 15 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 15 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 15 times.
30 if (!ig.isObject() || !ig["enabled"].isConvertibleTo(Json::booleanValue) || !ig["enabled"].asBool() ||
1221
3/6
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
15 !ig["id"].isIntegral()) {
1222 ✗ continue;
1223 }
1224
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 2 times.
15 if (!dumped) {
1225
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 nodes = pipeWireStreamNodes();
1226 13 dumped = true;
1227
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 2 times.
13 if (nodes.empty()) {
1228 11 return;
1229 }
1230 }
1231
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 const int groupId = ig["id"].asInt();
1232
1233
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 if (ig["members"].isArray()) {
1234
4/6
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 4 times.
10 for (Json::ArrayIndex mi = 0; mi < ig["members"].size(); mi++) {
1235
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 const Json::Value& mbr = ig["members"][mi];
1236
7/10
✓ 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.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 4 times.
6 if (!mbr.isObject() || muted(mbr)) {
1237 2 continue;
1238 }
1239
5/12
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
12 std::string slug = mbr["type"].isString() && mbr["type"].asString() == "fppd_stream"
1240
6/14
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
12 ? (mbr["sourceId"].isString() ? mbr["sourceId"].asString() : "fppd_stream_1")
1241
4/24
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 4 times.
✗ Branch 20 not taken.
✓ Branch 21 taken 4 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
12 : (mbr["name"].isString() ? mbr["name"].asString() : "Member " + std::to_string(mi));
1242
4/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 const std::string loopback = "fpp_loopback_ig" + std::to_string(groupId) + "_" + normalizeCardIdForNode(slug);
1243 4 std::vector<const PipeWireStreamNode*> found, fallback;
1244
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 4 times.
32 for (const auto& n : nodes) {
1245
3/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 24 times.
28 if (n.name == "output." + loopback) {
1246
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 found.push_back(&n);
1247
7/12
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 22 times.
✓ Branch 6 taken 24 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 22 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
24 } else if (n.name == loopback || n.name == "input." + loopback) {
1248
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 fallback.push_back(&n);
1249 }
1250 }
1251
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
8 for (const auto* n : found.empty() ? fallback : found) {
1252
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 setPipeWireNodeVolumeLinear(n->id, level(mbr), n->channels);
1253 }
1254 4 }
1255 }
1256
1257 // Every path the bus has (its outputs), at its saved level or the
1258 // generator's default of 100% when the routing matrix has none.
1259
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 if (ig["outputs"].isArray()) {
1260
4/12
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
4 const Json::Value& eq = ig["effects"].isObject() ? ig["effects"]["eq"] : Json::Value::nullSingleton();
1261
1/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
4 const bool hasEffects = eq.isObject() && eq["enabled"].isConvertibleTo(Json::booleanValue) &&
1262
1/20
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
4 eq["enabled"].asBool() && eq["bands"].isArray() && !eq["bands"].empty();
1263 ✗ const std::string route = hasEffects ? "fpp_route_ig_" + std::to_string(groupId)
1264
12/34
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 4 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 4 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 4 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 4 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 4 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✓ Branch 25 taken 4 times.
✗ Branch 26 not taken.
✓ Branch 27 taken 4 times.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
4 : "fpp_input_" + normalizeCardIdForNode(ig["name"].isString() ? ig["name"].asString() : "");
1265
8/14
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 4 times.
8 for (const auto& out : ig["outputs"]) {
1266
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (!out.isIntegral()) {
1267 ✗ continue;
1268 }
1269
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 const std::string key = std::to_string(out.asInt());
1270
6/12
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
6 const Json::Value& path = ig["routing"].isObject() && ig["routing"][key].isObject()
1271
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
6 ? ig["routing"][key]
1272
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 : Json::Value::nullSingleton();
1273
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 auto target = outputNodes.find(out.asInt());
1274
7/12
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 4 times.
4 if ((path.isObject() && muted(path)) || target == outputNodes.end()) {
1275 ✗ continue;
1276 }
1277
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 4 times.
32 for (const auto& n : nodes) {
1278
5/10
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 26 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
80 const bool ours = n.name == route || startsWith(n.name, route + ".") ||
1279
11/18
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 22 times.
✓ Branch 8 taken 26 times.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 26 times.
✓ Branch 11 taken 2 times.
✓ Branch 12 taken 26 times.
✓ Branch 13 taken 2 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
54 startsWith(n.name, "output." + route + "_");
1280
6/6
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 24 times.
28 if (ours && n.target == target->second) {
1281
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 setPipeWireNodeVolumeLinear(n.id, level(path), n.channels);
1282 }
1283 }
1284
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 }
1285 4 }
1286 }
1287
6/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 11 times.
35 }
1288
1289 // Reapply the saved mix bus levels. A mix bus is an input group's own
1290 // combine-stream sink (fpp_input_<name>) -- the output of the mix, a stage of
1291 // its own between the sources feeding it and the routing paths leaving it.
1292 // PipeWire rebuilds those sinks at full volume on every start, so without this
1293 // a bus fader lasts only until the next reboot. The members' and routing
1294 // paths' levels follow (restoreInputGroupSendAndPathLevels()).
1295 //
1296 // Advanced mode only: Simple mode has no input groups, and no file.
1297 //
1298 // KEEP IN SYNC with RestorePipeWireInputGroupVolumes() in
1299 // www/api/controllers/pipewire.php.
1300 19 static void restorePipeWireInputGroupVolumes() {
1301
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 const std::string inputGroupsJsonPath = FPP_MEDIA_DIR + "/config/pipewire-input-groups.json";
1302
3/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 13 times.
19 if (!FileExists(inputGroupsJsonPath)) {
1303 6 return;
1304 }
1305
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Json::Value root;
1306
7/16
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 13 times.
✓ Branch 10 taken 13 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 13 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
13 if (!LoadJsonFromString(GetFileContents(inputGroupsJsonPath), root) || !root.isMember("inputGroups")) {
1307 ✗ return;
1308 }
1309
8/14
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 15 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 28 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 15 times.
✓ Branch 13 taken 13 times.
28 for (const auto& ig : root["inputGroups"]) {
1310 // Read at boot: an entry with a field of the wrong JSON type is skipped,
1311 // not thrown on (see loadAudioGroupsConfig()).
1312 60 auto fits = [&ig](const char* key, Json::ValueType t) {
1313
3/4
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
60 return !ig.isMember(key) || ig[key].isConvertibleTo(t);
1314 15 };
1315
5/10
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 15 times.
✗ Branch 9 not taken.
30 if (!ig.isObject() || !fits("enabled", Json::booleanValue) || !fits("name", Json::stringValue) ||
1316
6/12
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 15 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 15 times.
30 !fits("volume", Json::intValue) || !fits("mute", Json::booleanValue)) {
1317 ✗ continue;
1318 }
1319
4/8
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 15 times.
15 if (!ig.get("enabled", false).asBool()) {
1320 ✗ continue;
1321 }
1322
5/10
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 15 times.
✗ Branch 9 not taken.
15 std::string busNode = "fpp_input_" + normalizeCardIdForNode(ig.get("name", "Input Group").asString());
1323
4/8
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
15 setPipeWireSinkVolume(busNode, ig.get("volume", 100).asInt());
1324
4/8
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
15 setPipeWireSinkMute(busNode, ig.get("mute", false).asBool());
1325 15 }
1326
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 restoreInputGroupSendAndPathLevels(root["inputGroups"]);
1327
3/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 6 times.
19 }
1328
1329 // Reapply the user's configured group/member volumes after the PipeWire stack has
1330 // been (re)started -- WirePlumber restores its own persisted state on every start,
1331 // which is not necessarily what the FPP UI has configured.
1332 //
1333 // This is a direct C++ port of RestorePipeWireGroupVolumes() in
1334 // www/api/controllers/pipewire.php, which remains the implementation used by the
1335 // UI's interactive applies (where the interpreter is already warm). Forking it at
1336 // boot cost php + common.php + the API controller plus sudo'd pactl calls --
1337 // measured 6s uncontended and 12s under boot load on a single-core BBB -- to emit
1338 // a handful of pactl calls, and all of it landed on the critical path to fppd, and
1339 // therefore to the moment the web UI is actually usable. fppinit already runs as
1340 // root, so the sudo hops go away too.
1341 //
1342 // KEEP IN SYNC with the PHP. The node-naming convention is shared with
1343 // GeneratePipeWireGroupsConfig(); normalizeCardIdForNode() already mirrors its
1344 // slugging rule, and this reuses it rather than re-deriving it.
1345 19 static void restorePipeWireVolumes() {
1346 // Read the config that describes the graph actually running. Simple mode's
1347 // groups live in pipewire-audio-groups-simple.json and its single group is
1348 // named "Default" (node fpp_group_default, what buildSimplePipeWireGroupsConf
1349 // emits); the advanced file is a different set of groups under different
1350 // names. Reading the advanced one unconditionally -- which is what this did,
1351 // and what the PHP still did -- meant a Simple-mode box with a leftover
1352 // advanced config pactl'd node names that do not exist, so every restore was
1353 // a silent no-op and any volume stored for the running group was ignored.
1354
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
38 std::string mediaBackend = "pipewire-simple";
1355
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
19 getRawSetting("MediaBackend", mediaBackend);
1356 19 std::transform(mediaBackend.begin(), mediaBackend.end(), mediaBackend.begin(), [](unsigned char c) {
1357 215 return std::tolower(c);
1358 });
1359
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
38 const std::string groupsJsonPath = FPP_MEDIA_DIR + "/config/" +
1360
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 (mediaBackend == "pipewire-simple"
1361 ? "pipewire-audio-groups-simple.json"
1362
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
22 : "pipewire-audio-groups.json");
1363
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 Json::Value root;
1364
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 const bool haveGroups = loadAudioGroupsConfig(groupsJsonPath, root)
1365
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 && root.isMember("groups");
1366
1367 // The filter-chain/combine-stream nodes appear a moment after the daemon
1368 // itself, and pactl against a half-built graph silently sets nothing.
1369
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
19 if (!waitForPipeWireSinks(30)) {
1370 ✗ printf("FPP - Timed out waiting for PipeWire sinks; restoring volumes anyway\n");
1371 }
1372
1373 // A missing or unreadable groups file is not a reason to skip the mix
1374 // buses: they live in their own file and their sinks come up just as loud.
1375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!haveGroups) {
1376 ✗ restorePipeWireInputGroupVolumes();
1377 ✗ return;
1378 }
1379
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 printf("FPP - Restoring PipeWire audio group volumes...\n");
1380
8/14
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 19 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 19 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 38 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 19 times.
✓ Branch 13 taken 19 times.
38 for (const auto& grp : root["groups"]) {
1381
12/28
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 19 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 19 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 19 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 19 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 19 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 19 times.
✓ Branch 18 taken 19 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 19 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 19 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
19 if (!grp.get("enabled", false).asBool() || !grp.isMember("members") || grp["members"].empty()) {
1382 ✗ continue;
1383 }
1384
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 int groupId = grp.get("id", 0).asInt();
1385
7/14
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 19 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 19 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 19 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 19 times.
✗ Branch 13 not taken.
51 setPipeWireSinkVolume("fpp_group_" + normalizeCardIdForNode(grp.get("name", "Group").asString()),
1386
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
38 grp.get("volume", 100).asInt());
1387
8/14
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 19 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 19 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 38 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 19 times.
✓ Branch 13 taken 19 times.
38 for (const auto& mbr : grp["members"]) {
1388
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 std::string cardId = mbr.get("cardId", "").asString();
1389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (cardId.empty()) {
1390 ✗ continue;
1391 }
1392
6/12
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 19 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 19 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 19 times.
✗ Branch 11 not taken.
51 setPipeWireSinkVolume("fpp_fx_g" + std::to_string(groupId) + "_" + normalizeCardIdForNode(cardId),
1393
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
38 mbr.get("volume", 100).asInt());
1394 // A member targeting a WirePlumber-managed node (e.g. an HDMI output)
1395 // must have that node pinned to 100%: WirePlumber initialises them at
1396 // ~40%, which would silently attenuate everything the filter chain
1397 // delivers. FPP-owned nodes are already created at full volume.
1398
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 std::string nodeTarget = mbr.get("nodeTarget", "").asString();
1399
12/32
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 19 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 19 times.
✓ Branch 18 taken 8 times.
✓ Branch 19 taken 11 times.
✓ Branch 20 taken 8 times.
✓ Branch 21 taken 11 times.
✗ Branch 22 not taken.
✓ Branch 23 taken 19 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
35 if (!nodeTarget.empty() && !startsWith(nodeTarget, "fpp_") && !startsWith(nodeTarget, "aes67_")) {
1400 ✗ setPipeWireSinkVolume(nodeTarget, 100);
1401 }
1402
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 }
1403 }
1404
1405
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 restorePipeWireInputGroupVolumes();
1406
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 }
1407
1408 // WirePlumber's ALSA monitor enumerates every card independently of the static
1409 // fpp_alsa_* adapters PipeWire loads from 95-fpp-alsa-sink.conf, so a card FPP
1410 // already owns ALSO gets a monitor-created node (alsa_output.<sysfs>.<profile>).
1411 // Both want the same PCM, and the monitor's node -- backed by a real device
1412 // object, unlike a bare context.objects adapter -- wins the default-sink
1413 // election. Every untargeted stream then lands on a node whose snd_pcm_open()
1414 // returns EBUSY: the node goes to error and the stream neither starts nor ends,
1415 // so the client blocks in poll() forever. That is not a hypothetical -- it is
1416 // how the flite IP announcement wedged a Type=oneshot boot service and held
1417 // multi-user.target unreachable indefinitely.
1418 //
1419 // FPP declares both sink and source adapters for every present card, so the
1420 // monitor has nothing left to contribute for these: disable its device outright.
1421 // Keyed on the ALSA card short-name because api.alsa.card.name is populated by
1422 // the time the monitor evaluates monitor.alsa.rules, whereas alsa.id /
1423 // alsa.card_name are attached to the device only afterwards -- a rule keyed on
1424 // those silently never matches (verified on hardware).
1425 //
1426 // Returns true if the file changed. WirePlumber reads rules only at startup,
1427 // so the caller must restart it for a change to take effect.
1428 127 static bool ensureWirePlumberAlsaDupeSuppression(const std::set<std::string>& cardNames) {
1429
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 const std::string confPath = "/etc/wireplumber/wireplumber.conf.d/50-fpp-suppress-alsa-dupes.conf";
1430
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 65 times.
127 if (cardNames.empty()) {
1431 // Nothing FPP owns statically (no usable card, or the PipeWire backend is
1432 // off): drop any stale rule rather than leaving cards suppressed with no
1433 // adapter to replace them.
1434
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
62 if (!FileExists(confPath)) {
1435 62 return false;
1436 }
1437 ✗ unlink(confPath.c_str());
1438 ✗ return true;
1439 }
1440
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 std::ostringstream out;
1441 out << "# Auto-generated by FPP - do not edit manually\n"
1442 << "# Suppresses WirePlumber's duplicate ALSA device for every card that already\n"
1443 << "# has a static fpp_alsa_* adapter in 95-fpp-alsa-sink.conf. Two nodes on one\n"
1444 << "# PCM means the loser fails to open with EBUSY and hangs its clients forever.\n"
1445
5/10
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 65 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 65 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 65 times.
✗ Branch 9 not taken.
65 << "monitor.alsa.rules = [\n";
1446
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 65 times.
162 for (const auto& name : cardNames) {
1447 out << " {\n"
1448 << " matches = [ { api.alsa.card.name = \"" << name << "\" } ]\n"
1449 << " actions = { update-props = { device.disabled = true } }\n"
1450
6/12
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 97 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 97 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 97 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 97 times.
✗ Branch 11 not taken.
97 << " }\n";
1451 }
1452
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 out << "]\n";
1453
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 const std::string conf = out.str();
1454
10/14
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 27 times.
✓ Branch 9 taken 38 times.
✓ Branch 10 taken 25 times.
✓ Branch 11 taken 40 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
65 if (FileExists(confPath) && GetFileContents(confPath) == conf) {
1455 25 return false;
1456 }
1457
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
40 exec("/bin/mkdir -p /etc/wireplumber/wireplumber.conf.d");
1458
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 PutFileContents(confPath, conf);
1459 40 chmod(confPath.c_str(), 0644);
1460 40 return true;
1461 127 }
1462
1463 // First-boot fast path for Simple PipeWire mode: synthesise the same audio
1464 // config that apply_pipewire_simple_config (PHP) would, but entirely in C++ so
1465 // no PHP interpreter is forked. Writes the simple groups JSON, the
1466 // 97-fpp-audio-groups.conf (both /etc dest and the media-dir cache so it
1467 // survives reboot), removes the input-groups conf (simple mode has none), and
1468 // sets PipeWireSinkName. The caller restarts PipeWire once afterwards.
1469 50 static void generateSimplePipeWireAudioConfig(int card, const std::string& cId,
1470 bool cardHasBootAdapter, int perSize,
1471 int pipewireSampleRate) {
1472
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 const std::string simpleGroupsJsonPath = FPP_MEDIA_DIR + "/config/pipewire-audio-groups-simple.json";
1473
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 const std::string videoSimpleJsonPath = FPP_MEDIA_DIR + "/config/pipewire-video-groups-simple.json";
1474
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 const std::string groupsConfCache = FPP_MEDIA_DIR + "/config/pipewire-audio-groups.conf";
1475
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 const std::string groupsConfDest = "/etc/pipewire/pipewire.conf.d/97-fpp-audio-groups.conf";
1476
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 const std::string igConfCache = FPP_MEDIA_DIR + "/config/pipewire-input-groups.conf";
1477
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 const std::string igConfDest = "/etc/pipewire/pipewire.conf.d/96-fpp-input-groups.conf";
1478
1479
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 printf("FPP - pipewire-simple: generating audio config in C++ (card %s)\n", cId.c_str());
1480
1481 // Simple groups JSON — mirrors BuildSimpleAudioGroupsData() in pipewire.php.
1482
3/6
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
✗ Branch 5 not taken.
50 Json::Value root, group, member;
1483
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 group["id"] = 1;
1484
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 group["name"] = "Default";
1485
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 group["enabled"] = true;
1486
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 group["channels"] = 2;
1487
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 group["volume"] = 100;
1488
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 group["activeGroup"] = true;
1489
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 member["cardId"] = cId;
1490
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 member["channels"] = 2;
1491
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 member["delayMs"] = 0;
1492
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 member["volume"] = 100;
1493
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 group["members"] = Json::Value(Json::arrayValue);
1494
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 group["members"].append(member);
1495
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 root["groups"] = Json::Value(Json::arrayValue);
1496
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 root["groups"].append(group);
1497
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 PutFileContents(simpleGroupsJsonPath, SaveJsonToString(root));
1498
1499 // 97-fpp-audio-groups.conf — write the /etc dest and the media-dir cache
1500 // identically so the boot-time skip-regen fast path (cache == dest) holds.
1501
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 std::string conf = buildSimplePipeWireGroupsConf(card, cId, cardHasBootAdapter, perSize, pipewireSampleRate);
1502
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 exec("/bin/mkdir -p /etc/pipewire/pipewire.conf.d");
1503
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 PutFileContents(groupsConfDest, conf);
1504
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 PutFileContents(groupsConfCache, conf);
1505
1506 // Simple mode has no input groups — remove any leftover 96 conf + cache.
1507
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
50 if (FileExists(igConfDest)) unlink(igConfDest.c_str());
1508
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
50 if (FileExists(igConfCache)) unlink(igConfCache.c_str());
1509
1510 // Route fppd at the group sink; clear stale advanced-mode per-slot names.
1511
3/6
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
✗ Branch 5 not taken.
150 setRawSetting("PipeWireSinkName", "fpp_group_default");
1512
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 50 times.
250 for (int s = 2; s <= 5; s++) {
1513
3/6
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 200 times.
✗ Branch 5 not taken.
492 setRawSetting("PipeWireSinkName_" + std::to_string(s), "");
1514 }
1515
1516 // Record the (empty) simple video config so the UI/video path matches what
1517 // ApplyPipeWireVideoGroups would leave for an unconfigured VideoOutput.
1518
3/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 18 times.
50 if (!FileExists(videoSimpleJsonPath)) {
1519
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
64 PutFileContents(videoSimpleJsonPath, "{\n \"videoOutputGroups\": []\n}");
1520 }
1521 50 }
1522
1523 // `recoveryPass` is set only by the self-heal at the very end of this function,
1524 // which discards the cached audio config and re-runs the whole setup once when
1525 // the stack it just started produced no sink at all. It exists solely to bound
1526 // that retry to one attempt.
1527 127 static void runAudioSetup(bool recoveryPass) {
1528
3/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
✓ Branch 3 taken 42 times.
127 if (!FileExists("/root/.libao")) {
1529
3/6
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 85 times.
✗ Branch 5 not taken.
340 PutFileContents("/root/.libao", "dev=default");
1530 }
1531
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
254 std::string mediaBackend = "pipewire-simple";
1532
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
127 getRawSetting("MediaBackend", mediaBackend);
1533
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 std::string mediaBackendLower = mediaBackend;
1534 127 std::transform(mediaBackendLower.begin(), mediaBackendLower.end(), mediaBackendLower.begin(), [](unsigned char c) {
1535 1810 return std::tolower(c);
1536 });
1537 // The ALSA ("Hardware Direct") backend has been retired -- FPP10 always uses
1538 // a PipeWire backend. Migrate any device still set to "alsa" to the default
1539 // pipewire-simple and persist it, so we never take the (being-removed) ALSA
1540 // path and every other MediaBackend reader sees the new value. Done here, the
1541 // earliest audio entry point, so the rest of setupAudio runs as PipeWire.
1542
3/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 126 times.
127 if (mediaBackendLower == "alsa") {
1543
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("FPP - MediaBackend 'alsa' is retired; migrating to 'pipewire-simple'\n");
1544
3/6
✓ 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.
3 setRawSetting("MediaBackend", "pipewire-simple");
1545
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 mediaBackend = "pipewire-simple";
1546
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 mediaBackendLower = "pipewire-simple";
1547 }
1548
5/8
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 115 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 115 times.
✗ Branch 7 not taken.
127 bool usePipeWireBackend = (mediaBackendLower == "pipewire" || mediaBackendLower == "pipewire-simple");
1549
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 bool runningInDocker = FileExists("/.dockerenv");
1550
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 const std::string audioEnvPath = "/run/fppd/fpp-audio.env";
1551
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
127 printf("FPP - Audio backend: %s\n", usePipeWireBackend ? "PipeWire" : "ALSA");
1552
1553 // AudioOutput is persisted as a stable ALSA card ID string (e.g. "S3",
1554 // "bcm2835ALSA") rather than a card index, so a probe-order change (USB
1555 // add/remove, slow-probing cape, kernel update) doesn't repoint us at the
1556 // wrong device.
1557 // - empty/unset -> first present card (index 0), as before
1558 // - all-digits -> legacy index; used as-is and migrated to an ID below
1559 // - otherwise -> card ID; matched against /proc/asound/cards
1560 127 std::string audioOutputId;
1561
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
127 getRawSetting("AudioOutput", audioOutputId);
1562
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 TrimWhiteSpace(audioOutputId);
1563
4/4
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 75 times.
127 bool legacyNumeric = !audioOutputId.empty() && audioOutputId.find_first_not_of("0123456789") == std::string::npos;
1564
1565 // "Dummy" selects the synthetic snd-dummy card (a box that wants no real
1566 // audio). It only ever registers because we modprobe it, so waiting for it
1567 // below could never succeed -- load it now and every wait in this function
1568 // is already satisfied.
1569
13/20
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 114 times.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 9 times.
✓ Branch 10 taken 13 times.
✓ Branch 11 taken 114 times.
✓ Branch 12 taken 13 times.
✓ Branch 13 taken 114 times.
✓ Branch 14 taken 4 times.
✓ Branch 15 taken 123 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
153 if (audioOutputId == "Dummy" && getAlsaCardNumForId("Dummy") < 0) {
1570
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 printf("FPP - Audio device 'Dummy' selected; loading snd-dummy\n");
1571
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 modprobe("snd-dummy");
1572 }
1573
1574 // A card selected by ID may not have registered yet: cape modules are
1575 // modprobed back at cape detect, but an ASoC card (e.g. the K32Max's
1576 // CapeAudio-pcm5102a) binds asynchronously and can land after we get here.
1577 // Wait for it before probing, otherwise the aplay snapshot below misses it,
1578 // snd-dummy gets loaded in its place, and the selection resolves to Dummy.
1579 // Only a selected-but-absent card waits, so a board with no selection (or
1580 // one whose card is already up) pays nothing.
1581
9/10
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 73 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 125 times.
127 if (!audioOutputId.empty() && !legacyNumeric && getAlsaCardNumForId(audioOutputId) < 0) {
1582
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 printf("FPP - Waiting for audio device '%s' to register...\n", audioOutputId.c_str());
1583 2 int waited = 0;
1584
6/8
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 200 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 200 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 200 times.
✓ Branch 7 taken 2 times.
202 while (waited < 100 && getAlsaCardNumForId(audioOutputId) < 0) {
1585
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 std::this_thread::sleep_for(std::chrono::milliseconds(100));
1586 200 ++waited;
1587 }
1588
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (getAlsaCardNumForId(audioOutputId) < 0) {
1589
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 printf("FPP - Audio device '%s' did not appear after %d seconds\n", audioOutputId.c_str(), waited / 10);
1590 } else {
1591 ✗ printf("FPP - Audio device '%s' registered after %d.%d seconds\n", audioOutputId.c_str(), waited / 10, waited % 10);
1592 }
1593 }
1594
1595 127 std::string aplay;
1596 127 std::vector<std::string> lines;
1597 // Keyed "card N", in card-number order: as plain strings "card 10" sorts
1598 // before "card 2", and the fallback below takes the first usable card.
1599 struct CardKeyLess {
1600 2321 bool operator()(const std::string& a, const std::string& b) const {
1601 2321 int na = atoi(a.c_str() + std::min<size_t>(5, a.size()));
1602 2321 int nb = atoi(b.c_str() + std::min<size_t>(5, b.size()));
1603
2/2
✓ Branch 0 taken 671 times.
✓ Branch 1 taken 1650 times.
2321 return na != nb ? na < nb : a < b;
1604 }
1605 };
1606 127 std::map<std::string, std::string, CardKeyLess> cards;
1607 127 std::map<std::string, std::string, CardKeyLess> cardLines; // full aplay line per "card N"
1608 // Normalised card IDs that got an fpp_alsa_* sink node in 95-fpp-alsa-sink.conf
1609 // below; consulted by the Simple-mode C++ config generator to know whether a
1610 // card already has a boot-time adapter or needs a custom one inline.
1611 127 std::set<std::string> bootAdapterCids;
1612 // True once the Simple-mode config has been generated in C++ this run, so the
1613 // post-restart PHP regenerate/volume-restore dance can be skipped (the config
1614 // is already complete and self-contained).
1615 127 bool cppGeneratedSimpleConfig = false;
1616 127 std::map<std::string, bool> hdmiStatus;
1617 127 bool hasNonHDMI = false;
1618 // An onboard HDMI audio device: its aplay line names HDMI and it is not a
1619 // USB card. Only the SoC's HDMI ports answer to the DRM connector status
1620 // read below; a USB adapter with HDMI in its name (a DisplayLink "USB3 to
1621 // HDMI", whose line is "card 0: HDMI [USB3 to HDMI]") is an ordinary sound
1622 // card. Judged by its name alone it counted as no real card, so snd-dummy
1623 // was loaded, and with no display on the Pi's own port it was ruled a dead
1624 // HDMI output: the selection fell to Dummy and Simple mode played into
1625 // "FPP Audio (no sound card)", silently (issue #3159).
1626 333 auto lineHasHDMI = [](const std::string& l) {
1627
1/2
✓ Branch 0 taken 333 times.
✗ Branch 1 not taken.
333 std::string lc = l;
1628 333 std::transform(lc.begin(), lc.end(), lc.begin(), ::tolower);
1629
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 84 times.
333 if (lc.find("hdmi") == std::string::npos) {
1630 249 return false;
1631 }
1632 // "card N: ..."
1633
4/6
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 81 times.
✓ Branch 5 taken 3 times.
84 return !(l.starts_with("card ") && alsaCardIsUsb(atoi(l.c_str() + 5)));
1634 333 };
1635 // Snapshot the cards "aplay -l" can see. Wrapped so it can be re-run after
1636 // waiting for a late-binding card below.
1637 131 auto probeCards = [&]() {
1638
2/4
✓ Branch 0 taken 131 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 131 times.
✗ Branch 3 not taken.
131 aplay = execAndReturn("/usr/bin/aplay -l 2>&1");
1639
1/2
✓ Branch 0 taken 131 times.
✗ Branch 1 not taken.
131 lines = split(aplay, '\n');
1640 131 cards.clear();
1641 131 cardLines.clear();
1642 131 hasNonHDMI = false;
1643
2/2
✓ Branch 0 taken 425 times.
✓ Branch 1 taken 131 times.
556 for (auto& l : lines) {
1644 // "card 10: S3 [USB Audio Device], device 0: ..." -- the number is
1645 // as wide as it is. Fixed offsets made card 10 "card 1" with no
1646 // id, overwriting card 1.
1647 425 size_t colon = l.find(':');
1648
6/8
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 324 times.
✓ Branch 2 taken 101 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 101 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 101 times.
✓ Branch 7 taken 324 times.
425 if (l.starts_with("card ") && colon != std::string::npos && colon > 5) {
1649
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 std::string k = l.substr(0, colon);
1650
3/10
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 101 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
101 std::string v = colon + 2 <= l.size() ? l.substr(colon + 2) : "";
1651
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 v = v.substr(0, v.find(' '));
1652
2/4
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
✗ Branch 3 not taken.
101 cards[k] = v;
1653
2/4
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
✗ Branch 3 not taken.
101 cardLines[k] = l;
1654 // The synthetic snd-dummy is not real hardware; with an explicit
1655 // "Dummy" selection it is loaded before this probe runs and must
1656 // not defeat the no-real-soundcard fast paths below.
1657
5/8
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 37 times.
✓ Branch 4 taken 64 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 64 times.
✗ Branch 7 not taken.
101 hasNonHDMI |= !lineHasHDMI(l) && v != "Dummy";
1658 101 }
1659 }
1660 131 };
1661
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 probeCards();
1662 127 int hdmiIdx = 0;
1663 127 bool anyHDMIConnected = false;
1664
2/2
✓ Branch 0 taken 508 times.
✓ Branch 1 taken 127 times.
635 for (int x = 0; x < 4; x++) {
1665
2/4
✓ Branch 0 taken 508 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 508 times.
✗ Branch 3 not taken.
508 std::string cstr = "/sys/class/drm/card" + std::to_string(x) + "-HDMI-A-1/status";
1666
3/4
✓ Branch 0 taken 508 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 489 times.
508 if (FileExists(cstr)) {
1667
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 19 times.
95 for (int p = 1; p < 5; p++) {
1668
4/8
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 76 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 76 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 76 times.
✗ Branch 7 not taken.
76 std::string cstr = "/sys/class/drm/card" + std::to_string(x) + "-HDMI-A-" + std::to_string(p) + "/status";
1669
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 std::string c = GetFileContents(cstr);
1670
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 std::string k = "vc4hdmi" + std::to_string(hdmiIdx);
1671
4/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 34 times.
✓ Branch 3 taken 3 times.
76 bool conn = c.contains("connected") && !c.contains("disconnected");
1672
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 hdmiStatus[k] = conn;
1673 76 anyHDMIConnected |= conn;
1674 76 hdmiIdx++;
1675 76 }
1676 }
1677 508 }
1678 // True when the only audio device is the synthetic snd-dummy (no real,
1679 // non-HDMI sound card present) -- the common case on a BeagleBone with no
1680 // audio cape. Used later to skip the expensive PipeWire device-enumeration
1681 // and group-regeneration dance, which only matters for real/hot-pluggable
1682 // hardware.
1683
9/16
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 60 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 60 times.
✓ Branch 8 taken 60 times.
✓ Branch 9 taken 67 times.
✓ Branch 10 taken 60 times.
✓ Branch 11 taken 67 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
187 bool noRealSoundcard = (!hasNonHDMI || contains(aplay, "no soundcards"));
1684 // Loading snd-dummy is a one-way door: it takes the next free card number, so
1685 // a real card that binds a moment later lands behind it and the default
1686 // selection resolves to Dummy. Before committing, give hardware that is
1687 // physically here -- declared in the device tree, or a plugged-in USB audio
1688 // device -- a bounded chance to finish binding. Cape modules are modprobed
1689 // back in fppinit's cape detect (an earlier systemd unit) and an ASoC card
1690 // binds synchronously as its last component registers, so this is normally
1691 // already satisfied -- it only pays out when the driver is still being
1692 // autoloaded. Gated so a board with neither (a BeagleBone with no audio cape,
1693 // the common case) waits zero.
1694 // An explicit "Dummy" selection also skips this: the user asked for no real
1695 // audio, so there is nothing worth waiting on even if hardware is present.
1696
15/20
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 67 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 54 times.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 54 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 53 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 53 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 48 times.
✓ Branch 13 taken 5 times.
✓ Branch 14 taken 48 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 48 times.
✓ Branch 18 taken 5 times.
✓ Branch 19 taken 122 times.
127 if (noRealSoundcard && audioOutputId != "Dummy" && countRealAlsaCards() == 0 && (deviceTreeDeclaresSoundCard() || usbAudioDevicePresent())) {
1697
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 printf("FPP - No soundcard yet, but audio hardware is present; waiting...\n");
1698 // Don't just wait on udev -- it may not have autoloaded the CPU DAI or
1699 // machine driver yet, and without them the card can never bind no matter
1700 // how long we sit here.
1701
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 loadPendingSoundDrivers();
1702 5 int waited = 0;
1703
7/8
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 94 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 90 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 90 times.
✓ Branch 7 taken 5 times.
95 while (waited < 50 && countRealAlsaCards() == 0) {
1704
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 std::this_thread::sleep_for(std::chrono::milliseconds(100));
1705 90 ++waited;
1706 }
1707
3/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
5 if (countRealAlsaCards() > 0) {
1708 // The card is registered in the kernel, but "aplay -l" reads
1709 // /dev/snd/controlC*, which udev creates a beat later (~115ms measured
1710 // on a BeagleBone). This is the one part udev genuinely owns, so settle
1711 // for it rather than re-probing into the gap.
1712
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 exec("/usr/bin/udevadm settle --timeout=5 > /dev/null 2>&1");
1713
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 printf("FPP - Soundcard registered after %d.%ds\n", waited / 10, waited % 10);
1714
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 probeCards();
1715
6/16
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
12 noRealSoundcard = (!hasNonHDMI || contains(aplay, "no soundcards"));
1716 } else {
1717
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("FPP - No soundcard appeared after %d seconds\n", waited / 10);
1718 }
1719 }
1720
12/18
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 63 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 50 times.
✓ Branch 7 taken 13 times.
✓ Branch 8 taken 63 times.
✓ Branch 9 taken 64 times.
✓ Branch 10 taken 63 times.
✓ Branch 11 taken 64 times.
✓ Branch 12 taken 50 times.
✓ Branch 13 taken 77 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
253 if (noRealSoundcard && getAlsaCardNumForId("Dummy") < 0) {
1721
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 printf("FPP - No Soundcard Detected, loading snd-dummy\n");
1722
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 modprobe("snd-dummy");
1723 }
1724 // Resolve the selected card ID (read above) to the current card number;
1725 // everything below continues to work with the numeric index.
1726 // True when the selected card ID names a device that isn't present, so we
1727 // are running this boot on a fallback card. The selection is the user's and
1728 // the absence may be transient (a card still probing, a USB device
1729 // unplugged for the evening), so a fallback must not be written back over
1730 // it -- doing so silently loses the selection across a reboot.
1731 127 bool selectedCardMissing = false;
1732 // True when this boot plays on some other card than the selection --
1733 // the selected card is missing, is HDMI with no display, or never
1734 // answered. The fallback is used for this boot only and recorded in
1735 // EFFECTIVE_AUDIO_OUTPUT_PATH; AudioOutput keeps the user's choice.
1736 // Writing the fallback into AudioOutput lost that choice for good: a show
1737 // controller whose TV was off at boot came back on the headphone jack
1738 // every boot after, display or not.
1739 127 bool onFallback = false;
1740 127 int card = 0;
1741
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 79 times.
127 if (audioOutputId.empty()) {
1742 48 card = 0;
1743
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 75 times.
79 } else if (legacyNumeric) {
1744
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 card = std::stoi(audioOutputId);
1745 } else {
1746
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 card = getAlsaCardNumForId(audioOutputId);
1747
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 73 times.
75 if (card < 0) {
1748
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 printf("FPP - Audio device '%s' not currently present; falling back to card 0 for this boot\n", audioOutputId.c_str());
1749 2 card = 0;
1750 2 selectedCardMissing = true;
1751 2 onFallback = true;
1752 }
1753 }
1754
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 std::string cstr = "card " + std::to_string(card);
1755 127 bool found = false;
1756 127 int count = 0;
1757
1758 // Treat the selected card as unusable if it's any HDMI card (vc4hdmi OR
1759 // legacy bcm2835 HDMI) with no display connected. Playing to an
1760 // unconnected HDMI audio device panics the kernel on some Pis.
1761 232 auto cardIsDeadHDMI = [&](const std::string& k) {
1762
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 44 times.
232 if (!lineHasHDMI(cardLines[k])) return false;
1763
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 2 times.
44 if (cards[k].starts_with("vc4hdmi")) {
1764 // hdmiStatus is keyed by a synthesized connector index ("vc4hdmi0",
1765 // "vc4hdmi1" on dual-HDMI boards). Single-HDMI boards (e.g. Pi Zero
1766 // 2 W, Pi 3) register the card as plain "vc4hdmi", which never
1767 // matches an "NN" index key; the std::map read then yields false and
1768 // the card is wrongly declared "no HDMI connected" even when a
1769 // display is attached, causing setupAudio to permanently revert
1770 // AudioOutput back to card 0. When there is no per-port status for
1771 // this card, fall back to the any-HDMI-connected signal (same
1772 // semantics as the legacy bcm2835 HDMI branch below).
1773
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 auto it = hdmiStatus.find(cards[k]);
1774
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 7 times.
42 if (it != hdmiStatus.end()) {
1775 35 return !it->second;
1776 }
1777 7 return !anyHDMIConnected;
1778 }
1779 // legacy bcm2835 HDMI shares the physical port; if any HDMI is
1780 // connected, assume this device may work, otherwise treat as dead.
1781 2 return !anyHDMIConnected;
1782 127 };
1783
1784
3/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 124 times.
127 if (cardIsDeadHDMI(cstr)) {
1785 // Walk cards in index order and pick the first non-HDMI, or HDMI with
1786 // display connected. Falls through to the snd-dummy case otherwise.
1787 3 int fallback = -1;
1788
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 for (const auto& [k, _] : cards) {
1789
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
4 if (!cardIsDeadHDMI(k)) {
1790
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 fallback = std::stoi(k.substr(5));
1791 3 break;
1792 }
1793 }
1794
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (fallback >= 0 && fallback != card) {
1795
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 printf("FPP - Audio device %d has no HDMI connected, using card %d (%s) for this boot\n",
1796
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 card, fallback, cards["card " + std::to_string(fallback)].c_str());
1797 3 card = fallback;
1798 } else {
1799 ✗ card = cards.size();
1800 ✗ found = true;
1801 }
1802 3 onFallback = true;
1803 }
1804
3/4
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 127 times.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
254 while (!found && count < 50) {
1805 // A read: "cset numid=3" was the old probe, which wrote whatever
1806 // control 3 is on any card but the Pi's own (output routing there).
1807
3/6
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 127 times.
✗ Branch 5 not taken.
127 std::string amixer = execAndReturn("/usr/bin/amixer -c " + std::to_string(card) + " info 2>&1");
1808
3/6
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 127 times.
254 if (contains(amixer, "Invalid ")) {
1809 ✗ ++count;
1810 ✗ std::this_thread::sleep_for(std::chrono::milliseconds(200));
1811 } else {
1812 127 found = true;
1813 }
1814 127 }
1815
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
127 if (!found) {
1816 // The probe below is an amixer control write, which a working card
1817 // can refuse; don't record card 0 over the selection on its word.
1818 ✗ printf("FPP - Could not find audio device %d, defaulting to device 0.\n", card);
1819 ✗ CopyFileContents("/opt/fpp/etc/asoundrc.plain", "/root/.asoundrc");
1820 } else {
1821
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 printf("FPP - Waited for %d seconds for audio device\n", (count / 5));
1822 // Persist the selection as the stable ALSA card ID. This migrates a
1823 // legacy numeric value and canonicalizes the stored ID so the next
1824 // boot matches by name regardless of probe order. Skipped when we're on
1825 // a fallback card: that would overwrite the user's choice with whatever
1826 // happened to be present (typically the synthetic Dummy).
1827
6/10
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 122 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 122 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 122 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
132 std::string canonicalId = onFallback ? "" : getAlsaCardId(card);
1828
6/6
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 74 times.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 79 times.
127 if (!canonicalId.empty() && canonicalId != audioOutputId) {
1829
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 printf("FPP - Persisting selected audio device as ID '%s'\n", canonicalId.c_str());
1830
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
96 setRawSetting("AudioOutput", canonicalId);
1831 }
1832 // Point ALSA's default at the chosen card so root-context playback
1833 // (e.g. flite from fppinit) doesn't land on a dead HDMI device.
1834
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
127 std::string arc = GetFileContents("/opt/fpp/etc/asoundrc.plain");
1835
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 std::string cardLine = "card " + std::to_string(card);
1836 127 size_t pos = 0;
1837
2/2
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 127 times.
381 while ((pos = arc.find("card 0", pos)) != std::string::npos) {
1838
1/2
✓ Branch 0 taken 254 times.
✗ Branch 1 not taken.
254 arc.replace(pos, 6, cardLine);
1839 254 pos += cardLine.size();
1840 }
1841
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
127 PutFileContents("/root/.asoundrc", arc);
1842 127 }
1843 // What this boot plays on, for whatever reads AudioOutput later (Simple
1844 // mode's PHP apply, fppd's volume control). They use "effective" while
1845 // AudioOutput still equals "selected", and AudioOutput once the user has
1846 // changed it.
1847
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 52 times.
127 if (!runningInDocker) {
1848 75 mkdir("/run/fppd", 0755);
1849
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 Json::Value eff;
1850
2/4
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
75 eff["selected"] = audioOutputId;
1851
3/6
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
75 eff["effective"] = getAlsaCardId(card);
1852
2/4
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
75 eff["fallback"] = onFallback;
1853
3/6
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
147 PutFileContents(EFFECTIVE_AUDIO_OUTPUT_PATH, SaveJsonToString(eff));
1854 75 }
1855
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
127 int v = getRawSettingInt("volume", -1);
1856
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 43 times.
127 if (v == -1) {
1857
3/6
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 84 times.
✗ Branch 5 not taken.
252 setRawSetting("volume", "70");
1858 84 v = 70;
1859 }
1860 127 std::string origAudio0CardType;
1861
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
254 getRawSetting("AudioCard0Type", origAudio0CardType);
1862
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 std::string audioCardType = "unknown";
1863
3/6
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 127 times.
✗ Branch 5 not taken.
127 std::string aplayl = execAndReturn("/usr/bin/aplay -l | grep 'card " + std::to_string(card) + "'");
1864 #ifdef PLATFORM_PI
1865 // Pi headphone jack needs a volume adjustment, in reality a lot of sound cards do, but we
1866 // don't want to put in a lot of special cases here so only handle the Pi
1867
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
16 if (card == 0) {
1868
8/30
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 1 times.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
3 if (contains(aplayl, "[bcm2") && !contains(aplayl, "-i2s")) {
1869 ✗ v = (v / 2) + 50;
1870 ✗ audioCardType = "bcm2";
1871 }
1872 }
1873 #endif
1874
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 43 times.
127 if (audioCardType != origAudio0CardType) {
1875
2/4
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
168 setRawSetting("AudioCard0Type", audioCardType);
1876 }
1877
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 std::string cardType = aplayl.substr(aplayl.find("[") + 1);
1878
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 cardType = cardType.substr(0, cardType.find("]"));
1879
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 printf("FPP - Found sound card of type %s\n", cardType.c_str());
1880 127 std::string asoundrc;
1881
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 if (usePipeWireBackend) {
1882
7/10
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 87 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 87 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 87 times.
✓ Branch 9 taken 40 times.
127 if (!FileExists("/etc/pipewire/client.conf") && FileExists("/usr/share/pipewire/client.conf")) {
1883
3/6
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 87 times.
✗ Branch 5 not taken.
348 CopyFileContents("/usr/share/pipewire/client.conf", "/etc/pipewire/client.conf");
1884 }
1885
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
254 asoundrc = GetFileContents("/opt/fpp/etc/asoundrc.pipewire");
1886 } else {
1887 ✗ if (FileExists(FPP_MEDIA_DIR + "/tmp/asoundrc")) {
1888 ✗ if (contains(cardType, "pcm510")) {
1889 ✗ printf("FPP - Using Cape Provided asoundrc\n");
1890 ✗ asoundrc = GetFileContents(FPP_MEDIA_DIR + "/tmp/asoundrc");
1891 } else {
1892 ✗ asoundrc = GetFileContents("/opt/fpp/etc/asoundrc.dmix");
1893 }
1894 }
1895 ✗ if (asoundrc.empty()) {
1896 ✗ if (contains(cardType, "vc4-hdmi")) {
1897 ✗ replaceAll(cardType, "-", "");
1898 ✗ asoundrc = GetFileContents("/opt/fpp/etc/asoundrc.hdmi");
1899 ✗ } else if (contains(cardType, "bcm2")) {
1900 ✗ asoundrc = GetFileContents("/opt/fpp/etc/asoundrc.plain");
1901 } else {
1902 ✗ asoundrc = GetFileContents("/opt/fpp/etc/asoundrc.dmix");
1903 }
1904 }
1905 }
1906 // The legacy ALSA templates carry a BUFFERSIZE token; the PipeWire one does
1907 // not, and the ALSA backend is retired (see the migration above), so nothing
1908 // reachable consumes this any more. The old AudioBufferSize setting was
1909 // removed rather than left in the UI doing nothing; substitute the value it
1910 // defaulted to so the templates stay valid if one is ever used again.
1911 127 constexpr int LEGACY_ALSA_BUFFER_SIZE = 3072;
1912
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
254 int perSize = getRawSettingInt("AudioPeriodSize", 1024);
1913
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
166 replaceAll(asoundrc, "CARDTYPE", cardType);
1914
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
342 replaceAll(asoundrc, "BUFFERSIZE", std::to_string(LEGACY_ALSA_BUFFER_SIZE));
1915
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
303 replaceAll(asoundrc, "PERIODSIZE", std::to_string(perSize));
1916
2/2
✓ Branch 0 taken 1270 times.
✓ Branch 1 taken 127 times.
1397 for (int x = 0; x < 10; x++) {
1917
2/2
✓ Branch 0 taken 1143 times.
✓ Branch 1 taken 127 times.
1270 if (x != card) {
1918
3/6
✓ Branch 0 taken 1143 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1143 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1143 times.
✗ Branch 5 not taken.
1143 replaceAll(asoundrc, "card " + std::to_string(x), "card " + std::to_string(card));
1919 }
1920 }
1921 // AudioFormat used to encode both a rate and a sample format (44100/S16,
1922 // 44100/S32, ...), but only the rate has been used since the ALSA backend
1923 // was retired -- PipeWire probes the card for the format. The setting now
1924 // offers just the rates (0 default, 1 = 44100, 4 = 48000, 7 = 96000), so
1925 // fold any stored bit-depth variant onto its rate. Without this the box
1926 // keeps a value the dropdown no longer lists, which renders as no selection
1927 // and silently drops to 44100 the first time the page is saved.
1928
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
127 int rate = getRawSettingInt("AudioFormat", 0);
1929 127 int normalizedRate = rate;
1930
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 126 times.
127 if (rate >= 7) {
1931 1 normalizedRate = 7;
1932
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 124 times.
126 } else if (rate >= 4) {
1933 2 normalizedRate = 4;
1934
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 123 times.
124 } else if (rate >= 1) {
1935 1 normalizedRate = 1;
1936 }
1937
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 124 times.
127 if (normalizedRate != rate) {
1938
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 printf("FPP - AudioFormat %d no longer selects a sample format; storing %d\n", rate, normalizedRate);
1939
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
9 setRawSetting("AudioFormat", std::to_string(normalizedRate));
1940 3 rate = normalizedRate;
1941 }
1942 127 int pipewireSampleRate = 44100;
1943
4/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 123 times.
127 switch (rate) {
1944 1 case 1:
1945 // replaceAll(asoundrc, "rate 44100", "rate 44100");
1946 1 break;
1947 2 case 4:
1948
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
6 replaceAll(asoundrc, "rate 44100", "rate 48000");
1949 2 pipewireSampleRate = 48000;
1950 2 break;
1951 1 case 7:
1952
3/6
✓ 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.
3 replaceAll(asoundrc, "rate 44100", "rate 96000");
1953 1 pipewireSampleRate = 96000;
1954 1 break;
1955 123 default:
1956 123 break;
1957 }
1958
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
254 PutFileContents("/root/.asoundrc", asoundrc);
1959
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 const std::string pipewireSinkConfPath = "/etc/pipewire/pipewire.conf.d/95-fpp-alsa-sink.conf";
1960 // Snapshot what PipeWire loaded at boot so we can tell whether the freshly
1961 // generated sink config actually differs (and thus needs a restart below).
1962
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 const std::string existingSinkConf = GetFileContents(pipewireSinkConfPath);
1963 127 bool sinkConfigChanged = false;
1964 // Cheap pre-check: is the persisted 95-fpp-alsa-sink.conf still valid for the
1965 // cards present right now? It is when its fpp_alsa_* sink adapters cover
1966 // exactly the present, usable (non-dummy, non-dead-HDMI) cards. When so, skip
1967 // the whole per-card ALSA probe below -- aplay/amixer/arecord plus a 2s
1968 // hw-params dump each, all via popen, which is the slowest part of audio
1969 // setup -- since it only changes anything when a card was added or removed.
1970 // The conf persists in /etc across reboots, so the common boot hits this.
1971 //
1972 // Which cards share an audio group with another sink, which is what decides
1973 // the USB headroom below. Derived here, before the probe, so the validity
1974 // check can compare it: it needs nothing the probe produces, only the groups
1975 // JSON and the aplay lines already in hand.
1976 const std::map<std::string, int> groupSizeByCard = pipewireGroupSizeByCard(
1977
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
254 FPP_MEDIA_DIR + "/config/" +
1978
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 (mediaBackendLower == "pipewire-simple" ? "pipewire-audio-groups-simple.json"
1979
4/6
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 127 times.
✗ Branch 5 not taken.
215 : "pipewire-audio-groups.json"));
1980 14 auto cardSharesGroup = [&groupSizeByCard](const std::string& cId) {
1981
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 auto it = groupSizeByCard.find(cId);
1982
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
14 return it != groupSizeByCard.end() && it->second > 1;
1983 127 };
1984 97 auto lineIsUsbCard = [](const std::string& l) {
1985 97 return l.find("USB Audio") != std::string::npos;
1986 };
1987 127 std::set<std::string> sharedGroupUsbCids; // USB cards that must keep the follower headroom
1988 127 std::set<std::string> adapterCandidateCids; // present cards that would get an adapter
1989 127 std::set<std::string> adapterCardNames; // ...and their ALSA short-names, for the WirePlumber rule
1990
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 127 times.
228 for (const auto& [key, cardId] : cards) {
1991
3/6
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 101 times.
✗ Branch 5 not taken.
101 std::string cId = getAlsaCardId(std::stoi(key.substr(5)));
1992
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
101 if (cId.empty()) cId = cardId;
1993
7/10
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 101 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 97 times.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 97 times.
101 if (cId == "Dummy" || cardIsDeadHDMI(key)) continue;
1994
2/4
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
97 adapterCandidateCids.insert(normalizeCardIdForNode(cId));
1995
8/14
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 97 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✓ Branch 7 taken 83 times.
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 97 times.
97 if (cardLines.count(key) && lineIsUsbCard(cardLines[key]) && cardSharesGroup(cId)) {
1996 ✗ sharedGroupUsbCids.insert(normalizeCardIdForNode(cId));
1997 }
1998 // "card N: <id> [<short name>], device M: ..." -- that first bracketed
1999 // field is exactly what the ALSA monitor exposes as api.alsa.card.name,
2000 // which is what ensureWirePlumberAlsaDupeSuppression() matches on.
2001
2/4
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
97 if (cardLines.count(key)) {
2002
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 const std::string& l = cardLines[key];
2003 97 auto b = l.find('[');
2004 97 auto e = l.find(']');
2005
3/6
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 97 times.
✗ Branch 5 not taken.
97 if (b != std::string::npos && e != std::string::npos && e > b) {
2006
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 std::string nm = l.substr(b + 1, e - b - 1);
2007
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 TrimWhiteSpace(nm);
2008 // A quote would break out of the generated SPA-JSON string; such a
2009 // card name has never been seen, but a malformed rules file would
2010 // take down the whole monitor, so skip rather than risk it.
2011
3/6
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 97 times.
✗ Branch 5 not taken.
97 if (!nm.empty() && nm.find('"') == std::string::npos) {
2012
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 adapterCardNames.insert(nm);
2013 }
2014 97 }
2015 }
2016
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 4 times.
101 }
2017 127 std::set<std::string> existingAdapterCids; // fpp_alsa_* sink adapters already in the conf
2018
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 std::regex alsaAdapterRe(R"RE(node\.name = "fpp_alsa_([a-z0-9_]+)")RE");
2019
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 for (auto it = std::sregex_iterator(existingSinkConf.begin(), existingSinkConf.end(), alsaAdapterRe);
2020
3/4
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 127 times.
148 it != std::sregex_iterator(); ++it) {
2021
4/8
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 21 times.
✗ Branch 7 not taken.
21 existingAdapterCids.insert((*it)[1].str());
2022 127 }
2023 // A conf written by an older FPP describes the same cards by rules that have
2024 // since been corrected, and the card-set comparison below cannot see that.
2025 // Treat a missing or older generation tag as reason enough to re-probe.
2026
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
127 const bool sinkConfGenerationCurrent = startsWith(existingSinkConf, alsaSinkConfGenerationTag());
2027
5/8
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 104 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 23 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 127 times.
127 if (usePipeWireBackend && !existingSinkConf.empty() && !sinkConfGenerationCurrent) {
2028 ✗ printf("FPP - PipeWire: ALSA sink config predates the current probe rules; re-probing\n");
2029 }
2030 // Compared against the recorded candidate list, not against the adapters the
2031 // conf declares: the probe legitimately writes fewer adapters than there are
2032 // candidates. See alsaSinkConfCardsTag().
2033 const bool sinkConfCardsMatch =
2034
3/6
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 127 times.
✗ Branch 5 not taken.
127 contains(existingSinkConf, alsaSinkConfCardsTag(adapterCandidateCids) + "\n");
2035 // Group membership moves independently of the card set, so this has to be
2036 // reported separately -- blaming a headroom change on the hardware would send
2037 // anyone reading the boot log looking for a card that did not actually move.
2038 const bool sinkConfHeadroomMatch =
2039
3/6
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 127 times.
✗ Branch 5 not taken.
127 contains(existingSinkConf, alsaSinkConfUsbHeadroomTag(sharedGroupUsbCids) + "\n");
2040
3/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 4 times.
150 bool sinkConfStillValid = !existingSinkConf.empty() && sinkConfGenerationCurrent &&
2041
3/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
150 sinkConfCardsMatch && sinkConfHeadroomMatch;
2042
8/10
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 104 times.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 19 times.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 123 times.
127 if (usePipeWireBackend && !existingSinkConf.empty() && sinkConfGenerationCurrent && !sinkConfStillValid) {
2043
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 printf("FPP - PipeWire: %s; re-probing\n",
2044 !sinkConfCardsMatch ? "set of present sound cards has changed"
2045 : "USB audio group membership has changed");
2046 }
2047 // A conf written before a card's channel-count quirk existed (or while the
2048 // card was held open at a stale stereo negotiation) can cover all cards yet
2049 // still pin a known multi-channel card to too few channels. Verify each
2050 // quirk card's adapter declares at least the quirk count; otherwise reprobe.
2051
3/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 108 times.
127 if (usePipeWireBackend && sinkConfStillValid) {
2052
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 18 times.
51 for (const auto& [key, cardName] : cards) {
2053
4/8
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
33 int quirkCh = cardLines.count(key) ? quirkMinChannelsForCard(cardLines[key]) : 0;
2054
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
33 if (quirkCh <= 0) continue;
2055
3/6
✓ 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.
1 std::string cId = getAlsaCardId(std::stoi(key.substr(5)));
2056
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (cId.empty()) cId = cardName;
2057
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 std::regex chRe("node\\.name = \"fpp_alsa_" + normalizeCardIdForNode(cId) +
2058
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 "\"[\\s\\S]*?audio\\.channels = (\\d+)");
2059 1 std::smatch chM;
2060
8/18
✓ 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 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 1 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
1 if (std::regex_search(existingSinkConf, chM, chRe) && std::stoi(chM[1].str()) < quirkCh) {
2061
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("FPP - PipeWire: %s adapter has %s channels but card needs %d; regenerating\n",
2062
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 cId.c_str(), chM[1].str().c_str(), quirkCh);
2063 1 sinkConfStillValid = false;
2064 1 break;
2065 }
2066
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
3 }
2067 }
2068 // The persisted conf pins each adapter's audio.rate (and, via the probe, the
2069 // graph clock) to whatever AudioFormat was set when it was written. Changing
2070 // that setting has to regenerate, which the card-set comparison above cannot
2071 // see -- the same cards are still present, just at a different rate.
2072 //
2073 // Compared against the recorded request, NOT against default.clock.rate: that
2074 // field holds the rate the card achieved, so on a cape that refines the
2075 // request upward it never equals pipewireSampleRate and every boot re-probed.
2076
3/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 109 times.
145 if (usePipeWireBackend && sinkConfStillValid &&
2077
9/18
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 18 times.
✓ Branch 8 taken 18 times.
✓ Branch 9 taken 109 times.
✓ Branch 10 taken 18 times.
✓ Branch 11 taken 109 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 127 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
145 !contains(existingSinkConf, alsaSinkConfRateTag(pipewireSampleRate) + "\n")) {
2078 ✗ printf("FPP - PipeWire: configured sample rate is now %d; regenerating\n", pipewireSampleRate);
2079 ✗ sinkConfStillValid = false;
2080 }
2081 // Period size moves independently of everything above -- the card set, the
2082 // rate and the group membership can all be unchanged while AudioPeriodSize
2083 // has been retyped in the UI. A conf written before the tag existed has no
2084 // such line and re-probes once, which is correct: its period-size is
2085 // whatever the old default was, not necessarily what the setting now says.
2086
3/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 109 times.
145 if (usePipeWireBackend && sinkConfStillValid &&
2087
11/18
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 16 times.
✓ Branch 8 taken 18 times.
✓ Branch 9 taken 109 times.
✓ Branch 10 taken 18 times.
✓ Branch 11 taken 109 times.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 125 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
145 !contains(existingSinkConf, alsaSinkConfPeriodSizeTag(perSize) + "\n")) {
2088
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 printf("FPP - PipeWire: configured period size is now %d; regenerating\n", perSize);
2089 2 sinkConfStillValid = false;
2090 }
2091
3/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 111 times.
127 if (usePipeWireBackend && sinkConfStillValid) {
2092
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 printf("FPP - PipeWire: ALSA sink adapters already match present cards; skipping probe\n");
2093 // bootAdapterCids must still reflect the conf's adapters for the
2094 // Simple-mode group generator below; take them from the existing conf.
2095
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 bootAdapterCids = existingAdapterCids;
2096
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 } else if (usePipeWireBackend) {
2097
2/4
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 111 times.
✗ Branch 3 not taken.
222 exec("/bin/mkdir -p /etc/pipewire/pipewire.conf.d");
2098 // Create FPP ALSA adapter nodes for ALL playback-capable cards present
2099 // at boot. This ensures consistent naming (fpp_alsa_{cardIdNorm}) and
2100 // prevents WirePlumber from creating confusingly-named duplicates.
2101 // USB devices plugged in after boot get adapters created at Apply time
2102 // by GeneratePipeWireGroupsConfig() in pipewire.php.
2103
2/4
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 111 times.
✗ Branch 3 not taken.
111 std::string arecordAll = execAndReturn("/usr/bin/arecord -l 2>/dev/null");
2104
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 std::ostringstream pipewireSink;
2105 // The rate the graph clock has to run at: what the *selected* card's PCM
2106 // actually ends up clocked at, which is not always what was asked for.
2107 // A fixed-bit-clock cape can refine the request upward -- an AM62x
2108 // PCM5102A cape clocks no lower than 88200, so a 44100 request lands on
2109 // 88200 -- and a graph pinned to the requested rate then resamples for
2110 // the life of the process. Filled in from the device below.
2111 111 int graphClockRate = pipewireSampleRate;
2112
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 pipewireSink << "context.objects = [\n";
2113
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 111 times.
184 for (const auto& [key, cardName] : cards) {
2114
2/4
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 73 times.
✗ Branch 3 not taken.
73 int cardNum = std::stoi(key.substr(5)); // "card N" -> N
2115
1/2
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
73 std::string cId = getAlsaCardId(cardNum);
2116
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
73 if (cId.empty()) cId = cardName;
2117 // Never create a boot-time adapter for the synthetic snd-dummy card.
2118 // It is only present here when snd-dummy happened to be loaded before
2119 // the aplay probe (e.g. a re-run of setupAudio within one boot); on a
2120 // clean boot it is loaded *after* this probe and so isn't in `cards`.
2121 // Letting it slip in would make the 95-conf non-deterministic and
2122 // leave the cached 97 group config targeting an fpp_alsa_dummy that
2123 // vanishes on the next clean reboot. The dummy is always handled
2124 // inline by the Simple-mode group config (buildSimplePipeWireGroupsConf).
2125
2/4
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 73 times.
73 if (cId == "Dummy") continue;
2126 // Normalise card ID for node name: lowercase, non-alnum → underscore
2127
1/2
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
73 std::string cidNorm = cId;
2128 73 std::transform(cidNorm.begin(), cidNorm.end(), cidNorm.begin(), ::tolower);
2129
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 73 times.
775 for (auto& ch : cidNorm) {
2130
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 702 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
702 if (!std::isalnum(static_cast<unsigned char>(ch)) && ch != '_') ch = '_';
2131 }
2132
2133 // Probe ALSA HW params with an exclusive open. This can fail for two
2134 // very different reasons that must NOT be conflated:
2135 // 1. The device is genuinely dead — e.g. HDMI with nothing
2136 // connected (error 524/ENOMEDIUM). Skip it.
2137 // 2. The device is merely busy because PipeWire/WirePlumber already
2138 // grabbed it. This code runs in postNetwork, AFTER
2139 // fpp-pipewire.service has started and opened every card it knows
2140 // about, so the *selected* card is routinely busy here. Skipping
2141 // it would drop the adapter for the very device that's playing
2142 // and (once PipeWire is restarted for any reason) silence it.
2143 // A busy device reports EBUSY and, because it is open, exposes its
2144 // live negotiated params in /proc; a dead device reports ENOMEDIUM and
2145 // its /proc hw_params reads "closed".
2146
3/6
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 73 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 73 times.
✗ Branch 5 not taken.
73 std::string hwParams = execAndReturn("timeout 2 /usr/bin/aplay -D hw:" + cId + " --dump-hw-params /dev/zero 2>&1 | head -40");
2147 // Set when hwParams below is synthesised from /proc rather than read
2148 // from the device, which changes how much the format line is worth.
2149 73 bool paramsFromLiveDevice = false;
2150 // Rate read straight off a device that is already open, if it is.
2151 73 int liveRate = 0;
2152
4/6
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 73 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 44 times.
✓ Branch 5 taken 29 times.
146 if (!contains(hwParams, "HW Params")) {
2153
3/6
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 44 times.
✗ Branch 5 not taken.
44 std::string procHw = GetFileContents("/proc/asound/card" + std::to_string(cardNum) + "/pcm0p/sub0/hw_params");
2154
10/28
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 44 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 44 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 44 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 44 times.
✓ Branch 12 taken 44 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 44 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 44 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 44 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
176 bool busy = contains(hwParams, "resource busy") || contains(hwParams, "Resource busy");
2155
9/16
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 18 times.
✓ Branch 8 taken 18 times.
✓ Branch 9 taken 26 times.
✓ Branch 10 taken 18 times.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
62 bool procOpen = !procHw.empty() && !contains(procHw, "closed");
2156
2/4
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
44 if (!busy && !procOpen) {
2157
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 printf("FPP - PipeWire: skipping card %d (%s) — device cannot be opened\n",
2158 cardNum, cId.c_str());
2159 44 continue;
2160 }
2161 // Present but busy: synthesise an aplay-style "HW Params" block
2162 // from the live /proc values so the channel/format detection below
2163 // works unchanged. /proc reports the format PipeWire actually
2164 // negotiated (always a real PCM, never IEC958-only). Fall back to
2165 // safe stereo S16_LE if /proc lacks the fields.
2166 ✗ std::string synthFmt = "S16_LE";
2167 ✗ std::string synthCh = "2";
2168 ✗ std::smatch pm;
2169 // /proc reports the rate the driver actually settled on, which is
2170 // exactly what the graph clock needs and costs no probe.
2171 ✗ if (std::regex_search(procHw, pm, std::regex(R"(rate:\s*(\d+))"))) {
2172 ✗ liveRate = std::stoi(pm[1].str());
2173 }
2174 ✗ if (std::regex_search(procHw, pm, std::regex(R"(format:\s*(\S+))"))) {
2175 ✗ synthFmt = pm[1].str();
2176 }
2177 ✗ if (std::regex_search(procHw, pm, std::regex(R"(channels:\s*(\d+))"))) {
2178 ✗ synthCh = pm[1].str();
2179 }
2180 hwParams = "HW Params of device (busy — synthesised from /proc)\n"
2181 ✗ "FORMAT: " + synthFmt + "\n"
2182 ✗ "CHANNELS: " + synthCh + "\n";
2183 ✗ paramsFromLiveDevice = true;
2184 ✗ printf("FPP - PipeWire: card %d (%s) busy (held by PipeWire); using live params FORMAT=%s CHANNELS=%s\n",
2185 cardNum, cId.c_str(), synthFmt.c_str(), synthCh.c_str());
2186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 }
2187 // Verify card supports at least one standard PCM format
2188
8/22
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 26 times.
✓ Branch 10 taken 29 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 29 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
64 bool hasPcmFormat = contains(hwParams, "S16_LE") || contains(hwParams, "S24_LE")
2189
20/34
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 3 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 2 times.
✓ Branch 15 taken 1 times.
✓ Branch 16 taken 3 times.
✓ Branch 17 taken 26 times.
✓ Branch 18 taken 3 times.
✓ Branch 19 taken 26 times.
✓ Branch 20 taken 3 times.
✓ Branch 21 taken 26 times.
✓ Branch 22 taken 3 times.
✓ Branch 23 taken 26 times.
✓ Branch 24 taken 3 times.
✓ Branch 25 taken 26 times.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
70 || contains(hwParams, "S24_3LE") || contains(hwParams, "S32_LE");
2190
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28 times.
29 if (!hasPcmFormat) {
2191
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("FPP - PipeWire: skipping card %d (%s) — no standard PCM format (IEC958 only?)\n",
2192 cardNum, cId.c_str());
2193 1 continue;
2194 }
2195
2196
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 std::string productName = getAlsaCardProductName(cardNum, cardName);
2197
3/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
28 std::string desc = productName + " (" + cId + ")";
2198
2199 // USB audio cards need extra headroom and use different channel-detection logic.
2200
5/8
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 23 times.
28 bool isUsbCard = cardLines.count(key) && cardLines[key].find("USB Audio") != std::string::npos;
2201
2202 // Detect playback channels from ALSA HW params.
2203 // Formats: "CHANNELS: 8" (fixed), "CHANNELS[2]: 2 8" (discrete
2204 // list), "CHANNELS: [1 8]" (continuous range).
2205 //
2206 // A continuous range like "[1 8]" means the driver accepts any
2207 // count in that range — it does NOT imply that many physical
2208 // outputs. The Pi onboard analog (bcm2835) reports "[1 8]"
2209 // because it has 8 hardware mixer subdevices, but it is a
2210 // 2-channel stereo jack. Opening it as 8ch feeds interleaved
2211 // 8-channel data to a stereo DAC → garbled, high-pitched noise.
2212 // So for non-USB cards with a range we default to stereo;
2213 // only fixed/discrete declarations use the largest value.
2214 28 int maxChannels = 2;
2215 28 std::smatch chMatch;
2216
3/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
28 if (std::regex_search(hwParams, chMatch, std::regex(R"(CHANNELS\[?\d*\]?:\s+(.+))"))) {
2217
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 std::string chLine = chMatch[1].str();
2218 28 bool isRange = chLine.find('[') != std::string::npos;
2219
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 std::regex numRe(R"(\d+)");
2220
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 std::sregex_iterator it(chLine.begin(), chLine.end(), numRe);
2221
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 std::sregex_iterator end;
2222 28 std::vector<int> nums;
2223
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 28 times.
60 for (; it != end; ++it) {
2224
5/10
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 32 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 32 times.
✗ Branch 9 not taken.
32 nums.push_back(std::stoi((*it)[0].str()));
2225 }
2226
5/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 24 times.
28 if (isRange && nums.size() >= 2) {
2227 4 int lo = nums.front();
2228 4 int hi = nums.back();
2229
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (isUsbCard) {
2230 // USB audio: a range [lo hi] covers actual multi-channel
2231 // altsets — use the maximum.
2232 1 maxChannels = hi;
2233 } else {
2234 // Non-USB (e.g. bcm2835 analog): range is misleading,
2235 // default to stereo clamped into the range.
2236 3 maxChannels = std::min(std::max(2, lo), hi);
2237 }
2238 } else {
2239 // Fixed or discrete list: take the largest declared count.
2240
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
48 for (int ch : nums) {
2241
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23 times.
24 if (ch > maxChannels) maxChannels = ch;
2242 }
2243 }
2244 28 }
2245 // For USB audio also read /proc/asound/cardN/stream0 which lists
2246 // every playback altset with its exact channel count. This catches
2247 // cases where aplay reports a range or probes only one altset.
2248
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 23 times.
28 if (isUsbCard) {
2249
3/6
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 std::string stream0 = GetFileContents("/proc/asound/card" + std::to_string(cardNum) + "/stream0");
2250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!stream0.empty()) {
2251 // Only look in the Playback section (stop before Capture:)
2252 ✗ size_t capturePos = stream0.find("\nCapture:");
2253 ✗ std::string playSection = (capturePos != std::string::npos) ? stream0.substr(0, capturePos) : stream0;
2254 ✗ std::regex chRe(R"(\bChannels:\s+(\d+))");
2255 ✗ std::sregex_iterator it(playSection.begin(), playSection.end(), chRe);
2256 ✗ std::sregex_iterator end;
2257 ✗ for (; it != end; ++it) {
2258 ✗ int ch = std::stoi((*it)[1].str());
2259 ✗ if (ch > maxChannels) maxChannels = ch;
2260 }
2261 ✗ }
2262 5 }
2263 // Quirk override: known multi-channel I2S cards report a continuous
2264 // range and would be clamped to stereo above; a busy card can also
2265 // synthesise a stale 2ch value from /proc. Either way the quirk
2266 // count wins (issue #2620: HiFiBerry DAC8x stuck at 2 channels).
2267
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 if (cardLines.count(key)) {
2268
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 int quirkCh = quirkMinChannelsForCard(cardLines[key]);
2269
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 26 times.
28 if (quirkCh > maxChannels) {
2270
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 printf("FPP - PipeWire: card %d (%s) quirk raises channels %d -> %d\n",
2271 cardNum, cId.c_str(), maxChannels, quirkCh);
2272 2 maxChannels = quirkCh;
2273 }
2274 }
2275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (maxChannels > 8) maxChannels = 8; // cap at 7.1
2276
2277 // Detect best audio format from ALSA HW params
2278 // FORMAT line examples: "FORMAT: S16_LE S24_3LE", "FORMAT: S16_LE S24_LE S32_LE"
2279 // Priority: S32 > S24 > S16. ALSA uses _ (S24_3LE), PipeWire drops it (S24LE).
2280 // A wider format only counts if the card can still reach the target
2281 // rate in it — fixed-bit-clock I2S cards advertise S32_LE but reach
2282 // it only at half the rate (see bestFormatForRate).
2283
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 std::string audioFormat = "S16LE"; // legacy fallback when no PCM format is found
2284 28 std::smatch fmtMatch;
2285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (paramsFromLiveDevice) {
2286 // The single format in /proc is not an advertisement, it is what
2287 // the hardware is running right now -- the very thing the probe
2288 // exists to establish, already established. Probing it again is
2289 // worse than redundant: formatHoldsRate() has to open the device,
2290 // gets the same EBUSY that forced this synthesis in the first
2291 // place, and (correctly, by its own bias-to-safe rule) reports
2292 // "unverifiable" -- silently rewriting a working S32 card as
2293 // S16LE. PipeWire holds the card on every postNetwork run, so
2294 // that is not an edge case, it is the normal path for the
2295 // selected output; measured on an AM62x PCM5102A cape running
2296 // S32_LE, which the probe demoted on every regeneration.
2297 ✗ if (std::regex_search(hwParams, fmtMatch, std::regex(R"(FORMAT[^:]*:\s+(\S+))"))) {
2298 ✗ audioFormat = pipewireFormatName(fmtMatch[1].str());
2299 }
2300
3/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
28 } else if (std::regex_search(hwParams, fmtMatch, std::regex(R"(FORMAT[^:]*:\s+(.+))"))) {
2301
4/8
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
56 audioFormat = bestFormatForRate(fmtMatch[1].str(), "hw:" + cId,
2302 28 pipewireSampleRate, maxChannels);
2303 }
2304
2305 // Channel position arrays matching PipeWire convention
2306 static const char* positionArrays[] = {
2307 nullptr,
2308 "[ MONO ]",
2309 "[ FL FR ]",
2310 "[ FL FR FC ]",
2311 "[ FL FR RL RR ]",
2312 "[ FL FR FC RL RR ]",
2313 "[ FL FR FC LFE RL RR ]",
2314 "[ FL FR FC LFE RL RR RC ]",
2315 "[ FL FR FC LFE RL RR SL SR ]"
2316 };
2317
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 const char* posStr = (maxChannels >= 1 && maxChannels <= 8) ? positionArrays[maxChannels] : "[ FL FR ]";
2318
2319 // The graph clock follows the card FPP actually outputs on. Only that
2320 // one matters: there is a single graph rate, and any other card is
2321 // resampled to it regardless. Prefer the live rate off an already-open
2322 // device (free, and it is the negotiated truth); otherwise ask the
2323 // device what it would settle on for the format just chosen, which is
2324 // the only way to see an upward refinement -- --dump-hw-params reports
2325 // the unrefined capability space and ignores -r entirely.
2326
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (cardNum == card) {
2327 int achieved = liveRate > 0
2328
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 ? liveRate
2329
3/8
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
28 : achievedRateForFormat("hw:" + cId, audioFormat, pipewireSampleRate, maxChannels);
2330
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
28 if (achieved > 0 && achieved != graphClockRate) {
2331 ✗ printf("FPP - PipeWire: card %d (%s) clocks at %d, not the configured %d; "
2332 "running the graph at %d to avoid resampling\n",
2333 cardNum, cId.c_str(), achieved, pipewireSampleRate, achieved);
2334 }
2335
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (achieved > 0) {
2336 28 graphClockRate = achieved;
2337 }
2338 }
2339
2340 // See kUsbHeadroomSoleSink: a USB card only pays the drift headroom
2341 // when it shares a group with another sink and so may have to follow
2342 // that sink's clock. Alone in its group it is the graph driver, and
2343 // the extra frames are latency bought for nothing.
2344
4/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
28 const bool usbSharesGroup = isUsbCard && sharedGroupUsbCids.count(cidNorm) > 0;
2345 28 int headroom = kHeadroomNonUsb;
2346
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 23 times.
28 if (isUsbCard) {
2347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 headroom = usbSharesGroup ? kUsbHeadroomSharedGroup : kUsbHeadroomSoleSink;
2348 }
2349
2350 28 std::string usbNote;
2351
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 23 times.
28 if (isUsbCard) {
2352
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 usbNote = std::string(" (USB, ") +
2353
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
15 (usbSharesGroup ? "shares a group" : "sole sink in its group") +
2354
3/6
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
15 ", headroom=" + std::to_string(headroom) + ")";
2355 }
2356
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 printf("FPP - PipeWire: creating adapter fpp_alsa_%s for card %d (%s) [%dch %s]%s\n",
2357 cidNorm.c_str(), cardNum, cId.c_str(), maxChannels, audioFormat.c_str(),
2358 usbNote.c_str());
2359 pipewireSink << " { factory = adapter\n"
2360 << " args = {\n"
2361 << " factory.name = api.alsa.pcm.sink\n"
2362 << " node.name = \"fpp_alsa_" << cidNorm << "\"\n"
2363 << " node.description = \"" << desc << "\"\n"
2364 << " media.class = \"Audio/Sink\"\n"
2365 << " api.alsa.path = \"hw:" << cId << "\"\n"
2366
15/30
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 28 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 28 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 28 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 28 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 28 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 28 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 28 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 28 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 28 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 28 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 28 times.
✗ Branch 29 not taken.
28 << " api.alsa.period-size = " << perSize << "\n"
2367
3/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
28 << " api.alsa.headroom = " << headroom << "\n"
2368 << " audio.format = \"" << audioFormat << "\"\n"
2369
6/12
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 28 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 28 times.
✗ Branch 11 not taken.
28 << " audio.rate = " << pipewireSampleRate << "\n"
2370
3/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
28 << " audio.channels = " << maxChannels << "\n"
2371 << " audio.position = " << posStr << "\n"
2372 << " }\n"
2373 << " " << kNoFailFlag << "\n"
2374
9/18
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 28 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 28 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 28 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 28 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 28 times.
✗ Branch 17 not taken.
28 << " }\n";
2375
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 bootAdapterCids.insert(cidNorm);
2376 // If this card has capture capability, also create an Audio/Source node.
2377
3/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 28 times.
28 if (arecordAll.find("card " + std::to_string(cardNum) + ":") != std::string::npos) {
2378 // Detect capture channel count from ALSA HW params
2379 ✗ int capChannels = 2; // safe default — most USB cards need at least 2
2380 ✗ std::string capParams = execAndReturn("timeout 2 /usr/bin/arecord -D hw:" + cId + " --dump-hw-params /dev/zero 2>&1 | head -40");
2381 ✗ if (contains(capParams, "HW Params")) {
2382 ✗ std::smatch capChMatch;
2383 ✗ if (std::regex_search(capParams, capChMatch, std::regex(R"(CHANNELS\[?\d*\]?:\s+(.+))"))) {
2384 ✗ std::string capChLine = capChMatch[1].str();
2385 // Find the smallest number (minimum channels)
2386 ✗ std::regex capNumRe(R"(\d+)");
2387 ✗ std::sregex_iterator capIt(capChLine.begin(), capChLine.end(), capNumRe);
2388 ✗ std::sregex_iterator capEnd;
2389 ✗ int minCap = 99;
2390 ✗ for (; capIt != capEnd; ++capIt) {
2391 ✗ int c = std::stoi((*capIt)[0].str());
2392 ✗ if (c > 0 && c < minCap) minCap = c;
2393 }
2394 ✗ if (minCap > 0 && minCap < 99) capChannels = minCap;
2395 ✗ }
2396 ✗ }
2397 ✗ printf("FPP - PipeWire: card %d (%s) has capture [%dch], creating source node\n",
2398 cardNum, cId.c_str(), capChannels);
2399 pipewireSink << " { factory = adapter\n"
2400 << " args = {\n"
2401 << " factory.name = api.alsa.pcm.source\n"
2402 << " node.name = \"fpp_alsain_" << cidNorm << "\"\n"
2403 << " node.description = \"" << desc << "\"\n"
2404 << " node.nick = \"" << cId << "\"\n"
2405 << " media.class = \"Audio/Source\"\n"
2406 << " api.alsa.path = \"hw:" << cId << "\"\n"
2407 << " audio.format = \"" << audioFormat << "\"\n"
2408 << " audio.rate = 44100\n"
2409 ✗ << " audio.channels = " << capChannels << "\n"
2410 << " }\n"
2411 << " " << kNoFailFlag << "\n"
2412 ✗ << " }\n";
2413 ✗ }
2414
6/6
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 28 times.
✓ Branch 5 taken 45 times.
163 }
2415
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 pipewireSink << "]\n";
2416 // Assembled here rather than streamed from the top: the clock rate is only
2417 // known once the selected card has been probed. Section order does not
2418 // matter to SPA-JSON, but the generation tag must stay the first line --
2419 // the cache check above matches it as a prefix.
2420 //
2421 // Whenever the graph rate differs from the device rate the ALSA sink
2422 // adapter resamples on every cycle, for as long as the graph runs, not
2423 // just while media is playing. The daemon default in 90-fpp.conf is
2424 // 48000; this file sorts after it, so the value set here wins. Measured
2425 // per graph cycle in the sink node: 478us -> 171us on a 44100 I2S cape,
2426 // 538us -> 107us on a 44100 USB card, 779us -> 86us on an 88200 cape.
2427 // allowed-rates is deliberately left to 90-fpp.conf -- a clock rate
2428 // outside that list is honoured anyway (verified with 88200 against the
2429 // stock [44100 48000 96000]), and narrowing it here would only forbid
2430 // switches that already do not happen.
2431
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 std::ostringstream sinkConf;
2432
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 sinkConf << alsaSinkConfGenerationTag() << "\n"
2433 // What AudioFormat asked for, which default.clock.rate below no
2434 // longer records once a cape refines it. See alsaSinkConfRateTag().
2435 111 << alsaSinkConfRateTag(pipewireSampleRate) << "\n"
2436 // What AudioPeriodSize asked for, which nothing else in the
2437 // file records. See alsaSinkConfPeriodSizeTag().
2438 111 << alsaSinkConfPeriodSizeTag(perSize) << "\n"
2439 // The cards considered, including any the probe then skipped as
2440 // unusable. See alsaSinkConfCardsTag().
2441 111 << alsaSinkConfCardsTag(adapterCandidateCids) << "\n"
2442 // Which USB cards were charged the follower headroom, which
2443 // group membership can change without the card set changing.
2444 // See alsaSinkConfUsbHeadroomTag().
2445 111 << alsaSinkConfUsbHeadroomTag(sharedGroupUsbCids) << "\n"
2446 << "context.properties = {\n"
2447
17/34
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 111 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 111 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 111 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 111 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 111 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 111 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 111 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 111 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 111 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 111 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 111 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 111 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 111 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 111 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 111 times.
✗ Branch 31 not taken.
✓ Branch 32 taken 111 times.
✗ Branch 33 not taken.
111 << " default.clock.rate = " << graphClockRate << "\n"
2448 << "}\n"
2449
4/8
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 111 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 111 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 111 times.
✗ Branch 7 not taken.
111 << pipewireSink.str();
2450
2/4
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 111 times.
✗ Branch 3 not taken.
111 if (sinkConf.str() != existingSinkConf) {
2451
2/4
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 111 times.
✗ Branch 3 not taken.
111 PutFileContents(pipewireSinkConfPath, sinkConf.str());
2452 // PipeWire only reads this file at startup; a card add/remove (or a
2453 // capability change) rewrites it, so flag a restart below to load it.
2454 111 sinkConfigChanged = true;
2455 }
2456
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
111 } else if (FileExists(pipewireSinkConfPath)) {
2457 ✗ unlink(pipewireSinkConfPath.c_str());
2458 ✗ sinkConfigChanged = true;
2459 }
2460 // Stop WirePlumber from double-opening the cards we just declared adapters
2461 // for. Runs on the skip-probe path too: the rules file is what makes the
2462 // static adapters the sole owner of each PCM, and an existing install has
2463 // never had it written. Rules are read only at WirePlumber start, so fold a
2464 // change into sinkConfigChanged to pick up the restart below.
2465
6/8
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✓ Branch 5 taken 87 times.
✓ Branch 6 taken 40 times.
✓ Branch 7 taken 87 times.
127 if (usePipeWireBackend && ensureWirePlumberAlsaDupeSuppression(adapterCardNames)) {
2466
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 printf("FPP - PipeWire: suppressing WirePlumber duplicate ALSA devices for %d card(s)\n",
2467 40 (int)adapterCardNames.size());
2468 40 sinkConfigChanged = true;
2469 }
2470
3/6
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 127 times.
✗ Branch 5 not taken.
127 std::string mixers = execAndReturn("/usr/bin/amixer -c " + std::to_string(card) + " scontrols | cut -f2 -d\"'\"");
2471
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 58 times.
127 if (mixers.empty()) {
2472 // for some sound cards, the mixer devices won't show up
2473 // until something is played. Play one second of silence
2474
2/4
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
69 exec("/usr/bin/aplay -d 1 /opt/fpp/media/silence_5sec.wav >> /dev/null 2>&1 &");
2475
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 std::this_thread::sleep_for(std::chrono::milliseconds(200));
2476
3/6
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 69 times.
✗ Branch 5 not taken.
69 mixers = execAndReturn("/usr/bin/amixer -c " + std::to_string(card) + " scontrols | cut -f2 -d\"'\"");
2477 }
2478
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 TrimWhiteSpace(mixers);
2479 127 std::string mixer;
2480
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
127 getRawSetting("AudioMixerDevice", mixer);
2481
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 std::string origMixer = mixer;
2482
7/8
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 26 times.
✓ Branch 6 taken 101 times.
✓ Branch 7 taken 26 times.
127 if (mixer.empty() || !contains(mixers, mixer)) {
2483
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 mixer = mixers;
2484
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 100 times.
101 if (mixer.find("\n") != std::string::npos) {
2485
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 mixer = mixer.substr(0, mixer.find("\n"));
2486 }
2487 }
2488
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 95 times.
127 if (mixer != origMixer) {
2489
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 printf("FPP - Setting mixer device to %s\n", mixer.c_str());
2490
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
64 setRawSetting("AudioMixerDevice", mixer);
2491 }
2492 // Quoted for the shell: control names have spaces ("PCM Playback"), and
2493 // AudioMixerDevice is a setting.
2494
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 std::string quotedMixer = "'";
2495
2/2
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 127 times.
339 for (char ch : mixer) {
2496
5/16
✗ Branch 0 not taken.
✓ Branch 1 taken 212 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 212 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 212 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 212 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 212 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
636 quotedMixer += (ch == '\'') ? std::string("'\\''") : std::string(1, ch);
2497 }
2498
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 quotedMixer += "'";
2499
7/14
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 127 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 127 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 127 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 127 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 127 times.
✗ Branch 13 not taken.
127 exec("/usr/bin/amixer -c " + std::to_string(card) + " set " + quotedMixer + " " + std::to_string(v) + "% > /dev/null 2>&1");
2500
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
127 setRawSetting("AudioCardType", cardType);
2501
2502
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 52 times.
127 if (!runningInDocker) {
2503 75 mkdir("/run/fppd", 0755);
2504
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 std::ostringstream audioEnv;
2505
4/8
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 75 times.
✗ Branch 7 not taken.
75 audioEnv << "SDL_AUDIODRIVER=" << (usePipeWireBackend ? "pulse" : "alsa") << "\n";
2506 // Keep OpenAL on the same backend as the rest of FPP audio.
2507 // In hardware (ALSA) mode this prevents OpenAL from defaulting to
2508 // PulseAudio and aborting during teardown if Pulse is unavailable.
2509
4/8
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 75 times.
✗ Branch 7 not taken.
75 audioEnv << "ALSOFT_DRIVERS=" << (usePipeWireBackend ? "pulse" : "alsa") << "\n";
2510
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 if (usePipeWireBackend) {
2511 audioEnv << "PIPEWIRE_RUNTIME_DIR=/run/pipewire-fpp\n"
2512 << "XDG_RUNTIME_DIR=/run/pipewire-fpp\n"
2513 << "PIPEWIRE_CONFIG_DIR=/etc/pipewire\n"
2514
4/8
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 75 times.
✗ Branch 7 not taken.
75 << "PULSE_RUNTIME_PATH=/run/pipewire-fpp/pulse\n";
2515 }
2516
2/4
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
75 PutFileContents(audioEnvPath, audioEnv.str());
2517
2/4
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
127 } else if (FileExists(audioEnvPath)) {
2518 ✗ unlink(audioEnvPath.c_str());
2519 }
2520
2521 // --- Audio Output Groups Configuration ---
2522 // On first PipeWire setup, create default output and input group configs
2523 // that mirror the legacy ALSA configuration: one output group with the
2524 // configured sound card, and one input group with fppd_stream_1 routed
2525 // to it. This ensures audio works immediately when switching to PipeWire.
2526
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 const std::string groupsJsonPath = FPP_MEDIA_DIR + "/config/pipewire-audio-groups.json";
2527
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 const std::string simpleGroupsJsonPath = FPP_MEDIA_DIR + "/config/pipewire-audio-groups-simple.json";
2528
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 const std::string igJsonPath = FPP_MEDIA_DIR + "/config/pipewire-input-groups.json";
2529
2530 // Simple PipeWire mode: the active config lives in pipewire-audio-groups-simple.json
2531 // and PipeWireSinkName is written by ApplyPipeWireSimpleConfig(). If that file is
2532 // missing (e.g. OS upgrade, fresh flash, or settings migrated without an explicit
2533 // UI save) synthesise it now from the current AudioOutput/VideoOutput settings so
2534 // PipeWireSinkName is populated before fppd starts.
2535 // Regenerate the simple groups config when it's missing OR when it
2536 // references a sound card that's no longer present. The latter is the
2537 // card-changed case (e.g. a USB card added to a previously dummy-only board,
2538 // or removed): the stale config would otherwise leave an unresolved device,
2539 // producing a "# WARNING:" conf that (a) is wrong and (b) defeats the
2540 // skip-regen fast path, forcing the full regen+restart dance on every boot.
2541 // Regenerating here re-points it at the current AudioOutput card.
2542 // The selected output card as a stable ALSA card ID (derived from the
2543 // AudioOutput setting above). The Simple config must point at this card.
2544
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 std::string selCid = getAlsaCardId(card);
2545
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
127 if (selCid.empty()) selCid = "Dummy"; // an empty ID only happens with no real card
2546 // Regenerate the simple config when it is missing, references an absent card,
2547 // OR points at a different card than the currently-selected output. The last
2548 // case is the post-upgrade / settings-changed scenario: e.g. AudioOutput is a
2549 // USB card (card 1) but the cached config still names the onboard card 0,
2550 // which is present so the cards-present check alone would wrongly skip it.
2551 254 bool simpleConfigStale = !FileExists(simpleGroupsJsonPath)
2552
3/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 4 times.
27 || !pipewireConfigCardsPresent(simpleGroupsJsonPath)
2553
8/12
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 100 times.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 22 times.
✓ Branch 8 taken 23 times.
✓ Branch 9 taken 104 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
154 || simpleConfigCardId(simpleGroupsJsonPath) != selCid;
2554 // Upgrade case for boards with no sound card: an install configured by an
2555 // older FPP has a cached config holding the full hw:Dummy adapter ->
2556 // filter-chain -> combine-stream graph, which never reaches idle and so
2557 // cycles PipeWire at the quantum rate forever (~47 wakeups/s, ~4% of a core
2558 // on a single-core PocketBeagle) for audio that can never be heard.
2559 // None of the tests above catch it: the cached card ID is still "Dummy" so
2560 // it matches selCid, and snd-dummy was modprobed earlier this boot so
2561 // /proc/asound/cards lists [Dummy] and the cards-present test passes too.
2562 // The config is therefore never regenerated and the board keeps the old
2563 // graph on every boot. Force exactly one regeneration when the active conf
2564 // predates the null-sink form. Self-limiting: the regenerated conf contains
2565 // support.null-audio-sink, so this cannot drive a regen/restart loop.
2566
7/8
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 105 times.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 17 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 1 times.
132 if (!simpleConfigStale && selCid == "Dummy" &&
2567
10/18
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 122 times.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 122 times.
✓ Branch 8 taken 5 times.
✓ Branch 9 taken 122 times.
✓ Branch 10 taken 4 times.
✓ Branch 11 taken 123 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
137 GetFileContents("/etc/pipewire/pipewire.conf.d/97-fpp-audio-groups.conf")
2568 5 .find("support.null-audio-sink") == std::string::npos) {
2569
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 printf("FPP - No sound card: audio config predates the null sink, regenerating\n");
2570 4 simpleConfigStale = true;
2571 }
2572 // 97-fpp-audio-groups.conf and 95-fpp-alsa-sink.conf can end up declaring
2573 // the same fpp_alsa_* node for the same card, and none of the tests above
2574 // notice: they all ask about the *card*, which has not changed.
2575 //
2576 // buildSimplePipeWireGroupsConf() emits its own adapter only when the card
2577 // had no boot adapter at the time (cardHasBootAdapter, from bootAdapterCids
2578 // below). That answer goes stale as soon as 95 is regenerated with the card
2579 // present -- a USB card that enumerates after the first pass, or the
2580 // recovery pass re-running this whole function -- and nothing re-runs the
2581 // simple generator afterwards to hand ownership back.
2582 //
2583 // The result is two adapters on one PCM under one node.name. That is the
2584 // failure ensureWirePlumberAlsaDupeSuppression() exists to prevent from the
2585 // WirePlumber side: the loser's snd_pcm_open() returns EBUSY, its node goes
2586 // to error, and a stream that landed on it blocks in poll() forever. The
2587 // rule there cannot see this one, because both declarations are static.
2588 //
2589 // Self-limiting: the regenerated conf omits the adapter (the card now has a
2590 // boot adapter), so this cannot hold on the following pass.
2591
3/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 1 times.
18 if (!simpleConfigStale && selCid != "Dummy" &&
2592
8/12
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 109 times.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 16 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 125 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
294 bootAdapterCids.count(normalizeCardIdForNode(selCid)) > 0 &&
2593
11/20
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 14 times.
✓ Branch 8 taken 16 times.
✓ Branch 9 taken 111 times.
✓ Branch 10 taken 16 times.
✓ Branch 11 taken 111 times.
✓ Branch 12 taken 16 times.
✓ Branch 13 taken 111 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
163 contains(GetFileContents("/etc/pipewire/pipewire.conf.d/97-fpp-audio-groups.conf"),
2594
11/20
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 16 times.
✓ Branch 7 taken 111 times.
✓ Branch 8 taken 16 times.
✓ Branch 9 taken 111 times.
✓ Branch 10 taken 16 times.
✓ Branch 11 taken 111 times.
✓ Branch 12 taken 17 times.
✓ Branch 13 taken 110 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
143 "node.name = \"fpp_alsa_" + normalizeCardIdForNode(selCid) + "\"")) {
2595
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 printf("FPP - Simple audio config duplicates the boot ALSA adapter for %s; regenerating\n",
2596 selCid.c_str());
2597 2 simpleConfigStale = true;
2598 }
2599 // The selected card's filter-chain has to play into fpp_alsa_<card> once FPP
2600 // owns the card, and nothing above checks where it actually points.
2601 //
2602 // A card selected while WirePlumber still served it (hot-plugged after boot,
2603 // so not yet in 95) is resolved by the PHP apply to WirePlumber's
2604 // alsa_output.* node -- correct at that moment. The next run here adds the
2605 // card to 95 and disables its WirePlumber device, so that node never exists
2606 // again and the filter-chain output stays unlinked: silent, no error. The
2607 // run that takes ownership regenerates (ownershipChanged below), but a conf
2608 // already in this state is not caught by anything else: the card is present
2609 // and selected, and the Simple JSON records no nodeTarget for
2610 // pipewireConfigSuppressedTarget() to inspect. So read the conf itself.
2611 //
2612 // Self-limiting: both generators target fpp_alsa_<card> for a card FPP owns,
2613 // so the regenerated conf passes this on the following run.
2614
3/6
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
16 if (!simpleConfigStale && usePipeWireBackend && mediaBackendLower == "pipewire-simple" &&
2615
8/14
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 15 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 125 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
143 selCid != "Dummy" && adapterCandidateCids.count(normalizeCardIdForNode(selCid)) > 0 &&
2616
5/6
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 111 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 3 times.
159 FileExists("/etc/pipewire/pipewire.conf.d/97-fpp-audio-groups.conf") &&
2617
11/20
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 10 times.
✓ Branch 8 taken 12 times.
✓ Branch 9 taken 115 times.
✓ Branch 10 taken 12 times.
✓ Branch 11 taken 115 times.
✓ Branch 12 taken 12 times.
✓ Branch 13 taken 115 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
155 !contains(GetFileContents("/etc/pipewire/pipewire.conf.d/97-fpp-audio-groups.conf"),
2618
11/20
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 12 times.
✓ Branch 7 taken 115 times.
✓ Branch 8 taken 12 times.
✓ Branch 9 taken 115 times.
✓ Branch 10 taken 12 times.
✓ Branch 11 taken 115 times.
✓ Branch 12 taken 15 times.
✓ Branch 13 taken 112 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
139 "node.target = \"fpp_alsa_" + normalizeCardIdForNode(selCid) + "\"")) {
2619
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 printf("FPP - Simple audio config does not target FPP's adapter for %s; regenerating\n",
2620 selCid.c_str());
2621 2 simpleConfigStale = true;
2622 }
2623 // Upgrade case for #3063: fppd's sink (fpp_group_default) carries
2624 // priority.session so WirePlumber picks it as the default when it has no
2625 // stored choice. Only a regenerated conf has that line, and a box that
2626 // already had working audio is updated and rebooted with its old conf,
2627 // which every test above passes (right card, present, targeted). It then
2628 // has only syncPipeWireDefaultSink() and no ranking behind it. Force one
2629 // regeneration. The null-sink form (no sound card) has nothing to rank.
2630 //
2631 // Self-limiting: both generators emit the line for the Simple group, so
2632 // the regenerated conf passes this on the following run.
2633
3/6
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
14 if (!simpleConfigStale && usePipeWireBackend && mediaBackendLower == "pipewire-simple" &&
2634
10/12
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 10 times.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 10 times.
✓ Branch 11 taken 117 times.
141 selCid != "Dummy" && FileExists("/etc/pipewire/pipewire.conf.d/97-fpp-audio-groups.conf")) {
2635
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 const std::string conf = GetFileContents("/etc/pipewire/pipewire.conf.d/97-fpp-audio-groups.conf");
2636
13/30
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 10 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 9 times.
✓ Branch 12 taken 10 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 10 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 10 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 10 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 times.
✓ Branch 21 taken 9 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
50 if (contains(conf, "node.name = \"fpp_group_default\"") && !contains(conf, "priority.session")) {
2637
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("FPP - Simple audio config predates the default-sink ranking; regenerating\n");
2638 1 simpleConfigStale = true;
2639 }
2640 10 }
2641
5/6
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 63 times.
✓ Branch 5 taken 12 times.
127 if (usePipeWireBackend && !runningInDocker && mediaBackendLower == "pipewire-simple"
2642
5/6
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 50 times.
✓ Branch 5 taken 77 times.
254 && simpleConfigStale) {
2643 // The simple config is missing or points at an absent card (fresh flash,
2644 // OS upgrade, or a USB card added/removed). It's always the auto-generated
2645 // single-group/single-card shape, so synthesise it directly in C++ rather
2646 // than forking the PHP (apply_pipewire_simple_config), which costs ~10-30s
2647 // of interpreter startup on a single-core SBC.
2648 //
2649 // Two cases keep the PHP for correctness rather than speed:
2650 // * VideoOutput is configured — apply_pipewire_simple_config also builds
2651 // the video output groups, which the C++ path doesn't cover.
2652 // * The shipped WirePlumber combine-fallback hook source isn't present
2653 // to copy (a broken checkout — normally can't happen) — defer to the
2654 // PHP apply path rather than generating a config without the hook.
2655 50 std::string videoOutput;
2656
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 getRawSetting("VideoOutput", videoOutput);
2657
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 TrimWhiteSpace(videoOutput);
2658
1/10
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
50 bool videoConfigured = !(videoOutput.empty() || videoOutput == "Disabled" || videoOutput == "--Default--");
2659
4/8
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 50 times.
✗ Branch 7 not taken.
50 if (!videoConfigured && ensureWirePlumberLinkingHook()) {
2660
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 bool cardHasBootAdapter = bootAdapterCids.count(normalizeCardIdForNode(selCid)) > 0;
2661
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 generateSimplePipeWireAudioConfig(card, selCid, cardHasBootAdapter, perSize, pipewireSampleRate);
2662 50 cppGeneratedSimpleConfig = true;
2663 } else {
2664 ✗ printf("FPP - pipewire-simple: simple groups config missing or references an absent card; regenerating from AudioOutput setting...\n");
2665 ✗ system("/usr/bin/php /opt/fpp/scripts/apply_pipewire_simple_config");
2666 }
2667 50 }
2668
2669
8/10
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 52 times.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 32 times.
✓ Branch 7 taken 43 times.
✓ Branch 8 taken 32 times.
✓ Branch 9 taken 95 times.
127 if (usePipeWireBackend && !runningInDocker && !FileExists(groupsJsonPath)) {
2670
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 std::string defCardId = getAlsaCardId(card);
2671
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 printf("FPP - First PipeWire setup: creating default output group (card %s) and input group\n", defCardId.c_str());
2672
2673 // Default output group: one group with the legacy audio device
2674
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 Json::Value ogRoot;
2675
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 Json::Value group;
2676
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 group["id"] = 1;
2677
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 group["name"] = "Main Output";
2678
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 group["enabled"] = true;
2679
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 Json::Value member;
2680
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 member["cardId"] = defCardId;
2681
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 member["channels"] = 2;
2682
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 member["delayMs"] = 0.0;
2683
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 group["members"] = Json::Value(Json::arrayValue);
2684
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 group["members"].append(member);
2685
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 ogRoot["groups"] = Json::Value(Json::arrayValue);
2686
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 ogRoot["groups"].append(group);
2687
2688
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 PutFileContents(groupsJsonPath, SaveJsonToString(ogRoot));
2689
2690 // Default input group: fppd_stream_1 routed to output group 1
2691
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 Json::Value igRoot;
2692
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 Json::Value ig;
2693
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 ig["id"] = 1;
2694
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 ig["name"] = "Mix Bus 1";
2695
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 ig["enabled"] = true;
2696
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 ig["channels"] = 2;
2697
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 ig["volume"] = 100;
2698
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 Json::Value igMember;
2699
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 igMember["type"] = "fppd_stream";
2700
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 igMember["sourceId"] = "fppd_stream_1";
2701
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 igMember["name"] = "Media Playback";
2702
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 igMember["volume"] = 100;
2703
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 igMember["mute"] = false;
2704
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 ig["members"] = Json::Value(Json::arrayValue);
2705
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 ig["members"].append(igMember);
2706
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 ig["outputs"] = Json::Value(Json::arrayValue);
2707
3/6
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
32 ig["outputs"].append(1);
2708
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 igRoot["inputGroups"] = Json::Value(Json::arrayValue);
2709
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 igRoot["inputGroups"].append(ig);
2710
2711
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 PutFileContents(igJsonPath, SaveJsonToString(igRoot));
2712 32 }
2713
2714 // Restore cached combine-stream and input group configs so group sinks
2715 // survive reboot. These are initially copied as-is so PipeWire starts
2716 // with the right module layout. After PipeWire is up and WirePlumber
2717 // has enumerated devices, a regeneration script re-resolves card→sink
2718 // mappings to handle card-number changes.
2719
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 const std::string groupsConfCache = FPP_MEDIA_DIR + "/config/pipewire-audio-groups.conf";
2720
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 const std::string groupsConfDest = "/etc/pipewire/pipewire.conf.d/97-fpp-audio-groups.conf";
2721
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 const std::string igConfCache = FPP_MEDIA_DIR + "/config/pipewire-input-groups.conf";
2722
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 const std::string igConfDest = "/etc/pipewire/pipewire.conf.d/96-fpp-input-groups.conf";
2723
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 const std::string vbanConfCache = FPP_MEDIA_DIR + "/config/pipewire-vban.conf";
2724
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 const std::string vbanConfDest = "/etc/pipewire/pipewire.conf.d/94-fpp-vban.conf";
2725 127 bool hasGroupsConfig = false;
2726 // Track whether the effective PipeWire config actually changes from what it
2727 // already loaded at boot. PipeWire (fpp-pipewire.service) starts during boot
2728 // with the persisted /etc/pipewire configs, so a restart in postNetwork is
2729 // only needed when something actually differs -- otherwise the costly
2730 // pipewire-pulse/WirePlumber/session-bus restart cascade buys nothing. On a
2731 // no-soundcard board the config is identical every boot, so this skips it.
2732 // A changed ALSA-sink adapter config (95-fpp-alsa-sink.conf, handled above)
2733 // needs the same restart as a changed groups config: PipeWire reads both only
2734 // at start. Seed the flag from it so a card add/remove isn't silently ignored.
2735 // A C++-generated Simple config (just written above) always needs the one
2736 // restart below to take effect — PipeWire started at boot without it.
2737
4/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 111 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 11 times.
127 bool audioConfigChanged = sinkConfigChanged || cppGeneratedSimpleConfig;
2738
8/10
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 52 times.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 67 times.
✓ Branch 7 taken 8 times.
✓ Branch 8 taken 67 times.
✓ Branch 9 taken 60 times.
127 if (usePipeWireBackend && !runningInDocker && FileExists(groupsConfCache)) {
2739
12/20
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 64 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 64 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 64 times.
✓ Branch 10 taken 64 times.
✓ Branch 11 taken 3 times.
✓ Branch 12 taken 64 times.
✓ Branch 13 taken 3 times.
✓ Branch 14 taken 3 times.
✓ Branch 15 taken 64 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
67 if (!FileExists(groupsConfDest) || GetFileContents(groupsConfCache) != GetFileContents(groupsConfDest)) {
2740
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 printf("FPP - Restoring PipeWire audio output groups config\n");
2741
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
3 exec("/bin/cp " + groupsConfCache + " " + groupsConfDest);
2742 3 audioConfigChanged = true;
2743 }
2744 67 hasGroupsConfig = true;
2745
7/10
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 52 times.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
✓ Branch 9 taken 52 times.
60 } else if (usePipeWireBackend && !runningInDocker && FileExists(groupsJsonPath)) {
2746 // JSON exists but no cached .conf yet (e.g. first-time default creation).
2747 // The regeneration script will generate the .conf from the JSON.
2748 8 hasGroupsConfig = true;
2749 8 audioConfigChanged = true;
2750
3/10
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 52 times.
52 } else if (usePipeWireBackend && !runningInDocker && FileExists(groupsConfDest)) {
2751 // JSON config was deleted but stale conf remains — clean up
2752 ✗ if (!FileExists(groupsJsonPath)) {
2753 ✗ unlink(groupsConfDest.c_str());
2754 ✗ audioConfigChanged = true;
2755 }
2756 }
2757
6/10
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 52 times.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 75 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 127 times.
127 if (usePipeWireBackend && !runningInDocker && FileExists(igConfCache)) {
2758 ✗ if (!FileExists(igConfDest) || GetFileContents(igConfCache) != GetFileContents(igConfDest)) {
2759 ✗ printf("FPP - Restoring PipeWire input groups config\n");
2760 ✗ exec("/bin/cp " + igConfCache + " " + igConfDest);
2761 ✗ audioConfigChanged = true;
2762 }
2763 }
2764
2765 // VBAN streams are PipeWire modules declared in 94-fpp-vban.conf, numbered
2766 // below the group confs so a received source and a send sink both exist
2767 // before 96/97 try to link them. Restore it the same way, and drop a stale
2768 // dest when the cache is gone so deleting every VBAN stream does not leave
2769 // the modules loading forever.
2770
3/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 52 times.
127 if (usePipeWireBackend && !runningInDocker) {
2771
2/4
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 75 times.
75 if (FileExists(vbanConfCache)) {
2772 ✗ if (!FileExists(vbanConfDest) || GetFileContents(vbanConfCache) != GetFileContents(vbanConfDest)) {
2773 ✗ printf("FPP - Restoring PipeWire VBAN config\n");
2774 ✗ exec("/bin/cp " + vbanConfCache + " " + vbanConfDest);
2775 ✗ audioConfigChanged = true;
2776 }
2777
2/4
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 75 times.
75 } else if (FileExists(vbanConfDest)) {
2778 ✗ unlink(vbanConfDest.c_str());
2779 ✗ audioConfigChanged = true;
2780 }
2781 }
2782
2783 // --- AES67 cleanup ---
2784 // AES67 is now managed by AES67Manager in fppd (GStreamer-based).
2785 // Remove any leftover PipeWire RTP module configs from the legacy Python approach.
2786 // Note the unlinks are all evaluated -- no short-circuiting -- so every stale
2787 // file goes even if an earlier one was already absent.
2788 127 bool hadLegacyAES67 = (unlink("/etc/pipewire/pipewire.conf.d/96-fpp-aes67-rtp.conf") == 0);
2789 127 hadLegacyAES67 |= (unlink("/etc/pipewire/pipewire.conf.d/96-fpp-aes67-sap.conf") == 0);
2790 127 hadLegacyAES67 |= (unlink("/etc/ptp4l-fpp.conf") == 0);
2791 // Kill any leftover legacy daemons -- but only when this boot actually found
2792 // legacy config to remove. The daemons were only ever launched from those
2793 // files, so once they are gone there is nothing to kill, and two `pkill -f`
2794 // scans (each a /bin/sh plus a walk of every /proc/*/cmdline) were costing
2795 // real time on a single-core board on every boot, forever, on the critical
2796 // path to fppd -- to hunt for processes belonging to a removed feature.
2797
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 126 times.
127 if (hadLegacyAES67) {
2798
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 exec("pkill -f fpp_aes67_sap 2>/dev/null || true");
2799
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 exec("pkill -f 'ptp4l.*ptp4l-fpp' 2>/dev/null || true");
2800 }
2801
2802 // PipeWire is already running (started at boot with the persisted configs),
2803 // so only restart it when the config actually changed. A restart is
2804 // synchronous and triggers an expensive pipewire-pulse/WirePlumber/
2805 // session-bus cascade, so skipping it on an unchanged boot (always the case
2806 // on a no-soundcard board) saves ~10-15s on a single-core SBC.
2807
3/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 52 times.
127 if (usePipeWireBackend && !runningInDocker) {
2808 // Keep the FPP PipeWire systemd units current. install_pipewire.sh
2809 // copies them once at install time and nothing ever refreshed them
2810 // afterwards, so unit fixes (e.g. the RestartSec/StartLimitIntervalSec
2811 // backoff that stops PipeWire from permanently giving up when a USB
2812 // sound card enumerates a few seconds late) never reached
2813 // already-installed systems. Only refresh units that are already
2814 // installed -- a missing unit means install_pipewire.sh hasn't run yet.
2815 75 bool unitsChanged = false;
2816
2/2
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 75 times.
300 for (const char* svc : { "fpp-pipewire.service", "fpp-wireplumber.service", "fpp-pipewire-pulse.service" }) {
2817
2/4
✓ Branch 0 taken 225 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 225 times.
✗ Branch 3 not taken.
450 std::string unitSrc = std::string("/opt/fpp/etc/systemd/") + svc;
2818
2/4
✓ Branch 0 taken 225 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 225 times.
✗ Branch 3 not taken.
225 std::string unitDst = std::string("/lib/systemd/system/") + svc;
2819
13/24
✓ Branch 0 taken 225 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 225 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 225 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 117 times.
✓ Branch 7 taken 108 times.
✓ Branch 8 taken 117 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 117 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 117 times.
✓ Branch 14 taken 117 times.
✓ Branch 15 taken 108 times.
✓ Branch 16 taken 117 times.
✓ Branch 17 taken 108 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 225 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
225 if (FileExists(unitSrc) && FileExists(unitDst) && GetFileContents(unitSrc) != GetFileContents(unitDst)) {
2820 ✗ printf("FPP - Updating systemd unit %s\n", svc);
2821 ✗ CopyFileContents(unitSrc, unitDst);
2822 ✗ unitsChanged = true;
2823 }
2824 225 }
2825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 if (unitsChanged) {
2826 ✗ exec("/usr/bin/systemctl daemon-reload");
2827 }
2828
2829 // The WirePlumber linking hook (blocks rogue default-target fallback
2830 // links for fpp_* nodes) is needed in ALL PipeWire modes, but was only
2831 // installed by the Simple-mode C++ path or a groups Apply from the UI.
2832 // A groups-mode box that never re-applied since the hook shipped runs
2833 // without it: a member stream whose target is missing (e.g. an fx
2834 // chain for a disconnected HDMI output) gets fallback-linked to the
2835 // default sink, double-opening that device and stalling the whole
2836 // graph mid-playback. Install it here and restart the stack when it
2837 // was newly installed (WirePlumber only reads hooks at startup).
2838
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 bool linkingHookPresent = FileExists("/usr/share/wireplumber/scripts/linking/fpp-block-combine-fallback.lua")
2839
4/6
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 68 times.
✗ Branch 5 not taken.
75 && FileExists("/etc/wireplumber/wireplumber.conf.d/60-fpp-block-combine-fallback.conf");
2840
6/8
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 68 times.
75 if (!linkingHookPresent && ensureWirePlumberLinkingHook()) {
2841
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 printf("FPP - Installed WirePlumber FPP linking hook\n");
2842 7 audioConfigChanged = true;
2843 }
2844
2845 // Validate/regenerate the audio group config from the JSON, and use its
2846 // exit code (2 == it changed/created the .conf) to decide whether a
2847 // restart is needed. Run it whenever there's a groups config -- NOT gated
2848 // on the dest already existing (chicken-and-egg: the regen is what creates
2849 // it).
2850 //
2851 // No real sound card: the synthetic snd-dummy can't go missing or shift
2852 // cards, so the cached .conf is permanently valid. Run WITHOUT --force so
2853 // the regen fast-exits (no pw-dump, no rewrite) when the cache is already
2854 // clean -- avoiding a full regenerate + PipeWire restart on every boot.
2855 // (--force is non-deterministic and reports "changed" every run, so it
2856 // would restart PipeWire every boot.) With a real card we keep --force so
2857 // a removed/shifted device is stripped before PipeWire opens it (PipeWire
2858 // crashes fatally on a missing ALSA device).
2859 // Set when the cheap C++ check below proves the cached group conf is
2860 // complete, already loaded, and references only present cards. Survives
2861 // that block so the post-start path can skip re-deriving the same answer
2862 // with a forked, forced PHP regeneration.
2863 75 bool cachedGroupsConfValidated = false;
2864
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 if (hasGroupsConfig) {
2865 // Decide whether the cached PipeWire group config is still valid in
2866 // C++ (cheap file reads) so we only fork the regeneration PHP -- which
2867 // costs ~3s of php/common.php startup, ~6s under boot contention --
2868 // when something actually changed. This covers BOTH no-soundcard and
2869 // real-card boards (e.g. BBB capes with onboard PCM5012A): skip the
2870 // PHP when the cached conf is complete (no unresolved-device warning),
2871 // matches what PipeWire loaded at boot (dest), and every sound card it
2872 // references is still present. The regen only runs on a genuine change
2873 // (first boot, settings edit, or a card added/removed), which is rare.
2874 75 bool needRegen = true;
2875
7/10
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 67 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 67 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 67 times.
✓ Branch 9 taken 8 times.
75 if (FileExists(groupsConfCache) && FileExists(groupsConfDest)) {
2876
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 std::string cached = GetFileContents(groupsConfCache);
2877 const std::string& activeJson =
2878
3/4
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 4 times.
67 (mediaBackendLower == "pipewire-simple") ? simpleGroupsJsonPath : groupsJsonPath;
2879 // The cached conf bakes in the passive-link choice, and neither
2880 // the card-presence check nor the dest comparison can see a
2881 // change to PipeWirePassiveSinks -- both sides stay consistent
2882 // with each other while disagreeing with the setting. Only this
2883 // file carries the marker; the input groups live in 96-*.conf.
2884
2/4
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
✗ Branch 3 not taken.
67 const bool wantPassive = getRawSettingInt("PipeWirePassiveSinks", 1) != 0;
2885 67 const bool cachedPassive = cached.find("node.passive = true") != std::string::npos;
2886 // A new 95 adapter or suppression rule changes which node names
2887 // can exist at all, so a conf resolved against the old set may
2888 // now target a node that is never coming back. The C++ Simple
2889 // generator just wrote its conf against the new set and needs no
2890 // second opinion.
2891
4/4
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 45 times.
67 const bool ownershipChanged = sinkConfigChanged && !cppGeneratedSimpleConfig;
2892
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 const std::string suppressedTarget = pipewireConfigSuppressedTarget(activeJson, adapterCandidateCids);
2893
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 65 times.
67 if (!suppressedTarget.empty()) {
2894
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 printf("FPP - PipeWire audio group targets %s, which FPP's own adapter replaces; regenerating\n",
2895 suppressedTarget.c_str());
2896 }
2897 134 if (cached.find("# WARNING:") == std::string::npos
2898
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 10 times.
67 && wantPassive == cachedPassive
2899
4/8
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
✓ Branch 5 taken 17 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
124 && cached == GetFileContents(groupsConfDest)
2900
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 5 times.
57 && !ownershipChanged
2901
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 2 times.
52 && suppressedTarget.empty()
2902
5/8
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 57 times.
✓ Branch 7 taken 10 times.
134 && pipewireConfigCardsPresent(activeJson)) {
2903 50 needRegen = false;
2904 // Remember this for the post-start block further down, which
2905 // otherwise re-runs the same resolution as a forced PHP regen.
2906 50 cachedGroupsConfValidated = true;
2907
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 printf("FPP - PipeWire audio config valid, loaded, and all referenced cards present; skipping regeneration\n");
2908 }
2909 67 }
2910
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 50 times.
75 if (needRegen) {
2911
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 std::string regenCmd = "/usr/bin/php /opt/fpp/scripts/regenerate_pipewire_groups";
2912
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 11 times.
25 if (!noRealSoundcard) {
2913
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 regenCmd += " --force";
2914 }
2915
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 printf("FPP - Validating PipeWire audio group config against current hardware...\n");
2916
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 int rc = system(regenCmd.c_str());
2917
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 17 times.
25 if (WEXITSTATUS(rc) == 2) {
2918 8 audioConfigChanged = true;
2919 }
2920 25 }
2921 }
2922
2923 // The skip-restart optimisation below assumes PipeWire is already
2924 // running (started at boot with the persisted configs). Verify that
2925 // assumption: with a pre-backoff unit file, a USB sound card that
2926 // enumerates a few seconds late makes PipeWire exhaust its start-limit
2927 // burst and give up early in boot (adapter creation is fatal on a
2928 // missing ALSA device), leaving the whole stack down. Skipping the
2929 // restart then silences all audio even though every config matches.
2930
2/4
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
75 std::string pwState = execAndReturn("/usr/bin/systemctl is-active fpp-pipewire.service");
2931
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 TrimWhiteSpace(pwState);
2932
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 bool pipewireRunning = (pwState == "active");
2933 // "inactive" here is a COLD START, not the crashed-stack case above: the FPP
2934 // PipeWire units ship disabled (nothing in the boot transaction pulls them
2935 // in -- postNetwork is what starts them), so on a stock box pwState is
2936 // "inactive" every single boot. Conflating that with failure made the
2937 // skip-restart fast path below dead code on exactly the boards it was
2938 // written for: every boot took the full restart + 3s settle + forced PHP
2939 // regenerate + PHP volume restore tail (~11s on a single-core BBB) purely to
2940 // re-derive a config the cheap C++ validation above had already proven
2941 // byte-identical and card-complete a few seconds earlier.
2942 // Starting the daemons now is in fact a *stronger* guarantee than the
2943 // already-running case: they read the configs that are on disk right now.
2944 // "failed"/"activating"/"deactivating" remain genuine recovery and still
2945 // take the slow path.
2946
4/6
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 62 times.
✗ Branch 5 not taken.
75 bool pipewireColdStart = !pipewireRunning && (pwState == "inactive");
2947 75 bool pipewireRestarted = false;
2948
3/4
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
75 if (!pipewireRunning && !pipewireColdStart) {
2949 ✗ printf("FPP - PipeWire service is '%s'; recovering audio stack\n", pwState.c_str());
2950 // Clear any start-limit lockout so the restart below can succeed.
2951 ✗ exec("/usr/bin/systemctl reset-failed fpp-pipewire.service fpp-wireplumber.service fpp-pipewire-pulse.service 2>/dev/null");
2952 }
2953 75 bool pipewireColdStarted = false;
2954
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6 times.
75 if (!audioConfigChanged && pipewireRunning) {
2955 // Config matches what PipeWire loaded at boot -- restarting would
2956 // change nothing (and the volume-restore below only exists to undo a
2957 // restart's reset of WirePlumber state, so it's unneeded too).
2958
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 printf("FPP - PipeWire audio config unchanged since boot; skipping restart\n");
2959
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
73 } else if (!audioConfigChanged && pipewireColdStart) {
2960 // Cold start with a config nothing has touched: bring the stack up and
2961 // stop there. A daemon that has not started yet cannot be holding a
2962 // stale config, so a restart-to-reload would be pure ceremony.
2963
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 exec("/usr/bin/systemctl start fpp-pipewire.service fpp-wireplumber.service fpp-pipewire-pulse.service");
2964 6 pipewireColdStarted = true;
2965
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 printf("FPP - PipeWire started with the unchanged on-disk config\n");
2966
3/4
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 55 times.
67 } else if (hasGroupsConfig && noRealSoundcard) {
2967 // Config changed but there's no real sound card: a restart is needed
2968 // to load it, but there are no USB cards to enumerate and no
2969 // card-number shifts to resolve, so skip the enumerate/regen dance.
2970
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 exec("/usr/bin/systemctl restart fpp-pipewire.service fpp-wireplumber.service fpp-pipewire-pulse.service");
2971 12 pipewireRestarted = true;
2972
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 printf("FPP - No real sound card present; skipping PipeWire device enumeration/regeneration\n");
2973 } else {
2974
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
55 exec("/usr/bin/systemctl restart fpp-pipewire.service fpp-wireplumber.service fpp-pipewire-pulse.service");
2975 55 pipewireRestarted = true;
2976 }
2977
2978 // Wait for WirePlumber to enumerate ALSA devices before regenerating
2979 // configs. WirePlumber needs a moment to discover USB sound cards
2980 // and create their PipeWire sink nodes.
2981 //
2982 // Skipped when we just generated the Simple config in C++: it's
2983 // self-contained (combine-stream/filter-chain target the static
2984 // fpp_alsa_* adapter loaded from 95/97 conf), so there are no card→sink
2985 // mappings to re-resolve via pw-dump and no need to fork the PHP
2986 // regenerate/restore-volume scripts. The single restart above is enough.
2987 //
2988 // Also skipped when the cheap C++ validation already proved the cached
2989 // conf byte-identical to what is being loaded and every card it names
2990 // present: the forced regen re-resolves precisely the card→node mapping
2991 // that check verified, so it can only ever conclude "unchanged" -- at a
2992 // cost of 3s of settle plus ~4.5s of PHP on a single-core BBB, on every
2993 // boot. That was the bulk of a 24s fpp_postnetwork.
2994
5/6
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 2 times.
75 bool audioStackTouched = audioConfigChanged || pipewireRestarted || pipewireColdStarted;
2995
9/10
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 73 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 20 times.
✓ Branch 7 taken 41 times.
✓ Branch 8 taken 13 times.
✓ Branch 9 taken 7 times.
75 if (audioStackTouched && hasGroupsConfig && !noRealSoundcard && !cppGeneratedSimpleConfig && !cachedGroupsConfValidated) {
2996
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 printf("FPP - Waiting for WirePlumber to enumerate devices...\n");
2997
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 std::this_thread::sleep_for(std::chrono::seconds(3));
2998
2999 // Regenerate output/input group configs using current device state.
3000 // This fixes card-number shifts after reboot and resolves any
3001 // previously unresolved members. Exit code 2 = config changed.
3002
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 printf("FPP - Regenerating PipeWire audio group configs...\n");
3003
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 int regenResult = system("/usr/bin/php /opt/fpp/scripts/regenerate_pipewire_groups --force");
3004 13 regenResult = WEXITSTATUS(regenResult);
3005
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (regenResult == 2) {
3006 // Config was changed — restart PipeWire to pick up the new config
3007 ✗ printf("FPP - PipeWire group config changed, restarting PipeWire...\n");
3008 ✗ exec("/usr/bin/systemctl restart fpp-pipewire.service fpp-wireplumber.service fpp-pipewire-pulse.service");
3009 // Counts as a restart for the fppd reconnect below: a live fppd's
3010 // cached daemon connection dies here just as surely as it does in
3011 // the restart paths above.
3012 ✗ pipewireRestarted = true;
3013 // Wait for WirePlumber & combine-stream/filter-chain nodes to come up
3014 ✗ std::this_thread::sleep_for(std::chrono::seconds(3));
3015 }
3016 }
3017
3018 // A board with a real sound card that reaches this point with no sink in
3019 // the graph is not merely slow: PipeWire aborts context creation and
3020 // exits when it cannot build a node the config declares, and systemd then
3021 // restarts it every few seconds forever. Nothing else notices -- every
3022 // check above was satisfied by a config that is internally consistent and
3023 // names only present cards; it is simply one the hardware rejects. Until
3024 // now the only trace at this level was a "timed out waiting for sinks"
3025 // line we then ignored, so the box booted clean and played no audio, on
3026 // every boot, until someone deleted the file by hand.
3027 //
3028 // The graph is the ground truth the config checks cannot reach, so use it:
3029 // discard the derived config and re-run the setup, which re-probes the
3030 // hardware from scratch. Bounded to a single retry by recoveryPass, and
3031 // cheap where it matters -- a healthy box gets one extra pactl on the
3032 // first poll, and only a box that is already silent pays the timeout.
3033 //
3034 // Only derived files go. pipewire-audio-groups.json is the user's own
3035 // configuration and is what the regeneration reads to rebuild the rest.
3036
10/12
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 61 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 63 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14 times.
✓ Branch 9 taken 49 times.
✓ Branch 10 taken 14 times.
✓ Branch 11 taken 61 times.
75 if (!noRealSoundcard && (audioStackTouched || pipewireRunning) && !waitForPipeWireSinks(30)) {
3037
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 if (!recoveryPass) {
3038
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 printf("FPP - PipeWire produced no sinks; discarding the derived audio config and re-probing\n");
3039 7 unlink(pipewireSinkConfPath.c_str());
3040
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 unlink((FPP_MEDIA_DIR + "/config/pipewire-audio-groups-simple.json").c_str());
3041 7 unlink(groupsConfCache.c_str());
3042
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 runAudioSetup(true);
3043 7 return;
3044 }
3045
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 printf("FPP - PipeWire still has no sinks after re-probing the audio hardware; audio will not work.\n");
3046
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 printf("FPP - Check 'journalctl -u fpp-pipewire' for the reason the daemon is failing to start.\n");
3047 }
3048
3049 // Per-group/per-member volume restore is needed whenever the stack came up
3050 // or changed under us. Deliberately NOT gated on the regeneration above:
3051 // this is orthogonal to whether the config resolved correctly. WirePlumber
3052 // reapplies its own persisted volume state every time it starts, so a cold
3053 // start needs the user's configured levels pushed back just as much as a
3054 // restart does -- gating it on the regen would silently leave a box booting
3055 // at whatever level WirePlumber last remembered.
3056 //
3057
7/8
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 54 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 19 times.
✓ Branch 7 taken 35 times.
68 if (audioStackTouched && hasGroupsConfig && !noRealSoundcard && !cppGeneratedSimpleConfig) {
3058
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 restorePipeWireVolumes();
3059 }
3060
3061 // Every boot, not only when the stack was touched: what WirePlumber
3062 // remembers is exactly what goes stale. On a healthy box the sinks are
3063 // already up and this costs two pactl calls.
3064
8/10
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 68 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 59 times.
✓ Branch 7 taken 9 times.
✓ Branch 8 taken 59 times.
✓ Branch 9 taken 9 times.
68 if ((audioStackTouched || pipewireRunning) && waitForPipeWireSinks(10)) {
3065
1/2
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
59 syncPipeWireDefaultSink();
3066 }
3067
3068 // fppd cannot survive the daemon being restarted underneath it. The
3069 // GStreamer PipeWire plugin caches its connection to the daemon process
3070 // wide, so once that connection dies every later pipewiresink reuses the
3071 // dead one: the pipeline reports PLAYING, no error is ever posted on the
3072 // bus, and playback is silent until the process restarts. Verified by
3073 // wedging a box this way and then playing the same pipeline from a
3074 // separate process, which worked -- the daemon and graph were healthy
3075 // and only fppd's own connection was dead.
3076 //
3077 // So whenever this run brought the stack up or down, restart fppd too.
3078 // Gated on fppd actually running, which at boot it is not: setupAudio runs
3079 // in postNetwork, before fppd starts, so a normal boot never takes this.
3080 // Only a later re-run -- an audio settings apply -- does.
3081 //
3082 // pipewireColdStarted counts as well: a stack that was down while fppd was
3083 // up leaves fppd's cached connection just as dead as a restart does, and
3084 // starting the daemons underneath it does not revive it.
3085 //
3086 // `fpp -r` rather than `systemctl restart fppd`: it asks fppd to re-exec
3087 // itself, which is an equally fresh process image -- that is what clears
3088 // the cached connection -- and it resumes a playlist that was running,
3089 // which systemctl cannot. It also keeps the PID, so repeated applies do
3090 // not burn the unit's start limit. Matches RestartPipeWireStack() in
3091 // pipewire.php, which is the same decision on the PHP side.
3092
6/6
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 67 times.
134 if ((pipewireRestarted || pipewireColdStarted) &&
3093
3/4
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 65 times.
66 system("/usr/bin/systemctl is-active --quiet fppd") == 0) {
3094
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("FPP - PipeWire was restarted under a running fppd; restarting fppd so it reconnects\n");
3095
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 exec("/opt/fpp/src/fpp -r");
3096 }
3097
3/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
195 } else if (!runningInDocker) {
3098 ✗ exec("/usr/bin/systemctl stop fpp-pipewire-pulse.service fpp-wireplumber.service fpp-pipewire.service");
3099 }
3100
3101 // AES67 is now initialized by AES67Manager in fppd after PipeWire is running.
3102 // No external daemons to start here.
3103
32/32
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 120 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 120 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 120 times.
✓ Branch 7 taken 7 times.
✓ Branch 8 taken 120 times.
✓ Branch 9 taken 7 times.
✓ Branch 10 taken 120 times.
✓ Branch 11 taken 7 times.
✓ Branch 12 taken 120 times.
✓ Branch 13 taken 7 times.
✓ Branch 14 taken 120 times.
✓ Branch 15 taken 7 times.
✓ Branch 16 taken 120 times.
✓ Branch 17 taken 7 times.
✓ Branch 18 taken 120 times.
✓ Branch 19 taken 7 times.
✓ Branch 20 taken 120 times.
✓ Branch 21 taken 7 times.
✓ Branch 22 taken 120 times.
✓ Branch 23 taken 7 times.
✓ Branch 24 taken 120 times.
✓ Branch 25 taken 7 times.
✓ Branch 26 taken 120 times.
✓ Branch 27 taken 7 times.
✓ Branch 28 taken 120 times.
✓ Branch 29 taken 7 times.
✓ Branch 30 taken 120 times.
✓ Branch 31 taken 7 times.
232 }
3104
3105 120 void setupAudio() {
3106 120 runAudioSetup(false);
3107 120 }
3108