GCC Code Coverage Report


Directory: ./
File: mediaoutput/VideoOutputManager.cpp
Date: 2026-10-29 08:11:58
Exec Total Coverage
Lines: 1066 1244 85.7%
Functions: 61 70 87.1%
Branches: 1212 2239 54.1%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2026 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12
13 #include "fpp-pch.h"
14
15 #include "Warnings.h" // WarningHolder -- needed directly for NOPCH builds
16
17 #include "VideoOutputManager.h"
18 #include "VideoInputManager.h"
19 #include "StreamSlotManager.h"
20 #include "GStreamerOut.h"
21 #include "common.h"
22 #include "settings.h"
23 #include "log.h"
24 #include "overlays/PixelOverlay.h"
25 #include "overlays/PixelOverlayModel.h"
26
27 #include <algorithm> // any_of -- one output per connector (needed directly for NOPCH builds)
28 #include <cmath> // lround -- crop fractions to pixels (needed directly for NOPCH builds)
29 #include <fstream>
30 #include <cctype>
31 #include <cstring>
32 #include <thread>
33 #include <arpa/inet.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <ifaddrs.h>
37 #include <unistd.h>
38 #include <sys/wait.h>
39 #include <fcntl.h>
40
41 #if __has_include(<gst/app/gstappsink.h>)
42 #include <gst/app/gstappsink.h>
43 #endif
44
45 #include "fpp-json.h"
46 #include "../SysSocket.h"
47
48 // SAP (Session Announcement Protocol) constants — RFC 2974
49 static constexpr const char* SAP_MCAST_ADDR = "239.255.255.255";
50 static constexpr int SAP_PORT = 9875;
51 static constexpr int SAP_TTL = 255;
52 static constexpr int SAP_VERSION = 1;
53 static constexpr int SAP_INTERVAL_S = 30;
54
55 #ifdef HAS_GSTREAMER_VIDEO_OUTPUT
56 // Resolve a fractional VideoCropRect against a concrete frame size and push the
57 // result into a videocrop element. Offsets and extents are forced even: NV12 /
58 // I420 subsample chroma 2x2, and an odd source rectangle on a DRM plane splits
59 // a chroma pair between the two halves of a split, tinting the seam.
60 3 static void ApplyCropForFrameSize(GstElement* cropElem, const VideoCropRect& crop,
61 int frameW, int frameH) {
62
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (frameW <= 0 || frameH <= 0)
63 ✗ return;
64
65
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 auto evenDown = [](long v) { return (int)(v < 0 ? 0 : (v & ~1L)); };
66
67 3 int left = evenDown(lround(crop.x * frameW));
68 3 int top = evenDown(lround(crop.y * frameH));
69 3 int cropW = evenDown(lround(crop.width * frameW));
70 3 int cropH = evenDown(lround(crop.height * frameH));
71
72 // Keep the region inside the frame even if the fractions round outward.
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (left >= frameW) left = evenDown(frameW - 2);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (top >= frameH) top = evenDown(frameH - 2);
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (left + cropW > frameW) cropW = evenDown(frameW - left);
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (top + cropH > frameH) cropH = evenDown(frameH - top);
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (cropW < 2) cropW = 2;
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (cropH < 2) cropH = 2;
79
80 // "fill": trim the region, centred, down to the display's shape so the
81 // aspect-preserving scale downstream covers the display with no bars.
82 // Done here rather than with an aspectratiocrop element so the whole crop
83 // stays one videocrop next to the sink, where it rides along as the DRM
84 // plane's source rectangle instead of copying every frame.
85
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (crop.targetAspect > 0.0) {
86 1 double regionAspect = (double)cropW / (double)cropH;
87
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (regionAspect > crop.targetAspect) {
88 // Region is wider than the display — trim the sides.
89 1 int fillW = evenDown(lround(cropH * crop.targetAspect));
90
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (fillW >= 2 && fillW < cropW) {
91 1 left += evenDown((cropW - fillW) / 2);
92 1 cropW = fillW;
93 }
94 ✗ } else if (regionAspect < crop.targetAspect) {
95 // Region is taller than the display — trim top and bottom.
96 ✗ int fillH = evenDown(lround(cropW / crop.targetAspect));
97 ✗ if (fillH >= 2 && fillH < cropH) {
98 ✗ top += evenDown((cropH - fillH) / 2);
99 ✗ cropH = fillH;
100 }
101 }
102 }
103
104 3 g_object_set(cropElem,
105 "left", left,
106 "top", top,
107 3 "right", frameW - left - cropW,
108
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 "bottom", frameH - top - cropH,
109 NULL);
110
111
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogDebug(VB_MEDIAOUT, "VideoOutputManager: crop %s → source %dx%d, region %dx%d+%d+%d\n",
112 GST_ELEMENT_NAME(cropElem), frameW, frameH, cropW, cropH, left, top);
113 }
114
115 // Watches the caps arriving at a videocrop element and re-resolves the
116 // fractional region whenever the source resolution is announced. Runs before
117 // the first buffer, so the very first frame is already cropped -- important
118 // here because the alternative (setting the crop after PLAYING) would flash a
119 // full uncropped frame across both displays on every track change.
120 23 static GstPadProbeReturn CropCapsProbe(GstPad* pad, GstPadProbeInfo* info, gpointer userData) {
121 23 GstEvent* event = GST_PAD_PROBE_INFO_EVENT(info);
122
3/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 3 times.
23 if (!event || GST_EVENT_TYPE(event) != GST_EVENT_CAPS)
123 20 return GST_PAD_PROBE_OK;
124
125 3 GstCaps* caps = nullptr;
126
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 gst_event_parse_caps(event, &caps);
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!caps)
128 ✗ return GST_PAD_PROBE_OK;
129
130
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 GstStructure* s = gst_caps_get_structure(caps, 0);
131 3 int width = 0, height = 0;
132
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
6 if (s && gst_structure_get_int(s, "width", &width) &&
133
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 gst_structure_get_int(s, "height", &height)) {
134
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 GstElement* elem = gst_pad_get_parent_element(pad);
135
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (elem) {
136
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 ApplyCropForFrameSize(elem, *static_cast<const VideoCropRect*>(userData),
137 width, height);
138
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 gst_object_unref(elem);
139 }
140 }
141 3 return GST_PAD_PROBE_OK;
142 }
143
144 // Make an existing videocrop element track `crop` for whatever resolution the
145 // source turns out to be. Split out from CreateCropElement so the gst_parse_launch
146 // paths, which get their videocrop from the pipeline description, share it.
147 3 static void AttachCropResolver(GstElement* cropElem, const VideoCropRect& crop) {
148 // The probe outlives this call, so the region it reads has to as well.
149 // Owned by the element: freed with it, whichever bin ends up holding it.
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 VideoCropRect* cropCopy = new VideoCropRect(crop);
151
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 g_object_set_data_full(G_OBJECT(cropElem), "fpp-crop-rect", cropCopy,
152
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 [](gpointer p) { delete static_cast<VideoCropRect*>(p); });
153
154 3 GstPad* sinkPad = gst_element_get_static_pad(cropElem, "sink");
155
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (sinkPad) {
156 3 gst_pad_add_probe(sinkPad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
157 CropCapsProbe, cropCopy, nullptr);
158 3 gst_object_unref(sinkPad);
159 }
160
161
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 LogInfo(VB_MEDIAOUT, "VideoOutputManager: '%s' shows source region %.1f%%,%.1f%% %.1f%%x%.1f%%%s\n",
162 GST_ELEMENT_NAME(cropElem), crop.x * 100.0, crop.y * 100.0,
163 crop.width * 100.0, crop.height * 100.0,
164 crop.targetAspect > 0.0 ? ", cropped to the display's shape (fill)" : "");
165 3 }
166
167 16 GstElement* VideoOutputManager::CreateCropElement(const VideoCropRect& crop,
168 const std::string& name) {
169
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
16 if (crop.IsFullFrame())
170 14 return nullptr;
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!crop.IsValid()) {
172 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: ignoring invalid crop region "
173 "%.3f,%.3f %.3fx%.3f for '%s'\n",
174 crop.x, crop.y, crop.width, crop.height, name.c_str());
175 ✗ return nullptr;
176 }
177
178 2 GstElement* cropElem = gst_element_factory_make("videocrop", name.c_str());
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!cropElem) {
180 ✗ LogErr(VB_MEDIAOUT, "VideoOutputManager: videocrop element not available — "
181 "is gstreamer1.0-plugins-good installed? Output '%s' will show the full frame\n",
182 name.c_str());
183 // Permanent: the element appears only with a package install and a restart.
184 ✗ WarningHolder::AddWarning(31, "Video crop unavailable: videocrop element missing (install gstreamer1.0-plugins-good)");
185 ✗ return nullptr;
186 }
187
188 2 AttachCropResolver(cropElem, crop);
189 2 return cropElem;
190 }
191
192 16 std::vector<GstElement*> VideoOutputManager::CreateScaleElements(
193 const std::string& scaling, int displayWidth, int displayHeight,
194 const std::string& namePrefix) {
195 16 std::vector<GstElement*> elems;
196
197 // "fit" is what kmssink does unaided; "fill" is already handled by the
198 // crop's target aspect. Neither needs an element, and adding one would
199 // only cost a CPU rescale of every frame.
200
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 if (scaling != "stretch")
201 16 return elems;
202
203 ✗ if (displayWidth <= 0 || displayHeight <= 0) {
204 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: '%s' asked for stretch but the display "
205 "size is unknown — showing it fitted instead\n", namePrefix.c_str());
206 ✗ return elems;
207 }
208
209 // add-borders=false is the whole point: with the output caps pinned to the
210 // display's exact size and square pixels, videoscale has no freedom left
211 // and scales non-uniformly rather than letterboxing.
212 ✗ GstElement* scale = gst_element_factory_make("videoscale", (namePrefix + "_scale").c_str());
213 ✗ GstElement* caps = gst_element_factory_make("capsfilter", (namePrefix + "_caps").c_str());
214 ✗ if (!scale || !caps) {
215 ✗ LogErr(VB_MEDIAOUT, "VideoOutputManager: videoscale/capsfilter unavailable for '%s' — "
216 "showing it fitted instead\n", namePrefix.c_str());
217 ✗ if (scale) gst_object_unref(scale);
218 ✗ if (caps) gst_object_unref(caps);
219 ✗ return elems;
220 }
221
222 ✗ g_object_set(scale, "add-borders", FALSE, NULL);
223
224 ✗ GstCaps* c = gst_caps_new_simple("video/x-raw",
225 "width", G_TYPE_INT, displayWidth,
226 "height", G_TYPE_INT, displayHeight,
227 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
228 NULL);
229 ✗ g_object_set(caps, "caps", c, NULL);
230 ✗ gst_caps_unref(c);
231
232 ✗ LogInfo(VB_MEDIAOUT, "VideoOutputManager: '%s' stretched to %dx%d (aspect ratio ignored)\n",
233 namePrefix.c_str(), displayWidth, displayHeight);
234
235 ✗ elems.push_back(scale);
236 ✗ elems.push_back(caps);
237 ✗ return elems;
238 ✗ }
239 #endif
240
241 7021 VideoOutputManager& VideoOutputManager::Instance() {
242
3/4
✓ Branch 0 taken 1887 times.
✓ Branch 1 taken 5134 times.
✓ Branch 2 taken 1887 times.
✗ Branch 3 not taken.
7021 static VideoOutputManager instance;
243 7021 return instance;
244 }
245
246 ✗ VideoOutputManager::~VideoOutputManager() {
247 ✗ Shutdown();
248 ✗ }
249
250 1887 void VideoOutputManager::Init() {
251
1/2
✓ Branch 0 taken 1887 times.
✗ Branch 1 not taken.
1887 std::lock_guard<std::mutex> lock(m_mutex);
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1887 times.
1887 if (m_initialized)
253 ✗ return;
254
255 // Only init if PipeWire backend is active (Simple or Advanced)
256
3/4
✓ Branch 0 taken 1887 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1881 times.
1887 if (!isPipeWireBackend()) {
257
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 std::string backend = getSetting("MediaBackend");
258
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 LogDebug(VB_MEDIAOUT, "VideoOutputManager: Skipping init (MediaBackend=%s, not pipewire)\n", backend.c_str());
259 6 return;
260 6 }
261
262 // Check if video routing is configured
263
1/2
✓ Branch 0 taken 1881 times.
✗ Branch 1 not taken.
1881 std::string videoSink = getSetting("PipeWireVideoSinkName");
264
2/2
✓ Branch 0 taken 1796 times.
✓ Branch 1 taken 85 times.
1881 if (videoSink.empty()) {
265
1/2
✓ Branch 0 taken 1796 times.
✗ Branch 1 not taken.
1796 LogDebug(VB_MEDIAOUT, "VideoOutputManager: No PipeWireVideoSinkName configured, skipping\n");
266 1796 return;
267 }
268
269 85 m_initialized = true;
270
271 // Load config but don't start on-demand consumers yet — those start
272 // when GStreamerOut calls StartConsumers() after the producer is ready.
273 // However, consumers with a sourceNode (targeting a persistent video
274 // input source) can start immediately.
275
3/4
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✓ Branch 3 taken 19 times.
85 if (LoadConfig()) {
276 // Persistent consumers (those with a sourceNode) are NOT started
277 // here — they are deferred until NotifyProducerReady() is called
278 // by VideoInputManager once the source pipeline has its first
279 // video frame. This avoids the consumer spinning on expensive
280 // videoconvert+videoscale of black frames during the HLS buffering
281 // window (which can burn a full CPU core at 30fps 1080p).
282 66 int persistentCount = 0;
283
2/2
✓ Branch 0 taken 275 times.
✓ Branch 1 taken 66 times.
341 for (auto& c : m_consumers) {
284
2/2
✓ Branch 0 taken 243 times.
✓ Branch 1 taken 32 times.
275 if (!c.sourceNode.empty())
285 243 persistentCount++;
286 }
287
1/2
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
66 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Initialized with %d consumer(s) "
288 "(%d persistent deferred, %d on-demand)\n",
289 (int)m_consumers.size(), persistentCount,
290 (int)m_consumers.size() - persistentCount);
291 // Start SAP multicast announcer for any persistent RTP consumers
292
2/4
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 66 times.
66 if (HasRunningRtpLocked())
293 ✗ StartSAPAnnouncer();
294 }
295
4/4
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 1796 times.
✓ Branch 2 taken 85 times.
✓ Branch 3 taken 1802 times.
3683 }
296
297 94 void VideoOutputManager::Reload() {
298
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Reloading configuration\n");
299
300 // Stop SAP before taking the lock (SAP thread needs m_mutex)
301
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 StopSAPAnnouncer();
302 {
303
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 std::lock_guard<std::mutex> rl(m_restartMutex);
304 94 m_restarts.clear();
305 94 }
306
307 94 std::string savedProducer;
308 {
309
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 std::lock_guard<std::mutex> lock(m_mutex);
310
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 StopAllConsumers();
311 94 m_consumers.clear();
312
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 savedProducer = m_activeProducer;
313 94 }
314
315 // Wait for detached teardown threads to finish releasing DRM/CMA
316 // before starting new consumer pipelines.
317
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 WaitForPendingTeardowns(5000);
318
319 // Re-check if video routing is configured
320
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 std::string videoSink = getSetting("PipeWireVideoSinkName");
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 if (videoSink.empty()) {
322 ✗ LogInfo(VB_MEDIAOUT, "VideoOutputManager: Video routing disabled (no PipeWireVideoSinkName)\n");
323 ✗ return;
324 }
325
326 94 bool shouldStartSAP = false;
327 {
328
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 std::lock_guard<std::mutex> lock(m_mutex);
329 94 m_initialized = true;
330
331
3/4
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93 times.
✓ Branch 3 taken 1 times.
94 if (LoadConfig()) {
332
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Reloaded with %d consumer(s)\n", (int)m_consumers.size());
333 // If a producer was active, restart consumers targeting it
334
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 80 times.
93 if (!savedProducer.empty()) {
335
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 m_activeProducer = savedProducer;
336
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 for (auto& consumer : m_consumers) {
337
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 if (consumer.type == "rtp")
338 13 shouldStartSAP = true;
339
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
26 StartConsumer(consumer);
340 }
341 }
342
343 // A consumer bound to a video input source is normally started by
344 // NotifyProducerReady, which the source fires once its pipeline has
345 // a first frame. The stop above killed such a consumer while its
346 // source carried on running, and a source that never stopped will
347 // never fire that notification again — so without this the output
348 // stays dark after every Apply until fppd or the source restarts.
349 // That is what made a settings change look like it did nothing.
350
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 93 times.
186 for (auto& consumer : m_consumers) {
351
5/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 92 times.
✓ Branch 5 taken 1 times.
93 if (consumer.sourceNode.empty() || consumer.running)
352 92 continue;
353
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (!VideoInputManager::Instance().IsSourceRunning(consumer.sourceNode))
354 ✗ continue; // still deferred: its source will notify us
355
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (consumer.type == "rtp")
356 ✗ shouldStartSAP = true;
357
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Restarting consumer '%s' — source '%s' "
358 "is already running, so no producer-ready notification is coming\n",
359 consumer.name.c_str(), consumer.sourceNode.c_str());
360
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
2 if (!StartConsumer(consumer))
361 ✗ ScheduleConsumerRestart(consumer.sourceNode);
362 }
363 }
364
4/6
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
94 shouldStartSAP = shouldStartSAP && HasRunningRtpLocked();
365 94 }
366
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 81 times.
94 if (shouldStartSAP)
367
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 StartSAPAnnouncer();
368
2/4
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 94 times.
✗ Branch 3 not taken.
94 }
369
370 1867 void VideoOutputManager::Shutdown() {
371 // Idempotent: main() calls this at shutdown and ~VideoOutputManager() calls
372 // it again at static-destruction time. Run the teardown only once.
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1867 times.
1867 if (m_shutdownDone.exchange(true)) {
374 ✗ return;
375 }
376 // Stop SAP announcer first — outside the lock because SAP thread needs m_mutex
377
1/2
✓ Branch 0 taken 1867 times.
✗ Branch 1 not taken.
1867 StopSAPAnnouncer();
378 // The restart worker takes m_mutex too.
379 1867 std::thread restartWorker;
380 {
381
1/2
✓ Branch 0 taken 1867 times.
✗ Branch 1 not taken.
1867 std::lock_guard<std::mutex> rl(m_restartMutex);
382 1867 m_stopRestarts = true;
383 1867 m_restarts.clear();
384 1867 restartWorker = std::move(m_restartThread);
385 1867 }
386 1867 m_restartCv.notify_all();
387
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1858 times.
1867 if (restartWorker.joinable()) {
388
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 restartWorker.join();
389 }
390 // The warm worker takes m_mutex too.
391 1867 std::thread warmWorker;
392 {
393
1/2
✓ Branch 0 taken 1867 times.
✗ Branch 1 not taken.
1867 std::lock_guard<std::mutex> wl(m_warmMutex);
394 1867 m_stopWarm = true;
395 1867 m_prewarmRequests.clear();
396 1867 warmWorker = std::move(m_warmThread);
397 1867 }
398 1867 m_warmCv.notify_all();
399
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1850 times.
1867 if (warmWorker.joinable()) {
400
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 warmWorker.join();
401 }
402
403 {
404
1/2
✓ Branch 0 taken 1867 times.
✗ Branch 1 not taken.
1867 std::lock_guard<std::mutex> lock(m_mutex);
405
1/2
✓ Branch 0 taken 1867 times.
✗ Branch 1 not taken.
1867 StopAllConsumers();
406 1867 m_consumers.clear();
407 1867 m_activeProducer.clear();
408 1867 m_initialized = false;
409 1867 }
410 // The teardown threads StopAllConsumers() started use this object's
411 // teardown mutex and condition variable; at exit this object is destroyed
412 // next, under them. Bounded: a wedged teardown must not hang shutdown.
413
1/2
✓ Branch 0 taken 1867 times.
✗ Branch 1 not taken.
1867 WaitForPendingTeardowns(5000);
414
1/2
✓ Branch 0 taken 1867 times.
✗ Branch 1 not taken.
1867 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Shutdown complete\n");
415 1867 }
416
417 45 void VideoOutputManager::StartConsumers(const std::string& producerNodeName,
418 uint64_t producerGeneration) {
419 45 bool shouldStartSAP = false;
420 45 WaitForCmaIfLow();
421 {
422
1/2
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
45 std::lock_guard<std::mutex> lock(m_mutex);
423
6/6
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 43 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 43 times.
45 if (!m_initialized || m_consumers.empty()) {
424 2 return;
425 }
426
3/6
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 43 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 43 times.
86 if (producerGeneration != 0 && producerGeneration != m_producerGeneration.load()) {
427 // The producer was stopped between its start and this call (its
428 // deferred attach runs on another thread). Consumers started now
429 // would run against a dead producer until the next track.
430 ✗ LogDebug(VB_MEDIAOUT, "VideoOutputManager: producer '%s' already stopped, not starting consumers\n",
431 producerNodeName.c_str());
432 ✗ return;
433 }
434
435
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 m_activeProducer = producerNodeName;
436
437 // Extract stream slot number from producer node name (e.g. "fppd_video_stream_2" → 2)
438 43 int producerSlot = 0;
439 {
440
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 const std::string prefix = "fppd_video_stream_";
441 43 auto pos = producerNodeName.find(prefix);
442
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (pos != std::string::npos) {
443
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 std::string slotStr = producerNodeName.substr(pos + prefix.size());
444
1/4
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
43 try { producerSlot = std::stoi(slotStr); } catch (...) {}
445 43 }
446 43 }
447
448 // Only start consumers that don't have a fixed sourceNode — those
449 // depend on fppd media playback. Consumers with sourceNode are
450 // already running (started at Init).
451 // If the consumer has streamSlots configured, only start it for matching slots.
452 43 int started = 0;
453 43 int reused = 0;
454
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 43 times.
93 for (auto& consumer : m_consumers) {
455
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 4 times.
50 if (consumer.sourceNode.empty()) {
456 // Check stream slot filter
457
5/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 45 times.
46 if (!consumer.streamSlots.empty() && producerSlot > 0) {
458 1 bool slotMatch = false;
459
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 for (int s : consumer.streamSlots) {
460
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (s == producerSlot) { slotMatch = true; break; }
461 }
462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!slotMatch) continue;
463 }
464
3/4
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 33 times.
46 if (consumer.type == "rtp")
465 13 shouldStartSAP = true;
466
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
46 if (consumer.running && consumer.parked) {
467
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (consumer.channel == producerNodeName) {
468 // Prewarmed or parked from the last video: only
469 // needs to go PLAYING.
470
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 ActivateConsumer(consumer);
471 22 started++;
472 22 reused++;
473 22 continue;
474 }
475 ✗ StopConsumer(consumer); // parked on another slot's channel
476 }
477
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 StartConsumer(consumer);
478 24 started++;
479 }
480 }
481
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Started %d on-demand consumer(s) (%d already built) "
482 "targeting producer '%s' (slot %d)\n",
483 started, reused, producerNodeName.c_str(), producerSlot);
484
4/6
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
43 shouldStartSAP = shouldStartSAP && HasRunningRtpLocked();
485
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 2 times.
45 }
486 // Start SAP announcer outside the lock (SAPAnnounceLoop needs m_mutex)
487
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 30 times.
43 if (shouldStartSAP)
488 13 StartSAPAnnouncer();
489 }
490
491 10 void VideoOutputManager::ClaimConnectorForMedia(int connectorId) {
492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (connectorId <= 0) {
493 ✗ return;
494 }
495 10 int stopped = 0;
496 {
497
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 std::lock_guard<std::mutex> lock(m_mutex);
498
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 m_mediaConnectors[connectorId]++;
499
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 10 times.
21 for (auto& c : m_consumers) {
500
7/10
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 7 times.
✓ Branch 9 taken 4 times.
11 if (c.sourceNode.empty() || c.type != "hdmi" || c.connectorId != connectorId) {
501 7 continue;
502 }
503 // Running or not, it waits for the media to finish: one that is
504 // between retries would otherwise start up under the video.
505 4 c.suspendedForMedia = true;
506
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (c.running) {
507
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 LogInfo(VB_MEDIAOUT, "VideoOutputManager: '%s' stands aside for media on connector %s\n",
508 c.name.c_str(), c.connector.c_str());
509
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 StopConsumer(c);
510 4 stopped++;
511 }
512 }
513 10 }
514
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 if (stopped) {
515 // Its lease on the connector goes once its pipeline is down; the
516 // media's own lease needs it.
517 4 WaitForPendingTeardowns(2000);
518 }
519 }
520
521 9 void VideoOutputManager::ReleaseConnectorFromMedia(int connectorId) {
522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (connectorId <= 0) {
523 ✗ return;
524 }
525 9 std::set<std::string> channels;
526 {
527
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 std::lock_guard<std::mutex> lock(m_mutex);
528
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 auto it = m_mediaConnectors.find(connectorId);
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (it == m_mediaConnectors.end()) {
530 ✗ return;
531 }
532
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (--it->second > 0) {
533 ✗ return; // another slot's media still shows here
534 }
535
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 m_mediaConnectors.erase(it);
536
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 for (auto& c : m_consumers) {
537
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
9 if (c.suspendedForMedia && c.connectorId == connectorId) {
538 // No media holds the connector now. Cleared here, not on a
539 // successful start: a comeback that fails (display off, a
540 // pipeline that won't build) must leave it to the ordinary
541 // retry, which skips a consumer still standing aside.
542 4 c.suspendedForMedia = false;
543
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 channels.insert(c.sourceNode);
544 }
545 }
546
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 }
547 // Through the retry worker, which brings a consumer back only while its
548 // source runs (a dead source restarts itself and notifies).
549
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
13 for (const auto& channel : channels) {
550
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 ScheduleConsumerResume(channel, MEDIA_RESUME_DELAY_MS);
551 }
552
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 }
553
554 64 void VideoOutputManager::NotifyProducerReady(const std::string& channelName) {
555
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 WaitForCmaIfLow();
556
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 std::lock_guard<std::mutex> lock(m_mutex);
557
558 // Zombie sweep: a consumer whose bus-monitor thread saw ERROR/EOS sets
559 // pipelineDead but leaves `running` true (nothing else clears it), so it
560 // would hold its DRM lease and CMA forever and never be started again.
561 // StopConsumer() clears `running` synchronously (only its pipeline-NULL
562 // transition is detached), so the start loop below picks it straight up.
563
2/2
✓ Branch 0 taken 872 times.
✓ Branch 1 taken 64 times.
936 for (auto& c : m_consumers) {
564 872 if (c.sourceNode == channelName &&
565
9/10
✓ Branch 0 taken 842 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 606 times.
✓ Branch 3 taken 236 times.
✓ Branch 4 taken 606 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 478 times.
✓ Branch 7 taken 128 times.
✓ Branch 8 taken 478 times.
✓ Branch 9 taken 394 times.
872 c.running && c.pipelineDead && c.pipelineDead->load()) {
566
1/2
✓ Branch 0 taken 478 times.
✗ Branch 1 not taken.
478 LogWarn(VB_MEDIAOUT, "VideoOutputManager: consumer '%s' pipeline died (ERROR/EOS) — tearing down for restart\n",
567 c.name.c_str());
568
1/2
✓ Branch 0 taken 478 times.
✗ Branch 1 not taken.
478 StopConsumer(c);
569 }
570 }
571
572 // Group persistent HDMI consumers by sourceNode for grouped start.
573 64 std::map<std::string, std::vector<size_t>> hdmiSourceGroups;
574
2/2
✓ Branch 0 taken 872 times.
✓ Branch 1 taken 64 times.
936 for (size_t i = 0; i < m_consumers.size(); i++) {
575 872 auto& c = m_consumers[i];
576
9/10
✓ Branch 0 taken 842 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 714 times.
✓ Branch 3 taken 128 times.
✓ Branch 4 taken 714 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 695 times.
✓ Branch 7 taken 19 times.
✓ Branch 8 taken 695 times.
✓ Branch 9 taken 177 times.
872 if (c.sourceNode == channelName && !c.running && c.type == "hdmi") {
577
2/4
✓ Branch 0 taken 695 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 695 times.
✗ Branch 3 not taken.
695 hdmiSourceGroups[c.sourceNode].push_back(i);
578 }
579 }
580
581 64 int started = 0;
582 64 std::set<size_t> startedAsGroup;
583
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 64 times.
87 for (auto& [sourceNode, indices] : hdmiSourceGroups) {
584
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 19 times.
23 if (indices.size() > 1) {
585
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (StartHdmiConsumerGroup(indices)) {
586 4 started += indices.size();
587
3/4
✓ Branch 0 taken 676 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 676 times.
✓ Branch 3 taken 4 times.
680 for (auto idx : indices) startedAsGroup.insert(idx);
588 }
589 }
590 }
591
592
2/2
✓ Branch 0 taken 872 times.
✓ Branch 1 taken 64 times.
936 for (size_t i = 0; i < m_consumers.size(); i++) {
593 872 auto& c = m_consumers[i];
594
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 804 times.
1714 if (c.sourceNode == channelName && !c.running
595
6/8
✓ Branch 0 taken 842 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 38 times.
✓ Branch 7 taken 834 times.
1714 && startedAsGroup.find(i) == startedAsGroup.end()) {
596
4/6
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 35 times.
✓ Branch 5 taken 3 times.
76 if (StartConsumer(c))
597 35 started++;
598 }
599 }
600
601
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 26 times.
64 if (started > 0)
602
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Producer '%s' ready — started %d consumer(s)\n",
603 channelName.c_str(), started);
604
605 // StartConsumer() refuses a display that isn't connected, and nothing
606 // else will try it again while this source keeps running. An HDMI
607 // consumer saved without a connector id is a config error a retry
608 // can't fix.
609 // One standing aside for media comes back when the media releases it.
610
2/2
✓ Branch 0 taken 871 times.
✓ Branch 1 taken 61 times.
932 for (const auto& c : m_consumers) {
611
7/8
✓ Branch 0 taken 842 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 839 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 868 times.
874 if (c.sourceNode == channelName && !c.running && !c.suspendedForMedia &&
612
5/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
3 !(c.type == "hdmi" && (c.connectorId <= 0 || c.cardPath.empty()))) {
613
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 ScheduleConsumerRestart(channelName);
614 3 break;
615 }
616 }
617 64 }
618
619 636 void VideoOutputManager::ScheduleConsumerRestart(const std::string& channelName) {
620
1/2
✓ Branch 0 taken 636 times.
✗ Branch 1 not taken.
636 std::lock_guard<std::mutex> rl(m_restartMutex);
621
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 635 times.
636 if (m_stopRestarts) {
622 1 return;
623 }
624
1/2
✓ Branch 0 taken 635 times.
✗ Branch 1 not taken.
635 RestartState& r = m_restarts[channelName];
625
2/2
✓ Branch 0 taken 627 times.
✓ Branch 1 taken 8 times.
635 if (r.pending) {
626 627 return; // one retry covers every consumer on the channel
627 }
628 8 auto now = std::chrono::steady_clock::now();
629
7/10
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 6 times.
✓ Branch 9 taken 2 times.
8 if (r.delayMs == 0 || now - r.lastFailure > std::chrono::milliseconds(RESTART_QUIET_RESET_MS)) {
630 6 r.delayMs = RESTART_MIN_DELAY_MS;
631 } else {
632 2 r.delayMs = std::min(r.delayMs * 2, RESTART_MAX_DELAY_MS);
633 }
634 8 r.lastFailure = now;
635
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 r.due = now + std::chrono::milliseconds(r.delayMs);
636 8 r.pending = true;
637
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Retrying consumers of '%s' in %d ms\n",
638 channelName.c_str(), r.delayMs);
639
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 if (!m_restartRunning) {
640
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (m_restartThread.joinable()) {
641
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 m_restartThread.join(); // a finished worker; it no longer needs the lock
642 }
643 7 m_restartRunning = true;
644
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
14 m_restartThread = std::thread([this]() { RestartWorker(); });
645 }
646 8 m_restartCv.notify_all();
647
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 628 times.
636 }
648
649 4 void VideoOutputManager::ScheduleConsumerResume(const std::string& channelName, int delayMs) {
650
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 std::lock_guard<std::mutex> rl(m_restartMutex);
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (m_stopRestarts) {
652 ✗ return;
653 }
654
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 RestartState& r = m_restarts[channelName];
655
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 auto due = std::chrono::steady_clock::now() + std::chrono::milliseconds(delayMs);
656
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 if (!r.pending || due < r.due) {
657 4 r.due = due;
658 }
659 4 r.pending = true;
660
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!m_restartRunning) {
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (m_restartThread.joinable()) {
662 ✗ m_restartThread.join();
663 }
664 4 m_restartRunning = true;
665
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 m_restartThread = std::thread([this]() { RestartWorker(); });
666 }
667 4 m_restartCv.notify_all();
668
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 }
669
670 11 void VideoOutputManager::RestartWorker() {
671
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
11 SetThreadName("FPP-VideoOutRst");
672
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 std::unique_lock<std::mutex> l(m_restartMutex);
673
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 3 times.
32 while (!m_stopRestarts) {
674 29 std::string channel;
675 29 std::chrono::steady_clock::time_point due;
676
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 29 times.
58 for (const auto& [name, r] : m_restarts) {
677
5/10
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 21 times.
✓ Branch 9 taken 8 times.
29 if (r.pending && (channel.empty() || r.due < due)) {
678
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 channel = name;
679 21 due = r.due;
680 }
681 }
682
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 21 times.
29 if (channel.empty()) {
683 8 break;
684 }
685
3/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9 times.
21 if (std::chrono::steady_clock::now() < due) {
686
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 m_restartCv.wait_until(l, due);
687 12 continue;
688 }
689
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 m_restarts[channel].pending = false;
690
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 l.unlock();
691 // A source that is down restarts itself and calls
692 // NotifyProducerReady() when it is back, which is the same retry.
693
4/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 2 times.
9 if (VideoInputManager::Instance().IsSourceRunning(channel)) {
694
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 NotifyProducerReady(channel);
695 }
696
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 l.lock();
697
3/3
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 12 times.
29 }
698 11 m_restartRunning = false;
699 11 }
700
701 50 bool VideoOutputManager::HasSlotChannelConsumers(int streamSlot) const {
702
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 std::lock_guard<std::mutex> lock(m_mutex);
703
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 11 times.
67 for (const auto& c : m_consumers) {
704
3/4
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 17 times.
56 if (IsSlotChannelMember(c, streamSlot))
705 39 return true;
706 }
707 11 return false;
708 50 }
709
710 187 bool VideoOutputManager::IsSlotChannelMember(const ConsumerInfo& c, int streamSlot) {
711
7/8
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 52 times.
✓ Branch 3 taken 37 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 150 times.
✓ Branch 6 taken 37 times.
✓ Branch 7 taken 150 times.
187 if ((c.type != "overlay" && c.type != "rtp") || !c.sourceNode.empty())
712 37 return false;
713
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
150 return c.streamSlots.empty() || streamSlot <= 0 ||
714
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
150 std::find(c.streamSlots.begin(), c.streamSlots.end(), streamSlot) != c.streamSlots.end();
715 }
716
717 58 std::vector<VideoOutputManager::HdmiConsumerInfo> VideoOutputManager::GetHdmiConsumers(
718 int streamSlot, const std::set<int>& skipConnectorIds) const {
719
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 std::lock_guard<std::mutex> lock(m_mutex);
720 58 std::vector<HdmiConsumerInfo> result;
721
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 58 times.
117 for (const auto& c : m_consumers) {
722
6/8
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 39 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 20 times.
✓ Branch 6 taken 39 times.
✓ Branch 7 taken 20 times.
59 if (c.type != "hdmi" || c.connectorId <= 0)
723 47 continue;
724 // Only on-demand consumers (no fixed sourceNode)
725
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 15 times.
20 if (!c.sourceNode.empty())
726 5 continue;
727 // Stream slot filter
728
5/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 13 times.
15 if (!c.streamSlots.empty() && streamSlot > 0) {
729 2 bool match = false;
730
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int s : c.streamSlots)
731
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (s == streamSlot) { match = true; break; }
732
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!match) continue;
733 }
734
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 HdmiConsumerInfo info;
735 14 info.connectorId = c.connectorId;
736
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 info.connector = c.connector;
737
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 info.cardPath = c.cardPath;
738 14 info.width = c.width;
739 14 info.height = c.height;
740 14 info.crop = c.crop;
741
4/12
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 14 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
14 info.scaling = c.scaling.empty() ? "fit" : c.scaling;
742
743 // The cardPath/connectorId stored in the config were captured when the
744 // group was configured, but DRM card numbering is not stable across
745 // reboots (a Pi5 was observed moving HDMI from card2 to card1 simply by
746 // rebooting). A stale path points kmssink at the wrong device, so
747 // re-resolve from the connector name — which is stable — and only fall
748 // back to the stored values if the connector can't be found now.
749 // Done before the skipConnectorIds test because the caller's skip list
750 // is built from live lookups, so it must be compared against a live id.
751 // Without GStreamer there is no DRM resolver, so the stored values
752 // assigned above stand as-is.
753 #ifdef HAS_GSTREAMER
754
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11 times.
14 if (!c.connector.empty()) {
755
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 auto live = GStreamerOutput::ResolveDrmConnector(c.connector);
756
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (!live.cardPath.empty()) {
757
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (live.cardPath != c.cardPath) {
758
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogInfo(VB_MEDIAOUT,
759 "VideoOutputManager: %s moved %s → %s since config was saved; using current path\n",
760 c.connector.c_str(), c.cardPath.c_str(), live.cardPath.c_str());
761 }
762
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 info.cardPath = live.cardPath;
763 }
764
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (live.connectorId > 0)
765 3 info.connectorId = live.connectorId;
766 // The stored size is the mode that was active when the group was
767 // saved; the display may have been re-plugged at another mode
768 // since, and for a connector that was down at save time it is 0.
769 // Both "fill" and "stretch" are defined against the real panel, so
770 // prefer what the connector reports now.
771
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (live.displayWidth > 0 && live.displayHeight > 0) {
772 3 info.width = live.displayWidth;
773 3 info.height = live.displayHeight;
774 }
775 3 }
776 #endif
777
778 // "fill" is expressed as a crop to the display's shape, so the single
779 // videocrop already next to the sink delivers it for free. Without a
780 // known display size there is no shape to crop to, so it stays "fit".
781
3/10
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 14 times.
14 if (info.scaling == "fill" && info.width > 0 && info.height > 0)
782 ✗ info.crop.targetAspect = (double)info.width / (double)info.height;
783
784
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
14 if (skipConnectorIds.count(info.connectorId))
785 ✗ continue;
786 // One display takes one on-demand output. Two enabled groups that
787 // both list the same connector all subscribe to the one video bus, so
788 // without this a single video asks for two kmssinks on that connector;
789 // GStreamerOut names each sink branch after its connector, and the
790 // second branch's elements collide in the bin. The first entry wins,
791 // which is also the one GetHdmiCropForConnector() reports.
792
3/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 12 times.
14 if (std::any_of(result.begin(), result.end(),
793 3 [&](const HdmiConsumerInfo& r) { return r.connectorId == info.connectorId; })) {
794
5/8
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
2 LogWarn(VB_MEDIAOUT, "VideoOutputManager: %s is already driven by an earlier video output; "
795 "ignoring '%s' for this stream\n",
796 c.connector.empty() ? std::to_string(info.connectorId).c_str() : c.connector.c_str(),
797 c.name.c_str());
798 2 continue;
799 }
800
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 result.push_back(info);
801
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
14 }
802 58 return result;
803 58 }
804
805 4 VideoCropRect VideoOutputManager::GetHdmiCropForConnector(int streamSlot, int connectorId) const {
806
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (connectorId <= 0)
807 ✗ return VideoCropRect();
808
809 // Deliberately no lock here and no skip list: GetHdmiConsumers takes the
810 // lock itself, and reusing it keeps the entry matching (on-demand only,
811 // stream-slot filter) and the live connector re-resolution in one place.
812
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 for (const auto& info : GetHdmiConsumers(streamSlot)) {
813 ✗ if (info.connectorId == connectorId)
814 ✗ return info.crop;
815
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 }
816 4 return VideoCropRect();
817 }
818
819 4 std::string VideoOutputManager::GetHdmiScalingForConnector(int streamSlot, int connectorId) const {
820
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (connectorId <= 0)
821 ✗ return "fit";
822
823 // Same no-lock, no-skip-list rationale as GetHdmiCropForConnector.
824
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 for (const auto& info : GetHdmiConsumers(streamSlot)) {
825 ✗ if (info.connectorId == connectorId)
826 ✗ return info.scaling;
827
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 }
828
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 return "fit";
829 }
830
831 43 void VideoOutputManager::StopConsumers() {
832 43 bool shouldStopSAP = false;
833 {
834
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 std::lock_guard<std::mutex> lock(m_mutex);
835 43 m_producerGeneration++;
836
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (!m_initialized) return;
837
838 // Only stop on-demand consumers (those without a fixed sourceNode).
839 // Persistent-source consumers keep running independently of media playback.
840 // Overlay and RTP members are parked instead, for the next video.
841 43 int stopped = 0;
842 43 int parked = 0;
843
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 const uint64_t holdUntil = GetTimeMS() + LINGER_MS;
844
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 43 times.
91 for (auto& consumer : m_consumers) {
845
7/8
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 33 times.
✓ Branch 6 taken 15 times.
✓ Branch 7 taken 33 times.
48 if (!consumer.sourceNode.empty() || !consumer.running || consumer.parked)
846 15 continue;
847
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
33 if (IsSlotChannelMember(consumer, 0)) {
848
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 ParkConsumer(consumer, holdUntil);
849 33 parked++;
850 } else {
851 ✗ StopConsumer(consumer);
852 ✗ stopped++;
853 }
854 }
855
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Stopped %d and parked %d on-demand consumer(s) (producer gone)\n",
856 stopped, parked);
857 43 m_activeProducer.clear();
858
859 // Check if any RTP consumers are still running (persistent-source)
860
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 shouldStopSAP = !HasRunningRtpLocked();
861
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 10 times.
43 if (parked) {
862
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 KickWarmWorker();
863 }
864
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 }
865
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (shouldStopSAP)
866 43 StopSAPAnnouncer();
867 }
868
869 22 void VideoOutputManager::ActivateConsumer(ConsumerInfo& consumer) {
870 #ifdef HAS_GSTREAMER_VIDEO_OUTPUT
871
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 1 times.
22 if (consumer.overlayState) {
872
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 std::lock_guard<std::mutex> lock(consumer.overlayState->mtx);
873 21 consumer.overlayState->wasDisabled = false;
874 21 consumer.overlayState->active = true;
875 21 }
876 {
877
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 std::lock_guard<std::mutex> sl(*consumer.stateLock);
878 22 consumer.playing->store(true);
879
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (consumer.pipeline) {
880
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 gst_element_set_state(consumer.pipeline, GST_STATE_PLAYING);
881 }
882 22 }
883 22 consumer.parked = false;
884 22 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' resumed on %s\n",
885 consumer.name.c_str(), consumer.channel.c_str());
886 #endif
887 22 }
888
889 33 void VideoOutputManager::ParkConsumer(ConsumerInfo& consumer, uint64_t holdUntilMs) {
890 #ifdef HAS_GSTREAMER_VIDEO_OUTPUT
891 // Stop showing first, then give the model back, as StopConsumer does.
892
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 13 times.
33 if (consumer.overlayState) {
893 20 std::shared_ptr<OverlayState> state = consumer.overlayState;
894
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 std::lock_guard<std::mutex> lock(state->mtx);
895 20 state->active = false;
896
3/6
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
20 if (state->wasDisabled && state->model) {
897
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 state->model->setState(PixelOverlayState(PixelOverlayState::Disabled));
898 }
899 20 state->wasDisabled = false;
900 20 }
901 // PAUSED stops the source (it is live), so an RTP member stops sending
902 // too. On its own thread, like StopConsumer's teardown: the streaming
903 // thread may be inside a callback the caller's locks could block.
904
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (consumer.pipeline) {
905
3/6
✓ 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.
33 GstElement* p = GST_ELEMENT(gst_object_ref(consumer.pipeline));
906 33 std::shared_ptr<std::atomic<bool>> playing = consumer.playing;
907 33 std::shared_ptr<std::mutex> stateLock = consumer.stateLock;
908 33 std::shared_ptr<std::atomic<bool>> stopping = consumer.shutdownRequested;
909 33 playing->store(false);
910
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
66 std::thread([p, playing, stateLock, stopping]() {
911 {
912 // Not once StopConsumer() has begun tearing this run down:
913 // running after its NULL, PAUSED brought the pipeline back
914 // up -- kmssink and all -- after its DRM lease was returned.
915
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 std::lock_guard<std::mutex> sl(*stateLock);
916
3/6
✓ 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.
33 if (!playing->load() && !stopping->load()) {
917
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 gst_element_set_state(p, GST_STATE_PAUSED);
918 }
919 33 }
920 33 gst_object_unref(p);
921
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
66 }).detach();
922 33 }
923 33 consumer.parked = true;
924 33 consumer.holdUntilMs = holdUntilMs;
925 33 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' parked for %d ms\n",
926 consumer.name.c_str(), (int)(holdUntilMs - GetTimeMS()));
927 #endif
928 33 }
929
930 302 void VideoOutputManager::Prewarm(int streamSlot, int holdMs) {
931
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (streamSlot <= 0) {
932 ✗ return;
933 }
934 {
935
1/2
✓ Branch 0 taken 302 times.
✗ Branch 1 not taken.
302 std::lock_guard<std::mutex> lock(m_mutex);
936
7/8
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 252 times.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 39 times.
✓ Branch 6 taken 263 times.
✓ Branch 7 taken 39 times.
302 if (!m_initialized || !std::any_of(m_consumers.begin(), m_consumers.end(),
937 56 [streamSlot](const ConsumerInfo& c) { return IsSlotChannelMember(c, streamSlot); })) {
938 263 return;
939 }
940
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 263 times.
302 }
941 {
942
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 std::lock_guard<std::mutex> wl(m_warmMutex);
943
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 uint64_t& until = m_prewarmRequests[streamSlot];
944
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 until = std::max<uint64_t>(until, GetTimeMS() + holdMs);
945 39 }
946 39 KickWarmWorker();
947 }
948
949 72 void VideoOutputManager::KickWarmWorker() {
950
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 std::lock_guard<std::mutex> wl(m_warmMutex);
951
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
72 if (m_stopWarm) {
952 ✗ return;
953 }
954
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 55 times.
72 if (!m_warmThread.joinable()) {
955
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
34 m_warmThread = std::thread([this]() { WarmWorker(); });
956 }
957 72 m_warmCv.notify_all();
958
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 }
959
960 17 void VideoOutputManager::WarmWorker() {
961
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 SetThreadName("FPP-VideoWarm");
962
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 std::unique_lock<std::mutex> wl(m_warmMutex);
963
2/2
✓ Branch 0 taken 645 times.
✓ Branch 1 taken 17 times.
662 while (!m_stopWarm) {
964 645 std::map<int, uint64_t> requests;
965 645 requests.swap(m_prewarmRequests);
966
1/2
✓ Branch 0 taken 645 times.
✗ Branch 1 not taken.
645 wl.unlock();
967
1/2
✓ Branch 0 taken 645 times.
✗ Branch 1 not taken.
645 ServiceWarm(requests);
968
1/2
✓ Branch 0 taken 645 times.
✗ Branch 1 not taken.
645 wl.lock();
969
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 645 times.
645 if (m_stopWarm) {
970 ✗ break;
971 }
972
1/2
✓ Branch 0 taken 645 times.
✗ Branch 1 not taken.
645 if (m_prewarmRequests.empty()) {
973
1/2
✓ Branch 0 taken 645 times.
✗ Branch 1 not taken.
645 m_warmCv.wait_for(wl, std::chrono::milliseconds(250));
974 }
975
1/2
✓ Branch 0 taken 645 times.
✗ Branch 1 not taken.
645 }
976 17 }
977
978 645 void VideoOutputManager::ServiceWarm(const std::map<int, uint64_t>& requests) {
979 #ifdef HAS_GSTREAMER_VIDEO_OUTPUT
980
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 606 times.
645 if (!requests.empty()) {
981
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 WaitForCmaIfLow();
982 }
983
1/2
✓ Branch 0 taken 645 times.
✗ Branch 1 not taken.
645 std::lock_guard<std::mutex> lock(m_mutex);
984
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 645 times.
645 if (!m_initialized) {
985 ✗ return;
986 }
987
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 645 times.
684 for (const auto& [slot, holdUntil] : requests) {
988
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 const std::string channel = StreamSlotManager::GetVideoNodeName(slot);
989
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 39 times.
81 for (auto& consumer : m_consumers) {
990
3/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 39 times.
42 if (!IsSlotChannelMember(consumer, slot)) {
991 3 continue;
992 }
993
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 29 times.
39 if (consumer.running) {
994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!consumer.parked) {
995 ✗ continue; // showing a video now
996 }
997
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 if (consumer.channel == channel) {
998 9 consumer.holdUntilMs = std::max(consumer.holdUntilMs, holdUntil);
999 9 continue;
1000 }
1001
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 StopConsumer(consumer); // parked on another slot's channel
1002 }
1003
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 if (StartConsumer(consumer, channel, false)) {
1004 30 consumer.holdUntilMs = holdUntil;
1005
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' prewarmed on %s\n",
1006 consumer.name.c_str(), channel.c_str());
1007 }
1008 }
1009 39 }
1010
1/2
✓ Branch 0 taken 645 times.
✗ Branch 1 not taken.
645 const uint64_t now = GetTimeMS();
1011
2/2
✓ Branch 0 taken 690 times.
✓ Branch 1 taken 645 times.
1335 for (auto& consumer : m_consumers) {
1012
6/6
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 177 times.
✓ Branch 2 taken 219 times.
✓ Branch 3 taken 294 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 216 times.
690 if (consumer.running && consumer.parked && now >= consumer.holdUntilMs) {
1013
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' idle, tearing it down\n",
1014 consumer.name.c_str());
1015
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 StopConsumer(consumer);
1016 }
1017 }
1018 #endif
1019
1/2
✓ Branch 0 taken 645 times.
✗ Branch 1 not taken.
645 }
1020
1021 ✗ bool VideoOutputManager::HasConsumers() const {
1022 ✗ std::lock_guard<std::mutex> lock(m_mutex);
1023 ✗ return !m_consumers.empty();
1024 ✗ }
1025
1026 ✗ bool VideoOutputManager::HasActiveConsumers() const {
1027 ✗ std::lock_guard<std::mutex> lock(m_mutex);
1028 ✗ for (const auto& c : m_consumers) {
1029 ✗ if (c.running && !c.parked)
1030 ✗ return true;
1031 }
1032 ✗ return false;
1033 ✗ }
1034
1035 ✗ int VideoOutputManager::ActiveConsumerCount() const {
1036 ✗ std::lock_guard<std::mutex> lock(m_mutex);
1037 ✗ int count = 0;
1038 ✗ for (const auto& c : m_consumers) {
1039 ✗ if (c.running && !c.parked)
1040 ✗ count++;
1041 }
1042 ✗ return count;
1043 ✗ }
1044
1045 179 bool VideoOutputManager::LoadConfig() {
1046
2/4
✓ Branch 0 taken 179 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 179 times.
✗ Branch 3 not taken.
179 std::string configPath = FPP_DIR_MEDIA("/config/pipewire-video-consumers.json");
1047
1048
3/4
✓ Branch 0 taken 179 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 178 times.
179 if (!FileExists(configPath)) {
1049
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogDebug(VB_MEDIAOUT, "VideoOutputManager: No consumer config at %s\n", configPath.c_str());
1050 1 return false;
1051 }
1052
1053
1/2
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
178 std::ifstream ifs(configPath);
1054
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 178 times.
178 if (!ifs.is_open()) {
1055 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: Cannot open %s\n", configPath.c_str());
1056 ✗ return false;
1057 }
1058
1059
1/2
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
178 Json::Value root;
1060
1/2
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
178 Json::CharReaderBuilder builder;
1061 178 std::string errors;
1062
2/4
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 178 times.
178 if (!Json::parseFromStream(builder, ifs, &root, &errors)) {
1063 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: JSON parse error: %s\n", errors.c_str());
1064 ✗ return false;
1065 }
1066
1067
2/4
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 178 times.
178 if (!root.isArray()) {
1068 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: Config is not an array\n");
1069 ✗ return false;
1070 }
1071
1072
7/12
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 178 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 384 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 384 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 562 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 384 times.
✓ Branch 11 taken 178 times.
562 for (const auto& entry : root) {
1073
1/2
✓ Branch 0 taken 384 times.
✗ Branch 1 not taken.
384 ConsumerInfo ci;
1074 // asInt()/asString() throw on a value of the wrong type, and this
1075 // runs at startup with nothing above to catch it; skip that consumer.
1076 try {
1077
3/4
✓ Branch 0 taken 384 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 383 times.
384 if (!entry.isObject()) {
1078 1 continue;
1079 }
1080
4/6
✓ Branch 0 taken 383 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 383 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 382 times.
✓ Branch 5 taken 1 times.
385 ci.id = entry.get("id", 0).asInt();
1081
3/6
✓ Branch 0 taken 382 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 382 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 382 times.
✗ Branch 5 not taken.
382 ci.name = entry.get("name", "").asString();
1082
3/6
✓ Branch 0 taken 382 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 382 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 382 times.
✗ Branch 5 not taken.
382 ci.type = entry.get("type", "").asString();
1083
3/6
✓ Branch 0 taken 382 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 382 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 382 times.
✗ Branch 5 not taken.
382 ci.pipeWireNodeName = entry.get("pipeWireNodeName", "").asString();
1084
1085
3/4
✓ Branch 0 taken 382 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
✓ Branch 3 taken 155 times.
382 if (ci.type == "hdmi") {
1086
3/6
✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 227 times.
✗ Branch 5 not taken.
227 ci.connector = entry.get("connector", "").asString();
1087
3/6
✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 227 times.
✗ Branch 5 not taken.
227 ci.cardPath = entry.get("cardPath", "").asString();
1088
3/6
✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 227 times.
✗ Branch 5 not taken.
227 ci.connectorId = entry.get("connectorId", -1).asInt();
1089
3/6
✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 227 times.
✗ Branch 5 not taken.
227 ci.width = entry.get("width", 0).asInt();
1090
3/6
✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 227 times.
✗ Branch 5 not taken.
227 ci.height = entry.get("height", 0).asInt();
1091
3/6
✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 227 times.
✗ Branch 5 not taken.
227 ci.scaling = entry.get("scaling", "fit").asString();
1092 // Anything else would silently land in the "not stretch, not fill"
1093 // bucket and look like fit without saying so.
1094
9/14
✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 224 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 227 times.
227 if (ci.scaling != "fit" && ci.scaling != "fill" && ci.scaling != "stretch") {
1095 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: consumer '%s' has unknown scaling "
1096 "mode '%s' — using fit\n", ci.name.c_str(), ci.scaling.c_str());
1097 ✗ ci.scaling = "fit";
1098 }
1099 // Optional sub-region of the source, as fractions of the frame.
1100 // Absent (the common case) leaves the full-frame default.
1101
8/12
✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 225 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 225 times.
227 if (entry.isMember("crop") && entry["crop"].isObject()) {
1102
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 const Json::Value& c = entry["crop"];
1103
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.
2 ci.crop.x = c.get("x", 0.0).asDouble();
1104
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.
2 ci.crop.y = c.get("y", 0.0).asDouble();
1105
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.
2 ci.crop.width = c.get("width", 1.0).asDouble();
1106
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.
2 ci.crop.height = c.get("height", 1.0).asDouble();
1107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ci.crop.IsValid()) {
1108 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: consumer '%s' has an out-of-range "
1109 "crop region (%.3f,%.3f %.3fx%.3f) — using the full frame\n",
1110 ci.name.c_str(), ci.crop.x, ci.crop.y, ci.crop.width, ci.crop.height);
1111 ✗ ci.crop = VideoCropRect();
1112 }
1113 }
1114
3/4
✓ Branch 0 taken 155 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 111 times.
155 } else if (ci.type == "overlay") {
1115
3/6
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 44 times.
✗ Branch 5 not taken.
44 ci.overlayModel = entry.get("overlayModel", "").asString();
1116
3/4
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✓ Branch 3 taken 13 times.
111 } else if (ci.type == "rtp") {
1117
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 ci.address = entry.get("address", "239.0.0.1").asString();
1118
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 ci.port = entry.get("port", 5004).asInt();
1119
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 ci.rtpEncoding = entry.get("encoding", "raw").asString();
1120 }
1121
1122
5/6
✓ Branch 0 taken 382 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 369 times.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 369 times.
382 if (ci.type.empty() || ci.pipeWireNodeName.empty()) {
1123
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 LogWarn(VB_MEDIAOUT, "VideoOutputManager: Skipping consumer with missing type or node name\n");
1124 13 continue;
1125 }
1126
1127
3/6
✓ Branch 0 taken 369 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 369 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 369 times.
✗ Branch 5 not taken.
369 ci.sourceNode = entry.get("sourceNode", "").asString();
1128
1129
8/12
✓ Branch 0 taken 369 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 368 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 368 times.
369 if (entry.isMember("streamSlots") && entry["streamSlots"].isArray()) {
1130
8/14
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 times.
✓ Branch 13 taken 1 times.
2 for (const auto& s : entry["streamSlots"]) {
1131
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 int slot = s.asInt();
1132
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (slot >= 1 && slot <= 5)
1133
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ci.streamSlots.push_back(slot);
1134 }
1135 }
1136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } catch (const std::exception& ex) {
1137
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogWarn(VB_MEDIAOUT, "VideoOutputManager: skipping a consumer in %s, a value has the wrong type: %s\n",
1138 configPath.c_str(), ex.what());
1139 1 continue;
1140 1 }
1141
1142 // Both go into a gst_parse_launch() description unquoted; a space or
1143 // '!' would re-parse as extra pipeline elements. Real node names and
1144 // addresses never need anything outside these characters.
1145 467 auto onlyChars = [](const std::string& v, const char* extra) {
1146
2/2
✓ Branch 0 taken 5783 times.
✓ Branch 1 taken 466 times.
6249 for (unsigned char c : v) {
1147
4/4
✓ Branch 0 taken 1275 times.
✓ Branch 1 taken 4508 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1274 times.
5783 if (!std::isalnum(c) && !strchr(extra, c)) {
1148 1 return false;
1149 }
1150 }
1151 466 return true;
1152 };
1153
3/4
✓ Branch 0 taken 369 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 368 times.
738 if (!onlyChars(ci.sourceNode, "_.-") ||
1154
6/8
✓ Branch 0 taken 369 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✓ Branch 3 taken 271 times.
✓ Branch 4 taken 98 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 97 times.
369 (ci.type == "rtp" && (ci.address.empty() || !onlyChars(ci.address, ".-:")))) {
1155
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogErr(VB_MEDIAOUT, "VideoOutputManager: consumer '%s' has an invalid source or address, skipping it\n",
1156 ci.name.c_str());
1157 1 continue;
1158 }
1159
1160
1/2
✓ Branch 0 taken 368 times.
✗ Branch 1 not taken.
368 m_consumers.push_back(std::move(ci));
1161
2/2
✓ Branch 0 taken 368 times.
✓ Branch 1 taken 16 times.
384 }
1162
1163 178 return !m_consumers.empty();
1164 179 }
1165
1166 4 bool VideoOutputManager::StartHdmiConsumerGroup(const std::vector<size_t>& indices) {
1167 #ifdef HAS_GSTREAMER_VIDEO_OUTPUT
1168 // Consumers start from a config reload, which can happen before any media
1169 // has played -- and playback used to be the only caller of gst_init().
1170 // See VideoInputManager::StartSource().
1171
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 GStreamerOutput::EnsureGStreamerInit();
1172
1173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (indices.size() < 2) return false;
1174
1175 // The first consumer in the group owns the pipeline
1176 4 size_t ownerIdx = indices[0];
1177 4 ConsumerInfo& owner = m_consumers[ownerIdx];
1178
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (owner.running) {
1180 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: Group owner '%s' already running\n", owner.name.c_str());
1181 ✗ return true;
1182 }
1183
1184 4 setenv("PIPEWIRE_RUNTIME_DIR", "/run/pipewire-fpp", 0);
1185
1186 // Validate all consumers and check connector connectivity
1187 4 std::vector<size_t> validIndices;
1188
2/2
✓ Branch 0 taken 676 times.
✓ Branch 1 taken 4 times.
680 for (size_t idx : indices) {
1189 676 auto& c = m_consumers[idx];
1190
3/6
✓ Branch 0 taken 676 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 676 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 676 times.
676 if (c.connectorId <= 0 || c.cardPath.empty()) {
1191 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: Skipping group member '%s' — no valid connector\n", c.name.c_str());
1192 ✗ continue;
1193 }
1194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 676 times.
676 if (!c.connector.empty()) {
1195 ✗ auto drmCheck = GStreamerOutput::ResolveDrmConnector(c.connector);
1196 ✗ if (!drmCheck.connected) {
1197 ✗ LogInfo(VB_MEDIAOUT, "VideoOutputManager: Skipping group member '%s' — connector %s not connected\n",
1198 c.name.c_str(), c.connector.c_str());
1199 ✗ continue;
1200 }
1201 ✗ }
1202
2/4
✓ Branch 0 taken 676 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 676 times.
676 if (m_mediaConnectors.count(c.connectorId)) {
1203 ✗ c.suspendedForMedia = true;
1204 ✗ continue;
1205 }
1206
1/2
✓ Branch 0 taken 676 times.
✗ Branch 1 not taken.
676 validIndices.push_back(idx);
1207 }
1208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (validIndices.empty()) return false;
1209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (validIndices.size() == 1) {
1210 // Only one valid — fall back to single consumer
1211 ✗ return StartConsumer(m_consumers[validIndices[0]]);
1212 }
1213
1214 // Start each consumer as an independent pipeline with its own intervideosrc.
1215 // This avoids CMA/DRM buffer exhaustion issues when both kmssink elements
1216 // in a combined pipeline allocate dumb buffers simultaneously on a shared fd.
1217 4 bool allOk = true;
1218
2/2
✓ Branch 0 taken 676 times.
✓ Branch 1 taken 4 times.
680 for (size_t i = 0; i < validIndices.size(); i++) {
1219
3/6
✓ Branch 0 taken 676 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 676 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 676 times.
1352 if (!StartConsumer(m_consumers[validIndices[i]])) {
1220 ✗ allOk = false;
1221 }
1222 }
1223 4 return allOk;
1224
1225 #if 0 // Combined pipeline disabled — see comment above
1226 std::string sourceNodeName = m_consumers[validIndices[0]].sourceNode;
1227 std::string pipelineDesc = "intervideosrc timeout=5000000000 channel=" + sourceNodeName
1228 + " ! videoconvert ! tee name=t";
1229 std::string names; // for logging
1230 for (size_t i = 0; i < validIndices.size(); i++) {
1231 auto& c = m_consumers[validIndices[i]];
1232 std::string sinkName = "sink" + std::to_string(i);
1233 pipelineDesc += " t. ! queue max-size-buffers=2 leaky=downstream ! videoscale ! ";
1234 if (c.width > 0 && c.height > 0) {
1235 // Square pixels for the same reason as the producer caps: without
1236 // it videoscale satisfies the size request by bending the PAR and
1237 // the picture reaches the sink with the wrong display aspect.
1238 pipelineDesc += "video/x-raw,format=BGRx,width=" + std::to_string(c.width)
1239 + ",height=" + std::to_string(c.height)
1240 + ",pixel-aspect-ratio=1/1 ! ";
1241 } else {
1242 pipelineDesc += "video/x-raw,format=BGRx ! ";
1243 }
1244 pipelineDesc += "kmssink name=" + sinkName + " sync=false"
1245 " connector-id=" + std::to_string(c.connectorId)
1246 + " restore-crtc=true skip-vsync=true";
1247 if (!names.empty()) names += ", ";
1248 names += c.name;
1249 }
1250
1251 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Starting HDMI group [%s] with combined pipeline: %s\n",
1252 names.c_str(), pipelineDesc.c_str());
1253
1254 GError* error = nullptr;
1255 GstElement* pipeline = gst_parse_launch(pipelineDesc.c_str(), &error);
1256 if (!pipeline) {
1257 LogErr(VB_MEDIAOUT, "VideoOutputManager: Group pipeline creation failed: %s\n",
1258 error ? error->message : "unknown");
1259 WarningHolder::AddWarning(31, "Video output group pipeline creation failed");
1260 if (error) g_error_free(error);
1261 return false;
1262 }
1263 if (error) {
1264 LogWarn(VB_MEDIAOUT, "VideoOutputManager: Group pipeline warning: %s\n", error->message);
1265 g_error_free(error);
1266 }
1267
1268 // intervideosrc is configured inline (channel name in pipeline desc).\n // No PipeWire node configuration or manual pw-link needed.
1269
1270 // Configure each kmssink with shared DRM fd and plane
1271 for (size_t i = 0; i < validIndices.size(); i++) {
1272 auto& c = m_consumers[validIndices[i]];
1273 std::string sinkName = "sink" + std::to_string(i);
1274 GstElement* sink = gst_bin_get_by_name(GST_BIN(pipeline), sinkName.c_str());
1275 if (sink) {
1276 // Prefer live-resolved card path over config value
1277 std::string resolvedCard;
1278 int resolvedConnId = c.connectorId;
1279 if (!c.connector.empty()) {
1280 auto drmCheck = GStreamerOutput::ResolveDrmConnector(c.connector);
1281 resolvedCard = drmCheck.cardPath;
1282 if (drmCheck.connectorId > 0)
1283 resolvedConnId = drmCheck.connectorId;
1284 }
1285 std::string cardForFd = !resolvedCard.empty() ? resolvedCard : c.cardPath;
1286 int drmFd = cardForFd.empty() ? -1 : GStreamerOutput::GetSharedDrmFd(cardForFd);
1287 if (drmFd >= 0) {
1288 g_object_set(sink, "fd", drmFd, NULL);
1289 int planeId = GStreamerOutput::FindPrimaryPlaneForConnector(drmFd, resolvedConnId);
1290 if (planeId >= 0) {
1291 g_object_set(sink, "plane-id", planeId, NULL);
1292 }
1293 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Group sink '%s' fd=%d plane=%d connector=%d\n",
1294 c.name.c_str(), drmFd, planeId, resolvedConnId);
1295 } else {
1296 g_object_set(sink, "driver-name", "vc4", NULL);
1297 }
1298 gst_object_unref(sink);
1299 }
1300 }
1301
1302 // Store pipeline in the first valid consumer (the "owner")
1303 size_t pipelineOwnerIdx = validIndices[0];
1304 m_consumers[pipelineOwnerIdx].pipeline = pipeline;
1305 m_consumers[pipelineOwnerIdx].shutdownRequested = false;
1306 m_consumers[pipelineOwnerIdx].running = true;
1307 m_consumers[pipelineOwnerIdx].groupPipelineOwnerIdx = -1; // owner
1308
1309 // Mark other group members as running, pointing to the owner
1310 for (size_t i = 1; i < validIndices.size(); i++) {
1311 auto& c = m_consumers[validIndices[i]];
1312 c.running = true;
1313 c.shutdownRequested = false;
1314 c.pipeline = nullptr;
1315 c.groupPipelineOwnerIdx = (int)pipelineOwnerIdx;
1316 }
1317
1318 // Start background thread for pipeline lifecycle
1319 std::string groupNames = names;
1320 std::atomic<bool>* shutdownFlag = &m_consumers[pipelineOwnerIdx].shutdownRequested;
1321
1322 m_consumers[pipelineOwnerIdx].startThread = std::thread(
1323 [pipeline, groupNames, shutdownFlag]() {
1324 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Group [%s] background thread starting\n", groupNames.c_str());
1325
1326 GstStateChangeReturn ret = gst_element_set_state(pipeline, GST_STATE_PLAYING);
1327 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Group [%s] set_state(PLAYING) returned %d\n",
1328 groupNames.c_str(), (int)ret);
1329
1330 if (ret == GST_STATE_CHANGE_FAILURE) {
1331 if (!shutdownFlag->load())
1332 LogWarn(VB_MEDIAOUT, "VideoOutputManager: Group [%s] pipeline failed to start\n", groupNames.c_str());
1333 return;
1334 }
1335
1336 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Group [%s] pipeline reached PLAYING\n", groupNames.c_str());
1337
1338 // Bus monitor loop
1339 GstBus* bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
1340 int busLoopIter = 0;
1341 while (!shutdownFlag->load()) {
1342 GstMessage* msg = gst_bus_timed_pop(bus, 500 * GST_MSECOND);
1343 busLoopIter++;
1344 if (busLoopIter % 10 == 1) {
1345 GstState cur = GST_STATE_VOID_PENDING, pend = GST_STATE_VOID_PENDING;
1346 gst_element_get_state(pipeline, &cur, &pend, 0);
1347 if (cur != GST_STATE_PLAYING || pend != GST_STATE_VOID_PENDING) {
1348 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Group [%s] state check: current=%d pending=%d\n",
1349 groupNames.c_str(), (int)cur, (int)pend);
1350 }
1351 }
1352 if (!msg) continue;
1353 switch (GST_MESSAGE_TYPE(msg)) {
1354 case GST_MESSAGE_ERROR: {
1355 GError* err = nullptr; gchar* debug = nullptr;
1356 gst_message_parse_error(msg, &err, &debug);
1357 if (!shutdownFlag->load())
1358 LogWarn(VB_MEDIAOUT, "VideoOutputManager: Group [%s] error: %s (%s)\n",
1359 groupNames.c_str(), err ? err->message : "?", debug ? debug : "");
1360 if (err) g_error_free(err); g_free(debug);
1361 gst_message_unref(msg); gst_object_unref(bus);
1362 return;
1363 }
1364 case GST_MESSAGE_EOS:
1365 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Group [%s] EOS\n", groupNames.c_str());
1366 gst_message_unref(msg); gst_object_unref(bus);
1367 return;
1368 case GST_MESSAGE_ASYNC_DONE:
1369 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Group [%s] ASYNC_DONE (preroll complete)\n",
1370 groupNames.c_str());
1371 break;
1372 case GST_MESSAGE_STATE_CHANGED:
1373 if (GST_MESSAGE_SRC(msg) == GST_OBJECT(pipeline)) {
1374 GstState oldSt, newSt, pendSt;
1375 gst_message_parse_state_changed(msg, &oldSt, &newSt, &pendSt);
1376 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Group [%s] state %d -> %d (pending %d)\n",
1377 groupNames.c_str(), (int)oldSt, (int)newSt, (int)pendSt);
1378 }
1379 break;
1380 case GST_MESSAGE_WARNING: {
1381 GError* err = nullptr; gchar* debug = nullptr;
1382 gst_message_parse_warning(msg, &err, &debug);
1383 LogWarn(VB_MEDIAOUT, "VideoOutputManager: Group [%s] warning: %s (%s)\n",
1384 groupNames.c_str(), err ? err->message : "?", debug ? debug : "");
1385 if (err) g_error_free(err); g_free(debug);
1386 break;
1387 }
1388 default: break;
1389 }
1390 gst_message_unref(msg);
1391 }
1392 gst_object_unref(bus);
1393 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Group [%s] background thread exiting\n", groupNames.c_str());
1394 });
1395
1396 return true;
1397 #endif // #if 0 -- combined pipeline disabled
1398 #else
1399 return false;
1400 #endif
1401 4 }
1402
1403 782 bool VideoOutputManager::StartConsumer(ConsumerInfo& consumer, const std::string& channelArg,
1404 bool startPlaying) {
1405 #ifdef HAS_GSTREAMER_VIDEO_OUTPUT
1406
1/2
✓ Branch 0 taken 782 times.
✗ Branch 1 not taken.
782 GStreamerOutput::EnsureGStreamerInit();
1407
1408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 782 times.
782 if (consumer.running) {
1409 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' already running\n", consumer.name.c_str());
1410 ✗ return true;
1411 }
1412
1413 // Set PipeWire env (same as GStreamerOut)
1414 782 setenv("PIPEWIRE_RUNTIME_DIR", "/run/pipewire-fpp", 0);
1415
1416 // Build GStreamer consumer pipeline description
1417 782 std::string pipelineDesc;
1418
1419 // Common source: intervideosrc reads from source's intervideosink channel.
1420 // This uses GStreamer's in-process inter-pipeline communication,
1421 // bypassing PipeWire for video (PipeWire's video driver scheduling
1422 // cannot drive video graph cycles at rate=0).
1423 // timeout=5s — must not overflow gint64 when converted to microseconds
1424 // and added to g_get_monotonic_time(); UINT64_MAX caused spin-loop.
1425 // A consumer without a fixed source follows media playback, which
1426 // publishes the active slot's channel (StartConsumers() records which).
1427 782 const std::string channel = !channelArg.empty() ? channelArg
1428
5/6
✓ Branch 0 taken 752 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 37 times.
✓ Branch 3 taken 715 times.
✓ Branch 4 taken 782 times.
✗ Branch 5 not taken.
782 : consumer.sourceNode.empty() ? m_activeProducer : consumer.sourceNode;
1429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 782 times.
782 if (channel.empty()) {
1430 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: consumer '%s' has no source to read\n", consumer.name.c_str());
1431 ✗ return false;
1432 }
1433 const std::string srcDesc = "intervideosrc timeout=5000000000 channel="
1434
2/4
✓ Branch 0 taken 782 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 782 times.
✗ Branch 3 not taken.
782 + channel + " ! videoconvert ! ";
1435
1/2
✓ Branch 0 taken 782 times.
✗ Branch 1 not taken.
782 pipelineDesc = srcDesc + "videoscale ! ";
1436
1437
3/4
✓ Branch 0 taken 782 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 707 times.
✓ Branch 3 taken 75 times.
782 if (consumer.type == "hdmi") {
1438
5/6
✓ Branch 0 taken 707 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 702 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 702 times.
707 if (consumer.connectorId <= 0 || consumer.cardPath.empty()) {
1439
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 LogWarn(VB_MEDIAOUT, "VideoOutputManager: HDMI consumer '%s' has no valid connector\n", consumer.name.c_str());
1440 11 return false;
1441 }
1442
1443 // Check if the connector is actually connected before launching
1444 // a consumer pipeline — avoids wasted effort and error log noise.
1445 // The mode reported here is also the authority on the display size:
1446 // the values in the config are whatever was current when the group was
1447 // saved, and are 0 for a connector that was down at the time. "fill"
1448 // and "stretch" are both defined against the real panel, so they need
1449 // the live figures.
1450 702 int dispW = consumer.width;
1451 702 int dispH = consumer.height;
1452
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 699 times.
702 if (!consumer.connector.empty()) {
1453
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 auto drmCheck = GStreamerOutput::ResolveDrmConnector(consumer.connector);
1454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!drmCheck.connected) {
1455 ✗ LogInfo(VB_MEDIAOUT, "VideoOutputManager: Skipping HDMI consumer '%s' — "
1456 "connector %s (id=%d) is not connected\n",
1457 consumer.name.c_str(), consumer.connector.c_str(), consumer.connectorId);
1458 ✗ return false;
1459 }
1460
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (drmCheck.displayWidth > 0 && drmCheck.displayHeight > 0) {
1461 3 dispW = drmCheck.displayWidth;
1462 3 dispH = drmCheck.displayHeight;
1463 }
1464
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 }
1465
1466 // Media playback shows on this connector (its kmssink holds it); a
1467 // persistent consumer comes back when that ends. Two kmssinks cannot
1468 // share one connector.
1469
3/4
✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 696 times.
702 if (m_mediaConnectors.count(consumer.connectorId)) {
1470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!consumer.sourceNode.empty()) {
1471 ✗ consumer.suspendedForMedia = true;
1472 }
1473
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Skipping HDMI consumer '%s' — "
1474 "media is playing on connector %s (id=%d)\n",
1475 consumer.name.c_str(), consumer.connector.c_str(), consumer.connectorId);
1476 6 return false;
1477 }
1478
1479 // Check CMA memory before starting HDMI consumer — kmssink needs
1480 // ~16-24 MB of CMA for dumb buffers. CmaFree may appear low because
1481 // the kernel uses CMA pages for general movable allocations; those
1482 // pages are migrated on demand when a real DMA allocation occurs.
1483 // The wait for in-flight teardowns to give their buffers back happens
1484 // before m_mutex is taken (WaitForCmaIfLow()); here just warn and
1485 // proceed — the kernel will attempt CMA migration, and if it truly
1486 // fails the DRM ioctl (and hence pipeline start) will error out on
1487 // its own.
1488 {
1489
1/2
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
696 long cmaFreeKB = ReadCmaFreeKB();
1490
3/4
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 687 times.
696 if (cmaFreeKB >= 0 && cmaFreeKB < CMA_WARN_KB) {
1491
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 LogWarn(VB_MEDIAOUT, "VideoOutputManager: CMA low (%ld kB) for '%s' — "
1492 "proceeding anyway (kernel will attempt CMA migration)\n",
1493 cmaFreeKB, consumer.name.c_str());
1494 }
1495 }
1496
1497 // Add scaling caps for kmssink (vc4 DRM). Do NOT force BGRx —
1498 // vc4 display hardware on all Pi models (3/4/5) natively accepts
1499 // YUV formats (I420, NV12) and performs YUV→RGB conversion in the
1500 // HVS scanout pipeline for free. Letting GStreamer auto-negotiate
1501 // the format keeps the data in YUV end-to-end so videoscale
1502 // processes 1.5 bytes/pixel instead of 4 (BGRx), cutting CPU by ~60%.
1503
2/4
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 696 times.
✗ Branch 3 not taken.
696 int fps = VideoInputManager::Instance().GetSourceFramerate(consumer.sourceNode);
1504
2/2
✓ Branch 0 taken 637 times.
✓ Branch 1 taken 59 times.
696 if (fps <= 0) fps = 30; // fallback for on-demand consumers
1505
1506 // How the frame meets the panel. kmssink scales whatever it is given
1507 // up to the display preserving aspect ratio, and has no property to
1508 // stop it doing so, which decides how each mode is reached:
1509 //
1510 // fit — kmssink's own behaviour; nothing to add.
1511 // fill — hand kmssink a region already shaped like the display,
1512 // so its aspect-preserving scale covers the panel exactly.
1513 // Expressed as a target aspect on the crop, so the same
1514 // single videocrop does the region and the fill together.
1515 // stretch — the sink cannot do it, so the frame has to arrive at the
1516 // display's exact size: a real videoscale with borders off.
1517
3/10
✗ Branch 0 not taken.
✓ Branch 1 taken 696 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 696 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 696 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
696 std::string scaling = consumer.scaling.empty() ? "fit" : consumer.scaling;
1518
8/10
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 693 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 695 times.
696 if (scaling != "fit" && (dispW <= 0 || dispH <= 0)) {
1519
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogWarn(VB_MEDIAOUT, "VideoOutputManager: consumer '%s' asked for %s but the "
1520 "display size is unknown — showing it fitted instead\n",
1521 consumer.name.c_str(), scaling.c_str());
1522
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 scaling = "fit";
1523 }
1524
1525 // Idempotent, and derived from the size resolved above rather than the
1526 // stored one, so a display that comes back at a different mode gets a
1527 // correct fill on the next start.
1528
1/2
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
696 consumer.crop.targetAspect = (scaling == "fill")
1529
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 695 times.
696 ? (double)dispW / (double)dispH : 0.0;
1530
1531 // Showing a sub-region means the display size caps have to go: they
1532 // would make videoscale squeeze the whole source down to the panel
1533 // first, and the crop would then take half of an already-squashed
1534 // frame. Crop at source resolution instead and let kmssink scale the
1535 // region up during scanout, which is free and better quality anyway.
1536
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 695 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
696 bool cropping = !consumer.crop.IsFullFrame() && consumer.crop.IsValid();
1537
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 695 times.
696 if (cropping) {
1538 // Naming a missing element in the description fails the whole
1539 // gst_parse_launch, taking the output down entirely. Losing the
1540 // crop and showing the full frame is the better failure.
1541
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 GstElementFactory* cropFactory = gst_element_factory_find("videocrop");
1542
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (cropFactory) {
1543
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 gst_object_unref(cropFactory);
1544 } else {
1545 ✗ LogErr(VB_MEDIAOUT, "VideoOutputManager: videocrop element not available for '%s' — "
1546 "is gstreamer1.0-plugins-good installed? Showing the full frame\n",
1547 consumer.name.c_str());
1548 // Permanent: the element appears only with a package install and a restart.
1549 ✗ WarningHolder::AddWarning(31, "Video crop unavailable: videocrop element missing (install gstreamer1.0-plugins-good)");
1550 ✗ cropping = false;
1551 // Without the crop there is no region and no fill. Say so, so
1552 // the missing plugin does not read as a scaling bug.
1553 ✗ consumer.crop.targetAspect = 0.0;
1554 ✗ if (scaling == "fill") {
1555 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: consumer '%s' falls back to fit — "
1556 "fill needs videocrop\n", consumer.name.c_str());
1557 ✗ scaling = "fit";
1558 }
1559 }
1560 }
1561
1562 // Stretch keeps the display caps: they are what forces the non-uniform
1563 // scale. A region crop then has to be selected BEFORE that scale, at
1564 // source resolution, or it would take its slice out of an already
1565 // squashed frame — the same trap the comment above describes. That
1566 // costs the videocrop its metadata-only fast path, which stretch was
1567 // paying for anyway with a full videoscale pass.
1568
1/2
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
696 const bool stretching = (scaling == "stretch");
1569
1570
1/2
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
696 pipelineDesc = srcDesc;
1571
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 695 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
696 if (cropping && stretching)
1572 ✗ pipelineDesc += "videocrop name=vcrop ! ";
1573
1/2
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
696 pipelineDesc += "videoscale";
1574
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 695 times.
696 if (stretching)
1575
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 pipelineDesc += " add-borders=false";
1576
1/2
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
696 pipelineDesc += " ! ";
1577
1578
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 695 times.
696 if (stretching) {
1579 // Guaranteed non-zero: an unknown display size already dropped the
1580 // mode back to fit above.
1581
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 pipelineDesc += "video/x-raw,width=" + std::to_string(dispW)
1582
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
4 + ",height=" + std::to_string(dispH)
1583
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 + ",pixel-aspect-ratio=1/1"
1584
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
3 + ",framerate=" + std::to_string(fps) + "/1 ! ";
1585
5/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 693 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
695 } else if (consumer.width > 0 && consumer.height > 0 && !cropping) {
1586
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 pipelineDesc += "video/x-raw,width=" + std::to_string(consumer.width)
1587
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
4 + ",height=" + std::to_string(consumer.height)
1588
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 + ",pixel-aspect-ratio=1/1"
1589
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
3 + ",framerate=" + std::to_string(fps) + "/1 ! ";
1590 } else {
1591 // No size caps: kmssink scales the frame to the panel during
1592 // scanout, for free and preserving aspect ratio — which is fit,
1593 // and is also what makes the fill crop land correctly.
1594
3/6
✓ Branch 0 taken 694 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 694 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 694 times.
✗ Branch 5 not taken.
694 pipelineDesc += "video/x-raw,framerate=" + std::to_string(fps) + "/1 ! ";
1595 }
1596
1597 // Queue absorbs timing jitter between intervideosrc and kmssink;
1598 // without it GStreamer warns about missing latency compensation.
1599
1/2
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
696 pipelineDesc += "queue max-size-buffers=2 ! ";
1600
1601 // Immediately upstream of kmssink so the crop rides along as metadata
1602 // (the DRM plane's source rectangle) rather than copying every frame.
1603
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 695 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
696 if (cropping && !stretching)
1604
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 pipelineDesc += "videocrop name=vcrop ! ";
1605
1606 // kmssink sync=true paces frame rendering by intervideosrc timestamps
1607 // (fresh monotonic timestamps, independent of HLS timing).
1608 // skip-vsync=true, as on every other kmssink here: without it kmssink
1609 // waits for its page-flip event with drmHandleEvent() on its fd. When
1610 // DrmCards::AcquireLease() can't lease the connector (a restart while
1611 // the old consumer still holds it), that fd is the card's shared one,
1612 // whose events the framebuffer outputs drain: kmssink then stalled 3 s
1613 // on an event they took, or ran its handler on one of theirs, whose
1614 // user data is a flip token, not its pointer (SIGSEGV).
1615 pipelineDesc += "kmssink name=sink sync=true"
1616
1/2
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
1392 " connector-id=" + std::to_string(consumer.connectorId)
1617
2/4
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 696 times.
✗ Branch 3 not taken.
696 + " restore-crtc=true skip-vsync=true";
1618
1619
3/4
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✓ Branch 3 taken 41 times.
771 } else if (consumer.type == "overlay") {
1620 // Resolve the pixel overlay model to get its dimensions
1621
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 PixelOverlayModel* model = PixelOverlayManager::INSTANCE.getModel(consumer.overlayModel);
1622
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 33 times.
34 if (!model) {
1623
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogWarn(VB_MEDIAOUT, "VideoOutputManager: Overlay model '%s' not found for consumer '%s'\n",
1624 consumer.overlayModel.c_str(), consumer.name.c_str());
1625 1 return false;
1626 }
1627
1628 33 int overlayW = model->getWidth();
1629 33 int overlayH = model->getHeight();
1630
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 if (overlayW <= 0 || overlayH <= 0) {
1631 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: Overlay model '%s' has invalid size %dx%d\n",
1632 consumer.overlayModel.c_str(), overlayW, overlayH);
1633 ✗ return false;
1634 }
1635
1636 // Set up shared overlay state for the appsink callback
1637
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 consumer.overlayState = std::make_shared<OverlayState>();
1638 33 consumer.overlayState->model = model;
1639 33 consumer.overlayState->width = overlayW;
1640 33 consumer.overlayState->height = overlayH;
1641 // A prewarmed member stays dark until ActivateConsumer().
1642 33 consumer.overlayState->active = startPlaying;
1643
1644 // Register model listener so we track model replacement/deletion
1645 33 std::weak_ptr<OverlayState> weakState = consumer.overlayState;
1646
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
66 PixelOverlayManager::INSTANCE.addModelListener(consumer.overlayModel,
1647
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
66 "VideoOutputManager_" + consumer.pipeWireNodeName,
1648 66 [weakState](PixelOverlayModel* m) {
1649
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (auto state = weakState.lock()) {
1650
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::lock_guard<std::mutex> lock(state->mtx);
1651 2 state->model = m;
1652
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (m) {
1653 1 state->width = m->getWidth();
1654 1 state->height = m->getHeight();
1655 }
1656 4 }
1657 2 });
1658
1659 // Build pipeline: pipewiresrc → videoconvert → videoscale → capsfilter(RGB, WxH) → appsink
1660
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
66 pipelineDesc += "video/x-raw,format=RGB,width=" + std::to_string(overlayW)
1661
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
132 + ",height=" + std::to_string(overlayH)
1662
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
66 + ",pixel-aspect-ratio=1/1"
1663
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
33 + " ! appsink name=sink emit-signals=true sync=true max-buffers=2 drop=true";
1664
1665
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Overlay consumer '%s' targeting model '%s' (%dx%d)\n",
1666 consumer.name.c_str(), consumer.overlayModel.c_str(), overlayW, overlayH);
1667
1668
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
74 } else if (consumer.type == "rtp") {
1669 // RTP video output — encoding-dependent pipeline
1670
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 std::string enc = consumer.rtpEncoding;
1671
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
41 if (enc.empty()) enc = "raw";
1672
1673
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 41 times.
41 if (enc == "h264") {
1674 pipelineDesc += "x264enc tune=zerolatency bitrate=4000 speed-preset=ultrafast"
1675 " key-int-max=30 bframes=0"
1676 " ! video/x-h264,profile=baseline"
1677 ✗ " ! rtph264pay config-interval=1 pt=96";
1678
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 41 times.
41 } else if (enc == "h265") {
1679 pipelineDesc += "x265enc tune=zerolatency bitrate=3000 speed-preset=ultrafast"
1680 " key-int-max=30"
1681 ✗ " ! rtph265pay config-interval=1 pt=96";
1682
3/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 1 times.
41 } else if (enc == "mjpeg") {
1683
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 pipelineDesc += "jpegenc quality=80 ! rtpjpegpay pt=26";
1684 } else {
1685
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 pipelineDesc += "rtpvrawpay pt=96";
1686 }
1687
1688
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
82 pipelineDesc += " ! udpsink host=" + consumer.address
1689
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
164 + " port=" + std::to_string(consumer.port)
1690
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 + " auto-multicast=true";
1691
1692 // Generate SDP file for receivers (VLC, ffplay, etc.)
1693
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 WriteSdpFile(consumer, enc);
1694
1695 41 } else {
1696 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: Unknown consumer type '%s'\n", consumer.type.c_str());
1697 ✗ return false;
1698 }
1699
1700
1/2
✓ Branch 0 taken 770 times.
✗ Branch 1 not taken.
770 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Starting consumer '%s' (%s): %s\n",
1701 consumer.name.c_str(), consumer.type.c_str(), pipelineDesc.c_str());
1702
1703 770 GError* error = nullptr;
1704
1/2
✓ Branch 0 taken 770 times.
✗ Branch 1 not taken.
770 consumer.pipeline = gst_parse_launch(pipelineDesc.c_str(), &error);
1705
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 768 times.
770 if (!consumer.pipeline) {
1706
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 LogErr(VB_MEDIAOUT, "VideoOutputManager: Failed to create pipeline for '%s': %s\n",
1707 consumer.name.c_str(), error ? error->message : "unknown error");
1708 // Re-raised by every later attempt (a persistent consumer is retried,
1709 // an on-demand one starts with each video), so it ages out once the
1710 // pipeline builds again.
1711
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
2 WarningHolder::AddWarningTimeout(120, 31, "Video output pipeline creation failed for '" + consumer.name + "'");
1712
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (error) g_error_free(error);
1713 2 return false;
1714 }
1715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 768 times.
768 if (error) {
1716 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: Pipeline warning for '%s': %s\n",
1717 consumer.name.c_str(), error->message);
1718 ✗ g_error_free(error);
1719 }
1720
1721 // intervideosrc is configured inline — no PipeWire node setup needed.
1722
1723 // For HDMI consumers, pass the shared DRM fd to kmssink so all
1724 // pipelines share a single DRM master — avoids contention on card1.
1725 // fd must be set BEFORE driver-name (kmssink rejects fd if devname is set).
1726 // When fd is available, driver-name is unnecessary (derived from the fd).
1727 // When fd is unavailable, fall back to driver-name=vc4.
1728
3/4
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 695 times.
✓ Branch 3 taken 73 times.
768 if (consumer.type == "hdmi") {
1729 // The crop element (if the description asked for one) needs its
1730 // fractional region bound before the pipeline sees any caps.
1731
3/6
✓ Branch 0 taken 695 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 695 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 695 times.
✗ Branch 5 not taken.
695 GstElement* cropElem = gst_bin_get_by_name(GST_BIN(consumer.pipeline), "vcrop");
1732
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 694 times.
695 if (cropElem) {
1733
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AttachCropResolver(cropElem, consumer.crop);
1734
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 gst_object_unref(cropElem);
1735 }
1736
1737
3/6
✓ Branch 0 taken 695 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 695 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 695 times.
✗ Branch 5 not taken.
695 GstElement* sink = gst_bin_get_by_name(GST_BIN(consumer.pipeline), "sink");
1738
1/2
✓ Branch 0 taken 695 times.
✗ Branch 1 not taken.
695 if (sink) {
1739 // Prefer live-resolved card path over config value
1740 695 std::string resolvedCard;
1741 695 int resolvedConnId = consumer.connectorId;
1742
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 694 times.
695 if (!consumer.connector.empty()) {
1743
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 auto drmCheck = GStreamerOutput::ResolveDrmConnector(consumer.connector);
1744
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 resolvedCard = drmCheck.cardPath;
1745
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (drmCheck.connectorId > 0)
1746 1 resolvedConnId = drmCheck.connectorId;
1747 1 }
1748
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 694 times.
✓ Branch 2 taken 695 times.
✗ Branch 3 not taken.
695 std::string cardForFd = !resolvedCard.empty() ? resolvedCard : consumer.cardPath;
1749
1/2
✓ Branch 0 taken 695 times.
✗ Branch 1 not taken.
695 if (!cardForFd.empty()) {
1750
1/2
✓ Branch 0 taken 695 times.
✗ Branch 1 not taken.
695 consumer.drmLease = DrmCards::AcquireLease(cardForFd, resolvedConnId);
1751 }
1752 695 int drmFd = consumer.drmLease.fd;
1753
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 688 times.
695 if (drmFd >= 0) {
1754
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 g_object_set(sink, "fd", drmFd, NULL);
1755 7 int planeId = consumer.drmLease.planeId;
1756
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
7 if (planeId >= 0) {
1757
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 g_object_set(sink, "plane-id", planeId, NULL);
1758 }
1759
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' kmssink on DRM fd=%d (%s) plane=%d\n",
1760 consumer.name.c_str(), drmFd, consumer.drmLease.isLease ? "lease" : "card's shared fd", planeId);
1761 } else {
1762
1/2
✓ Branch 0 taken 688 times.
✗ Branch 1 not taken.
688 g_object_set(sink, "driver-name", "vc4", NULL);
1763
1/2
✓ Branch 0 taken 688 times.
✗ Branch 1 not taken.
688 LogInfo(VB_MEDIAOUT, "VideoOutputManager: No shared DRM fd for consumer '%s', using driver-name=vc4\n",
1764 consumer.name.c_str());
1765 }
1766
1/2
✓ Branch 0 taken 695 times.
✗ Branch 1 not taken.
695 gst_object_unref(sink);
1767 695 }
1768 }
1769
1770 // Wire up appsink callback for overlay consumers.
1771 // user_data is a heap-allocated COPY of the shared_ptr<OverlayState> (not
1772 // the raw OverlayState*), with a GDestroyNotify that deletes it. This
1773 // keeps OverlayState alive until GStreamer itself destroys the appsink
1774 // element, which can be well after StopConsumer() resets
1775 // consumer.overlayState: StopConsumer's pipeline-NULL transition runs on
1776 // a detached thread, so the appsink's own streaming thread can still be
1777 // in flight (about to call OnNewSample) when the ConsumerInfo's copy of
1778 // the shared_ptr is reset — passing the raw pointer there was a
1779 // use-after-free race.
1780
6/8
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 736 times.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 32 times.
✓ Branch 7 taken 736 times.
768 if (consumer.type == "overlay" && consumer.overlayState) {
1781
3/6
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
32 GstElement* sink = gst_bin_get_by_name(GST_BIN(consumer.pipeline), "sink");
1782
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (sink) {
1783 32 GstAppSinkCallbacks callbacks = {};
1784 32 callbacks.new_sample = OnOverlaySample;
1785
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
32 auto* stateRef = new std::shared_ptr<OverlayState>(consumer.overlayState);
1786
3/6
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
32 gst_app_sink_set_callbacks(GST_APP_SINK(sink), &callbacks, stateRef,
1787 ✗ [](gpointer data) {
1788
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 delete static_cast<std::shared_ptr<OverlayState>*>(data);
1789 32 });
1790
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 gst_object_unref(sink);
1791 }
1792 }
1793
1794 // Start the pipeline in a background thread.
1795 // With intervideosrc, set_state(PLAYING) should complete quickly
1796 // (no PipeWire negotiation needed).
1797 // Fresh shared_ptr instances per run: a still-draining prior thread (if
1798 // any) keeps its own copy and is unaffected by this restart.
1799
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 consumer.shutdownRequested = std::make_shared<std::atomic<bool>>(false);
1800
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 consumer.pipelineDead = std::make_shared<std::atomic<bool>>(false);
1801
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 consumer.playing = std::make_shared<std::atomic<bool>>(startPlaying);
1802
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 consumer.stateLock = std::make_shared<std::mutex>();
1803 768 consumer.running = true;
1804 768 consumer.suspendedForMedia = false;
1805 768 consumer.parked = !startPlaying;
1806
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 consumer.channel = channel;
1807
1808
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 std::string consumerName = consumer.name;
1809 768 GstElement* pipeline = consumer.pipeline;
1810 // Own a ref on the pipeline for the lifetime of the bus-monitor thread.
1811 // StopConsumer()'s detached teardown thread calls gst_object_unref() on
1812 // the pipeline, which can drop the LAST ref while this thread is about
1813 // to touch it (gst_element_get_state / gst_bus_timed_pop) — without our
1814 // own ref that's a use-after-free race. Released by PipelineRefGuard on
1815 // every exit path below.
1816
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 gst_object_ref(pipeline);
1817 768 std::shared_ptr<std::atomic<bool>> shutdownFlag = consumer.shutdownRequested;
1818 768 std::shared_ptr<std::atomic<bool>> pipelineDead = consumer.pipelineDead;
1819
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 std::string sourceNode = consumer.sourceNode;
1820 768 std::shared_ptr<std::atomic<bool>> playing = consumer.playing;
1821 768 std::shared_ptr<std::mutex> stateLock = consumer.stateLock;
1822
1823
2/14
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 768 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
1536 consumer.startThread = std::thread([pipeline, consumerName, shutdownFlag, pipelineDead, sourceNode,
1824 playing, stateLock]() {
1825 // A persistent consumer that dies while its source runs gets no
1826 // producer-ready notification to bring it back.
1827 1401 auto scheduleRestart = [&sourceNode]() {
1828
1/2
✓ Branch 0 taken 633 times.
✗ Branch 1 not taken.
633 if (!sourceNode.empty()) {
1829 633 VideoOutputManager::Instance().ScheduleConsumerRestart(sourceNode);
1830 }
1831 1401 };
1832 struct PipelineRefGuard {
1833 GstElement* p;
1834 768 ~PipelineRefGuard() { gst_object_unref(p); }
1835 768 } pipelineRefGuard{pipeline};
1836
1837
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' background thread starting pipeline\n",
1838 consumerName.c_str());
1839
1840 // intervideosrc doesn't need PipeWire links — go straight to PLAYING,
1841 // or to PAUSED for a prewarmed member (intervideosrc is live, so
1842 // that is immediate and nothing flows until it is activated).
1843 GstStateChangeReturn ret;
1844 bool startPlaying;
1845 {
1846
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 std::lock_guard<std::mutex> sl(*stateLock);
1847 768 startPlaying = playing->load();
1848
3/4
✓ Branch 0 taken 738 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 768 times.
✗ Branch 3 not taken.
768 ret = gst_element_set_state(pipeline, startPlaying ? GST_STATE_PLAYING : GST_STATE_PAUSED);
1849 768 }
1850
3/4
✓ Branch 0 taken 738 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 768 times.
✗ Branch 3 not taken.
768 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' set_state(%s) returned %d\n",
1851 consumerName.c_str(), startPlaying ? "PLAYING" : "PAUSED", (int)ret);
1852
1853
2/2
✓ Branch 0 taken 676 times.
✓ Branch 1 taken 92 times.
768 if (ret == GST_STATE_CHANGE_FAILURE) {
1854 // Check if we were asked to shut down — don't log as error if so
1855
2/2
✓ Branch 0 taken 631 times.
✓ Branch 1 taken 45 times.
676 if (!shutdownFlag->load()) {
1856
1/2
✓ Branch 0 taken 631 times.
✗ Branch 1 not taken.
631 LogWarn(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' pipeline failed to start "
1857 "(connector may be disconnected)\n", consumerName.c_str());
1858 // Never reached PLAYING and nobody asked us to stop — mark
1859 // dead so persistent-consumer restart logic notices instead
1860 // of `running` staying true forever with a pipeline that
1861 // will never produce frames.
1862 631 pipelineDead->store(true);
1863
1/2
✓ Branch 0 taken 631 times.
✗ Branch 1 not taken.
631 scheduleRestart();
1864 }
1865 676 return;
1866 }
1867
1868
1/2
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
92 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' pipeline reached PLAYING (ret=%d)\n",
1869 consumerName.c_str(), ret);
1870
1871 // Monitor the pipeline bus for errors / EOS while running
1872
3/6
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 92 times.
✗ Branch 5 not taken.
92 GstBus* bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
1873 92 int busLoopIter = 0;
1874
2/2
✓ Branch 0 taken 2990 times.
✓ Branch 1 taken 90 times.
3080 while (!shutdownFlag->load()) {
1875
1/2
✓ Branch 0 taken 2990 times.
✗ Branch 1 not taken.
2990 GstMessage* msg = gst_bus_timed_pop(bus, 500 * GST_MSECOND);
1876 2990 busLoopIter++;
1877
1878 // Periodic state check every ~5s (10 iterations * 500ms)
1879
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 2662 times.
2990 if (busLoopIter % 10 == 1) {
1880 328 GstState cur = GST_STATE_VOID_PENDING, pend = GST_STATE_VOID_PENDING;
1881
1/2
✓ Branch 0 taken 328 times.
✗ Branch 1 not taken.
328 gst_element_get_state(pipeline, &cur, &pend, 0);
1882
8/8
✓ Branch 0 taken 237 times.
✓ Branch 1 taken 91 times.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 206 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 30 times.
✓ Branch 6 taken 207 times.
✓ Branch 7 taken 121 times.
328 if (playing->load() && (cur != GST_STATE_PLAYING || pend != GST_STATE_VOID_PENDING)) {
1883
1/2
✓ Branch 0 taken 207 times.
✗ Branch 1 not taken.
207 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' state check: current=%d pending=%d\n",
1884 consumerName.c_str(), (int)cur, (int)pend);
1885 }
1886 }
1887
1888
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 2658 times.
2990 if (!msg) continue;
1889
1890
5/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
✓ Branch 3 taken 2031 times.
✓ Branch 4 taken 74 times.
✓ Branch 5 taken 464 times.
2658 switch (GST_MESSAGE_TYPE(msg)) {
1891 2 case GST_MESSAGE_ERROR: {
1892 2 GError* err = nullptr;
1893 2 gchar* debug = nullptr;
1894
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_message_parse_error(msg, &err, &debug);
1895
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!shutdownFlag->load()) {
1896
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.
2 LogWarn(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' error: %s (%s)\n",
1897 consumerName.c_str(), err ? err->message : "?", debug ? debug : "");
1898 // Pipeline died on its own (not via StopConsumer) —
1899 // see pipelineDead comment on the ConsumerInfo field.
1900 2 pipelineDead->store(true);
1901
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 scheduleRestart();
1902 }
1903
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (err) g_error_free(err);
1904
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 g_free(debug);
1905
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_message_unref(msg);
1906
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_object_unref(bus);
1907 2 return;
1908 }
1909 ✗ case GST_MESSAGE_EOS:
1910 ✗ LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' received EOS\n",
1911 consumerName.c_str());
1912 ✗ if (!shutdownFlag->load()) {
1913 ✗ pipelineDead->store(true);
1914 ✗ scheduleRestart();
1915 }
1916 ✗ gst_message_unref(msg);
1917 ✗ gst_object_unref(bus);
1918 ✗ return;
1919 87 case GST_MESSAGE_ASYNC_DONE:
1920
1/2
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
87 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' ASYNC_DONE (preroll complete)\n",
1921 consumerName.c_str());
1922 87 break;
1923 2031 case GST_MESSAGE_STATE_CHANGED:
1924
4/6
✓ Branch 0 taken 2031 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2031 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 300 times.
✓ Branch 5 taken 1731 times.
2031 if (GST_MESSAGE_SRC(msg) == GST_OBJECT(pipeline)) {
1925 GstState oldSt, newSt, pendSt;
1926
1/2
✓ Branch 0 taken 300 times.
✗ Branch 1 not taken.
300 gst_message_parse_state_changed(msg, &oldSt, &newSt, &pendSt);
1927
1/2
✓ Branch 0 taken 300 times.
✗ Branch 1 not taken.
300 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' state %d -> %d (pending %d)\n",
1928 consumerName.c_str(), (int)oldSt, (int)newSt, (int)pendSt);
1929 }
1930 2031 break;
1931 74 case GST_MESSAGE_WARNING: {
1932 74 GError* err = nullptr;
1933 74 gchar* debug = nullptr;
1934
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 gst_message_parse_warning(msg, &err, &debug);
1935
3/6
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 74 times.
✗ Branch 5 not taken.
74 LogWarn(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' warning: %s (%s)\n",
1936 consumerName.c_str(), err ? err->message : "?", debug ? debug : "");
1937
2/4
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
74 if (err) g_error_free(err);
1938
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 g_free(debug);
1939 74 break;
1940 }
1941 464 default:
1942 464 break;
1943 }
1944
1/2
✓ Branch 0 taken 2656 times.
✗ Branch 1 not taken.
2656 gst_message_unref(msg);
1945 }
1946
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 gst_object_unref(bus);
1947
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' background thread exiting\n",
1948 consumerName.c_str());
1949
3/4
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 678 times.
1536 });
1950
1951
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' launched in background thread\n",
1952 consumer.name.c_str());
1953 768 return true;
1954 #else
1955 LogWarn(VB_MEDIAOUT, "VideoOutputManager: GStreamer not available\n");
1956 return false;
1957 #endif
1958 782 }
1959
1960 #ifdef HAS_GSTREAMER_VIDEO_OUTPUT
1961 2631 GstFlowReturn VideoOutputManager::OnOverlaySample(GstAppSink* appsink, gpointer userData) {
1962 // userData is a heap std::shared_ptr<OverlayState>* (see StartConsumer) —
1963 // unwrap it rather than treating it as a raw OverlayState*. The
1964 // shared_ptr keeps OverlayState alive independent of ConsumerInfo, so
1965 // this is safe even if StopConsumer has already reset the ConsumerInfo's
1966 // own copy.
1967 2631 auto* stateRef = static_cast<std::shared_ptr<OverlayState>*>(userData);
1968
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2631 times.
2631 if (!stateRef)
1969 ✗ return GST_FLOW_EOS;
1970 2631 OverlayState* state = stateRef->get();
1971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2631 times.
2631 if (!state)
1972 ✗ return GST_FLOW_EOS;
1973
1974
1/2
✓ Branch 0 taken 2631 times.
✗ Branch 1 not taken.
2631 GstSample* sample = gst_app_sink_pull_sample(appsink);
1975
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2631 times.
2631 if (!sample)
1976 ✗ return GST_FLOW_OK;
1977 // Inactive: parked or prewarmed (or being stopped). Drop the frame
1978 // rather than end the stream, which would end a parked pipeline.
1979
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 2532 times.
2631 if (!state->active.load()) {
1980
1/2
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
99 gst_sample_unref(sample);
1981 99 return GST_FLOW_OK;
1982 }
1983
1984
1/2
✓ Branch 0 taken 2532 times.
✗ Branch 1 not taken.
2532 GstBuffer* buffer = gst_sample_get_buffer(sample);
1985 // The frame's own size comes from its caps. state->width/height follow
1986 // the model, which a listener can swap for one of another size while
1987 // frames negotiated for the old one are still arriving.
1988 2532 int w = 0;
1989 2532 int h = 0;
1990
2/4
✓ Branch 0 taken 2532 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2532 times.
✗ Branch 3 not taken.
2532 if (GstCaps* caps = gst_sample_get_caps(sample)) {
1991
2/4
✓ Branch 0 taken 2532 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2532 times.
✗ Branch 3 not taken.
2532 if (const GstStructure* st = gst_caps_get_structure(caps, 0)) {
1992
1/2
✓ Branch 0 taken 2532 times.
✗ Branch 1 not taken.
2532 gst_structure_get_int(st, "width", &w);
1993
1/2
✓ Branch 0 taken 2532 times.
✗ Branch 1 not taken.
2532 gst_structure_get_int(st, "height", &h);
1994 }
1995 }
1996 GstMapInfo map;
1997
5/10
✓ Branch 0 taken 2532 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2532 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2532 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2532 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2532 times.
✗ Branch 9 not taken.
2532 if (w > 0 && h > 0 && gst_buffer_map(buffer, &map, GST_MAP_READ)) {
1998
1/2
✓ Branch 0 taken 2532 times.
✗ Branch 1 not taken.
2532 std::lock_guard<std::mutex> lock(state->mtx);
1999 // Checked again under the lock: ParkConsumer hands the model back
2000 // under it, and a frame blitted after that would take it again.
2001
1/2
✓ Branch 0 taken 2532 times.
✗ Branch 1 not taken.
2532 PixelOverlayModel* model = state->active.load() ? state->model : nullptr;
2002 2532 const size_t rowBytes = (size_t)w * 3;
2003 2532 const size_t expectedSize = rowBytes * h;
2004 // Packed RGB rows are padded to 4 bytes unless the buffer is exactly
2005 // the tightly packed size.
2006
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2532 times.
2532 const size_t stride = (map.size == expectedSize) ? rowBytes : ((rowBytes + 3) & ~(size_t)3);
2007
3/4
✓ Branch 0 taken 2504 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 2504 times.
✗ Branch 3 not taken.
2532 if (model && map.size >= stride * (h - 1) + rowBytes) {
2008 // blitData(), not setData(): setData() reads
2009 // width*height*bytesPerPixel, and an RGBW model has 4 bytes per
2010 // pixel to this frame's 3. blitData() converts and clips.
2011 2504 const PixelOverlayState enabled(PixelOverlayState::Enabled);
2012
1/2
✓ Branch 0 taken 2504 times.
✗ Branch 1 not taken.
2504 if (stride == rowBytes) {
2013
1/2
✓ Branch 0 taken 2504 times.
✗ Branch 1 not taken.
2504 model->blitData(map.data, w, h, 3, 0, 0, enabled);
2014 } else {
2015 ✗ std::vector<uint8_t> stripped(expectedSize);
2016 ✗ for (int y = 0; y < h; y++) {
2017 ✗ memcpy(&stripped[(size_t)y * rowBytes], map.data + (size_t)y * stride, rowBytes);
2018 }
2019 ✗ model->blitData(stripped.data(), w, h, 3, 0, 0, enabled);
2020 ✗ }
2021
2022 // Auto-enable model if it was disabled
2023
3/4
✓ Branch 0 taken 2504 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✓ Branch 3 taken 2467 times.
2504 if (model->getState() == PixelOverlayState::Disabled) {
2024 37 state->wasDisabled = true;
2025
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 model->setState(PixelOverlayState(PixelOverlayState::Enabled));
2026 }
2027 }
2028
1/2
✓ Branch 0 taken 2532 times.
✗ Branch 1 not taken.
2532 gst_buffer_unmap(buffer, &map);
2029 2532 }
2030
2031
1/2
✓ Branch 0 taken 2532 times.
✗ Branch 1 not taken.
2532 gst_sample_unref(sample);
2032 2532 return GST_FLOW_OK;
2033 }
2034 #endif
2035
2036 844 long VideoOutputManager::ReadCmaFreeKB() {
2037
1/2
✓ Branch 0 taken 844 times.
✗ Branch 1 not taken.
844 FILE* f = fopen("/proc/meminfo", "r");
2038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 844 times.
844 if (!f) return -1;
2039 char line[256];
2040 844 long kb = -1;
2041
2/4
✓ Branch 0 taken 39588 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39588 times.
✗ Branch 3 not taken.
39588 while (fgets(line, sizeof(line), f)) {
2042
2/2
✓ Branch 0 taken 844 times.
✓ Branch 1 taken 38744 times.
39588 if (sscanf(line, "CmaFree: %ld kB", &kb) == 1) break;
2043 }
2044
1/2
✓ Branch 0 taken 844 times.
✗ Branch 1 not taken.
844 fclose(f);
2045 844 return kb;
2046 }
2047
2048 148 void VideoOutputManager::WaitForCmaIfLow() {
2049 // When CMA is short and a teardown is still releasing its DRM buffers,
2050 // give it a moment before starting consumers. Called before m_mutex is
2051 // taken: it can wait seconds, and media playback's Start() asks this
2052 // manager for crop rectangles under that lock while holding
2053 // mediaOutputLock.
2054 148 long cmaFreeKB = ReadCmaFreeKB();
2055
3/4
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 137 times.
148 if (cmaFreeKB >= 0 && cmaFreeKB < CMA_WARN_KB) {
2056 11 LogInfo(VB_MEDIAOUT, "VideoOutputManager: CMA low (%ld kB), waiting for pending teardowns\n", cmaFreeKB);
2057 11 WaitForPendingTeardowns(3000);
2058 }
2059 148 }
2060
2061 1976 bool VideoOutputManager::WaitForPendingTeardowns(int timeoutMs) {
2062
1/2
✓ Branch 0 taken 1976 times.
✗ Branch 1 not taken.
1976 std::unique_lock<std::mutex> tl(m_teardownMutex);
2063
2/2
✓ Branch 0 taken 1901 times.
✓ Branch 1 taken 75 times.
1976 if (m_pendingTeardowns <= 0)
2064 1901 return true;
2065
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Waiting for %d pending pipeline teardown(s) to release DRM/CMA...\n",
2066 m_pendingTeardowns);
2067
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 bool ok = m_teardownCV.wait_for(tl, std::chrono::milliseconds(timeoutMs),
2068 181 [this] { return m_pendingTeardowns <= 0; });
2069
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 70 times.
75 if (!ok) {
2070
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 LogWarn(VB_MEDIAOUT, "VideoOutputManager: Timed out waiting for pipeline teardowns "
2071 "(%d still pending after %d ms)\n", m_pendingTeardowns, timeoutMs);
2072 // A teardown that never completes leaks its pipeline's CMA, DRM
2073 // plane, and DRM fd ref forever, and holds m_pendingTeardowns above
2074 // zero permanently (blocking future WaitForPendingTeardowns calls
2075 // too). Surface it as a warning so a 24h soak-test log clearly
2076 // flags a wedged gst_element_set_state(NULL) instead of silently
2077 // bleeding memory.
2078 // Fixed text, so the teardown thread that finally finishes can take
2079 // it down (the count used to be in it, which no removal could match).
2080
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
15 WarningHolder::AddWarning(31, TEARDOWN_STUCK_WARNING);
2081 5 m_teardownStuckWarned = true;
2082 } else {
2083
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 LogInfo(VB_MEDIAOUT, "VideoOutputManager: All pipeline teardowns completed\n");
2084 }
2085 75 return ok;
2086 1976 }
2087
2088 854 void VideoOutputManager::StopConsumer(ConsumerInfo& consumer) {
2089 #ifdef HAS_GSTREAMER_VIDEO_OUTPUT
2090
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 768 times.
854 if (!consumer.running)
2091 86 return;
2092
2093 // If this consumer is part of a grouped pipeline (not the owner),
2094 // stop the pipeline owner instead — that tears down the whole group.
2095
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 768 times.
768 if (consumer.groupPipelineOwnerIdx >= 0) {
2096 ✗ int ownerIdx = consumer.groupPipelineOwnerIdx;
2097 ✗ consumer.groupPipelineOwnerIdx = -1;
2098 ✗ consumer.running = false;
2099 ✗ LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' is grouped — stopping group owner\n",
2100 consumer.name.c_str());
2101 ✗ if (ownerIdx < (int)m_consumers.size() && m_consumers[ownerIdx].running) {
2102 ✗ StopConsumer(m_consumers[ownerIdx]);
2103 }
2104 ✗ return;
2105 }
2106
2107 // Signal the background thread to exit first
2108 768 consumer.shutdownRequested->store(true);
2109
2110 // Deactivate overlay state before pipeline teardown to prevent
2111 // the appsink callback from accessing a model during shutdown
2112
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 736 times.
768 if (consumer.overlayState) {
2113 32 consumer.overlayState->active = false;
2114 }
2115
2116 // Detach the background thread — it may be blocked in a GStreamer
2117 // state change with PipeWire locks held, so joining could hang.
2118 // The thread checks shutdownRequested and will exit eventually.
2119 try {
2120
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 if (consumer.startThread.joinable())
2121
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 consumer.startThread.detach();
2122 ✗ } catch (...) {}
2123
2124 // Move pipeline NULL transition to a detached background thread.
2125 // gst_element_set_state(NULL) can block on PipeWire locks held by
2126 // the pipewiresrc element's internal thread during negotiation.
2127 // Never call it on the main thread or any critical path.
2128 // The thread decrements m_pendingTeardowns and notifies m_teardownCV
2129 // when done so callers (Reload, Resume) can wait for CMA to be freed.
2130
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 if (consumer.pipeline) {
2131 768 GstElement* p = consumer.pipeline;
2132 768 consumer.pipeline = nullptr;
2133 // Give the DRM lease back only after the pipeline reaches NULL (which
2134 // destroys the kmssink): closing it frees any GEM handles/FBs the
2135 // kmssink failed to release, and returns its plane for the next
2136 // consumer -- neither may happen while the kmssink is alive.
2137
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 DrmCards::Lease lease = consumer.drmLease;
2138 768 consumer.drmLease = DrmCards::Lease();
2139 // A restart may ask for the connector before this thread is done.
2140
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 DrmCards::RetireLease(lease);
2141 {
2142
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 std::lock_guard<std::mutex> tl(m_teardownMutex);
2143 768 m_pendingTeardowns++;
2144 768 }
2145 768 std::shared_ptr<std::mutex> stateLock = consumer.stateLock;
2146
1/4
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1536 std::thread([this, p, lease, stateLock]() mutable {
2147 768 uint64_t startMs = GetTimeMS();
2148 {
2149 // after any park's PAUSED already under way; see ParkConsumer()
2150
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 std::lock_guard<std::mutex> sl(*stateLock);
2151
1/2
✓ Branch 0 taken 766 times.
✗ Branch 1 not taken.
768 gst_element_set_state(p, GST_STATE_NULL);
2152 766 }
2153 766 gst_object_unref(p);
2154 766 DrmCards::ReleaseLease(lease);
2155 {
2156
1/2
✓ Branch 0 taken 766 times.
✗ Branch 1 not taken.
766 std::lock_guard<std::mutex> tl(m_teardownMutex);
2157 766 m_pendingTeardowns--;
2158
3/4
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 615 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 151 times.
766 if (m_pendingTeardowns == 0 && m_teardownStuckWarned) {
2159 ✗ m_teardownStuckWarned = false;
2160 ✗ WarningHolder::RemoveWarning(31, TEARDOWN_STUCK_WARNING);
2161 }
2162 766 }
2163 766 m_teardownCV.notify_all();
2164 766 LogDebug(VB_MEDIAOUT, "VideoOutputManager: pipeline teardown completed in %llu ms\n",
2165 (unsigned long long)(GetTimeMS() - startMs));
2166
2/4
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 768 times.
✗ Branch 3 not taken.
1534 }).detach();
2167
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
768 } else if (consumer.drmLease.fd >= 0) {
2168 // No pipeline to tear down (e.g. this consumer never fully started)
2169 // but a lease was still taken while wiring up the kmssink — release
2170 // it now since there's no teardown thread to do it later.
2171 ✗ DrmCards::ReleaseLease(consumer.drmLease);
2172 }
2173
2174 // Remove the listener, then restore the overlay model's state. In that
2175 // order: the listener runs under PixelOverlayManager's modelsLock and
2176 // takes state->mtx, so removing it (which takes modelsLock) while holding
2177 // state->mtx deadlocked against a model being re-added or auto-removed.
2178
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 736 times.
768 if (consumer.overlayState) {
2179
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 PixelOverlayManager::INSTANCE.removeModelListener(consumer.overlayModel,
2180
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
64 "VideoOutputManager_" + consumer.pipeWireNodeName);
2181 // Take the consumer's reference before locking: it is often the last
2182 // one (the appsink's goes with the pipeline), and the state must
2183 // outlive the lock on its own mutex. `state` is declared first, so
2184 // it is destroyed after `lock` has unlocked.
2185 32 std::shared_ptr<OverlayState> state = std::move(consumer.overlayState);
2186
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 std::lock_guard<std::mutex> lock(state->mtx);
2187
5/6
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✓ Branch 5 taken 16 times.
32 if (state->wasDisabled && state->model) {
2188
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 state->model->setState(PixelOverlayState(PixelOverlayState::Disabled));
2189 16 state->wasDisabled = false;
2190 }
2191 32 state->model = nullptr;
2192 32 }
2193
2194 768 consumer.running = false;
2195 768 consumer.parked = false;
2196
2197 // Also mark any grouped members as stopped
2198 768 int myIdx = (int)(&consumer - &m_consumers[0]);
2199
2/2
✓ Branch 0 taken 136179 times.
✓ Branch 1 taken 768 times.
136947 for (auto& c : m_consumers) {
2200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136179 times.
136179 if (c.groupPipelineOwnerIdx == myIdx) {
2201 ✗ c.groupPipelineOwnerIdx = -1;
2202 ✗ c.running = false;
2203 ✗ LogInfo(VB_MEDIAOUT, "VideoOutputManager: Grouped consumer '%s' stopped (group owner stopped)\n",
2204 c.name.c_str());
2205 }
2206 }
2207
2208 768 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Consumer '%s' stopped\n", consumer.name.c_str());
2209 #endif
2210 }
2211
2212 1961 void VideoOutputManager::StopAllConsumers() {
2213
2/2
✓ Branch 0 taken 368 times.
✓ Branch 1 taken 1961 times.
2329 for (auto& consumer : m_consumers) {
2214
1/2
✓ Branch 0 taken 368 times.
✗ Branch 1 not taken.
368 StopConsumer(consumer);
2215 }
2216 1961 }
2217
2218 67 std::string VideoOutputManager::BuildSdp(const ConsumerInfo& consumer, const std::string& encoding) {
2219 // Build SDP content describing the RTP stream.
2220 // See RFC 4566 (SDP), RFC 3984 (H.264 RTP), RFC 4175 (raw video RTP).
2221 67 std::string sdp;
2222
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 sdp += "v=0\r\n";
2223
3/6
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 67 times.
✗ Branch 5 not taken.
67 sdp += "o=FPP 0 0 IN IP4 " + consumer.address + "\r\n";
2224
3/6
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 67 times.
✗ Branch 5 not taken.
67 sdp += "s=FPP Video: " + consumer.name + "\r\n";
2225
3/6
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 67 times.
✗ Branch 5 not taken.
67 sdp += "c=IN IP4 " + consumer.address + "/32\r\n";
2226
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 sdp += "t=0 0\r\n";
2227
2228
2/4
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 67 times.
67 if (encoding == "h264") {
2229 ✗ sdp += "m=video " + std::to_string(consumer.port) + " RTP/AVP 96\r\n";
2230 ✗ sdp += "a=rtpmap:96 H264/90000\r\n";
2231 ✗ sdp += "a=fmtp:96 packetization-mode=1\r\n";
2232
2/4
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 67 times.
67 } else if (encoding == "h265") {
2233 ✗ sdp += "m=video " + std::to_string(consumer.port) + " RTP/AVP 96\r\n";
2234 ✗ sdp += "a=rtpmap:96 H265/90000\r\n";
2235
3/4
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✓ Branch 3 taken 1 times.
67 } else if (encoding == "mjpeg") {
2236
3/6
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 66 times.
✗ Branch 5 not taken.
66 sdp += "m=video " + std::to_string(consumer.port) + " RTP/AVP 26\r\n";
2237
1/2
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
66 sdp += "a=rtpmap:26 JPEG/90000\r\n";
2238 } else {
2239 // raw video (rtpvrawpay) — RFC 4175
2240
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 sdp += "m=video " + std::to_string(consumer.port) + " RTP/AVP 96\r\n";
2241
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 sdp += "a=rtpmap:96 raw/90000\r\n";
2242 }
2243
2244 67 return sdp;
2245 ✗ }
2246
2247 41 void VideoOutputManager::WriteSdpFile(const ConsumerInfo& consumer, const std::string& encoding) {
2248
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 std::string sdp = BuildSdp(consumer, encoding);
2249
2250 // Write to web-accessible location: /home/fpp/media/config/
2251
3/6
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 41 times.
✗ Branch 5 not taken.
41 std::string sdpPath = FPP_DIR_MEDIA("/config/rtp_" + consumer.pipeWireNodeName + ".sdp");
2252
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 std::ofstream ofs(sdpPath);
2253
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 if (ofs.is_open()) {
2254
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 ofs << sdp;
2255
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 ofs.close();
2256
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 LogInfo(VB_MEDIAOUT, "VideoOutputManager: Wrote SDP file %s\n", sdpPath.c_str());
2257 } else {
2258 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager: Failed to write SDP file %s\n", sdpPath.c_str());
2259 }
2260 41 }
2261
2262 // ──────────────────────────────────────────────────────────────────────────────
2263 // SAP (Session Announcement Protocol) — RFC 2974
2264 // ──────────────────────────────────────────────────────────────────────────────
2265
2266 26 static std::string GetLocalIPAddress() {
2267 // Return the first non-loopback IPv4 address
2268 26 struct ifaddrs* ifas = nullptr;
2269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (getifaddrs(&ifas) != 0)
2270 ✗ return "0.0.0.0";
2271
2272
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 std::string result = "0.0.0.0";
2273
1/2
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
104 for (struct ifaddrs* ifa = ifas; ifa; ifa = ifa->ifa_next) {
2274
3/4
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
✓ Branch 3 taken 52 times.
104 if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET)
2275 78 continue;
2276 52 auto* sin = (struct sockaddr_in*)ifa->ifa_addr;
2277 52 uint32_t ip = ntohl(sin->sin_addr.s_addr);
2278
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
52 if ((ip >> 24) == 127)
2279 26 continue;
2280 char buf[INET_ADDRSTRLEN];
2281 26 inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf));
2282
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 result = buf;
2283 26 break;
2284 }
2285 26 freeifaddrs(ifas);
2286 26 return result;
2287 26 }
2288
2289 52 std::vector<uint8_t> VideoOutputManager::BuildSAPPacket(const std::string& sourceIP,
2290 uint16_t msgIdHash,
2291 const std::string& sdp,
2292 bool isDeletion) {
2293 // RFC 2974 SAP packet:
2294 // Byte 0: V=1 (bits 7-5), A=0 (bit 4), R=0 (bit 3), T=isDeletion (bit 2),
2295 // E=0 (bit 1), C=0 (bit 0)
2296 // Byte 1: Auth length = 0
2297 // Bytes 2-3: Message ID hash (network byte order)
2298 // Bytes 4-7: Originating source (IPv4)
2299 // Payload type: "application/sdp\0"
2300 // SDP data
2301 52 uint8_t header0 = (SAP_VERSION << 5); // V=1
2302
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
52 if (isDeletion) {
2303 26 header0 |= 0x04; // T=1 (deletion)
2304 }
2305
2306 struct in_addr srcAddr;
2307 52 inet_pton(AF_INET, sourceIP.c_str(), &srcAddr);
2308
2309 52 const uint8_t* ipBytes = reinterpret_cast<const uint8_t*>(&srcAddr.s_addr);
2310 52 const uint8_t header[] = {
2311 header0,
2312 0, // auth length
2313 52 static_cast<uint8_t>((msgIdHash >> 8) & 0xFF),
2314 static_cast<uint8_t>(msgIdHash & 0xFF),
2315 52 ipBytes[0], ipBytes[1], ipBytes[2], ipBytes[3]
2316 52 };
2317 52 const char payloadType[] = "application/sdp"; // sizeof includes NUL terminator
2318
2319 52 std::vector<uint8_t> packet;
2320
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 packet.reserve(sizeof(header) + sizeof(payloadType) + sdp.size());
2321
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 packet.insert(packet.end(), header, header + sizeof(header));
2322
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 packet.insert(packet.end(), payloadType, payloadType + sizeof(payloadType));
2323
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 packet.insert(packet.end(), sdp.begin(), sdp.end());
2324
2325 52 return packet;
2326 ✗ }
2327
2328 135 bool VideoOutputManager::HasRunningRtpLocked() const {
2329
2/2
✓ Branch 0 taken 349 times.
✓ Branch 1 taken 109 times.
458 for (const auto& c : m_consumers) {
2330
9/10
✓ Branch 0 taken 349 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 305 times.
✓ Branch 4 taken 39 times.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 26 times.
✓ Branch 7 taken 13 times.
✓ Branch 8 taken 26 times.
✓ Branch 9 taken 323 times.
349 if (c.type == "rtp" && c.running && !c.parked)
2331 26 return true;
2332 }
2333 109 return false;
2334 }
2335
2336 26 void VideoOutputManager::StartSAPAnnouncer() {
2337 // Serialize with StopSAPAnnouncer() and with any other Start. The flag on
2338 // its own was a test-then-set across two unsynchronized threads.
2339 // The caller decides, under m_mutex, whether any RTP consumer is running
2340 // (HasRunningRtpLocked()). Walking m_consumers here under only m_sapLock
2341 // raced Reload() clearing and refilling the vector.
2342
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 std::lock_guard<std::mutex> lock(m_sapLock);
2343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (m_sapRunning.load())
2344 ✗ return;
2345
2346 // Never assign over a joinable thread -- that is an unconditional
2347 // std::terminate(). Stop() below always leaves this non-joinable, so this
2348 // only fires if some future path exits the loop another way; joining a
2349 // finished thread is cheap and keeps the invariant local to the assignment.
2350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (m_sapThread.joinable())
2351 ✗ m_sapThread.join();
2352
2353 26 m_sapRunning = true;
2354
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 m_sapThread = std::thread(&VideoOutputManager::SAPAnnounceLoop, this);
2355
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 }
2356
2357 2004 void VideoOutputManager::StopSAPAnnouncer() {
2358
1/2
✓ Branch 0 taken 2004 times.
✗ Branch 1 not taken.
2004 std::lock_guard<std::mutex> lock(m_sapLock);
2359 // No early-out on the flag. It used to return without joining when the
2360 // flag read false, which is exactly the state a Start/Stop race leaves
2361 // behind: Stop clears the flag and finds the thread not yet assigned, Start
2362 // then assigns it, and the object stays joinable with the loop already
2363 // exiting -- so the *next* Start assigned over a joinable thread and
2364 // terminated. Clearing and joining unconditionally is idempotent and
2365 // leaves m_sapThread non-joinable on every path.
2366 2004 m_sapRunning = false;
2367
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1978 times.
2004 if (m_sapThread.joinable())
2368
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 m_sapThread.join();
2369 2004 }
2370
2371 26 void VideoOutputManager::SAPAnnounceLoop() {
2372
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 LogInfo(VB_MEDIAOUT, "VideoOutputManager SAP announcer thread started\n");
2373
2374 26 int sock = OpenSocket(AF_INET, SOCK_DGRAM, 0);
2375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (sock < 0) {
2376 ✗ LogErr(VB_MEDIAOUT, "VideoOutputManager SAP: Failed to create socket: %s\n", FPPstrerror(errno));
2377 ✗ return;
2378 }
2379
2380 26 int ttl = SAP_TTL;
2381 26 setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
2382
2383 struct sockaddr_in sapAddr;
2384 26 memset(&sapAddr, 0, sizeof(sapAddr));
2385 26 sapAddr.sin_family = AF_INET;
2386 26 sapAddr.sin_port = htons(SAP_PORT);
2387 26 inet_pton(AF_INET, SAP_MCAST_ADDR, &sapAddr.sin_addr);
2388
2389
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 std::string localIP = GetLocalIPAddress();
2390
2391 // Build announce & delete packets for each running RTP consumer
2392 struct SAPEntry {
2393 std::vector<uint8_t> announcePacket;
2394 std::vector<uint8_t> deletePacket;
2395 };
2396 26 std::vector<SAPEntry> entries;
2397
2398 {
2399
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 std::lock_guard<std::mutex> lock(m_mutex);
2400
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
52 for (const auto& consumer : m_consumers) {
2401
4/8
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 26 times.
26 if (consumer.type != "rtp" || !consumer.running)
2402 ✗ continue;
2403
2404
3/10
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 26 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
26 std::string enc = consumer.rtpEncoding.empty() ? "raw" : consumer.rtpEncoding;
2405
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 std::string sdp = BuildSdp(consumer, enc);
2406
2407 // Simple hash from consumer id + port
2408 26 uint16_t hash = static_cast<uint16_t>(consumer.id * 31 + consumer.port);
2409
2410 26 SAPEntry entry;
2411
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 entry.announcePacket = BuildSAPPacket(localIP, hash, sdp, false);
2412
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 entry.deletePacket = BuildSAPPacket(localIP, hash, sdp, true);
2413
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 entries.push_back(std::move(entry));
2414 26 }
2415 26 }
2416
2417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (entries.empty()) {
2418 ✗ LogWarn(VB_MEDIAOUT, "VideoOutputManager SAP: No RTP consumers to announce\n");
2419 } else {
2420
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 LogInfo(VB_MEDIAOUT, "VideoOutputManager SAP: Announcing %d stream(s) to %s:%d every %ds\n",
2421 (int)entries.size(), SAP_MCAST_ADDR, SAP_PORT, SAP_INTERVAL_S);
2422 }
2423
2424
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 26 times.
40 while (m_sapRunning.load()) {
2425
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
28 for (const auto& entry : entries) {
2426
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ssize_t sent = sendto(sock, entry.announcePacket.data(), entry.announcePacket.size(), 0,
2427 (struct sockaddr*)&sapAddr, sizeof(sapAddr));
2428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (sent < 0) {
2429 ✗ LogErr(VB_MEDIAOUT, "VideoOutputManager SAP: sendto failed: %s\n", FPPstrerror(errno));
2430 }
2431 }
2432
2433 // Sleep for SAP_INTERVAL_S, checking shutdown flag every second
2434
5/6
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 15 times.
✓ Branch 5 taken 14 times.
29 for (int i = 0; i < SAP_INTERVAL_S && m_sapRunning.load(); i++) {
2435
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 std::this_thread::sleep_for(std::chrono::seconds(1));
2436 }
2437 }
2438
2439 // Send deletion packets on shutdown
2440
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
52 for (const auto& entry : entries) {
2441
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 sendto(sock, entry.deletePacket.data(), entry.deletePacket.size(), 0,
2442 (struct sockaddr*)&sapAddr, sizeof(sapAddr));
2443 }
2444
2445
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 close(sock);
2446
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 LogInfo(VB_MEDIAOUT, "VideoOutputManager SAP announcer thread stopped (deletion packets sent)\n");
2447 26 }
2448