GCC Code Coverage Report


Directory: ./
File: mediaoutput/mediaoutput.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 669 746 89.7%
Functions: 46 49 93.9%
Branches: 939 1765 53.2%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2022 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12
13 #include "fpp-pch.h"
14
15 #include "fpp-json.h"
16
17 #include "Warnings.h" // WarningHolder -- needed directly for NOPCH builds
18 #include "Timers.h" // periodic audio-device presence monitor
19
20 #include <pthread.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <cstdio> // popen/pclose for the amixer control probe
25 #include <algorithm>
26 #include <atomic>
27 #include <condition_variable>
28 #include <map>
29 #include <mutex>
30 #include <set>
31 #include <sstream>
32 #include <string>
33 #include <thread>
34 #include <vector>
35
36 #include "MultiSync.h"
37 #include "common.h"
38 #include "log.h"
39 #include "mediaoutput.h"
40 #include "commands/Commands.h"
41
42 #include "Plugins.h"
43 #include "Sequence.h"
44 #include "AES67Manager.h"
45 #include "GStreamerOut.h"
46 #include "OpusRTPManager.h"
47 #include "PipeWireOutputStream.h"
48 #include "StreamSlotManager.h"
49 #include "VideoDisplay.h"
50 #include "VideoOutputManager.h"
51 #include "mediadetails.h"
52 #include "settings.h"
53 #include "../config.h"
54 #ifdef PLATFORM_OSX
55 #include "../MacOSApp.h"
56 #endif
57
58 /////////////////////////////////////////////////////////////////////////////
59 MediaOutputBase* mediaOutput = 0;
60 float masterMediaPosition = 0.0;
61 std::mutex mediaOutputLock;
62 std::mutex drmMasterLock;
63
64 static bool firstOutCreate = true;
65
66 // The last media a remote closed, when, and where the master was in it. A SYNC
67 // packet that was already in flight when the media stopped (or when the master
68 // moved on to the next item) reaches us after the close; taken at face value it
69 // would reopen the file just closed -- or close the media that just started to
70 // make room for it. See UpdateMasterMediaPosition().
71 static std::mutex lastClosedMediaLock;
72 static std::string lastClosedMediaName;
73 static uint64_t lastClosedMediaMS = 0;
74 static float lastClosedMediaPos = 0.0f;
75 // How long after a close a SYNC that still points at the end of that media is
76 // treated as stale. Packets arrive every ~1s at most; a repeat of the same
77 // item restarts at the beginning, which this does not match.
78 static const uint64_t STALE_SYNC_WINDOW_MS = 1500;
79 static const float STALE_SYNC_SLACK_SEC = 1.0f;
80
81 // AudioOutput is persisted as a stable ALSA card ID string (e.g. "S3",
82 // "bcm2835ALSA"); setupAudio() in boot/FPPINIT_Audio.cpp migrates legacy numeric
83 // values on boot. Resolve it to the current card number for amixer. Read
84 // defensively: an all-numeric value is treated as a legacy card index, otherwise
85 // it is matched against /proc/asound/cards by ID. Mirrors getAlsaCardNumForId()
86 // in boot/FPPINIT_Audio.cpp (fppd and fppinit are separate binaries and cannot
87 // share the static helper).
88 4695 std::string GetEffectiveAudioOutput() {
89
1/2
✓ Branch 0 taken 4695 times.
✗ Branch 1 not taken.
4695 std::string selected = getSetting("AudioOutput");
90
1/2
✓ Branch 0 taken 4695 times.
✗ Branch 1 not taken.
4695 TrimWhiteSpace(selected);
91
1/2
✓ Branch 0 taken 4695 times.
✗ Branch 1 not taken.
4695 Json::Value eff;
92 14085 if (FileExists("/run/fppd/audio-output.json") &&
93
4/20
✓ Branch 0 taken 4695 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4695 times.
✗ 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 taken 4695 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 4695 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
9390 LoadJsonFromFile("/run/fppd/audio-output.json", eff, JsonRoot::Object) &&
94
4/20
✗ Branch 0 not taken.
✗ 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 taken 4695 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 4695 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 4695 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 4695 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.
4695 eff.get("selected", "").asString() == selected) {
95 ✗ std::string effective = eff.get("effective", "").asString();
96 ✗ if (!effective.empty()) {
97 ✗ return effective;
98 }
99 ✗ }
100 4695 return selected;
101 4695 }
102
103 3897 static int resolveAudioOutputCardNum() {
104
1/2
✓ Branch 0 taken 3897 times.
✗ Branch 1 not taken.
3897 std::string id = GetEffectiveAudioOutput();
105
1/2
✓ Branch 0 taken 3897 times.
✗ Branch 1 not taken.
3897 TrimWhiteSpace(id);
106
2/2
✓ Branch 0 taken 3891 times.
✓ Branch 1 taken 6 times.
3897 if (id.empty()) {
107 3891 return 0;
108 }
109
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (id.find_first_not_of("0123456789") == std::string::npos) {
110 try {
111
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return std::stoi(id);
112 ✗ } catch (...) {
113 ✗ return 0;
114 ✗ }
115 }
116
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 std::istringstream iss(GetFileContents("/proc/asound/cards"));
117 4 std::string line;
118
4/6
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 2 times.
10 while (std::getline(iss, line)) {
119 8 auto b = line.find('[');
120 8 auto e = line.find(']');
121
4/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
8 if (b != std::string::npos && e != std::string::npos && e > b) {
122
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 std::string cid = line.substr(b + 1, e - b - 1);
123
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 TrimWhiteSpace(cid);
124
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 if (cid == id) {
125
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::string num = line.substr(0, b);
126
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 TrimWhiteSpace(num);
127 try {
128
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return std::stoi(num);
129 ✗ } catch (...) {}
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 }
131
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 }
132 }
133 2 return 0; // selected card not present -> default to card 0
134 3897 }
135
136 #ifndef PLATFORM_OSX
137 // Warning ID for "configured audio output card is missing". Kept distinct so
138 // the warning can be added/removed idempotently as the card comes and goes.
139 static constexpr int AUDIO_CARD_MISSING_WARNING_ID = 58;
140
141 // Is the given stable ALSA card ID currently listed in /proc/asound/cards?
142 7 static bool alsaCardIdPresent(const std::string& id) {
143
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 std::istringstream iss(GetFileContents("/proc/asound/cards"));
144 7 std::string line;
145
4/6
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 6 times.
15 while (std::getline(iss, line)) {
146 9 auto b = line.find('[');
147 9 auto e = line.find(']');
148
4/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
9 if (b != std::string::npos && e != std::string::npos && e > b) {
149
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 std::string cid = line.substr(b + 1, e - b - 1);
150
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 TrimWhiteSpace(cid);
151
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 if (cid == id) {
152 1 return true;
153 }
154
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 }
155 }
156 6 return false;
157 7 }
158
159 // Periodically verify the configured audio output card is still present. USB
160 // sound cards can be unplugged (or drop off the bus) while fppd is running; when
161 // that happens PipeWire/ALSA fall back to the default device and audio silently
162 // plays somewhere unexpected -- and, historically, PipeWire's ALSA layer would
163 // also spam syslog retrying the dead device. Surface a UI warning so the user
164 // knows, and clear it automatically once the card returns.
165 //
166 // Only named cards (stable IDs like "S3", "ICUSBAUDIO7D") are monitored: an
167 // empty or all-numeric setting means the default/legacy card index, which we
168 // can't meaningfully test for presence by ID. A short debounce avoids flapping
169 // on transient enumeration gaps.
170 778 static void CheckAudioOutputCardPresence() {
171 static int missCount = 0;
172 static bool warningActive = false;
173 // RemoveWarning() matches the exact text, and the text names the card, so
174 // keep what was added rather than retyping it at the removal sites.
175
3/4
✓ Branch 0 taken 271 times.
✓ Branch 1 taken 507 times.
✓ Branch 2 taken 271 times.
✗ Branch 3 not taken.
778 static std::string warningText;
176
177
1/2
✓ Branch 0 taken 778 times.
✗ Branch 1 not taken.
778 std::string id = getSetting("AudioOutput");
178
1/2
✓ Branch 0 taken 778 times.
✗ Branch 1 not taken.
778 TrimWhiteSpace(id);
179
5/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 771 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 771 times.
✓ Branch 5 taken 7 times.
778 if (id.empty() || id.find_first_not_of("0123456789") == std::string::npos) {
180 // Nothing monitorable by ID; make sure any stale warning is cleared.
181
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 770 times.
771 if (warningActive) {
182
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 WarningHolder::RemoveWarning(AUDIO_CARD_MISSING_WARNING_ID, warningText);
183 1 warningActive = false;
184 }
185 771 missCount = 0;
186 771 return;
187 }
188
189
3/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 6 times.
7 if (alsaCardIdPresent(id)) {
190 1 missCount = 0;
191
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (warningActive) {
192
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogInfo(VB_MEDIAOUT, "Audio output card '%s' is present again; clearing warning.\n", id.c_str());
193
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 WarningHolder::RemoveWarning(AUDIO_CARD_MISSING_WARNING_ID, warningText);
194 1 warningActive = false;
195 }
196 1 return;
197 }
198
199 // Absent. Require two consecutive misses (~20s) before warning to ride out
200 // brief re-enumeration during resume/hotplug.
201
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (missCount < 2) {
202 4 ++missCount;
203 4 return;
204 }
205
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!warningActive) {
206
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogWarn(VB_MEDIAOUT, "Configured audio output card '%s' is no longer present.\n", id.c_str());
207
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 warningText = "Audio output card '" + id + "' is not present - it may have been unplugged. "
208
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 "Audio will fall back to the default output device until it is reconnected.";
209
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 WarningHolder::AddWarning(AUDIO_CARD_MISSING_WARNING_ID, warningText,
210 "pipewire-audio.php", "Audio Settings");
211 2 warningActive = true;
212 }
213
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 776 times.
778 }
214 #endif
215
216 // Starts idle: status defaults to MEDIAOUTPUTSTATUS_IDLE (0).
217 MediaOutputStatus mediaOutputStatus;
218
219 /*
220 *
221 */
222 2651 void InitMediaOutput(void) {
223 #ifndef PLATFORM_OSX
224 2651 int vol = getSettingInt("volume", -1);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2651 times.
2651 if (vol < 0) {
226 ✗ vol = 70;
227 }
228 2651 setVolume(vol);
229
230 // Watch for the configured audio output card disappearing at runtime
231 // (e.g. a USB sound card unplugged mid-show) and surface a UI warning.
232
2/4
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2651 times.
✗ Branch 3 not taken.
7363 Timers::INSTANCE.addPeriodicTimer("AudioCardPresence", 10000, []() {
233 778 CheckAudioOutputCardPresence();
234 778 });
235 #endif
236 #ifdef HAS_GSTREAMER
237 2651 GStreamerOutput::PreloadAsync();
238 2651 PipeWireOutputStream::SetKeepOpen(getSettingInt("PipeWireKeepOutputOpen") != 0);
239
3/6
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2651 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2651 times.
✗ Branch 5 not taken.
12665 registerSettingsListener("PipeWireOutputStream", "PipeWireKeepOutputOpen", [](const std::string& value) {
240 ✗ PipeWireOutputStream::SetKeepOpen(!value.empty() && value != "0");
241 ✗ });
242 #endif
243 2651 }
244
245 /*
246 *
247 */
248 void ShutdownPreparedMediaOutput();
249
250 2620 void CleanupMediaOutput(void) {
251 2620 CloseMediaOutput();
252 2620 ShutdownPreparedMediaOutput();
253 #ifdef HAS_GSTREAMER
254 2620 PipeWireOutputStream::ShutdownAll();
255 #endif
256 #ifdef HAS_VIDEO_DISPLAY
257 2620 VideoDisplay::ShutdownAll();
258 #endif
259 2620 }
260
261 267 void PrewarmMediaOutput(int holdMs) {
262 #ifdef HAS_GSTREAMER
263 267 PipeWireOutputStream::Prewarm(1, holdMs);
264 // The playlist may open with a video; slot 1's overlay and RTP members
265 // (if it has any) are built and waiting.
266 267 VideoOutputManager::Instance().Prewarm(1, holdMs);
267 #endif
268 267 }
269
270 // The volume last asked for: what getVolume() reports, at once, while the
271 // worker below catches the mixers up. Bumped with each request, and copied to
272 // s_volumeApplied once the worker has applied that request.
273 static std::atomic<int> volume{ 70 };
274 static std::atomic<uint64_t> s_volumeRequested{ 0 };
275 static std::atomic<uint64_t> s_volumeApplied{ 0 };
276 #ifndef PLATFORM_OSX
277 static int lastPipeWireVolume = -1; // Track last volume to avoid redundant mute toggles (volume worker only)
278 247877 int getVolume() {
279 247877 return volume;
280 }
281
282 // Read a command's stdout. Local to the mixer handling below, which has to parse
283 // amixer's output; libfpp has no shared helper for this.
284 2647 static std::string readCommandOutput(const std::string& cmd) {
285 2647 std::string out;
286
1/2
✓ Branch 0 taken 2647 times.
✗ Branch 1 not taken.
2647 FILE* f = popen(cmd.c_str(), "r");
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2647 times.
2647 if (!f) {
288 ✗ return out;
289 }
290 char buf[512];
291
3/4
✓ Branch 0 taken 2660 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 2647 times.
2660 while (fgets(buf, sizeof(buf), f) != nullptr) {
292
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 out += buf;
293 }
294
1/2
✓ Branch 0 taken 2647 times.
✗ Branch 1 not taken.
2647 pclose(f);
295 2647 return out;
296 ✗ }
297
298 // A playback volume control, and whether amixer reports a dB scale for it.
299 struct PlaybackVolumeControl {
300 std::string name;
301 bool hasDbScale = false;
302 };
303
304 // The playback volume controls on a card, cached: enumerating them costs an
305 // amixer per control, and setVolume() is called repeatedly while a slider moves.
306 // Most FPP capes are bare I2S DACs and return an empty list, so this is normally
307 // one amixer at startup and nothing thereafter.
308 3888 static const std::vector<PlaybackVolumeControl>& playbackVolumeControls(int card) {
309 static std::mutex cacheLock;
310
3/4
✓ Branch 0 taken 2644 times.
✓ Branch 1 taken 1244 times.
✓ Branch 2 taken 2644 times.
✗ Branch 3 not taken.
3888 static std::map<int, std::vector<PlaybackVolumeControl>> cache;
311
1/2
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
3888 std::unique_lock<std::mutex> lk(cacheLock);
312
1/2
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
3888 auto it = cache.find(card);
313
2/2
✓ Branch 0 taken 1244 times.
✓ Branch 1 taken 2644 times.
3888 if (it != cache.end()) {
314 1244 return it->second;
315 }
316 2644 std::vector<PlaybackVolumeControl> ctls;
317
3/6
✓ Branch 0 taken 2644 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2644 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2644 times.
✗ Branch 5 not taken.
2644 std::string list = readCommandOutput("amixer -c " + std::to_string(card) + " scontrols 2>/dev/null");
318
1/2
✓ Branch 0 taken 2644 times.
✗ Branch 1 not taken.
2644 std::istringstream iss(list);
319 2644 std::string line;
320
4/6
✓ Branch 0 taken 2647 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2647 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 2644 times.
2647 while (std::getline(iss, line)) {
321 // "Simple mixer control 'Main Digital',0"
322 3 std::size_t b = line.find('\'');
323 3 std::size_t e = line.rfind('\'');
324
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (b == std::string::npos || e <= b) {
325 ✗ continue;
326 }
327
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::string name = line.substr(b + 1, e - b - 1);
328 // The name is interpolated into a shell command. Control names are plain
329 // text, so anything that could escape the quoting is a reason to skip it
330 // rather than to quote harder.
331
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
3 if (name.empty() || name.find_first_of("\"'`$\\") != std::string::npos) {
332 ✗ continue;
333 }
334 // Only controls that actually carry a playback volume. A card like a
335 // PCM512x cape also exposes switches and tuning enums (Deemphasis, DSP
336 // Program, Auto Mute, Overclock...) that must not be written.
337
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 std::string info = readCommandOutput("amixer -c " + std::to_string(card) + " sget \"" +
338
3/6
✓ 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.
3 name + "\" 2>/dev/null");
339
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (info.find("pvolume") != std::string::npos) {
340 // A control with a dB scale prints its level as "[0.00dB]".
341 2 ctls.push_back({ name, info.find("dB]") != std::string::npos });
342 }
343
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 }
344
1/2
✓ Branch 0 taken 2644 times.
✗ Branch 1 not taken.
2644 return cache.emplace(card, std::move(ctls)).first->second;
345
2/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3890 }
346
347 // Output sinks the master volume has to attenuate that are NOT FPP-owned.
348 //
349 // The master targets the terminal output sinks so it multiplies with the group
350 // and member volumes instead of fighting them, and finds them by prefix
351 // (fpp_alsa_*, aes67_*). But a group member does not always target an FPP
352 // adapter: when WirePlumber already publishes a node for the card, the member
353 // targets that instead -- an HDMI output comes through as
354 // alsa_output.platform-<addr>.hdmi.hdmi-stereo. Those match no prefix, so the
355 // master silently skipped them: on a group with one fpp_alsa_ member and one
356 // HDMI member, moving the master attenuated half the group and unbalanced it,
357 // and on an HDMI-only box the master did nothing at all.
358 //
359 // RestorePipeWireGroupVolumes() already has to know about exactly these nodes
360 // (it pins them to 100% because WirePlumber initialises them at ~40%), so read
361 // the same member list it does and hand the names to the master too.
362 //
363 // The AES67 and Opus RTP senders (aes67_*_send, opusrtp_*_send) are not
364 // here: their nodes are capture streams, so pactl set-sink-volume finds no
365 // such sink. Their pipelines apply the master themselves (see
366 // AES67Manager::SetMasterVolume()).
367 //
368 // KEEP IN SYNC with restorePipeWireVolumes() in FPPINIT_Audio.cpp and
369 // RestorePipeWireGroupVolumes() in pipewire.php -- same file selection, same
370 // "not fpp_/aes67_" test.
371 3888 static std::set<std::string> nonFppOutputSinkNames() {
372 3888 std::set<std::string> names;
373 // Same default as settings.json: a missing key must not select the retired
374 // ALSA backend, and Simple mode's groups live in a different file.
375
2/4
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3888 times.
✗ Branch 3 not taken.
3888 std::string backend = toLowerCopy(getSetting("MediaBackend", "pipewire-simple"));
376 // Named separately: FPP_DIR_CONFIG does not parenthesise its argument, so a
377 // ternary passed straight in binds to the concatenation instead.
378
3/4
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3786 times.
✓ Branch 3 taken 102 times.
3888 const char* groupsFile = (backend == "pipewire-simple") ? "/pipewire-audio-groups-simple.json"
379 3888 : "/pipewire-audio-groups.json";
380
3/6
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3888 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3888 times.
✗ Branch 5 not taken.
3888 std::string path = FPP_DIR_CONFIG(groupsFile);
381
1/2
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
3888 Json::Value root;
382 // Both files can hold values of the wrong JSON type (hand edits, API
383 // writes); jsoncpp throws on those, and this runs at startup from
384 // setVolume(), so a throw terminated fppd. Take what can be read.
385 try {
386
7/10
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 3884 times.
✓ 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 3884 times.
3888 if (LoadJsonFromFile(path, root, JsonRoot::Object) && root.isMember("groups")) {
387
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 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 2 times.
6 for (const auto& grp : root["groups"]) {
388
12/22
✓ 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 taken 2 times.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 2 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 2 times.
✓ Branch 18 taken 2 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 2 times.
✗ Branch 21 not taken.
8 if (!grp.get("enabled", false).asBool() || !grp.isMember("members")) {
389 ✗ continue;
390 }
391
8/14
✓ 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 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 2 times.
6 for (const auto& mbr : grp["members"]) {
392
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 std::string t = mbr.get("nodeTarget", "").asString();
393
16/32
✓ 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 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 2 times.
✓ Branch 14 taken 2 times.
✓ Branch 15 taken 2 times.
✓ Branch 16 taken 2 times.
✓ Branch 17 taken 2 times.
✓ Branch 18 taken 4 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 4 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 2 times.
✓ Branch 23 taken 2 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.
16 if (t.empty() || startsWith(t, "fpp_") || startsWith(t, "aes67_")) {
394 2 continue; // already covered by the prefix match
395 }
396 // Interpolated into a shell command below. WirePlumber node names
397 // are plain identifiers; anything that could escape the quoting is a
398 // reason to skip the node rather than to quote harder.
399 2 if (t.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
400 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 "0123456789_.-") != std::string::npos) {
402 ✗ LogWarn(VB_MEDIAOUT, "Skipping master volume for node with unexpected name: %s\n", t.c_str());
403 ✗ continue;
404 }
405
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 names.insert(t);
406
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 }
407 }
408 }
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } catch (const std::exception& e) {
410
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 WarningHolder::InvalidConfigEntry(groupsFile + 1, e.what());
411 2 }
412
413 3888 return names;
414 3888 }
415
416 // Whether any AES67 or Opus RTP sender is configured. Read from the config
417 // files, not the managers: the volume restore at startup can run before they
418 // have loaded theirs.
419 3888 static bool networkSendersConfigured() {
420
2/2
✓ Branch 0 taken 7708 times.
✓ Branch 1 taken 3813 times.
11521 for (const char* file : { "/pipewire-aes67-instances.json", "/pipewire-opus-rtp-instances.json" }) {
421
1/2
✓ Branch 0 taken 7708 times.
✗ Branch 1 not taken.
7708 Json::Value root;
422 try {
423
14/32
✓ Branch 0 taken 7708 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7708 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7708 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7708 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 87 times.
✓ Branch 9 taken 7621 times.
✓ Branch 10 taken 87 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 87 times.
✓ Branch 14 taken 7708 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 7708 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 7708 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 7708 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 7621 times.
✓ Branch 23 taken 87 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.
23124 if (!LoadJsonFromFile(FPP_DIR_CONFIG(file), root, JsonRoot::Object) || !root.isMember("instances")) {
424 7621 continue;
425 }
426
8/14
✓ 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.
✓ Branch 6 taken 87 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 95 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 87 times.
✓ Branch 13 taken 8 times.
95 for (const auto& inst : root["instances"]) {
427
4/6
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83 times.
✓ Branch 5 taken 4 times.
95 std::string mode = inst.get("mode", "send").asString();
428
13/26
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 83 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 83 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 8 times.
✓ Branch 11 taken 75 times.
✓ Branch 12 taken 8 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 8 times.
✓ Branch 16 taken 83 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 83 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 75 times.
✓ Branch 21 taken 8 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
83 if (inst.get("enabled", true).asBool() && (mode == "send" || mode == "both")) {
429 75 return true;
430 }
431
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 75 times.
83 }
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 } catch (const std::exception& e) {
433
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.
12 WarningHolder::InvalidConfigEntry(file + 1, e.what());
434 4 }
435
3/3
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7621 times.
✓ Branch 2 taken 75 times.
7708 }
436 3813 return false;
437 }
438
439 // Put the card's hardware playback path at unity, so PipeWire's software volume
440 // is the only thing attenuating.
441 //
442 // This replaces pinning the single control named by the AudioMixerDevice setting
443 // to 100%, which was wrong in two directions. 100% is not unity on a control
444 // whose range runs above 0 dB: a PCM512x cape's "Main Digital" spans 0..255 with
445 // 0 dB at 207 and +24 dB at 255, so selecting the one control on that card that
446 // genuinely is the volume -- the obvious choice -- pinned it 24 dB into clipping.
447 // And pinning only the selected control left any other volume control sitting
448 // wherever the driver happened to leave it. The setting itself only ever picked
449 // the alphabetically first control anyway (amixer scontrols | head -1).
450 //
451 // bcm2835 keeps the old behaviour deliberately: its mixer reaches +4 dB and FPP
452 // wants that headroom available above the software stage.
453 //
454 // Whether to write 0dB or 100% is decided from the control's own description,
455 // not from amixer's exit status: fppd sets SA_NOCLDWAIT, so system() cannot
456 // collect the child and returns -1 even when amixer succeeded. Falling back on
457 // that "failure" wrote 100% straight after 0dB, which put a PCM512x's Digital
458 // control back at +24 dB on every volume change.
459 3888 static void pinCardPlaybackToUnity(int card, bool useMax) {
460
3/4
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 3888 times.
3892 for (const PlaybackVolumeControl& ctl : playbackVolumeControls(card)) {
461 // No dB scale: 100% is the top of a plain range.
462
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
4 const char* level = (useMax || !ctl.hasDbScale) ? "100%" : "0dB";
463
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.
8 const std::string cmd = "amixer -c " + std::to_string(card) + " sset \"" + ctl.name + "\" " +
464
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 level + " >/dev/null 2>&1";
465
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 system(cmd.c_str());
466 4 }
467 3888 }
468 #else
469 int getVolume() {
470 // The device's own level (the user can change it outside fppd), except
471 // while a change is still on its way to it: a caller that sets the
472 // volume and reads it straight back, or steps it twice in a row, must
473 // see the level it asked for, not the one the worker hasn't replaced yet.
474 if (s_volumeApplied.load() != s_volumeRequested.load()) {
475 return volume;
476 }
477 return MacOSGetVolume();
478 }
479 #endif
480
481 3897 static void applyVolume(int vol) {
482 // Only the amixer command below still uses this; the pactl commands are
483 // built as std::strings because their length depends on the user's config.
484 char buffer[1024];
485
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3897 times.
3897 if (vol < 0)
487 ✗ vol = 0;
488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3897 times.
3897 else if (vol > 100)
489 ✗ vol = 100;
490
491
1/2
✓ Branch 0 taken 3897 times.
✗ Branch 1 not taken.
3897 std::string mixerDevice = getSetting("AudioMixerDevice");
492
1/2
✓ Branch 0 taken 3897 times.
✗ Branch 1 not taken.
3897 int audioOutput = resolveAudioOutputCardNum();
493
1/2
✓ Branch 0 taken 3897 times.
✗ Branch 1 not taken.
3897 std::string audio0Type = getSetting("AudioCard0Type");
494 // Same default as settings.json: a missing key must not select the retired
495 // ALSA path.
496
2/4
✓ Branch 0 taken 3897 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3897 times.
✗ Branch 3 not taken.
3897 std::string mediaBackend = toLowerCopy(getSetting("MediaBackend", "pipewire-simple"));
497
498
6/8
✓ Branch 0 taken 3897 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3795 times.
✓ Branch 3 taken 102 times.
✓ Branch 4 taken 3795 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3786 times.
✓ Branch 7 taken 9 times.
3897 bool usePipeWireBackend = (mediaBackend == "pipewire" || mediaBackend == "pipewire-simple");
499
500 #ifndef PLATFORM_OSX
501 3897 float fvol = vol;
502
503 // === Hardware mixer ===
504 // PipeWire mode: put the card's playback controls at unity so the software
505 // volume further down is the sole attenuator. The 0 dB choice, the bcm2835
506 // exception, and why only pvolume controls are touched all live in
507 // pinCardPlaybackToUnity(). Many capes are bare I2S DACs with no playback
508 // mixer at all, so the control list is empty and nothing is shelled out --
509 // and nothing is lost, because the software volume is what actually
510 // attenuates, verified at the ALSA boundary through a loopback card (a sink
511 // node at 50% delivers 0.125 amplitude with no mixer involved).
512 //
513 // The ALSA branch below is legacy. "alsa" is no longer offered as a
514 // MediaBackend and FPPINIT migrates a stored one to pipewire-simple at boot,
515 // so it is unreachable in a supported configuration; it is kept as a
516 // fallback, and given the same taper as the PipeWire path so that the same
517 // number would still mean the same loudness if it ever did run.
518
2/2
✓ Branch 0 taken 3888 times.
✓ Branch 1 taken 9 times.
3897 if (usePipeWireBackend) {
519 3888 bool useMax = false;
520 #ifdef PLATFORM_PI
521
4/6
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 90 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 90 times.
91 useMax = (audioOutput == 0 && audio0Type == "bcm2");
522 #endif
523
1/2
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
3888 pinCardPlaybackToUnity(audioOutput, useMax);
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 } else if (mixerDevice.empty()) {
525 ✗ LogDebug(VB_MEDIAOUT, "No ALSA mixer control for card %d; skipping hardware volume\n",
526 audioOutput);
527
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 } else if (mixerDevice.find('\'') != std::string::npos) {
528 // The name is interpolated inside single quotes in the amixer command
529 // below -- the only character that can break out of that context. A
530 // quote can only get here via a hand-edited setting (real control
531 // names never contain one), so skip hardware volume exactly like a
532 // missing control is skipped above.
533
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogDebug(VB_MEDIAOUT, "ALSA mixer control name contains a quote; skipping hardware volume\n");
534 } else {
535 // Follow the same taper as the PipeWire path, where pactl's percentage
536 // is PulseAudio's cubic (perceptual) scale -- the same FPP volume should
537 // not mean a different loudness just because the backend changed. Only
538 // the taper can be matched, not the absolute level: amixer's percentage
539 // is of the control's own raw range, and cards routinely describe that
540 // range uselessly (a USB DAC measured here spans 0.00 to -0.12 dB across
541 // all 31 of its steps), so there is no portable dB to aim at.
542 7 const float v = vol / 100.0f;
543 7 fvol = 100.0f * v * v * v;
544 #ifdef PLATFORM_PI
545 ✗ if (audioOutput == 0 && audio0Type == "bcm2") {
546 ✗ fvol /= 2;
547 ✗ fvol += 50;
548 }
549 #endif
550 7 snprintf(buffer, sizeof(buffer), "amixer set -c %d '%s' -- %.2f%% >/dev/null 2>&1",
551 audioOutput, mixerDevice.c_str(), fvol);
552
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 LogDebug(VB_SETTING, "Volume change: %d \n", vol);
553
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 LogDebug(VB_MEDIAOUT, "Setting ALSA hardware volume: %s \n", buffer);
554
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 system(buffer);
555 }
556
557 // === PipeWire software volume ===
558 // Use pactl (PulseAudio compat layer) to set the sink volume. This is
559 // the same mechanism used by the per-output-group volume controls in the
560 // PHP UI (which are known to work). It avoids the fragile pw-cli node-ID
561 // lookup and correctly respects PIPEWIRE_RUNTIME_DIR / XDG_RUNTIME_DIR
562 // rather than PIPEWIRE_REMOTE.
563 //
564 // The master targets the CARD sinks (fpp_alsa_*), not PipeWireSinkName.
565 // PipeWireSinkName points at the primary output group, which is also the
566 // node the group volume control and RestorePipeWireGroupVolumes() write --
567 // so master and that group's slider were one knob, and whoever wrote last
568 // won. At boot that is always fppd (the volume restore runs in fppinit,
569 // before fppd starts), which silently discarded whatever the user had set
570 // on the group. A group left above the master therefore came back quieter
571 // after every reboot. The card sinks sit downstream of every group, so
572 // master, group and member volumes now multiply rather than overwrite.
573 //
574 // Volume 0 is expressed as a real 0% so the sink goes silent without
575 // needing a separate mute call; any non-zero restore includes an explicit
576 // unmute in case a prior 0% left the sink muted.
577
2/2
✓ Branch 0 taken 3888 times.
✓ Branch 1 taken 9 times.
3897 if (usePipeWireBackend) {
578 3888 const bool shouldMute = (vol == 0);
579 std::string pwPrefix = "PIPEWIRE_RUNTIME_DIR=/run/pipewire-fpp "
580 "XDG_RUNTIME_DIR=/run/pipewire-fpp "
581
1/2
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
3888 "PULSE_RUNTIME_PATH=/run/pipewire-fpp/pulse ";
582
583 // Every FPP card sink, plus the WirePlumber-managed nodes group members
584 // target directly (an HDMI output, say), which match no FPP prefix and
585 // were being skipped. The network senders are not sinks and take the
586 // master in their own pipelines (below).
587 // Those are appended by name rather than folded into the grep: they may
588 // legitimately be absent from the graph right now (an unplugged card
589 // whose stored nodeTarget is still in the config), and pactl shrugging
590 // at a name it does not know is exactly what the volume-restore path
591 // already relies on.
592 // Falls back to the default sink only when that leaves nothing at all
593 // (no 95-fpp-alsa-sink.conf adapters, no member targets and no network
594 // sender), which is the old behaviour and still better than attenuating
595 // nothing. Not with a sender: the default sink can be the group that
596 // feeds it, and the sender would then be attenuated twice.
597 // Deliberately no single quotes in here: the whole thing is embedded in
598 // an sh -c '...' string, so an inner ' would close it early (awk
599 // '{print $2}' silently truncated the command and left the volume
600 // unset). pactl's short listing is tab separated, so cut does the job.
601 3888 std::string extraSinks;
602
3/4
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3888 times.
3890 for (const std::string& n : nonFppOutputSinkNames()) {
603
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 extraSinks += " " + n;
604 3888 }
605
4/6
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 3813 times.
✓ Branch 4 taken 3888 times.
✗ Branch 5 not taken.
4183 std::string fallback = networkSendersConfigured() ? "" : "[ -z \"$S\" ] && [ -z \"$E\" ] && S=@DEFAULT_SINK@; ";
606
2/4
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3888 times.
✗ Branch 3 not taken.
7776 std::string sinkList = "E=\"" + extraSinks + "\"; " +
607 "S=$(pactl list sinks short 2>/dev/null | cut -f2 | "
608
1/2
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
7776 "grep -e ^fpp_alsa_); " +
609
1/2
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
3888 fallback +
610
1/2
✓ Branch 0 taken 3888 times.
✗ Branch 1 not taken.
3888 "S=\"$S $E\"; ";
611
612 // Built as a string, not into the fixed buffer above: the member-target
613 // names make the length depend on the user's config, and this command
614 // silently losing its tail is precisely how the volume went unset before.
615 3888 std::string cmd;
616
2/2
✓ Branch 0 taken 2337 times.
✓ Branch 1 taken 1551 times.
3888 if (shouldMute) {
617 // Set volume to 0% and explicitly mute for true silence.
618
2/4
✓ Branch 0 taken 2337 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2337 times.
✗ Branch 3 not taken.
4674 cmd = pwPrefix + " sh -c '" + sinkList +
619
1/2
✓ Branch 0 taken 2337 times.
✗ Branch 1 not taken.
2337 "for s in $S; do pactl set-sink-volume $s 0%; pactl set-sink-mute $s 1; done' >/dev/null 2>&1";
620
1/2
✓ Branch 0 taken 2337 times.
✗ Branch 1 not taken.
2337 LogDebug(VB_MEDIAOUT, "Muting PipeWire output sinks: %s \n", cmd.c_str());
621
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 1486 times.
1551 } else if (lastPipeWireVolume == 0) {
622 // Unmute and set volume together, undoing a prior 0%.
623
2/4
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
✗ Branch 3 not taken.
130 cmd = pwPrefix + " sh -c '" + sinkList +
624
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
130 "for s in $S; do pactl set-sink-mute $s 0; pactl set-sink-volume $s " +
625
2/4
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
✗ Branch 3 not taken.
195 std::to_string(vol) + "%; done' >/dev/null 2>&1";
626
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 LogDebug(VB_MEDIAOUT, "Unmuting and setting PipeWire output sink volume: %s \n", cmd.c_str());
627 } else {
628
2/4
✓ Branch 0 taken 1486 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1486 times.
✗ Branch 3 not taken.
2972 cmd = pwPrefix + " sh -c '" + sinkList +
629
2/4
✓ Branch 0 taken 1486 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1486 times.
✗ Branch 3 not taken.
5878 "for s in $S; do pactl set-sink-volume $s " + std::to_string(vol) +
630
1/2
✓ Branch 0 taken 1486 times.
✗ Branch 1 not taken.
1486 "%; done' >/dev/null 2>&1";
631
1/2
✓ Branch 0 taken 1486 times.
✗ Branch 1 not taken.
1486 LogDebug(VB_MEDIAOUT, "Setting PipeWire output sink volume: %s \n", cmd.c_str());
632 }
633
1/2
✓ Branch 0 taken 3886 times.
✗ Branch 1 not taken.
3888 system(cmd.c_str());
634
635 3886 lastPipeWireVolume = vol;
636
637 // The AES67 and Opus RTP senders: capture streams, which the sink pass
638 // above cannot reach.
639 #ifdef HAS_AES67_GSTREAMER
640
1/2
✓ Branch 0 taken 3886 times.
✗ Branch 1 not taken.
3886 AES67Manager::INSTANCE.SetMasterVolume(vol);
641 #endif
642 #ifdef HAS_OPUS_RTP_GSTREAMER
643
1/2
✓ Branch 0 taken 3886 times.
✗ Branch 1 not taken.
3886 OpusRTPManager::INSTANCE.SetMasterVolume(vol);
644 #endif
645 3886 }
646 #else
647 MacOSSetVolume(vol);
648 #endif
649
650
1/2
✓ Branch 0 taken 3895 times.
✗ Branch 1 not taken.
3895 std::unique_lock<std::mutex> lock(mediaOutputLock);
651 // In PipeWire mode, volume is controlled via pactl on the sink (above).
652 // Don't also set the GStreamer volume element or we get double attenuation.
653 // The GStreamer volume element is still used for per-track volumeAdjust
654 // (dB offset). In ALSA mode, propagate to GStreamer as before.
655
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3893 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
3895 if (mediaOutput && !usePipeWireBackend)
656 ✗ mediaOutput->SetVolume(vol);
657 3895 }
658
659
660 272 double masterVolumeGain(int volume) {
661
1/2
✓ Branch 0 taken 272 times.
✗ Branch 1 not taken.
272 const double v = std::clamp(volume, 0, 100) / 100.0;
662 272 return v * v * v;
663 }
664
665 // setVolume() shells out several times (pactl for every sink, amixer), and is
666 // called from the main loop (the command socket, once per step of the UI's
667 // volume slider), from API threads and from presets. A slow or wedged
668 // PipeWire stalled whichever of those asked. Requests now only record the
669 // target; one worker thread applies the latest, so a burst of slider steps
670 // costs one round of shell-outs, not one per step.
671 //
672 // The lock and the condition variable are leaked, not file-statics: the
673 // worker waits on them for the life of the process, and an exit() from
674 // startup (the command socket or the HTTP server failing, after
675 // InitMediaOutput() has started the worker) ran their destructors. glibc's
676 // pthread_cond_destroy() waits for the blocked worker, so fppd never exited
677 // and systemd never restarted it.
678 static std::mutex& s_volumeWorkerLock = *new std::mutex();
679 static std::condition_variable& s_volumeWorkerWake = *new std::condition_variable();
680 static int s_volumeTarget = -1; // -1: nothing pending
681 static uint64_t s_volumeTargetRequest = 0;
682 static bool s_volumeWorkerStarted = false;
683
684 3954 void setVolume(int vol) {
685
1/2
✓ Branch 0 taken 3954 times.
✗ Branch 1 not taken.
3954 vol = std::clamp(vol, 0, 100);
686
1/2
✓ Branch 0 taken 3954 times.
✗ Branch 1 not taken.
3954 std::lock_guard<std::mutex> lock(s_volumeWorkerLock);
687 3954 volume = vol;
688 3954 s_volumeTarget = vol;
689 3954 s_volumeTargetRequest = ++s_volumeRequested;
690
2/2
✓ Branch 0 taken 2651 times.
✓ Branch 1 taken 1303 times.
3954 if (!s_volumeWorkerStarted) {
691 2651 s_volumeWorkerStarted = true;
692 // Detached: it only ever waits for the next request, on a lock and
693 // condition variable that are never destroyed (see above).
694 5302 std::thread([]() {
695
2/4
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2651 times.
✗ Branch 3 not taken.
2651 SetThreadName("FPP-Volume");
696
1/2
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
2651 std::unique_lock<std::mutex> lock(s_volumeWorkerLock);
697 while (true) {
698
1/2
✓ Branch 0 taken 3897 times.
✗ Branch 1 not taken.
14120 s_volumeWorkerWake.wait(lock, []() { return s_volumeTarget >= 0; });
699 3897 int v = s_volumeTarget;
700 3897 uint64_t request = s_volumeTargetRequest;
701 3897 s_volumeTarget = -1;
702
1/2
✓ Branch 0 taken 3897 times.
✗ Branch 1 not taken.
3897 lock.unlock();
703
1/2
✓ Branch 0 taken 3895 times.
✗ Branch 1 not taken.
3897 applyVolume(v);
704 3895 s_volumeApplied = request;
705
1/2
✓ Branch 0 taken 3895 times.
✗ Branch 1 not taken.
3895 lock.lock();
706 3895 }
707
2/4
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2651 times.
✗ Branch 3 not taken.
2651 }).detach();
708 }
709 3954 s_volumeWorkerWake.notify_one();
710 3954 }
711
712 static std::set<std::string> AUDIO_EXTS = {
713 "mp3", "ogg", "m4a", "m4p", "wav", "au", "wma", "flac", "aac",
714 "MP3", "OGG", "M4A", "M4P", "WAV", "AU", "WMA", "FLAC", "AAC"
715 };
716 static std::map<std::string, std::string> VIDEO_EXTS = {
717 { "mp4", "mp4" }, { "MP4", "mp4" }, { "avi", "avi" }, { "AVI", "avi" }, { "mov", "mov" }, { "MOV", "mov" }, { "mkv", "mkv" }, { "MKV", "mkv" }, { "mpg", "mpg" }, { "MPG", "mpg" }, { "mpeg", "mpeg" }, { "MPEG", "mpeg" }
718 };
719
720 // Case-insensitive: the tables list only the all-lower and all-upper
721 // spellings (VIDEO_EXTS' are also the names GetVideoFilenameForMedia()
722 // probes for), and a folder ripped as "Song.Mp3" must still count as media.
723 4686 bool IsExtensionVideo(const std::string& ext) {
724
2/4
✓ Branch 0 taken 4686 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4686 times.
✗ Branch 3 not taken.
4686 return VIDEO_EXTS.find(toLowerCopy(ext)) != VIDEO_EXTS.end();
725 }
726 5016 bool IsExtensionAudio(const std::string& ext) {
727
2/4
✓ Branch 0 taken 5016 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5016 times.
✗ Branch 3 not taken.
5016 return AUDIO_EXTS.find(toLowerCopy(ext)) != AUDIO_EXTS.end();
728 }
729
730 171 std::string GetVideoFilenameForMedia(const std::string& filename, std::string& ext) {
731
1/2
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
171 ext = "";
732
1/2
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
171 std::string result("");
733 171 std::size_t found = filename.find_last_of(".");
734
1/2
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
171 std::string oext = filename.substr(found + 1);
735
1/2
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
171 std::string lext = toLowerCopy(oext);
736
1/2
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
171 std::string bfile = filename.substr(0, found + 1);
737
1/2
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
171 std::string hbfile = bfile;
738
4/8
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 171 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 171 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 171 times.
✗ Branch 7 not taken.
171 std::string videoPath = FPP_DIR_VIDEO("/" + bfile);
739
740
1/2
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
171 std::string hostname = getSetting("HostName");
741
1/2
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
171 std::string hostVideoPath = "";
742
2/4
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 171 times.
✗ Branch 3 not taken.
171 if (hostname != "") {
743
4/8
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 171 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 171 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 171 times.
✗ Branch 7 not taken.
171 hostVideoPath = videoPath.substr(0, videoPath.length() - 1) + "-" + hostname + ".";
744
4/8
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 171 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 171 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 171 times.
✗ Branch 7 not taken.
171 hbfile = filename.substr(0, found) + "-" + hostname + ".";
745 }
746
747
3/4
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 105 times.
✓ Branch 3 taken 66 times.
171 if (IsExtensionVideo(lext)) {
748
7/16
✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 105 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 105 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 105 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 105 times.
✓ Branch 10 taken 105 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 105 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
105 if (hostVideoPath != "" && FileExists(hostVideoPath + oext)) {
749 ✗ ext = lext;
750 ✗ result = hbfile + oext;
751
7/16
✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 105 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 105 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 105 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 105 times.
✓ Branch 10 taken 105 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 105 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
105 } else if (hostVideoPath != "" && FileExists(hostVideoPath + lext)) {
752 ✗ ext = lext;
753 ✗ result = hbfile + lext;
754
3/6
✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 105 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 105 times.
✗ Branch 5 not taken.
105 } else if (FileExists(videoPath + oext)) {
755
1/2
✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
105 ext = lext;
756
1/2
✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
105 result = bfile + oext;
757 ✗ } else if (FileExists(videoPath + lext)) {
758 ✗ ext = lext;
759 ✗ result = bfile + lext;
760 }
761
3/4
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 3 times.
66 } else if (IsExtensionAudio(lext)) {
762
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 48 times.
744 for (auto& n : VIDEO_EXTS) {
763
4/6
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 696 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✓ Branch 5 taken 681 times.
696 if (FileExists(hostVideoPath + n.first)) {
764
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 ext = n.second;
765
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 result = hbfile + n.first;
766 15 return result;
767
3/6
✓ Branch 0 taken 681 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 681 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 681 times.
681 } else if (FileExists(videoPath + n.first)) {
768 ✗ ext = n.second;
769 ✗ result = bfile + n.first;
770 ✗ return result;
771 }
772 }
773 }
774
775 156 return result;
776 171 }
777 1491 bool HasAudioForMedia(std::string& mediaFilename) {
778
1/2
✓ Branch 0 taken 1491 times.
✗ Branch 1 not taken.
1491 std::string fullMediaPath = mediaFilename;
779
780
1/2
✓ Branch 0 taken 1491 times.
✗ Branch 1 not taken.
1491 std::string hostname = getSetting("HostName");
781
2/4
✓ Branch 0 taken 1491 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1491 times.
✗ Branch 3 not taken.
1491 if (hostname != "") {
782 1491 std::size_t found = mediaFilename.find_last_of(".");
783 1491 std::string hostMediaPath;
784
2/2
✓ Branch 0 taken 1488 times.
✓ Branch 1 taken 3 times.
1491 if (found != std::string::npos) {
785
5/10
✓ Branch 0 taken 1488 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1488 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1488 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1488 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1488 times.
✗ Branch 9 not taken.
1488 hostMediaPath = mediaFilename.substr(0, found) + "-" + hostname + mediaFilename.substr(found);
786 } else {
787
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 hostMediaPath = mediaFilename + "-" + hostname;
788 }
789
2/4
✓ Branch 0 taken 1491 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1491 times.
1491 if (FileExists(hostMediaPath)) {
790 ✗ mediaFilename = hostMediaPath;
791 ✗ return true;
792 }
793
4/8
✓ Branch 0 taken 1491 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1491 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1491 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1491 times.
✗ Branch 7 not taken.
1491 hostMediaPath = FPP_DIR_MUSIC("/" + hostMediaPath);
794
3/4
✓ Branch 0 taken 1491 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 1463 times.
1491 if (FileExists(hostMediaPath)) {
795
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 mediaFilename = hostMediaPath;
796 28 return true;
797 }
798
2/2
✓ Branch 0 taken 1463 times.
✓ Branch 1 taken 28 times.
1491 }
799
800
2/4
✓ Branch 0 taken 1463 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1463 times.
1463 if (FileExists(mediaFilename)) {
801 ✗ return true;
802 }
803
4/8
✓ Branch 0 taken 1463 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1463 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1463 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1463 times.
✗ Branch 7 not taken.
1463 fullMediaPath = FPP_DIR_MUSIC("/" + mediaFilename);
804
3/4
✓ Branch 0 taken 1463 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1338 times.
✓ Branch 3 taken 125 times.
1463 if (FileExists(fullMediaPath)) {
805
1/2
✓ Branch 0 taken 1338 times.
✗ Branch 1 not taken.
1338 mediaFilename = fullMediaPath;
806 1338 return true;
807 }
808 125 return false;
809 1491 }
810 122 bool HasVideoForMedia(std::string& filename) {
811 122 std::string ext;
812
1/2
✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
122 std::string fp = GetVideoFilenameForMedia(filename, ext);
813
3/4
✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 113 times.
✓ Branch 3 taken 9 times.
122 if (fp != "") {
814
1/2
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
113 filename = fp;
815 }
816
1/2
✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
244 return fp != "";
817 122 }
818
819 109 static bool IsHDMIOut(std::string& vOut) {
820
6/8
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 104 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 104 times.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 104 times.
109 if (vOut == "--HDMI--" || vOut == "--hdmi--" || vOut == "HDMI") {
821 5 vOut = "HDMI-A-1";
822 }
823
8/8
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 100 times.
✓ Branch 7 taken 9 times.
109 if (vOut.starts_with("HDMI-") || vOut.starts_with("DSI-") || vOut.starts_with("Composite-")) {
824
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 3 times.
118 for (int x = 0; x < 4; x++) {
825
4/8
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 115 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 115 times.
✗ Branch 7 not taken.
115 std::string conn = "/sys/class/drm/card" + std::to_string(x) + "-" + vOut + "/status";
826
3/4
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 97 times.
✓ Branch 3 taken 18 times.
115 if (FileExists(conn)) {
827
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 std::string status = GetFileContents(conn);
828
2/4
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
97 return !contains(status, "disconnected");
829 97 }
830
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 97 times.
115 }
831 }
832 12 return false;
833 }
834
835 385 std::string ResolveVideoOutput(const std::string& requested) {
836 385 std::string vOut = requested;
837
6/8
✓ Branch 0 taken 385 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 385 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 314 times.
✓ Branch 5 taken 71 times.
✓ Branch 6 taken 314 times.
✓ Branch 7 taken 71 times.
385 if (vOut.empty() || vOut == "--Default--") {
838
1/2
✓ Branch 0 taken 314 times.
✗ Branch 1 not taken.
314 vOut = getSetting("VideoOutput");
839 }
840
2/2
✓ Branch 0 taken 302 times.
✓ Branch 1 taken 83 times.
385 if (vOut.empty()) {
841 #ifdef PLATFORM_OSX
842 // No connectors on a Mac; the display is the video window, which exists
843 // only in a GUI session (see MacOSApp.h).
844 vOut = MacOSAppActive() ? "--HDMI--" : "--Disabled--";
845 #else
846
7/10
✓ Branch 0 taken 302 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 302 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 302 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 57 times.
✓ Branch 7 taken 245 times.
✓ Branch 8 taken 57 times.
✓ Branch 9 taken 245 times.
302 if (FileExists("/sys/class/drm/card0-HDMI-A-1/status") || FileExists("/sys/class/drm/card1-HDMI-A-1/status")) {
847
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57 vOut = "--HDMI--";
848 } else {
849
1/2
✓ Branch 0 taken 245 times.
✗ Branch 1 not taken.
245 vOut = "--Disabled--";
850 }
851 #endif
852 }
853 385 return vOut;
854 ✗ }
855
856 202 MediaOutputBase* CreateMediaOutput(const std::string& mediaFilename, const std::string& vOut, int streamSlot) {
857
1/2
✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
202 std::string tmpFile(mediaFilename);
858 202 std::size_t found = mediaFilename.find_last_of(".");
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
202 if (found == std::string::npos) {
860 ✗ LogDebug(VB_MEDIAOUT, "Unable to determine extension of media file %s\n",
861 mediaFilename.c_str());
862 ✗ return nullptr;
863 }
864
2/4
✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 202 times.
✗ Branch 3 not taken.
202 std::string ext = toLowerCopy(mediaFilename.substr(found + 1));
865
1/2
✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
202 std::string vo = vOut;
866
867 // Use per-slot status for multi-stream; slot 1 uses global for backward compat
868
2/4
✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 202 times.
✗ Branch 3 not taken.
202 MediaOutputStatus* slotStatus = StreamSlotManager::Instance().GetStatus(streamSlot);
869
1/2
✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
202 slotStatus->output = "";
870
871 #ifdef HAS_GSTREAMER
872 // GStreamer is the sole media player. In PipeWire mode audio/video are
873 // routed through the PipeWire stack; in Hardware Direct (ALSA) mode
874 // GStreamer talks directly to ALSA for audio and uses kmssink for video.
875 202 bool useGStreamer = true;
876
877
6/8
✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 202 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 169 times.
✓ Branch 5 taken 33 times.
✓ Branch 6 taken 169 times.
✓ Branch 7 taken 33 times.
202 if (useGStreamer && IsExtensionAudio(ext)) {
878
1/2
✓ Branch 0 taken 169 times.
✗ Branch 1 not taken.
169 LogDebug(VB_MEDIAOUT, "Using GStreamer for audio playback: %s (slot %d)\n", mediaFilename.c_str(), streamSlot);
879
4/10
✓ Branch 0 taken 169 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 169 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 169 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 169 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
507 return new GStreamerOutput(mediaFilename, slotStatus, "--Disabled--", streamSlot);
880 }
881
8/12
✓ 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.
✓ Branch 8 taken 9 times.
✓ Branch 9 taken 24 times.
✓ Branch 10 taken 9 times.
✓ Branch 11 taken 24 times.
33 if (useGStreamer && IsExtensionVideo(ext) && !IsHDMIOut(vo)) {
882 // Video with PixelOverlay output — use GStreamer for both audio and video
883
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 LogDebug(VB_MEDIAOUT, "Using GStreamer for video+overlay playback: %s (overlay=%s, slot %d)\n",
884 mediaFilename.c_str(), vo.c_str(), streamSlot);
885
3/8
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
9 return new GStreamerOutput(mediaFilename, slotStatus, vo, streamSlot);
886 }
887
6/12
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 24 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 24 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 24 times.
✗ Branch 11 not taken.
24 if (useGStreamer && IsExtensionVideo(ext) && IsHDMIOut(vo)) {
888 // Video to HDMI via GStreamer kmssink — audio through PipeWire
889
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 LogDebug(VB_MEDIAOUT, "Using GStreamer for video+HDMI playback: %s (output=%s, slot %d)\n",
890 mediaFilename.c_str(), vo.c_str(), streamSlot);
891
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 slotStatus->output = vo;
892
3/8
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
24 return new GStreamerOutput(mediaFilename, slotStatus, vo, streamSlot);
893 }
894 #endif
895
896 ✗ return nullptr;
897 202 }
898
899 namespace {
900 struct PreparedMedia {
901 std::string file;
902 std::string videoOut;
903 MediaOutputBase* out = nullptr;
904 bool building = false;
905 uint64_t gen = 0;
906 };
907 std::mutex s_preparedLock;
908 std::condition_variable s_preparedDone;
909 std::map<int, PreparedMedia> s_prepared;
910 uint64_t s_preparedGen = 0;
911 // Threads building a prepare or deleting one never played, under
912 // s_preparedLock (s_preparedDone when it drops): fppd's shutdown waits for
913 // them, as a prepared video holds the display's lease until it is deleted.
914 int s_preparedThreads = 0;
915
916 105 void PreparedThreadDone() {
917
1/2
✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
105 std::lock_guard<std::mutex> lock(s_preparedLock);
918 105 s_preparedThreads--;
919 105 s_preparedDone.notify_all();
920 105 }
921 // How long a claim waits for a prepare still under way: about what building
922 // the output cold would take anyway.
923 constexpr int PREPARED_CLAIM_WAIT_MS = 2000;
924 // How long a prepare waits to be claimed before it is thrown away. A
925 // playlist that stops discards its own, but a MultiSync remote can't tell
926 // the master's last Stop from the one between two tracks, and an unclaimed
927 // prepare holds a decoder and keeps the display up. Prepares are made about
928 // 1.5 s before they are needed.
929 constexpr int PREPARED_EXPIRY_MS = 6000;
930 // How soon an expiry that found its prepare still building looks again.
931 constexpr int PREPARED_EXPIRY_RECHECK_MS = 1000;
932
933 // Off the caller's thread: deleting an output takes its pipeline down.
934 189 void DeleteUnplayed(MediaOutputBase* out) {
935
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 182 times.
189 if (out) {
936 {
937
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 std::lock_guard<std::mutex> lock(s_preparedLock);
938 7 s_preparedThreads++;
939 7 }
940 14 std::thread([out]() {
941
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 SetThreadName("FPP-MediaUnprep");
942
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 delete out;
943 7 PreparedThreadDone();
944
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
14 }).detach();
945 }
946 189 }
947
948 // Throw away slot's prepare `gen` if it is still unclaimed in `inMs`. Each
949 // prepare has a timer of its own: a timer of the same name replaces the last
950 // (Timers::addTimer()), and one slot's prepare cancelled another's expiry.
951 // One still building then looks again a little later.
952 98 void SchedulePreparedExpiry(int streamSlot, uint64_t gen, int inMs) {
953
4/8
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 98 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 98 times.
✗ Branch 7 not taken.
98 std::string name = "PreparedMediaExpiry" + std::to_string(streamSlot) + "_" + std::to_string(gen);
954
2/6
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
98 Timers::INSTANCE.addTimer(name, GetTimeMS() + inMs, [streamSlot, gen]() {
955 43 MediaOutputBase* expired = nullptr;
956 43 bool building = false;
957 {
958
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 std::lock_guard<std::mutex> lock(s_preparedLock);
959
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 auto it = s_prepared.find(streamSlot);
960
6/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 41 times.
✓ Branch 5 taken 2 times.
43 if (it == s_prepared.end() || it->second.gen != gen) {
961 41 return;
962 }
963 2 building = it->second.building;
964
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 41 times.
43 }
965
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (building) {
966 ✗ SchedulePreparedExpiry(streamSlot, gen, PREPARED_EXPIRY_RECHECK_MS);
967 ✗ return;
968 }
969 {
970
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::lock_guard<std::mutex> lock(s_preparedLock);
971
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto it = s_prepared.find(streamSlot);
972
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
2 if (it == s_prepared.end() || it->second.gen != gen || it->second.building) {
973 ✗ return;
974 }
975
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogDebug(VB_MEDIAOUT, "Discarding %s prepared for slot %d: not played\n", it->second.file.c_str(), streamSlot);
976 2 expired = it->second.out;
977
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 s_prepared.erase(it);
978
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 }
979 2 DeleteUnplayed(expired);
980 });
981 98 }
982 } // namespace
983
984 98 void PrepareMediaOutput(const std::string& mediaFilename, const std::string& videoOut, int streamSlot) {
985 #ifdef HAS_GSTREAMER
986 98 std::string ext;
987 98 std::size_t dot = mediaFilename.find_last_of(".");
988
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 if (dot != std::string::npos) {
989
2/4
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
98 ext = toLowerCopy(mediaFilename.substr(dot + 1));
990 }
991
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 std::string vo = videoOut;
992 // A video to a display, or (on PipeWire) anything whose sound plays
993 // through the slot's output stream, where the next track then carries on
994 // the stream's timeline (PipeWireOutputStream::ContinueTimeline()). What
995 // can't be prepared after all, GStreamerOutput::Prepare() turns down.
996
6/8
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 66 times.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 30 times.
✓ Branch 7 taken 2 times.
98 bool displayVideo = IsExtensionVideo(ext) && IsHDMIOut(vo);
997
7/12
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 98 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 32 times.
✓ Branch 7 taken 66 times.
✓ Branch 8 taken 32 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 32 times.
✗ Branch 11 not taken.
98 bool streamed = isPipeWireBackend() && (IsExtensionAudio(ext) || IsExtensionVideo(ext));
998
3/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 68 times.
98 if (!displayVideo && !streamed) {
999 ✗ return;
1000 }
1001 98 MediaOutputBase* old = nullptr;
1002 uint64_t gen;
1003 {
1004
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 std::lock_guard<std::mutex> lock(s_preparedLock);
1005
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 PreparedMedia& p = s_prepared[streamSlot];
1006
2/10
✗ Branch 0 not taken.
✓ Branch 1 taken 98 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 taken 98 times.
98 if (p.file == mediaFilename && p.videoOut == videoOut && (p.out || p.building)) {
1007 ✗ return;
1008 }
1009 98 old = p.out;
1010
5/8
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 96 times.
98 if (!p.file.empty() && (p.out || p.building)) {
1011
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogDebug(VB_MEDIAOUT, "Discarding %s prepared for slot %d: %s prepared instead\n",
1012 p.file.c_str(), streamSlot, mediaFilename.c_str());
1013 }
1014
2/6
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
98 p = PreparedMedia{ mediaFilename, videoOut, nullptr, true, ++s_preparedGen };
1015 98 gen = p.gen;
1016
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 }
1017
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 DeleteUnplayed(old);
1018
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 LogDebug(VB_MEDIAOUT, "Preparing %s for slot %d\n", mediaFilename.c_str(), streamSlot);
1019
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 SchedulePreparedExpiry(streamSlot, gen, PREPARED_EXPIRY_MS);
1020 // What CreateMediaOutput() would build it with: sound alone for an audio file.
1021
10/16
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 68 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 66 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 66 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 66 times.
✓ Branch 13 taken 32 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
164 std::string outVideoOut = displayVideo ? videoOut : (IsExtensionAudio(ext) ? std::string("--Disabled--") : videoOut);
1022 {
1023
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 std::lock_guard<std::mutex> lock(s_preparedLock);
1024 98 s_preparedThreads++;
1025 98 }
1026
2/8
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
294 std::thread([mediaFilename, outVideoOut, streamSlot, gen]() {
1027
2/4
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
98 SetThreadName("FPP-MediaPrep");
1028 // Not CreateMediaOutput(): that writes the slot's status, which is
1029 // the playing track's until this one starts.
1030 264 GStreamerOutput* out = new GStreamerOutput(mediaFilename, StreamSlotManager::Instance().GetStatus(streamSlot),
1031
4/10
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 98 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 98 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
98 outVideoOut, streamSlot);
1032 98 bool ok = out->Prepare();
1033 {
1034
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 std::lock_guard<std::mutex> lock(s_preparedLock);
1035
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 auto it = s_prepared.find(streamSlot);
1036
3/6
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 98 times.
✗ Branch 5 not taken.
98 if (it != s_prepared.end() && it->second.gen == gen) {
1037 98 it->second.building = false;
1038
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 5 times.
98 if (ok) {
1039 93 it->second.out = out;
1040 93 out = nullptr;
1041 }
1042 }
1043 98 s_preparedDone.notify_all();
1044 98 }
1045 // Not prepared, or no longer wanted.
1046
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 93 times.
98 delete out;
1047 98 PreparedThreadDone();
1048
2/4
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
196 }).detach();
1049 #endif
1050
2/4
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
98 }
1051
1052 280 MediaOutputBase* ClaimPreparedMediaOutput(const std::string& mediaFilename, const std::string& videoOut, int streamSlot) {
1053 280 MediaOutputBase* out = nullptr;
1054 280 MediaOutputBase* unwanted = nullptr;
1055 {
1056
1/2
✓ Branch 0 taken 280 times.
✗ Branch 1 not taken.
280 std::unique_lock<std::mutex> lock(s_preparedLock);
1057
1/2
✓ Branch 0 taken 280 times.
✗ Branch 1 not taken.
280 auto it = s_prepared.find(streamSlot);
1058
2/2
✓ Branch 0 taken 194 times.
✓ Branch 1 taken 86 times.
280 if (it == s_prepared.end()) {
1059 194 return nullptr;
1060 }
1061
3/6
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 86 times.
✗ Branch 5 not taken.
86 if (it->second.file == mediaFilename && it->second.videoOut == videoOut) {
1062 86 uint64_t gen = it->second.gen;
1063
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 s_preparedDone.wait_for(lock, std::chrono::milliseconds(PREPARED_CLAIM_WAIT_MS), [&]() {
1064
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 auto cur = s_prepared.find(streamSlot);
1065
3/6
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 86 times.
✗ Branch 5 not taken.
86 return cur == s_prepared.end() || cur->second.gen != gen || !cur->second.building;
1066 });
1067
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 it = s_prepared.find(streamSlot);
1068
3/6
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 86 times.
✗ Branch 5 not taken.
86 if (it != s_prepared.end() && it->second.gen == gen) {
1069 86 out = it->second.out;
1070
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 s_prepared.erase(it);
1071 }
1072 } else {
1073 ✗ LogDebug(VB_MEDIAOUT, "Discarding %s prepared for slot %d: %s plays instead\n",
1074 it->second.file.c_str(), streamSlot, mediaFilename.c_str());
1075 ✗ unwanted = it->second.out;
1076 ✗ s_prepared.erase(it);
1077 }
1078
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 194 times.
280 }
1079 86 DeleteUnplayed(unwanted);
1080
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 5 times.
86 if (out) {
1081 // What CreateMediaOutput() would have set: the display, for a video to it.
1082 81 std::string ext;
1083 81 std::size_t dot = mediaFilename.find_last_of(".");
1084
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 if (dot != std::string::npos) {
1085
2/4
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
✗ Branch 3 not taken.
81 ext = toLowerCopy(mediaFilename.substr(dot + 1));
1086 }
1087
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 std::string vo = videoOut;
1088
2/4
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
✗ Branch 3 not taken.
81 StreamSlotManager::Instance().GetStatus(streamSlot)->output =
1089
8/12
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 61 times.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 18 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 18 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 81 times.
✗ Branch 11 not taken.
162 (IsExtensionVideo(ext) && IsHDMIOut(vo)) ? videoOut : std::string();
1090
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 LogDebug(VB_MEDIAOUT, "Using %s prepared for slot %d\n", mediaFilename.c_str(), streamSlot);
1091 81 }
1092 86 return out;
1093 }
1094
1095 // fppd's shutdown: every prepare deleted here, and any still building or
1096 // being deleted on its own thread waited for (bounded), so that nothing holds
1097 // a display's lease once VideoDisplay::ShutdownAll() returns.
1098 2620 void ShutdownPreparedMediaOutput() {
1099 2620 std::vector<MediaOutputBase*> unwanted;
1100 {
1101
1/2
✓ Branch 0 taken 2620 times.
✗ Branch 1 not taken.
2620 std::lock_guard<std::mutex> lock(s_preparedLock);
1102
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2620 times.
2625 for (auto& [slot, p] : s_prepared) {
1103
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 unwanted.push_back(p.out);
1104 }
1105 2620 s_prepared.clear();
1106 2620 }
1107
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2620 times.
2625 for (MediaOutputBase* out : unwanted) {
1108
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 delete out;
1109 }
1110
1/2
✓ Branch 0 taken 2620 times.
✗ Branch 1 not taken.
2620 std::unique_lock<std::mutex> lock(s_preparedLock);
1111
2/4
✓ Branch 0 taken 2620 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2620 times.
5240 if (!s_preparedDone.wait_for(lock, std::chrono::seconds(15), [] { return s_preparedThreads == 0; })) {
1112 ✗ LogWarn(VB_MEDIAOUT, "%d prepared media still building or closing at shutdown\n", s_preparedThreads);
1113 }
1114 2620 }
1115
1116 4167 void DiscardPreparedMediaOutput(int streamSlot) {
1117 4167 std::vector<MediaOutputBase*> unwanted;
1118 {
1119
1/2
✓ Branch 0 taken 4167 times.
✗ Branch 1 not taken.
4167 std::lock_guard<std::mutex> lock(s_preparedLock);
1120
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4167 times.
4170 for (auto it = s_prepared.begin(); it != s_prepared.end();) {
1121
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 if (streamSlot == 0 || it->first == streamSlot) {
1122
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogDebug(VB_MEDIAOUT, "Discarding %s prepared for slot %d\n", it->second.file.c_str(), it->first);
1123
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 unwanted.push_back(it->second.out);
1124
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 it = s_prepared.erase(it);
1125 } else {
1126 ✗ ++it;
1127 }
1128 }
1129 4167 }
1130
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4167 times.
4170 for (MediaOutputBase* out : unwanted) {
1131
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 DeleteUnplayed(out);
1132 }
1133 4167 }
1134
1135 static std::set<std::string> alreadyWarned;
1136
1137 // What a remote plays for the master's `filename`: the video of that name if
1138 // there is one, else the file itself if it has sound; "" if neither.
1139 49 static std::string RemoteMediaFor(const std::string& filename) {
1140 49 std::size_t found = filename.find_last_of(".");
1141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (found == std::string::npos) {
1142 ✗ return "";
1143 }
1144
2/4
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
49 std::string ext = toLowerCopy(filename.substr(found + 1));
1145
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 std::string tmpFile = GetVideoFilenameForMedia(filename, ext);
1146
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 std::string audio = filename; // HasAudioForMedia() may rewrite it to the file found
1147
8/10
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 35 times.
✓ Branch 7 taken 7 times.
✓ Branch 8 taken 35 times.
✓ Branch 9 taken 14 times.
49 if (tmpFile == "" && HasAudioForMedia(audio)) {
1148
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 tmpFile = audio;
1149 }
1150 49 return tmpFile;
1151 49 }
1152
1153 3 void PrepareRemoteMediaOutput(const std::string& filename) {
1154
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (getFPPmode() != REMOTE_MODE) {
1155 ✗ return;
1156 }
1157
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::string tmpFile = RemoteMediaFor(filename);
1158
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (!tmpFile.empty()) {
1159
3/6
✓ 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.
6 PrepareMediaOutput(tmpFile, ResolveVideoOutput("--Default--"), 1);
1160 }
1161 3 }
1162
1163 /*
1164 *
1165 */
1166 63 int OpenMediaOutput(const std::string& filename) {
1167
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 LogDebug(VB_MEDIAOUT, "OpenMediaOutput(%s)\n", filename.c_str());
1168
1169
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 std::unique_lock<std::mutex> lock(mediaOutputLock);
1170
1171 try {
1172
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 62 times.
63 if (mediaOutput) {
1173
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 lock.unlock();
1174
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CloseMediaOutput();
1175 }
1176 // Only unlock here if we didn't already unlock above -- unlocking an
1177 // already-unlocked mutex throws std::system_error, which was being
1178 // mis-caught below and reported as "Error starting media" any time a
1179 // previous mediaOutput existed (e.g. remote-mode UpdateMasterMediaPosition).
1180
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 1 times.
63 if (lock.owns_lock()) {
1181
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 lock.unlock();
1182 }
1183
1184
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 std::string tmpFile(filename);
1185 63 std::size_t found = tmpFile.find_last_of(".");
1186
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 49 times.
63 if (found == std::string::npos) {
1187
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 LogDebug(VB_MEDIAOUT, "Unable to determine extension of media file %s\n",
1188 tmpFile.c_str());
1189 14 return 0;
1190 }
1191
2/4
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
49 std::string ext = toLowerCopy(tmpFile.substr(found + 1));
1192
1193
3/4
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 3 times.
49 if (getFPPmode() == REMOTE_MODE) {
1194
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 tmpFile = RemoteMediaFor(tmpFile);
1195
1196
3/4
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 39 times.
46 if (tmpFile == "") {
1197 // For v1.0 MultiSync, we can't sync audio to audio, so check for
1198 // a video file if the master is playing an audio file
1199 // video doesn't exist, punt
1200
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 tmpFile = filename;
1201
3/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 5 times.
7 if (alreadyWarned.find(tmpFile) == alreadyWarned.end()) {
1202 // The names come from sync packets; a show has a few
1203 // hundred at most, and a stream of made-up ones must not
1204 // grow this for good.
1205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (alreadyWarned.size() >= 1000) {
1206 ✗ alreadyWarned.clear();
1207 }
1208
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 alreadyWarned.emplace(tmpFile);
1209
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogDebug(VB_MEDIAOUT, "No video found for remote playing of %s\n", filename.c_str());
1210 }
1211 7 return 0;
1212 } else {
1213
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 LogDebug(VB_MEDIAOUT,
1214 "Player is playing %s audio, remote will try %s\n",
1215 filename.c_str(), tmpFile.c_str());
1216 }
1217 }
1218
1219
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 std::string vOut = ResolveVideoOutput("--Default--");
1220
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 LogDebug(VB_MEDIAOUT, "OpenMediaOutput: Creating media output for '%s' vOut='%s'\n", tmpFile.c_str(), vOut.c_str());
1221 // A remote may have it ready (the master's SYNC_PKT_PREPARE). Only
1222 // a remote: elsewhere this is Play Media and the like, which must not
1223 // discard what a playlist prepared.
1224
4/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 39 times.
✗ Branch 5 not taken.
42 MediaOutputBase* out = getFPPmode() == REMOTE_MODE ? ClaimPreparedMediaOutput(tmpFile, vOut, 1) : nullptr;
1225
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 2 times.
42 if (!out) {
1226
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 out = CreateMediaOutput(tmpFile, vOut);
1227 }
1228
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 LogDebug(VB_MEDIAOUT, "OpenMediaOutput: CreateMediaOutput returned %p\n", out);
1229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!out) {
1230 ✗ LogErr(VB_MEDIAOUT, "No Media Output handler for %s\n", tmpFile.c_str());
1231 ✗ WarningHolder::AddWarningTimeout(60, 30, "No media output handler for " + tmpFile + " (unsupported file type?)");
1232 ✗ return 0;
1233 }
1234
1235 // Install under a short lock, then run the plugin callbacks with the
1236 // lock RELEASED. playlistCallback/mediaCallback execute arbitrary
1237 // plugin code inline — a native plugin that reaches anything taking
1238 // mediaOutputLock (setVolume, CloseMediaOutput) would self-deadlock,
1239 // and a script plugin would hold the lock for its entire fork/wait
1240 // run. Same defect and fix as PlaylistEntryMedia::OpenMediaOutput.
1241
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 lock.lock();
1242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (mediaOutput) {
1243 // Another thread opened media while we were creating ours; it
1244 // won the race. Discard ours (teardown outside the lock).
1245 ✗ lock.unlock();
1246 ✗ LogWarn(VB_MEDIAOUT, "OpenMediaOutput: another media output was opened concurrently, discarding %s\n", tmpFile.c_str());
1247 ✗ delete out;
1248 ✗ return 0;
1249 }
1250 42 mediaOutput = out;
1251
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 lock.unlock();
1252
1253
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 Json::Value root;
1254
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 root["currentEntry"]["type"] = "media";
1255
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 root["currentEntry"]["mediaFilename"] = out->m_mediaFilename;
1256
1257
7/8
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 27 times.
✓ Branch 7 taken 15 times.
42 if (getFPPmode() == REMOTE_MODE && firstOutCreate) {
1258 27 firstOutCreate = false;
1259 // need to "fake" a playlist start as some plugins will not initialize
1260 // until a playlist is started, but remotes don't have playlists
1261
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 root["name"] = "FPP Remote";
1262
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 root["desc"] = "FPP Remote Mode";
1263
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 root["loop"] = false;
1264
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 root["repeat"] = false;
1265
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 root["random"] = false;
1266
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 root["size"] = 1;
1267
3/6
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
108 PluginManager::INSTANCE.playlistCallback(root, "start", "Main", 0);
1268 }
1269
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 MediaDetails::INSTANCE.ParseMedia(out->m_mediaFilename.c_str());
1270
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 PluginManager::INSTANCE.mediaCallback(root, MediaDetails::INSTANCE);
1271
1272
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 39 times.
42 if (multiSync->isMultiSyncEnabled()) {
1273
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 multiSync->SendMediaOpenPacket(out->m_mediaFilename);
1274 }
1275 42 return 1;
1276
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
63 } catch (const std::system_error& e) {
1277 ✗ LogErr(VB_MEDIAOUT, "System exception starting media for %s. Code: %d What: %s\n", filename.c_str(), e.code().value(), e.what());
1278 ✗ WarningHolder::AddWarningTimeout(60, 30, "Error starting media " + filename + ": " + e.what());
1279 ✗ return 0;
1280 ✗ }
1281 63 }
1282 1458 bool MatchesRunningMediaFilename(const std::string& filename) {
1283
2/2
✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 18 times.
1458 if (mediaOutput) {
1284
1/2
✓ Branch 0 taken 1440 times.
✗ Branch 1 not taken.
1440 std::string tmpFile = filename;
1285
3/4
✓ Branch 0 taken 1440 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1327 times.
✓ Branch 3 taken 113 times.
1440 if (HasAudioForMedia(tmpFile)) {
1286
6/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1320 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1324 times.
✓ Branch 5 taken 3 times.
1327 if (mediaOutput->m_mediaFilename == tmpFile || mediaOutput->m_mediaFilename == filename) {
1287 1324 return true;
1288 }
1289 }
1290
1/2
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
116 tmpFile = filename;
1291
3/4
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 113 times.
✓ Branch 3 taken 3 times.
116 if (HasVideoForMedia(tmpFile)) {
1292
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 113 times.
✗ Branch 5 not taken.
113 if (mediaOutput->m_mediaFilename == tmpFile || mediaOutput->m_mediaFilename == filename) {
1293 113 return true;
1294 }
1295 }
1296
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1437 times.
1440 }
1297 21 return false;
1298 }
1299
1300 53 int StartMediaOutput(const std::string& filename, int msTime) {
1301
3/4
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 42 times.
53 if (!MatchesRunningMediaFilename(filename)) {
1302
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 CloseMediaOutput();
1303 }
1304
1305
5/8
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 42 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 53 times.
53 if (mediaOutput && mediaOutput->IsPlaying()) {
1306 ✗ CloseMediaOutput();
1307 }
1308
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 42 times.
53 if (!mediaOutput) {
1309
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 OpenMediaOutput(filename);
1310 }
1311
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 42 times.
53 if (!mediaOutput) {
1312 11 return 0;
1313 }
1314
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 std::unique_lock<std::mutex> lock(mediaOutputLock);
1315 // Snapshot under the lock — the pre-flight checks above read the global
1316 // unlocked, and a concurrent CloseMediaOutput() may have nulled it.
1317 42 MediaOutputBase* out = mediaOutput;
1318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!out) {
1319 ✗ return 0;
1320 }
1321
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 39 times.
42 if (multiSync->isMultiSyncEnabled()) {
1322
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 multiSync->SendMediaSyncStartPacket(out->m_mediaFilename);
1323 }
1324
1325 // mediaOutputStatus is global and shared by every media output, and only
1326 // Process() ever writes the elapsed fields -- so until the new pipeline
1327 // reports its first position they still describe the *previous* item. On a
1328 // synced remote that stale value is actively dangerous: the first sync
1329 // packet after a clip change hands AdjustSpeed() a ~15s "local position"
1330 // against a master position of ~1s, it reads that as 14s out of sync and
1331 // issues a flushing seek into a pipeline that has not even prerolled yet
1332 // (issue #2727). Start every item from zero instead; AdjustSpeed()'s own
1333 // "not playing yet" guard then holds until a real position arrives.
1334 {
1335
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 std::lock_guard<std::mutex> lock(MediaOutputStatus::FieldsLock());
1336 42 mediaOutputStatus.mediaSeconds = 0.0;
1337 42 mediaOutputStatus.secondsElapsed = 0;
1338 42 mediaOutputStatus.subSecondsElapsed = 0;
1339 42 mediaOutputStatus.secondsRemaining = 0;
1340 42 mediaOutputStatus.subSecondsRemaining = 0;
1341 42 }
1342
1343
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 LogDebug(VB_MEDIAOUT, "StartMediaOutput: Calling Start(%d) on mediaOutput=%p\n", msTime, out);
1344
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 42 times.
42 if (!out->Start(msTime)) {
1345 ✗ LogErr(VB_MEDIAOUT, "Could not start media %s\n", out->m_mediaFilename.c_str());
1346 ✗ WarningHolder::AddWarningTimeout(60, 30, "Could not start media " + out->m_mediaFilename);
1347 ✗ mediaOutput = 0;
1348 ✗ lock.unlock();
1349 // Teardown (GStreamer state change + sleeps) never under the lock.
1350 ✗ delete out;
1351 ✗ return 0;
1352 }
1353 // MEDIA_STARTED preset commands run INLINE on this thread. A preset
1354 // containing a volume command takes mediaOutputLock again (self-deadlock
1355 // on this non-recursive mutex); one containing a playlist command takes
1356 // m_playlistMutex under mediaOutputLock, the reverse of every other
1357 // thread's order (AB-BA). Release before triggering.
1358
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 lock.unlock();
1359 42 std::map<std::string, std::string> keywords;
1360
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.
84 keywords["MEDIA_NAME"] = filename;
1361
4/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 41 times.
84 if (CommandManager::INSTANCE.HasPreset("MEDIA_STARTED")) {
1362
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 CommandManager::INSTANCE.TriggerPreset("MEDIA_STARTED", keywords);
1363 }
1364
1365 42 return 1;
1366 42 }
1367 2668 void CloseMediaOutput() {
1368
1/2
✓ Branch 0 taken 2668 times.
✗ Branch 1 not taken.
2668 LogDebug(VB_MEDIAOUT, "CloseMediaOutput()\n");
1369
1370 2668 mediaOutputStatus.status = MEDIAOUTPUTSTATUS_IDLE;
1371
1372
1/2
✓ Branch 0 taken 2668 times.
✗ Branch 1 not taken.
2668 std::unique_lock<std::mutex> lock(mediaOutputLock);
1373
2/2
✓ Branch 0 taken 2626 times.
✓ Branch 1 taken 42 times.
2668 if (!mediaOutput) {
1374 2626 return;
1375 }
1376
1377 // Move the global pointer into a local and clear the global while still
1378 // holding the lock, then release the lock before the (potentially
1379 // ~400ms) Stop()/teardown. This ensures only one thread can ever "win"
1380 // the null-check above -- a second, concurrent caller (e.g. the
1381 // MultiSync thread racing the playlist thread in remote mode) will see
1382 // mediaOutput already null and return immediately, instead of both
1383 // threads operating on (and eventually deleting) the same object.
1384 42 MediaOutputBase* out = mediaOutput;
1385 42 mediaOutput = nullptr;
1386
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 lock.unlock();
1387
1388
3/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 3 times.
42 if (getFPPmode() == REMOTE_MODE) {
1389
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 std::unique_lock<std::mutex> closedLock(lastClosedMediaLock);
1390
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 lastClosedMediaName = out->m_mediaFilename;
1391
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 lastClosedMediaMS = GetTimeMS();
1392 39 lastClosedMediaPos = masterMediaPosition;
1393 39 masterMediaPosition = 0.0f; // the next media starts from its own syncs
1394 39 }
1395
1396
3/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 4 times.
42 if (out->IsPlaying()) {
1397
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 out->Stop();
1398 }
1399
1400
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 39 times.
42 if (multiSync->isMultiSyncEnabled()) {
1401
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 multiSync->SendMediaSyncStopPacket(out->m_mediaFilename);
1402 }
1403
1404 42 std::map<std::string, std::string> keywords;
1405
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.
126 keywords["MEDIA_NAME"] = out->m_mediaFilename;
1406
3/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 42 times.
84 if (CommandManager::INSTANCE.HasPreset("MEDIA_STOPPED")) {
1407 ✗ CommandManager::INSTANCE.TriggerPreset("MEDIA_STOPPED", keywords);
1408 }
1409
1410
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 delete out;
1411
1412
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 Json::Value root;
1413
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 root["name"] = "FPP Remote";
1414
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 root["desc"] = "FPP Remote Mode";
1415
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 root["currentEntry"]["type"] = "media";
1416
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 root["currentEntry"]["mediaFilename"] = "";
1417
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 MediaDetails::INSTANCE.Clear();
1418
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 PluginManager::INSTANCE.mediaCallback(root, MediaDetails::INSTANCE);
1419
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 2626 times.
2668 }
1420
1421 1373 void UpdateMasterMediaPosition(const std::string& filename, float seconds) {
1422
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1370 times.
1373 if (getFPPmode() != REMOTE_MODE) {
1423 3 return;
1424 }
1425
1426
2/2
✓ Branch 0 taken 1361 times.
✓ Branch 1 taken 9 times.
1370 if (MatchesRunningMediaFilename(filename)) {
1427 1361 masterMediaPosition = seconds;
1428
1/2
✓ Branch 0 taken 1361 times.
✗ Branch 1 not taken.
1361 std::unique_lock<std::mutex> lock(mediaOutputLock);
1429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1361 times.
1361 if (!mediaOutput) {
1430 ✗ return;
1431 }
1432
1/2
✓ Branch 0 taken 1361 times.
✗ Branch 1 not taken.
1361 mediaOutput->AdjustSpeed(seconds);
1433 1361 return;
1434 1361 } else {
1435 {
1436 // A SYNC for media we just closed, still positioned where the
1437 // master was when it stopped, is a straggler. Joining mid-stream
1438 // on it would reopen stopped media or tear down what replaced it.
1439
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 std::unique_lock<std::mutex> closedLock(lastClosedMediaLock);
1440 // The output keeps the resolved path; a packet carries the name
1441 // the master played.
1442
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 std::string resolved = filename;
1443 9 bool sameFile = filename == lastClosedMediaName;
1444
6/8
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 5 times.
9 if (!sameFile && HasAudioForMedia(resolved)) {
1445 4 sameFile = resolved == lastClosedMediaName;
1446 }
1447
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if (!sameFile) {
1448
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 resolved = filename;
1449
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (HasVideoForMedia(resolved)) {
1450 ✗ sameFile = resolved == lastClosedMediaName;
1451 }
1452 }
1453
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (sameFile && lastClosedMediaMS != 0 && lastClosedMediaPos > 0.0f &&
1454
6/8
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 7 times.
15 (GetTimeMS() - lastClosedMediaMS) < STALE_SYNC_WINDOW_MS &&
1455
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 seconds >= lastClosedMediaPos - STALE_SYNC_SLACK_SEC) {
1456
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogDebug(VB_SYNC, "Ignoring stale sync for just-closed media %s (%.2f, stopped at %.2f)\n",
1457 filename.c_str(), seconds, lastClosedMediaPos);
1458 2 return;
1459 }
1460
4/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 2 times.
11 }
1461
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 OpenMediaOutput(filename);
1462
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 int msTime = (seconds > 0.0f) ? (int)(seconds * 1000.0f) : 0;
1463
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 StartMediaOutput(filename, msTime);
1464 7 masterMediaPosition = seconds;
1465
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 std::unique_lock<std::mutex> lock(mediaOutputLock);
1466
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
7 if (!mediaOutput) {
1467 5 return;
1468 }
1469
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 mediaOutput->AdjustSpeed(seconds);
1470 2 return;
1471 7 }
1472 }
1473