GCC Code Coverage Report


Directory: ./
File: mediaoutput/VideoOutputManager.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 1115 1318 84.6%
Functions: 63 73 86.3%
Branches: 1282 2493 51.4%

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