GCC Code Coverage Report


Directory: ./
File: mediaoutput/GStreamerOut.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 2933 3462 84.7%
Functions: 130 135 96.3%
Branches: 3466 6622 52.3%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2025 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12
13 #include "fpp-pch.h"
14
15 #include "fpp-json.h"
16
17 #include "Warnings.h" // WarningHolder -- needed directly for NOPCH builds
18 #include <cmath>
19
20 #include "GStreamerOut.h"
21
22 #ifdef HAS_GSTREAMER
23
24 #include <gst/gst.h>
25 #include <gst/app/gstappsink.h>
26 #include <climits>
27 #include <condition_variable>
28 #include <cstdlib>
29 #include <cctype>
30 #include <cstring>
31 #include <dirent.h>
32 #include <fcntl.h>
33 #ifdef __GLIBC__
34 #include <malloc.h>
35 #endif
36 #include <algorithm>
37 #include <fstream>
38 #include <map>
39 #include <memory>
40 #include <mutex>
41 #include <unistd.h>
42 #include <vector>
43 #include <sstream>
44 #include <condition_variable>
45 #include <thread>
46 #include <sys/ioctl.h>
47 #include <sys/stat.h>
48 // DRM/KMS is Linux-only. Without libdrm (macOS) there is no kmssink to hand a
49 // DRM fd or plane to, so the helpers below report "none" and every caller
50 // already falls back on that.
51 #ifdef PLATFORM_OSX
52 // No DRM/KMS on macOS: the display output is a window (MacOSApp.mm) that
53 // glimagesink renders into through GstVideoOverlay.
54 #include <gst/video/videooverlay.h>
55 #include "../MacOSApp.h"
56 #endif
57 #if __has_include(<xf86drm.h>)
58 #define FPP_GST_HAS_DRM
59 #include <libdrm/drm.h>
60 #include <libdrm/drm_mode.h>
61 #include <xf86drm.h>
62 #include <xf86drmMode.h>
63 #endif
64
65 #if defined(HAS_GST_ALLOCATORS) && __has_include(<gst/allocators/gstdmabuf.h>)
66 #include <gst/allocators/gstdmabuf.h>
67 #define FPP_KMS_BRANCH_ISOLATION 1
68 #endif
69
70 #include "common.h"
71 #include "log.h"
72 #include "PipeWireOutputStream.h"
73 #include "StreamSlotManager.h"
74
75 #include <array>
76 #include <functional>
77 #include "VideoDisplay.h"
78 #include "VideoOutputManager.h"
79 #include "RTSPOutputManager.h"
80 #include "mediadetails.h"
81 #include "mediaoutput.h"
82 #include "settings.h"
83 #include "Timers.h"
84 #include "channeloutput/channeloutputthread.h"
85 #include "../MultiSync.h"
86 #include "../FileMonitor.h"
87 #include "overlays/PixelOverlay.h"
88 #include "overlays/PixelOverlayModel.h"
89
90 // Static instance pointer for callbacks
91 GStreamerOutput* GStreamerOutput::m_currentInstance = nullptr;
92 std::mutex GStreamerOutput::m_overlayOutputsLock;
93 std::vector<GStreamerOutput*> GStreamerOutput::m_overlayOutputs;
94
95 // Raised once per run, so long enough to outlast a looping playlist's pass.
96 static constexpr int MEDIA_FAILURE_WARNING_SECONDS = 300;
97
98 // ──────────────────────────────────────────────────────────────────────────────
99 // Keeping kmssinks behind one video tee from using each other's framebuffers.
100 //
101 // With two kmssinks on the tee -- two outputs of a video group, or the primary
102 // display plus one -- each on its own DRM lease, kmssink shares framebuffers
103 // across them two ways, both in gst-plugins-bad:
104 //
105 // - A decoder that allocates from a kmssink's buffer pool (proposed back up
106 // through the tee) hands every sink that sink's KMS memories, and kmssink
107 // shows any KMS memory by its framebuffer id without checking which sink
108 // made it (gst_kms_sink_get_input_buffer()). The other sink scans out
109 // framebuffers that belong to the first one's DRM fd.
110 // - A DMABuf frame (a hardware decoder's) is imported as a framebuffer and
111 // cached on the upstream GstMemory under one qdata key ("kmsmem") shared by
112 // every kmssink in the process (gst_kms_allocator_cache()/get_cached(); its
113 // own source notes it "is not tee safe"). Each sink finds the other's
114 // framebuffer cached and uses it, or replaces it, which frees the
115 // framebuffer the other sink is scanning out.
116 //
117 // When the owning sink frees one of those framebuffers, the other sink's next
118 // drmModeSetPlane fails with ENOENT (and its branch is cut off), or its plane
119 // is left on a stale framebuffer and stops updating. Which side loses changes
120 // from run to run.
121 //
122 // So, only when two or more kmssinks share the tee, each branch
123 // - keeps its sink's pool to itself: the pools are taken out of the
124 // allocation query on the way back up, so the decoder allocates its own
125 // buffers. A software decoder's frames then reach each sink in system
126 // memory, and each sink copies them into framebuffers of its own; and
127 // - gets its own GstMemory per decoder DMABuf memory: a wrapper around a
128 // dup() of the same fd, which is still zero-copy. kmssink then caches on
129 // objects only it sees. The wrapper is kept as qdata on the decoder memory
130 // under a key unique to the branch, so it lives as long as the decoder's
131 // pooled memory (one import per pool buffer, as with a single sink) and is
132 // freed with it.
133 // A lone kmssink is left exactly as it was, zero-copy from its own pool.
134 16 static GstPadProbeReturn DropKmsBranchPools(GstPad*, GstPadProbeInfo* info, gpointer) {
135 16 GstQuery* query = GST_PAD_PROBE_INFO_QUERY(info);
136
3/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 10 times.
16 if (query && GST_QUERY_TYPE(query) == GST_QUERY_ALLOCATION) {
137
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 while (gst_query_get_n_allocation_pools(query) > 0) {
138 6 gst_query_remove_nth_allocation_pool(query, 0);
139 }
140 }
141 16 return GST_PAD_PROBE_OK;
142 }
143
144 #ifdef FPP_KMS_BRANCH_ISOLATION
145 ✗ static GstAllocator* KmsBranchDmabufAllocator() {
146 ✗ static GstAllocator* alloc = gst_dmabuf_allocator_new();
147 ✗ return alloc;
148 }
149
150 // The branch's wrapper for `mem`, made (or remade, if the decoder memory's
151 // region moved since) as needed. A new reference, or nullptr on failure.
152 ✗ static GstMemory* KmsBranchWrapper(GstMemory* mem, GQuark key) {
153 ✗ gsize offset = 0, maxsize = 0;
154 ✗ gsize size = gst_memory_get_sizes(mem, &offset, &maxsize);
155 ✗ GstMemory* w = (GstMemory*)gst_mini_object_get_qdata(GST_MINI_OBJECT_CAST(mem), key);
156 ✗ if (w && w->offset == offset && w->size == size && w->maxsize == maxsize) {
157 ✗ return gst_memory_ref(w);
158 }
159 ✗ GstAllocator* alloc = KmsBranchDmabufAllocator();
160 ✗ if (!alloc) {
161 ✗ return nullptr;
162 }
163 ✗ int fd = dup(gst_dmabuf_memory_get_fd(mem));
164 ✗ if (fd < 0) {
165 ✗ return nullptr;
166 }
167 ✗ w = gst_dmabuf_allocator_alloc(alloc, fd, maxsize);
168 ✗ if (!w) {
169 ✗ close(fd);
170 ✗ return nullptr;
171 }
172 ✗ if (offset != 0 || size != maxsize) {
173 ✗ gst_memory_resize(w, (gssize)offset, size);
174 }
175 // The decoder memory owns the wrapper (replacing, and so releasing, any
176 // stale one); buffers this branch has already sent keep their own refs.
177 ✗ gst_mini_object_set_qdata(GST_MINI_OBJECT_CAST(mem), key, w, (GDestroyNotify)gst_memory_unref);
178 ✗ return gst_memory_ref(w);
179 }
180
181 80 static GstPadProbeReturn IsolateKmsBranchProbe(GstPad*, GstPadProbeInfo* info, gpointer data) {
182 80 GstBuffer* in = GST_PAD_PROBE_INFO_BUFFER(info);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (!in) {
184 ✗ return GST_PAD_PROBE_OK;
185 }
186
1/2
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
80 guint n = gst_buffer_n_memory(in);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (n == 0) {
188 ✗ return GST_PAD_PROBE_OK;
189 }
190
1/2
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
80 for (guint i = 0; i < n; i++) {
191
3/6
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
80 if (!gst_is_dmabuf_memory(gst_buffer_peek_memory(in, i))) {
192 80 return GST_PAD_PROBE_OK;
193 }
194 }
195 ✗ GQuark key = (GQuark)GPOINTER_TO_UINT(data);
196 ✗ GstBuffer* out = gst_buffer_new();
197 ✗ for (guint i = 0; i < n; i++) {
198 ✗ GstMemory* w = KmsBranchWrapper(gst_buffer_peek_memory(in, i), key);
199 ✗ if (!w) {
200 // Pass the shared memory on rather than drop the frame.
201 ✗ gst_buffer_unref(out);
202 ✗ return GST_PAD_PROBE_OK;
203 }
204 ✗ gst_buffer_append_memory(out, w);
205 }
206 ✗ gst_buffer_copy_into(out, in, (GstBufferCopyFlags)(GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_META), 0, -1);
207 // Without COPY_MEMORY, copy_into leaves out the metas tagged as describing
208 // the memory -- GstVideoMeta's plane offsets and strides among them, which
209 // kmssink needs. The memories are equivalent, so copy those through each
210 // meta's own transform.
211 ✗ GQuark memoryTag = g_quark_from_static_string(GST_META_TAG_MEMORY_STR);
212 ✗ gpointer state = nullptr;
213 ✗ while (GstMeta* meta = gst_buffer_iterate_meta(in, &state)) {
214 ✗ if (meta->info->transform_func && gst_meta_api_type_has_tag(meta->info->api, memoryTag)) {
215 ✗ GstMetaTransformCopy copy = { FALSE, 0, (gsize)-1 };
216 ✗ meta->info->transform_func(out, meta, in, _gst_meta_transform_copy, &copy);
217 }
218 ✗ }
219 // Keeps the decoder's buffer (and its slot in the decoder's pool) busy
220 // until the sink is done showing the frame, as passing it through did.
221 ✗ gst_buffer_add_parent_buffer_meta(out, in);
222 ✗ gst_buffer_unref(in);
223 ✗ GST_PAD_PROBE_INFO_DATA(info) = out;
224 ✗ return GST_PAD_PROBE_OK;
225 }
226 #endif
227
228 // Isolate the kmssink branches on one tee from each other, if there is more
229 // than one. Each entry is a branch's first element after the tee (its queue)
230 // and a name unique to the branch.
231 50 static void IsolateKmsBranches(const std::vector<std::pair<GstElement*, std::string>>& branches) {
232
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 1 times.
50 if (branches.size() < 2) {
233 49 return;
234 }
235
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (const auto& [queue, branch] : branches) {
236
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 GstPad* src = gst_element_get_static_pad(queue, "src");
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!src) {
238 ✗ continue;
239 }
240 // PULL: the query on its way back, answered by the branch's sink.
241
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_pad_add_probe(src, (GstPadProbeType)(GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM | GST_PAD_PROBE_TYPE_PULL),
242 DropKmsBranchPools, nullptr, nullptr);
243 #ifdef FPP_KMS_BRANCH_ISOLATION
244
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 GQuark key = g_quark_from_string(("fpp-kms-branch-" + branch).c_str());
245
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_pad_add_probe(src, GST_PAD_PROBE_TYPE_BUFFER, IsolateKmsBranchProbe, GUINT_TO_POINTER(key), nullptr);
246 #endif
247
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_object_unref(src);
248 }
249 1 LogDebug(VB_MEDIAOUT, "GStreamer: %zu kmssink branches on the video tee, isolated from each other\n",
250 branches.size());
251 }
252
253 // ──────────────────────────────────────────────────────────────────────────────
254 // Wedged-decoder recovery (issue #2695).
255 //
256 // The Pi's V4L2 hardware decoder can wedge after ~24h of continuous use even
257 // with CMA healthy (confirmed on the 2026-07-03 soak: preroll failure with
258 // 402MB CmaFree). Once wedged, two things go wrong in-process:
259 // 1. gst_element_set_state(NULL) on the wedged pipeline blocks FOREVER —
260 // the soak's playlist thread hung 2h25m in that call and then died with
261 // SIGBUS. Teardown is therefore supervised: the state change runs on a
262 // reaper thread with a deadline, and on timeout the pipeline is
263 // deliberately ABANDONED (leaked, ~40 fds + threads) so the playlist
264 // thread survives. One leaked pipeline is recoverable; a hung playlist
265 // thread is not.
266 // 2. The decoder stays wedged, so subsequent videos fail preroll too.
267 // Consecutive wedge events (preroll-watchdog fires, abandoned
268 // teardowns) are counted here; at the limit fppd restarts itself via
269 // RestartFPPDResumingPlaylist() — a fresh process re-opens the V4L2
270 // device (and picks the show back up where it left off), which
271 // bounds the abandoned-pipeline fd leakage and recovers the decoder in
272 // the cases we can recover. The count resets the moment any pipeline
273 // reports a valid playback position (decoder demonstrably healthy).
274 // ──────────────────────────────────────────────────────────────────────────────
275 static constexpr int PIPELINE_TEARDOWN_TIMEOUT_MS = 10000;
276
277 // Gain the File Manager's Normalize stored for a song in the music folder or a
278 // video's sound (<config>/media_gain.json, written by
279 // www/common/audioNormalize.inc.php): { "<name relative to the music dir>" or
280 // "videos/<name relative to the videos dir>": { "gainDb": -3.2, "size": 1234, ... } }.
281 // Applied at playback instead of rewriting the file, so timing, quality and
282 // format never change. An entry whose size no longer matches the file (the
283 // song was replaced) is ignored. Returns a linear factor; 1.0 = none.
284 //
285 // The file is parsed once and again whenever FileMonitor says it changed
286 // (inotify, or a timestamp poll where there is none; the PHP side replaces it
287 // by rename, which FileMonitor follows), not at every song start. Slots start
288 // on different threads, so the parsed copy is shared under a lock and handed
289 // out as a snapshot.
290 static std::mutex s_normalizeGainsLock;
291 static std::shared_ptr<const Json::Value> s_normalizeGains;
292
293 257 static void LoadNormalizeGains(const std::string& gainFile) {
294 257 std::shared_ptr<Json::Value> root;
295
3/4
✓ Branch 0 taken 257 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 252 times.
257 if (FileExists(gainFile)) {
296
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 root = std::make_shared<Json::Value>();
297
5/10
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 5 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 5 times.
5 if (!LoadJsonFromFile(gainFile, *root) || !root->isObject()) {
298 // An empty one restored from a backup is "[]": PHP's json_encode
299 // of an empty array.
300 ✗ if (!(root->isArray() && root->empty())) {
301 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: %s is not a JSON object; normalize gains not applied\n", gainFile.c_str());
302 }
303 ✗ root.reset();
304 }
305 }
306
1/2
✓ Branch 0 taken 257 times.
✗ Branch 1 not taken.
257 std::unique_lock<std::mutex> l(s_normalizeGainsLock);
307 257 s_normalizeGains = root;
308 257 }
309
310 2181 static std::shared_ptr<const Json::Value> NormalizeGains() {
311 static std::once_flag watching;
312
1/2
✓ Branch 0 taken 2181 times.
✗ Branch 1 not taken.
2435 std::call_once(watching, []() {
313
3/6
✓ Branch 0 taken 254 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 254 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 254 times.
✗ Branch 5 not taken.
283 const std::string gainFile = FPP_DIR_CONFIG("/media_gain.json");
314
4/8
✓ Branch 0 taken 254 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 254 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 254 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 254 times.
✗ Branch 7 not taken.
704 FileMonitor::INSTANCE.AddFile("GStreamer:media_gain", gainFile, [gainFile]() {
315 3 LoadNormalizeGains(gainFile);
316 3 });
317
1/2
✓ Branch 0 taken 254 times.
✗ Branch 1 not taken.
254 LoadNormalizeGains(gainFile);
318 254 });
319
1/2
✓ Branch 0 taken 2181 times.
✗ Branch 1 not taken.
2181 std::unique_lock<std::mutex> l(s_normalizeGainsLock);
320 4362 return s_normalizeGains;
321 2181 }
322
323 2181 static double LookupNormalizeGain(const std::string& fullPath) {
324
3/6
✓ Branch 0 taken 2181 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2181 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2181 times.
✗ Branch 5 not taken.
4362 const std::string musicDir = FPP_DIR_MUSIC("/");
325
3/6
✓ Branch 0 taken 2181 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2181 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2181 times.
✗ Branch 5 not taken.
2181 const std::string videoDir = FPP_DIR_VIDEO("/");
326 2181 std::string name;
327
3/4
✓ Branch 0 taken 2181 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2047 times.
✓ Branch 3 taken 134 times.
2181 if (fullPath.compare(0, musicDir.size(), musicDir) == 0) {
328
1/2
✓ Branch 0 taken 2047 times.
✗ Branch 1 not taken.
2047 name = fullPath.substr(musicDir.size());
329
2/4
✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 134 times.
✗ Branch 3 not taken.
134 } else if (fullPath.compare(0, videoDir.size(), videoDir) == 0) {
330
2/4
✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 134 times.
✗ Branch 3 not taken.
134 name = "videos/" + fullPath.substr(videoDir.size());
331 } else {
332 ✗ return 1.0;
333 }
334
1/2
✓ Branch 0 taken 2181 times.
✗ Branch 1 not taken.
2181 std::shared_ptr<const Json::Value> root = NormalizeGains();
335
2/2
✓ Branch 0 taken 2171 times.
✓ Branch 1 taken 10 times.
2181 if (!root) {
336 2171 return 1.0;
337 }
338
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 const Json::Value& e = (*root)[name];
339
11/18
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 8 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 8 times.
✓ Branch 16 taken 2 times.
✓ Branch 17 taken 8 times.
10 if (!e.isObject() || !e["gainDb"].isNumeric() || !e["size"].isNumeric()) {
340 2 return 1.0;
341 }
342 struct stat st;
343
7/10
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 7 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 7 times.
8 if (stat(fullPath.c_str(), &st) != 0 || (Json::Int64)st.st_size != e["size"].asInt64()) {
344
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogDebug(VB_MEDIAOUT, "GStreamer: normalize gain for %s ignored (file changed since it was measured)\n", name.c_str());
345 1 return 1.0;
346 }
347 // +20 dB = 10.0, the volume element's maximum.
348
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 double db = std::clamp(e["gainDb"].asDouble(), -30.0, 20.0);
349
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 LogDebug(VB_MEDIAOUT, "GStreamer: normalize gain for %s: %+.1f dB\n", name.c_str(), db);
350 7 return pow(10.0, db / 20.0);
351 2181 }
352 // Longest a track waits for its slot's PipeWire output stream to reach the
353 // card. A cold open is ~0.6 s on an AM335x.
354 static constexpr int PW_STREAM_READY_TIMEOUT_MS = 3000;
355 // Longest a track waits for the previous one's leftovers to play out of the
356 // stream (normally ~100 ms: the feed's lead plus the tail-flush silence).
357 static constexpr int PW_STREAM_DRAIN_MAX_MS = 500;
358 // How long a track that would carry on the output stream's timeline waits
359 // for its pipeline to preroll first. A prepared pipeline already has; one
360 // built as its entry started takes tens of ms, bridged meanwhile with silence.
361 static constexpr int PW_STREAM_PREROLL_WAIT_MS = 2000;
362 static std::atomic<int> s_consecutiveWedgeEvents{ 0 };
363 // Ensures the escalation decision (restart vs reboot) is made exactly once
364 // per process — additional wedge events racing in while shutdown is already
365 // underway must not re-record restarts or re-trigger anything.
366 static std::atomic<bool> s_wedgeShutdownTriggered{ false };
367
368 // Wedge-triggered restart history, kept in tmpfs: survives fppd restarts but
369 // is cleared by a reboot — exactly the lifetime of the VideoCore firmware
370 // state we're trying to recover. On platforms without /run/fppd (macOS,
371 // Docker) the file is unusable and the count stays at 1, which keeps the
372 // reboot rung disengaged there by construction.
373 static constexpr const char* WEDGE_RESTART_STATE_FILE = "/run/fppd/gstreamer_wedge_restarts";
374 static constexpr long long WEDGE_RESTART_WINDOW_MS = 2LL * 60 * 60 * 1000; // 2 hours
375
376 // Record that a wedge-triggered fppd restart is happening now and return how
377 // many such restarts (including this one) occurred within the window.
378 2 static int RecordWedgeRestartAndCount() {
379
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 long long now = GetTimeMS();
380 2 std::vector<long long> stamps;
381 {
382
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::ifstream in(WEDGE_RESTART_STATE_FILE);
383 2 std::string line;
384
3/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
2 while (in && std::getline(in, line)) {
385 ✗ long long v = atoll(line.c_str());
386 ✗ if (v > 0 && v <= now && (now - v) < WEDGE_RESTART_WINDOW_MS) {
387 ✗ stamps.push_back(v);
388 }
389 }
390 2 }
391
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 stamps.push_back(now);
392
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::ofstream out(WEDGE_RESTART_STATE_FILE, std::ios::trunc);
393
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!out) {
394 ✗ return 1;
395 }
396
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (long long s : stamps) {
397
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 out << s << "\n";
398 }
399 2 return (int)stamps.size();
400 2 }
401
402 // Set on a decodebin that chose a hardware video decoder (OnAutoplugSelect).
403 static constexpr const char* HW_DECODER_CHOSEN_KEY = "fpp-hw-decoder-chosen";
404
405 // Whether `pipeline` holds a hardware video decoder (element class
406 // "Codec/Decoder/Video/Hardware" -- the V4L2 stateful and stateless decoders),
407 // or its decodebin chose one that never got made. decodebin chooses from the
408 // registry, and making the element loads its plugin, whose init opens every
409 // V4L2 device to register the decoders. A decoder driver that has died (a
410 // bcm2835-codec oops leaves its mutex held) blocks that open() for good, and
411 // every later load waits behind it, so no decoder shows up in the pipeline.
412 // A pipeline that chose one and holds no video decoder at all is stuck there;
413 // one that fell back to a software decoder is not.
414 2105 static bool PipelineHasHwVideoDecoder(GstElement* pipeline) {
415
8/16
✓ Branch 0 taken 2105 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2105 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2105 times.
✓ Branch 6 taken 2105 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 2105 times.
✓ Branch 10 taken 2105 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 2105 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 2105 times.
2105 if (!pipeline || !GST_IS_BIN(pipeline)) {
416 ✗ return false;
417 }
418 2105 bool found = false;
419 2105 bool chosen = false;
420 2105 bool videoDecoder = false;
421
3/6
✓ Branch 0 taken 2105 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2105 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2105 times.
✗ Branch 5 not taken.
2105 GstIterator* it = gst_bin_iterate_recurse(GST_BIN(pipeline));
422 2105 GValue item = G_VALUE_INIT;
423 2105 bool done = false;
424
3/4
✓ Branch 0 taken 34316 times.
✓ Branch 1 taken 2105 times.
✓ Branch 2 taken 34316 times.
✗ Branch 3 not taken.
36421 while (!done && !found) {
425
3/5
✓ Branch 0 taken 34316 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32211 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2105 times.
34316 switch (gst_iterator_next(it, &item)) {
426 32211 case GST_ITERATOR_OK: {
427
3/6
✓ Branch 0 taken 32211 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32211 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32211 times.
✗ Branch 5 not taken.
32211 GstElement* element = GST_ELEMENT(g_value_get_object(&item));
428
1/2
✓ Branch 0 taken 32211 times.
✗ Branch 1 not taken.
32211 GstElementFactory* f = gst_element_get_factory(element);
429
2/4
✓ Branch 0 taken 32211 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32211 times.
✗ Branch 3 not taken.
32211 const gchar* klass = f ? gst_element_factory_get_metadata(f, GST_ELEMENT_METADATA_KLASS) : nullptr;
430
5/6
✓ Branch 0 taken 32211 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2292 times.
✓ Branch 3 taken 29919 times.
✓ Branch 4 taken 92 times.
✓ Branch 5 taken 2200 times.
32211 if (klass && strstr(klass, "Decoder") && strstr(klass, "Video")) {
431 92 videoDecoder = true;
432 92 found = strstr(klass, "Hardware") != nullptr;
433 }
434
4/6
✓ Branch 0 taken 32211 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32211 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 32208 times.
32211 if (g_object_get_data(G_OBJECT(element), HW_DECODER_CHOSEN_KEY)) {
435 3 chosen = true;
436 }
437
1/2
✓ Branch 0 taken 32211 times.
✗ Branch 1 not taken.
32211 g_value_reset(&item);
438 32211 break;
439 }
440 ✗ case GST_ITERATOR_RESYNC:
441 ✗ gst_iterator_resync(it);
442 ✗ break;
443 2105 default:
444 2105 done = true;
445 2105 break;
446 }
447 }
448
1/2
✓ Branch 0 taken 2105 times.
✗ Branch 1 not taken.
2105 g_value_unset(&item);
449
1/2
✓ Branch 0 taken 2105 times.
✗ Branch 1 not taken.
2105 gst_iterator_free(it);
450
4/6
✓ Branch 0 taken 2105 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2102 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
2105 return found || (chosen && !videoDecoder);
451 }
452
453 // The restart and reboot rungs below exist for one failure: a hardware video
454 // decoder (V4L2 / VideoCore firmware) that has wedged and only a process
455 // restart or a reboot recovers. A pipeline without one that times out --
456 // an audio-only track whose pipewiresink stalled in teardown, a stream that
457 // never prerolled on a BeagleBone -- has nothing a reboot fixes, and counting
458 // it restarted fppd and rebooted show controllers that have no such decoder.
459 // Those are logged and left at that.
460 // The UI warning for a pipeline that never got to PLAYING with no hardware
461 // decoder to blame: in practice a PipeWire daemon restarted under fppd.
462 // Raised by whichever of the start thread and Process()'s preroll watchdog
463 // sees it first; the two are the same warning, so it shows once.
464 static const char* const kMediaDidNotStartWarning =
465 "Media playback did not start (audio backend connection lost — restart FPPD)";
466
467 18 static void RecordWedgeEventAndMaybeRestart(const char* what, bool hwDecoder) {
468
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
18 if (!hwDecoder) {
469 15 LogErr(VB_MEDIAOUT, "GStreamer: %s (no hardware decoder involved, not counted toward a restart)\n", what);
470 15 return;
471 }
472 3 int fails = s_consecutiveWedgeEvents.fetch_add(1) + 1;
473 // 0 disables the self-restart escalation (teardown abandonment still applies)
474 3 int limit = getSettingInt("GStreamerWedgeRestartLimit", 3);
475 3 LogErr(VB_MEDIAOUT, "GStreamer: %s (consecutive decoder-wedge events: %d, restart limit: %d)\n",
476 what, fails, limit);
477
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (limit <= 0 || fails < limit) {
478 ✗ return;
479 }
480
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (s_wedgeShutdownTriggered.exchange(true)) {
481 1 return; // restart/reboot already in flight
482 }
483
484 // A process restart re-opens the V4L2 device, which recovers the decoder
485 // when the wedge is driver-side. But the dmesg evidence from issue #2727
486 // shows the wedge can live in the VideoCore FIRMWARE (VCHIQ stops
487 // answering, "invalid message context") — no userspace restart resets
488 // that; only a reboot reinitializes the firmware. So: restart first,
489 // and if wedge-triggered restarts keep happening within the window, the
490 // restart rung clearly isn't working — escalate to a system reboot.
491 // GStreamerWedgeRebootLimit is the number of wedge-triggered restarts
492 // within 2 hours that triggers the reboot (default 2: one restart gets a
493 // chance to fix it, the second escalates). 0 disables the reboot rung.
494 2 int rebootLimit = getSettingInt("GStreamerWedgeRebootLimit", 2);
495 2 int recentRestarts = RecordWedgeRestartAndCount();
496
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 if (rebootLimit > 0 && recentRestarts >= rebootLimit) {
497 ✗ LogErr(VB_MEDIAOUT, "GStreamer: %d wedge-triggered fppd restarts within %d minutes did not recover the decoder — "
498 "VideoCore firmware is wedged (issues #2695/#2727), REBOOTING the system\n",
499 recentRestarts, (int)(WEDGE_RESTART_WINDOW_MS / 60000));
500 // Permanent: the reboot that follows clears it.
501 ✗ WarningHolder::AddWarning(37, "Rebooting: hardware video decoder firmware wedged; fppd restart did not recover it");
502 ✗ sync();
503 ✗ if (system("/usr/sbin/reboot") != 0) {
504 // Reboot unavailable (container, dev box) — fall back to the
505 // restart rung rather than doing nothing.
506 ✗ LogErr(VB_MEDIAOUT, "GStreamer: reboot command failed, falling back to fppd restart\n");
507 ✗ RestartFPPDResumingPlaylist();
508 ✗ return;
509 }
510 // Exit cleanly while the system goes down.
511 ✗ ShutdownFPPD(false);
512 ✗ return;
513 }
514
515 2 LogErr(VB_MEDIAOUT, "GStreamer: %d consecutive pipeline wedges — hardware decoder is not recoverable in-process, restarting fppd (restart %d/%d before reboot escalation)\n",
516 fails, recentRestarts, rebootLimit);
517
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
6 WarningHolder::AddWarningTimeout(300, 37, "Restarting fppd: hardware media decoder wedged (repeated pipeline hangs, issue #2695)");
518 // Resume whatever was playing: the restart exists to keep the show (and
519 // the remotes syncing to it) alive, not just to reset the decoder.
520 2 RestartFPPDResumingPlaylist();
521 }
522
523 // Reaper threads still inside gst_element_set_state(NULL): the teardowns in
524 // progress, plus every abandoned one that has not let go.
525 static std::atomic<int> s_liveReapers{ 0 };
526 // How many of those may be stuck before fppd restarts. Each abandoned
527 // teardown keeps its pipeline, its streaming threads and its reaper for good,
528 // and the decoder-wedge ladder above does not bound that: a pipeline without
529 // a hardware decoder is never counted, and the count resets whenever a clip
530 // plays. A box whose every teardown wedged (a leaked DRM lease left each
531 // kmssink after it sharing the card's fd) abandoned 226 pipelines in 50
532 // minutes, until a 32-bit fppd could not create another thread and aborted.
533 // A teardown that stalls and then lets go leaves this count again, so only
534 // pipelines that are genuinely stuck add up.
535 static constexpr int STUCK_TEARDOWN_RESTART_LIMIT = 4;
536
537 // Restart fppd, resuming the playlist, because abandoned teardowns are piling
538 // up. Not a decoder recovery: it does not feed the reboot rung.
539 2 static void RestartForStuckTeardowns(const char* why) {
540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (getSettingInt("GStreamerWedgeRestartLimit", 3) <= 0) {
541 ✗ return; // self-restarts are turned off; the pipelines stay leaked
542 }
543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s_wedgeShutdownTriggered.exchange(true)) {
544 ✗ return; // restart/reboot already in flight
545 }
546 2 LogErr(VB_MEDIAOUT, "GStreamer: %s, restarting fppd\n", why);
547
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
6 WarningHolder::AddWarningTimeout(300, 37, "Restarting fppd: media pipeline teardowns stuck");
548 2 RestartFPPDResumingPlaylist();
549 }
550
551 // Drive a pipeline to GST_STATE_NULL on a reaper thread with a deadline.
552 // Returns true when the pipeline reached NULL in time. Returns false when
553 // the transition wedged: the reaper thread keeps its own ref and either
554 // finishes late (logged) or blocks forever, in which case the pipeline and
555 // its resources are intentionally leaked. After a false return the caller
556 // must NOT release the pipeline's kernel-side resources (DRM planes, shared
557 // DRM fd refs) — the wedged pipeline still owns them.
558 // The picture's delay behind its sound on each slot, less a track's carry-on
559 // shift (m_timelineShiftNs): what the stream, graph and card add, the same from
560 // one track to the next. A new track's picture starts with it (Go()), rather
561 // than with none until its first position, which put the picture ahead of the
562 // sound for the first frames and then held it that long.
563 static std::array<std::atomic<int64_t>, StreamSlotManager::MAX_SLOTS + 1> s_pictureDelayBaseNs{};
564 // On a MultiSync remote, how far each slot's next prepared track starts ahead
565 // of (> 0) or behind (< 0) the master's start packet, in ms: learnt from how
566 // far off the last such track's sound began (AdjustFeedSpeed()). The
567 // difference is the two boxes' output delays, the same from track to track.
568 static std::array<std::atomic<int>, StreamSlotManager::MAX_SLOTS + 1> s_remoteStartAdjustMs{};
569 static constexpr int kMaxRemoteStartAdjustMs = 250;
570
571 // Hand the memory a finished track freed back to the system, a few seconds
572 // after it closes. Each track starts streaming threads of its own, and on a
573 // 64-bit board glibc gives them arenas of up to 64 MB each, which keep what
574 // those threads freed for reuse: a remote looping a 15 s video grew from 128
575 // to 157 MB over five hours, all of it given back by a malloc_trim(). Done
576 // on a thread of its own once the next track is playing (each close pushes
577 // it back), so nothing on the way from one track to the next waits for it.
578 3943 static void TrimHeapSoon() {
579 #ifdef __GLIBC__
580 struct Trimmer {
581 std::mutex lock;
582 std::condition_variable cv;
583 uint64_t dueMs = 0;
584 };
585 // Never freed: its thread waits on it for the life of the process.
586 204 static Trimmer* trimmer = [] {
587
2/4
✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 204 times.
204 Trimmer* t = new Trimmer();
588 408 std::thread([t]() {
589
2/4
✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 204 times.
✗ Branch 3 not taken.
204 SetThreadName("FPP-HeapTrim");
590
1/2
✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
204 std::unique_lock<std::mutex> lock(t->lock);
591 while (true) {
592
1/2
✓ Branch 0 taken 3974 times.
✗ Branch 1 not taken.
8086 t->cv.wait(lock, [t] { return t->dueMs != 0; });
593
1/2
✓ Branch 0 taken 3974 times.
✗ Branch 1 not taken.
3974 uint64_t now = GetMonotonicTimeMS();
594
2/2
✓ Branch 0 taken 3871 times.
✓ Branch 1 taken 103 times.
3974 if (now < t->dueMs) {
595
1/2
✓ Branch 0 taken 3702 times.
✗ Branch 1 not taken.
3871 t->cv.wait_for(lock, std::chrono::milliseconds(t->dueMs - now));
596 3702 continue;
597 }
598 103 t->dueMs = 0;
599
1/2
✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
103 lock.unlock();
600 103 auto t0 = std::chrono::steady_clock::now();
601 103 malloc_trim(0);
602
2/4
✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 103 times.
✗ Branch 3 not taken.
103 double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
603
1/2
✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
103 LogDebug(VB_MEDIAOUT, "GStreamer: returned freed memory to the system in %.1f ms\n", ms);
604
1/2
✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
103 lock.lock();
605 3805 }
606
2/4
✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 204 times.
✗ Branch 3 not taken.
204 }).detach();
607 204 return t;
608
5/8
✓ Branch 0 taken 205 times.
✓ Branch 1 taken 3738 times.
✓ Branch 2 taken 204 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 204 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
3943 }();
609 {
610
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 std::lock_guard<std::mutex> lock(trimmer->lock);
611
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 trimmer->dueMs = GetMonotonicTimeMS() + 3000;
612 3943 }
613 3943 trimmer->cv.notify_one();
614 #endif
615 3943 }
616
617 // `ifLate` runs on the teardown thread if the pipeline does reach NULL after
618 // all, once the caller has given up on it (returned false).
619 3401 static bool SetPipelineToNullSupervised(GstElement* pipeline, int timeoutMs,
620 std::function<void()> ifLate = nullptr) {
621 struct TeardownCtx {
622 std::mutex m;
623 std::condition_variable cv;
624 bool done = false;
625 bool abandoned = false;
626 std::function<void()> ifLate;
627 };
628
1/2
✓ Branch 0 taken 3401 times.
✗ Branch 1 not taken.
3401 auto ctx = std::make_shared<TeardownCtx>();
629 3401 ctx->ifLate = std::move(ifLate);
630
1/2
✓ Branch 0 taken 3401 times.
✗ Branch 1 not taken.
3401 gst_object_ref(pipeline);
631 3401 s_liveReapers++;
632 try {
633
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
10203 std::thread([pipeline, ctx, timeoutMs]() {
634
2/4
✓ Branch 0 taken 3401 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3401 times.
✗ Branch 3 not taken.
3401 SetThreadName("FPP-GstReaper");
635
1/2
✓ Branch 0 taken 3401 times.
✗ Branch 1 not taken.
3401 uint64_t start = GetMonotonicTimeMS();
636
1/2
✓ Branch 0 taken 3387 times.
✗ Branch 1 not taken.
3401 gst_element_set_state(pipeline, GST_STATE_NULL);
637
1/2
✓ Branch 0 taken 3387 times.
✗ Branch 1 not taken.
3387 gst_object_unref(pipeline);
638 3387 s_liveReapers--;
639
1/2
✓ Branch 0 taken 3387 times.
✗ Branch 1 not taken.
3387 uint64_t took = GetMonotonicTimeMS() - start;
640
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3385 times.
3387 if (took > (uint64_t)timeoutMs) {
641 // The caller already gave up and abandoned this pipeline —
642 // record that the kernel/GStreamer eventually let go so a
643 // "late but not never" wedge is distinguishable in soak logs.
644
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogWarn(VB_MEDIAOUT, "GStreamer: abandoned pipeline teardown eventually completed after %llu ms\n",
645 (unsigned long long)took);
646 }
647 3387 std::function<void()> late;
648 {
649
1/2
✓ Branch 0 taken 3387 times.
✗ Branch 1 not taken.
3387 std::lock_guard<std::mutex> l(ctx->m);
650 3387 ctx->done = true;
651
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3385 times.
3387 if (ctx->abandoned) {
652 2 late = std::move(ctx->ifLate);
653 }
654 3387 }
655 3387 ctx->cv.notify_all();
656
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3385 times.
3387 if (late) {
657
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 late();
658 }
659
2/4
✓ Branch 0 taken 3401 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3401 times.
✗ Branch 3 not taken.
6788 }).detach();
660 ✗ } catch (const std::system_error& e) {
661 // Out of threads. The pipeline is still running, so its last unref
662 // must not happen here: keep the reaper's ref and abandon it like a
663 // wedged one.
664 ✗ s_liveReapers--;
665 ✗ LogErr(VB_MEDIAOUT, "GStreamer: could not start a teardown thread (%s), abandoning the pipeline\n", e.what());
666 ✗ RestartForStuckTeardowns("out of threads for pipeline teardowns");
667 ✗ return false;
668 ✗ }
669
670
1/2
✓ Branch 0 taken 3401 times.
✗ Branch 1 not taken.
3401 std::unique_lock<std::mutex> l(ctx->m);
671
3/4
✓ Branch 0 taken 3400 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3385 times.
✓ Branch 3 taken 15 times.
10202 if (ctx->cv.wait_for(l, std::chrono::milliseconds(timeoutMs), [&] { return ctx->done; })) {
672 3385 return true;
673 }
674 // Given up on: if it does get there later, it runs ifLate (decided under
675 // the lock, so a teardown finishing right now is one or the other).
676 15 ctx->abandoned = true;
677
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 l.unlock();
678 15 int stuck = s_liveReapers.load();
679
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 if (stuck >= STUCK_TEARDOWN_RESTART_LIMIT) {
680 char why[96];
681 2 snprintf(why, sizeof(why), "%d abandoned pipeline teardowns are still stuck", stuck);
682
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 RestartForStuckTeardowns(why);
683 }
684 15 return false;
685 3400 }
686
687 // Static audio sample buffer for WLED audio-reactive effects
688 std::array<float, GStreamerOutput::SAMPLE_BUFFER_SIZE> GStreamerOutput::s_sampleBuffer = {};
689 int GStreamerOutput::s_sampleWritePos = 0;
690 int GStreamerOutput::s_sampleRate = 0;
691 std::mutex GStreamerOutput::s_sampleMutex;
692 uint64_t GStreamerOutput::s_sampleLastWriteMs = 0;
693 std::atomic<uint64_t> GStreamerOutput::s_sampleTapReadMs{ 0 };
694
695 // One-time GStreamer initialization.
696 //
697 // Every entry point that touches the GStreamer API has to come through here
698 // first: gst_parse_launch() against an uninitialized library dereferences the
699 // (still null) value table and segfaults inside gst_value_deserialize() rather
700 // than reporting a GError. Media playback used to be the only such entry
701 // point, so a lazy init on the playback path was enough; the video input and
702 // output managers now build pipelines from a config reload with no media in
703 // sight, which on a box that had never played anything took fppd down.
704 //
705 // Guarded on gst_is_initialized() rather than a local flag so it stays correct
706 // alongside any other caller that reached gst_init() first, and serialized
707 // because the WLED overlay, the managers and the playback path can all arrive
708 // here from different threads.
709 static std::mutex s_gstInitMutex;
710
711 // Caller holds s_gstInitMutex.
712 5302 static void SetGStreamerEnvLocked() {
713 static bool done = false;
714
2/2
✓ Branch 0 taken 2651 times.
✓ Branch 1 taken 2651 times.
5302 if (done) {
715 2651 return;
716 }
717 2651 done = true;
718 // Set PipeWire env vars so pipewiresink can find the FPP PipeWire runtime.
719 // Both Simple PipeWire and PipeWire Advanced share the same runtime stack.
720
2/2
✓ Branch 0 taken 2644 times.
✓ Branch 1 taken 7 times.
2651 if (isPipeWireBackend()) {
721 2644 setenv("PIPEWIRE_RUNTIME_DIR", "/run/pipewire-fpp", 1);
722 2644 setenv("XDG_RUNTIME_DIR", "/run/pipewire-fpp", 1);
723 2644 setenv("PULSE_RUNTIME_PATH", "/run/pipewire-fpp/pulse", 1);
724 2644 LogInfo(VB_MEDIAOUT, "GStreamer: Set PipeWire env (PIPEWIRE_RUNTIME_DIR=/run/pipewire-fpp)\n");
725 } else {
726
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 std::string mediaBackend = getSetting("MediaBackend");
727
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 LogInfo(VB_MEDIAOUT, "GStreamer: MediaBackend='%s', not setting PipeWire env\n", mediaBackend.c_str());
728 7 }
729 }
730
731 // videoconvert's I420 kernels, as compiled by ORC 0.4.41 for arm64 (the version
732 // Raspberry Pi OS trixie ships), read two bytes past the end of the frame: the
733 // chroma load for the last pixels of each row takes 4 bytes where 2 remain.
734 // Inside a frame that lands in the next row, harmlessly. At the end of the
735 // last V row it lands past the buffer -- also harmless in system memory, which
736 // has slack after it, but a hardware decoder's frames are mmapped V4L2 buffers
737 // sized exactly to the image. When the image needs no alignment padding
738 // (1280x384, 640x192, ...) the frame ends on a page boundary and the over-read
739 // faults, killing fppd on the first frame of every play. Reproduced against
740 // the stock libraries with a frame placed before an inaccessible page; ORC's C
741 // backup code reads the same frame cleanly.
742 //
743 // So a frame that a videoconvert is going to convert is first copied into
744 // system memory with tail padding, which is the slack GStreamer allocates for
745 // exactly this kind of SIMD over-read. Frames already in system memory
746 // (software decoders) and converters running in passthrough (the HDMI
747 // branches, which hand the decoder's buffers to kmssink untouched) are left
748 // alone, so zero-copy playback keeps its zero copy.
749 6492 static GstPadProbeReturn CopyDeviceFrameForConvert(GstPad* pad, GstPadProbeInfo* info, gpointer) {
750 6492 GstBuffer* buf = GST_PAD_PROBE_INFO_BUFFER(info);
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6492 times.
6492 if (!buf) {
752 ✗ return GST_PAD_PROBE_OK;
753 }
754 6492 bool systemMemory = true;
755
3/4
✓ Branch 0 taken 6492 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6492 times.
✓ Branch 3 taken 6034 times.
12526 for (guint i = 0, n = gst_buffer_n_memory(buf); i < n; i++) {
756
4/6
✓ Branch 0 taken 6492 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6492 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 458 times.
✓ Branch 5 taken 6034 times.
6492 if (!gst_memory_is_type(gst_buffer_peek_memory(buf, i), GST_ALLOCATOR_SYSMEM)) {
757 458 systemMemory = false;
758 458 break;
759 }
760 }
761
2/2
✓ Branch 0 taken 6034 times.
✓ Branch 1 taken 458 times.
6492 if (systemMemory) {
762 6034 return GST_PAD_PROBE_OK;
763 }
764
765 // Passthrough: identical caps on both sides, so the frame is never read.
766
1/2
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
458 GstElement* convert = gst_pad_get_parent_element(pad);
767
1/2
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
458 if (convert) {
768 458 bool passthrough = false;
769
1/2
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
458 GstPad* src = gst_element_get_static_pad(convert, "src");
770
1/2
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
458 GstCaps* inCaps = gst_pad_get_current_caps(pad);
771
2/4
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 458 times.
✗ Branch 3 not taken.
458 GstCaps* outCaps = src ? gst_pad_get_current_caps(src) : nullptr;
772
2/4
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 458 times.
✗ Branch 3 not taken.
458 if (inCaps && outCaps) {
773
1/2
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
458 passthrough = gst_caps_is_equal(inCaps, outCaps);
774 }
775
1/2
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
458 if (inCaps) {
776
1/2
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
458 gst_caps_unref(inCaps);
777 }
778
1/2
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
458 if (outCaps) {
779
1/2
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
458 gst_caps_unref(outCaps);
780 }
781
1/2
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
458 if (src) {
782
1/2
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
458 gst_object_unref(src);
783 }
784
1/2
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
458 gst_object_unref(convert);
785
2/2
✓ Branch 0 taken 453 times.
✓ Branch 1 taken 5 times.
458 if (passthrough) {
786 453 return GST_PAD_PROBE_OK;
787 }
788 }
789
790
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 gsize size = gst_buffer_get_size(buf);
791 GstAllocationParams params;
792
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 gst_allocation_params_init(&params);
793 5 params.align = 15;
794 5 params.padding = 64;
795
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 GstBuffer* copy = gst_buffer_new_allocate(nullptr, size, &params);
796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!copy) {
797 ✗ return GST_PAD_PROBE_OK;
798 }
799 GstMapInfo map;
800
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 if (!gst_buffer_map(copy, &map, GST_MAP_WRITE)) {
801 ✗ gst_buffer_unref(copy);
802 ✗ return GST_PAD_PROBE_OK;
803 }
804
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 gst_buffer_extract(buf, 0, map.data, size);
805
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 gst_buffer_unmap(copy, &map);
806
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 gst_buffer_copy_into(copy, buf,
807 (GstBufferCopyFlags)(GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_META),
808 0, -1);
809 // ...except that gst_buffer_copy_into() leaves out every meta tagged
810 // "memory" unless the memory itself is copied too, and GstVideoMeta is
811 // one: it records where the planes sit. Its offsets are buffer-relative
812 // and the copy keeps every byte where it was, so it still holds for the
813 // copy -- and without it videoconvert assumes the caps' default layout.
814 // That default happens to match a plain linear frame, but not a padded
815 // or tiled one: the Pi 4 HEVC decoder's NV12_128C8 pads each 128-byte
816 // column to 1088 rows, and read as 1080 the picture shears and the
817 // chroma comes from the wrong place. So carry it over by hand, through
818 // the meta's own copy transform. Looked up by name because libfpp does
819 // not link gstreamer-video; if the type was never registered, no buffer
820 // can be carrying one.
821
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 if (GType videoMetaApi = g_type_from_name("GstVideoMetaAPI")) {
822 5 gpointer state = nullptr;
823
3/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 5 times.
10 while (GstMeta* meta = gst_buffer_iterate_meta_filtered(buf, &state, videoMetaApi)) {
824
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (meta->info->transform_func) {
825 5 GstMetaTransformCopy whole = { FALSE, 0, (gsize)-1 };
826
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 meta->info->transform_func(copy, meta, buf, _gst_meta_transform_copy, &whole);
827 }
828 5 }
829 }
830
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 gst_buffer_unref(buf);
831 5 GST_PAD_PROBE_INFO_DATA(info) = copy;
832 5 return GST_PAD_PROBE_OK;
833 }
834
835 // The media path goes to filesrc as a property, not spliced into the
836 // gst_parse_launch() description. Inside the description a '"' in the
837 // filename (uploads keep them) ended the quoted value early, so the file never
838 // played, and a crafted name could add elements to the pipeline.
839 2178 static void SetFileSourceLocation(GstElement* pipeline, const std::string& path) {
840
1/2
✓ Branch 0 taken 2178 times.
✗ Branch 1 not taken.
2178 if (GstElement* fsrc = gst_bin_get_by_name(GST_BIN(pipeline), "fsrc")) {
841 2178 g_object_set(fsrc, "location", path.c_str(), NULL);
842 2178 gst_object_unref(fsrc);
843 }
844 2178 }
845
846 3701 static void GuardVideoConvert(GstElement* element) {
847 3701 GstElementFactory* factory = gst_element_get_factory(element);
848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3701 times.
3701 if (!factory) {
849 ✗ return;
850 }
851 3701 const char* name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
852
4/4
✓ Branch 0 taken 3491 times.
✓ Branch 1 taken 210 times.
✓ Branch 2 taken 3480 times.
✓ Branch 3 taken 11 times.
3701 if (strcmp(name, "videoconvert") != 0 && strcmp(name, "videoconvertscale") != 0) {
853 3480 return;
854 }
855 221 GstPad* sink = gst_element_get_static_pad(element, "sink");
856
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 if (sink) {
857 221 gst_pad_add_probe(sink, GST_PAD_PROBE_TYPE_BUFFER, CopyDeviceFrameForConvert, nullptr, nullptr);
858 221 gst_object_unref(sink);
859 }
860 }
861
862 // The stateless V4L2 H.265 decoder is skipped for a stream software decode can
863 // keep up with. GStreamer 1.26.2 (what Pi OS ships) dereferences a NULL
864 // request in v4l2slh265dec once the driver refuses a stream's bitstream
865 // parameters ("Driver did not accept the bitstream parameters"), which aborts
866 // fppd -- and when it happens on a MultiSync remote's mid-song start, every
867 // restart rejoins the same song and crashes again. Fixed upstream in 1.26.10.
868 // The refusal depends on the stream, so it can't be told ahead of time; what
869 // software costs can: a Pi 4 decodes 1080p30 several times faster than real
870 // time. Streams past 1080p60 keep the hardware decoder, which software could
871 // not play.
872 static constexpr int SOFTWARE_HEVC_MAX_PIXELS_PER_SECOND = 1920 * 1080 * 60;
873
874 // How late a frame may reach a pixel overlay model's appsink and still be
875 // shown (GstBaseSink's max-lateness): GStreamer's own video sinks use 20 ms.
876 static constexpr int VIDEO_OVERLAY_MAX_LATENESS_MS = 20;
877
878 400 static bool SkipStatelessHevcDecoder(GstCaps* caps, GstElementFactory* factory) {
879
2/4
✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 400 times.
✗ Branch 3 not taken.
400 const gchar* name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
880
8/12
✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 399 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 399 times.
✓ Branch 11 taken 1 times.
400 if (!name || strcmp(name, "v4l2slh265dec") != 0 || !caps || gst_caps_is_empty(caps)) {
881 399 return false;
882 }
883
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 const GstStructure* st = gst_caps_get_structure(caps, 0);
884 1 gint w = 0, h = 0, fpsN = 0, fpsD = 1;
885
7/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 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
1 if (!gst_structure_get_int(st, "width", &w) || !gst_structure_get_int(st, "height", &h) || w <= 0 || h <= 0) {
886 ✗ return false; // size unknown: leave the choice as it was
887 }
888
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 gst_structure_get_fraction(st, "framerate", &fpsN, &fpsD);
889
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 double fps = (fpsN > 0 && fpsD > 0) ? (double)fpsN / fpsD : 30.0;
890
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if ((double)w * h * fps > SOFTWARE_HEVC_MAX_PIXELS_PER_SECOND) {
891 ✗ return false;
892 }
893
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogInfo(VB_MEDIAOUT, "GStreamer: decoding %dx%d H.265 in software, not with %s (it can abort fppd when the driver refuses a stream)\n",
894 w, h, name);
895 1 return true;
896 }
897
898 // decodebin's "autoplug-select", asked before it makes each element: note a
899 // hardware video decoder on the decodebin (see PipelineHasHwVideoDecoder) and
900 // let it go ahead, unless the HardwareDecoding setting is off: then skip it
901 // and decodebin moves on to the next decoder for the stream (the software
902 // one). The values are GST_AUTOPLUG_SELECT_TRY (0) and _SKIP (2), whose enum
903 // is not public.
904 // A stream software decode can keep up with also skips the stateless H.265
905 // decoder (SkipStatelessHevcDecoder()).
906 400 static gint OnAutoplugSelect(GstElement* decodebin, GstPad*, GstCaps* caps, GstElementFactory* factory, gpointer) {
907
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 399 times.
400 if (SkipStatelessHevcDecoder(caps, factory)) {
908 1 return 2; // GST_AUTOPLUG_SELECT_SKIP, whose enum is not public either
909 }
910 399 const gchar* klass = gst_element_factory_get_metadata(factory, GST_ELEMENT_METADATA_KLASS);
911
7/8
✓ Branch 0 taken 399 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 250 times.
✓ Branch 3 taken 149 times.
✓ Branch 4 taken 127 times.
✓ Branch 5 taken 123 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 123 times.
399 if (klass && strstr(klass, "Decoder") && strstr(klass, "Video") && strstr(klass, "Hardware")) {
912
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (getSettingInt("HardwareDecoding", 1) == 0) {
913 1 return 2;
914 }
915 3 g_object_set_data(G_OBJECT(decodebin), HW_DECODER_CHOSEN_KEY, GINT_TO_POINTER(1));
916 }
917 398 return 0;
918 }
919
920 3701 static void WatchDecoderChoice(GstElement* element) {
921 3701 GstElementFactory* factory = gst_element_get_factory(element);
922
5/6
✓ Branch 0 taken 3701 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 157 times.
✓ Branch 3 taken 3544 times.
✓ Branch 4 taken 157 times.
✓ Branch 5 taken 3544 times.
3701 if (factory && strcmp(gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory)), "decodebin") == 0) {
923 157 g_signal_connect(element, "autoplug-select", G_CALLBACK(OnAutoplugSelect), nullptr);
924 }
925 3701 }
926
927 2651 static void OnDeepElementAdded(GstBin*, GstBin*, GstElement* element, gpointer) {
928 2651 GuardVideoConvert(element);
929 2651 WatchDecoderChoice(element);
930 2651 }
931
932 222 void GStreamerOutput::GuardVideoConvertInputs(GstElement* pipeline) {
933
8/16
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 222 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 222 times.
✓ Branch 6 taken 222 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 222 times.
✓ Branch 10 taken 222 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 222 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 222 times.
222 if (!pipeline || !GST_IS_BIN(pipeline)) {
934 ✗ return;
935 }
936 // Converters added from here on: deferred branches, and the ones
937 // decodebin and friends plug for themselves.
938
1/2
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
222 g_signal_connect(pipeline, "deep-element-added", G_CALLBACK(OnDeepElementAdded), nullptr);
939 // ...and the ones already there.
940
3/6
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 222 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 222 times.
✗ Branch 5 not taken.
222 GstIterator* it = gst_bin_iterate_recurse(GST_BIN(pipeline));
941 222 GValue item = G_VALUE_INIT;
942 222 bool done = false;
943
2/2
✓ Branch 0 taken 1272 times.
✓ Branch 1 taken 222 times.
1494 while (!done) {
944
3/5
✓ Branch 0 taken 1272 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1050 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 222 times.
1272 switch (gst_iterator_next(it, &item)) {
945 1050 case GST_ITERATOR_OK:
946
4/8
✓ Branch 0 taken 1050 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1050 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1050 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1050 times.
✗ Branch 7 not taken.
1050 GuardVideoConvert(GST_ELEMENT(g_value_get_object(&item)));
947
4/8
✓ Branch 0 taken 1050 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1050 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1050 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1050 times.
✗ Branch 7 not taken.
1050 WatchDecoderChoice(GST_ELEMENT(g_value_get_object(&item)));
948
1/2
✓ Branch 0 taken 1050 times.
✗ Branch 1 not taken.
1050 g_value_reset(&item);
949 1050 break;
950 ✗ case GST_ITERATOR_RESYNC:
951 ✗ gst_iterator_resync(it);
952 ✗ break;
953 222 default:
954 222 done = true;
955 222 break;
956 }
957 }
958
1/2
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
222 g_value_unset(&item);
959
1/2
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
222 gst_iterator_free(it);
960 }
961
962 9116 void GStreamerOutput::EnsureGStreamerInit() {
963
1/2
✓ Branch 0 taken 9116 times.
✗ Branch 1 not taken.
9116 std::lock_guard<std::mutex> lock(s_gstInitMutex);
964
3/4
✓ Branch 0 taken 9116 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2651 times.
✓ Branch 3 taken 6465 times.
9116 if (!gst_is_initialized()) {
965
1/2
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
2651 LogDebug(VB_MEDIAOUT, "GStreamer: EnsureGStreamerInit() entered\n");
966
1/2
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
2651 SetGStreamerEnvLocked();
967
1/2
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
2651 LogDebug(VB_MEDIAOUT, "GStreamer: Calling gst_init()...\n");
968
1/2
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
2651 gst_init(nullptr, nullptr);
969
2/4
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2651 times.
✗ Branch 3 not taken.
2651 LogInfo(VB_MEDIAOUT, "GStreamer initialized: %s\n", gst_version_string());
970 }
971 9116 }
972
973 2651 void GStreamerOutput::PreloadAsync() {
974 {
975 // setenv() is not safe against a concurrent getenv() on another
976 // thread, so it happens here, on the caller's, rather than on the
977 // background thread below.
978
1/2
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
2651 std::lock_guard<std::mutex> lock(s_gstInitMutex);
979
1/2
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
2651 SetGStreamerEnvLocked();
980 2651 }
981 5302 std::thread([]() {
982
2/4
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2651 times.
✗ Branch 3 not taken.
2651 SetThreadName("FPP-GstInit");
983 2651 EnsureGStreamerInit();
984
2/4
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2651 times.
✗ Branch 3 not taken.
5302 }).detach();
985 2651 }
986
987 4518 std::string GStreamerOutput::PipeWireSinkNameForSlot(int slot) {
988 4518 std::string sinkName = getSetting("PipeWireSinkName");
989 // For multi-stream slots > 1, check for a per-slot PipeWire sink setting.
990 // Format: PipeWireSinkName_2, PipeWireSinkName_3, etc.
991 // If not set, falls back to the global PipeWireSinkName.
992
2/2
✓ Branch 0 taken 2245 times.
✓ Branch 1 taken 2273 times.
4518 if (slot > 1) {
993
2/4
✓ Branch 0 taken 2245 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2245 times.
✗ Branch 3 not taken.
2245 std::string slotSinkName = getSetting(("PipeWireSinkName_" + std::to_string(slot)).c_str());
994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2245 times.
2245 if (!slotSinkName.empty()) {
995 ✗ sinkName = slotSinkName;
996 }
997 2245 }
998 4518 return sinkName;
999 ✗ }
1000
1001 // Pick the best available ALSA-direct audio sink element.
1002 //
1003 // We prefer alsasink (avoids autoaudiosink's slow pulsesink probe in
1004 // ALSA-only mode), but the gstreamer1.0-alsa plugin is not present on
1005 // every install — older/upgraded systems may lack it. If alsasink is
1006 // not registered, gst_parse_launch() / gst_element_factory_make() fail
1007 // with "no element alsasink" and audio never starts. Fall back to
1008 // autoaudiosink so playback always works. Result is cached; must be
1009 // called after gst_init().
1010 3 static const char* GetAlsaDirectSinkName() {
1011 #ifdef PLATFORM_OSX
1012 // No ALSA on macOS; autoaudiosink picks osxaudiosink (the default output
1013 // device), and there is no slow pulsesink probe to avoid.
1014 return "autoaudiosink";
1015 #endif
1016 static const char* cached = nullptr;
1017
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (!cached) {
1018 3 GstElementFactory* f = gst_element_factory_find("alsasink");
1019
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (f) {
1020 3 gst_object_unref(f);
1021 3 cached = "alsasink";
1022 } else {
1023 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: alsasink not available "
1024 "(gstreamer1.0-alsa not installed) — "
1025 "falling back to autoaudiosink\n");
1026 ✗ cached = "autoaudiosink";
1027 }
1028 }
1029 3 return cached;
1030 }
1031
1032 // Resolve a DRM connector name (e.g., "HDMI-A-1") to its card path, connector ID,
1033 // connection status, and current display resolution by scanning sysfs.
1034 // Works on all Pi models (Pi 3/4/5) and x86 — no libdrm ioctls needed.
1035 128 GStreamerOutput::DrmConnectorInfo GStreamerOutput::ResolveDrmConnector(const std::string& connectorName) {
1036 128 DrmConnectorInfo info;
1037
1038 // Scan /sys/class/drm/ for cardN-<connectorName>
1039
2/2
✓ Branch 0 taken 481 times.
✓ Branch 1 taken 50 times.
531 for (int cardNum = 0; cardNum < 8; cardNum++) {
1040
3/6
✓ Branch 0 taken 481 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 481 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 481 times.
✗ Branch 5 not taken.
481 std::string sysBase = "/sys/class/drm/card" + std::to_string(cardNum) + "-" + connectorName;
1041
1/2
✓ Branch 0 taken 481 times.
✗ Branch 1 not taken.
481 std::string statusPath = sysBase + "/status";
1042
3/4
✓ Branch 0 taken 481 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 403 times.
✓ Branch 3 taken 78 times.
481 if (!FileExists(statusPath))
1043 403 continue;
1044
1045
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 info.cardPath = "/dev/dri/card" + std::to_string(cardNum);
1046
1047 // Read connection status
1048
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 std::string status = GetFileContents(statusPath);
1049
3/4
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
✓ Branch 3 taken 1 times.
156 info.connected = (status.find("connected") != std::string::npos &&
1050 78 status.find("disconnected") == std::string::npos);
1051
1052 // Read connector ID (available since Linux 5.x)
1053
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 std::string cidPath = sysBase + "/connector_id";
1054
3/4
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 76 times.
✓ Branch 3 taken 2 times.
78 if (FileExists(cidPath)) {
1055
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 std::string cidStr = GetFileContents(cidPath);
1056 76 info.connectorId = atoi(cidStr.c_str());
1057 76 }
1058
1059 // Read display resolution from first available mode
1060
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 std::string modesPath = sysBase + "/modes";
1061
3/4
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 76 times.
✓ Branch 3 taken 2 times.
78 if (FileExists(modesPath)) {
1062
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 std::ifstream mf(modesPath);
1063 76 std::string firstMode;
1064
5/10
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 76 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 76 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 76 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 76 times.
✗ Branch 9 not taken.
76 if (std::getline(mf, firstMode) && !firstMode.empty()) {
1065 // Format: "1920x1080" or "1280x720"
1066 76 size_t xpos = firstMode.find('x');
1067
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if (xpos != std::string::npos) {
1068
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 info.displayWidth = atoi(firstMode.substr(0, xpos).c_str());
1069
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 info.displayHeight = atoi(firstMode.substr(xpos + 1).c_str());
1070 }
1071 }
1072 76 }
1073
1074
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 LogDebug(VB_MEDIAOUT, "GStreamer DRM: %s on card%d connector_id=%d connected=%d display=%dx%d\n",
1075 connectorName.c_str(), cardNum, info.connectorId, info.connected,
1076 info.displayWidth, info.displayHeight);
1077 78 break;
1078
4/4
✓ Branch 0 taken 403 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 403 times.
✓ Branch 3 taken 78 times.
1040 }
1079
1080 128 return info;
1081 ✗ }
1082
1083 // ──────────────────────────────────────────────────────────────────────────────
1084 // Read the maximum per-member delay (ms) across all enabled PipeWire audio
1085 // output groups. This compensates the GStreamer ts-offset so that audio
1086 // through the longest-delayed PipeWire path still lines up with video.
1087 // ──────────────────────────────────────────────────────────────────────────────
1088 2179 static int GetMaxPipeWireGroupDelayMs() {
1089
3/6
✓ Branch 0 taken 2179 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2179 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2179 times.
✗ Branch 5 not taken.
2179 std::string configPath = FPP_DIR_CONFIG("/pipewire-audio-groups.json");
1090
3/4
✓ Branch 0 taken 2179 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2167 times.
✓ Branch 3 taken 12 times.
2179 if (!FileExists(configPath))
1091 2167 return 0;
1092
1093
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Json::Value root;
1094
7/10
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 9 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 9 times.
12 if (!LoadJsonFromFile(configPath, root, JsonRoot::Object) || !root.isMember("groups"))
1095 3 return 0;
1096
1097 9 int maxDelay = 0;
1098 // A value of the wrong JSON type threw from here, on every media start,
1099 // to std::terminate; use what could be read.
1100 try {
1101
8/14
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 17 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 9 times.
✓ Branch 13 taken 8 times.
17 for (const auto& group : root["groups"]) {
1102
5/8
✓ 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.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
11 if (!group.get("enabled", false).asBool())
1103 8 continue;
1104 ✗ for (const auto& member : group["members"]) {
1105 ✗ int delayMs = member.get("delayMs", 0).asInt();
1106 ✗ if (delayMs > maxDelay)
1107 ✗ maxDelay = delayMs;
1108 }
1109 }
1110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } catch (const std::exception& e) {
1111
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
3 WarningHolder::InvalidConfigEntry("pipewire-audio-groups.json", e.what());
1112 1 }
1113 9 return maxDelay;
1114 2179 }
1115
1116 // ──────────────────────────────────────────────────────────────────────────────
1117 // Sample rate the PipeWire graph clock is running at, or 0 if it can't be
1118 // determined. Every stream that arrives at a different rate is resampled by
1119 // its client-side adapter for as long as it plays, so this is what the decode
1120 // chain wants to line up with.
1121 //
1122 // The live daemon is the authority (a card can refine the configured rate
1123 // upward, and force-rate can override it outright), so ask it; the generated
1124 // conf is only a fallback for when the query fails. Cached for the life of
1125 // the process: the graph rate only changes when the PipeWire daemon restarts,
1126 // and fppd restarts with it.
1127 // ──────────────────────────────────────────────────────────────────────────────
1128 376925 static int GetPipeWireGraphRate() {
1129 // Atomic: media starts on several threads can ask at once.
1130 static std::atomic<int> cachedRate{ -1 };
1131
2/2
✓ Branch 0 taken 376662 times.
✓ Branch 1 taken 263 times.
376925 if (cachedRate >= 0)
1132 376662 return cachedRate;
1133
1134 // pw-metadata prints lines of the form
1135 // update: id:0 key:'clock.rate' value:'44100' type:''
1136 526 auto metadataValue = [](const std::string& out, const std::string& key) -> int {
1137
2/4
✓ Branch 0 taken 526 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 526 times.
✗ Branch 3 not taken.
526 std::string needle = "key:'" + key + "' value:'";
1138 526 size_t p = out.find(needle);
1139
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 470 times.
526 if (p == std::string::npos)
1140 56 return 0;
1141 470 p += needle.size();
1142 470 size_t e = out.find('\'', p);
1143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 470 times.
470 if (e == std::string::npos)
1144 ✗ return 0;
1145
1/2
✓ Branch 0 taken 470 times.
✗ Branch 1 not taken.
470 return atoi(out.substr(p, e - p).c_str());
1146 526 };
1147
1148 // Bounded: this runs inside Start(), which StartMediaOutput() calls with
1149 // mediaOutputLock held, and pw-metadata waits forever on a PipeWire
1150 // daemon that is up but not answering.
1151 263 std::string dump;
1152
4/8
✓ Branch 0 taken 263 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 263 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 263 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 263 times.
✗ Branch 7 not taken.
263 if (FILE* p = popen(FileExists("/usr/bin/timeout")
1153 ? "/usr/bin/timeout -k 1 3 /usr/bin/pw-metadata -n settings 2>/dev/null"
1154 : "/usr/bin/pw-metadata -n settings 2>/dev/null",
1155 "r")) {
1156 char buf[1024];
1157 size_t r;
1158
3/4
✓ Branch 0 taken 498 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 235 times.
✓ Branch 3 taken 263 times.
498 while ((r = fread(buf, 1, sizeof(buf), p)) > 0)
1159
1/2
✓ Branch 0 taken 235 times.
✗ Branch 1 not taken.
235 dump.append(buf, r);
1160
1/2
✓ Branch 0 taken 263 times.
✗ Branch 1 not taken.
263 pclose(p);
1161 }
1162 // force-rate pins the graph outright; clock.rate is the default it would
1163 // otherwise settle on.
1164
2/4
✓ Branch 0 taken 263 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 263 times.
✗ Branch 3 not taken.
263 int rate = metadataValue(dump, "clock.force-rate");
1165
1/2
✓ Branch 0 taken 263 times.
✗ Branch 1 not taken.
263 if (rate <= 0)
1166
2/4
✓ Branch 0 taken 263 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 263 times.
✗ Branch 3 not taken.
526 rate = metadataValue(dump, "clock.rate");
1167
1168
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 235 times.
263 if (rate <= 0) {
1169 // Daemon unreachable — fall back to what FPP told it to run at. The
1170 // per-card file is written from the rate the hardware actually clocks
1171 // at and sorts after the defaults, so it wins where both exist.
1172 static const char* confs[] = {
1173 "/etc/pipewire/pipewire.conf.d/95-fpp-alsa-sink.conf",
1174 "/etc/pipewire/pipewire.conf.d/90-fpp.conf"
1175 };
1176
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
84 for (const char* conf : confs) {
1177
2/4
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
56 std::string contents = GetFileContents(conf);
1178 56 size_t p = contents.find("default.clock.rate");
1179
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 if (p == std::string::npos)
1180 56 continue;
1181 ✗ p = contents.find('=', p);
1182 ✗ if (p == std::string::npos)
1183 ✗ continue;
1184 ✗ rate = atoi(contents.c_str() + p + 1);
1185 ✗ if (rate > 0)
1186 ✗ break;
1187
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
56 }
1188 }
1189
1190 263 cachedRate = rate > 0 ? rate : 0;
1191
1/2
✓ Branch 0 taken 263 times.
✗ Branch 1 not taken.
263 LogDebug(VB_MEDIAOUT, "GStreamer: PipeWire graph clock rate: %d\n", cachedRate.load());
1192 263 return cachedRate;
1193 263 }
1194
1195 // ──────────────────────────────────────────────────────────────────────────────
1196 // Rate to pin the decode chain to before the tee, or 0 to leave it
1197 // unconstrained and let the source's own rate flow through.
1198 //
1199 // A file whose rate differs from the graph's gets resampled by somebody; the
1200 // only question is who. Measured on a single-core AM335x feeding a 44100 Hz
1201 // graph, 30 s of stereo audio, summing gstreamer + pipewire + wireplumber as a
1202 // percentage of the one core:
1203 //
1204 // source pinned 48000 unpinned pinned to graph rate
1205 // 44.1 kHz 25.2% 18.0% 18.0%
1206 // 48 kHz 20.9% 20.6% 24.0%
1207 // 88.2 kHz -- 41.0% 34.5%
1208 // 96 kHz 42.8% 41.2% 37.0%
1209 //
1210 // PipeWire's resampler is roughly three times cheaper than audioresample here,
1211 // so leaving the rate alone wins whenever the two are close — including the
1212 // 48 kHz-into-44.1 kHz case, where GStreamer converting costs more than the
1213 // 9% extra samples it would save. That flips once the source runs at twice
1214 // the graph rate or more: carrying that many extra samples through volume, the
1215 // tee, the sample tap and the sink outweighs the slower resampler, so drop the
1216 // rate early.
1217 //
1218 // The old unconditional 48000 was the worst of both worlds on a 44.1 kHz
1219 // graph, which is what an I2S cape or a 44.1 kHz USB card gives you:
1220 // audioresample converted 44100 up to 48000 and PipeWire converted it straight
1221 // back down.
1222 // ──────────────────────────────────────────────────────────────────────────────
1223 // Add every element to `pipeline` and link them head to tail. The kmssink
1224 // branches below vary in length -- a crop element appears only for an output
1225 // showing a sub-region, scaling elements only for a stretched one -- and
1226 // spelling out a gst_element_link_many() per combination does not scale past
1227 // the second optional element.
1228 //
1229 // gst_bin_add() refuses an element whose name is already in the bin, and then
1230 // drops the floating ref it was handed -- the element is freed. Linking it
1231 // afterwards is a use-after-free, so stop before linking anything.
1232 112 static bool AddAndLinkChain(GstElement* pipeline, const std::vector<GstElement*>& chain) {
1233
2/2
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 112 times.
502 for (GstElement* e : chain) {
1234
4/8
✓ Branch 0 taken 390 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 390 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 390 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 390 times.
390 if (!gst_bin_add(GST_BIN(pipeline), e))
1235 ✗ return false;
1236 }
1237
2/2
✓ Branch 0 taken 278 times.
✓ Branch 1 taken 112 times.
390 for (size_t i = 1; i < chain.size(); i++) {
1238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 278 times.
278 if (!gst_element_link(chain[i - 1], chain[i]))
1239 ✗ return false;
1240 }
1241 112 return true;
1242 }
1243
1244 // The elements an HDMI output puts between its converter and its kmssink, in
1245 // link order. A region crop has to come first: it selects from the source, so
1246 // anything that rescales the frame ahead of it would make it slice up an
1247 // already-resized picture. Either list may be empty -- a plain fitted,
1248 // whole-frame output gets nothing at all, which is the common case.
1249 12 static std::vector<GstElement*> BuildHdmiScaleChain(const VideoCropRect& crop,
1250 const std::string& scaling,
1251 int displayWidth, int displayHeight,
1252 const std::string& namePrefix) {
1253 12 std::vector<GstElement*> chain;
1254
4/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 10 times.
12 if (GstElement* cropElem = VideoOutputManager::CreateCropElement(crop, namePrefix + "_crop"))
1255
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 chain.push_back(cropElem);
1256 12 for (GstElement* e : VideoOutputManager::CreateScaleElements(scaling, displayWidth,
1257
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
24 displayHeight, namePrefix))
1258
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
12 chain.push_back(e);
1259 12 return chain;
1260 ✗ }
1261
1262 // decodebin's "autoplug-query": asked on behalf of an element it has plugged
1263 // but not yet linked downstream, which is exactly when a decoder settles its
1264 // output format. Left alone, a decoder sees no downstream preference and takes
1265 // the first format in its own template -- S16 for mpg123 -- and audioconvert
1266 // then turns that straight back into the F32 the persistent PipeWire stream
1267 // wants: two conversions of every sample where none is needed, about 2% of a
1268 // single-core board for an mp3. Offering F32 first lets a decoder that can
1269 // produce it do so; one that can't still sees every other raw format.
1270 5 static gboolean PreferFloatDecode(GstElement* bin, GstPad* pad, GstElement* element, GstQuery* query, gpointer userData) {
1271
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 if (GST_QUERY_TYPE(query) != GST_QUERY_CAPS || GST_PAD_DIRECTION(pad) != GST_PAD_SRC) {
1272 ✗ return FALSE;
1273 }
1274 static GstStaticCaps rawAudio = GST_STATIC_CAPS("audio/x-raw");
1275 static GstStaticCaps floatFirst = GST_STATIC_CAPS("audio/x-raw,format=F32LE,layout=interleaved; audio/x-raw");
1276
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 GstCaps* raw = gst_static_caps_get(&rawAudio);
1277
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 GstCaps* tmpl = gst_pad_get_pad_template_caps(pad);
1278
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 bool rawAudioOut = gst_caps_can_intersect(tmpl, raw);
1279
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 gst_caps_unref(tmpl);
1280
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 gst_caps_unref(raw);
1281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!rawAudioOut) {
1282 // A parser or demuxer, or a video decoder: not ours to answer.
1283 ✗ return FALSE;
1284 }
1285
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 GstCaps* prefer = gst_static_caps_get(&floatFirst);
1286 5 GstCaps* filter = nullptr;
1287
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 gst_query_parse_caps(query, &filter);
1288
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (filter) {
1289
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 GstCaps* both = gst_caps_intersect_full(prefer, filter, GST_CAPS_INTERSECT_FIRST);
1290
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 gst_caps_unref(prefer);
1291 5 prefer = both;
1292 }
1293
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 gst_query_set_caps_result(query, prefer);
1294
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 gst_caps_unref(prefer);
1295 5 return TRUE;
1296 }
1297
1298 2179 static int ChooseDecodeRate(int mediaRate, bool usePipeWire) {
1299 // Only PipeWire has a fixed graph rate to line up with. With alsasink the
1300 // sink negotiates against the device directly and audioresample fills in
1301 // whatever conversion that needs.
1302
4/4
✓ Branch 0 taken 2176 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 134 times.
✓ Branch 3 taken 2042 times.
2179 if (!usePipeWire || mediaRate <= 0)
1303 137 return 0;
1304 // No graph FPP configures runs below 44100, so nothing at or under 48000
1305 // can reach the 2x threshold — skip the query entirely for the rates
1306 // essentially all show media uses.
1307
2/2
✓ Branch 0 taken 2040 times.
✓ Branch 1 taken 2 times.
2042 if (mediaRate <= 48000)
1308 2040 return 0;
1309 2 int graphRate = GetPipeWireGraphRate();
1310
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 if (graphRate > 0 && mediaRate >= 2 * graphRate)
1311 1 return graphRate;
1312 1 return 0;
1313 }
1314
1315 // ---------------------------------------------------------------------------
1316 // gst-pipewire channel-order quirk detection (FalconChristmas/fpp#2620)
1317 //
1318 // pipewiresink's caps→SPA conversion (set_default_channels() in
1319 // gstpipewireformat.c, present through at least pipewire 1.4.2) ignores the
1320 // GStreamer channel-mask and stamps a hard-coded PulseAudio-order position
1321 // table onto the stream — [FL FR RL RR FC LFE SL SR] for 7.1 — while the
1322 // samples stay in GStreamer/WAV canonical order [FL FR FC LFE RL RR SL SR].
1323 // Every label-matched PipeWire link downstream then swaps FC/LFE with RL/RR
1324 // (5.0/5.1 layouts are affected the same way; mono/stereo/quad tables happen
1325 // to match; 3ch/7ch get no position table at all).
1326 //
1327 // Rather than keying a workaround off a version number (a distro backport
1328 // would silently break it in either direction), probe the installed plugin
1329 // once: connect a silent unrouted 7.1 stream and read back the channel
1330 // positions it declares. The verdict is cached keyed to the plugin binary's
1331 // identity, so a plugin upgrade re-probes and a fixed plugin automatically
1332 // disables the workaround.
1333 6 static bool IsGstPipeWireChannelOrderQuirky() {
1334 static int cachedVerdict = -1; // -1 unknown, 0 clean, 1 quirky (per fppd run)
1335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (cachedVerdict >= 0)
1336 ✗ return cachedVerdict == 1;
1337 6 cachedVerdict = 0; // default: no workaround unless proven needed
1338
1339 // Identity of the installed gst-pipewire plugin binary
1340 6 std::string pluginSig;
1341
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 GstPlugin* plugin = gst_registry_find_plugin(gst_registry_get(), "pipewire");
1342
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (plugin) {
1343
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 const gchar* fn = gst_plugin_get_filename(plugin);
1344
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (fn) {
1345 struct stat st;
1346
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (stat(fn, &st) == 0) {
1347
7/14
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 6 times.
✗ Branch 13 not taken.
16 pluginSig = std::string(fn) + ":" + std::to_string(st.st_mtime) + ":" + std::to_string(st.st_size);
1348 }
1349 }
1350
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 gst_object_unref(plugin);
1351 }
1352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (pluginSig.empty()) {
1353 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: cannot identify pipewire plugin; channel-order workaround disabled\n");
1354 ✗ return false;
1355 }
1356
1357 // Cached verdict from a previous run for this exact plugin binary?
1358
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 const std::string cachePath = getFPPMediaDir() + "/config/gst-pipewire-quirks.json";
1359
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (FileExists(cachePath)) {
1360 ✗ Json::Value cache;
1361 ✗ if (LoadJsonFromFile(cachePath, cache, JsonRoot::Object)
1362 ✗ && cache.get("pluginSig", "").asString() == pluginSig) {
1363 ✗ cachedVerdict = cache.get("channelOrderQuirky", false).asBool() ? 1 : 0;
1364 ✗ LogInfo(VB_MEDIAOUT, "GStreamer: pipewire channel-order quirk (cached): %s\n",
1365 cachedVerdict ? "PRESENT (workaround active)" : "absent");
1366 ✗ return cachedVerdict == 1;
1367 }
1368 ✗ }
1369
1370 // Probe: silent 7.1 stream, not linked to anything (node.autoconnect=false),
1371 // then read the position array the plugin declared from pw-dump.
1372
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 LogInfo(VB_MEDIAOUT, "GStreamer: probing pipewire plugin for channel-order quirk...\n");
1373 6 GError* perr = nullptr;
1374
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 GstElement* probe = gst_parse_launch(
1375 "audiotestsrc wave=silence is-live=true ! "
1376 "audio/x-raw,format=S16LE,rate=48000,channels=8,channel-mask=(bitmask)0xc3f ! "
1377 "pipewiresink sync=false "
1378 "stream-properties=\"props,node.name=(string)fpp_chorder_probe,node.autoconnect=(boolean)false\"",
1379 &perr);
1380
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (!probe || perr) {
1381 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: quirk probe pipeline failed: %s\n",
1382 perr ? perr->message : "unknown");
1383 ✗ if (perr)
1384 ✗ g_error_free(perr);
1385 ✗ if (probe)
1386 ✗ gst_object_unref(probe);
1387 ✗ return false;
1388 }
1389
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 gst_element_set_state(probe, GST_STATE_PLAYING);
1390
1391 // Poll pw-dump for the probe node's declared EnumFormat positions
1392 6 std::vector<std::string> positions;
1393
5/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 6 times.
12 for (int attempt = 0; attempt < 10 && positions.empty(); attempt++) {
1394
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 std::this_thread::sleep_for(std::chrono::milliseconds(300));
1395 // Bounded for the same reason as pw-metadata in GetPipeWireGraphRate().
1396
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 FILE* p = popen(FileExists("/usr/bin/timeout")
1397 ? "/usr/bin/timeout -k 1 3 /usr/bin/pw-dump 2>/dev/null"
1398 : "/usr/bin/pw-dump 2>/dev/null",
1399 "r");
1400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!p)
1401 ✗ break;
1402 6 std::string out;
1403 char buf[8192];
1404 size_t r;
1405
3/4
✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 129 times.
✓ Branch 3 taken 6 times.
135 while ((r = fread(buf, 1, sizeof(buf), p)) > 0)
1406
1/2
✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
129 out.append(buf, r);
1407
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 pclose(p);
1408
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 Json::Value dump;
1409
5/10
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 6 times.
6 if (!LoadJsonFromString(out, dump) || !dump.isArray())
1410 ✗ continue;
1411
7/12
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 503 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 503 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 509 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 503 times.
✓ Branch 11 taken 6 times.
509 for (const auto& obj : dump) {
1412
2/4
✓ Branch 0 taken 503 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 503 times.
503 if (!obj.isObject())
1413 ✗ continue;
1414
6/10
✓ Branch 0 taken 503 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 503 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 503 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 503 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 456 times.
✓ Branch 9 taken 47 times.
503 if (obj.get("type", "").asString() != "PipeWire:Interface:Node")
1415 456 continue;
1416
8/14
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 47 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 47 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 47 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 47 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 41 times.
✓ Branch 13 taken 6 times.
47 if (obj["info"]["props"].get("node.name", "").asString() != "fpp_chorder_probe")
1417 41 continue;
1418
8/18
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 6 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 6 times.
✗ Branch 17 not taken.
6 for (const auto& fmt : obj["info"]["params"]["EnumFormat"]) {
1419
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 const Json::Value& pos = fmt["position"];
1420
5/10
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6 times.
✗ Branch 9 not taken.
6 if (pos.isArray() && pos.size() == 8) {
1421
6/10
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 54 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 48 times.
✓ Branch 9 taken 6 times.
54 for (const auto& ch : pos)
1422
3/6
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
48 positions.push_back(ch.asString());
1423 6 break;
1424 }
1425 }
1426 }
1427
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 }
1428 // pipewiresink can deadlock in its teardown (see Stop()); don't let the
1429 // probe hang the Start() that asked for it.
1430
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (!SetPipelineToNullSupervised(probe, PIPELINE_TEARDOWN_TIMEOUT_MS)) {
1431 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: channel-order quirk probe did not stop, abandoning it\n");
1432 }
1433
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 gst_object_unref(probe);
1434
1435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (positions.size() != 8) {
1436 // PipeWire down or dump unparsable — leave workaround off, don't cache
1437 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: channel-order quirk probe inconclusive; workaround disabled this run\n");
1438 ✗ return false;
1439 }
1440 6 int idxRL = -1, idxFC = -1;
1441
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 6 times.
54 for (int i = 0; i < 8; i++) {
1442
3/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 42 times.
48 if (positions[i] == "RL")
1443 6 idxRL = i;
1444
3/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 36 times.
42 else if (positions[i] == "FC")
1445 6 idxFC = i;
1446 }
1447
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 bool quirky = (idxRL >= 0 && idxFC >= 0 && idxRL < idxFC);
1448
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 cachedVerdict = quirky ? 1 : 0;
1449
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 LogWarn(VB_MEDIAOUT, "GStreamer: pipewire declares 7.1 positions [%s %s %s %s %s %s %s %s] — channel-order quirk %s\n",
1450 positions[0].c_str(), positions[1].c_str(), positions[2].c_str(), positions[3].c_str(),
1451 positions[4].c_str(), positions[5].c_str(), positions[6].c_str(), positions[7].c_str(),
1452 quirky ? "PRESENT (pre-permute workaround active)" : "absent");
1453
1454
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 Json::Value cache;
1455
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 cache["pluginSig"] = pluginSig;
1456
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 cache["channelOrderQuirky"] = quirky;
1457
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 cache["declaredOrder"] = Json::arrayValue;
1458
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 6 times.
54 for (const auto& s : positions)
1459
3/6
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
48 cache["declaredOrder"].append(s);
1460
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 SaveJsonToFile(cache, cachePath);
1461 6 return quirky;
1462 6 }
1463
1464 // Build the pre-permute fragment that reorders GStreamer/WAV canonical
1465 // channel order into the PulseAudio order the quirky plugin will declare,
1466 // so PipeWire's label-matched links route the right content. Returns the
1467 // extra caps to append to the rate capsfilter and the permute elements.
1468 6 static void BuildChannelOrderFix(int channels, std::string& extraCaps, std::string& permuteChain) {
1469 struct Layout {
1470 int channels;
1471 const char* mask;
1472 std::vector<int> srcIdx; // pulse-order slot -> wav-order source channel
1473 };
1474 static const Layout layouts[] = {
1475 { 5, "0x37", { 0, 1, 3, 4, 2 } }, // [FL FR RL RR FC] <- [FL FR FC RL RR]
1476 { 6, "0x3f", { 0, 1, 4, 5, 2, 3 } }, // [FL FR RL RR FC LFE] <- [FL FR FC LFE RL RR]
1477 { 8, "0xc3f", { 0, 1, 4, 5, 2, 3, 6, 7 } } // [FL FR RL RR FC LFE SL SR] <- [FL FR FC LFE RL RR SL SR]
1478
5/16
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6 times.
✗ 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.
18 };
1479
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 for (const auto& l : layouts) {
1480
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 if (l.channels != channels)
1481 6 continue;
1482
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 extraCaps = ",channels=" + std::to_string(channels) + ",channel-mask=(bitmask)" + l.mask;
1483
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 std::string matrix = "<";
1484
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
42 for (int row = 0; row < channels; row++) {
1485
3/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 matrix += (row ? ",<" : "<");
1486
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 36 times.
252 for (int col = 0; col < channels; col++) {
1487
3/4
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 216 times.
✗ Branch 3 not taken.
216 matrix += (col ? "," : "");
1488
3/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 180 times.
✓ Branch 2 taken 216 times.
✗ Branch 3 not taken.
216 matrix += (l.srcIdx[row] == col) ? "(float)1.0" : "(float)0.0";
1489 }
1490
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 matrix += ">";
1491 }
1492
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 matrix += ">";
1493
4/8
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
6 permuteChain = "audioconvert mix-matrix=\"" + matrix + "\" ! audio/x-raw" + extraCaps + " ! ";
1494 6 return;
1495 6 }
1496 ✗ extraCaps.clear();
1497 ✗ permuteChain.clear();
1498 }
1499
1500 2182 GStreamerOutput::GStreamerOutput(const std::string& mediaFilename, MediaOutputStatus* status, const std::string& videoOut, int streamSlot)
1501
3/6
✓ Branch 0 taken 2182 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2182 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2182 times.
✗ Branch 5 not taken.
2182 : m_videoOut(videoOut), m_streamSlot(streamSlot) {
1502
1/2
✓ Branch 0 taken 2182 times.
✗ Branch 1 not taken.
2182 LogDebug(VB_MEDIAOUT, "GStreamer: CTOR enter (%s, videoOut=%s, slot=%d)\n", mediaFilename.c_str(), videoOut.c_str(), streamSlot);
1503
1/2
✓ Branch 0 taken 2182 times.
✗ Branch 1 not taken.
2182 m_mediaFilename = mediaFilename;
1504 2182 m_mediaOutputStatus = status;
1505
1/2
✓ Branch 0 taken 2182 times.
✗ Branch 1 not taken.
2182 m_allowSpeedAdjust = (getSettingInt("remoteIgnoreSync") == 0);
1506 // An audio-only track will play through the slot's persistent PipeWire
1507 // stream. Start bringing it up now: a MultiSync remote constructs this on
1508 // the master's media-open packet, well before the start packet arrives.
1509
7/8
✓ Branch 0 taken 2182 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1941 times.
✓ Branch 3 taken 241 times.
✓ Branch 4 taken 1814 times.
✓ Branch 5 taken 127 times.
✓ Branch 6 taken 2055 times.
✓ Branch 7 taken 127 times.
2182 if (m_videoOut == "--Disabled--" || m_videoOut.empty()) {
1510
1/2
✓ Branch 0 taken 2055 times.
✗ Branch 1 not taken.
2055 PipeWireOutputStream::Prewarm(m_streamSlot, PipeWireOutputStream::LINGER_MS);
1511
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 1 times.
157 } else if (m_videoOut.starts_with("HDMI") || m_videoOut.starts_with("--HDMI") ||
1512
6/6
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 111 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 115 times.
✓ Branch 5 taken 12 times.
157 m_videoOut.starts_with("DSI-") || m_videoOut.starts_with("Composite-")) {
1513 // A video for a display, so for the slot's video group: build its
1514 // overlay and RTP members while the video's own pipeline starts,
1515 // rather than after.
1516
2/4
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
115 VideoOutputManager::Instance().Prewarm(m_streamSlot, VideoOutputManager::LINGER_MS);
1517 }
1518
1/2
✓ Branch 0 taken 2182 times.
✗ Branch 1 not taken.
2182 EnsureGStreamerInit();
1519
1/2
✓ Branch 0 taken 2182 times.
✗ Branch 1 not taken.
2182 LogDebug(VB_MEDIAOUT, "GStreamer: CTOR done (%s)\n", mediaFilename.c_str());
1520 2182 }
1521
1522 4758 GStreamerOutput::~GStreamerOutput() {
1523 4210 Close();
1524 4758 }
1525
1526 2263 int GStreamerOutput::Start(int msTime) {
1527
1/2
✓ Branch 0 taken 2263 times.
✗ Branch 1 not taken.
2263 LogDebug(VB_MEDIAOUT, "GStreamer: Start(%d) enter - %s\n", msTime, m_mediaFilename.c_str());
1528
1/2
✓ Branch 0 taken 2263 times.
✗ Branch 1 not taken.
2263 std::lock_guard<std::recursive_mutex> opLock(m_opMutex);
1529
1530
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 2182 times.
2263 if (m_prepared) {
1531 81 m_prepared = false;
1532
7/10
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 79 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 79 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 79 times.
✓ Branch 9 taken 2 times.
81 if (msTime == 0 && m_pipeline && !m_pipelineFailed && !m_teardownComplete) {
1533
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 return StartPrepared();
1534 }
1535 // Started part-way in, or it failed while waiting: build it again
1536 // (the m_pipeline check below closes the prepared one first).
1537
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 LogDebug(VB_MEDIAOUT, "GStreamer: not using the prepared pipeline for %s (%s)\n", m_mediaFilename.c_str(),
1538 msTime ? "starting part-way in" : "it failed");
1539 }
1540 // A start adjustment is a prepared start's (see m_startAdjustMs); a
1541 // prepare below takes it afresh.
1542 2184 m_startAdjustMs = 0;
1543 2184 m_startSeekAdjMs = 0;
1544 2184 m_startErrPending = false;
1545 2184 m_preparedSeek.reset();
1546
1547 // Off the slot table until SetActiveOutput() at the end, as in Close():
1548 // an output started again below is still registered from its last run,
1549 // and the slot manager's calls into it (SetVolume() from Set Slot Volume
1550 // on an HTTP thread) would meet the guard, m_volume and the pipeline
1551 // being rebuilt here without the guard lock. Waits out any such call in
1552 // progress. m_opMutex before the slot manager's lock, as documented on
1553 // m_opMutex. The manager's pump try-locks m_opMutex under its own lock,
1554 // but a Process() it runs can go on to Stop() (and so into the slot
1555 // manager again) on the same thread; no path does that while another
1556 // thread sits here, but it is the exception to the order.
1557
2/4
✓ Branch 0 taken 2184 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2184 times.
✗ Branch 3 not taken.
2184 StreamSlotManager::Instance().DetachOutput(m_streamSlot, this);
1558
1559 // An output can be started again once its media has ended (a remote that
1560 // missed the stop and open packets is handed the same file again). That
1561 // run's pipeline, bus, guard and slot registration are all still here and
1562 // would be overwritten below, leaking the pipeline with its PipeWire node
1563 // and DRM resources and leaving its callbacks pointed at this object.
1564 #ifdef HAS_VIDEO_DISPLAY
1565 // A pipeline closed here is rebuilt below on the same display: a
1566 // prepared one that can't be used (started part-way in, or it failed),
1567 // or a remote starting the same file again. It may be the display's
1568 // only user -- the track before has let go -- so it would close, and
1569 // the console come up, before the rebuild took it again. Held until
1570 // Start() returns, by when the rebuild holds it itself.
1571 struct DisplayHold {
1572 std::shared_ptr<VideoDisplay> display;
1573 2184 ~DisplayHold() {
1574
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2182 times.
2184 if (display) {
1575 2 display->Release();
1576 }
1577 2184 }
1578 2184 } displayHold;
1579 #endif
1580
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2182 times.
2184 if (m_pipeline) {
1581
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogDebug(VB_MEDIAOUT, "GStreamer: Start() on an output that already ran, closing the old pipeline first\n");
1582 #ifdef HAS_VIDEO_DISPLAY
1583
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (m_display) {
1584 2 displayHold.display = m_display;
1585
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 displayHold.display->AddUser();
1586 }
1587 #endif
1588
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Close();
1589 }
1590 // Per-run state the watchdogs and the preroll guards key off.
1591 2184 m_wallStartMs = 0;
1592 2184 m_playStartMs = 0;
1593 2184 m_lastPosition = -1;
1594 2184 m_stallStartMs = 0;
1595 2184 m_pendingSeekNs = -1;
1596 2184 m_stopRequested = false;
1597 2184 m_reportedFailure = false;
1598 2184 m_reportedNoAudio = false;
1599 2184 m_sawAudioPad = false;
1600 2184 m_dkmsConnectorNames.clear();
1601 2184 m_dkmsCardPaths.clear();
1602 2184 m_pipelineFailed = false;
1603 2184 m_preparedTotalSecs = -1;
1604
2/2
✓ Branch 0 taken 2086 times.
✓ Branch 1 taken 98 times.
2184 if (!m_preparing) {
1605 // Before anything below writes the slot's status.
1606
2/4
✓ Branch 0 taken 2086 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2086 times.
✗ Branch 3 not taken.
2086 StreamSlotManager::Instance().ClaimSlot(m_streamSlot, this);
1607 2086 m_claimedSlot = true;
1608 2086 m_soundEndMs = 0;
1609
1610 // Flush PipeWire filter-chain delay ring-buffers EARLY in Start().
1611 // This runs as a fire-and-forget thread (pw-cli calls take ~200ms+).
1612 // By calling it here instead of right before set_state(PLAYING),
1613 // the flush has the entire pipeline-build time (~50-100ms) to complete
1614 // before audio actually starts flowing. Not for a track that will
1615 // carry on the output stream's timeline (see StartFlushes()).
1616
1/2
✓ Branch 0 taken 2086 times.
✗ Branch 1 not taken.
2086 m_flushesDeferred = PipeWireOutputStream::SlotMayCarryOn(m_streamSlot);
1617
2/2
✓ Branch 0 taken 1532 times.
✓ Branch 1 taken 554 times.
2086 if (!m_flushesDeferred) {
1618
1/2
✓ Branch 0 taken 1532 times.
✗ Branch 1 not taken.
1532 FlushPipeWireDelayBuffers();
1619 }
1620 }
1621
1622 // Fresh pipeline — allow Stop()'s teardown to run for this track
1623 2184 m_teardownComplete = false;
1624 2184 m_audible = false;
1625
1626 // Fresh lifetime guard for this pipeline's decodebin pad callbacks. Any
1627 // connection left over from a previous pipeline holds the previous guard,
1628 // which Close() already cleared, so it can never reach this object again.
1629
1/2
✓ Branch 0 taken 2184 times.
✗ Branch 1 not taken.
2184 auto cbGuard = std::make_shared<CallbackGuard>();
1630 2184 cbGuard->self = this;
1631
1/2
✓ Branch 0 taken 2184 times.
✗ Branch 1 not taken.
2184 m_cbGuard.store(cbGuard);
1632
1633 // Reset MultiSync rate-matching state for the new track
1634 2184 m_currentRate = 1.0f;
1635 2184 m_diffsSize = 0;
1636 2184 m_diffIdx = 0;
1637 2184 m_diffSum = 0;
1638 2184 m_rateSum = 0.0f;
1639 2184 m_lastDiff = -1;
1640 2184 m_rateDiff = 0;
1641 2184 m_lastRates.clear();
1642 2184 m_lastRatesSum = 0.0f;
1643
1644 // Build full path — check music dir, then video dir (mirrors SDLOutput)
1645
1/2
✓ Branch 0 taken 2184 times.
✗ Branch 1 not taken.
2184 std::string fullPath = m_mediaFilename;
1646
3/4
✓ Branch 0 taken 2184 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2150 times.
✓ Branch 3 taken 34 times.
2184 if (!FileExists(fullPath)) {
1647
4/8
✓ Branch 0 taken 2150 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2150 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2150 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2150 times.
✗ Branch 7 not taken.
6391 fullPath = FPP_DIR_MUSIC("/" + m_mediaFilename);
1648 }
1649
3/4
✓ Branch 0 taken 2184 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 137 times.
✓ Branch 3 taken 2047 times.
2184 if (!FileExists(fullPath)) {
1650
4/8
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 137 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 137 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 137 times.
✗ Branch 7 not taken.
405 fullPath = FPP_DIR_VIDEO("/" + m_mediaFilename);
1651 }
1652
3/4
✓ Branch 0 taken 2184 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2181 times.
2184 if (!FileExists(fullPath)) {
1653
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogErr(VB_MEDIAOUT, "GStreamer: media file not found: %s\n", m_mediaFilename.c_str());
1654
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 WarningHolder::AddWarningTimeout(60, 30, "Media file not found: " + m_mediaFilename);
1655 3 return 0;
1656 }
1657
1/2
✓ Branch 0 taken 2181 times.
✗ Branch 1 not taken.
2181 m_fileGain = LookupNormalizeGain(fullPath);
1658
1659 // Pre-populate duration from file metadata so that
1660 // PlaylistEntryMedia::GetLengthInMS() returns a valid value immediately.
1661 // Without this, the playlist status polls GetElapsedMS() before GStreamer
1662 // has queried the duration, causing m_duration to track elapsed time
1663 // and seconds_remaining to always report 0.
1664 2181 int mediaChannels = 0; // used by the pipewire channel-order workaround below
1665 2181 int mediaRate = 0; // used by the decode-rate choice below
1666 {
1667
1/2
✓ Branch 0 taken 2181 times.
✗ Branch 1 not taken.
2181 MediaDetails details;
1668
1/2
✓ Branch 0 taken 2181 times.
✗ Branch 1 not taken.
2181 details.ParseMedia(fullPath.c_str());
1669 2181 mediaChannels = details.channels;
1670 2181 mediaRate = details.sampleRate;
1671
2/2
✓ Branch 0 taken 2046 times.
✓ Branch 1 taken 135 times.
2181 if (details.lengthMS > 0) {
1672 2046 int totalSecs = details.lengthMS / 1000;
1673 2046 m_maxDuration = (gint64)details.lengthMS * GST_MSECOND;
1674
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 1981 times.
2046 if (m_preparing) {
1675 // The slot's status is the playing track's until this starts.
1676 65 m_preparedTotalSecs = totalSecs;
1677 } else {
1678
1/2
✓ Branch 0 taken 1981 times.
✗ Branch 1 not taken.
1981 std::lock_guard<std::mutex> lock(MediaOutputStatus::FieldsLock());
1679 1981 m_mediaOutputStatus->minutesTotal = totalSecs / 60;
1680 1981 m_mediaOutputStatus->secondsTotal = totalSecs % 60;
1681
1/2
✓ Branch 0 taken 1981 times.
✗ Branch 1 not taken.
1981 LogDebug(VB_MEDIAOUT, "GStreamer: pre-set duration from metadata: %d:%02d (%d ms)\n",
1682 m_mediaOutputStatus->minutesTotal, m_mediaOutputStatus->secondsTotal, details.lengthMS);
1683 1981 }
1684 }
1685 2181 }
1686
1687 // Read PipeWire video routing setting early — it affects whether we
1688 // still need a video pipeline even when the primary HDMI is disconnected.
1689 // Only honour it when PipeWire is actually the audio backend; in ALSA-only
1690 // mode PipeWire isn't running so pipewiresink would fail to connect and
1691 // block the pipeline (causing audio stall / playback abort).
1692
1/2
✓ Branch 0 taken 2181 times.
✗ Branch 1 not taken.
2181 bool usePipeWireBackendLocal = isPipeWireBackend();
1693
2/2
✓ Branch 0 taken 2178 times.
✓ Branch 1 taken 3 times.
2181 if (usePipeWireBackendLocal) {
1694
1/2
✓ Branch 0 taken 2178 times.
✗ Branch 1 not taken.
2178 m_pwVideoSinkName = getSetting("PipeWireVideoSinkName");
1695
2/2
✓ Branch 0 taken 1124 times.
✓ Branch 1 taken 1054 times.
2178 if (m_streamSlot > 1) {
1696
1/2
✓ Branch 0 taken 1124 times.
✗ Branch 1 not taken.
1124 std::string slotSetting = "PipeWireVideoSinkName_" + std::to_string(m_streamSlot);
1697
1/2
✓ Branch 0 taken 1124 times.
✗ Branch 1 not taken.
1124 std::string slotVideoSinkName = getSetting(slotSetting.c_str());
1698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1124 times.
1124 if (!slotVideoSinkName.empty()) {
1699 ✗ m_pwVideoSinkName = slotVideoSinkName;
1700 }
1701 1124 }
1702 } else {
1703 3 m_pwVideoSinkName.clear();
1704 }
1705
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2121 times.
2181 if (!m_pwVideoSinkName.empty()) {
1706
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 LogDebug(VB_MEDIAOUT, "GStreamer: PipeWireVideoSinkName='%s' (slot %d) — video will route through PipeWire\n",
1707 m_pwVideoSinkName.c_str(), m_streamSlot);
1708 }
1709
1710 // Determine if we need a video overlay branch or HDMI output
1711 2181 bool wantVideo = false; // PixelOverlay mode
1712 2181 bool wantHDMI = false; // DRM/KMS HDMI mode
1713
1714
7/8
✓ Branch 0 taken 2181 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1943 times.
✓ Branch 3 taken 238 times.
✓ Branch 4 taken 129 times.
✓ Branch 5 taken 1814 times.
✓ Branch 6 taken 129 times.
✓ Branch 7 taken 2052 times.
2181 if (m_videoOut != "--Disabled--" && !m_videoOut.empty()) {
1715 // Check if this is an HDMI/DRM output connector
1716
4/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 1 times.
162 if (m_videoOut.starts_with("HDMI-") || m_videoOut.starts_with("DSI-") ||
1717 16 m_videoOut.starts_with("Composite-") ||
1718
12/16
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 13 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 times.
✓ Branch 13 taken 12 times.
✓ Branch 14 taken 117 times.
✓ Branch 15 taken 12 times.
146 m_videoOut == "--HDMI--" || m_videoOut == "--hdmi--" || m_videoOut == "HDMI") {
1719 #ifdef PLATFORM_OSX
1720 // A Mac has no connectors to resolve: every display target is the
1721 // slot's video window. It stands in for the primary HDMI output,
1722 // as connector 0, so the rest of the HDMI path - its pacing, its
1723 // position queries, its synchronous teardown - applies unchanged.
1724 if (MacOSAppActive()) {
1725 m_wantHDMI = true;
1726 wantHDMI = true;
1727 m_macVideoWindow = true;
1728 m_hdmiConnectorId = 0;
1729 m_hdmiCardPath.clear();
1730 LogDebug(VB_MEDIAOUT, "GStreamer: %s -> macOS video window (slot %d)\n", m_videoOut.c_str(), m_streamSlot);
1731 } else {
1732 LogWarn(VB_MEDIAOUT, "GStreamer: no GUI session, so no video window for %s; playing audio only\n", m_videoOut.c_str());
1733 }
1734 #else
1735 // Resolve the connector name
1736
1/2
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
117 std::string connectorName = m_videoOut;
1737
10/14
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 115 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 115 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 115 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 114 times.
✓ Branch 12 taken 3 times.
✓ Branch 13 taken 114 times.
117 if (connectorName == "--HDMI--" || connectorName == "--hdmi--" || connectorName == "HDMI") {
1738
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 connectorName = "HDMI-A-1";
1739 }
1740
1/2
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
117 DrmConnectorInfo drmInfo = ResolveDrmConnector(connectorName);
1741
4/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 2 times.
117 if (drmInfo.connected && drmInfo.connectorId >= 0) {
1742
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 62 times.
128 if (!m_pwVideoSinkName.empty()) {
1743 // PipeWire video routing is active — VideoOutput is a legacy
1744 // setting that shouldn't drive a direct kmssink when PipeWire
1745 // groups define all outputs. Build the video pipeline (so the
1746 // pipewiresink receives decoded frames) but don't create a
1747 // primary kmssink; consumer kmssinks in VideoOutputManager
1748 // (one per group member) own every HDMI connector.
1749 2 wantHDMI = true;
1750
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogDebug(VB_MEDIAOUT, "GStreamer: PipeWire video routing active — ignoring VideoOutput=%s for direct kmssink, consumers handle HDMI\n",
1751 connectorName.c_str());
1752 } else {
1753 62 m_wantHDMI = true;
1754 62 wantHDMI = true;
1755 62 m_hdmiConnectorId = drmInfo.connectorId;
1756
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 m_hdmiCardPath = drmInfo.cardPath;
1757 62 m_hdmiDisplayWidth = drmInfo.displayWidth;
1758 62 m_hdmiDisplayHeight = drmInfo.displayHeight;
1759
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 LogDebug(VB_MEDIAOUT, "GStreamer HDMI output: connector=%s id=%d card=%s resolution=%dx%d\n",
1760 connectorName.c_str(), m_hdmiConnectorId, m_hdmiCardPath.c_str(),
1761 m_hdmiDisplayWidth, m_hdmiDisplayHeight);
1762 }
1763
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 2 times.
53 } else if (!drmInfo.connected) {
1764 // Configured connector not connected — do NOT fall back to
1765 // another connector. Each HDMI output is independently
1766 // configured; stealing a different connector would conflict
1767 // with whatever that connector is supposed to display.
1768
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
51 if (!m_pwVideoSinkName.empty()) {
1769 // PipeWire video routing is active — still build the
1770 // video pipeline (without kmssink) so consumers on
1771 // other connectors can display the stream.
1772 // Don't set m_hdmiDisplayWidth/Height — without a local
1773 // kmssink there's no need to software-scale in the main
1774 // pipeline. Consumer kmssinks do DRM hardware scaling.
1775 48 wantHDMI = true;
1776
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 LogWarn(VB_MEDIAOUT, "GStreamer: %s is not connected — building video pipeline without kmssink (PipeWire consumers will handle output)\n", connectorName.c_str());
1777 } else {
1778
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogWarn(VB_MEDIAOUT, "GStreamer: %s is not connected, disabling video\n", connectorName.c_str());
1779 }
1780 } else {
1781
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogWarn(VB_MEDIAOUT, "GStreamer: could not resolve connector ID for %s\n", connectorName.c_str());
1782 }
1783 #endif
1784 117 } else {
1785 // PixelOverlay model name
1786 12 wantVideo = true;
1787 }
1788 }
1789
1790
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 2084 times.
2181 if (m_preparing) {
1791 // Only a video to the persistent display, or sound alone, is
1792 // prepared: their builds touch nothing the playing track uses. The
1793 // other shapes claim a model, a PipeWire video node, AES67 branches or
1794 // the macOS window as they are built. (Sound alone still has to go
1795 // to the output stream: see below.)
1796 97 bool aes67 = false;
1797 #ifdef HAS_AES67_GSTREAMER
1798 97 aes67 = AES67Manager::INSTANCE.IsActive();
1799 #endif
1800
7/10
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 97 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 95 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 95 times.
97 if (wantVideo || !m_pwVideoSinkName.empty() || m_macVideoWindow || aes67) {
1801
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogDebug(VB_MEDIAOUT, "GStreamer: %s is not a video to the display or sound alone; not preparing it\n",
1802 m_mediaFilename.c_str());
1803 2 return 0;
1804 }
1805 }
1806
1807
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2167 times.
2179 if (wantVideo) {
1808 // Register listener and get model
1809
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 PixelOverlayManager::INSTANCE.addModelListener(m_videoOut, OverlayListenerId(),
1810 12 [this](PixelOverlayModel* m) {
1811
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::lock_guard<std::mutex> lock(m_videoOverlayModelLock);
1812 3 m_videoOverlayModel = m;
1813 3 });
1814
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 PixelOverlayModel* model = PixelOverlayManager::INSTANCE.getModel(m_videoOut);
1815 {
1816 // The listener above and the channel output thread use it too.
1817
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 std::lock_guard<std::mutex> lock(m_videoOverlayModelLock);
1818 12 m_videoOverlayModel = model;
1819 12 }
1820
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (model) {
1821 12 model->getSize(m_videoOverlayWidth, m_videoOverlayHeight);
1822
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 LogDebug(VB_MEDIAOUT, "GStreamer video overlay: model=%s size=%dx%d\n",
1823 m_videoOut.c_str(), m_videoOverlayWidth, m_videoOverlayHeight);
1824 {
1825
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 std::lock_guard<std::mutex> lock(m_overlayOutputsLock);
1826
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 if (std::find(m_overlayOutputs.begin(), m_overlayOutputs.end(), this) == m_overlayOutputs.end()) {
1827
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 m_overlayOutputs.push_back(this);
1828 }
1829 12 }
1830 } else {
1831 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: PixelOverlay model '%s' not found, skipping video\n",
1832 m_videoOut.c_str());
1833 ✗ wantVideo = false;
1834 }
1835 }
1836
1837 // Build the pipeline
1838 // Audio: filesrc ! decodebin ! audioconvert ! audioresample ! tee name=t
1839 // t. ! queue ! volume ! pipewiresink
1840 // t. ! queue ! audioconvert ! F32LE,1ch ! appsink (WLED tap)
1841 // Video (when overlay model available):
1842 // decodebin pad-added -> videoconvert ! videoscale ! capsfilter(RGB,WxH) ! appsink
1843 //
1844 // When video is needed, we must use decodebin's pad-added signal for dynamic linking,
1845 // since decodebin creates pads on-the-fly for each stream type.
1846 // We still use gst_parse_launch for the audio chain and manually add the video chain.
1847
1848
1/2
✓ Branch 0 taken 2179 times.
✗ Branch 1 not taken.
2179 LogDebug(VB_MEDIAOUT, "GStreamer: Start() building pipeline...\n");
1849
1850 2179 bool usePipeWire = usePipeWireBackendLocal;
1851
1852 // Rate to hand the sink, or 0 to pass the file's own rate straight through
1853 // (see ChooseDecodeRate). Shared by all three pipeline shapes below.
1854
1/2
✓ Branch 0 taken 2179 times.
✗ Branch 1 not taken.
2179 int decodeRate = ChooseDecodeRate(mediaRate, usePipeWire);
1855
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2178 times.
2179 if (decodeRate) {
1856
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogDebug(VB_MEDIAOUT, "GStreamer: media rate %d Hz, resampling to the %d Hz graph rate\n",
1857 mediaRate, decodeRate);
1858 } else {
1859
1/2
✓ Branch 0 taken 2178 times.
✗ Branch 1 not taken.
2178 LogDebug(VB_MEDIAOUT, "GStreamer: media rate %d Hz passed through unconstrained\n", mediaRate);
1860 }
1861
1862 2179 std::string pipelineSinkName;
1863
2/2
✓ Branch 0 taken 2176 times.
✓ Branch 1 taken 3 times.
2179 if (usePipeWire) {
1864
1/2
✓ Branch 0 taken 2176 times.
✗ Branch 1 not taken.
2176 pipelineSinkName = PipeWireSinkNameForSlot(m_streamSlot);
1865 }
1866
3/4
✓ Branch 0 taken 2176 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2179 times.
✗ Branch 3 not taken.
2179 LogDebug(VB_MEDIAOUT, "GStreamer: PipeWireSinkName='%s' (slot %d, backend=%s)\n",
1867 pipelineSinkName.c_str(), m_streamSlot, usePipeWire ? "pipewire" : "non-pipewire");
1868
1869 // Log PipeWire group delay for reference (handled natively by PipeWire
1870 // filter-chain delay nodes, not by GStreamer ts-offset).
1871
1/2
✓ Branch 0 taken 2179 times.
✗ Branch 1 not taken.
2179 int maxGroupDelayMs = GetMaxPipeWireGroupDelayMs();
1872
1/2
✓ Branch 0 taken 2179 times.
✗ Branch 1 not taken.
2179 LogDebug(VB_MEDIAOUT, "GStreamer: PipeWire maxGroupDelay=%dms (handled by PipeWire filter-chain)\n",
1873 maxGroupDelayMs);
1874
1875 // PipeWireVideoSinkName was already read above (before connector check)
1876
1877 // Stream identity: use slot number for PipeWire node naming
1878
1/2
✓ Branch 0 taken 2179 times.
✗ Branch 1 not taken.
2179 std::string streamNodeName = StreamSlotManager::GetNodeName(m_streamSlot);
1879
1/2
✓ Branch 0 taken 2179 times.
✗ Branch 1 not taken.
2179 std::string streamNodeDesc = StreamSlotManager::GetNodeDescription(m_streamSlot);
1880 // The slot's persistent output stream carries the same node.name, so a
1881 // pipewiresink of this pipeline's own is also tagged with an id of its
1882 // own, by which ApplyRate() finds its node.
1883 static std::atomic<uint64_t> nextStreamId{ 1 };
1884
1/2
✓ Branch 0 taken 2179 times.
✗ Branch 1 not taken.
2179 const std::string pwSinkStreamId = std::to_string(nextStreamId++);
1885 2179 m_pwSinkStreamId.clear();
1886
1887 2179 std::string sinkStr;
1888
2/2
✓ Branch 0 taken 2164 times.
✓ Branch 1 taken 15 times.
2179 if (!pipelineSinkName.empty()) {
1889 // stream-properties cannot be set inline in gst_parse_launch (GstStructure
1890 // values with spaces break the parser). Set it post-launch instead.
1891 // target-object is set post-launch too (below), never pasted in: it
1892 // comes from the PipeWireSinkName setting, and in the description a
1893 // crafted value added elements that read or wrote files as root.
1894
1/2
✓ Branch 0 taken 2164 times.
✗ Branch 1 not taken.
2164 sinkStr = "pipewiresink name=pwsink sync=true";
1895
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 } else if (usePipeWire) {
1896 // PipeWire backend with no specific sink configured — use pipewiresink
1897 // and let PipeWire route to the default output.
1898
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 sinkStr = "pipewiresink name=pwsink sync=true";
1899 } else {
1900 // Prefer alsasink directly — autoaudiosink probes pulsesink first
1901 // which fails slowly in ALSA-only mode, causing startup delay.
1902 // buffer-time=500000 (500ms) gives USB audio devices adequate ring
1903 // buffer headroom to avoid underruns during initial isochronous
1904 // scheduling. Falls back to autoaudiosink if the gstreamer1.0-alsa
1905 // plugin is not installed (otherwise the pipeline fails to build).
1906
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 const char* alsaSink = GetAlsaDirectSinkName();
1907
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (strcmp(alsaSink, "alsasink") == 0) {
1908
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 sinkStr = "alsasink buffer-time=500000";
1909 } else {
1910 ✗ sinkStr = alsaSink;
1911 }
1912 }
1913
1914 2179 GError* error = nullptr;
1915
1916 // A video's sound (one or two channels) plays through the slot's
1917 // persistent output stream, as an audio file's does, not a pipewiresink of
1918 // its own: that per-track stream ran on the system clock, so the sound
1919 // drifted off the card's own rate; it lost the head of every track to the
1920 // stream coming up; it sat behind the picture and the lights by the
1921 // card's queue, which nothing corrected; and on a MultiSync remote each
1922 // rate change queued sound it then played late (~180 ms on a Pi 5). Run
1923 // on the stream's feed clock, the picture keeps time with the sound -- and
1924 // a remote steers both (AdjustFeedSpeed()). A video with no sound, or
1925 // more than two channels, keeps the old sink.
1926
4/4
✓ Branch 0 taken 2042 times.
✓ Branch 1 taken 134 times.
✓ Branch 2 taken 2017 times.
✓ Branch 3 taken 25 times.
2176 PipeWireOutputStream* videoPwStream = (usePipeWire && mediaChannels >= 1 && mediaChannels <= 2)
1927
3/4
✓ Branch 0 taken 2176 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2017 times.
✗ Branch 3 not taken.
4355 ? PipeWireOutputStream::ForSlot(m_streamSlot)
1928 2179 : nullptr;
1929
1930 // A prepared pipeline must never have an audio sink of its own: waiting
1931 // in PAUSED next to the playing track's, a second pipewiresink on the
1932 // slot held that track short of its end and then wedged its teardown,
1933 // and an alsasink would open the card the playing track holds. Where the
1934 // file's channel count is unknown (taglib reads none from Matroska), the
1935 // sound goes to the output stream for now, and OnPadAdded() checks the
1936 // decoded caps: one or two channels is the case the stream is meant for,
1937 // and anything else leaves the prepared pipeline to be built again.
1938 2179 m_prepareCheckChannels = false;
1939
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 2084 times.
2179 if (m_preparing) {
1940
4/6
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 63 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
95 if (!videoPwStream && usePipeWire && mediaChannels == 0) {
1941
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 videoPwStream = PipeWireOutputStream::ForSlot(m_streamSlot);
1942 32 m_prepareCheckChannels = videoPwStream != nullptr;
1943 }
1944
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (!videoPwStream) {
1945 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: %s would have an audio sink of its own; not preparing it\n",
1946 m_mediaFilename.c_str());
1947 ✗ return 0;
1948 }
1949 }
1950
1951
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2167 times.
2179 if (wantVideo) {
1952 // Build pipeline with decodebin pad-added for dynamic audio+video linking
1953 std::string pipelineStr =
1954
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 "filesrc name=fsrc ! decodebin name=decoder";
1955
1956
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 LogDebug(VB_MEDIAOUT, "GStreamer pipeline (video): %s\n", pipelineStr.c_str());
1957
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 m_pipeline = gst_parse_launch(pipelineStr.c_str(), &error);
1958
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (error) {
1959 ✗ LogErr(VB_MEDIAOUT, "GStreamer pipeline error: %s\n", error->message);
1960 ✗ g_error_free(error);
1961 ✗ return 0;
1962 }
1963
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 SetFileSourceLocation(m_pipeline, fullPath);
1964
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GuardVideoConvertInputs(m_pipeline);
1965
1966 // Build audio sub-chain: audioconvert ! audioresample ! tee ! ...
1967
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstElement* audioconvert = gst_element_factory_make("audioconvert", "aconv");
1968
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstElement* audioresample = gst_element_factory_make("audioresample", "aresample");
1969
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstElement* tee = gst_element_factory_make("tee", "t");
1970
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstElement* queue1 = gst_element_factory_make("queue", "q1");
1971 // Generous buffer for PipeWire connection startup.
1972
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 g_object_set(queue1, "max-size-time", (guint64)(5 * GST_SECOND),
1973 "max-size-buffers", 0, "max-size-bytes", 0, NULL);
1974
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 m_volume = gst_element_factory_make("volume", "vol");
1975 12 GstElement* sink = nullptr;
1976
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (videoPwStream) {
1977
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 sink = gst_element_factory_make("appsink", "pwfeed");
1978
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (sink) {
1979
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 g_object_set(sink, "sync", TRUE, "enable-last-sample", FALSE, NULL);
1980 }
1981
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 } else if (!pipelineSinkName.empty()) {
1982
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 sink = gst_element_factory_make("pipewiresink", "pwsink");
1983
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 g_object_set(sink, "target-object", pipelineSinkName.c_str(), NULL);
1984 // sync=TRUE: gate audio buffer delivery by PTS against
1985 // GstSystemClock (the pipeline clock we force above).
1986 // Both pipewiresink and kmssink sync against the same system
1987 // clock, so A/V stay aligned. No ts-offset needed — PipeWire
1988 // filter-chain delay nodes handle inter-member alignment,
1989 // and PipeWire quantum latency (~21ms) ≈ DRM vsync (~16ms).
1990
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 g_object_set(sink, "sync", TRUE, NULL);
1991 // Name the stream. It is left to PipeWire's channel mixer: with
1992 // stream.dont-remix a 5.1 file on a stereo sink lost its centre,
1993 // LFE and rear channels, and a stereo file on a mono sink its
1994 // right channel. Channel order on a surround sink is kept by
1995 // the pre-permute (IsGstPipeWireChannelOrderQuirky()).
1996
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 GstStructure* props = gst_structure_new("props",
1997 "node.name", G_TYPE_STRING, streamNodeName.c_str(),
1998 "node.description", G_TYPE_STRING, streamNodeDesc.c_str(),
1999 "fpp.stream.id", G_TYPE_STRING, pwSinkStreamId.c_str(),
2000 NULL);
2001
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 g_object_set(sink, "stream-properties", props, NULL);
2002
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 m_pwSinkStreamId = pwSinkStreamId;
2003
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 gst_structure_free(props);
2004 } else {
2005 // Prefer alsasink directly — avoids autoaudiosink probe delay.
2006 // Fall back to autoaudiosink if gstreamer1.0-alsa is missing.
2007 ✗ const char* alsaSinkName = GetAlsaDirectSinkName();
2008 ✗ sink = gst_element_factory_make(alsaSinkName, "audiosink");
2009 ✗ if (sink && strcmp(alsaSinkName, "alsasink") == 0) {
2010 ✗ g_object_set(sink, "buffer-time", (gint64)500000, NULL);
2011 }
2012 }
2013
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstElement* queue2 = gst_element_factory_make("queue", "q2");
2014
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstElement* audioconvert2 = gst_element_factory_make("audioconvert", "aconv2");
2015
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstElement* capsfilterAudio = gst_element_factory_make("capsfilter", "acapsf");
2016
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 m_appsink = gst_element_factory_make("appsink", "sampletap");
2017
2018 // Set audio tap caps: F32LE mono
2019
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstCaps* audioCaps = gst_caps_new_simple("audio/x-raw",
2020 "format", G_TYPE_STRING, "F32LE",
2021 "channels", G_TYPE_INT, 1, NULL);
2022
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 g_object_set(capsfilterAudio, "caps", audioCaps, NULL);
2023
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 gst_caps_unref(audioCaps);
2024
2025 // Configure queues
2026
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 g_object_set(queue2, "max-size-buffers", 3, "leaky", 2 /* downstream */, NULL);
2027
2028 // Configure audio appsink
2029
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 g_object_set(m_appsink, "emit-signals", TRUE, "sync", FALSE,
2030 "max-buffers", 3, "drop", TRUE, NULL);
2031
2032
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstElement* rateCapsfilter = gst_element_factory_make("capsfilter", "ratecaps");
2033 // Feeding the output stream, the sound must arrive in its one fixed
2034 // format (see PipeWireOutputStream::CapsFields()).
2035
7/14
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
34 GstCaps* rateCaps = videoPwStream ? gst_caps_from_string(("audio/x-raw" + PipeWireOutputStream::CapsFields()).c_str())
2036
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
10 : decodeRate ? gst_caps_new_simple("audio/x-raw", "rate", G_TYPE_INT, decodeRate, NULL)
2037
3/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 10 times.
22 : gst_caps_new_empty_simple("audio/x-raw");
2038
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 g_object_set(rateCapsfilter, "caps", rateCaps, NULL);
2039
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 gst_caps_unref(rateCaps);
2040
2041 // Add audio elements to pipeline
2042 // gst_bin_add_many() stops at the first NULL, taking it for the end
2043 // of the list: an element that couldn't be made (no pipewiresink, no
2044 // ALSA sink plugin) left the rest of the chain out of the pipeline,
2045 // floating, and every link after it failed.
2046 {
2047 12 GstElement* parts[] = { audioconvert, audioresample, rateCapsfilter, tee, queue1, m_volume, sink,
2048 12 queue2, audioconvert2, capsfilterAudio, m_appsink };
2049 12 bool missing = false;
2050
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 12 times.
144 for (GstElement* e : parts) {
2051 132 missing |= (e == nullptr);
2052 }
2053
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
12 if (missing) {
2054
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogErr(VB_MEDIAOUT, "GStreamer: could not create the audio chain (a GStreamer plugin is missing)\n");
2055
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
12 for (GstElement* e : parts) {
2056
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 if (e) {
2057
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 gst_object_unref(gst_object_ref_sink(e));
2058 }
2059 }
2060 1 m_volume = nullptr;
2061 1 m_appsink = nullptr;
2062
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 gst_object_unref(m_pipeline);
2063 1 m_pipeline = nullptr;
2064 1 return 0;
2065 }
2066 }
2067
3/6
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
11 gst_bin_add_many(GST_BIN(m_pipeline), audioconvert, audioresample, rateCapsfilter, tee,
2068 queue1, m_volume, sink,
2069 queue2, audioconvert2, capsfilterAudio, m_appsink, NULL);
2070
2071 // Take our own refs on elements we keep pointers to.
2072 // gst_bin_add sinks the floating ref; we need our own ref so Close() can safely unref.
2073
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 gst_object_ref(m_volume);
2074
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 gst_object_ref(m_appsink);
2075
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
11 if (videoPwStream) {
2076
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 m_pwFeed = GST_ELEMENT(gst_object_ref(sink));
2077 2 m_pwStream = videoPwStream;
2078 }
2079
2080 // Link audio chain
2081
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
11 if (!gst_element_link_many(audioconvert, audioresample, rateCapsfilter, tee, NULL)) {
2082 ✗ LogErr(VB_MEDIAOUT, "GStreamer: Failed to link audioconvert->audioresample->ratecaps->tee\n");
2083 }
2084
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
11 if (!gst_element_link_many(queue1, m_volume, sink, NULL)) {
2085 ✗ LogErr(VB_MEDIAOUT, "GStreamer: Failed to link queue1->volume->sink\n");
2086 }
2087
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
11 if (!gst_element_link_many(queue2, audioconvert2, capsfilterAudio, m_appsink, NULL)) {
2088 ✗ LogErr(VB_MEDIAOUT, "GStreamer: Failed to link queue2->audioconvert2->capsfilter->appsink\n");
2089 }
2090
2091 // Link tee to both queues
2092
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 GstPad* teeSrc1 = gst_element_request_pad_simple(tee, "src_%u");
2093
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 GstPad* q1Sink = gst_element_get_static_pad(queue1, "sink");
2094
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 gst_pad_link(teeSrc1, q1Sink);
2095
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 gst_object_unref(teeSrc1);
2096
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 gst_object_unref(q1Sink);
2097
2098
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 GstPad* teeSrc2 = gst_element_request_pad_simple(tee, "src_%u");
2099
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 GstPad* q2Sink = gst_element_get_static_pad(queue2, "sink");
2100
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 gst_pad_link(teeSrc2, q2Sink);
2101
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 gst_object_unref(teeSrc2);
2102
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 gst_object_unref(q2Sink);
2103
2104 // Remember the audio chain entry point for pad-added linkage
2105 11 m_audioChain = audioconvert;
2106
2107 // Attach AES67 zero-hop RTP branches if any send instances are active
2108 #ifdef HAS_AES67_GSTREAMER
2109
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 AttachAES67Branches(tee);
2110 #endif
2111
2112 // Build video sub-chain: videoconvertscale ! capsfilter(RGB,WxH) ! appsink
2113 //
2114 // One videoconvertscale, not videoconvert ! videoscale: the caps
2115 // pin RGB, so videoscale would scale RGB and videoconvert had to
2116 // convert each whole decoded frame to RGB before the scale threw
2117 // nearly all of it away. videoconvertscale scales the decoder's
2118 // frame first and converts only the model-sized result: a 1080p60
2119 // file to a 384x192 model took 183 s of one Pi 5 core at 1.5 GHz
2120 // per 180 s of video, and takes 78. It takes the same formats as
2121 // videoconvert (a Pi 4 decoder's NV12_128C8 included), and
2122 // GuardVideoConvert() guards it the same way. GStreamer before
2123 // 1.22 doesn't have it, and keeps the pair.
2124
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 GstElement* videoconvert = gst_element_factory_make("videoconvertscale", "vconv");
2125 11 GstElement* videoscale = nullptr;
2126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!videoconvert) {
2127 ✗ videoconvert = gst_element_factory_make("videoconvert", "vconv");
2128 ✗ videoscale = gst_element_factory_make("videoscale", "vscale");
2129 }
2130
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 GstElement* capsfilterVideo = gst_element_factory_make("capsfilter", "vcapsf");
2131
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 GstElement* videoQueue = gst_element_factory_make("queue", "vq");
2132
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 m_videoAppsink = gst_element_factory_make("appsink", "videosink");
2133
2134 // Set video caps: RGB at overlay model dimensions
2135
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 GstCaps* videoCaps = gst_caps_new_simple("video/x-raw",
2136 "format", G_TYPE_STRING, "RGB",
2137 "width", G_TYPE_INT, m_videoOverlayWidth,
2138 "height", G_TYPE_INT, m_videoOverlayHeight, NULL);
2139
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 g_object_set(capsfilterVideo, "caps", videoCaps, NULL);
2140
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 gst_caps_unref(videoCaps);
2141
2142 // Configure video appsink: emit signals, sync=TRUE to pace delivery at real-time speed.
2143 // Without sync=TRUE, all frames are consumed immediately and position jumps to EOF,
2144 // falsely triggering stall detection (especially for video-only files).
2145 // drop=TRUE ensures old frames are discarded if ProcessVideoOverlay can't keep up.
2146 //
2147 // qos + max-lateness: a frame that reaches the sink later than that
2148 // is dropped, and the QoS events that go upstream let the converter
2149 // and the decoder skip the frames that would only be late too. An
2150 // appsink renders late frames by default, so a box that couldn't
2151 // decode and convert the video in real time showed every frame
2152 // late: the full queue held up the demuxer, which starved the sound,
2153 // and picture, sound and the lights following them all fell behind
2154 // the clock for good -- seconds over a song. Dropping keeps them on
2155 // time and shows what the box can manage.
2156
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 g_object_set(m_videoAppsink, "emit-signals", TRUE, "sync", TRUE,
2157 "max-buffers", 2, "drop", TRUE,
2158 "qos", TRUE, "max-lateness", (gint64)(VIDEO_OVERLAY_MAX_LATENESS_MS * GST_MSECOND), NULL);
2159
2160 // Add video elements to pipeline
2161
3/6
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
11 gst_bin_add_many(GST_BIN(m_pipeline), videoQueue, videoconvert,
2162 capsfilterVideo, m_videoAppsink, NULL);
2163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (videoscale) {
2164 ✗ gst_bin_add(GST_BIN(m_pipeline), videoscale);
2165 }
2166
2167 // Take our own ref on the video appsink pointer
2168
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 gst_object_ref(m_videoAppsink);
2169
2170 // Link video chain
2171
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
11 bool linked = videoscale ? gst_element_link_many(videoQueue, videoconvert, videoscale, capsfilterVideo,
2172 m_videoAppsink, NULL)
2173
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 : gst_element_link_many(videoQueue, videoconvert, capsfilterVideo,
2174 11 m_videoAppsink, NULL);
2175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!linked) {
2176 ✗ LogErr(VB_MEDIAOUT, "GStreamer: Failed to link video chain\n");
2177 }
2178
2179 // Remember the video chain entry point for pad-added linkage
2180 11 m_videoChain = videoQueue;
2181
2182 // Connect decodebin pad-added signal for dynamic linking
2183
3/6
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
11 GstElement* decoder = gst_bin_get_by_name(GST_BIN(m_pipeline), "decoder");
2184
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 ConnectPadSignals(decoder, true);
2185
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 gst_object_unref(decoder);
2186
2187
4/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 112 times.
✓ Branch 3 taken 2055 times.
2179 } else if (wantHDMI) {
2188 // ---------------------------------------------------------------
2189 // HDMI/DRM video output via kmssink (Phase 4)
2190 // Pipeline: filesrc ! decodebin name=decoder
2191 // Audio: pad-added -> audioconvert ! audioresample ! tee
2192 // tee -> queue ! volume ! pipewiresink
2193 // tee -> queue ! audioconvert ! F32LE,1ch ! appsink (WLED tap)
2194 // Video: pad-added -> queue ! videoconvert ! videoscale ! capsfilter ! kmssink
2195 //
2196 // decodebin auto-selects the best available decoder:
2197 // Pi 5: v4l2slh265dec for H.265, avdec_h264 for H.264
2198 // Pi 4: v4l2 stateless H.264 if kernel supports, else avdec_h264
2199 // Pi 3/Zero2: avdec_h264 software decode
2200 // All platforms: avdec_h264 from gstreamer1.0-libav as universal fallback
2201 // ---------------------------------------------------------------
2202 std::string pipelineStr =
2203
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 "filesrc name=fsrc ! decodebin name=decoder";
2204
2205
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 LogDebug(VB_MEDIAOUT, "GStreamer pipeline (HDMI): %s connector-id=%d card=%s\n",
2206 pipelineStr.c_str(), m_hdmiConnectorId, m_hdmiCardPath.c_str());
2207
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 m_pipeline = gst_parse_launch(pipelineStr.c_str(), &error);
2208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 if (error) {
2209 ✗ LogErr(VB_MEDIAOUT, "GStreamer HDMI pipeline error: %s\n", error->message);
2210 ✗ g_error_free(error);
2211 ✗ return 0;
2212 }
2213
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 SetFileSourceLocation(m_pipeline, fullPath);
2214
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GuardVideoConvertInputs(m_pipeline);
2215
2216 // Build audio sub-chain (same as overlay mode)
2217
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstElement* audioconvert = gst_element_factory_make("audioconvert", "aconv");
2218
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstElement* audioresample = gst_element_factory_make("audioresample", "aresample");
2219
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstElement* tee = gst_element_factory_make("tee", "t");
2220
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstElement* queue1 = gst_element_factory_make("queue", "q1");
2221 // Generous buffer for PipeWire connection startup.
2222
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 g_object_set(queue1, "max-size-time", (guint64)(5 * GST_SECOND),
2223 "max-size-buffers", 0, "max-size-bytes", 0, NULL);
2224
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 m_volume = gst_element_factory_make("volume", "vol");
2225 112 GstElement* sink = nullptr;
2226
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 81 times.
112 if (videoPwStream) {
2227
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 sink = gst_element_factory_make("appsink", "pwfeed");
2228
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (sink) {
2229
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 g_object_set(sink, "sync", TRUE, "enable-last-sample", FALSE, NULL);
2230 }
2231
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 } else if (!pipelineSinkName.empty()) {
2232
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 sink = gst_element_factory_make("pipewiresink", "pwsink");
2233
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 g_object_set(sink, "target-object", pipelineSinkName.c_str(), NULL);
2234 // sync=TRUE: same rationale as the wantVideo branch above.
2235
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 g_object_set(sink, "sync", TRUE, NULL);
2236 // Name the stream; remixing stays on (see the wantVideo branch).
2237
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 GstStructure* props = gst_structure_new("props",
2238 "node.name", G_TYPE_STRING, streamNodeName.c_str(),
2239 "node.description", G_TYPE_STRING, streamNodeDesc.c_str(),
2240 "fpp.stream.id", G_TYPE_STRING, pwSinkStreamId.c_str(),
2241 NULL);
2242
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 g_object_set(sink, "stream-properties", props, NULL);
2243
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 m_pwSinkStreamId = pwSinkStreamId;
2244
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 gst_structure_free(props);
2245 } else {
2246 // Prefer alsasink directly — avoids autoaudiosink probe delay.
2247 // Fall back to autoaudiosink if gstreamer1.0-alsa is missing.
2248 ✗ const char* alsaSinkName = GetAlsaDirectSinkName();
2249 ✗ sink = gst_element_factory_make(alsaSinkName, "audiosink");
2250 ✗ if (sink && strcmp(alsaSinkName, "alsasink") == 0) {
2251 ✗ g_object_set(sink, "buffer-time", (gint64)500000, NULL);
2252 }
2253 }
2254
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstElement* queue2 = gst_element_factory_make("queue", "q2");
2255
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstElement* audioconvert2 = gst_element_factory_make("audioconvert", "aconv2");
2256
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstElement* capsfilterAudio = gst_element_factory_make("capsfilter", "acapsf");
2257
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 m_appsink = gst_element_factory_make("appsink", "sampletap");
2258
2259 // Set audio tap caps: F32LE mono
2260
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstCaps* audioCaps = gst_caps_new_simple("audio/x-raw",
2261 "format", G_TYPE_STRING, "F32LE",
2262 "channels", G_TYPE_INT, 1, NULL);
2263
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 g_object_set(capsfilterAudio, "caps", audioCaps, NULL);
2264
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 gst_caps_unref(audioCaps);
2265
2266
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 g_object_set(queue2, "max-size-buffers", 3, "leaky", 2 /* downstream */, NULL);
2267
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 g_object_set(m_appsink, "emit-signals", TRUE, "sync", FALSE,
2268 "max-buffers", 3, "drop", TRUE, NULL);
2269
2270
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstElement* rateCapsfilter = gst_element_factory_make("capsfilter", "ratecaps");
2271 // Feeding the output stream, the sound must arrive in its one fixed
2272 // format (see PipeWireOutputStream::CapsFields()).
2273
7/14
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 31 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 31 times.
✓ Branch 9 taken 81 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
305 GstCaps* rateCaps = videoPwStream ? gst_caps_from_string(("audio/x-raw" + PipeWireOutputStream::CapsFields()).c_str())
2274
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
81 : decodeRate ? gst_caps_new_simple("audio/x-raw", "rate", G_TYPE_INT, decodeRate, NULL)
2275
3/4
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 81 times.
193 : gst_caps_new_empty_simple("audio/x-raw");
2276
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 g_object_set(rateCapsfilter, "caps", rateCaps, NULL);
2277
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 gst_caps_unref(rateCaps);
2278
2279 // gst_bin_add_many() stops at the first NULL, taking it for the end
2280 // of the list: an element that couldn't be made (no pipewiresink, no
2281 // ALSA sink plugin) left the rest of the chain out of the pipeline,
2282 // floating, and every link after it failed.
2283 {
2284 112 GstElement* parts[] = { audioconvert, audioresample, rateCapsfilter, tee, queue1, m_volume, sink,
2285 112 queue2, audioconvert2, capsfilterAudio, m_appsink };
2286 112 bool missing = false;
2287
2/2
✓ Branch 0 taken 1232 times.
✓ Branch 1 taken 112 times.
1344 for (GstElement* e : parts) {
2288 1232 missing |= (e == nullptr);
2289 }
2290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 if (missing) {
2291 ✗ LogErr(VB_MEDIAOUT, "GStreamer: could not create the audio chain (a GStreamer plugin is missing)\n");
2292 ✗ for (GstElement* e : parts) {
2293 ✗ if (e) {
2294 ✗ gst_object_unref(gst_object_ref_sink(e));
2295 }
2296 }
2297 ✗ m_volume = nullptr;
2298 ✗ m_appsink = nullptr;
2299 ✗ gst_object_unref(m_pipeline);
2300 ✗ m_pipeline = nullptr;
2301 ✗ return 0;
2302 }
2303 }
2304
3/6
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 112 times.
✗ Branch 5 not taken.
112 gst_bin_add_many(GST_BIN(m_pipeline), audioconvert, audioresample, rateCapsfilter, tee,
2305 queue1, m_volume, sink,
2306 queue2, audioconvert2, capsfilterAudio, m_appsink, NULL);
2307
2308
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 gst_object_ref(m_volume);
2309
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 gst_object_ref(m_appsink);
2310
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 81 times.
112 if (videoPwStream) {
2311
3/6
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
31 m_pwFeed = GST_ELEMENT(gst_object_ref(sink));
2312 31 m_pwStream = videoPwStream;
2313 }
2314
2315
2/4
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 112 times.
112 if (!gst_element_link_many(audioconvert, audioresample, rateCapsfilter, tee, NULL)) {
2316 ✗ LogErr(VB_MEDIAOUT, "GStreamer HDMI: Failed to link audioconvert->audioresample->ratecaps->tee\n");
2317 }
2318
2/4
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 112 times.
112 if (!gst_element_link_many(queue1, m_volume, sink, NULL)) {
2319 ✗ LogErr(VB_MEDIAOUT, "GStreamer HDMI: Failed to link queue1->volume->sink\n");
2320 }
2321
2/4
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 112 times.
112 if (!gst_element_link_many(queue2, audioconvert2, capsfilterAudio, m_appsink, NULL)) {
2322 ✗ LogErr(VB_MEDIAOUT, "GStreamer HDMI: Failed to link audio appsink chain\n");
2323 }
2324
2325
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstPad* teeSrc1 = gst_element_request_pad_simple(tee, "src_%u");
2326
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstPad* q1Sink = gst_element_get_static_pad(queue1, "sink");
2327
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 gst_pad_link(teeSrc1, q1Sink);
2328
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 gst_object_unref(teeSrc1);
2329
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 gst_object_unref(q1Sink);
2330
2331
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstPad* teeSrc2 = gst_element_request_pad_simple(tee, "src_%u");
2332
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstPad* q2Sink = gst_element_get_static_pad(queue2, "sink");
2333
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 gst_pad_link(teeSrc2, q2Sink);
2334
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 gst_object_unref(teeSrc2);
2335
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 gst_object_unref(q2Sink);
2336
2337 112 m_audioChain = audioconvert;
2338
2339 // Attach AES67 zero-hop RTP branches if any send instances are active
2340 #ifdef HAS_AES67_GSTREAMER
2341
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 AttachAES67Branches(tee);
2342 #endif
2343
2344 // Build video sub-chain: queue ! videoconvert ! videoscale ! capsfilter ! kmssink
2345 // When PipeWire video routing is active, a tee is inserted before kmssink
2346 // so a deferred pipewiresink can expose the stream in the PipeWire graph.
2347 // Each kmssink runs on its own DRM lease (DrmCards::AcquireLease())
2348 // so multiple HDMI outputs on the same card work simultaneously.
2349 112 bool haveHdmiConnector = (m_hdmiConnectorId >= 0);
2350
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 GstElement* videoQueue = gst_element_factory_make("queue", "vq");
2351
2352
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 50 times.
112 if (haveHdmiConnector) {
2353 #ifdef HAS_VIDEO_DISPLAY
2354 // Without PipeWire video routing the display has nothing else
2355 // hanging off it, so it runs on its own pipeline, which outlives
2356 // this one: between tracks it holds the last frame instead of
2357 // dropping to the console (see VideoDisplay.h). The routed
2358 // shape keeps its kmssink here, on the tee.
2359
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 if (m_pwVideoSinkName.empty()) {
2360
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 VideoDisplay::Config cfg;
2361
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 cfg.cardPath = m_hdmiCardPath;
2362 62 cfg.connectorId = m_hdmiConnectorId;
2363
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 cfg.connectorName = m_videoOut;
2364
9/14
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 60 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 60 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 60 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 60 times.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 60 times.
62 if (cfg.connectorName == "--HDMI--" || cfg.connectorName == "--hdmi--" || cfg.connectorName == "HDMI") {
2365
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 cfg.connectorName = "HDMI-A-1";
2366 }
2367 62 cfg.width = m_hdmiDisplayWidth;
2368 62 cfg.height = m_hdmiDisplayHeight;
2369
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
62 cfg.crop = VideoOutputManager::Instance().GetHdmiCropForConnector(m_streamSlot, m_hdmiConnectorId);
2370
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
62 cfg.scaling = VideoOutputManager::Instance().GetHdmiScalingForConnector(m_streamSlot, m_hdmiConnectorId);
2371 // Prepared, it only joins the display a video is already
2372 // showing on: built now, it would take the connector from
2373 // whatever is there until this one plays.
2374
4/6
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
62 m_display = m_preparing ? VideoDisplay::AcquireRunning(cfg) : VideoDisplay::Acquire(cfg);
2375
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2 times.
62 if (m_display) {
2376
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 m_kmssink = gst_element_factory_make("appsink", "displayfeed");
2377
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (m_kmssink) {
2378
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 m_displayFeedToken = m_display->AttachFeed(m_kmssink, !m_preparing);
2379 } else {
2380 ✗ ReleaseDisplay();
2381 }
2382 } else {
2383
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogWarn(VB_MEDIAOUT, "GStreamer: could not open the persistent display on %s; using a kmssink of this track's own\n",
2384 cfg.connectorName.c_str());
2385 }
2386 62 }
2387
5/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 60 times.
62 if (!m_display && m_preparing) {
2388 // No video showing there now, so nothing to keep up; and a
2389 // kmssink of its own would take the display from what is.
2390
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (m_volume) {
2391
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_object_unref(m_volume);
2392 2 m_volume = nullptr;
2393 }
2394
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (m_appsink) {
2395
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_object_unref(m_appsink);
2396 2 m_appsink = nullptr;
2397 }
2398
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_object_unref(m_pipeline);
2399 2 m_pipeline = nullptr;
2400 2 return 0;
2401 }
2402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!m_display) {
2403 #endif
2404 #ifdef PLATFORM_OSX
2405 // The "kmssink" slot holds whatever drives the primary display;
2406 // here that is glimagesink drawing into the slot's window. The
2407 // view is fetched now, before the pipeline starts, and handed over
2408 // directly - see MacOSShowVideoWindow() for why it must not wait
2409 // for the sink's prepare-window-handle message.
2410 m_kmssink = gst_element_factory_make("glimagesink", "kmsvideosink");
2411 void* view = m_kmssink ? MacOSShowVideoWindow(m_streamSlot) : nullptr;
2412 if (view) {
2413 gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(m_kmssink), (guintptr)view);
2414 } else {
2415 LogWarn(VB_MEDIAOUT, "GStreamer: could not open the macOS video window; video will open in its own window\n");
2416 }
2417 #else
2418 ✗ m_kmssink = gst_element_factory_make("kmssink", "kmsvideosink");
2419 #endif
2420 ✗ if (!m_kmssink) {
2421 ✗ LogErr(VB_MEDIAOUT, "GStreamer: kmssink element not available — is gstreamer1.0-plugins-bad installed?\n");
2422 // Permanent: the element appears only with a package install and a restart.
2423 ✗ WarningHolder::AddWarning(31, "Video output unavailable: kmssink element missing (install gstreamer1.0-plugins-bad)");
2424 // Release the extra refs taken above (~827-828) before discarding
2425 // the pipeline. Close() only unrefs m_volume/m_appsink when
2426 // m_pipeline is still set, and we're about to null it out below,
2427 // so without this those two refs leak every time kmssink is
2428 // unavailable.
2429 ✗ if (m_volume) {
2430 ✗ gst_object_unref(m_volume);
2431 ✗ m_volume = nullptr;
2432 }
2433 ✗ if (m_appsink) {
2434 ✗ gst_object_unref(m_appsink);
2435 ✗ m_appsink = nullptr;
2436 }
2437 ✗ gst_object_unref(m_pipeline);
2438 ✗ m_pipeline = nullptr;
2439 ✗ return 0;
2440 }
2441 #ifndef PLATFORM_OSX
2442 // A persistent consumer on this connector (a camera group) stands
2443 // aside until the video is over. Then a lease on the connector,
2444 // so the KMS framebuffer outputs on the card keep their DRM
2445 // master (see DrmCards.h).
2446 ✗ VideoOutputManager::Instance().ClaimConnectorForMedia(m_hdmiConnectorId);
2447 ✗ m_mediaConnectors.push_back(m_hdmiConnectorId);
2448 ✗ DrmCards::Lease lease = DrmCards::AcquireLease(m_hdmiCardPath, m_hdmiConnectorId);
2449 ✗ if (lease.fd >= 0) {
2450 ✗ g_object_set(m_kmssink,
2451 "fd", lease.fd,
2452 "connector-id", m_hdmiConnectorId,
2453 "restore-crtc", TRUE,
2454 "skip-vsync", TRUE,
2455 NULL);
2456 ✗ if (lease.planeId >= 0) {
2457 ✗ g_object_set(m_kmssink, "plane-id", lease.planeId, NULL);
2458 }
2459 ✗ m_drmLeases.push_back(lease);
2460 } else {
2461 ✗ g_object_set(m_kmssink,
2462 "driver-name", "vc4",
2463 "connector-id", m_hdmiConnectorId,
2464 "restore-crtc", TRUE,
2465 "skip-vsync", TRUE,
2466 NULL);
2467 }
2468 #endif
2469 #ifdef HAS_VIDEO_DISPLAY
2470 ✗ }
2471 #endif
2472 }
2473
2474 // kmssink handles format conversion + scaling in hardware.
2475 // No capsfilter needed — let sinks negotiate with the decoder directly.
2476
2477
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 60 times.
110 if (!m_pwVideoSinkName.empty()) {
2478 // ── PipeWire video routing (tee + deferred pipewiresink) ──
2479 // When HDMI is connected, the primary pipeline drives it via kmssink.
2480 // When HDMI is not connected, vtee routes only to pipewiresink.
2481 // In both cases a tee exposes the stream in the PipeWire graph for
2482 // overlay consumers and secondary HDMI outputs.
2483 50 m_videoPipeWireRouting = true;
2484
2485
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 GstElement* vtee = gst_element_factory_make("tee", "vtee");
2486
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 g_object_set(vtee, "allow-not-linked", TRUE, NULL);
2487 // Each kmssink branch's first element, for IsolateKmsBranches().
2488 50 std::vector<std::pair<GstElement*, std::string>> kmsBranches;
2489
2490 // Link videoQueue directly to vtee — skip videoconvert/videoscale.
2491 // kmssink accepts the HW decoder's native output (NV12 DMA buffers)
2492 // and handles format conversion + scaling in hardware. Putting
2493 // videoconvert/videoscale before the tee forces CPU-side conversion
2494 // of every frame (~100% CPU on Pi4). Paths that need specific
2495 // formats (e.g., pipewiresink) include their own converter.
2496
3/6
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
✗ Branch 5 not taken.
50 gst_bin_add_many(GST_BIN(m_pipeline), videoQueue, vtee, NULL);
2497
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
50 if (!gst_element_link_many(videoQueue, vtee, NULL)) {
2498 ✗ LogErr(VB_MEDIAOUT, "GStreamer video: Failed to link vq → vtee\n");
2499 }
2500
2501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (haveHdmiConnector) {
2502 // Link tee → kmsQueue → kmssink via request pad
2503 ✗ GstElement* kmsQueue = gst_element_factory_make("queue", "vkmsq");
2504 // Passthrough for HW-decoded NV12/DMA; required for software
2505 // decode (I420 won't negotiate against the DRM plane).
2506 ✗ GstElement* teeKmsConvert = gst_element_factory_make("videoconvert", "vteekmsconv");
2507 // The primary display is a configured output like any other:
2508 // it can be one half of a split, and it has its own scaling
2509 // mode, both read from the same config the consumers use.
2510 ✗ std::vector<GstElement*> kmsChain = { kmsQueue, teeKmsConvert };
2511 ✗ for (GstElement* e : BuildHdmiScaleChain(
2512 ✗ VideoOutputManager::Instance().GetHdmiCropForConnector(m_streamSlot, m_hdmiConnectorId),
2513 ✗ VideoOutputManager::Instance().GetHdmiScalingForConnector(m_streamSlot, m_hdmiConnectorId),
2514 ✗ m_hdmiDisplayWidth, m_hdmiDisplayHeight, "vkms"))
2515 ✗ kmsChain.push_back(e);
2516 ✗ kmsChain.push_back(m_kmssink);
2517
2518 ✗ if (!AddAndLinkChain(m_pipeline, kmsChain)) {
2519 ✗ LogErr(VB_MEDIAOUT, "GStreamer: Failed to link primary kmssink chain\n");
2520 }
2521 ✗ kmsBranches.emplace_back(kmsQueue, "primary-" + std::to_string(m_hdmiConnectorId));
2522 ✗ GstPad* teeSrc = gst_element_request_pad_simple(vtee, "src_%u");
2523 ✗ GstPad* kmsQSink = gst_element_get_static_pad(kmsQueue, "sink");
2524 ✗ gst_pad_link(teeSrc, kmsQSink);
2525 ✗ gst_object_unref(teeSrc);
2526 ✗ gst_object_unref(kmsQSink);
2527 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: video tee active — kmssink direct, pipewiresink deferred\n");
2528 ✗ } else {
2529 // No primary HDMI — vtee routes to deferred pipewiresink
2530 // and direct consumer kmssinks linked below.
2531
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 LogDebug(VB_MEDIAOUT, "GStreamer: video tee active — no primary kmssink (HDMI disconnected)\n");
2532 }
2533
2534 // ── Consumer direct kmssink branches ──
2535 // Added to pipeline BEFORE PLAYING so video renders from frame 0.
2536 // Previously these were in the deferred thread, but that caused
2537 // a ~500ms-1s gap where vtee dropped all frames (decoder raced
2538 // ahead and the first rendered frame was already ~1 second in).
2539 {
2540 50 std::set<int> excludeConnectors;
2541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (m_hdmiConnectorId >= 0)
2542 ✗ excludeConnectors.insert(m_hdmiConnectorId);
2543
2544
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 auto hdmiConsumers = VideoOutputManager::Instance().GetHdmiConsumers(
2545
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 m_streamSlot, excludeConnectors);
2546
2547
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 50 times.
62 for (const auto& hc : hdmiConsumers) {
2548 // Resolve the DRM connector live from sysfs — the config
2549 // may have a stale cardPath (e.g. card1 when HDMI is card0).
2550 12 std::string resolvedCard;
2551 12 int resolvedConnId = hc.connectorId;
2552
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (!hc.connector.empty()) {
2553
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto drmCheck = ResolveDrmConnector(hc.connector);
2554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!drmCheck.connected) {
2555 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: skipping direct kmssink for connector %d (%s) — not connected\n",
2556 hc.connectorId, hc.connector.c_str());
2557 ✗ continue;
2558 }
2559
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 resolvedCard = drmCheck.cardPath;
2560
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (drmCheck.connectorId > 0)
2561 2 resolvedConnId = drmCheck.connectorId;
2562
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 }
2563
2564
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 std::string sinkName = "dkms_" + std::to_string(resolvedConnId);
2565
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstElement* dkmsSink = gst_element_factory_make("kmssink", sinkName.c_str());
2566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!dkmsSink) {
2567 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: kmssink not available for connector %d\n", resolvedConnId);
2568 ✗ continue;
2569 }
2570
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 m_dkmsConnectorNames[resolvedConnId] = hc.connector;
2571
2572 // sync=TRUE: pace video rendering to PTS timestamps via
2573 // the PipeWire pipeline clock, keeping A/V in sync.
2574 // max-lateness=-1: NEVER drop frames as "too late".
2575 // On cold start the PipeWire clock can have a startup
2576 // offset (0.91x ratio in logs) causing frames to appear
2577 // late to kmssink. The default max-lateness (5ms) would
2578 // silently drop them → blank screen. With -1, late frames
2579 // are rendered immediately instead of dropped, while early
2580 // frames still wait for their PTS — correct pacing with
2581 // no blanking.
2582 // skip-vsync=TRUE: required for vc4 atomic modesetting
2583 // to avoid double-vsync wait (kmssink + kernel).
2584 // Prefer the live-resolved card path over the config value.
2585 22 std::string cardForDkms = !resolvedCard.empty() ? resolvedCard
2586
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 : !hc.cardPath.empty() ? hc.cardPath
2587
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
22 : m_hdmiCardPath;
2588
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 m_dkmsCardPaths[resolvedConnId] = cardForDkms;
2589 12 DrmCards::Lease lease;
2590
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 if (!cardForDkms.empty()) {
2591
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 VideoOutputManager::Instance().ClaimConnectorForMedia(resolvedConnId);
2592
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 m_mediaConnectors.push_back(resolvedConnId);
2593
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 lease = DrmCards::AcquireLease(cardForDkms, resolvedConnId);
2594 }
2595 12 int drmFd = lease.fd;
2596
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 LogDebug(VB_MEDIAOUT, "GStreamer: dkms_%d cardPath='%s' fd=%d lease=%d\n",
2597 resolvedConnId, cardForDkms.c_str(), drmFd, lease.isLease);
2598
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (drmFd >= 0) {
2599
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 m_drmLeases.push_back(lease);
2600
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 g_object_set(dkmsSink,
2601 "fd", drmFd,
2602 "connector-id", resolvedConnId,
2603 "sync", TRUE,
2604 "max-lateness", (gint64)-1,
2605 "skip-vsync", TRUE,
2606 NULL);
2607 2 int planeId = lease.planeId;
2608
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (planeId >= 0) {
2609
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 g_object_set(dkmsSink, "plane-id", planeId, NULL);
2610 }
2611 // Verify the fd was accepted by kmssink
2612 2 gint readbackFd = -1;
2613
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 g_object_get(dkmsSink, "fd", &readbackFd, NULL);
2614
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogDebug(VB_MEDIAOUT, "GStreamer: dkms_%d fd=%d plane=%d\n",
2615 resolvedConnId, readbackFd, planeId);
2616 } else {
2617
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 g_object_set(dkmsSink,
2618 "driver-name", "vc4",
2619 "connector-id", resolvedConnId,
2620 "sync", TRUE,
2621 "max-lateness", (gint64)-1,
2622 "skip-vsync", TRUE,
2623 NULL);
2624 }
2625
2626
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstElement* dQueue = gst_element_factory_make("queue", nullptr);
2627
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 g_object_set(dQueue,
2628 "max-size-buffers", 3,
2629 "max-size-bytes", 0,
2630 "max-size-time", (guint64)0,
2631 NULL);
2632
2633 // videoconvert negotiates to passthrough for a HW decoder's
2634 // native NV12/DMA (no CPU cost), but is required when the
2635 // decode happened in software -- I420 will not negotiate
2636 // against the DRM plane and the pipeline would fail with
2637 // "not-negotiated". See the direct-kmssink path below.
2638
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstElement* dConvert = gst_element_factory_make("videoconvert", nullptr);
2639
2640 // Crop and scaling for this output. With nothing but a
2641 // crop it sits last before the sink, so kmssink takes it as
2642 // the DRM plane's source rectangle (metadata only, no
2643 // per-frame copy) -- that is what lets one decode of a
2644 // double-wide file feed a different half to each display,
2645 // and what makes "fill" free. "stretch" adds a videoscale
2646 // behind it and does cost a copy.
2647 12 std::vector<GstElement*> dChain;
2648
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 if (GstElement* guard = MakeBranchErrorGuard())
2649
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 dChain.push_back(guard);
2650
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 dChain.push_back(dQueue);
2651
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 dChain.push_back(dConvert);
2652 12 for (GstElement* e : BuildHdmiScaleChain(
2653 12 hc.crop, hc.scaling, hc.width, hc.height,
2654
4/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 12 times.
26 "d_" + std::to_string(resolvedConnId)))
2655
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
14 dChain.push_back(e);
2656
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 dChain.push_back(dkmsSink);
2657
2658
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
12 if (!AddAndLinkChain(m_pipeline, dChain)) {
2659 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: failed to link kmssink chain for connector %d\n",
2660 resolvedConnId);
2661 ✗ continue;
2662 }
2663
2664
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstPad* teeSrc = gst_element_request_pad_simple(vtee, "src_%u");
2665
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstPad* dQSink = gst_element_get_static_pad(dChain.front(), "sink");
2666
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstPadLinkReturn lr = gst_pad_link(teeSrc, dQSink);
2667
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 gst_object_unref(teeSrc);
2668
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 gst_object_unref(dQSink);
2669
2670
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (lr == GST_PAD_LINK_OK) {
2671
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kmsBranches.emplace_back(dQueue, std::to_string(resolvedConnId));
2672
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 m_directConnectorIds.insert(resolvedConnId);
2673
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 LogDebug(VB_MEDIAOUT, "GStreamer: direct kmssink for connector %d (%dx%d) linked to vtee (pre-PLAYING)\n",
2674 resolvedConnId, hc.width, hc.height);
2675 } else {
2676 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: failed to link vtee → kmssink for connector %d (ret=%d)\n",
2677 resolvedConnId, lr);
2678 }
2679
5/10
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
12 }
2680 50 }
2681
2682
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 IsolateKmsBranches(kmsBranches);
2683
2684 // ── Slot channel for the other consumers ──
2685 // Overlay and RTP members of a playback group, and RTSP streams
2686 // aimed at this slot, read the slot's intervideo channel. Nothing
2687 // published it -- the pipewiresink branch below is only for the
2688 // PipeWire graph -- so they never got a frame. Only built when one
2689 // of them exists: HDMI members are fed directly above, and the
2690 // conversion costs CPU on every frame. Converting here, in this
2691 // pipeline, also keeps hardware-decoded frames (and their device
2692 // memory) out of the consumers' pipelines: this videoconvert has
2693 // the copy guard every converter in this pipeline gets.
2694
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 bool slotChannelWanted = VideoOutputManager::Instance().HasSlotChannelConsumers(m_streamSlot);
2695 #ifdef HAS_RTSP_OUTPUT_GSTREAMER
2696
5/6
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 10 times.
50 slotChannelWanted = slotChannelWanted || RTSPOutputManager::INSTANCE.ServesStreamSlot(m_streamSlot);
2697 #endif
2698
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 10 times.
50 if (slotChannelWanted) {
2699
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 std::string channel = StreamSlotManager::GetVideoNodeName(m_streamSlot);
2700
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 GstElement* chQueue = gst_element_factory_make("queue", "vchq");
2701
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 GstElement* chConvert = gst_element_factory_make("videoconvert", "vchconv");
2702
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 GstElement* chCaps = gst_element_factory_make("capsfilter", "vchcaps");
2703
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 GstElement* chSink = gst_element_factory_make("intervideosink", "vchsink");
2704
4/8
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 40 times.
✗ Branch 7 not taken.
40 if (chQueue && chConvert && chCaps && chSink) {
2705
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 g_object_set(chQueue, "leaky", 2, "max-size-buffers", 2,
2706 "max-size-bytes", 0, "max-size-time", (guint64)0, NULL);
2707
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 GstCaps* i420 = gst_caps_from_string("video/x-raw,format=I420");
2708
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 g_object_set(chCaps, "caps", i420, NULL);
2709
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 gst_caps_unref(i420);
2710 // sync: with no display there is no kmssink, and a file with
2711 // no audio track loses the audio sink too, so this may be the
2712 // only sink left to hold playback to the clock; without it the
2713 // file decodes as fast as it can and is over before a consumer
2714 // has started. The leaky queue keeps a waiting sink from ever
2715 // backing up the tee.
2716
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 g_object_set(chSink, "channel", channel.c_str(), "sync", TRUE, NULL);
2717
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
80 std::vector<GstElement*> chain = { chQueue, chConvert, chCaps, chSink };
2718 40 GstPad* teeSrc = nullptr;
2719 40 GstPad* qSink = nullptr;
2720
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (AddAndLinkChain(m_pipeline, chain) &&
2721
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
40 (teeSrc = gst_element_request_pad_simple(vtee, "src_%u")) &&
2722
4/8
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 40 times.
✗ Branch 7 not taken.
120 (qSink = gst_element_get_static_pad(chQueue, "sink")) &&
2723
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
40 gst_pad_link(teeSrc, qSink) == GST_PAD_LINK_OK) {
2724
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 LogDebug(VB_MEDIAOUT, "GStreamer: video published on channel %s\n", channel.c_str());
2725 } else {
2726 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: could not publish video on channel %s\n", channel.c_str());
2727 }
2728
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
40 if (teeSrc) gst_object_unref(teeSrc);
2729
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
40 if (qSink) gst_object_unref(qSink);
2730 40 } else {
2731 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: intervideosink unavailable, video groups' overlay/RTP/RTSP outputs get no video\n");
2732 ✗ for (GstElement* e : { chQueue, chConvert, chCaps, chSink }) {
2733 ✗ if (e) gst_object_unref(e);
2734 }
2735 }
2736 40 }
2737
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
110 } else if (haveHdmiConnector) {
2738 // ── Direct kmssink (no PipeWire video routing) ──
2739 // videoQueue → videoconvert → kmssink. A hardware decoder hands us
2740 // NV12/DMA that kmssink takes directly, and in that case videoconvert
2741 // negotiates to passthrough and costs nothing -- so this keeps the
2742 // "no CPU-side conversion" property that matters on a Pi4. But when
2743 // the decode is in SOFTWARE the format is typically I420, which the
2744 // DRM plane will not accept, and without a converter the whole
2745 // pipeline dies with "not-negotiated". That is not hypothetical: a
2746 // Pi5 has no hardware H.264 decoder at all, so every H.264 video
2747 // takes the software path and HDMI output failed outright.
2748
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 GstElement* kmsConvert = gst_element_factory_make("videoconvert", "vkmsconv");
2749 // The crop and scaling configured for this display still apply with
2750 // no PipeWire routing -- a single-output split (one half shown, the
2751 // other discarded) is legitimate, and neither setting should
2752 // silently do nothing just because nothing else is consuming the
2753 // stream.
2754
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
120 std::vector<GstElement*> kmsChain = { videoQueue, kmsConvert };
2755 // The persistent display applies them itself, ahead of its sink.
2756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!m_display) {
2757 ✗ for (GstElement* e : BuildHdmiScaleChain(
2758 ✗ VideoOutputManager::Instance().GetHdmiCropForConnector(m_streamSlot, m_hdmiConnectorId),
2759 ✗ VideoOutputManager::Instance().GetHdmiScalingForConnector(m_streamSlot, m_hdmiConnectorId),
2760 ✗ m_hdmiDisplayWidth, m_hdmiDisplayHeight, "vkms"))
2761 ✗ kmsChain.push_back(e);
2762 }
2763
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 kmsChain.push_back(m_kmssink);
2764
2765
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 60 times.
60 if (!AddAndLinkChain(m_pipeline, kmsChain)) {
2766 ✗ LogErr(VB_MEDIAOUT, "GStreamer HDMI: Failed to link video chain\n");
2767 }
2768 60 }
2769
2770 110 m_videoChain = videoQueue;
2771 110 m_hasVideoStream = true; // expect video stream from decodebin
2772
2773 // Connect decodebin pad-added signal for dynamic linking
2774
3/6
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 110 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 110 times.
✗ Branch 5 not taken.
110 GstElement* decoder = gst_bin_get_by_name(GST_BIN(m_pipeline), "decoder");
2775
1/2
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
110 ConnectPadSignals(decoder, true);
2776
1/2
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
110 gst_object_unref(decoder);
2777
2778
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 2 times.
112 } else {
2779 // Audio-only pipeline (original gst_parse_launch approach)
2780
1/2
✓ Branch 0 taken 2055 times.
✗ Branch 1 not taken.
2055 LogDebug(VB_MEDIAOUT, "GStreamer: Building audio-only pipeline\n");
2781 // expose-all-streams=false + audio caps makes decodebin discard any
2782 // video stream instead of auto-plugging a decoder for it. Without
2783 // this, playing an mp4 audio-only still spins up the bcm2835 hardware
2784 // H.264 decoder for a video branch nothing consumes; tearing that down
2785 // leaves vb2 buffers active and sprays kernel WARN stack dumps
2786 // (videobuf2-core "driver bug: stop_streaming...") on every stop.
2787 // Multichannel + quirky gst-pipewire plugin: pin the layout and
2788 // pre-permute the samples into the (wrong) PulseAudio channel order
2789 // the plugin will declare, so the labels PipeWire routes by actually
2790 // match the content. Self-disabling: once a fixed plugin is
2791 // installed the probe returns false and no permute is inserted.
2792 2055 std::string chOrderCaps, chOrderPermute;
2793
4/6
✓ Branch 0 taken 2052 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2046 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2046 times.
2052 if (usePipeWire && (mediaChannels == 5 || mediaChannels == 6 || mediaChannels == 8)
2794
6/8
✓ Branch 0 taken 2052 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 2049 times.
4107 && IsGstPipeWireChannelOrderQuirky()) {
2795
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 BuildChannelOrderFix(mediaChannels, chOrderCaps, chOrderPermute);
2796
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 LogWarn(VB_MEDIAOUT, "GStreamer: applying %dch channel-order workaround for quirky gst-pipewire plugin\n",
2797 mediaChannels);
2798 }
2799
8/14
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2054 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2054 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2054 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 2054 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
6163 std::string rateCaps = decodeRate ? ",rate=" + std::to_string(decodeRate) : "";
2800 // On PipeWire, play through the slot's persistent output stream rather
2801 // than a pipewiresink of our own: a new PipeWire stream per track is
2802 // most of a second of silence on slow boards (see
2803 // PipeWireOutputStream.h). The appsink syncs, so this pipeline still
2804 // paces the audio.
2805 //
2806 // That stream's format is fixed for its lifetime (pipewiresink never
2807 // renegotiates a connected stream -- new caps are taken as a relabel,
2808 // so 16-bit samples would play as floats: full-scale noise), so this
2809 // pipeline converts to it. Files with more than two channels keep a
2810 // pipewiresink of their own, where the channel-order workaround above
2811 // applies.
2812
2/2
✓ Branch 0 taken 2027 times.
✓ Branch 1 taken 25 times.
2052 PipeWireOutputStream* pwStream = (usePipeWire && mediaChannels <= 2)
2813
3/4
✓ Branch 0 taken 2052 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2027 times.
✗ Branch 3 not taken.
4107 ? PipeWireOutputStream::ForSlot(m_streamSlot)
2814 2055 : nullptr;
2815
2/2
✓ Branch 0 taken 2027 times.
✓ Branch 1 taken 28 times.
2055 if (pwStream) {
2816
1/2
✓ Branch 0 taken 2027 times.
✗ Branch 1 not taken.
2027 rateCaps = PipeWireOutputStream::CapsFields();
2817 }
2818
6/10
✓ Branch 0 taken 2027 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 2027 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2027 times.
✓ Branch 7 taken 28 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
4082 std::string audioSinkStr = pwStream ? std::string("appsink name=pwfeed sync=true enable-last-sample=false") : sinkStr;
2819 std::string pipelineStr =
2820 "filesrc name=fsrc ! decodebin name=decoder expose-all-streams=false caps=\"audio/x-raw\" ! audioconvert ! audioresample ! "
2821
4/8
✓ Branch 0 taken 2055 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2055 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2055 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2055 times.
✗ Branch 7 not taken.
4110 "audio/x-raw" + rateCaps + chOrderCaps + " ! " + chOrderPermute +
2822 "tee name=t "
2823
2/4
✓ Branch 0 taken 2055 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2055 times.
✗ Branch 3 not taken.
4110 "t. ! queue ! volume name=vol ! " + audioSinkStr + " "
2824 "t. ! queue name=q2 max-size-buffers=3 leaky=downstream ! "
2825 "audioconvert ! audio/x-raw,format=F32LE,channels=1 ! "
2826
1/2
✓ Branch 0 taken 2055 times.
✗ Branch 1 not taken.
2055 "appsink name=sampletap emit-signals=true sync=false max-buffers=3 drop=true";
2827
2828
1/2
✓ Branch 0 taken 2055 times.
✗ Branch 1 not taken.
2055 LogDebug(VB_MEDIAOUT, "GStreamer pipeline: %s\n", pipelineStr.c_str());
2829
2830
1/2
✓ Branch 0 taken 2055 times.
✗ Branch 1 not taken.
2055 LogDebug(VB_MEDIAOUT, "GStreamer: Calling gst_parse_launch()...\n");
2831
1/2
✓ Branch 0 taken 2055 times.
✗ Branch 1 not taken.
2055 m_pipeline = gst_parse_launch(pipelineStr.c_str(), &error);
2832
1/2
✓ Branch 0 taken 2055 times.
✗ Branch 1 not taken.
2055 LogDebug(VB_MEDIAOUT, "GStreamer: gst_parse_launch() returned (pipeline=%p, error=%p)\n", m_pipeline, error);
2833
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2054 times.
2055 if (error) {
2834
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogErr(VB_MEDIAOUT, "GStreamer pipeline error: %s\n", error->message);
2835
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 g_error_free(error);
2836 1 return 0;
2837 }
2838
1/2
✓ Branch 0 taken 2054 times.
✗ Branch 1 not taken.
2054 SetFileSourceLocation(m_pipeline, fullPath);
2839
2840 // This pipeline's only sink is audio, so a file with nothing decodable
2841 // to feed it cannot play at all -- and the caps filter above turns that
2842 // into an autoplug failure decodebin reports as a missing plug-in, which
2843 // sends people off installing packages that were never the problem.
2844 // Watch the pads so the real reason can be given instead; the linking
2845 // itself stays with gst_parse_launch's own delayed-link handler.
2846 2054 m_audioOnlyPipeline = true;
2847
4/8
✓ Branch 0 taken 2054 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2054 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2054 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2054 times.
✗ Branch 7 not taken.
2054 if (GstElement* fbDecoder = gst_bin_get_by_name(GST_BIN(m_pipeline), "decoder")) {
2848
1/2
✓ Branch 0 taken 2054 times.
✗ Branch 1 not taken.
2054 ConnectPadSignals(fbDecoder, false);
2849
2/2
✓ Branch 0 taken 2026 times.
✓ Branch 1 taken 28 times.
2054 if (pwStream) {
2850
1/2
✓ Branch 0 taken 2026 times.
✗ Branch 1 not taken.
2026 g_signal_connect(fbDecoder, "autoplug-query", G_CALLBACK(PreferFloatDecode), nullptr);
2851 }
2852
1/2
✓ Branch 0 taken 2054 times.
✗ Branch 1 not taken.
2054 gst_object_unref(fbDecoder);
2853 }
2854
2855 // Get the volume element for later control
2856
3/6
✓ Branch 0 taken 2054 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2054 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2054 times.
✗ Branch 5 not taken.
2054 m_volume = gst_bin_get_by_name(GST_BIN(m_pipeline), "vol");
2857
2858 // Get the appsink
2859
3/6
✓ Branch 0 taken 2054 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2054 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2054 times.
✗ Branch 5 not taken.
2054 m_appsink = gst_bin_get_by_name(GST_BIN(m_pipeline), "sampletap");
2860
2861
2/2
✓ Branch 0 taken 2026 times.
✓ Branch 1 taken 28 times.
2054 if (pwStream) {
2862
3/6
✓ Branch 0 taken 2026 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2026 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2026 times.
✗ Branch 5 not taken.
2026 m_pwFeed = gst_bin_get_by_name(GST_BIN(m_pipeline), "pwfeed");
2863
1/2
✓ Branch 0 taken 2026 times.
✗ Branch 1 not taken.
2026 if (m_pwFeed) {
2864 2026 m_pwStream = pwStream;
2865 }
2866 }
2867
2868 // Set stream-properties on pipewiresink (must be done post-launch;
2869 // gst_parse_launch can't deserialize GstStructure with spaced values).
2870 // Always set when using PipeWire backend, to name the stream.
2871
5/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2051 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 2051 times.
✓ Branch 5 taken 3 times.
2054 if (usePipeWire || !pipelineSinkName.empty()) {
2872
3/6
✓ Branch 0 taken 2051 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2051 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2051 times.
✗ Branch 5 not taken.
2051 GstElement* pwsink = gst_bin_get_by_name(GST_BIN(m_pipeline), "pwsink");
2873
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2026 times.
2051 if (pwsink) {
2874
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 times.
25 if (!pipelineSinkName.empty()) {
2875
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 g_object_set(pwsink, "target-object", pipelineSinkName.c_str(), NULL);
2876 }
2877
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 GstStructure* props = gst_structure_new("props",
2878 "node.name", G_TYPE_STRING, streamNodeName.c_str(),
2879 "node.description", G_TYPE_STRING, streamNodeDesc.c_str(),
2880 "fpp.stream.id", G_TYPE_STRING, pwSinkStreamId.c_str(),
2881 NULL);
2882
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 g_object_set(pwsink, "stream-properties", props, NULL);
2883
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 m_pwSinkStreamId = pwSinkStreamId;
2884
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 gst_structure_free(props);
2885
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 gst_object_unref(pwsink);
2886 }
2887 }
2888
2889 // Attach AES67 zero-hop RTP branches to the audio tee
2890 #ifdef HAS_AES67_GSTREAMER
2891 {
2892
3/6
✓ Branch 0 taken 2054 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2054 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2054 times.
✗ Branch 5 not taken.
2054 GstElement* tee = gst_bin_get_by_name(GST_BIN(m_pipeline), "t");
2893
1/2
✓ Branch 0 taken 2054 times.
✗ Branch 1 not taken.
2054 if (tee) {
2894
1/2
✓ Branch 0 taken 2054 times.
✗ Branch 1 not taken.
2054 AttachAES67Branches(tee);
2895
1/2
✓ Branch 0 taken 2054 times.
✗ Branch 1 not taken.
2054 gst_object_unref(tee);
2896 }
2897 }
2898 #endif
2899
10/10
✓ Branch 0 taken 2054 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2054 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2054 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2054 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 2054 times.
✓ Branch 9 taken 1 times.
2059 }
2900
2901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2175 times.
2175 if (!m_pipeline) {
2902 ✗ LogErr(VB_MEDIAOUT, "Failed to create GStreamer pipeline\n");
2903 // Per track: the next one tries again.
2904 ✗ WarningHolder::AddWarningTimeout(60, 31, "Failed to create GStreamer media pipeline");
2905 ✗ return 0;
2906 }
2907
2908 // Connect audio appsink callback
2909 2175 m_shutdownFlag.store(false);
2910
1/2
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
2175 if (m_appsink) {
2911
1/2
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
2175 m_appsinkSignalId = g_signal_connect(m_appsink, "new-sample", G_CALLBACK(OnNewSample), this);
2912 // Only WLED's audio-reactive effects read the tap, and most shows run
2913 // none, yet the branch costs a thread, a downmix and a copy of every
2914 // buffer -- a few percent of a single-core board. Drop its buffers at
2915 // the branch's queue unless something has read recently. The sink
2916 // then may never see a buffer, so it must not hold up preroll.
2917
1/2
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
2175 g_object_set(m_appsink, "async", FALSE, NULL);
2918
4/8
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2175 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2175 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2175 times.
✗ Branch 7 not taken.
2175 if (GstElement* tapQueue = gst_bin_get_by_name(GST_BIN(m_pipeline), "q2")) {
2919
2/4
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2175 times.
✗ Branch 3 not taken.
2175 if (GstPad* tapIn = gst_element_get_static_pad(tapQueue, "sink")) {
2920
1/2
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
2175 gst_pad_add_probe(tapIn, (GstPadProbeType)(GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_BUFFER_LIST),
2921 GateSampleTap, nullptr, nullptr);
2922
1/2
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
2175 gst_object_unref(tapIn);
2923 }
2924
1/2
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
2175 gst_object_unref(tapQueue);
2925 }
2926
1/2
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
2175 LogDebug(VB_MEDIAOUT, "GStreamer audio sample tap connected\n");
2927 } else {
2928 ✗ m_appsinkSignalId = 0;
2929 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: could not find sampletap appsink element\n");
2930 }
2931
2932 // Connect video appsink callback
2933
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 2164 times.
2175 if (m_videoAppsink) {
2934
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 m_videoAppsinkSignalId = g_signal_connect(m_videoAppsink, "new-sample",
2935 G_CALLBACK(OnNewVideoSample), this);
2936 11 m_hasVideoStream = true;
2937
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 LogDebug(VB_MEDIAOUT, "GStreamer video appsink connected\n");
2938 } else {
2939 2164 m_videoAppsinkSignalId = 0;
2940 }
2941
2942 // Compute the target volume (1.0 unless dB adjustment is set)
2943 2175 double targetVolume = m_fileGain;
2944
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2175 times.
2175 if (m_volumeAdjust != 0) {
2945 ✗ targetVolume *= pow(10.0, m_volumeAdjust / 2000.0); // volAdj is in 0.01dB units
2946 }
2947 // The volume element rejects anything over 10.0 (+20 dB) and keeps the
2948 // old value -- mid-ramp that would leave the song stuck at a fraction.
2949 2175 targetVolume = std::min(targetVolume, 10.0);
2950
2951 // Start muted — the background thread will ramp up to targetVolume
2952 // after the pipeline reaches PLAYING. This eliminates audible clicks
2953 // caused by USB/ALSA/PipeWire sink initialisation transients.
2954 //
2955 // Not when feeding the persistent output stream: it is already running,
2956 // so there is no sink start-up to hide -- and the ramp is timed by the
2957 // wall clock, not the audio, so it mutes whatever the pipeline prerolled.
2958 // A cold start prerolls while it waits for the stream, and that first
2959 // decoded buffer (~90 ms of the song) went out silent.
2960
1/2
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
2175 if (m_volume) {
2961
3/4
✓ Branch 0 taken 2057 times.
✓ Branch 1 taken 118 times.
✓ Branch 2 taken 2175 times.
✗ Branch 3 not taken.
2175 g_object_set(m_volume, "volume", m_pwStream ? targetVolume : 0.0, NULL);
2962 }
2963
2964 // Get the bus for message handling
2965
1/2
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
2175 LogDebug(VB_MEDIAOUT, "GStreamer: Getting bus and setting sync handler...\n");
2966
1/2
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
2175 m_bus = gst_element_get_bus(m_pipeline);
2967
2968 // Install sync handler for autonomous bus message processing
2969 // This allows GStreamer playback to work without external Process() calls.
2970 // It runs on streaming threads, so it gets the lifetime guard rather than
2971 // `this`; the bus drops that reference once no call is in flight.
2972
2/6
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2175 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2175 gst_bus_set_sync_handler(m_bus, BusSyncHandler,
2973
2/4
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2175 times.
✗ Branch 3 not taken.
2175 new std::shared_ptr<CallbackGuard>(m_cbGuard.load()),
2974 ReleaseBusCallbackGuard);
2975
2976 // Force the pipeline to use GstSystemClock instead of auto-selecting
2977 // the PipeWire clock. The PipeWire clock (provided by the audio
2978 // pipewiresink) is frozen at 0 on cold start — it does not begin
2979 // ticking until PipeWire's graph processes the first audio quantum,
2980 // which can be delayed hundreds of milliseconds. During that window
2981 // kmssink's sync=TRUE blocks forever in gst_clock_id_wait() because
2982 // every frame PTS is "in the future" relative to clock time 0.
2983 // Diagnostics confirmed: rendered=3 (preroll only) across 160 s of
2984 // playback, clock_time=0 at first Process(), dkms_43 state=PAUSED.
2985 //
2986 // With GstSystemClock the system monotonic clock drives sync:
2987 // - Video kmssink paces frames correctly from the first frame.
2988 // - Audio pipewiresink (sync=TRUE) delivers buffers at wall-clock
2989 // rate; PipeWire's internal quantum scheduling handles actual HW
2990 // playout timing, so audio stays correct.
2991 // - Any drift between the system clock and PipeWire's graph clock
2992 // is negligible over typical media durations (sub-ms per minute).
2993 //
2994 // Audio-only pipelines are excluded, because there the trade is all cost
2995 // and no benefit: with no kmssink there is nothing to wedge, and a running
2996 // wall clock costs the head of every file. base_time is latched when the
2997 // pipeline reaches PLAYING, but pipewiresink prerolls before its PipeWire
2998 // stream is actually streaming, so everything the sink renders during that
2999 // gap is already late and never reaches the graph. Measured on a BBB with
3000 // an I2S cape, playing a 10 s chirp and capturing the sink monitor: 240 ms
3001 // to 760 ms of the start missing, run to run. Leaving pipewiresink's own
3002 // clock in place -- it does not advance until the stream runs, so no buffer
3003 // is ever late -- brings that down to one quantum (~20 ms).
3004 //
3005 // A pipeline feeding the persistent output stream has no pipewiresink of
3006 // its own. It runs on the stream's card clock: a monotonic clock like the
3007 // system's, but held to the rate the card actually plays at, so the feed
3008 // delivers exactly what the card takes (the "sub-ms per minute" above is
3009 // not true of real cards -- see PipeWireOutputStream::DisciplineClock()).
3010 // Video in the same pipeline then keeps time with its own sound. (It is
3011 // the stream's feed clock: the card clock, unless a MultiSync remote
3012 // steers this track's speed -- see AdjustFeedSpeed().)
3013
2/2
✓ Branch 0 taken 2057 times.
✓ Branch 1 taken 118 times.
2175 if (m_pwStream) {
3014
1/2
✓ Branch 0 taken 2057 times.
✗ Branch 1 not taken.
2057 GstClock* cardClock = m_pwStream->FeedClock();
3015
3/6
✓ Branch 0 taken 2057 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2057 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2057 times.
✗ Branch 5 not taken.
2057 gst_pipeline_use_clock(GST_PIPELINE(m_pipeline), cardClock);
3016
1/2
✓ Branch 0 taken 2057 times.
✗ Branch 1 not taken.
2057 gst_object_unref(cardClock);
3017
1/2
✓ Branch 0 taken 2057 times.
✗ Branch 1 not taken.
2057 LogDebug(VB_MEDIAOUT, "GStreamer: pipeline clock is the output stream's card clock\n");
3018
4/4
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 81 times.
✓ Branch 3 taken 28 times.
118 } else if (wantVideo || wantHDMI) {
3019 // A monotonic system clock of its own, not the process-wide one:
3020 // ApplyRate() recalibrates the pipeline's clock.
3021
5/10
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 90 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 90 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 90 times.
✗ Branch 9 not taken.
90 GstClock* sysClock = GST_CLOCK(gst_object_ref_sink(
3022 g_object_new(GST_TYPE_SYSTEM_CLOCK, "clock-type", GST_CLOCK_TYPE_MONOTONIC, NULL)));
3023
3/6
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 90 times.
✗ Branch 5 not taken.
90 gst_pipeline_use_clock(GST_PIPELINE(m_pipeline), sysClock);
3024
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 gst_object_unref(sysClock);
3025
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 LogDebug(VB_MEDIAOUT, "GStreamer: forced pipeline clock to GstSystemClock\n");
3026 }
3027
3028 // Flush AES67 send pipelines just before PLAYING so the drop probe
3029 // catches any PipeWire graph transition artifacts (combine-stream
3030 // gaining a new source). The Close()-time flush is consumed by
3031 // silence buffers during idle, so this Start()-time flush is the
3032 // one that actually protects the AES67 receivers.
3033 #ifdef HAS_AES67_GSTREAMER
3034 // Not while preparing (the playing track's sends), nor for a track that
3035 // carries on the stream's timeline: its sound runs on behind the last
3036 // one's with no transition to hide (StartPrepared() and the start thread
3037 // see to those).
3038
8/8
✓ Branch 0 taken 2082 times.
✓ Branch 1 taken 93 times.
✓ Branch 2 taken 1529 times.
✓ Branch 3 taken 553 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1527 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 2173 times.
2175 if (!m_preparing && !m_flushesDeferred && AES67Manager::INSTANCE.IsActive()) {
3039
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 AES67Manager::INSTANCE.FlushSendPipelines();
3040 }
3041 #endif
3042
3043
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 2082 times.
2175 if (m_preparing) {
3044 // Decode up to the first frame now; StartPrepared() does the rest.
3045 // The thread holds its own ref, as Go()'s does, and puts a cancelled
3046 // pipeline back to NULL itself (see the RefGuard there).
3047
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 m_preparedSinkName = pipelineSinkName;
3048 93 m_preparedTargetVolume = targetVolume;
3049 93 m_prepared = true; // before the bus can carry an error
3050
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 m_startThreadCancel = std::make_shared<std::atomic<bool>>(false);
3051 93 std::shared_ptr<std::atomic<bool>> cancel = m_startThreadCancel;
3052
3/6
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 93 times.
✗ Branch 5 not taken.
93 GstElement* pipeline = GST_ELEMENT(gst_object_ref(m_pipeline));
3053
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 std::string name = m_mediaFilename;
3054
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 uint64_t t0 = GetMonotonicTimeMS();
3055 // On a remote, how far this track is to start in or held (see
3056 // m_startAdjustMs). Only a remote ever learns one (AdjustFeedSpeed()),
3057 // and only for tracks that start prepared: the same path each time,
3058 // so the same start-up delay, and a sound through the slot's stream
3059 // to measure it by.
3060 93 m_startAdjustMs = 0;
3061 93 m_startSeekAdjMs = 0;
3062 93 m_startErrPending = false;
3063 93 m_preparedSeek.reset();
3064
3/6
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 93 times.
✗ Branch 5 not taken.
93 if (m_pwFeed && m_streamSlot >= 1 && m_streamSlot <= StreamSlotManager::MAX_SLOTS) {
3065 93 m_startAdjustMs = s_remoteStartAdjustMs[m_streamSlot].load();
3066 93 m_startErrPending = true;
3067
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (m_startAdjustMs > 0) {
3068 ✗ m_preparedSeek = std::make_shared<PreparedSeek>();
3069 }
3070 }
3071 93 std::shared_ptr<PreparedSeek> preparedSeek = m_preparedSeek;
3072 93 int prepSeekMs = m_startAdjustMs;
3073 // Here, before Prepare() returns, not on the thread below: a claim
3074 // can come at once (an insert in an entry's last moments, a remote's
3075 // Open right behind its Prepare), and Go()'s thread could then reach
3076 // PLAYING first, for this to put the pipeline back to PAUSED, frozen.
3077 // (Prepare() runs on its own thread, and PAUSED returns at once;
3078 // only waiting for the preroll takes time.)
3079
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 gst_element_set_state(m_pipeline, GST_STATE_PAUSED);
3080
1/8
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
279 std::thread([pipeline, cancel, name, t0, preparedSeek, prepSeekMs]() {
3081
2/4
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93 times.
✗ Branch 3 not taken.
93 SetThreadName("FPP-GstPrepare");
3082 93 GstState state = GST_STATE_NULL;
3083
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 gst_element_get_state(pipeline, &state, nullptr, (GstClockTime)PREROLL_TIMEOUT_MS * GST_MSECOND);
3084
3/6
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 93 times.
✗ Branch 5 not taken.
93 LogDebug(VB_MEDIAOUT, "GStreamer: prepared %s in %d ms (%s)\n", name.c_str(),
3085 (int)(GetMonotonicTimeMS() - t0), gst_element_state_get_name(state));
3086
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 93 times.
93 if (preparedSeek && state == GST_STATE_PAUSED && !cancel->load()) {
3087 // Held only while the seek is sent (its preroll runs on after,
3088 // and Go() waits for a preroll anyway): a claim meanwhile
3089 // either finds it made or makes it itself, never both.
3090 ✗ bool sought = false;
3091 {
3092 ✗ std::lock_guard<std::mutex> seekLock(preparedSeek->lock);
3093 ✗ if (preparedSeek->state == PreparedSeek::Pending &&
3094 ✗ gst_element_seek_simple(pipeline, GST_FORMAT_TIME,
3095 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
3096 ✗ (gint64)prepSeekMs * GST_MSECOND)) {
3097 ✗ preparedSeek->state = PreparedSeek::Done;
3098 ✗ sought = true;
3099 }
3100 ✗ }
3101 ✗ if (sought) {
3102 ✗ gst_element_get_state(pipeline, nullptr, nullptr, (GstClockTime)PREROLL_TIMEOUT_MS * GST_MSECOND);
3103 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: %s will start %d ms in, as the last track's sound was behind the master\n",
3104 name.c_str(), prepSeekMs);
3105 }
3106 }
3107
3/12
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 93 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 93 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
93 if (cancel->load() && !SetPipelineToNullSupervised(pipeline, PIPELINE_TEARDOWN_TIMEOUT_MS)) {
3108 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: cancelled prepare could not return its pipeline to NULL, abandoning it\n");
3109 }
3110
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 gst_object_unref(pipeline);
3111
2/4
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93 times.
✗ Branch 3 not taken.
186 }).detach();
3112 93 return 1;
3113 93 }
3114
1/2
✓ Branch 0 taken 2082 times.
✗ Branch 1 not taken.
2082 return Go(msTime, pipelineSinkName, targetVolume);
3115 2263 }
3116
3117 // What Prepare() set aside, now that the prepared output is to play.
3118 79 int GStreamerOutput::StartPrepared() {
3119 79 LogDebug(VB_MEDIAOUT, "GStreamer: starting %s, prepared\n", m_mediaFilename.c_str());
3120 79 StreamSlotManager::Instance().DetachOutput(m_streamSlot, this);
3121 79 m_stopRequested = false;
3122 79 StreamSlotManager::Instance().ClaimSlot(m_streamSlot, this);
3123 79 m_claimedSlot = true;
3124 79 m_soundEndMs = 0;
3125 79 m_flushesDeferred = PipeWireOutputStream::SlotMayCarryOn(m_streamSlot);
3126
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 49 times.
79 if (!m_flushesDeferred) {
3127 30 StartFlushes();
3128 }
3129
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 18 times.
79 if (m_preparedTotalSecs >= 0) {
3130
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 std::lock_guard<std::mutex> lock(MediaOutputStatus::FieldsLock());
3131 61 m_mediaOutputStatus->minutesTotal = m_preparedTotalSecs / 60;
3132 61 m_mediaOutputStatus->secondsTotal = m_preparedTotalSecs % 60;
3133 61 }
3134 #ifdef HAS_VIDEO_DISPLAY
3135
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 78 times.
79 if (m_releaseDisplayPending.exchange(false)) {
3136 1 ReleaseDisplay(); // it turned out to have no picture
3137 }
3138
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 64 times.
79 if (m_display) {
3139 15 m_display->ActivateFeed(m_displayFeedToken);
3140 }
3141 #endif
3142 // The start adjustment Prepare() took (m_startAdjustMs): a seek the
3143 // prepare thread hasn't made yet is Go()'s to make.
3144 79 m_startSeekAdjMs = 0;
3145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 if (m_preparedSeek) {
3146 ✗ std::lock_guard<std::mutex> seekLock(m_preparedSeek->lock);
3147 ✗ if (m_preparedSeek->state != PreparedSeek::Done) {
3148 ✗ m_preparedSeek->state = PreparedSeek::Taken;
3149 ✗ m_startSeekAdjMs = m_startAdjustMs;
3150 }
3151 ✗ }
3152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 if (m_startAdjustMs < 0) {
3153 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: %s starts %d ms late, as the last track's sound was ahead of the master\n",
3154 m_mediaFilename.c_str(), -m_startAdjustMs);
3155 }
3156 79 return Go(0, m_preparedSinkName, m_preparedTargetVolume);
3157 }
3158
3159 98 bool GStreamerOutput::Prepare() {
3160 #ifdef HAS_VIDEO_DISPLAY
3161
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 std::lock_guard<std::recursive_mutex> opLock(m_opMutex);
3162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
98 if (m_pipeline) {
3163 ✗ return m_prepared;
3164 }
3165 98 m_preparing = true;
3166
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 int ok = Start(0);
3167 98 m_preparing = false;
3168
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 93 times.
98 if (!ok) {
3169
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Close();
3170 5 return false;
3171 }
3172 93 return true;
3173 #else
3174 return false;
3175 #endif
3176 98 }
3177
3178 2161 int GStreamerOutput::Go(int msTime, const std::string& pipelineSinkName, double targetVolume) {
3179 // A prepared pipeline has prerolled, so it knows already when the file
3180 // has no sound (no-more-pads found none): nothing to feed the stream,
3181 // and no reason to wait for it to open and drain before playing.
3182
6/6
✓ Branch 0 taken 2043 times.
✓ Branch 1 taken 118 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2041 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2159 times.
2161 if (m_pwStream && m_pwFeedOrphaned.exchange(false)) {
3183 2 LogDebug(VB_MEDIAOUT, "GStreamer: %s has no sound; not using the output stream\n", m_mediaFilename.c_str());
3184 2 ReleasePipeWireStream(); // not acquired yet: only drops our refs
3185 }
3186
2/2
✓ Branch 0 taken 2041 times.
✓ Branch 1 taken 120 times.
2161 if (m_pwStream) {
3187 2041 m_pwStream->Acquire(pipelineSinkName);
3188 2041 m_pwStream->AttachFeed(m_pwFeed);
3189 2041 m_pwStreamAcquired = true;
3190 }
3191 2161 m_feedsStream = m_pwStream != nullptr;
3192
3193 // Move the potentially-blocking state transition to a background thread.
3194 // pipewiresink's READY→PAUSED blocks in pw_thread_loop_wait if its
3195 // target PipeWire node doesn't exist yet, which would hang the calling
3196 // HTTP handler thread and make the entire fppd API unresponsive.
3197 2161 m_playing = true;
3198 // Anchor the preroll watchdog (Process()): if the pipeline never reaches a
3199 // playing position, this is the reference point for declaring it wedged.
3200 2161 m_playStartMs = GetMonotonicTimeMS();
3201
3202 {
3203 // A remote's learnt start adjustment (m_startAdjustMs): started that
3204 // far in (if the prepare thread hasn't sought there already), or held
3205 // that long before it plays.
3206 2161 int seekMs = msTime + m_startSeekAdjMs;
3207 2161 int holdMs = std::max(-m_startAdjustMs, 0);
3208 2161 bool videoPW = m_videoPipeWireRouting;
3209 2161 int streamSlot = m_streamSlot;
3210
3211 // The thread must NOT capture `this`: it may still be running (blocked in
3212 // gst_element_set_state / pipewiresink) when this GStreamerOutput is torn
3213 // down and freed, and any `this->member` access would be a use-after-free.
3214 // Capture owning refs on the GStreamer objects we touch (pipeline already
3215 // ref'd above; volume ref'd here) plus a shared cancellation token; Stop()
3216 // sets the token so a fast stop aborts the ramp/attach early.
3217 // A prepared output keeps its prepare thread's token, so that a Stop()
3218 // cancels both threads.
3219
6/6
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 2080 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 79 times.
✓ Branch 4 taken 2082 times.
✓ Branch 5 taken 79 times.
2161 if (!m_startThreadCancel || m_startThreadCancel->load()) {
3220
1/2
✓ Branch 0 taken 2082 times.
✗ Branch 1 not taken.
2082 m_startThreadCancel = std::make_shared<std::atomic<bool>>(false);
3221 }
3222 2161 std::shared_ptr<std::atomic<bool>> cancel = m_startThreadCancel;
3223
1/2
✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
2161 m_startSeekPending = std::make_shared<std::atomic<bool>>(seekMs > 0);
3224 2161 std::shared_ptr<std::atomic<bool>> seekPending = m_startSeekPending;
3225 2161 GstElement* pipeline = m_pipeline;
3226
1/2
✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
2161 gst_object_ref(pipeline);
3227
5/8
✓ Branch 0 taken 2159 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2159 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2159 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2159 times.
✗ Branch 7 not taken.
2161 GstElement* volume = m_volume ? GST_ELEMENT(gst_object_ref(m_volume)) : nullptr;
3228 // A per-slot singleton that is never freed, so safe to capture.
3229 2161 PipeWireOutputStream* pwStream = m_pwStream;
3230 // Only a pipeline whose clocked sinks are the stream's feed and the
3231 // persistent display may carry on the stream's timeline: the start
3232 // thread shifts exactly those (see m_timelineShiftNs).
3233
1/2
✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
2161 m_timelineShiftNs = std::make_shared<std::atomic<int64_t>>(0);
3234 2161 std::shared_ptr<std::atomic<int64_t>> timelineShift = m_timelineShiftNs;
3235
1/2
✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
2161 m_timelineHandover = std::make_shared<TimelineHandover>();
3236 2161 std::shared_ptr<TimelineHandover> handover = m_timelineHandover;
3237
7/8
✓ Branch 0 taken 2041 times.
✓ Branch 1 taken 120 times.
✓ Branch 2 taken 2040 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2038 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 2038 times.
✗ Branch 7 not taken.
2161 bool mayContinue = m_pwFeed && !m_videoPipeWireRouting && !m_videoAppsink && m_directConnectorIds.empty();
3238 2161 GstElement* displayFeed = nullptr;
3239 {
3240 // OnNoMorePads() can remove the kmssink on a streaming thread.
3241
1/2
✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
2161 std::shared_ptr<CallbackGuard> guard = m_cbGuard.load();
3242 2161 std::unique_lock<std::recursive_mutex> guardLock;
3243
1/2
✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
2161 if (guard) {
3244
1/2
✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
2161 guardLock = std::unique_lock<std::recursive_mutex>(guard->mtx);
3245 }
3246
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 2114 times.
2161 if (m_kmssink) {
3247 #ifdef HAS_VIDEO_DISPLAY
3248
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 if (m_display) {
3249
3/6
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 47 times.
✗ Branch 5 not taken.
47 displayFeed = GST_ELEMENT(gst_object_ref(m_kmssink));
3250 // Behind its sound from the first frame, by what the last
3251 // track's was (the start thread adds a carry-on's shift).
3252
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 int64_t base = (m_streamSlot >= 1 && m_streamSlot <= StreamSlotManager::MAX_SLOTS)
3253
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
141 ? s_pictureDelayBaseNs[m_streamSlot].load() : 0;
3254
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
47 if (base > 0 && m_pwFeed) {
3255
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 g_object_set(m_kmssink, "ts-offset", (gint64)base, NULL);
3256 3 m_pictureDelayNs = base;
3257 }
3258 } else
3259 #endif
3260 {
3261 ✗ mayContinue = false;
3262 }
3263 }
3264 2161 }
3265
5/8
✓ Branch 0 taken 2038 times.
✓ Branch 1 taken 123 times.
✓ Branch 2 taken 2038 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2038 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2038 times.
✗ Branch 7 not taken.
2161 GstElement* pwFeed = mayContinue ? GST_ELEMENT(gst_object_ref(m_pwFeed)) : nullptr;
3266 2161 bool flushesDeferred = m_flushesDeferred;
3267 2161 m_flushesDeferred = false;
3268 // See VideoOutputManager::ProducerGeneration(): consumers are started
3269 // from this thread, possibly after this output has been stopped.
3270
1/2
✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
2161 const uint64_t producerGeneration = VideoOutputManager::Instance().ProducerGeneration();
3271
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
4322 std::thread([pipeline, volume, cancel, seekPending, seekMs, holdMs, videoPW, targetVolume,
3272 streamSlot, pwStream, timelineShift, handover, pwFeed, displayFeed,
3273 producerGeneration, flushesDeferred]() {
3274 // Releases our owning refs on exit no matter which path we return on.
3275 //
3276 // A pipeline must never lose its last ref above NULL: disposing it
3277 // with streaming threads still running frees decodebin's elements
3278 // under them. Stop() takes the pipeline to NULL and drops its own
3279 // refs, but nothing stops this thread from raising the state again
3280 // afterwards -- it checks `cancel` and then calls set_state(), and
3281 // Stop() can land in between. When that happens this ref is the
3282 // last one, so a cancelled start puts the pipeline back to NULL
3283 // itself before letting go.
3284 struct RefGuard {
3285 GstElement* pipeline;
3286 GstElement* volume;
3287 GstElement* pwFeed;
3288 GstElement* displayFeed;
3289 std::shared_ptr<std::atomic<bool>> cancel;
3290 2147 ~RefGuard() {
3291
2/2
✓ Branch 0 taken 1262 times.
✓ Branch 1 taken 885 times.
3409 if (cancel->load() &&
3292
6/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1261 times.
✓ Branch 2 taken 1262 times.
✓ Branch 3 taken 885 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 2146 times.
3409 !SetPipelineToNullSupervised(pipeline, PIPELINE_TEARDOWN_TIMEOUT_MS)) {
3293 1 LogWarn(VB_MEDIAOUT, "GStreamer: cancelled start could not return its pipeline to NULL, abandoning it\n");
3294 }
3295
2/2
✓ Branch 0 taken 2145 times.
✓ Branch 1 taken 2 times.
2147 if (volume) gst_object_unref(volume);
3296
2/2
✓ Branch 0 taken 2026 times.
✓ Branch 1 taken 121 times.
2147 if (pwFeed) gst_object_unref(pwFeed);
3297
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 2102 times.
2147 if (displayFeed) gst_object_unref(displayFeed);
3298 2147 gst_object_unref(pipeline);
3299 2147 }
3300 2161 } refGuard{pipeline, volume, pwFeed, displayFeed, cancel};
3301
3302 // Starting part-way in (a playlist entry resuming after Insert
3303 // Playlist Immediate) needs the seek to land before playback
3304 // starts. A seek sent while the PLAYING transition is still
3305 // ASYNC is dropped: it travels upstream from the sinks, and the
3306 // audio chain is not linked to decodebin until its pad-added
3307 // fires, so gst_element_seek_simple() returns FALSE. The media
3308 // then played from 0 while the sequence resumed at the paused
3309 // frame, and the channel output servo, seeing the lights minutes
3310 // ahead of the audio, slowed output to a standstill until the
3311 // audio caught up. Preroll in PAUSED (decodebin is linked by
3312 // then), seek, and only then go to PLAYING.
3313
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2147 times.
2161 if (seekMs > 0) {
3314
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 LogDebug(VB_MEDIAOUT, "GStreamer: Prerolling to start at %dms...\n", seekMs);
3315
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 GstStateChangeReturn pret = gst_element_set_state(pipeline, GST_STATE_PAUSED);
3316
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (pret != GST_STATE_CHANGE_FAILURE) {
3317
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 gst_element_get_state(pipeline, nullptr, nullptr,
3318 (GstClockTime)PREROLL_TIMEOUT_MS * GST_MSECOND);
3319 }
3320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (cancel->load()) {
3321 ✗ seekPending->store(false);
3322 ✗ return;
3323 }
3324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!gst_element_seek_simple(pipeline, GST_FORMAT_TIME,
3325 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
3326
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 (gint64)seekMs * GST_MSECOND)) {
3327 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: seek to %dms failed, media will play from the start\n", seekMs);
3328 }
3329 14 seekPending->store(false);
3330 }
3331
3332 2161 bool continued = false;
3333
2/2
✓ Branch 0 taken 2041 times.
✓ Branch 1 taken 120 times.
2161 if (pwStream) {
3334 // Preroll (decoder start-up) while the output stream comes up.
3335 // Nothing may go to PLAYING before the stream reaches the
3336 // card: PipeWire runs a freshly linked chain on a placeholder
3337 // driver while the ALSA node is still opening, and anything
3338 // played then is lost.
3339
1/2
✓ Branch 0 taken 2041 times.
✗ Branch 1 not taken.
2041 gst_element_set_state(pipeline, GST_STATE_PAUSED);
3340
1/2
✓ Branch 0 taken 2041 times.
✗ Branch 1 not taken.
2041 uint64_t waitStart = GetMonotonicTimeMS();
3341
3/4
✓ Branch 0 taken 2029 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 2016 times.
2041 if (!pwStream->WaitReady(PW_STREAM_READY_TIMEOUT_MS)) {
3342
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 LogWarn(VB_MEDIAOUT, "GStreamer: PipeWire output stream %d not ready after %d ms, starting anyway\n",
3343 streamSlot, PW_STREAM_READY_TIMEOUT_MS);
3344
3/4
✓ Branch 0 taken 2016 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
✓ Branch 3 taken 1840 times.
2016 } else if (GetMonotonicTimeMS() - waitStart > 5) {
3345
2/4
✓ Branch 0 taken 176 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
✗ Branch 3 not taken.
176 LogDebug(VB_MEDIAOUT, "GStreamer: waited %d ms for PipeWire output stream %d\n",
3346 (int)(GetMonotonicTimeMS() - waitStart), streamSlot);
3347 }
3348 // A track starting right behind another carries on the
3349 // stream's timeline where it can: its clock's base time is
3350 // where the queue ends, so its first sample plays right after
3351 // the last one queued, and its position maps onto the sound
3352 // exactly as the last track's did. It must have prerolled
3353 // first, to hand over its first buffer on time.
3354 //
3355 // Otherwise it queues behind what that one left in the stream;
3356 // start the clock only once that has played, or the position
3357 // leads the sound by that much.
3358 2029 GstState prerolled = GST_STATE_NULL;
3359 6084 if (pwFeed &&
3360
3/4
✓ Branch 0 taken 2026 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1848 times.
✓ Branch 3 taken 178 times.
2026 gst_element_get_state(pipeline, &prerolled, nullptr,
3361 1848 (GstClockTime)PW_STREAM_PREROLL_WAIT_MS * GST_MSECOND) == GST_STATE_CHANGE_SUCCESS &&
3362
8/8
✓ Branch 0 taken 2026 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1812 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 1805 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 1805 times.
✓ Branch 7 taken 224 times.
4055 prerolled == GST_STATE_PAUSED && !cancel->load()) {
3363 1805 GstClockTime base = GST_CLOCK_TIME_NONE;
3364 1805 int64_t early = 0;
3365
1/2
✓ Branch 0 taken 1805 times.
✗ Branch 1 not taken.
1805 std::unique_lock<std::mutex> handoverLock(handover->lock);
3366
6/8
✓ Branch 0 taken 1805 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1805 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
✓ Branch 5 taken 1780 times.
✓ Branch 6 taken 25 times.
✓ Branch 7 taken 1780 times.
1805 if (!handover->ended && pwStream->ContinueTimeline(&base, &early)) {
3367 // The first sample is due at `base`, and is handed
3368 // over `early` before it: a base that much earlier,
3369 // and the feed's ts-offset that much less negative
3370 // (see m_timelineShiftNs). The picture is held back
3371 // by the same to match.
3372
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 int64_t lead = PipeWireOutputStream::FeedLeadNs();
3373
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 gst_element_set_start_time(pipeline, GST_CLOCK_TIME_NONE);
3374
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 gst_element_set_base_time(pipeline, base - early + (GstClockTime)holdMs * GST_MSECOND);
3375
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 g_object_set(pwFeed, "ts-offset", (gint64)(early - lead), NULL);
3376
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 24 times.
25 if (displayFeed) {
3377 1 gint64 held = 0;
3378
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 g_object_get(displayFeed, "ts-offset", &held, NULL);
3379
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 g_object_set(displayFeed, "ts-offset", (gint64)(held + early), NULL);
3380 }
3381 25 timelineShift->store(early);
3382 25 continued = true;
3383 }
3384 1805 }
3385
2/2
✓ Branch 0 taken 2004 times.
✓ Branch 1 taken 25 times.
2029 if (!continued) {
3386
2/2
✓ Branch 0 taken 575 times.
✓ Branch 1 taken 1429 times.
2004 if (flushesDeferred) {
3387 // Expected to carry on, and didn't: between drained
3388 // tracks after all.
3389
1/2
✓ Branch 0 taken 575 times.
✗ Branch 1 not taken.
575 StartFlushes();
3390 }
3391
1/2
✓ Branch 0 taken 2004 times.
✗ Branch 1 not taken.
2004 pwStream->WaitDrained(PW_STREAM_DRAIN_MAX_MS);
3392 }
3393
2/2
✓ Branch 0 taken 1254 times.
✓ Branch 1 taken 775 times.
2029 if (cancel->load()) {
3394 1254 return;
3395 }
3396 }
3397
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 895 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
895 if (holdMs > 0 && !continued) {
3398 ✗ uint64_t until = GetMonotonicTimeMS() + holdMs;
3399 ✗ while (!cancel->load() && GetMonotonicTimeMS() < until) {
3400 ✗ std::this_thread::sleep_for(std::chrono::milliseconds(2));
3401 }
3402 ✗ if (cancel->load()) {
3403 ✗ return;
3404 }
3405 }
3406
3407
1/2
✓ Branch 0 taken 895 times.
✗ Branch 1 not taken.
895 LogDebug(VB_MEDIAOUT, "GStreamer: Setting pipeline to PLAYING...\n");
3408
1/2
✓ Branch 0 taken 895 times.
✗ Branch 1 not taken.
895 GstStateChangeReturn ret = gst_element_set_state(pipeline, GST_STATE_PLAYING);
3409
1/2
✓ Branch 0 taken 895 times.
✗ Branch 1 not taken.
895 LogDebug(VB_MEDIAOUT, "GStreamer: set_state returned %d\n", ret);
3410
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 870 times.
895 if (continued) {
3411 // Once it is PLAYING, the pipeline keeps its own time again:
3412 // a pause or a flushing seek works out a new base time as
3413 // usual. With no start time it would keep this one, and its
3414 // position would jump by however long it was paused.
3415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret == GST_STATE_CHANGE_ASYNC) {
3416 ✗ gst_element_get_state(pipeline, nullptr, nullptr, (GstClockTime)PREROLL_TIMEOUT_MS * GST_MSECOND);
3417 }
3418
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 gst_element_set_start_time(pipeline, 0);
3419 }
3420
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 894 times.
895 if (ret == GST_STATE_CHANGE_FAILURE) {
3421 // Don't touch the (possibly freed) object — failure is reported
3422 // to the rest of FPP via the GStreamer bus ERROR message
3423 // (ProcessMessages clears m_playing) and the stall watchdog.
3424
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogErr(VB_MEDIAOUT, "Failed to set GStreamer pipeline to PLAYING\n");
3425
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
3 WarningHolder::AddWarningTimeout(60, 30, "Could not start media playback (pipeline failed to start)");
3426 1 return;
3427 }
3428
3429 // Fade volume from 0 to target over ~50ms to eliminate startup
3430 // clicks from sink initialisation transients. (Not needed, and
3431 // harmful, when feeding the persistent stream -- see Start().)
3432
8/8
✓ Branch 0 taken 892 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 117 times.
✓ Branch 3 taken 775 times.
✓ Branch 4 taken 116 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 116 times.
✓ Branch 7 taken 778 times.
894 if (volume && !pwStream && !cancel->load()) {
3433 116 constexpr int kRampSteps = 10;
3434 116 constexpr int kRampStepUs = 5000; // 5ms per step = 50ms total
3435
5/6
✓ Branch 0 taken 1160 times.
✓ Branch 1 taken 116 times.
✓ Branch 2 taken 1160 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1160 times.
✓ Branch 5 taken 116 times.
1276 for (int i = 1; i <= kRampSteps && !cancel->load(); i++) {
3436 1160 double v = targetVolume * ((double)i / kRampSteps);
3437
1/2
✓ Branch 0 taken 1160 times.
✗ Branch 1 not taken.
1160 g_object_set(volume, "volume", v, NULL);
3438
1/2
✓ Branch 0 taken 1160 times.
✗ Branch 1 not taken.
1160 std::this_thread::sleep_for(std::chrono::microseconds(kRampStepUs));
3439 }
3440
1/2
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
116 if (!cancel->load())
3441
1/2
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
116 g_object_set(volume, "volume", targetVolume, NULL);
3442 }
3443
3444 // Confirm the async transition actually completed. ASYNC only means
3445 // "in progress": if a sink never prerolls, the pipeline sits in
3446 // PAUSED forever, produces silence, and posts NOTHING on the bus, so
3447 // nothing above notices. That is exactly what a PipeWire daemon
3448 // restart under a live fppd leaves behind -- the GStreamer PipeWire
3449 // plugin caches its daemon connection process-wide, so every later
3450 // pipewiresink reuses a dead one. Process()'s preroll watchdog,
3451 // which the playlist and the slot pump (Play Media) both run, has
3452 // the same timeout and usually gets there first: it stops the
3453 // output, which cancels this wait, so it raises the same warning.
3454
2/2
✓ Branch 0 taken 891 times.
✓ Branch 1 taken 3 times.
894 if (!cancel->load()) {
3455 891 GstState startedState = GST_STATE_NULL;
3456
1/2
✓ Branch 0 taken 890 times.
✗ Branch 1 not taken.
891 GstStateChangeReturn scr = gst_element_get_state(pipeline, &startedState, nullptr,
3457 (GstClockTime)PREROLL_TIMEOUT_MS * GST_MSECOND);
3458
3/4
✓ Branch 0 taken 884 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 884 times.
890 if (scr != GST_STATE_CHANGE_SUCCESS || startedState != GST_STATE_PLAYING) {
3459
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 if (!cancel->load()) {
3460 // FAILURE and "still not there after the timeout" are
3461 // different faults and want different advice, but this
3462 // reported both as the stale-PipeWire-connection case.
3463 // An element that errors out returns FAILURE straight
3464 // away, so the message claimed a 15-second timeout it
3465 // had not waited for and blamed a PipeWire restart that
3466 // had not happened -- for any failure at all, including
3467 // a media file the pipeline simply could not play. The
3468 // bus error handler has already logged the real cause in
3469 // that case, so say nothing more than the outcome here.
3470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (scr == GST_STATE_CHANGE_FAILURE) {
3471 ✗ LogErr(VB_MEDIAOUT,
3472 "GStreamer: pipeline failed to start (state=%s) — see the error logged above "
3473 "for the cause.\n",
3474 gst_element_state_get_name(startedState));
3475 } else {
3476
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 LogErr(VB_MEDIAOUT,
3477 "GStreamer: pipeline never reached PLAYING within %dms (state=%s) — output will "
3478 "be silent. A PipeWire restart under a running fppd does this; fppd must be "
3479 "restarted to reconnect.\n",
3480 PREROLL_TIMEOUT_MS, gst_element_state_get_name(startedState));
3481
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.
4 WarningHolder::AddWarningTimeout(60, 30, kMediaDidNotStartWarning);
3482 }
3483 }
3484 }
3485 }
3486
3487 // Deferred: attach pipewiresink to video tee and start consumer
3488 // pipelines. Pipewiresink must be added AFTER the pipeline has
3489 // fully reached PLAYING because gst_element_sync_state_with_parent
3490 // on a new element during a pending state transition can stall.
3491
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 845 times.
893 if (videoPW) {
3492 GstState state;
3493
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
48 gst_element_get_state(pipeline, &state, nullptr, 10 * GST_SECOND);
3494
5/6
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 45 times.
47 if (state < GST_STATE_PLAYING || cancel->load()) {
3495
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogWarn(VB_MEDIAOUT, "GStreamer: Pipeline not PLAYING (state=%d), skipping pipewiresink\n", state);
3496 3 return;
3497 }
3498
3499 // The consumers read the slot's intervideo channel, which the
3500 // pipeline has published since before PLAYING; none of them
3501 // reads the PipeWire node attached below. Start them now:
3502 // waiting for that node (2 s for a sink that nothing links,
3503 // so never prerolls) left the first seconds of every video
3504 // out of the overlay and RTP members.
3505
1/2
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
45 std::string videoNodeName = StreamSlotManager::GetVideoNodeName(streamSlot);
3506
2/4
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
45 VideoOutputManager::Instance().StartConsumers(videoNodeName, producerGeneration);
3507
3508
3/6
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45 times.
✗ Branch 5 not taken.
45 GstElement* vtee = gst_bin_get_by_name(GST_BIN(pipeline), "vtee");
3509
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 44 times.
45 if (!vtee) {
3510
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogWarn(VB_MEDIAOUT, "GStreamer: vtee not found, cannot attach pipewiresink\n");
3511 1 return;
3512 }
3513
3514
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 GstElement* pwvideosink = gst_element_factory_make("pipewiresink", "pwvideosink");
3515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (!pwvideosink) {
3516 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: pipewiresink not available\n");
3517 ✗ gst_object_unref(vtee);
3518 ✗ return;
3519 }
3520
3521
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 std::string videoNodeDesc = StreamSlotManager::GetVideoNodeDescription(streamSlot);
3522
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 GstStructure* vprops = gst_structure_new("props",
3523 "media.class", G_TYPE_STRING, "Stream/Output/Video",
3524 "node.name", G_TYPE_STRING, videoNodeName.c_str(),
3525 "node.description", G_TYPE_STRING, videoNodeDesc.c_str(),
3526 "node.autoconnect", G_TYPE_BOOLEAN, FALSE,
3527 "node.always-process", G_TYPE_BOOLEAN, TRUE,
3528 NULL);
3529
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 g_object_set(pwvideosink, "stream-properties", vprops, NULL);
3530
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 gst_structure_free(vprops);
3531
3532
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 g_object_set(pwvideosink, "async", FALSE, "sync", FALSE, NULL);
3533 // Nothing links this node, so it never negotiates a format and
3534 // never leaves READY. A bin waits for EOS from every element
3535 // flagged as a sink, and it never posts one: playback ran past
3536 // the end of the file until the "media reached end" watchdog
3537 // stopped it, 5-10 s later. It is a tap for the PipeWire
3538 // graph, not an output, so the pipeline doesn't count it.
3539 44 GST_OBJECT_FLAG_UNSET(pwvideosink, GST_ELEMENT_FLAG_SINK);
3540
3541
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 GstElement* pwQueue = gst_element_factory_make("queue", "vpwq");
3542
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 g_object_set(pwQueue,
3543 "leaky", 2,
3544 "max-size-buffers", 2,
3545 "max-size-bytes", 0,
3546 "max-size-time", (guint64)0,
3547 NULL);
3548
3549
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 gst_bin_add_many(GST_BIN(pipeline), pwQueue, pwvideosink, NULL);
3550
2/4
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
44 if (!gst_element_link(pwQueue, pwvideosink)) {
3551 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: failed to link vpwq → pwvideosink\n");
3552 }
3553 44 GstElement* pwHead = pwQueue;
3554
2/4
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
44 if (GstElement* guard = MakeBranchErrorGuard()) {
3555
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 gst_bin_add(GST_BIN(pipeline), guard);
3556
2/4
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
44 if (gst_element_link(guard, pwQueue)) {
3557 44 pwHead = guard;
3558 } else {
3559 ✗ gst_bin_remove(GST_BIN(pipeline), guard);
3560 }
3561 }
3562
3563
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (pwHead != pwQueue) {
3564
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 gst_element_sync_state_with_parent(pwHead);
3565 }
3566
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 gboolean qSync = gst_element_sync_state_with_parent(pwQueue);
3567
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 gboolean pwSync = gst_element_sync_state_with_parent(pwvideosink);
3568
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 LogDebug(VB_MEDIAOUT, "GStreamer: deferred state sync — queue=%d pipewiresink=%d\n",
3569 qSync, pwSync);
3570
3571
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 GstPad* teeSrc = gst_element_request_pad_simple(vtee, "src_%u");
3572
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 GstPad* pwQSink = gst_element_get_static_pad(pwHead, "sink");
3573
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 GstPadLinkReturn linkRet = gst_pad_link(teeSrc, pwQSink);
3574
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (linkRet != GST_PAD_LINK_OK) {
3575 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: vtee→vpwq pad link failed: %d\n", linkRet);
3576 }
3577
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 gst_object_unref(teeSrc);
3578
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 gst_object_unref(pwQSink);
3579
3580 GstState pwState, pwPending;
3581
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 gst_element_get_state(pwvideosink, &pwState, &pwPending, 2 * GST_SECOND);
3582
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 LogDebug(VB_MEDIAOUT, "GStreamer: pipewiresink state=%d pending=%d after attach\n",
3583 pwState, pwPending);
3584
3585
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 LogDebug(VB_MEDIAOUT, "GStreamer: pipewiresink attached to vtee (node=%s)\n",
3586 videoNodeName.c_str());
3587
3588
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 gst_object_unref(vtee);
3589
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 1 times.
45 }
3590 // Owning refs on pipeline/volume released by refGuard on scope exit.
3591
4/6
✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2161 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 889 times.
✓ Branch 5 taken 1258 times.
4308 }).detach();
3592 2161 }
3593
3594 // m_currentInstance owns the WLED audio-reactive tap. Slot 1 (primary)
3595 // always takes priority; secondary slots only claim it if no other
3596 // instance is active.
3597 {
3598
1/2
✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
2161 std::lock_guard<std::mutex> lock(s_sampleMutex);
3599
4/4
✓ Branch 0 taken 1124 times.
✓ Branch 1 taken 1037 times.
✓ Branch 2 taken 330 times.
✓ Branch 3 taken 794 times.
2161 if (m_streamSlot == 1 || m_currentInstance == nullptr) {
3600
1/2
✓ Branch 0 taken 1367 times.
✗ Branch 1 not taken.
1367 SetSampleTapOwnerLocked(this);
3601 }
3602 2161 }
3603
3604 // Register with StreamSlotManager
3605 2161 StreamSlotManager::Instance().SetActiveOutput(m_streamSlot, this);
3606
3607 #ifdef HAS_AES67_GSTREAMER
3608 // AES67 send pipelines run continuously (always sending to multicast).
3609 // No resume needed — filter-chain outputs silence when idle.
3610 #endif
3611
3612
1/2
✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
2161 if (m_mediaOutputStatus) {
3613 2161 m_mediaOutputStatus->status = MEDIAOUTPUTSTATUS_PLAYING;
3614 }
3615
3616 // Ensure channel output thread is running so ProcessVideoOverlay gets called
3617 // (same as SDLOutput behavior)
3618 bool haveModel;
3619 {
3620
1/2
✓ Branch 0 taken 2161 times.
✗ Branch 1 not taken.
2161 std::lock_guard<std::mutex> lock(m_videoOverlayModelLock);
3621 2161 haveModel = m_videoOverlayModel != nullptr;
3622 2161 }
3623
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 2150 times.
2161 if (haveModel) {
3624 11 StartChannelOutputThread();
3625 }
3626
3627 2161 Starting();
3628 2161 LogDebug(VB_MEDIAOUT, "GStreamer started playing: %s\n", m_mediaFilename.c_str());
3629 2161 return 1;
3630 }
3631
3632 2146 int GStreamerOutput::Stop(void) {
3633
1/2
✓ Branch 0 taken 2146 times.
✗ Branch 1 not taken.
2146 LogDebug(VB_MEDIAOUT, "GStreamerOutput::Stop()\n");
3634
1/2
✓ Branch 0 taken 2146 times.
✗ Branch 1 not taken.
2146 std::unique_lock<std::recursive_mutex> opLock(m_opMutex);
3635 // Idempotency guard: Stop() is invoked both directly (e.g. from
3636 // CloseMediaOutput() when the media is still playing) and again via the
3637 // destructor's Close(). The teardown below includes a 250ms audio-silence
3638 // flush and a 150ms DRM-release sleep; running it twice adds ~400ms of
3639 // dead time to every clip transition, which on a multisync remote pushes
3640 // playback progressively behind the master. Run the teardown once only.
3641
4/4
✓ Branch 0 taken 2119 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 2100 times.
✓ Branch 3 taken 19 times.
2146 if (m_pipeline && !m_teardownComplete) {
3642 // Nothing below may race decodebin's no-more-pads handler, which
3643 // removes and unrefs m_appsink, m_volume and m_videoAppsink on a
3644 // streaming thread, or a bus handler still running for this pipeline.
3645
1/2
✓ Branch 0 taken 2100 times.
✗ Branch 1 not taken.
2100 DetachCallbacks();
3646
3647 // Detach AES67 zero-hop RTP branches BEFORE pipeline goes NULL —
3648 // this resumes the standalone send pipeline so AES67 keeps working
3649 // between tracks.
3650 #ifdef HAS_AES67_GSTREAMER
3651
1/2
✓ Branch 0 taken 2100 times.
✗ Branch 1 not taken.
2100 DetachAES67Branches();
3652 #endif
3653
3654 // Set shutdown flag to prevent appsink callbacks from doing work
3655 // during teardown — without this, the streaming thread can deadlock
3656 // with gst_element_set_state(NULL) due to malloc arena locks.
3657 2100 m_shutdownFlag.store(true);
3658
3659 // Nothing from the teardown reaches the screen: the display keeps the
3660 // last frame shown for the next track to replace.
3661 #ifdef HAS_VIDEO_DISPLAY
3662
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 2053 times.
2100 if (m_display) {
3663
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 m_display->DetachFeed(m_displayFeedToken);
3664 }
3665 #endif
3666
3667 // Tell the detached Start() PLAYING-transition thread (if still running)
3668 // to abort its volume ramp / deferred pipewiresink attach. It captures
3669 // only this token plus owning GStreamer refs, so it is safe even after
3670 // this object is freed.
3671
2/2
✓ Branch 0 taken 2099 times.
✓ Branch 1 taken 1 times.
2100 if (m_startThreadCancel) {
3672 2099 m_startThreadCancel->store(true);
3673 }
3674
3675 // Stop feeding the persistent output stream before anything else, so
3676 // nothing from this track's teardown reaches it. The stream stays open
3677 // for the next track.
3678 2100 bool fedPipeWireStream = m_pwStream != nullptr;
3679
1/2
✓ Branch 0 taken 2100 times.
✗ Branch 1 not taken.
2100 ReleasePipeWireStream();
3680
3681 // Disconnect appsink signals BEFORE state change to prevent
3682 // callbacks firing during pipeline teardown.
3683
2/2
✓ Branch 0 taken 2088 times.
✓ Branch 1 taken 12 times.
2100 if (m_appsink) {
3684
1/2
✓ Branch 0 taken 2088 times.
✗ Branch 1 not taken.
2088 if (m_appsinkSignalId > 0) {
3685
1/2
✓ Branch 0 taken 2088 times.
✗ Branch 1 not taken.
2088 g_signal_handler_disconnect(m_appsink, m_appsinkSignalId);
3686 2088 m_appsinkSignalId = 0;
3687 }
3688
1/2
✓ Branch 0 taken 2088 times.
✗ Branch 1 not taken.
2088 g_object_set(m_appsink, "emit-signals", FALSE, NULL);
3689 }
3690
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2096 times.
2100 if (m_videoAppsink) {
3691
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (m_videoAppsinkSignalId > 0) {
3692
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 g_signal_handler_disconnect(m_videoAppsink, m_videoAppsinkSignalId);
3693 4 m_videoAppsinkSignalId = 0;
3694 }
3695
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 g_object_set(m_videoAppsink, "emit-signals", FALSE, NULL);
3696
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 LogVideoOverlayDrops();
3697 }
3698
3699 // Remove bus sync handler before state change
3700
2/2
✓ Branch 0 taken 2099 times.
✓ Branch 1 taken 1 times.
2100 if (m_bus) {
3701
1/2
✓ Branch 0 taken 2099 times.
✗ Branch 1 not taken.
2099 gst_bus_set_sync_handler(m_bus, nullptr, nullptr, nullptr);
3702 }
3703
3704 // Stop video output consumers BEFORE tearing down the producer pipeline
3705 // to avoid pipewiresrc crash from disappearing PipeWire producer node
3706
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 2057 times.
2100 if (m_videoPipeWireRouting) {
3707
2/4
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
43 VideoOutputManager::Instance().StopConsumers();
3708 }
3709
3710 // NOTE: Do NOT pre-disconnect pipewiresink elements here.
3711 // Any operation that triggers gst_pipewire_sink_change_state or
3712 // gst_pipewire_sink_event will call pw_thread_loop_lock, which
3713 // deadlocks if the PipeWire callback thread is inside
3714 // on_param_changed. The 200ms delay before StartConsumers in
3715 // the deferred-attach thread ensures the buffer pool is active so
3716 // on_param_changed never blocks, and the pipeline NULL below can
3717 // proceed normally.
3718
3719 // For pipelines with no DRM/KMS involvement (audio-only), the rest of
3720 // the teardown - the 250ms silence flush and the pipeline state
3721 // change to NULL - has no ordering dependency on anything the next
3722 // track needs: PipeWire mixes concurrent streams, and the next
3723 // pipeline uses its own stream node. Run that slow tail on a
3724 // detached thread so a track transition (or a looping playlist wrap)
3725 // doesn't spend ~0.5s of lights-frozen time on it. Pipelines that
3726 // touch DRM (kmssink, direct connectors, reserved planes, shared DRM
3727 // fds) keep the fully synchronous path below: their kernel resources
3728 // must be released before the next pipeline may claim them.
3729
3/4
✓ Branch 0 taken 2007 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2006 times.
✓ Branch 3 taken 1 times.
6114 bool asyncTeardownEligible = !m_hasVideoStream && !m_kmssink &&
3730 2007 m_directConnectorIds.empty() &&
3731
2/4
✓ Branch 0 taken 2006 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2006 times.
✗ Branch 3 not taken.
4012 !m_videoPipeWireRouting &&
3732
3/4
✓ Branch 0 taken 2007 times.
✓ Branch 1 taken 93 times.
✓ Branch 2 taken 2006 times.
✗ Branch 3 not taken.
6113 m_drmLeases.empty() && m_mediaConnectors.empty();
3733
2/2
✓ Branch 0 taken 2006 times.
✓ Branch 1 taken 94 times.
2100 if (asyncTeardownEligible) {
3734 // These are what Close() would run inside its m_pipeline guard;
3735 // that guard will be skipped because we null the members here.
3736 // (And on the same terms: see Close().)
3737
9/10
✓ Branch 0 taken 2004 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1980 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 1980 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1202 times.
✓ Branch 7 taken 778 times.
✓ Branch 8 taken 1226 times.
✓ Branch 9 taken 780 times.
2006 if (m_claimedSlot && !(fedPipeWireStream && PipeWireOutputStream::SlotMayCarryOn(m_streamSlot))) {
3738
1/2
✓ Branch 0 taken 1226 times.
✗ Branch 1 not taken.
1226 FlushPipeWireDelayBuffers();
3739 #ifdef HAS_AES67_GSTREAMER
3740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1226 times.
1226 if (AES67Manager::INSTANCE.IsActive()) {
3741 ✗ AES67Manager::INSTANCE.FlushSendPipelines();
3742 }
3743 #endif
3744 }
3745
1/2
✓ Branch 0 taken 2006 times.
✗ Branch 1 not taken.
2006 Stopping();
3746
3747 // The detached thread takes over this object's refs; it must not
3748 // touch `this` (the object is typically destroyed right after).
3749 4012 std::thread([pipeline = m_pipeline, volume = m_volume, bus = m_bus,
3750 2006 appsink = m_appsink, videoAppsink = m_videoAppsink,
3751 2006 flushSilence = !fedPipeWireStream]() {
3752
2/4
✓ Branch 0 taken 2006 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2006 times.
✗ Branch 3 not taken.
2006 SetThreadName("FPP-GstTeardown");
3753 // A track that fed the persistent stream has nothing to flush:
3754 // its feed is already detached, and the stream plays silence
3755 // on its own once nothing feeds it.
3756
4/4
✓ Branch 0 taken 2005 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 1982 times.
2006 if (volume && flushSilence) {
3757 // Silence flush: push zeros through the PipeWire chain so
3758 // stale buffer contents can't replay as a click when the
3759 // next track starts (see comment on the synchronous path).
3760 23 g_object_set(volume, "volume", 0.0, NULL);
3761
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
23 std::this_thread::sleep_for(std::chrono::milliseconds(250));
3762 }
3763 2005 const bool hwDecoder = PipelineHasHwVideoDecoder(pipeline);
3764
3/4
✓ Branch 0 taken 2004 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 1995 times.
2005 if (!SetPipelineToNullSupervised(pipeline, PIPELINE_TEARDOWN_TIMEOUT_MS)) {
3765 // Reaper keeps its own refs to a wedged pipeline; drop
3766 // ours below either way (mirrors Close()'s behavior).
3767 9 RecordWedgeEventAndMaybeRestart("async pipeline teardown wedged, abandoning pipeline", hwDecoder);
3768 }
3769
2/2
✓ Branch 0 taken 2003 times.
✓ Branch 1 taken 1 times.
2004 if (appsink)
3770 2003 gst_object_unref(appsink);
3771
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2004 times.
2004 if (videoAppsink)
3772 ✗ gst_object_unref(videoAppsink);
3773
2/2
✓ Branch 0 taken 2003 times.
✓ Branch 1 taken 1 times.
2004 if (volume)
3774 2003 gst_object_unref(volume);
3775
2/2
✓ Branch 0 taken 2003 times.
✓ Branch 1 taken 1 times.
2004 if (bus)
3776 2003 gst_object_unref(bus);
3777 2004 gst_object_unref(pipeline);
3778
2/4
✓ Branch 0 taken 2006 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2006 times.
✗ Branch 3 not taken.
4010 }).detach();
3779
3780 2006 m_pipeline = nullptr;
3781 2006 m_volume = nullptr;
3782 2006 m_bus = nullptr;
3783 2006 m_appsink = nullptr;
3784 2006 m_videoAppsink = nullptr;
3785
1/2
✓ Branch 0 taken 2006 times.
✗ Branch 1 not taken.
2006 ReleaseDisplay();
3786
3787 2006 m_playing = false;
3788
6/8
✓ Branch 0 taken 2006 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2006 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2004 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 2004 times.
✓ Branch 7 taken 2 times.
2006 if (m_mediaOutputStatus && OwnsSlotStatus()) {
3789 2004 m_mediaOutputStatus->status = MEDIAOUTPUTSTATUS_IDLE;
3790 }
3791 // Last touch of `this`: Stopped() may hand the object to a
3792 // deferred delete (Play Media's runningCommandMedia), so nothing
3793 // may be written after it -- the lock included.
3794 2006 m_teardownComplete = true;
3795
1/2
✓ Branch 0 taken 2006 times.
✗ Branch 1 not taken.
2006 opLock.unlock();
3796
1/2
✓ Branch 0 taken 2006 times.
✗ Branch 1 not taken.
2006 Stopped();
3797 2006 return 1;
3798 }
3799
3800 // Flush silence through the PipeWire graph before tearing down.
3801 // When the pipeline stops, PipeWire combine-stream and filter-chain
3802 // nodes go IDLE with whatever audio was last in their buffers. If a
3803 // new track starts before WirePlumber suspends those nodes (~5s),
3804 // the stale buffer contents replay as an audible click.
3805 // Setting GStreamer volume to 0 while the pipeline is still PLAYING
3806 // pushes silence through the entire chain (filter-chain delay buffers,
3807 // combine-stream mixers, ALSA ring buffers), overwriting stale data.
3808 // 250ms covers the longest configured delay (206ms) plus a few
3809 // PipeWire quanta for propagation.
3810 //
3811 // Not for a track that fed the persistent output stream, as on the
3812 // asynchronous path above: its feed is detached already, so the
3813 // zeros would go nowhere, and the stream plays silence on its own.
3814 // Every video with sound takes this path, and the wait was 250 ms
3815 // of a frozen picture at each track change.
3816
4/4
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 20 times.
94 if (m_volume && !fedPipeWireStream) {
3817
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 g_object_set(m_volume, "volume", 0.0, NULL);
3818
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 std::this_thread::sleep_for(std::chrono::milliseconds(250));
3819 }
3820
3821
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 Stopping();
3822
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 LogDebug(VB_MEDIAOUT, "GStreamerOutput::Stop() - setting pipeline to NULL\n");
3823 // Supervised: a wedged V4L2 decoder (issue #2695) makes this state
3824 // change block forever — on the 2026-07-03 soak it hung the playlist
3825 // thread for 2h25m and ended in SIGBUS. On timeout the pipeline is
3826 // abandoned (reaper thread keeps the ref) so playback can continue.
3827
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 const bool hwDecoder = PipelineHasHwVideoDecoder(m_pipeline);
3828 // If an abandoned pipeline does stop after all, its decoder has let
3829 // go of the display's frames: give back the use of the display that
3830 // ReleaseDisplay() keeps for it, and the display can close.
3831 94 std::function<void()> ifLate;
3832 #ifdef HAS_VIDEO_DISPLAY
3833
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 47 times.
94 if (m_display) {
3834 94 ifLate = [display = m_display]() {
3835 2 LogInfo(VB_MEDIAOUT, "GStreamer: giving back the display an abandoned pipeline held\n");
3836 2 display->Release();
3837
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 };
3838 }
3839 #endif
3840
3/4
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 91 times.
✓ Branch 3 taken 3 times.
94 if (SetPipelineToNullSupervised(m_pipeline, PIPELINE_TEARDOWN_TIMEOUT_MS, std::move(ifLate))) {
3841
1/2
✓ Branch 0 taken 91 times.
✗ Branch 1 not taken.
91 LogDebug(VB_MEDIAOUT, "GStreamerOutput::Stop() - pipeline NULL complete\n");
3842 } else {
3843 3 m_teardownAbandoned = true;
3844
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 RecordWedgeEventAndMaybeRestart("pipeline teardown wedged, abandoning pipeline", hwDecoder);
3845 }
3846
3847 // On Raspberry Pi's vc4/v3d DRM driver, the kernel needs time to
3848 // fully release CRTC/plane resources after kmssink's DRM fd is
3849 // closed. Without this delay, a subsequent Start() may fail with
3850 // "general resource error" from kmssink because the connector is
3851 // still held by the kernel. 150ms covers the observed ~100-120ms
3852 // release window on Pi 5 hardware.
3853 // Only needed when kmssink is in the primary pipeline (not when
3854 // PipeWire routing sends HDMI through a consumer pipeline), and
3855 // pointless after an abandoned teardown (nothing was released).
3856
10/12
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 91 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45 times.
✓ Branch 5 taken 46 times.
✓ Branch 6 taken 45 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
✓ Branch 9 taken 83 times.
✓ Branch 10 taken 8 times.
✓ Branch 11 taken 86 times.
94 if (!m_teardownAbandoned && !m_macVideoWindow && ((m_kmssink && !m_display) || !m_directConnectorIds.empty())) {
3857
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 std::this_thread::sleep_for(std::chrono::milliseconds(150));
3858
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 LogDebug(VB_MEDIAOUT, "GStreamerOutput::Stop() - DRM release delay complete\n");
3859 }
3860
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 ReleaseDisplay();
3861
3862 94 m_playing = false;
3863
6/8
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 94 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 76 times.
✓ Branch 5 taken 18 times.
✓ Branch 6 taken 76 times.
✓ Branch 7 taken 18 times.
94 if (m_mediaOutputStatus && OwnsSlotStatus()) {
3864 76 m_mediaOutputStatus->status = MEDIAOUTPUTSTATUS_IDLE;
3865 }
3866 // Last touch of `this` - see the async path above.
3867 94 m_teardownComplete = true;
3868
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 opLock.unlock();
3869
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 Stopped();
3870 94 }
3871 140 return 1;
3872 2146 }
3873
3874 6043 void GStreamerOutput::DetachCallbacks() {
3875 // Start() hands an owning pipeline ref to a detached thread that calls
3876 // set_state(PLAYING), so decodebin can still be exposing pads after the
3877 // teardown nulls m_audioChain/m_videoChain and drops our pipeline ref --
3878 // and after ~GStreamerOutput frees the object outright. Clearing `self`
3879 // under the guard lock both blocks until any in-flight callback finishes
3880 // and makes every later one a no-op. The bus sync handler doesn't hold
3881 // the lock while it runs, so wait for it separately. Never call this from
3882 // a streaming thread: it waits for the handlers running on them.
3883
3/4
✓ Branch 0 taken 6043 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2108 times.
✓ Branch 3 taken 3935 times.
6043 if (auto guard = m_cbGuard.load()) {
3884 {
3885
1/2
✓ Branch 0 taken 2108 times.
✗ Branch 1 not taken.
2108 std::unique_lock<std::recursive_mutex> lock(guard->mtx);
3886 2108 guard->self = nullptr;
3887
1/2
✓ Branch 0 taken 2108 times.
✗ Branch 1 not taken.
4220 guard->busIdle.wait(lock, [&guard] { return guard->busInFlight == 0; });
3888 2108 }
3889
1/2
✓ Branch 0 taken 2108 times.
✗ Branch 1 not taken.
2108 m_cbGuard.store(nullptr);
3890 6043 }
3891 6043 }
3892
3893 // Per output: two outputs on one model (a Play Media restarted on it, whose
3894 // old output closes after the new one starts) used to share one id, and the
3895 // old one's Close() removed the new one's listener -- leaving it holding a
3896 // model pointer nothing would clear when the model was deleted.
3897 170 std::string GStreamerOutput::OverlayListenerId() const {
3898 char id[48];
3899 170 snprintf(id, sizeof(id), "GStreamerOut_%p", (const void*)this);
3900
1/2
✓ Branch 0 taken 170 times.
✗ Branch 1 not taken.
340 return id;
3901 }
3902
3903 34 bool GStreamerOutput::SetPipelineToNullBounded(GstElement* pipeline, int timeoutMs) {
3904
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 return SetPipelineToNullSupervised(pipeline, timeoutMs);
3905 }
3906
3907 // A video played to a pixel overlay model drops the frames it can't show on
3908 // time (see the video appsink's max-lateness). A box that drops many can't
3909 // decode and scale the file in real time; say so once per track, so a report
3910 // of a choppy matrix comes with the reason.
3911 4 void GStreamerOutput::LogVideoOverlayDrops() {
3912 4 GstStructure* stats = nullptr;
3913
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 g_object_get(m_videoAppsink, "stats", &stats, NULL);
3914
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!stats) {
3915 ✗ return;
3916 }
3917 4 guint64 rendered = 0, dropped = 0;
3918
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 gst_structure_get_uint64(stats, "rendered", &rendered);
3919
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 gst_structure_get_uint64(stats, "dropped", &dropped);
3920
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 gst_structure_free(stats);
3921 // The decoder skips frames for the QoS events too, and those never reach
3922 // the sink, so its counts are a floor on what was lost, not a share of
3923 // the file.
3924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (dropped * 20 > rendered + dropped) {
3925 ✗ LogInfo(VB_MEDIAOUT, "GStreamer: %s showed %" G_GUINT64_FORMAT " video frames on %s and dropped %" G_GUINT64_FORMAT
3926 " that came late (the decoder may have skipped more): this box can't decode and scale it in "
3927 "real time; a lower resolution or frame rate would play whole\n",
3928 m_mediaFilename.c_str(), rendered, m_videoOut.c_str(), dropped);
3929 } else {
3930
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 LogDebug(VB_MEDIAOUT, "GStreamer: %s showed %" G_GUINT64_FORMAT " video frames on %s, dropped %" G_GUINT64_FORMAT " late\n",
3931 m_mediaFilename.c_str(), rendered, m_videoOut.c_str(), dropped);
3932 }
3933 }
3934
3935 // A video whose sound feeds the output stream shows each frame when the feed
3936 // clock reaches it; the sound of that moment leaves the card a card's queue
3937 // later. Hold the picture back by that queue (a sink's ts-offset), so picture,
3938 // sound and the lights, which follow the sound, all meet. Re-applied only as
3939 // the queue's average moves.
3940 169956 void GStreamerOutput::AlignPictureToSound(int64_t latNs) {
3941
4/4
✓ Branch 0 taken 162822 times.
✓ Branch 1 taken 7134 times.
✓ Branch 2 taken 161864 times.
✓ Branch 3 taken 958 times.
169956 if (latNs <= 0 || std::llabs(latNs - m_pictureDelayNs) < 2 * GST_MSECOND) {
3942 168998 return;
3943 }
3944 958 m_pictureDelayNs = latNs;
3945 958 std::vector<GstElement*> sinks;
3946 {
3947 // OnNoMorePads() can remove the kmssink on a streaming thread.
3948
1/2
✓ Branch 0 taken 958 times.
✗ Branch 1 not taken.
958 std::shared_ptr<CallbackGuard> guard = m_cbGuard.load();
3949 958 std::unique_lock<std::recursive_mutex> lock;
3950
1/2
✓ Branch 0 taken 958 times.
✗ Branch 1 not taken.
958 if (guard) {
3951
1/2
✓ Branch 0 taken 958 times.
✗ Branch 1 not taken.
958 lock = std::unique_lock<std::recursive_mutex>(guard->mtx);
3952 }
3953
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 942 times.
958 if (m_kmssink) {
3954
4/8
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 16 times.
✗ Branch 7 not taken.
16 sinks.push_back(GST_ELEMENT(gst_object_ref(m_kmssink)));
3955 }
3956
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 950 times.
958 if (m_videoAppsink) {
3957
4/8
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
8 sinks.push_back(GST_ELEMENT(gst_object_ref(m_videoAppsink)));
3958 }
3959 958 }
3960
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 958 times.
959 for (int cid : m_directConnectorIds) {
3961
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::string name = "dkms_" + std::to_string(cid);
3962
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 not taken.
✓ Branch 7 taken 1 times.
1 if (GstElement* dkms = gst_bin_get_by_name(GST_BIN(m_pipeline), name.c_str())) {
3963 ✗ sinks.push_back(dkms);
3964 }
3965 1 }
3966
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 958 times.
982 for (GstElement* sink : sinks) {
3967
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 g_object_set(sink, "ts-offset", (gint64)latNs, NULL);
3968
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 gst_object_unref(sink);
3969 }
3970
1/2
✓ Branch 0 taken 958 times.
✗ Branch 1 not taken.
958 LogDebug(VB_MEDIAOUT, "GStreamer: picture held back %.1f ms to meet the sound\n", latNs / 1e6);
3971 958 }
3972
3973 33 void GStreamerOutput::EndTimelineShift() {
3974 // A seek in the moment the start thread runs a carried-on pipeline on
3975 // the timeline's base time with no start time (before it sets one again
3976 // once PLAYING) would keep that base time; with a start time the flushing
3977 // seek works out a new one. Under the handover's lock, so that the start
3978 // thread can't put the timeline's base time back after it (it starts the
3979 // track drained instead).
3980 33 std::unique_lock<std::mutex> handoverLock;
3981
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (m_timelineHandover) {
3982
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 handoverLock = std::unique_lock<std::mutex>(m_timelineHandover->lock);
3983 33 m_timelineHandover->ended = true;
3984 }
3985
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (m_pipeline) {
3986
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 gst_element_set_start_time(m_pipeline, 0);
3987 }
3988
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.
66 if (!m_timelineShiftNs || m_timelineShiftNs->exchange(0) == 0) {
3989 33 return;
3990 }
3991 ✗ if (m_pwFeed) {
3992 ✗ g_object_set(m_pwFeed, "ts-offset", (gint64)-PipeWireOutputStream::FeedLeadNs(), NULL);
3993 }
3994 // The picture follows at the next AlignPictureToSound().
3995
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 }
3996
3997 6046 void GStreamerOutput::ReleaseDisplay() {
3998 #ifdef HAS_VIDEO_DISPLAY
3999
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 5998 times.
6046 if (m_display) {
4000 48 m_display->DetachFeed(m_displayFeedToken);
4001
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 46 times.
48 if (m_teardownAbandoned) {
4002 // The pipeline was abandoned still running (a wedged decoder),
4003 // and its decoder may still hold frames from the display's pool,
4004 // on the display's DRM lease. Keep this track's use of the
4005 // display, so it never closes the lease under them; it shows the
4006 // next track as before. The teardown gives it back if it does
4007 // finish (Stop()), else the restart that wedged teardowns lead to
4008 // (STUCK_TEARDOWN_RESTART_LIMIT).
4009 2 LogWarn(VB_MEDIAOUT, "GStreamer: keeping the display's lease for an abandoned pipeline\n");
4010 } else {
4011 46 m_display->Release();
4012 }
4013 48 m_display.reset();
4014 48 m_displayFeedToken = 0;
4015 }
4016 #endif
4017 6046 }
4018
4019 2102 void GStreamerOutput::ReleasePipeWireStream() {
4020
2/2
✓ Branch 0 taken 2005 times.
✓ Branch 1 taken 97 times.
2102 if (m_pwStream) {
4021
2/2
✓ Branch 0 taken 1989 times.
✓ Branch 1 taken 16 times.
2005 if (m_pwStreamAcquired) {
4022 1989 m_pwStream->Release(m_pwFeed);
4023 }
4024 2005 m_pwStream = nullptr;
4025 2005 m_pwStreamAcquired = false;
4026 }
4027
2/2
✓ Branch 0 taken 2005 times.
✓ Branch 1 taken 97 times.
2102 if (m_pwFeed) {
4028 2005 gst_object_unref(m_pwFeed);
4029 2005 m_pwFeed = nullptr;
4030 }
4031 2102 }
4032
4033 // ---------------------------------------------------------------------------
4034 // Audio sink latency
4035 //
4036 // gst_element_query_position() on pipewiresink reports what the sink has
4037 // rendered into the PipeWire graph, not what has left the card, and
4038 // pipewiresink adds nothing of its own to the GStreamer LATENCY query -- it
4039 // answers min = upstream + processing-deadline + render-delay with render-delay
4040 // at 0, identically no matter which node it targets, so it never asks PipeWire
4041 // how far the data still has to travel. Everything queued downstream is
4042 // therefore invisible to mediaSeconds, and the sequence -- which
4043 // CalculateNewChannelOutputDelay() servos to exactly that number -- runs ahead
4044 // of the sound by the whole downstream depth.
4045 //
4046 // Measured with the sequence's own frame index captured off the wire against a
4047 // sample-accurate audio reference: lights ahead by 22 ms on an AM62x I2S cape,
4048 // 56 ms on a Pi 5 I2S cape, and 160 ms on a BeagleBone with a full-speed USB
4049 // dongle. It is a per-platform constant, which is why it cannot be a setting.
4050 //
4051 // The USB figure is not fixed for all time: it is dominated by that card's
4052 // api.alsa.headroom, and a USB card that is the sole sink in its audio group is
4053 // now given a quarter of what it used to get (see kUsbHeadroomSoleSink in
4054 // FPPINIT_Audio.cpp), which took the same BeagleBone from 160 ms to ~80 ms.
4055 // Nothing below needs changing for that -- the queue depth is read live and
4056 // scaled by the card's own rate, so the correction simply gets smaller -- but do
4057 // not treat the numbers above as the values a current box will report.
4058 //
4059 // The card's own delay figure is the closest estimate obtainable without a new
4060 // library or a patched GStreamer plugin: /proc/asound/cardN/pcmMp/sub0/status
4061 // reports `delay` in frames -- the distance from PipeWire's write pointer to
4062 // the DAC. It matched the measured offset to within 3 ms on the two platforms
4063 // where both could be cross-checked (157 vs 160 ms, 21 vs 22 ms). PipeWire's
4064 // own declared SPA_PARAM_Latency was tried and is worse: it is the nominal
4065 // quantum + headroom + period, which under-reports the live queue by 30 ms on
4066 // the USB box.
4067 //
4068 // This corrects the *reported position*. It deliberately does not touch
4069 // mediaOffset, which stays a user knob applied on top in setMediaElapsed().
4070 // ---------------------------------------------------------------------------
4071
4072 37 static int AlsaCardNumber(const std::string& cardId) {
4073
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (cardId.empty()) {
4074 ✗ return -1;
4075 }
4076
3/6
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 37 times.
✗ Branch 5 not taken.
37 std::istringstream iss(GetFileContents("/proc/asound/cards"));
4077 37 std::string line;
4078
3/6
✓ Branch 0 taken 121 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 121 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 121 times.
✗ Branch 5 not taken.
121 while (std::getline(iss, line)) {
4079 // " 2 [KulpLightsHIFI ]: simple-card - ..."
4080 121 std::size_t lb = line.find('[');
4081 121 std::size_t rb = line.find(']');
4082
4/6
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 79 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 79 times.
121 if (lb == std::string::npos || rb == std::string::npos || rb < lb) {
4083 42 continue;
4084 }
4085
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 std::string id = line.substr(lb + 1, rb - lb - 1);
4086
5/6
✓ Branch 0 taken 394 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 315 times.
✓ Branch 3 taken 79 times.
✓ Branch 4 taken 315 times.
✓ Branch 5 taken 79 times.
394 while (!id.empty() && id.back() == ' ') {
4087 315 id.pop_back();
4088 }
4089
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 42 times.
79 if (id == cardId) {
4090 37 return std::atoi(line.c_str());
4091 }
4092
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 37 times.
79 }
4093 ✗ return -1;
4094 37 }
4095
4096 // Card the running graph actually feeds.
4097 //
4098 // Taken from the adapters FPP generated rather than trusting AudioOutput
4099 // outright -- the setting can name a card that has since been unplugged, and
4100 // the queue depth is only readable for a card the graph really holds. But the
4101 // conf declares an adapter for EVERY playback-capable card, emitted in
4102 // card-number order, so simply reading its first api.alsa.path yields card 0
4103 // whether or not that is the output. On a box with three USB cards and
4104 // AudioOutput=ICUSBAUDIO7D (card 2) that read card 0, found its PCM 'closed'
4105 // (idle cards suspend), and so applied no correction at all -- the sequence
4106 // stayed ahead of the sound, which is the whole defect this is here to remove.
4107 //
4108 // The order of preference is what the graph feeds, then what the setting says,
4109 // then anything at all:
4110 //
4111 // 1. a card the group config actually targets, matching AudioOutput
4112 // 2. any card the group config targets
4113 // 3. a declared adapter matching AudioOutput
4114 // 4. the first declared adapter
4115 //
4116 // Steps 1 and 2 exist because AudioOutput can legitimately disagree with the
4117 // running graph. The setting is flagged reboot-required, so between changing
4118 // the output card and rebooting, the setting names the new card while the graph
4119 // still feeds the old one -- reading the setting there picks a card that is not
4120 // playing, whose PCM is closed, and applies no correction at all. The group
4121 // config is what PipeWire actually loaded, so it is the better authority; this
4122 // is what the original code was reaching for by reading the conf rather than the
4123 // setting, before it settled on "first adapter in the file" and got card 0.
4124 //
4125 // Both generated confs have to be searched, not just the boot-time one. The
4126 // boot probe deliberately skips a card it cannot drive directly -- an
4127 // IEC958-only vc4-hdmi is the common one ("no standard PCM format") -- and the
4128 // group config then creates that card's adapter itself, through sysdefault:
4129 // rather than hw:. A Pi playing audio over HDMI therefore has its only sink
4130 // adapter in 97-fpp-audio-groups.conf and none in the 95 conf, so searching the
4131 // 95 conf alone finds either the wrong card or no card, and silently applies no
4132 // correction at all. That configuration is Simple mode's default on a board
4133 // with no other sound card, which is most of them.
4134 798 static int AlsaSinkCardNumber() {
4135 // 95 first: where a card FPP can drive directly is declared. 97 second: the
4136 // group config's own adapters, for the cards 95 had to skip.
4137
2/4
✓ Branch 0 taken 798 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 798 times.
✗ Branch 3 not taken.
798 const std::string groupsConf = GetFileContents("/etc/pipewire/pipewire.conf.d/97-fpp-audio-groups.conf");
4138 const std::string confs[] = {
4139
1/2
✓ Branch 0 taken 798 times.
✗ Branch 1 not taken.
798 GetFileContents("/etc/pipewire/pipewire.conf.d/95-fpp-alsa-sink.conf"),
4140 groupsConf
4141
2/8
✓ Branch 0 taken 798 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 798 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
3990 };
4142 // Reads the value of `key = "..."` at or after `from`, or "" if absent.
4143 24 auto quotedValue = [](const std::string& s, const std::string& key, std::size_t from) {
4144 24 std::size_t k = s.find(key, from);
4145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (k == std::string::npos) {
4146 ✗ return std::string();
4147 }
4148 24 std::size_t a = s.find('"', k + key.size());
4149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (a == std::string::npos) {
4150 ✗ return std::string();
4151 }
4152 24 std::size_t b = s.find('"', a + 1);
4153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (b == std::string::npos) {
4154 ✗ return std::string();
4155 }
4156 24 return s.substr(a + 1, b - a - 1);
4157 };
4158
4159 // Adapters the group config's filter chains actually play into. Only
4160 // fpp_alsa_* targets count: the combine streams carry node.target too, but
4161 // theirs name the fpp_fx_* filter chains rather than a card.
4162 798 std::set<std::string> graphTargets;
4163
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 798 times.
810 for (std::size_t t = groupsConf.find("node.target"); t != std::string::npos;
4164 12 t = groupsConf.find("node.target", t + 1)) {
4165
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
24 std::string target = quotedValue(groupsConf, "node.target", t);
4166
4/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 6 times.
24 if (startsWith(target, "fpp_alsa_")) {
4167
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 graphTargets.insert(target);
4168 }
4169 12 }
4170
4171 // AudioOutput is a stable ALSA card ID on current installs, a bare card
4172 // number on older ones; resolveAudioOutputCardNum() accepts both, so match
4173 // its handling here rather than assuming either spelling.
4174
1/2
✓ Branch 0 taken 798 times.
✗ Branch 1 not taken.
798 std::string wanted = GetEffectiveAudioOutput();
4175
1/2
✓ Branch 0 taken 798 times.
✗ Branch 1 not taken.
798 TrimWhiteSpace(wanted);
4176 798 int wantedCard = -1;
4177
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 796 times.
798 if (!wanted.empty()) {
4178 4 wantedCard = (wanted.find_first_not_of("0123456789") == std::string::npos)
4179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 ? std::atoi(wanted.c_str())
4180
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 : AlsaCardNumber(wanted);
4181 }
4182
4183 798 int targetedCard = -1; // first adapter the graph plays into
4184 798 int settingCard = -1; // first adapter matching AudioOutput
4185 798 int firstCard = -1; // first adapter of any kind
4186 // Sink adapters only -- the capture adapters FPP also declares are named
4187 // fpp_alsain_, which this needle does not match.
4188
1/2
✓ Branch 0 taken 798 times.
✗ Branch 1 not taken.
798 const std::string nameKey = "node.name = \"fpp_alsa_";
4189
2/2
✓ Branch 0 taken 1594 times.
✓ Branch 1 taken 796 times.
2390 for (const std::string& conf : confs) {
4190
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1592 times.
1598 for (std::size_t n = conf.find(nameKey); n != std::string::npos; n = conf.find(nameKey, n + 1)) {
4191
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
12 std::string nodeName = quotedValue(conf, "node.name", n);
4192 // "hw:CardId", or "sysdefault:CardId" for a card reached through the
4193 // ALSA plug layer -- the card ID is what follows the colon either way.
4194
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 std::string path = quotedValue(conf, "api.alsa.path", n);
4195 6 std::size_t colon = path.find(':');
4196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (colon == std::string::npos) {
4197 ✗ continue;
4198 }
4199
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 int card = AlsaCardNumber(path.substr(colon + 1));
4200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (card < 0) {
4201 ✗ continue;
4202 }
4203
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 const bool targeted = graphTargets.count(nodeName) > 0;
4204
3/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
6 if (targeted && card == wantedCard) {
4205 2 return card; // the graph feeds it and the setting names it
4206 }
4207
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (targeted && targetedCard < 0) {
4208 4 targetedCard = card;
4209 }
4210
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (card == wantedCard && settingCard < 0) {
4211 ✗ settingCard = card;
4212 }
4213
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (firstCard < 0) {
4214 4 firstCard = card;
4215 }
4216
4/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
8 }
4217 }
4218
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 792 times.
796 if (targetedCard >= 0) {
4219 4 return targetedCard;
4220 }
4221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 792 times.
792 if (settingCard >= 0) {
4222 ✗ return settingCard;
4223 }
4224 792 return firstCard;
4225
2/4
✓ Branch 0 taken 1596 times.
✓ Branch 1 taken 798 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3990 }
4226
4227 // First playback substream of the card; the device index is not always 0.
4228 35 static std::string AlsaPlaybackStatusPath(int card) {
4229
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 std::string base = "/proc/asound/card" + std::to_string(card);
4230
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 DIR* d = opendir(base.c_str());
4231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (!d) {
4232 ✗ return "";
4233 }
4234 35 std::vector<std::string> devs;
4235
3/4
✓ Branch 0 taken 175 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
✓ Branch 3 taken 35 times.
175 while (struct dirent* e = readdir(d)) {
4236
1/2
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
280 std::string n = e->d_name;
4237
7/10
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 105 times.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 35 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 35 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 35 times.
✓ Branch 9 taken 105 times.
140 if (n.size() > 4 && n.compare(0, 3, "pcm") == 0 && n.back() == 'p') {
4238
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 devs.push_back(n);
4239 }
4240 140 }
4241
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 closedir(d);
4242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (devs.empty()) {
4243 ✗ return "";
4244 }
4245
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 std::sort(devs.begin(), devs.end());
4246
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 return base + "/" + devs[0] + "/sub0/status";
4247 35 }
4248
4249 // Value of every `key = "..."` in [from, to) of `s`.
4250 198 static std::vector<std::string> QuotedValues(const std::string& s, const std::string& key,
4251 std::size_t from, std::size_t to) {
4252 198 std::vector<std::string> out;
4253
4/4
✓ Branch 0 taken 326 times.
✓ Branch 1 taken 140 times.
✓ Branch 2 taken 268 times.
✓ Branch 3 taken 58 times.
466 for (std::size_t k = s.find(key, from); k != std::string::npos && k < to; k = s.find(key, k + key.size())) {
4254 268 std::size_t a = s.find('"', k + key.size());
4255 268 std::size_t eol = s.find('\n', k);
4256
2/4
✓ Branch 0 taken 268 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 268 times.
268 if (a == std::string::npos || a > eol) {
4257 ✗ continue;
4258 }
4259 268 std::size_t b = s.find('"', a + 1);
4260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 268 times.
268 if (b == std::string::npos) {
4261 ✗ break;
4262 }
4263
2/4
✓ Branch 0 taken 268 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 268 times.
✗ Branch 3 not taken.
268 out.push_back(s.substr(a + 1, b - a - 1));
4264 }
4265 198 return out;
4266 ✗ }
4267
4268 // Cards a PipeWire sink ultimately plays into, following FPP's generated
4269 // configs: an input mix bus (96) targets an output group, the group's
4270 // combine-stream (97) targets one filter chain per member, and each filter
4271 // chain targets that member's sink -- an fpp_alsa_* adapter for a card, or a
4272 // network sender (AES67, Opus RTP) for anything else. Within one module block
4273 // every node.name is treated as feeding every node.target, which is exactly
4274 // right for all of these block shapes. A sink that reaches no adapter, e.g. a
4275 // group whose only member is an AES67 sender, yields no cards.
4276 222 static std::vector<int> AlsaCardsFedBy(const std::string& sinkName) {
4277
2/4
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 222 times.
✗ Branch 3 not taken.
444 const std::string conf95 = GetFileContents("/etc/pipewire/pipewire.conf.d/95-fpp-alsa-sink.conf");
4278
2/4
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 222 times.
✗ Branch 3 not taken.
444 const std::string conf97 = GetFileContents("/etc/pipewire/pipewire.conf.d/97-fpp-audio-groups.conf");
4279
2/4
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 222 times.
✗ Branch 3 not taken.
222 const std::string conf96 = GetFileContents("/etc/pipewire/pipewire.conf.d/96-fpp-input-groups.conf");
4280
4281 222 std::map<std::string, std::set<std::string>> edges;
4282
1/2
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
222 const std::string blockKey = "name = libpipewire-module-";
4283
2/2
✓ Branch 0 taken 444 times.
✓ Branch 1 taken 222 times.
666 for (const std::string* conf : { &conf96, &conf97 }) {
4284 444 std::vector<std::size_t> starts;
4285
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 444 times.
514 for (std::size_t b = conf->find(blockKey); b != std::string::npos; b = conf->find(blockKey, b + 1)) {
4286
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 starts.push_back(b);
4287 }
4288
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 444 times.
514 for (std::size_t i = 0; i < starts.size(); i++) {
4289
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 41 times.
70 std::size_t end = (i + 1 < starts.size()) ? starts[i + 1] : conf->size();
4290
2/4
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
140 std::vector<std::string> names = QuotedValues(*conf, "node.name", starts[i], end);
4291
2/4
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
70 std::vector<std::string> targets = QuotedValues(*conf, "node.target", starts[i], end);
4292
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 70 times.
210 for (const auto& n : names) {
4293
2/4
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
✗ Branch 3 not taken.
140 edges[n].insert(targets.begin(), targets.end());
4294 }
4295 70 }
4296 444 }
4297
4298 // fpp_alsa_* adapter -> card, from whichever conf declared it.
4299 222 std::map<std::string, int> adapterCard;
4300
2/2
✓ Branch 0 taken 444 times.
✓ Branch 1 taken 222 times.
666 for (const std::string* conf : { &conf95, &conf97 }) {
4301
1/2
✓ Branch 0 taken 444 times.
✗ Branch 1 not taken.
444 const std::string nameKey = "node.name = \"fpp_alsa_";
4302
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 444 times.
473 for (std::size_t n = conf->find(nameKey); n != std::string::npos; n = conf->find(nameKey, n + 1)) {
4303 29 std::size_t next = conf->find(nameKey, n + 1);
4304
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
58 std::vector<std::string> name = QuotedValues(*conf, "node.name", n, n + nameKey.size() + 128);
4305
3/6
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
29 std::vector<std::string> path = QuotedValues(*conf, "api.alsa.path", n, next == std::string::npos ? conf->size() : next);
4306
3/6
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 29 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
29 if (name.empty() || path.empty()) {
4307 ✗ continue;
4308 }
4309 29 std::size_t colon = path[0].find(':');
4310
4/10
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
29 int card = colon == std::string::npos ? -1 : AlsaCardNumber(path[0].substr(colon + 1));
4311
4/8
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
29 if (card >= 0 && !adapterCard.count(name[0])) {
4312
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 adapterCard[name[0]] = card;
4313 }
4314
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
29 }
4315 444 }
4316
4317 222 std::vector<int> cards;
4318 222 std::set<std::string> seen;
4319
3/6
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 222 times.
✓ Branch 3 taken 222 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
666 std::vector<std::string> todo{ sinkName };
4320
2/2
✓ Branch 0 taken 333 times.
✓ Branch 1 taken 222 times.
555 while (!todo.empty()) {
4321
1/2
✓ Branch 0 taken 333 times.
✗ Branch 1 not taken.
333 std::string node = todo.back();
4322 333 todo.pop_back();
4323
3/4
✓ Branch 0 taken 333 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 292 times.
333 if (!seen.insert(node).second) {
4324 41 continue;
4325 }
4326
1/2
✓ Branch 0 taken 292 times.
✗ Branch 1 not taken.
292 auto ac = adapterCard.find(node);
4327
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 263 times.
292 if (ac != adapterCard.end()) {
4328
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
29 if (std::find(cards.begin(), cards.end(), ac->second) == cards.end()) {
4329
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 cards.push_back(ac->second);
4330 }
4331 29 continue;
4332 }
4333
1/2
✓ Branch 0 taken 263 times.
✗ Branch 1 not taken.
263 auto e = edges.find(node);
4334
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 193 times.
263 if (e != edges.end()) {
4335
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 todo.insert(todo.end(), e->second.begin(), e->second.end());
4336 }
4337
2/2
✓ Branch 0 taken 263 times.
✓ Branch 1 taken 70 times.
333 }
4338 222 return cards;
4339
1/6
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
444 }
4340
4341 376923 int GStreamerOutput::PipeWireGraphRate() {
4342 376923 int rate = GetPipeWireGraphRate();
4343
2/2
✓ Branch 0 taken 376052 times.
✓ Branch 1 taken 871 times.
376923 return rate > 0 ? rate : 48000;
4344 }
4345
4346 306 std::string GStreamerOutput::AlsaSinkStatusPath(const std::string& sinkName) {
4347
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 222 times.
306 if (sinkName.empty()) {
4348 // PipeWire's default sink: nothing in FPP's configs says which card.
4349 84 return std::string();
4350 }
4351
1/2
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
222 std::vector<int> cards = AlsaCardsFedBy(sinkName);
4352
3/4
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
222 return cards.empty() ? std::string() : AlsaPlaybackStatusPath(cards[0]);
4353 222 }
4354
4355 73 static int ReadIntField(const std::string& text, const char* key) {
4356 73 std::size_t k = text.find(key);
4357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (k == std::string::npos) {
4358 ✗ return -1;
4359 }
4360 73 std::size_t c = text.find(':', k);
4361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (c == std::string::npos) {
4362 ✗ return -1;
4363 }
4364 73 return std::atoi(text.c_str() + c + 1);
4365 }
4366
4367 // Say once that the position is going out uncorrected and why: once per
4368 // media at most, and once per reason for the whole run -- each pass of a
4369 // looping clip is a new media output, and a board with no usable sink would
4370 // otherwise log it at every pass. Deliberately not a WarningHolder banner:
4371 // the condition is a tuning shortfall rather than a fault the user must act
4372 // on immediately.
4373 792 void GStreamerOutput::NoteNoSinkLatency(const std::string& why) {
4374
2/2
✓ Branch 0 taken 710 times.
✓ Branch 1 taken 82 times.
792 if (m_warnedNoSinkLatency) {
4375 710 return;
4376 }
4377 82 m_warnedNoSinkLatency = true;
4378 static std::mutex saidLock;
4379
3/4
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
82 static std::set<std::string> said;
4380 {
4381
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
82 std::lock_guard<std::mutex> lock(saidLock);
4382
3/4
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 49 times.
82 if (!said.insert(why).second) {
4383
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 LogDebug(VB_MEDIAOUT, "GStreamer: no audio sink latency available (%s)\n", why.c_str());
4384 33 return;
4385 }
4386
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 33 times.
82 }
4387 49 LogWarn(VB_MEDIAOUT,
4388 "GStreamer: no audio sink latency available (%s) — reported position is not "
4389 "corrected for the card's queue, so the sequence may lead the audio by tens of ms\n",
4390 why.c_str());
4391 }
4392
4393 // The card's queue depth as last read, by status path. It belongs to the
4394 // card, not to the track: every track has its own GStreamerOutput, and while
4395 // the reading was kept per track each one started from no reading at all.
4396 // The first track after the card had suspended (the default between songs)
4397 // then found the card still waking, took that as "no correction" for 250 ms,
4398 // and reported a position the whole queue (50-80 ms) ahead of the sound until
4399 // the first real reading snapped it back (GH #3212).
4400 static std::mutex s_cardLatencyLock;
4401 static std::map<std::string, int64_t> s_cardLatencyNs;
4402
4403 7692 int64_t GStreamerOutput::AudioSinkLatencyNs(bool* notRunning) {
4404
1/2
✓ Branch 0 taken 7692 times.
✗ Branch 1 not taken.
7692 if (notRunning) {
4405 7692 *notRunning = false;
4406 }
4407 // The queue depth is essentially constant while a track plays; re-reading
4408 // it on every Process() would put a /proc read in the main loop at frame
4409 // rate for no benefit.
4410
1/2
✓ Branch 0 taken 7692 times.
✗ Branch 1 not taken.
7692 uint64_t now = GetMonotonicTimeMS();
4411
4/4
✓ Branch 0 taken 7604 times.
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 6833 times.
✓ Branch 3 taken 771 times.
7692 if (m_sinkLatencyCheckedMs != 0 && (now - m_sinkLatencyCheckedMs) < 250) {
4412 6833 return m_sinkLatencyNs;
4413 }
4414
4415
2/2
✓ Branch 0 taken 798 times.
✓ Branch 1 taken 61 times.
859 if (m_alsaStatusPath.empty()) {
4416 798 m_sinkLatencyCheckedMs = now;
4417
1/2
✓ Branch 0 taken 798 times.
✗ Branch 1 not taken.
798 int card = AlsaSinkCardNumber();
4418
2/2
✓ Branch 0 taken 792 times.
✓ Branch 1 taken 6 times.
798 if (card < 0) {
4419 // Nothing to read means no correction, which is not a small thing:
4420 // uncorrected, the sequence leads the sound by the whole ALSA queue
4421 // depth -- 22 to 160 ms on the boards this was measured on. That is
4422 // audible and looks exactly like a misconfigured show, so say so
4423 // once rather than leaving the user to find it by ear.
4424
2/4
✓ Branch 0 taken 792 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 792 times.
✗ Branch 3 not taken.
792 NoteNoSinkLatency("no FPP ALSA sink adapter matches the configured audio output");
4425 792 m_sinkLatencyNs = 0;
4426 792 return 0;
4427 }
4428
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 m_alsaStatusPath = AlsaPlaybackStatusPath(card);
4429
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 m_alsaHwParamsPath = m_alsaStatusPath;
4430
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 std::size_t s = m_alsaHwParamsPath.rfind("/status");
4431
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (s != std::string::npos) {
4432
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 m_alsaHwParamsPath.replace(s, 7, "/hw_params");
4433 }
4434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (m_alsaStatusPath.empty()) {
4435 ✗ NoteNoSinkLatency("card " + std::to_string(card) + " exposes no playback substream status");
4436 ✗ m_sinkLatencyNs = 0;
4437 ✗ return 0;
4438 }
4439
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 std::lock_guard<std::mutex> lock(s_cardLatencyLock);
4440
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 auto it = s_cardLatencyNs.find(m_alsaStatusPath);
4441
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 m_sinkLatencyNs = it == s_cardLatencyNs.end() ? 0 : it->second;
4442 6 }
4443
4444
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 std::string status = GetFileContents(m_alsaStatusPath);
4445
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 int delayFrames = status.find("state: RUNNING") == std::string::npos ? 0 : ReadIntField(status, "delay");
4446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (delayFrames <= 0) {
4447 // Not playing. Before it has played any of this track (opening, or
4448 // waking from suspend) nothing of it is heard yet; later on (an xrun)
4449 // the position carries on as it was. Not cached, so the first
4450 // reading once it runs is taken at the next call rather than up to
4451 // 250 ms later. What it queued last time is still the best guess at
4452 // what it will queue now.
4453 ✗ if (notRunning && !m_sinkSeenRunning) {
4454 ✗ *notRunning = true;
4455 }
4456 ✗ return m_sinkLatencyNs;
4457 }
4458 67 m_sinkLatencyCheckedMs = now;
4459 67 m_sinkSeenRunning = true;
4460
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 61 times.
67 if (m_alsaRate <= 0) {
4461
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 m_alsaRate = ReadIntField(GetFileContents(m_alsaHwParamsPath), "rate");
4462 }
4463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (m_alsaRate <= 0) {
4464 ✗ NoteNoSinkLatency("could not read the card's rate from hw_params");
4465 ✗ m_sinkLatencyNs = 0;
4466 ✗ return 0;
4467 }
4468 67 int64_t ns = (int64_t)delayFrames * 1000000000LL / m_alsaRate;
4469 // A sane sink is a few ms to a few hundred; anything past a second means
4470 // the field was misread (dmix, for one, leaves appl_ptr at 0 and reports a
4471 // hugely negative delay) and is not worth trusting.
4472
2/4
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 67 times.
67 if (ns < 0 || ns > 1000000000LL) {
4473 ✗ NoteNoSinkLatency("card reports an implausible queue depth (" +
4474 ✗ std::to_string(delayFrames) + " frames at " +
4475 ✗ std::to_string(m_alsaRate) + " Hz)");
4476 ✗ m_sinkLatencyNs = 0;
4477 ✗ return 0;
4478 }
4479 // The queue saw-tooths through a whole graph quantum (~21 ms) as PipeWire
4480 // refills it, and each reading lands anywhere on the tooth. What the
4481 // sound is behind by on average is the mean, so average the readings
4482 // (~2 s at one every 250 ms): taken raw, they moved the position, and the
4483 // lights with it, by up to a quantum every quarter second.
4484
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 6 times.
67 m_sinkLatencyNs = m_sinkLatencyNs > 0 ? m_sinkLatencyNs + (ns - m_sinkLatencyNs) / 8 : ns;
4485
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 std::lock_guard<std::mutex> lock(s_cardLatencyLock);
4486
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 s_cardLatencyNs[m_alsaStatusPath] = m_sinkLatencyNs;
4487 67 return m_sinkLatencyNs;
4488 67 }
4489
4490 194445 int GStreamerOutput::Process(void) {
4491 // Busy being started or stopped on another thread: skip this tick rather
4492 // than wait for it (see m_opMutex).
4493 194445 std::unique_lock<std::recursive_mutex> opLock(m_opMutex, std::try_to_lock);
4494
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 194357 times.
194445 if (!opLock.owns_lock()) {
4495 88 return 1;
4496 }
4497 #ifdef HAS_VIDEO_DISPLAY
4498
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 194355 times.
194357 if (m_releaseDisplayPending.exchange(false)) {
4499
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 ReleaseDisplay(); // the file turned out to have no picture (OnNoMorePads())
4500 }
4501 #endif
4502
6/8
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 194356 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 194356 times.
194357 if (m_stopRequested.exchange(false) && m_pipeline && !m_teardownComplete) {
4503
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogInfo(VB_MEDIAOUT, "GStreamer: stopping %s on request\n", m_mediaFilename.c_str());
4504
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 opLock.unlock();
4505
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 Stop();
4506 1 return 1;
4507 }
4508
3/4
✓ Branch 0 taken 194327 times.
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 194327 times.
194356 if (!m_pipeline || !m_bus) {
4509 29 return 0;
4510 }
4511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194327 times.
194327 if (m_pwFeedOrphaned.exchange(false)) {
4512 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: %s has no sound; releasing its output stream\n", m_mediaFilename.c_str());
4513 ✗ ReleasePipeWireStream();
4514 }
4515
5/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 194325 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 194325 times.
194329 if (m_haveFailedBranches.load(std::memory_order_acquire) && !m_teardownComplete &&
4516
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !m_shutdownFlag.load(std::memory_order_acquire)) {
4517
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 SetMainLoopPhase("GStreamer remove failed branch");
4518
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 RemoveFailedBranches();
4519 }
4520 // Sub-phases for the main-loop stall watchdog (issue #2727): everything
4521 // below runs on the main loop and calls into GStreamer, which takes both
4522 // element locks and (via pipewiresink) PipeWire's thread-loop lock, so a
4523 // sink that is stuck in preroll can park the main loop right here.
4524
1/2
✓ Branch 0 taken 194327 times.
✗ Branch 1 not taken.
194327 SetMainLoopPhase("GStreamer ProcessMessages");
4525
1/2
✓ Branch 0 taken 194327 times.
✗ Branch 1 not taken.
194327 ProcessMessages();
4526
4527 // Update position
4528
2/2
✓ Branch 0 taken 194157 times.
✓ Branch 1 taken 170 times.
194327 if (m_playing) {
4529 194157 gint64 pos = 0, dur = 0;
4530 // Query position from a sync'd sink, NOT the pipeline.
4531 // Pipeline-level queries return the demuxer's read-ahead position,
4532 // which races far ahead when sinks use leaky queues or async=FALSE.
4533 // Prefer kmssink (sync=TRUE, video playout position), then a
4534 // consumer direct-kmssink (sync=TRUE), then audio pipewiresink
4535 // (sync=TRUE), then the pipeline as fallback.
4536 194157 GstElement* posSource = nullptr;
4537 194157 bool ownPosSource = false;
4538 {
4539 // OnNoMorePads() removes the kmssink of a file that turned out to
4540 // have no video, on a streaming thread, under the callback guard;
4541 // the bin's was the only reference, so take our own under it.
4542
1/2
✓ Branch 0 taken 194157 times.
✗ Branch 1 not taken.
194157 std::shared_ptr<CallbackGuard> guard = m_cbGuard.load();
4543 194157 std::unique_lock<std::recursive_mutex> lock;
4544
1/2
✓ Branch 0 taken 194157 times.
✗ Branch 1 not taken.
194157 if (guard) {
4545
1/2
✓ Branch 0 taken 194157 times.
✗ Branch 1 not taken.
194157 lock = std::unique_lock<std::recursive_mutex>(guard->mtx);
4546 }
4547 // A video whose sound feeds the output stream follows the sound
4548 // (below), and its picture is held back to match (see
4549 // AlignPictureToSound()).
4550
4/4
✓ Branch 0 taken 9342 times.
✓ Branch 1 taken 184815 times.
✓ Branch 2 taken 6888 times.
✓ Branch 3 taken 2454 times.
194157 if (m_kmssink && !m_pwFeed) {
4551
3/6
✓ Branch 0 taken 6888 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6888 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6888 times.
✗ Branch 5 not taken.
6888 posSource = GST_ELEMENT(gst_object_ref(m_kmssink));
4552 6888 ownPosSource = true;
4553 }
4554 194157 }
4555
8/8
✓ Branch 0 taken 187269 times.
✓ Branch 1 taken 6888 times.
✓ Branch 2 taken 10834 times.
✓ Branch 3 taken 176435 times.
✓ Branch 4 taken 1364 times.
✓ Branch 5 taken 9470 times.
✓ Branch 6 taken 1364 times.
✓ Branch 7 taken 192793 times.
194157 if (!posSource && !m_pwFeed && !m_directConnectorIds.empty()) {
4556
1/2
✓ Branch 0 taken 1364 times.
✗ Branch 1 not taken.
1364 std::string name = "dkms_" + std::to_string(*m_directConnectorIds.begin());
4557
3/6
✓ Branch 0 taken 1364 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1364 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1364 times.
✗ Branch 5 not taken.
1364 posSource = gst_bin_get_by_name(GST_BIN(m_pipeline), name.c_str());
4558
2/2
✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 125 times.
1364 if (posSource) ownPosSource = true;
4559 1364 }
4560 // Only the audio sink's position gets the ALSA-queue correction below;
4561 // a kmssink is reporting video playout, which is a different path.
4562 194157 bool posFromAudioSink = false;
4563
4/4
✓ Branch 0 taken 186030 times.
✓ Branch 1 taken 8127 times.
✓ Branch 2 taken 176435 times.
✓ Branch 3 taken 9595 times.
194157 if (!posSource && m_pwFeed) {
4564 // Feeding the persistent output stream: the feed is the synced
4565 // sink, and what it has rendered has only just entered the stream.
4566
3/6
✓ Branch 0 taken 176435 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 176435 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 176435 times.
✗ Branch 5 not taken.
176435 posSource = GST_ELEMENT(gst_object_ref(m_pwFeed));
4567 176435 ownPosSource = true;
4568 176435 posFromAudioSink = true;
4569 }
4570
2/2
✓ Branch 0 taken 9595 times.
✓ Branch 1 taken 184562 times.
194157 if (!posSource) {
4571
1/2
✓ Branch 0 taken 9595 times.
✗ Branch 1 not taken.
9595 SetMainLoopPhase("GStreamer get pwsink");
4572
3/6
✓ Branch 0 taken 9595 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9595 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9595 times.
✗ Branch 5 not taken.
9595 posSource = gst_bin_get_by_name(GST_BIN(m_pipeline), "pwsink");
4573
2/2
✓ Branch 0 taken 9181 times.
✓ Branch 1 taken 414 times.
9595 if (posSource) {
4574 9181 ownPosSource = true;
4575 9181 posFromAudioSink = true;
4576 }
4577 }
4578
2/2
✓ Branch 0 taken 414 times.
✓ Branch 1 taken 193743 times.
194157 if (!posSource) posSource = m_pipeline;
4579
1/2
✓ Branch 0 taken 194157 times.
✗ Branch 1 not taken.
194157 SetMainLoopPhase("GStreamer query position");
4580
1/2
✓ Branch 0 taken 194157 times.
✗ Branch 1 not taken.
194157 bool havePos = gst_element_query_position(posSource, GST_FORMAT_TIME, &pos);
4581
5/8
✓ Branch 0 taken 188842 times.
✓ Branch 1 taken 5315 times.
✓ Branch 2 taken 188842 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 188842 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 194157 times.
194157 if (havePos && m_startSeekPending && m_startSeekPending->load()) {
4582 // Prerolled at 0 and not yet seeked to the start position (see
4583 // Start()). Reporting 0 now would drag the sequence back to it.
4584 ✗ havePos = false;
4585 }
4586 // The duration only moves as a VBR estimate is refined, but a
4587 // pipeline-wide query is anything but cheap: the bin asks every sink,
4588 // and each one walks back through the tee and the decoder to the
4589 // parser, which converts filesrc's size (an fstat apiece). Once a
4590 // second is plenty; at the main loop's 100 Hz it was most of its
4591 // CPU on a single-core board.
4592 194157 bool haveDur = false;
4593
1/2
✓ Branch 0 taken 194157 times.
✗ Branch 1 not taken.
194157 uint64_t nowMs = GetMonotonicTimeMS();
4594
4/4
✓ Branch 0 taken 191799 times.
✓ Branch 1 taken 2358 times.
✓ Branch 2 taken 3516 times.
✓ Branch 3 taken 188283 times.
194157 if (m_maxDuration <= 0 || nowMs - m_durationQueriedMs >= 1000) {
4595 5874 m_durationQueriedMs = nowMs;
4596
1/2
✓ Branch 0 taken 5874 times.
✗ Branch 1 not taken.
5874 SetMainLoopPhase("GStreamer query duration");
4597
1/2
✓ Branch 0 taken 5874 times.
✗ Branch 1 not taken.
5874 haveDur = gst_element_query_duration(m_pipeline, GST_FORMAT_TIME, &dur);
4598 }
4599
1/2
✓ Branch 0 taken 194157 times.
✗ Branch 1 not taken.
194157 SetMainLoopPhase("GStreamer position post-processing");
4600
3/4
✓ Branch 0 taken 193743 times.
✓ Branch 1 taken 414 times.
✓ Branch 2 taken 193743 times.
✗ Branch 3 not taken.
194157 if (ownPosSource) gst_object_unref(posSource);
4601
4602 // One-shot: log pipeline clock and sink sync state on first position update
4603
4/4
✓ Branch 0 taken 188842 times.
✓ Branch 1 taken 5315 times.
✓ Branch 2 taken 1013 times.
✓ Branch 3 taken 187829 times.
194157 if (havePos && m_wallStartMs == 0) {
4604
1/2
✓ Branch 0 taken 1013 times.
✗ Branch 1 not taken.
1013 m_wallStartMs = GetMonotonicTimeMS(); // record wall time at first pos (also used by preroll watchdog)
4605 // A valid position means the pipeline prerolled and the decoder
4606 // is producing — clear the consecutive-wedge escalation counter.
4607 1013 s_consecutiveWedgeEvents = 0;
4608
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1012 times.
1013 if (m_pendingSeekNs >= 0) {
4609 1 gint64 seekNs = m_pendingSeekNs;
4610 1 m_pendingSeekNs = -1;
4611
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogDebug(VB_MEDIAOUT, "GStreamer: prerolled, applying held seek to %0.3fs\n",
4612 (double)seekNs / GST_SECOND);
4613
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 EndTimelineShift();
4614
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!gst_element_seek(m_pipeline, 1.0, GST_FORMAT_TIME,
4615 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
4616 GST_SEEK_TYPE_SET, seekNs, GST_SEEK_TYPE_NONE, 0)) {
4617 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: held seek to %0.3fs failed\n", (double)seekNs / GST_SECOND);
4618 }
4619 }
4620 // The remaining pipeline/sink introspection here exists purely to
4621 // populate the debug log — skip the GStreamer queries entirely when
4622 // MediaOut isn't at DEBUG (or more verbose).
4623
3/4
✓ Branch 0 taken 1013 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✓ Branch 3 taken 930 times.
1013 if (WillLog(LOG_DEBUG, VB_MEDIAOUT)) {
4624
3/6
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83 times.
✗ Branch 5 not taken.
83 GstClock* clock = gst_pipeline_get_clock(GST_PIPELINE(m_pipeline));
4625
2/4
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
83 LogDebug(VB_MEDIAOUT, "GStreamer: pipeline clock=%s pos=%.1fs\n",
4626 clock ? GST_OBJECT_NAME(clock) : "(none)",
4627 (float)pos / GST_SECOND);
4628
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 if (clock) {
4629
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 GstClockTime ct = gst_clock_get_time(clock);
4630
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 GstClockTime bt = gst_element_get_base_time(m_pipeline);
4631
9/18
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 83 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 83 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 83 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 83 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 83 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 83 times.
✗ Branch 17 not taken.
83 LogDebug(VB_MEDIAOUT, "GStreamer: clock_time=%" GST_TIME_FORMAT " base_time=%" GST_TIME_FORMAT "\n",
4632 GST_TIME_ARGS(ct), GST_TIME_ARGS(bt));
4633
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 gst_object_unref(clock);
4634 }
4635 // Log consumer kmssink stats (rendered/dropped frame counts)
4636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 for (int cid : m_directConnectorIds) {
4637 ✗ std::string name = "dkms_" + std::to_string(cid);
4638 ✗ GstElement* dkms = gst_bin_get_by_name(GST_BIN(m_pipeline), name.c_str());
4639 ✗ if (dkms) {
4640 ✗ GstStructure* stats = nullptr;
4641 ✗ g_object_get(dkms, "stats", &stats, NULL);
4642 ✗ if (stats) {
4643 ✗ guint64 rendered = 0, dropped = 0;
4644 ✗ gst_structure_get_uint64(stats, "rendered", &rendered);
4645 ✗ gst_structure_get_uint64(stats, "dropped", &dropped);
4646 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: %s stats: rendered=%" G_GUINT64_FORMAT " dropped=%" G_GUINT64_FORMAT "\n",
4647 name.c_str(), rendered, dropped);
4648 ✗ gst_structure_free(stats);
4649 }
4650 GstState st, pend;
4651 ✗ gst_element_get_state(dkms, &st, &pend, 0);
4652 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: %s state=%d pending=%d\n", name.c_str(), st, pend);
4653 ✗ gst_object_unref(dkms);
4654 }
4655 ✗ }
4656 // Check sync on sinks
4657
3/6
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83 times.
✗ Branch 5 not taken.
83 GstElement* pwsink = gst_bin_get_by_name(GST_BIN(m_pipeline), "pwsink");
4658
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 53 times.
83 if (pwsink) {
4659
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 gboolean s; g_object_get(pwsink, "sync", &s, NULL);
4660
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 LogDebug(VB_MEDIAOUT, "GStreamer: pwsink sync=%d\n", s);
4661
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 gst_object_unref(pwsink);
4662 }
4663
3/6
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83 times.
✗ Branch 5 not taken.
83 GstElement* kms = gst_bin_get_by_name(GST_BIN(m_pipeline), "kmsvideosink");
4664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (kms) {
4665 ✗ gboolean s; g_object_get(kms, "sync", &s, NULL);
4666 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: kmssink sync=%d\n", s);
4667 ✗ gst_object_unref(kms);
4668 }
4669 }
4670 }
4671
4672 // Periodic wall-clock vs stream-position diagnostic (DEBUG only — the
4673 // stall/preroll watchdogs below run independently of this block).
4674
8/10
✓ Branch 0 taken 188842 times.
✓ Branch 1 taken 5315 times.
✓ Branch 2 taken 188842 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 188842 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 23766 times.
✓ Branch 7 taken 165076 times.
✓ Branch 8 taken 23766 times.
✓ Branch 9 taken 170391 times.
194157 if (havePos && m_wallStartMs > 0 && WillLog(LOG_DEBUG, VB_MEDIAOUT)) {
4675
1/2
✓ Branch 0 taken 23766 times.
✗ Branch 1 not taken.
23766 uint64_t wallMs = GetMonotonicTimeMS() - m_wallStartMs;
4676 23766 float wallSec = wallMs / 1000.0f;
4677 23766 float streamSec = (float)pos / GST_SECOND;
4678
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 23732 times.
23766 if (wallMs - m_lastWallLogMs > 5000) {
4679 34 m_lastWallLogMs = wallMs;
4680
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 float ratio = (wallSec > 0.1f) ? streamSec / wallSec : 0.0f;
4681
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 LogDebug(VB_MEDIAOUT, "GStreamer: wall=%.1fs stream=%.1fs ratio=%.2fx\n",
4682 wallSec, streamSec, ratio);
4683 // Log pipeline clock time and consumer kmssink stats at each 5s checkpoint
4684
3/6
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 34 times.
✗ Branch 5 not taken.
34 GstClock* chkClock = gst_pipeline_get_clock(GST_PIPELINE(m_pipeline));
4685
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (chkClock) {
4686
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 GstClockTime ct = gst_clock_get_time(chkClock);
4687
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 GstClockTime bt = gst_element_get_base_time(m_pipeline);
4688
21/42
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 34 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 34 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 34 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 34 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 34 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 34 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 34 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 34 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 34 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 34 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 34 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 34 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 34 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 34 times.
✗ Branch 31 not taken.
✓ Branch 32 taken 34 times.
✗ Branch 33 not taken.
✓ Branch 34 taken 34 times.
✗ Branch 35 not taken.
✓ Branch 36 taken 34 times.
✗ Branch 37 not taken.
✓ Branch 38 taken 34 times.
✗ Branch 39 not taken.
✓ Branch 40 taken 34 times.
✗ Branch 41 not taken.
34 LogDebug(VB_MEDIAOUT, "GStreamer: clock_time=%" GST_TIME_FORMAT " base_time=%" GST_TIME_FORMAT " running=%" GST_TIME_FORMAT "\n",
4689 GST_TIME_ARGS(ct), GST_TIME_ARGS(bt),
4690 GST_TIME_ARGS(ct >= bt ? ct - bt : 0));
4691
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 gst_object_unref(chkClock);
4692 }
4693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 for (int cid : m_directConnectorIds) {
4694 ✗ std::string name = "dkms_" + std::to_string(cid);
4695 ✗ GstElement* dkms = gst_bin_get_by_name(GST_BIN(m_pipeline), name.c_str());
4696 ✗ if (dkms) {
4697 GstState st, pend;
4698 ✗ gst_element_get_state(dkms, &st, &pend, 0);
4699 ✗ GstStructure* stats = nullptr;
4700 ✗ g_object_get(dkms, "stats", &stats, NULL);
4701 ✗ if (stats) {
4702 ✗ guint64 rendered = 0, dropped = 0;
4703 ✗ gst_structure_get_uint64(stats, "rendered", &rendered);
4704 ✗ gst_structure_get_uint64(stats, "dropped", &dropped);
4705 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: %s rendered=%" G_GUINT64_FORMAT " dropped=%" G_GUINT64_FORMAT " state=%d pending=%d\n",
4706 name.c_str(), rendered, dropped, st, pend);
4707 ✗ gst_structure_free(stats);
4708 }
4709 ✗ gst_object_unref(dkms);
4710 }
4711 ✗ }
4712 }
4713 }
4714
4715
2/2
✓ Branch 0 taken 188842 times.
✓ Branch 1 taken 5315 times.
194157 if (havePos) {
4716 // Track the maximum observed duration — VBR MP3 files can report
4717 // fluctuating durations as GStreamer revises its estimate during
4718 // decoding. Using the max prevents time-remaining from jumping
4719 // backwards or going negative.
4720
4/4
✓ Branch 0 taken 3468 times.
✓ Branch 1 taken 185374 times.
✓ Branch 2 taken 140 times.
✓ Branch 3 taken 3328 times.
188842 if (haveDur && dur > m_maxDuration) {
4721 140 m_maxDuration = dur;
4722 }
4723 188842 gint64 effectiveDur = m_maxDuration;
4724
4725 188842 float elapsed = (float)pos / GST_SECOND;
4726
2/2
✓ Branch 0 taken 188385 times.
✓ Branch 1 taken 457 times.
188842 float remaining = (effectiveDur > pos) ? (float)(effectiveDur - pos) / GST_SECOND : 0.0f;
4727
4728 // Back the position up to what is actually leaving the DAC, so the
4729 // sequence servo and the MultiSync packets both follow the sound
4730 // rather than the graph's write pointer. See AudioSinkLatencyNs().
4731
2/2
✓ Branch 0 taken 180317 times.
✓ Branch 1 taken 8525 times.
188842 if (posFromAudioSink) {
4732 // Fed into the output stream, what is heard is measured: the
4733 // stream knows which of the feed's samples is leaving the
4734 // speaker now (HeardRunningTime()), and the position is backed
4735 // up by however far the pipeline's running time is past it --
4736 // the stream's queue, the graph and the card, and, for a track
4737 // carrying on the stream's timeline, the lead it runs ahead.
4738 180317 int64_t latNs = 0;
4739 180317 bool measured = false;
4740
3/4
✓ Branch 0 taken 172976 times.
✓ Branch 1 taken 7341 times.
✓ Branch 2 taken 172976 times.
✗ Branch 3 not taken.
180317 if (m_pwFeed && m_pwStream) {
4741 172976 GstClockTime heardRt = 0;
4742
1/2
✓ Branch 0 taken 172976 times.
✗ Branch 1 not taken.
172976 int heard = m_pwStream->HeardRunningTime(m_pwFeed, &heardRt);
4743
2/2
✓ Branch 0 taken 169614 times.
✓ Branch 1 taken 3362 times.
172976 if (heard == 1) {
4744
2/4
✓ Branch 0 taken 169614 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 169614 times.
✗ Branch 3 not taken.
169614 if (GstClock* clock = gst_element_get_clock(m_pipeline)) {
4745
1/2
✓ Branch 0 taken 169614 times.
✗ Branch 1 not taken.
169614 GstClockTime now = gst_clock_get_time(clock);
4746
1/2
✓ Branch 0 taken 169614 times.
✗ Branch 1 not taken.
169614 gst_object_unref(clock);
4747
1/2
✓ Branch 0 taken 169614 times.
✗ Branch 1 not taken.
169614 latNs = (int64_t)(now - gst_element_get_base_time(m_pipeline)) - (int64_t)heardRt;
4748 // The sound is never further ahead of the running
4749 // time than the feed's lead (as it is after a
4750 // flush mid-track, the feed's buffers coming that
4751 // early into an empty queue): a measurement that
4752 // says so is not of this track.
4753
1/2
✓ Branch 0 taken 169614 times.
✗ Branch 1 not taken.
169614 measured = latNs > -(PipeWireOutputStream::FeedLeadNs() + (int64_t)(50 * GST_MSECOND));
4754
2/2
✓ Branch 0 taken 169605 times.
✓ Branch 1 taken 9 times.
169614 if (measured) {
4755
1/2
✓ Branch 0 taken 169605 times.
✗ Branch 1 not taken.
169605 AlignPictureToSound(latNs);
4756
2/4
✓ Branch 0 taken 169605 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 169605 times.
✗ Branch 3 not taken.
169605 if (m_streamSlot >= 1 && m_streamSlot <= StreamSlotManager::MAX_SLOTS) {
4757 169605 s_pictureDelayBaseNs[m_streamSlot] =
4758
1/2
✓ Branch 0 taken 169605 times.
✗ Branch 1 not taken.
339210 latNs - (m_timelineShiftNs ? m_timelineShiftNs->load() : 0);
4759 }
4760 } else {
4761 9 latNs = 0;
4762 }
4763 }
4764
2/2
✓ Branch 0 taken 3019 times.
✓ Branch 1 taken 343 times.
3362 } else if (heard == 0) {
4765 // None of it heard yet: the sound before it (the last
4766 // track's tail) is still playing. It is at its start.
4767 3019 latNs = pos;
4768 3019 measured = true;
4769
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 342 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
343 } else if (heard == -2 && m_pictureDelayNs > 0) {
4770 // Nothing of it in the stream (a seek flushed it and
4771 // the pipeline is prerolling again, or it ran dry):
4772 // what was last measured still holds. Measured now,
4773 // it would grow by the wait, and the picture held
4774 // back by that much showed the seek's first frame
4775 // seconds late.
4776 1 latNs = m_pictureDelayNs;
4777 1 measured = true;
4778 }
4779 }
4780
2/2
✓ Branch 0 taken 172625 times.
✓ Branch 1 taken 7692 times.
180317 if (measured) {
4781 172625 m_measuredLatNs = latNs;
4782
1/2
✓ Branch 0 taken 172625 times.
✗ Branch 1 not taken.
172625 m_measuredLatMs = GetMonotonicTimeMS();
4783 } else {
4784 // No timing from PipeWire: the card's own queue, and the
4785 // lead a track carrying on the timeline runs ahead. A
4786 // card not yet playing has played none of it.
4787 7692 bool notRunning = false;
4788
1/2
✓ Branch 0 taken 7692 times.
✗ Branch 1 not taken.
7692 latNs = AudioSinkLatencyNs(&notRunning);
4789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7692 times.
7692 if (notRunning) {
4790 ✗ latNs = pos;
4791 } else {
4792
1/2
✓ Branch 0 taken 7692 times.
✗ Branch 1 not taken.
7692 if (m_timelineShiftNs) {
4793 15384 latNs += m_timelineShiftNs->load();
4794 }
4795
2/2
✓ Branch 0 taken 351 times.
✓ Branch 1 taken 7341 times.
7692 if (m_pwFeed) {
4796
1/2
✓ Branch 0 taken 351 times.
✗ Branch 1 not taken.
351 AlignPictureToSound(latNs);
4797 }
4798 }
4799 }
4800 180317 float latSec = (float)latNs / 1000000000.0f;
4801 180317 elapsed -= latSec;
4802
2/2
✓ Branch 0 taken 322 times.
✓ Branch 1 taken 179995 times.
180317 if (elapsed < 0.0f) {
4803 322 elapsed = 0.0f;
4804 }
4805 180317 remaining += latSec;
4806
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 180312 times.
180317 if (remaining < 0.0f) {
4807 5 remaining = 0.0f;
4808 }
4809 // Not while the position is held at the start (latNs == pos:
4810 // none of the track heard yet): that is no correction, and
4811 // logged once it stood in for the one that follows.
4812
6/6
✓ Branch 0 taken 9539 times.
✓ Branch 1 taken 170778 times.
✓ Branch 2 taken 998 times.
✓ Branch 3 taken 8541 times.
✓ Branch 4 taken 482 times.
✓ Branch 5 taken 516 times.
180317 if (!m_loggedSinkLatency && latSec != 0.0f && latNs != pos) {
4813 482 m_loggedSinkLatency = true;
4814
3/4
✓ Branch 0 taken 476 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 482 times.
✗ Branch 3 not taken.
482 LogDebug(VB_MEDIAOUT, "GStreamer: position backed up %.1f ms to the sound (%s)\n",
4815 latSec * 1000.0f, measured ? "measured" : "estimated");
4816 }
4817 }
4818 // Audible before this report, not just as of it: see below.
4819 188842 bool wasAudible = m_audible;
4820
2/2
✓ Branch 0 taken 185459 times.
✓ Branch 1 taken 3383 times.
188842 if (elapsed > 0.0f) {
4821 185459 m_audible = true;
4822 }
4823
1/2
✓ Branch 0 taken 188842 times.
✗ Branch 1 not taken.
188842 setMediaElapsed(elapsed, remaining);
4824
4825 // Only the primary slot represents the show's synced position;
4826 // companion streams (slot > 1, e.g. extraMedia) play locally on
4827 // each device and must not broadcast themselves as the master.
4828
8/8
✓ Branch 0 taken 184555 times.
✓ Branch 1 taken 4287 times.
✓ Branch 2 taken 176631 times.
✓ Branch 3 taken 7924 times.
✓ Branch 4 taken 3666 times.
✓ Branch 5 taken 172965 times.
✓ Branch 6 taken 3666 times.
✓ Branch 7 taken 185176 times.
188842 if (m_streamSlot == 1 && m_drivesShowClock && multiSync->isMultiSyncEnabled()) {
4829
1/2
✓ Branch 0 taken 3666 times.
✗ Branch 1 not taken.
3666 multiSync->SendMediaSyncPacket(m_mediaFilename, m_mediaOutputStatus->mediaSeconds);
4830 }
4831 // Only the show's own media paces the sequence. Media started
4832 // beside it (Play Media on another slot, a companion) followed the
4833 // same servo, so the sequence slewed toward whichever clock
4834 // reported last.
4835 // Not until the sound has been audible for a report. Until then
4836 // the sequence running is not this media's: it is held until the
4837 // media is audible (PlaylistEntryBoth), and the one before it is
4838 // still playing the end of its own song's sound, which carries on
4839 // in the output stream ahead of this one's. Steered toward this
4840 // media's start, that one held its last frames and left the
4841 // light timing off for the next sequence's start.
4842
4/4
✓ Branch 0 taken 177110 times.
✓ Branch 1 taken 11732 times.
✓ Branch 2 taken 174427 times.
✓ Branch 3 taken 2683 times.
188842 if (m_drivesShowClock && wasAudible) {
4843 // The position is in card time when it comes from the output
4844 // stream; frames go out at the same pace.
4845
2/2
✓ Branch 0 taken 162352 times.
✓ Branch 1 taken 12075 times.
174427 if (m_pwStream) {
4846
1/2
✓ Branch 0 taken 162352 times.
✗ Branch 1 not taken.
162352 SetMediaClockRate(m_pwStream->ClockRate());
4847 }
4848
1/2
✓ Branch 0 taken 174427 times.
✗ Branch 1 not taken.
174427 CalculateNewChannelOutputDelay(m_mediaOutputStatus->mediaSeconds);
4849 }
4850
4851 // Always update total duration — it may be refined for VBR media
4852
2/2
✓ Branch 0 taken 188448 times.
✓ Branch 1 taken 394 times.
188842 if (effectiveDur > 0) {
4853 188448 int totalSecs = (int)(effectiveDur / GST_SECOND);
4854 188448 int newMin = totalSecs / 60;
4855 188448 int newSec = totalSecs % 60;
4856
1/2
✓ Branch 0 taken 188448 times.
✗ Branch 1 not taken.
188448 std::lock_guard<std::mutex> lock(MediaOutputStatus::FieldsLock());
4857
1/2
✓ Branch 0 taken 188448 times.
✗ Branch 1 not taken.
188448 if (newMin != m_mediaOutputStatus->minutesTotal ||
4858
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 188318 times.
188448 newSec != m_mediaOutputStatus->secondsTotal) {
4859 130 m_mediaOutputStatus->minutesTotal = newMin;
4860 130 m_mediaOutputStatus->secondsTotal = newSec;
4861
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 LogDebug(VB_MEDIAOUT, "GStreamer duration: %d:%02d\n", newMin, newSec);
4862 }
4863 188448 }
4864
4865 // Stall watchdog: detect when PipeWire stops consuming data
4866 // (e.g. HDMI sink unplugged causing combine-stream to block)
4867 // Skip watchdog near end of media — position naturally stops advancing
4868
4/4
✓ Branch 0 taken 188448 times.
✓ Branch 1 taken 394 times.
✓ Branch 2 taken 15311 times.
✓ Branch 3 taken 173137 times.
188842 bool nearEnd = (effectiveDur > 0 && (effectiveDur - pos) < GST_SECOND);
4869
4870
2/2
✓ Branch 0 taken 186798 times.
✓ Branch 1 taken 2044 times.
188842 if (pos != m_lastPosition) {
4871 186798 m_lastPosition = pos;
4872 186798 m_stallStartMs = 0; // position advancing, clear stall timer
4873
2/2
✓ Branch 0 taken 383 times.
✓ Branch 1 taken 1661 times.
2044 } else if (nearEnd) {
4874 // Position at/near end of media — this is natural completion, not a stall.
4875 // Wait for EOS from the pipeline, or handle it ourselves after a grace period.
4876
1/2
✓ Branch 0 taken 383 times.
✗ Branch 1 not taken.
383 uint64_t now = GetMonotonicTimeMS();
4877
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 299 times.
383 if (m_stallStartMs == 0) {
4878 84 m_stallStartMs = now;
4879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 299 times.
299 } else if ((now - m_stallStartMs) > (STALL_TIMEOUT_MS * 2)) {
4880 // EOS should have arrived by now but didn't — force end
4881 ✗ LogInfo(VB_MEDIAOUT, "GStreamer: media reached end (%.1fs/%.1fs), forcing stop\n",
4882 elapsed, (float)effectiveDur / GST_SECOND);
4883 ✗ m_playing = false;
4884 ✗ if (m_mediaOutputStatus && OwnsSlotStatus()) {
4885 ✗ m_mediaOutputStatus->status = MEDIAOUTPUTSTATUS_IDLE;
4886 }
4887 ✗ Stopping();
4888 ✗ Stopped();
4889 6 return 0;
4890 }
4891 } else {
4892 // Position unchanged — start or continue stall timer
4893
1/2
✓ Branch 0 taken 1661 times.
✗ Branch 1 not taken.
1661 uint64_t now = GetMonotonicTimeMS();
4894
2/2
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 1455 times.
1661 if (m_stallStartMs == 0) {
4895 206 m_stallStartMs = now;
4896
1/2
✓ Branch 0 taken 206 times.
✗ Branch 1 not taken.
206 LogDebug(VB_MEDIAOUT, "GStreamer: position stalled at %.1fs, starting watchdog\n", elapsed);
4897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1455 times.
1455 } else if ((now - m_stallStartMs) > STALL_TIMEOUT_MS) {
4898 ✗ LogWarn(VB_MEDIAOUT, "GStreamer pipeline stalled for %dms at position %.1fs — "
4899 "audio sink may be blocked (HDMI unplugged?). Stopping playback.\n",
4900 STALL_TIMEOUT_MS, elapsed);
4901 // The cause, when it showed itself, already has its banner.
4902 ✗ if (!m_reportedFailure.exchange(true, std::memory_order_acq_rel)) {
4903 ✗ WarningHolder::AddWarningTimeout(MEDIA_FAILURE_WARNING_SECONDS, 30,
4904 ✗ "Playback of " + m_mediaFilename + " stalled and was stopped");
4905 }
4906 ✗ m_playing = false;
4907 ✗ if (m_mediaOutputStatus && OwnsSlotStatus()) {
4908 ✗ m_mediaOutputStatus->status = MEDIAOUTPUTSTATUS_IDLE;
4909 }
4910 ✗ Stopping();
4911 ✗ Stopped();
4912 ✗ return 0;
4913 }
4914 }
4915 } else {
4916 // Preroll watchdog: position query keeps failing because the
4917 // pipeline never reached PLAYING. Normal startup clears this within
4918 // a fraction of a second (m_wallStartMs gets set on the first valid
4919 // position). If we're still here after PREROLL_TIMEOUT_MS the
4920 // pipeline is wedged in preroll — e.g. the V4L2 HW decoder stopped
4921 // producing frames after a long video-only loop (issue #2695) so the
4922 // video sink never prerolls and the whole pipeline stays PAUSED.
4923 // The regular stall watchdog above can't catch this (it lives inside
4924 // the havePos branch), so without this check m_playing stays true
4925 // forever, the playlist never advances, and only a reboot recovers.
4926 // Force a stop so FinishPlay()/CloseMediaOutput() tears the pipeline
4927 // down and the playlist can move on to the next item.
4928
5/6
✓ Branch 0 taken 5289 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 5289 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 5309 times.
10604 if (m_wallStartMs == 0 && m_playStartMs > 0 &&
4929
3/4
✓ Branch 0 taken 5289 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 5283 times.
5289 (GetMonotonicTimeMS() - m_playStartMs) > PREROLL_TIMEOUT_MS) {
4930
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 LogWarn(VB_MEDIAOUT, "GStreamer pipeline failed to preroll within %dms "
4931 "(stuck in PAUSED — HW decoder may have wedged, see issue #2695). "
4932 "Forcing stop so the playlist advances.\n", PREROLL_TIMEOUT_MS);
4933
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 bool hwDecoder = PipelineHasHwVideoDecoder(m_pipeline);
4934
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 RecordWedgeEventAndMaybeRestart("pipeline failed to preroll", hwDecoder);
4935 // Stopping cancels the start thread's wait for PLAYING, which
4936 // was the only place this reached the UI: tell the user here.
4937 // A wedged hardware decoder has its own restart and reboot
4938 // rungs above.
4939
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (!hwDecoder) {
4940
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
16 WarningHolder::AddWarningTimeout(60, 30, kMediaDidNotStartWarning);
4941 }
4942 6 m_playing = false;
4943
4/8
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
6 if (m_mediaOutputStatus && OwnsSlotStatus()) {
4944 6 m_mediaOutputStatus->status = MEDIAOUTPUTSTATUS_IDLE;
4945 }
4946
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 Stopping();
4947
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 Stopped();
4948 6 return 0;
4949 }
4950
1/2
✓ Branch 0 taken 5309 times.
✗ Branch 1 not taken.
5309 LogExcess(VB_MEDIAOUT, "GStreamer position query pending (pipeline not yet PLAYING)\n");
4951 }
4952 }
4953
4954
2/2
✓ Branch 0 taken 194151 times.
✓ Branch 1 taken 170 times.
194321 return m_playing ? 1 : 0;
4955 194445 }
4956
4957 194327 void GStreamerOutput::ProcessMessages() {
4958
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194327 times.
194327 if (!m_bus)
4959 ✗ return;
4960
4961 GstMessage* msg;
4962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194327 times.
194327 while ((msg = gst_bus_pop(m_bus)) != nullptr) {
4963 ✗ switch (GST_MESSAGE_TYPE(msg)) {
4964 ✗ case GST_MESSAGE_EOS:
4965 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: End of stream\n");
4966 ✗ if (m_loopCount > 0 || m_loopCount == -1) {
4967 // Loop: seek back to beginning
4968 ✗ if (m_loopCount > 0)
4969 ✗ m_loopCount--;
4970 ✗ EndTimelineShift();
4971 ✗ gst_element_seek_simple(m_pipeline, GST_FORMAT_TIME,
4972 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),
4973 0);
4974 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: Looping (remaining: %d)\n", m_loopCount);
4975 } else {
4976 ✗ m_playing = false;
4977 ✗ if (m_mediaOutputStatus && OwnsSlotStatus()) {
4978 ✗ m_mediaOutputStatus->status = MEDIAOUTPUTSTATUS_IDLE;
4979 }
4980 ✗ Stopping();
4981 ✗ Stopped();
4982 }
4983 ✗ break;
4984
4985 ✗ case GST_MESSAGE_ERROR: {
4986 GError* err;
4987 gchar* debug;
4988 ✗ gst_message_parse_error(msg, &err, &debug);
4989 ✗ LogErr(VB_MEDIAOUT, "GStreamer error: %s\n", err->message);
4990 ✗ LogDebug(VB_MEDIAOUT, "GStreamer debug: %s\n", debug ? debug : "(none)");
4991 ✗ g_error_free(err);
4992 ✗ g_free(debug);
4993 ✗ m_playing = false;
4994 ✗ if (m_mediaOutputStatus && OwnsSlotStatus()) {
4995 ✗ m_mediaOutputStatus->status = MEDIAOUTPUTSTATUS_IDLE;
4996 }
4997 ✗ Stopping();
4998 ✗ Stopped();
4999 ✗ break;
5000 }
5001
5002 ✗ case GST_MESSAGE_STATE_CHANGED: {
5003 ✗ if (GST_MESSAGE_SRC(msg) == GST_OBJECT(m_pipeline)) {
5004 GstState oldState, newState, pending;
5005 ✗ gst_message_parse_state_changed(msg, &oldState, &newState, &pending);
5006 ✗ LogDebug(VB_MEDIAOUT, "GStreamer state: %s -> %s\n",
5007 gst_element_state_get_name(oldState),
5008 gst_element_state_get_name(newState));
5009 ✗ if (newState == GST_STATE_PLAYING) {
5010 ✗ Playing();
5011 }
5012 }
5013 ✗ break;
5014 }
5015
5016 ✗ default:
5017 ✗ break;
5018 }
5019 ✗ gst_message_unref(msg);
5020 }
5021 }
5022
5023 // What a playback error says is wrong with the file, or nullptr when it isn't
5024 // the file's fault. STREAM_ERROR_FAILED ("Internal data stream error") is left
5025 // out: a demuxer posts it when anything downstream fails, a sink included.
5026 9 static const char* MediaFileFault(GstMessage* msg, const GError* err, bool audioOnly) {
5027
4/8
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
9 GstElement* src = GST_IS_ELEMENT(GST_MESSAGE_SRC(msg)) ? GST_ELEMENT(GST_MESSAGE_SRC(msg)) : nullptr;
5028
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 GstElementFactory* f = src ? gst_element_get_factory(src) : nullptr;
5029
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 const char* factory = f ? GST_OBJECT_NAME(f) : "";
5030
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
9 if (err->domain == GST_STREAM_ERROR) {
5031
2/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
5 switch (err->code) {
5032 ✗ case GST_STREAM_ERROR_CODEC_NOT_FOUND:
5033 ✗ return "it uses a format this device can't decode";
5034 ✗ case GST_STREAM_ERROR_DECODE: {
5035 // A hardware decoder's own fault (issue #2695) is not the file's.
5036 ✗ const char* klass = f ? gst_element_factory_get_metadata(f, GST_ELEMENT_METADATA_KLASS) : nullptr;
5037 ✗ if (strncmp(factory, "v4l2", 4) == 0 || (klass && strstr(klass, "Hardware"))) {
5038 ✗ return nullptr;
5039 }
5040 ✗ return "the file looks damaged or is not a media file";
5041 }
5042 3 case GST_STREAM_ERROR_TYPE_NOT_FOUND:
5043 case GST_STREAM_ERROR_WRONG_TYPE:
5044 case GST_STREAM_ERROR_DEMUX:
5045 case GST_STREAM_ERROR_FORMAT:
5046 3 return "the file looks damaged or is not a media file";
5047 2 default:
5048 2 return nullptr;
5049 }
5050 }
5051 // The audio-only pipeline's missing plug-in is the no-audio case, not this.
5052
6/8
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 4 times.
4 if (err->domain == GST_CORE_ERROR && err->code == GST_CORE_ERROR_MISSING_PLUGIN && !audioOnly) {
5053 ✗ return "it uses a format this device can't decode";
5054 }
5055
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
4 if (err->domain == GST_RESOURCE_ERROR && strcmp(factory, "filesrc") == 0) {
5056 ✗ return "the file could not be read";
5057 }
5058 4 return nullptr;
5059 }
5060
5061 // A sink's message is often just "general resource error"; the reason is the
5062 // last line of its debug text ("drmModeSetPlane failed: No space left on device (28)").
5063 9 std::string GStreamerOutput::ErrorReason(const GError* err, const gchar* debug) {
5064
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 std::string d = debug ? debug : "";
5065
3/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9 times.
9 while (!d.empty() && isspace((unsigned char)d.back())) {
5066 ✗ d.pop_back();
5067 }
5068 9 size_t nl = d.find_last_of('\n');
5069
5/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 2 times.
9 if (nl != std::string::npos && nl + 1 < d.size()) {
5070
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 return d.substr(nl + 1);
5071 }
5072
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 return err->message ? err->message : "unknown error";
5073 9 }
5074
5075 3 void GStreamerOutput::ReportFileFailure(const char* what, const std::string& reason) {
5076
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (m_reportedFailure.exchange(true, std::memory_order_acq_rel)) {
5077 1 return;
5078 }
5079
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
8 WarningHolder::AddWarningTimeout(MEDIA_FAILURE_WARNING_SECONDS, 30,
5080
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
4 "Can't play " + m_mediaFilename + ": " + what + " (" + reason + ")");
5081 }
5082
5083 // Keyed on the display and the reason, not the file, so a playlist of videos
5084 // failing the same way shares one banner.
5085 3 void GStreamerOutput::ReportVideoOutputFailure(const char* sinkName, const std::string& reason) {
5086
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (m_reportedFailure.exchange(true, std::memory_order_acq_rel)) {
5087 1 return;
5088 }
5089 2 std::string display;
5090 2 std::string warning;
5091 #ifdef PLATFORM_OSX
5092 display = "the video window";
5093 #else
5094
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::string cardPath = m_hdmiCardPath;
5095 2 int connectorId = m_hdmiConnectorId;
5096
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (strncmp(sinkName, "dkms_", 5) == 0) {
5097 2 int connId = atoi(sinkName + 5);
5098
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto it = m_dkmsConnectorNames.find(connId);
5099
3/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
6 display = (it != m_dkmsConnectorNames.end() && !it->second.empty()) ? it->second
5100
1/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 : "display connector " + std::to_string(connId);
5101
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto card = m_dkmsCardPaths.find(connId);
5102
3/10
✓ 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 taken 2 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
2 cardPath = card != m_dkmsCardPaths.end() ? card->second : "";
5103 2 connectorId = connId;
5104 } else {
5105 ✗ display = m_videoOut;
5106 ✗ if (display == "--HDMI--" || display == "--hdmi--" || display == "HDMI") {
5107 ✗ display = "HDMI-A-1";
5108 }
5109 }
5110 // One cause of ENOSPC that FPP can name, with the setting that avoids it.
5111
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 warning = VideoOutputManager::AspectMismatchWarning(display, cardPath, connectorId, reason);
5112 #endif
5113
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (warning.empty()) {
5114
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 warning = "Video output to " + display + " failed (" + reason + ")";
5115 }
5116
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 WarningHolder::AddWarningTimeout(MEDIA_FAILURE_WARNING_SECONDS, 74, warning); // Display Refused Video
5117 2 }
5118
5119 118747 GstBusSyncReply GStreamerOutput::BusSyncHandler(GstBus* bus, GstMessage* msg, gpointer userData) {
5120 // Everything this handler doesn't act on is dropped without touching the
5121 // output. Passing it on queued it on the bus for Process() to pop, and
5122 // nothing calls Process() for Play Media with no playlist running: every
5123 // tag, QoS, state-change and stream-status message piled up there for the
5124 // whole of the media, a looping video's indefinitely. Of the state
5125 // changes, only the pipeline's own matter -- it is the one source on its
5126 // bus with no parent.
5127
3/3
✓ Branch 0 taken 305 times.
✓ Branch 1 taken 84739 times.
✓ Branch 2 taken 33703 times.
118747 switch (GST_MESSAGE_TYPE(msg)) {
5128 305 case GST_MESSAGE_EOS:
5129 case GST_MESSAGE_ERROR:
5130 305 break;
5131 84739 case GST_MESSAGE_STATE_CHANGED:
5132
3/4
✓ Branch 0 taken 84739 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 79640 times.
✓ Branch 3 taken 5099 times.
84739 if (!GST_MESSAGE_SRC(msg) || GST_OBJECT_PARENT(GST_MESSAGE_SRC(msg))) {
5133
1/2
✓ Branch 0 taken 79640 times.
✗ Branch 1 not taken.
79640 gst_message_unref(msg);
5134 79640 return GST_BUS_DROP;
5135 }
5136 5099 break;
5137 33703 default:
5138
1/2
✓ Branch 0 taken 33703 times.
✗ Branch 1 not taken.
33703 gst_message_unref(msg);
5139 33703 return GST_BUS_DROP;
5140 }
5141
5142 // An EOS or error marks the output idle below, and the playlist can then
5143 // destroy it from the main thread while this call is still running. The
5144 // scope holds Close() off until the call returns.
5145
1/2
✓ Branch 0 taken 5404 times.
✗ Branch 1 not taken.
10808 BusCallbackScope scope(userData);
5146 5404 GStreamerOutput* self = scope.self;
5147
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 5377 times.
5404 if (!self)
5148 27 return GST_BUS_PASS;
5149
5150
3/4
✓ Branch 0 taken 296 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 5072 times.
✗ Branch 3 not taken.
5377 switch (GST_MESSAGE_TYPE(msg)) {
5151 296 case GST_MESSAGE_EOS:
5152
1/2
✓ Branch 0 taken 296 times.
✗ Branch 1 not taken.
296 LogDebug(VB_MEDIAOUT, "GStreamer sync: End of stream\n");
5153
3/4
✓ Branch 0 taken 294 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 294 times.
296 if (self->m_loopCount > 0 || self->m_loopCount == -1) {
5154
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (self->m_loopCount > 0)
5155 2 self->m_loopCount--;
5156
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 ScheduleLoopSeek(scope.guard);
5157
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogDebug(VB_MEDIAOUT, "GStreamer sync: Looping (remaining: %d)\n", self->m_loopCount);
5158 } else {
5159 // When the last of the sound is heard, as the output stream
5160 // measures it. Read now, before the stream lays silence behind it
5161 // for the next track.
5162
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 58 times.
294 if (self->m_feedsStream) {
5163
1/2
✓ Branch 0 taken 236 times.
✗ Branch 1 not taken.
236 int64_t queued = PipeWireOutputStream::QueuedAheadNsForSlot(self->m_streamSlot);
5164
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 2 times.
236 if (queued >= 0) {
5165
1/2
✓ Branch 0 taken 234 times.
✗ Branch 1 not taken.
234 self->m_soundEndMs = GetMonotonicTimeMS() + (uint64_t)(queued / 1000000);
5166 }
5167 }
5168 // Detach AES67 inline branches so the standalone pipeline
5169 // resumes sending — EOS means no more data will flow through
5170 // the tee, so the inline branch is useless.
5171 #ifdef HAS_AES67_GSTREAMER
5172
1/2
✓ Branch 0 taken 294 times.
✗ Branch 1 not taken.
294 self->DetachAES67Branches();
5173 #endif
5174 294 self->m_playing = false;
5175
4/8
✓ Branch 0 taken 294 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 294 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 294 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 294 times.
✗ Branch 7 not taken.
294 if (self->m_mediaOutputStatus && self->OwnsSlotStatus()) {
5176 294 self->m_mediaOutputStatus->status = MEDIAOUTPUTSTATUS_IDLE;
5177 }
5178
1/2
✓ Branch 0 taken 294 times.
✗ Branch 1 not taken.
294 self->Stopping();
5179
1/2
✓ Branch 0 taken 294 times.
✗ Branch 1 not taken.
294 self->Stopped();
5180 }
5181
1/2
✓ Branch 0 taken 296 times.
✗ Branch 1 not taken.
296 gst_message_unref(msg);
5182 296 return GST_BUS_DROP;
5183
5184 9 case GST_MESSAGE_ERROR: {
5185 GError* err;
5186 gchar* debug;
5187
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 gst_message_parse_error(msg, &err, &debug);
5188
5189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (self->m_prepared.load()) {
5190 // Prepared and waiting while another track plays: the slot's
5191 // status and the callbacks are that track's. Start() builds this
5192 // one again, and reports whatever fails then.
5193 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: prepared %s failed (%s); it will be built again when it starts\n",
5194 self->m_mediaFilename.c_str(), err && err->message ? err->message : "unknown error");
5195 ✗ self->m_pipelineFailed = true;
5196 ✗ g_error_free(err);
5197 ✗ g_free(debug);
5198 ✗ gst_message_unref(msg);
5199 ✗ return GST_BUS_DROP;
5200 }
5201
5202 // Identify which element produced the error
5203
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 const gchar* srcName = GST_MESSAGE_SRC(msg) ?
5204 9 GST_OBJECT_NAME(GST_MESSAGE_SRC(msg)) : "unknown";
5205
5206 // AES67 inline RTP branch elements are named "aes67_*".
5207 // Errors from these branches (e.g. network issues, PipeWire
5208 // disconnects) should NOT stop media playback.
5209 // Video PipeWire sink errors ("pwvideosink") are always non-fatal
5210 // because kmssink is the primary video output — the pipewiresink
5211 // is a secondary tee branch for PipeWire graph visibility only.
5212 9 bool isAES67Branch = (strncmp(srcName, "aes67_", 6) == 0);
5213 9 bool isVideoPWSink = (strcmp(srcName, "pwvideosink") == 0);
5214 // Direct kmssink branches (dkms_*) may fail if a display is
5215 // disconnected during playback — treat as non-fatal.
5216 9 bool isDirectKmsSink = (strncmp(srcName, "dkms_", 5) == 0);
5217
5218 // A file with nothing decodable to feed the audio-only pipeline fails
5219 // here, and the raw GStreamer wording is actively misleading: decodebin
5220 // reports its autoplug failure as "your installation is missing a
5221 // plug-in" (nothing is missing -- there is simply no audio to plug), and
5222 // the demuxer then follows with a generic "Internal data stream error".
5223 // A video-only clip played on an item whose video output is disabled or
5224 // whose display is unplugged lands exactly here, so say what actually
5225 // happened and what to do about it. Everything else keeps the raw
5226 // message -- this only claims the case it can prove, which is that
5227 // decodebin never produced an audio pad.
5228 // "No audio pad yet" is not on its own evidence that the file has no
5229 // audio: anything that fails before decodebin gets that far leaves the
5230 // flag clear too. Stopping PipeWire under a running fppd and playing a
5231 // plain WAV does exactly that -- pwsink errors first, and blaming the
5232 // file for an audio-backend outage is a worse lie than the one being
5233 // fixed. So the source has to be the decode chain as well.
5234 9 bool errorFromDecoder = false;
5235
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 3 times.
20 for (GstObject* o = GST_MESSAGE_SRC(msg); o; o = GST_OBJECT_PARENT(o)) {
5236 17 const gchar* n = GST_OBJECT_NAME(o);
5237
3/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 11 times.
17 if (n && strcmp(n, "decoder") == 0) {
5238 6 errorFromDecoder = true;
5239 6 break;
5240 }
5241 }
5242 // A damaged file is not one with no audio, nor is the follow-on error after it.
5243
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 const char* fileFault = MediaFileFault(msg, err, self->m_audioOnlyPipeline);
5244
3/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
6 if (self->m_audioOnlyPipeline && errorFromDecoder && !fileFault &&
5245
6/6
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 7 times.
17 !self->m_reportedFailure.load(std::memory_order_acquire) &&
5246
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !self->m_sawAudioPad.load(std::memory_order_acquire)) {
5247
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!self->m_reportedNoAudio.exchange(true, std::memory_order_acq_rel)) {
5248
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogErr(VB_MEDIAOUT,
5249 "GStreamer: '%s' has no playable audio stream, and no video output is "
5250 "configured for it, so there is nothing to play. If this is a video-only "
5251 "file, set the item's Video Output to a connected display; FPP falls back to "
5252 "audio-only whenever the chosen display is disabled or unplugged.\n",
5253 self->m_mediaFilename.c_str());
5254
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
3 WarningHolder::AddWarningTimeout(60, 30,
5255
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 "No playable audio in " + self->m_mediaFilename +
5256
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 " and no video output — nothing to play");
5257
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogDebug(VB_MEDIAOUT, "GStreamer no-audio underlying error (src=%s): %s\n",
5258 srcName, err->message);
5259 #ifdef HAS_AES67_GSTREAMER
5260
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 self->DetachAES67Branches();
5261 #endif
5262 1 self->m_playing = false;
5263
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.
1 if (self->m_mediaOutputStatus && self->OwnsSlotStatus()) {
5264 1 self->m_mediaOutputStatus->status = MEDIAOUTPUTSTATUS_IDLE;
5265 }
5266
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 self->Stopping();
5267
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 self->Stopped();
5268 } else {
5269 // The demuxer's follow-on error describes the same event; logging
5270 // it again reads as a second, unrelated failure.
5271
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogDebug(VB_MEDIAOUT, "GStreamer no-audio follow-on error (src=%s): %s\n",
5272 srcName, err->message);
5273 }
5274
4/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 4 times.
7 } else if (isAES67Branch || isVideoPWSink || isDirectKmsSink) {
5275
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::string reason = ErrorReason(err, debug);
5276
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogWarn(VB_MEDIAOUT, "GStreamer non-fatal error (src=%s) playing %s: %s\n",
5277 srcName, self->m_mediaFilename.c_str(), reason.c_str());
5278
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (isDirectKmsSink) {
5279
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 self->ReportVideoOutputFailure(srcName, reason);
5280 }
5281 // Logging it as non-fatal isn't enough: the sink is about to
5282 // return GST_FLOW_ERROR, which travels back through its queue to
5283 // the tee and the demuxer, and that ends the media. This runs on
5284 // the sink's streaming thread before it returns, so cutting the
5285 // branch off at the tee here gets in ahead of that.
5286
9/18
✓ 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 not taken.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 3 times.
✓ Branch 12 taken 3 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 3 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 3 times.
✗ Branch 17 not taken.
3 if ((isVideoPWSink || isDirectKmsSink) && GST_IS_ELEMENT(GST_MESSAGE_SRC(msg))) {
5287
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 self->CutFailedBranch(GST_ELEMENT(GST_MESSAGE_SRC(msg)));
5288 }
5289
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 LogDebug(VB_MEDIAOUT, "GStreamer AES67 branch debug: %s\n",
5290 debug ? debug : "(none)");
5291 3 } else {
5292 4 bool primaryVideoSink = strcmp(srcName, "kmsvideosink") == 0;
5293
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
4 std::string reason = (primaryVideoSink || (fileFault && err->domain == GST_CORE_ERROR))
5294 4 ? ErrorReason(err, debug)
5295
4/12
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
8 : std::string(err->message ? err->message : "unknown error");
5296
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 LogErr(VB_MEDIAOUT, "GStreamer sync error (src=%s): %s\n", srcName, reason.c_str());
5297
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 LogDebug(VB_MEDIAOUT, "GStreamer sync debug: %s\n", debug ? debug : "(none)");
5298
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if (fileFault) {
5299
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 self->ReportFileFailure(fileFault, reason);
5300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (primaryVideoSink) {
5301 ✗ self->ReportVideoOutputFailure(srcName, reason);
5302 }
5303 #ifdef HAS_AES67_GSTREAMER
5304
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 self->DetachAES67Branches();
5305 #endif
5306 4 self->m_playing = false;
5307
4/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 if (self->m_mediaOutputStatus && self->OwnsSlotStatus()) {
5308 4 self->m_mediaOutputStatus->status = MEDIAOUTPUTSTATUS_IDLE;
5309 }
5310
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 self->Stopping();
5311
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 self->Stopped();
5312 4 }
5313
5314
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 g_error_free(err);
5315
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 g_free(debug);
5316
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 gst_message_unref(msg);
5317 9 return GST_BUS_DROP;
5318 }
5319
5320 5072 case GST_MESSAGE_STATE_CHANGED: {
5321
3/6
✓ Branch 0 taken 5072 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5072 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5072 times.
✗ Branch 5 not taken.
5072 if (GST_MESSAGE_SRC(msg) == GST_OBJECT(self->m_pipeline)) {
5322 GstState oldState, newState, pending;
5323
1/2
✓ Branch 0 taken 5072 times.
✗ Branch 1 not taken.
5072 gst_message_parse_state_changed(msg, &oldState, &newState, &pending);
5324
3/6
✓ Branch 0 taken 5072 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5072 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5072 times.
✗ Branch 5 not taken.
5072 LogDebug(VB_MEDIAOUT, "GStreamer sync state: %s -> %s\n",
5325 gst_element_state_get_name(oldState),
5326 gst_element_state_get_name(newState));
5327
2/2
✓ Branch 0 taken 917 times.
✓ Branch 1 taken 4155 times.
5072 if (newState == GST_STATE_PLAYING) {
5328
1/2
✓ Branch 0 taken 917 times.
✗ Branch 1 not taken.
917 self->Playing();
5329 }
5330 }
5331
1/2
✓ Branch 0 taken 5072 times.
✗ Branch 1 not taken.
5072 gst_message_unref(msg);
5332 5072 return GST_BUS_DROP;
5333 }
5334
5335 ✗ default:
5336 ✗ gst_message_unref(msg);
5337 ✗ return GST_BUS_DROP;
5338 }
5339 }
5340
5341 // Read a single "<Key>: <N> kB" line from /proc/meminfo. Returns -1 if the
5342 // key isn't present. Small and self-contained on purpose — mirrors the
5343 // CmaFree check VideoOutputManager::StartConsumer does before launching an
5344 // HDMI consumer, generalized to whichever key is asked for.
5345 7886 static long ReadMeminfoKB(const char* key) {
5346
1/2
✓ Branch 0 taken 7886 times.
✗ Branch 1 not taken.
7886 FILE* f = fopen("/proc/meminfo", "r");
5347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7886 times.
7886 if (!f)
5348 ✗ return -1;
5349 char line[256];
5350 7886 long kb = -1;
5351 7886 size_t keyLen = strlen(key);
5352
2/4
✓ Branch 0 taken 366067 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 366067 times.
✗ Branch 3 not taken.
366067 while (fgets(line, sizeof(line), f)) {
5353
3/4
✓ Branch 0 taken 7886 times.
✓ Branch 1 taken 358181 times.
✓ Branch 2 taken 7886 times.
✗ Branch 3 not taken.
366067 if (strncmp(line, key, keyLen) == 0 && line[keyLen] == ':') {
5354 7886 sscanf(line + keyLen + 1, "%ld", &kb);
5355 7886 break;
5356 }
5357 }
5358
1/2
✓ Branch 0 taken 7886 times.
✗ Branch 1 not taken.
7886 fclose(f);
5359 7886 return kb;
5360 }
5361
5362 3943 int GStreamerOutput::Close(void) {
5363
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 LogDebug(VB_MEDIAOUT, "GStreamerOutput::Close()\n");
5364
5365 // Leave the slot table before anything is torn down. The slot manager
5366 // calls Process(), SeekTo() and SetVolume() through its pointer under its
5367 // own lock, so this waits out any such call and makes it the last: from
5368 // here on nothing can reach this output that way while it is dismantled
5369 // or freed. The slot itself (status, KMS gate) is cleared at the end.
5370
2/4
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3943 times.
✗ Branch 3 not taken.
3943 StreamSlotManager::Instance().DetachOutput(m_streamSlot, this);
5371
5372
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 std::lock_guard<std::recursive_mutex> opLock(m_opMutex);
5373
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 DetachCallbacks();
5374 3943 m_prepared = false;
5375
5376 // Neither for a prepared output thrown away unplayed (the delay lines
5377 // and sends carry the track that is playing), nor for one whose sound
5378 // the next track carries on from: its tail is still on its way out.
5379
7/8
✓ Branch 0 taken 3919 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 283 times.
✓ Branch 3 taken 3636 times.
✓ Branch 4 taken 283 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 15 times.
✓ Branch 7 taken 268 times.
3943 bool flushTail = m_claimedSlot && !(m_pwStream && PipeWireOutputStream::SlotMayCarryOn(m_streamSlot));
5380
4/4
✓ Branch 0 taken 379 times.
✓ Branch 1 taken 3564 times.
✓ Branch 2 taken 97 times.
✓ Branch 3 taken 282 times.
3943 if (m_pipeline && flushTail) {
5381 // Flush PipeWire filter-chain delay buffers: a delayed group
5382 // member's chain still holds the end of this track (see
5383 // FlushPipeWireDelayBuffers()). Detached, so Close() doesn't wait.
5384
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 FlushPipeWireDelayBuffers();
5385
5386 // Flush AES67 send pipelines — drop a few buffers to discard
5387 // tail-end audio from the ending track. The Start()-time flush
5388 // is the primary protection; this Close() flush is supplementary
5389 // for cases where Close→Start is very fast (back-to-back tracks).
5390 #ifdef HAS_AES67_GSTREAMER
5391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97 times.
97 if (AES67Manager::INSTANCE.IsActive()) {
5392 ✗ AES67Manager::INSTANCE.FlushSendPipelines();
5393 }
5394 #endif
5395 }
5396
2/2
✓ Branch 0 taken 379 times.
✓ Branch 1 taken 3564 times.
3943 if (m_pipeline) {
5397
5398 // Stop() handles shutdown flag, appsink/bus cleanup, AES67 detach,
5399 // and the pipeline state change.
5400
1/2
✓ Branch 0 taken 379 times.
✗ Branch 1 not taken.
379 Stop();
5401
5402 // Restore overlay model state if we enabled it
5403
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 375 times.
379 if (m_wasOverlayDisabled) {
5404
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 std::lock_guard<std::mutex> lock(m_videoOverlayModelLock);
5405
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (m_videoOverlayModel) {
5406
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 m_videoOverlayModel->setState(PixelOverlayState(PixelOverlayState::Disabled));
5407 }
5408 4 m_wasOverlayDisabled = false;
5409 4 }
5410
5411
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 296 times.
379 if (m_appsink) {
5412
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 gst_object_unref(m_appsink);
5413 83 m_appsink = nullptr;
5414 }
5415
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 375 times.
379 if (m_videoAppsink) {
5416
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 gst_object_unref(m_videoAppsink);
5417 4 m_videoAppsink = nullptr;
5418 }
5419
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 296 times.
379 if (m_volume) {
5420
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 gst_object_unref(m_volume);
5421 83 m_volume = nullptr;
5422 }
5423
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 285 times.
379 if (m_bus) {
5424
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 gst_object_unref(m_bus);
5425 94 m_bus = nullptr;
5426 }
5427 // Stop()'s asynchronous teardown has already handed the pipeline off.
5428
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 285 times.
379 if (m_pipeline) {
5429
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 gst_object_unref(m_pipeline);
5430 94 m_pipeline = nullptr;
5431 }
5432 }
5433 // Branches cut off but not yet removed go down with the pipeline; only
5434 // our refs on them are left to drop, before the planes are returned below.
5435
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 ReleaseFailedBranches();
5436
5437 // Hand back the DRM leases this pipeline's kmssinks ran on, which also
5438 // returns their planes. Must run after gst_object_unref(m_pipeline)
5439 // above: the kmssinks are destroyed by that unref, and closing a lease
5440 // under a live one is undefined. Closing it is also what frees any GEM
5441 // handles or framebuffers a kmssink failed to release. Runs outside the
5442 // `if (m_pipeline)` guard so a Start() that failed after taking a lease
5443 // still gives it back.
5444 //
5445 // EXCEPT after an abandoned (wedged) teardown: the pipeline and its
5446 // kmssinks are still alive on the reaper thread and still using these.
5447 // Leak them with the pipeline; the restart once teardowns pile up
5448 // (STUCK_TEARDOWN_RESTART_LIMIT) bounds the loss.
5449
2/2
✓ Branch 0 taken 3938 times.
✓ Branch 1 taken 5 times.
3943 if (!m_teardownAbandoned) {
5450
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3938 times.
3939 for (DrmCards::Lease& lease : m_drmLeases) {
5451
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 DrmCards::ReleaseLease(lease);
5452 }
5453 // The connectors are free again: what stood aside for this video
5454 // (a camera group on the same display) comes back.
5455
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3938 times.
3943 for (int connectorId : m_mediaConnectors) {
5456
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 VideoOutputManager::Instance().ReleaseConnectorFromMedia(connectorId);
5457 }
5458 }
5459 3943 m_drmLeases.clear();
5460 3943 m_mediaConnectors.clear();
5461
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 ReleaseDisplay();
5462
5463 // Clean up video overlay state
5464 // The frame state is shared with the sample callback (m_videoFrameMutex)
5465 // and the channel output thread's overlay push (m_videoFrameMutex for the
5466 // frame, m_videoOverlayModelLock for the delivery count), so it is reset
5467 // under the same locks, one at a time.
5468 uint64_t framesReceived;
5469 uint64_t framesDelivered;
5470 {
5471
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 std::lock_guard<std::mutex> lock(m_videoFrameMutex);
5472 3943 framesReceived = m_videoFramesReceived;
5473 3943 m_videoFrameReady = false;
5474 3943 m_videoFramesReceived = 0;
5475 3943 }
5476 {
5477
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 std::lock_guard<std::mutex> lock(m_videoOverlayModelLock);
5478 3943 framesDelivered = m_videoFramesDelivered;
5479 3943 m_videoFramesDelivered = 0;
5480 3943 }
5481
3/4
✓ Branch 0 taken 3939 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3939 times.
3943 if (framesReceived > 0 || framesDelivered > 0) {
5482
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 LogDebug(VB_MEDIAOUT, "GStreamer video overlay stats: %lu frames received, %lu delivered\n",
5483 (unsigned long)framesReceived, (unsigned long)framesDelivered);
5484 }
5485 3943 m_hasVideoStream = false;
5486 3943 m_feedVideoLeadS = 0.06;
5487 3943 m_feedSeekTimesMs.clear();
5488 3943 m_feedSeekGaveUp = false;
5489 3943 m_audioChain = nullptr;
5490 3943 m_videoChain = nullptr;
5491 3943 m_kmssink = nullptr; // owned by pipeline bin, already freed
5492 3943 m_wantHDMI = false;
5493 #ifdef PLATFORM_OSX
5494 if (m_macVideoWindow) {
5495 // Deferred, and cancelled if the next item opens it again.
5496 MacOSHideVideoWindowSoon(m_streamSlot);
5497 }
5498 #endif
5499 3943 m_macVideoWindow = false;
5500
5501 // Consumers already stopped before pipeline NULL; just clear state
5502 3943 m_videoPipeWireRouting = false;
5503 3943 m_pwVideoSinkName.clear();
5504
5505 // Remove model listener
5506
7/8
✓ Branch 0 taken 389 times.
✓ Branch 1 taken 3554 times.
✓ Branch 2 taken 389 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 158 times.
✓ Branch 5 taken 231 times.
✓ Branch 6 taken 158 times.
✓ Branch 7 taken 3785 times.
3943 if (!m_videoOut.empty() && m_videoOut != "--Disabled--") {
5507
2/4
✓ Branch 0 taken 158 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 158 times.
✗ Branch 3 not taken.
158 PixelOverlayManager::INSTANCE.removeModelListener(m_videoOut, OverlayListenerId());
5508 }
5509 // Deregister before taking m_videoOverlayModelLock: ProcessVideoOverlay()
5510 // walks the registry and then takes that lock, so acquiring them in the
5511 // other order here would be a lock inversion.
5512 {
5513
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 std::lock_guard<std::mutex> lock(m_overlayOutputsLock);
5514
2/4
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3943 times.
✗ Branch 3 not taken.
4002 m_overlayOutputs.erase(std::remove(m_overlayOutputs.begin(), m_overlayOutputs.end(), this),
5515 3943 m_overlayOutputs.end());
5516 3943 }
5517 {
5518
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 std::lock_guard<std::mutex> lock(m_videoOverlayModelLock);
5519 3943 m_videoOverlayModel = nullptr;
5520 3943 }
5521
5522 {
5523 // If we're closing the WLED tap owner, hand it to another active slot
5524 // (prefer slot 1). The candidate is found before taking the tap lock
5525 // so the slot manager's lock is never taken under it.
5526 3943 GStreamerOutput* next = nullptr;
5527
2/2
✓ Branch 0 taken 14270 times.
✓ Branch 1 taken 2282 times.
16552 for (int s = 1; s <= StreamSlotManager::MAX_SLOTS; s++) {
5528
2/2
✓ Branch 0 taken 2968 times.
✓ Branch 1 taken 11302 times.
14270 if (s == m_streamSlot) continue;
5529
2/4
✓ Branch 0 taken 11302 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11302 times.
✗ Branch 3 not taken.
11302 GStreamerOutput* other = StreamSlotManager::Instance().GetActiveOutput(s);
5530
7/8
✓ Branch 0 taken 2077 times.
✓ Branch 1 taken 9225 times.
✓ Branch 2 taken 2077 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1661 times.
✓ Branch 5 taken 416 times.
✓ Branch 6 taken 1661 times.
✓ Branch 7 taken 9641 times.
11302 if (other && other != this && other->m_playing) {
5531 1661 next = other;
5532 1661 break;
5533 }
5534 }
5535
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 std::lock_guard<std::mutex> lock(s_sampleMutex);
5536
2/2
✓ Branch 0 taken 1091 times.
✓ Branch 1 taken 2852 times.
3943 if (m_currentInstance == this) {
5537
1/2
✓ Branch 0 taken 1091 times.
✗ Branch 1 not taken.
1091 SetSampleTapOwnerLocked(next);
5538 }
5539 3943 }
5540
5541 // Deregister from StreamSlotManager (only if we still own the slot --
5542 // see ClearSlot()'s ownership check).
5543
2/4
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3943 times.
✗ Branch 3 not taken.
3943 StreamSlotManager::Instance().ClearSlot(m_streamSlot, this);
5544
5545 // Per-track CMA trend line -- after every Close() (not just when
5546 // m_pipeline was set), including failed-Start cycles, so a slow leak can
5547 // be correlated with build/teardown count instead of only track count.
5548 // At info only when free CMA reaches a new low by a MB or more: a leak
5549 // still shows as a run of those, and a show looping one clip all night
5550 // doesn't write a line to the SD card at every pass.
5551 {
5552 static std::atomic<long> lowestFreeKB{ LONG_MAX };
5553
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 long cmaFreeKB = ReadMeminfoKB("CmaFree");
5554
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 long cmaTotalKB = ReadMeminfoKB("CmaTotal");
5555 3943 long lowest = lowestFreeKB.load();
5556
6/8
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 204 times.
✓ Branch 3 taken 3739 times.
✓ Branch 4 taken 204 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 204 times.
✓ Branch 7 taken 3739 times.
4147 if (cmaFreeKB >= 0 && cmaFreeKB + 1024 <= lowest && lowestFreeKB.compare_exchange_strong(lowest, cmaFreeKB)) {
5557
1/2
✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
204 LogInfo(VB_MEDIAOUT, "GStreamer: post-close CmaFree=%ld kB / CmaTotal=%ld kB (lowest yet)\n", cmaFreeKB, cmaTotalKB);
5558 } else {
5559
1/2
✓ Branch 0 taken 3739 times.
✗ Branch 1 not taken.
3739 LogDebug(VB_MEDIAOUT, "GStreamer: post-close CmaFree=%ld kB / CmaTotal=%ld kB\n", cmaFreeKB, cmaTotalKB);
5560 }
5561 }
5562
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 TrimHeapSoon();
5563
5564 3943 return 1;
5565 3943 }
5566
5567 167889 int GStreamerOutput::IsPlaying(void) {
5568
2/2
✓ Branch 0 taken 167674 times.
✓ Branch 1 taken 215 times.
167889 return m_playing ? 1 : 0;
5569 }
5570
5571 1363 int GStreamerOutput::AdjustSpeed(float masterMediaPosition) {
5572 1363 std::unique_lock<std::recursive_mutex> opLock(m_opMutex, std::try_to_lock);
5573
4/8
✓ Branch 0 taken 1363 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1363 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1363 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1363 times.
1363 if (!opLock.owns_lock() || !m_pipeline || !m_allowSpeedAdjust)
5574 ✗ return 1;
5575
5576 // Nothing below may touch a pipeline that has never produced a position:
5577 // both branches that act on a large diff issue a *synchronous* seek, and a
5578 // seek into a pipeline still stuck in preroll blocks the calling thread
5579 // forever inside the wedged sink. This runs on the main loop (MultiSync
5580 // ProcessControlPacket -> UpdateMasterMediaPosition) while holding
5581 // mediaOutputLock, so that block takes the whole player down: no sync
5582 // packets, no status, no `fpp` commands, and "Restart FPPD" never even gets
5583 // read (issue #2727 -- confirmed 2026-07-29 on FPPv4-3, main thread parked
5584 // on a PipeWire mutex with the pipeline still ASYNC). Sitting out the
5585 // sync until preroll completes is free: the preroll watchdog in Process()
5586 // tears a genuinely wedged pipeline down and the playlist advances.
5587
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1344 times.
1363 if (m_wallStartMs == 0) {
5588
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 LogDebug(VB_MEDIAOUT, "GStreamer: skipping speed adjust, pipeline has not prerolled yet (master %0.3f)\n",
5589 masterMediaPosition);
5590 19 return 1;
5591 }
5592
5593 // Can't adjust speed if not playing yet
5594
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1342 times.
1344 if (m_mediaOutputStatus->mediaSeconds < 0.01f) {
5595
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogDebug(VB_MEDIAOUT, "GStreamer: Can't adjust speed if not playing yet (%0.3f/%0.3f)\n",
5596 masterMediaPosition, m_mediaOutputStatus->mediaSeconds);
5597 2 return 1;
5598 }
5599
6/6
✓ Branch 0 taken 1272 times.
✓ Branch 1 taken 70 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 1264 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 1334 times.
1342 if (m_mediaOutputStatus->mediaSeconds > 1 && m_mediaOutputStatus->status == MEDIAOUTPUTSTATUS_IDLE) {
5600
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 LogDebug(VB_MEDIAOUT, "GStreamer: Can't adjust speed if beyond end of media (%0.3f/%0.3f)\n",
5601 masterMediaPosition, m_mediaOutputStatus->mediaSeconds);
5602 8 return 1;
5603 }
5604
5605
3/4
✓ Branch 0 taken 1216 times.
✓ Branch 1 taken 118 times.
✓ Branch 2 taken 1216 times.
✗ Branch 3 not taken.
1334 if (m_pwStream && m_pwFeed) {
5606
1/2
✓ Branch 0 taken 1216 times.
✗ Branch 1 not taken.
1216 return AdjustFeedSpeed(masterMediaPosition);
5607 }
5608
5609 118 float rate = m_currentRate;
5610
5611
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 115 times.
118 if (m_lastRates.empty()) {
5612 // Preload rate list with normal (1.0) rate
5613
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 m_lastRates.push_back(1.0f);
5614 3 m_lastRatesSum = 1.0f;
5615 }
5616
5617 118 int rawdiff = (int)(m_mediaOutputStatus->mediaSeconds * 1000) - (int)(masterMediaPosition * 1000);
5618 118 int diff = rawdiff;
5619 118 int sign = 1;
5620
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 13 times.
118 if (diff < 0) {
5621 105 sign = -1;
5622 105 diff = -diff;
5623 }
5624
5625
3/4
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 112 times.
118 if ((m_mediaOutputStatus->mediaSeconds < 1) || (diff > 3000)) {
5626
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 LogDebug(VB_MEDIAOUT, "GStreamer Diff: %d\tMaster: %0.3f Local: %0.3f Rate: %0.3f\n",
5627 rawdiff, masterMediaPosition, m_mediaOutputStatus->mediaSeconds, m_currentRate);
5628 } else {
5629
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 LogExcess(VB_MEDIAOUT, "GStreamer Diff: %d\tMaster: %0.3f Local: %0.3f Rate: %0.3f\n",
5630 rawdiff, masterMediaPosition, m_mediaOutputStatus->mediaSeconds, m_currentRate);
5631 }
5632
5633
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 pushDiff(rawdiff, m_currentRate);
5634
5635 // Sign-flip detection: if diff sign flipped and we're not at normal speed,
5636 // reset to 1.0 to avoid oscillation
5637
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 15 times.
118 int oldSign = m_lastDiff < 0 ? -1 : 1;
5638
5/6
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 5 times.
118 if ((oldSign != sign) && (m_lastDiff != 0) && (m_currentRate != 1.0f)) {
5639
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogDebug(VB_MEDIAOUT, "GStreamer Diff: %d\tFlipped, reset speed to normal\t(%0.3f)\n", rawdiff, 1.0f);
5640
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ApplyRate(1.0f);
5641 // Reset rate average list to 1.0
5642 1 m_lastRates.clear();
5643
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_lastRates.push_back(1.0f);
5644 1 m_lastRatesSum = 1.0f;
5645 1 m_currentRate = 1.0f;
5646 1 m_rateDiff = 0;
5647 1 m_lastDiff = rawdiff;
5648 1 return 1;
5649 }
5650
5651
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 97 times.
117 if (diff < 30) {
5652 // Close enough — return to normal rate
5653
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 17 times.
20 if (m_currentRate != 1.0f) {
5654 3 rate = 1.0f;
5655
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogDebug(VB_MEDIAOUT, "GStreamer Diff: %d\tVery close, use normal rate\t(%0.3f)\n", rawdiff, rate);
5656
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 ApplyRate(rate);
5657
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 m_lastRates.push_back(rate);
5658 3 m_lastRatesSum += rate;
5659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 while ((int)m_lastRates.size() > RATE_AVERAGE_COUNT) {
5660 ✗ m_lastRatesSum -= m_lastRates.front();
5661 ✗ m_lastRates.pop_front();
5662 }
5663 3 m_currentRate = rate;
5664 3 m_rateDiff = 0;
5665 3 m_lastDiff = rawdiff;
5666 }
5667 20 return 1;
5668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97 times.
97 } else if (diff > 10000) {
5669 // More than 10 seconds off — jump to the master position
5670 ✗ gint64 pos_ns = (gint64)(masterMediaPosition * GST_SECOND);
5671 ✗ LogDebug(VB_MEDIAOUT, "GStreamer Diff: %d\tVery far, jumping to: %0.3f\t(currently at %0.3f)\n",
5672 rawdiff, masterMediaPosition, m_mediaOutputStatus->mediaSeconds);
5673 ✗ if (!m_pwSinkStreamId.empty() && m_currentRate != 1.0f) {
5674 // The seek's rate of 1 doesn't reach the clock or the stream.
5675 ✗ ApplyRate(1.0f);
5676 }
5677 ✗ EndTimelineShift();
5678 ✗ gst_element_seek(m_pipeline, 1.0, GST_FORMAT_TIME,
5679 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
5680 GST_SEEK_TYPE_SET, pos_ns, GST_SEEK_TYPE_NONE, 0);
5681 ✗ m_lastRates.clear();
5682 ✗ m_lastRates.push_back(1.0f);
5683 ✗ m_lastRatesSum = 1.0f;
5684 ✗ m_currentRate = 1.0f;
5685 ✗ m_rateDiff = 0;
5686 ✗ m_lastDiff = -1; // after seeking, assume slightly behind master
5687 ✗ return 1;
5688
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 83 times.
97 } else if (diff < 100) {
5689 // Very close — could be transient; delay one cycle
5690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!m_lastDiff) {
5691 ✗ LogDebug(VB_MEDIAOUT, "GStreamer Diff: %d\tVery close but could be transient, wait till next time\n", rawdiff);
5692 ✗ m_lastDiff = rawdiff;
5693 ✗ return 1;
5694 }
5695 }
5696
5697 // Calculate proportional rate adjustment
5698 97 float rateDiffF = (float)diff;
5699
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 29 times.
97 if (m_mediaOutputStatus->mediaSeconds > 10) {
5700 68 rateDiffF /= 100.0f;
5701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (rateDiffF > 10.0f)
5702 ✗ rateDiffF = 10.0f;
5703 } else {
5704 // In first 10 seconds, use larger rate changes to sync faster
5705 29 rateDiffF /= 50.0f;
5706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (rateDiffF > 20.0f)
5707 ✗ rateDiffF = 20.0f;
5708 }
5709
5710 97 rateDiffF *= sign;
5711 97 int rateDiffI = (int)std::round(rateDiffF);
5712
5713
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 LogExcess(VB_MEDIAOUT, "GStreamer Diff: %d\trateDiffI: %d m_rateDiff: %d\n", rawdiff, rateDiffI, m_rateDiff);
5714
5715
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 79 times.
97 if (rateDiffI < m_rateDiff) {
5716
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 18 times.
50 for (int r = rateDiffI; r < m_rateDiff; r++) {
5717 32 rate = rate * 1.02f;
5718 }
5719
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 LogDebug(VB_MEDIAOUT, "GStreamer Diff: %d\tSpeedUp %0.3f/%0.3f [goal/current]\n", rawdiff, rate, m_currentRate);
5720
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 51 times.
79 } else if (rateDiffI > m_rateDiff) {
5721
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 28 times.
58 for (int r = rateDiffI; r > m_rateDiff; r--) {
5722 30 rate = rate * 0.98f;
5723 }
5724
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 LogDebug(VB_MEDIAOUT, "GStreamer Diff: %d\tSlowDown %0.3f/%0.3f [goal/current]\n", rawdiff, rate, m_currentRate);
5725 } else {
5726 // No rate change needed
5727
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 LogExcess(VB_MEDIAOUT, "GStreamer Diff: %d\tno rate change\n", rawdiff);
5728 51 return 1;
5729 }
5730
5731 // Add to rate history for running average
5732
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 m_lastRates.push_back(rate);
5733 46 m_lastRatesSum += rate;
5734
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 27 times.
46 if ((int)m_lastRates.size() > RATE_AVERAGE_COUNT) {
5735 19 m_lastRatesSum -= m_lastRates.front();
5736 19 m_lastRates.pop_front();
5737 }
5738
5739 // Cross-unity check: if rate crossed 1.0, reset to 1.0
5740
6/8
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 42 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 4 times.
46 if (((rate > 1.0f) && (m_currentRate < 1.0f)) || ((rate < 1.0f) && (m_currentRate > 1.0f))) {
5741 ✗ rate = 1.0f;
5742 ✗ m_rateDiff = 0;
5743 }
5744
5745
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 LogExcess(VB_MEDIAOUT, "GStreamer Diff: %d\toldDiff: %d\tnewRate: %0.3f oldRate: %0.3f avgRate: %0.3f rateSum: %0.3f/%d\n",
5746 rawdiff, m_lastDiff, m_lastRates.back(), m_currentRate, rate, m_lastRatesSum, (int)m_lastRates.size());
5747
5748 // Clamp rate to safe range
5749
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (rate > 2.0f)
5750 ✗ rate = 2.0f;
5751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (rate < 0.5f)
5752 ✗ rate = 0.5f;
5753
5754 // Only apply if rate changed by > 0.001
5755
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 if ((int)(rate * 1000) != (int)(m_currentRate * 1000)) {
5756
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 LogDebug(VB_MEDIAOUT, "GStreamer Diff: %d\tApplyRate\t(%0.3f)\n", rawdiff, rate);
5757
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 ApplyRate(rate);
5758 46 m_currentRate = rate;
5759
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (rate == 1.0f) {
5760 ✗ m_rateDiff = 0;
5761 } else {
5762 46 m_rateDiff = rateDiffI;
5763 }
5764 }
5765
5766 46 m_lastDiff = rawdiff;
5767 46 return 1;
5768 1363 }
5769
5770 // A remote holding its copy of the show audio to the master, when that audio
5771 // plays through the slot's persistent output stream.
5772 //
5773 // AdjustSpeed()'s rate seeks never moved the sound there. The stream plays at
5774 // the card's fixed rate, so a pipeline sped up 2% only handed it audio faster
5775 // than the card took it: the surplus queued where neither the position nor
5776 // the card's delay shows it, and the remote settled ~0.6 s behind the master
5777 // while reporting itself in sync (slowing down starved the stream instead).
5778 // And mp3 and FLAC -- most shows -- refuse instant rate changes outright
5779 // (GstBaseParse), so every adjustment was a flushing seek.
5780 //
5781 // Here the speed is the stream's feed speed: the feed clock runs that much
5782 // faster or slower and the stream resamples to the card's rate, so the media
5783 // really plays faster or slower (PipeWireOutputStream::SetFeedSpeed()). A
5784 // PI servo on the error holds it, within +-0.5% (under 9 cents of pitch;
5785 // card-to-card drift needs well under 0.1%). An error too big to slew away
5786 // in a few seconds -- a remote starting late, a missed sync -- is one
5787 // accurate seek to where the master will be once the seek is heard, aimed
5788 // using what earlier seeks took.
5789 //
5790 // Every decision is made on the lowest reading of the last two seconds, not
5791 // the latest (FeedSyncWindow): a late sync packet can only make this remote
5792 // look ahead, so one alone never seeks, never moves the servo, and never
5793 // teaches the next seek its aim. An error the window agrees on is acted on
5794 // as before -- behind at once, ahead once two seconds of packets say so.
5795 1216 int GStreamerOutput::AdjustFeedSpeed(float masterMediaPosition) {
5796 1216 constexpr double kResyncS = 0.050; // beyond this, seek rather than slew
5797 1216 constexpr double kMaxSpeedOffset = 0.005; // +-0.5%
5798 1216 constexpr double kKp = 0.1; // per second of error
5799 1216 constexpr double kKi = kKp * kKp / 4; // critically damped
5800 1216 constexpr uint64_t kResyncSettleMs = 2000;
5801 // Video (see m_feedVideoLeadS): a seek costs the picture up to seconds and
5802 // lands within a few hundred ms at best, so seek only for a real error,
5803 // let the servo take a wider range to close the rest, and stop seeking
5804 // after kMaxSeeks in kSeekWindowMs.
5805 1216 constexpr double kVideoResyncS = 0.300;
5806 1216 constexpr double kVideoMaxSpeedOffset = 0.02; // +-2% while far off
5807 1216 constexpr double kVideoMaxLeadS = 0.5;
5808 1216 constexpr double kDefaultLeadS = 0.06;
5809 1216 constexpr size_t kMaxSeeks = 3;
5810 1216 constexpr uint64_t kSeekWindowMs = 20000;
5811 1216 const bool video = m_hasVideoStream;
5812
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 1157 times.
1216 double& leadS = video ? m_feedVideoLeadS : s_feedResyncLeadS;
5813
5814
1/2
✓ Branch 0 taken 1216 times.
✗ Branch 1 not taken.
1216 uint64_t now = GetMonotonicTimeMS();
5815 // Where the sound is now, as Process() reports it (what the feed has
5816 // handed over, less the card's queue) -- but read now: the last
5817 // Process() can be tens of ms old on a remote, which made every error
5818 // reading tens of ms off.
5819 //
5820 // Backed up by what Process() last measured, as Process() does: the card
5821 // queue estimate alone isn't what the master's position (or this one's
5822 // own reports) are measured by, and at the first song after the card had
5823 // suspended it isn't known at all.
5824 1216 double local = m_mediaOutputStatus->mediaSeconds;
5825 1216 gint64 pos = 0;
5826
4/8
✓ Branch 0 taken 1216 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1216 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1216 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1216 times.
✗ Branch 7 not taken.
1216 if (gst_element_query_position(m_pwFeed, GST_FORMAT_TIME, &pos) && pos >= 0) {
5827 int64_t latNs;
5828
2/4
✓ Branch 0 taken 1216 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1216 times.
✗ Branch 3 not taken.
1216 if (m_measuredLatMs != 0 && now - m_measuredLatMs < 1000) {
5829 1216 latNs = m_measuredLatNs;
5830 } else {
5831 ✗ bool notRunning = false;
5832 ✗ latNs = AudioSinkLatencyNs(&notRunning);
5833 ✗ if (notRunning) {
5834 ✗ latNs = pos;
5835 }
5836 }
5837 1216 local = std::max(0.0, (double)(pos - latNs) / GST_SECOND - mediaOffset);
5838 }
5839 1216 double raw = local - masterMediaPosition; // > 0: this remote is ahead
5840
5841
4/4
✓ Branch 0 taken 687 times.
✓ Branch 1 taken 529 times.
✓ Branch 2 taken 92 times.
✓ Branch 3 taken 595 times.
1216 if (m_feedResyncMs && now - m_feedResyncMs < kResyncSettleMs) {
5842 92 return 1; // still landing
5843 }
5844 // The lowest of the last two seconds (FeedSyncWindow). Until a second
5845 // reading agrees, nothing is seeked or learnt on it.
5846
1/2
✓ Branch 0 taken 1124 times.
✗ Branch 1 not taken.
1124 double err = m_feedErrWindow.Add(now, raw, masterMediaPosition);
5847 1124 bool agreed = m_feedErrWindow.Agreed();
5848
4/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1096 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 14 times.
1124 if (m_feedResyncLanding && agreed) {
5849 // How far the last seek landed off is how much more (or less) lead
5850 // the next one needs.
5851 14 m_feedResyncLanding = false;
5852
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
14 if (std::fabs(err) < 1.0) {
5853
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 leadS = std::clamp(leadS - 0.8 * err, 0.0, video ? kVideoMaxLeadS : 2.0);
5854 } else {
5855 // A landing that far off teaches nothing, and the lead is never
5856 // corrected from it: left alone, a lead that overshoots by a second
5857 // lands every seek that far ahead and seeks again, for good.
5858 1 leadS = kDefaultLeadS;
5859 }
5860
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 LogDebug(VB_MEDIAOUT, "GStreamer feed sync: resync landed %+.1f ms from the master; seeks now aim %.0f ms ahead\n",
5861 err * 1000.0, leadS * 1000.0);
5862 }
5863
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1124 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1124 if (m_startErrPending && agreed) {
5864 // The first error this track agrees on, before the servo has moved
5865 // it: how far its start was off, the adjustment it started with
5866 // included. The next prepared track starts with half of it: starts
5867 // also scatter, by up to a graph cycle and on a busy box by tens of
5868 // ms, and taken whole each would push the next start that far the
5869 // other way. A steady difference is within a few ms by the fourth
5870 // track. An error past what an adjustment may take is a start gone
5871 // wrong some other way, not the boxes' difference, and teaches
5872 // nothing.
5873 ✗ m_startErrPending = false;
5874 ✗ if (m_feedSeekTimesMs.empty() && local < 5.0 && std::fabs(err) * 1000.0 < kMaxRemoteStartAdjustMs) {
5875 ✗ int next = std::clamp(m_startAdjustMs - (int)std::lround(err * 500.0),
5876 ✗ -kMaxRemoteStartAdjustMs, kMaxRemoteStartAdjustMs);
5877 ✗ s_remoteStartAdjustMs[m_streamSlot] = next;
5878 ✗ LogDebug(VB_MEDIAOUT, "GStreamer feed sync: started %+.1f ms from the master (moved %+d ms); the next track starts %+d ms\n",
5879 err * 1000.0, m_startAdjustMs, next);
5880 }
5881 }
5882
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 1074 times.
1124 if (video) {
5883
6/6
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 46 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 50 times.
54 while (!m_feedSeekTimesMs.empty() && now - m_feedSeekTimesMs.front() > kSeekWindowMs) {
5884 4 m_feedSeekTimesMs.pop_front();
5885 }
5886 }
5887
8/8
✓ Branch 0 taken 1082 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 1036 times.
✓ Branch 4 taken 72 times.
✓ Branch 5 taken 1010 times.
✓ Branch 6 taken 31 times.
✓ Branch 7 taken 41 times.
1124 if (agreed && std::fabs(err) > (video ? kVideoResyncS : kResyncS) && !m_feedSeekGaveUp) {
5888
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 27 times.
31 if (video) {
5889
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 m_feedSeekTimesMs.push_back(now);
5890
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (m_feedSeekTimesMs.size() > kMaxSeeks) {
5891 1 m_feedSeekGaveUp = true;
5892
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogWarn(VB_MEDIAOUT, "GStreamer feed sync: %zu resync seeks in %d s did not hold this video to the master "
5893 "(keyframes too far apart?); steering its speed only from here\n",
5894 m_feedSeekTimesMs.size(), (int)(kSeekWindowMs / 1000));
5895 }
5896 }
5897 }
5898
8/8
✓ Branch 0 taken 1082 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 1036 times.
✓ Branch 4 taken 72 times.
✓ Branch 5 taken 1010 times.
✓ Branch 6 taken 30 times.
✓ Branch 7 taken 42 times.
1124 if (agreed && std::fabs(err) > (video ? kVideoResyncS : kResyncS) && !m_feedSeekGaveUp) {
5899 30 double target = masterMediaPosition + leadS;
5900
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 LogInfo(VB_MEDIAOUT, "GStreamer feed sync: %+.0f ms from the master (%.3f vs %.3f), seeking to %.3f\n",
5901 err * 1000.0, local, masterMediaPosition, target);
5902
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 m_pwStream->SetFeedSpeed(m_pwFeed, 1.0);
5903 30 m_feedSpeedErrInt = 0.0;
5904 30 m_feedSpeedLastMs = 0;
5905 30 m_feedResyncMs = now;
5906 30 m_feedResyncLanding = true;
5907 30 m_feedErrWindow.Clear(); // the seek starts a new timeline
5908
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 EndTimelineShift();
5909 30 gst_element_seek(m_pipeline, 1.0, GST_FORMAT_TIME,
5910 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
5911
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 GST_SEEK_TYPE_SET, (gint64)(target * GST_SECOND), GST_SEEK_TYPE_NONE, 0);
5912
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 m_pwStream->FlushFeed(m_pwFeed);
5913 30 return 1;
5914 }
5915
5916
3/4
✓ Branch 0 taken 1052 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 1052 times.
✗ Branch 3 not taken.
1094 double dt = m_feedSpeedLastMs ? std::clamp((double)(now - m_feedSpeedLastMs) / 1000.0, 0.0, 1.0) : 0.0;
5917 1094 m_feedSpeedLastMs = now;
5918
1/2
✓ Branch 0 taken 1094 times.
✗ Branch 1 not taken.
1094 m_feedSpeedErrInt = std::clamp(m_feedSpeedErrInt + err * dt, -kMaxSpeedOffset / kKi, kMaxSpeedOffset / kKi);
5919
3/4
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 1047 times.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
1094 const double maxOffset = (video && std::fabs(err) > kResyncS) ? kVideoMaxSpeedOffset : kMaxSpeedOffset;
5920
1/2
✓ Branch 0 taken 1094 times.
✗ Branch 1 not taken.
1094 double speed = 1.0 - std::clamp(kKp * err + kKi * m_feedSpeedErrInt, -maxOffset, maxOffset);
5921
1/2
✓ Branch 0 taken 1094 times.
✗ Branch 1 not taken.
1094 m_pwStream->SetFeedSpeed(m_pwFeed, speed);
5922 // `error` is what the servo steered by; `this packet` its own reading,
5923 // larger when it arrived late.
5924
1/2
✓ Branch 0 taken 1094 times.
✗ Branch 1 not taken.
1094 LogDebug(VB_MEDIAOUT, "GStreamer feed sync: master %.3f local %.3f error %+.1f ms, speed %+.0f ppm (this packet %+.1f ms)\n",
5925 masterMediaPosition, local, err * 1000.0, (speed - 1.0) * 1e6, raw * 1000.0);
5926 1094 return 1;
5927 }
5928
5929 double GStreamerOutput::s_feedResyncLeadS = 0.06;
5930
5931 118 void GStreamerOutput::pushDiff(int diff, float rate) {
5932 118 m_diffSum += diff;
5933 118 m_rateSum += rate;
5934
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 93 times.
118 if (m_diffsSize < MAX_DIFFS) {
5935 25 m_diffIdx = m_diffsSize;
5936 25 m_diffsSize++;
5937 } else {
5938 93 m_diffIdx++;
5939
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 83 times.
93 if (m_diffIdx == MAX_DIFFS) {
5940 10 m_diffIdx = 0;
5941 }
5942 93 m_diffSum -= m_diffs[m_diffIdx].first;
5943 93 m_rateSum -= m_diffs[m_diffIdx].second;
5944 }
5945 118 m_diffs[m_diffIdx].first = diff;
5946 118 m_diffs[m_diffIdx].second = rate;
5947 118 }
5948
5949 1 bool GStreamerOutput::SeekTo(float seconds) {
5950 1 std::unique_lock<std::recursive_mutex> opLock(m_opMutex, std::try_to_lock);
5951
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (!opLock.owns_lock() || !m_pipeline)
5952 ✗ return false;
5953 1 gint64 pos_ns = (gint64)(seconds * GST_SECOND);
5954 // Same rule as AdjustSpeed(): a flushing seek into a pipeline that has
5955 // never produced a position blocks the caller for good if its preroll is
5956 // wedged (#2727), and callers hold StreamSlotManager's lock on the main
5957 // loop. The usual case is a companion that was just started for a
5958 // resumed entry, so keep the position and let Process() seek once the
5959 // pipeline is running.
5960
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (m_wallStartMs == 0) {
5961
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogDebug(VB_MEDIAOUT, "GStreamer: holding seek to %0.3fs until the pipeline has prerolled\n", seconds);
5962 1 m_pendingSeekNs = pos_ns;
5963 1 return true;
5964 }
5965 ✗ EndTimelineShift();
5966 ✗ return gst_element_seek(m_pipeline, 1.0, GST_FORMAT_TIME,
5967 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
5968 GST_SEEK_TYPE_SET, pos_ns, GST_SEEK_TYPE_NONE, 0)
5969 ? true
5970 ✗ : false;
5971 1 }
5972
5973 50 void GStreamerOutput::ApplyRate(float rate) {
5974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (!m_pipeline)
5975 ✗ return;
5976
5977 // A pipewiresink of the pipeline's own is drained by PipeWire at the
5978 // graph's rate whatever the pipeline's, and the rate seek below changed
5979 // nothing that could be heard or seen: a remote playing more than two
5980 // channels fell behind a fast master at the full difference. Two things
5981 // move it instead. The stream's adapter resamples by its rate prop, so
5982 // it takes the media that much faster or slower; but a sink that syncs
5983 // hands buffers over only as its clock reaches them, so on its own that
5984 // starves the stream rather than speeding it up. So the pipeline's
5985 // clock is run at the same rate, as the persistent stream's feed clock is
5986 // (AdjustFeedSpeed()): the sink delivers at the speed the stream takes,
5987 // and the position -- read off that clock -- moves with the sound.
5988
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (!m_pwSinkStreamId.empty()) {
5989
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (GstClock* clock = gst_pipeline_get_clock(GST_PIPELINE(m_pipeline))) {
5990 GstClockTime cInt, cExt, num, den;
5991
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 gst_clock_get_calibration(clock, &cInt, &cExt, &num, &den);
5992
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 GstClockTime internal = gst_clock_get_internal_time(clock);
5993
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 GstClockTime external = gst_clock_adjust_with_calibration(clock, internal, cInt, cExt, num, den);
5994
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 gst_clock_set_calibration(clock, internal, external, (GstClockTime)llround(rate * 1e9), 1000000000ULL);
5995
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 gst_object_unref(clock);
5996 }
5997 50 SetPipeWireStreamRate(m_pwSinkStreamId, rate);
5998 50 return;
5999 }
6000
6001 // Use instant-rate-change (GStreamer >= 1.18) for glitch-free rate adjustment.
6002 // Falls back to a flush seek if instant-rate-change fails.
6003 ✗ gboolean ok = gst_element_seek(m_pipeline, (gdouble)rate, GST_FORMAT_TIME,
6004 GST_SEEK_FLAG_INSTANT_RATE_CHANGE,
6005 GST_SEEK_TYPE_NONE, 0, GST_SEEK_TYPE_NONE, 0);
6006 ✗ if (!ok) {
6007 // Fallback: flush seek to current position with new rate
6008 ✗ gint64 pos = 0;
6009 ✗ if (gst_element_query_position(m_pipeline, GST_FORMAT_TIME, &pos)) {
6010 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: instant-rate-change failed, falling back to flush seek at %" GST_TIME_FORMAT "\n",
6011 GST_TIME_ARGS(pos));
6012 ✗ EndTimelineShift();
6013 ✗ gst_element_seek(m_pipeline, (gdouble)rate, GST_FORMAT_TIME,
6014 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
6015 GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_NONE, 0);
6016 } else {
6017 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: ApplyRate(%0.3f) failed — could not query position\n", rate);
6018 }
6019 }
6020 }
6021
6022 // A reference to the volume element, or null. OnNoMorePads() removes the
6023 // audio chain from a video-only pipeline and drops m_volume -- freeing the
6024 // element -- on a streaming thread, under the callback guard, while the
6025 // volume setters run on the playlist or an HTTP thread (a slot's saved volume
6026 // is restored while the pipeline is still prerolling, which is exactly when
6027 // no-more-pads fires). Taken under the same guard, the reference keeps the
6028 // element alive for the caller's g_object_set().
6029 //
6030 // Stop() races this too: an output stays on its slot until Close(), so Set
6031 // Slot Volume reaches one that Play Media is replacing or Stop Media Slot is
6032 // stopping on another HTTP thread. DetachCallbacks() clears `self` under the
6033 // guard lock before Stop() hands m_volume to the teardown thread and before
6034 // Close() unrefs it, so with `self` gone m_volume is not ours to read, and the
6035 // guard pointer itself is read atomically because DetachCallbacks() drops it.
6036 // `self` being set says only that no teardown has begun: Start() builds the
6037 // pipeline, m_volume included, without the guard lock, and is kept safe by
6038 // taking the output off its slot first, not by anything here.
6039 // Not m_opMutex: SetSlotVolume() calls in holding StreamSlotManager's lock,
6040 // which m_opMutex holders take, and Process() holds m_opMutex every tick.
6041 40995 GstElement* GStreamerOutput::RefVolumeElement() {
6042
1/2
✓ Branch 0 taken 40995 times.
✗ Branch 1 not taken.
40995 std::shared_ptr<CallbackGuard> guard = m_cbGuard.load();
6043
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 40971 times.
40995 if (!guard) {
6044 24 return nullptr;
6045 }
6046
1/2
✓ Branch 0 taken 40971 times.
✗ Branch 1 not taken.
40971 std::unique_lock<std::recursive_mutex> lock(guard->mtx);
6047
5/6
✓ Branch 0 taken 40971 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 40966 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 40966 times.
40971 if (!guard->self || !m_volume) {
6048 5 return nullptr;
6049 }
6050
3/6
✓ Branch 0 taken 40966 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40966 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40966 times.
✗ Branch 5 not taken.
40966 return GST_ELEMENT(gst_object_ref(m_volume));
6051 40995 }
6052
6053 113 void GStreamerOutput::SetVolume(int volume) {
6054 113 TrySetVolume(volume);
6055 113 }
6056
6057 40995 bool GStreamerOutput::TrySetVolume(int volume) {
6058 40995 GstElement* vol = RefVolumeElement();
6059
2/2
✓ Branch 0 taken 40966 times.
✓ Branch 1 taken 29 times.
40995 if (vol) {
6060 // volume parameter is 0-100 percentage
6061 40966 double linearVol = volume / 100.0 * m_fileGain;
6062 40966 g_object_set(vol, "volume", linearVol, NULL);
6063 40966 gst_object_unref(vol);
6064 40966 LogDebug(VB_MEDIAOUT, "GStreamer volume set to %d%% (%.2f)\n", volume, linearVol);
6065 }
6066 40995 return vol != nullptr;
6067 }
6068
6069 56 GstElement* GStreamerOutput::MakeBranchErrorGuard() {
6070 56 GstElement* guard = gst_element_factory_make("errorignore", nullptr);
6071
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 if (guard) {
6072 // Only a failing sink is in scope: a branch that can't negotiate
6073 // still fails the media, as it always has, rather than sitting dead.
6074 56 g_object_set(guard,
6075 "ignore-error", TRUE,
6076 "ignore-notnegotiated", FALSE,
6077 "convert-to", GST_FLOW_OK,
6078 NULL);
6079 }
6080 56 return guard;
6081 }
6082
6083 3 bool GStreamerOutput::CutFailedBranch(GstElement* sink) {
6084 // Walk upstream from the sink to the tee feeding its branch.
6085 3 FailedBranch b;
6086
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 GstElement* el = GST_ELEMENT(gst_object_ref(sink));
6087
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 for (int depth = 0; depth < 16 && el; depth++) {
6088
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 b.elements.push_back(el);
6089
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstPad* sinkPad = gst_element_get_static_pad(el, "sink");
6090
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 GstPad* peer = sinkPad ? gst_pad_get_peer(sinkPad) : nullptr;
6091
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (sinkPad) {
6092
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 gst_object_unref(sinkPad);
6093 }
6094
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 GstElement* up = peer ? gst_pad_get_parent_element(peer) : nullptr;
6095
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!up) {
6096 ✗ if (peer) {
6097 ✗ gst_object_unref(peer);
6098 }
6099 ✗ break;
6100 }
6101
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GstElementFactory* f = gst_element_get_factory(up);
6102
3/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 9 times.
12 if (f && strcmp(GST_OBJECT_NAME(f), "tee") == 0) {
6103 3 b.tee = up;
6104 3 b.teePad = peer;
6105 3 break;
6106 }
6107
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 gst_object_unref(peer);
6108 9 el = up;
6109 }
6110 1 auto release = [](FailedBranch& fb) {
6111
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (GstElement* e : fb.elements) {
6112
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 gst_object_unref(e);
6113 }
6114
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (fb.teePad) {
6115 1 gst_object_unref(fb.teePad);
6116 }
6117
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (fb.tee) {
6118 1 gst_object_unref(fb.tee);
6119 }
6120 1 };
6121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!b.teePad) {
6122 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: no tee above failed sink %s; it cannot be cut off\n",
6123 GST_OBJECT_NAME(sink));
6124 ✗ release(b);
6125 ✗ return false;
6126 }
6127
6128
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::lock_guard<std::mutex> lock(m_failedBranchLock);
6129
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 for (const auto& fb : m_failedBranches) {
6130
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (fb.teePad == b.teePad) {
6131
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 release(b); // a second error from a branch already cut off
6132 1 return true;
6133 }
6134 }
6135 // Everything the tee sends this way is dropped and reported as
6136 // delivered, so the failed sink's error never makes it upstream.
6137
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_pad_add_probe(b.teePad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
6138 49 [](GstPad*, GstPadProbeInfo*, gpointer) { return GST_PAD_PROBE_DROP; },
6139 nullptr, nullptr);
6140
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogWarn(VB_MEDIAOUT, "GStreamer: %s failed; cut its branch off so %s keeps playing\n",
6141 GST_OBJECT_NAME(sink), m_mediaFilename.c_str());
6142
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 m_failedBranches.push_back(std::move(b));
6143 2 m_haveFailedBranches.store(true, std::memory_order_release);
6144 2 return true;
6145 3 }
6146
6147 2 void GStreamerOutput::RemoveFailedBranches() {
6148 // The cut branch would still count as a sink that has to preroll after a
6149 // seek and post EOS at the end -- it can do neither, so a seek would wait
6150 // forever and the media would never finish. Take it out of the pipeline.
6151 2 std::vector<FailedBranch> branches;
6152 {
6153
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::lock_guard<std::mutex> lock(m_failedBranchLock);
6154 2 branches.swap(m_failedBranches);
6155 2 m_haveFailedBranches.store(false, std::memory_order_release);
6156 2 }
6157
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (auto& b : branches) {
6158
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (GstPad* qSink = gst_pad_get_peer(b.teePad)) {
6159
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_pad_unlink(b.teePad, qSink);
6160
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_object_unref(qSink);
6161 }
6162
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_element_release_request_pad(b.tee, b.teePad);
6163 // Upstream first, so nothing is pushing into an element going down.
6164
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (auto it = b.elements.rbegin(); it != b.elements.rend(); ++it) {
6165 8 GstElement* e = *it;
6166
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 gst_element_set_state(e, GST_STATE_NULL);
6167
5/10
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
8 if (m_pipeline && GST_OBJECT_PARENT(e) == GST_OBJECT(m_pipeline)) {
6168
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 gst_bin_remove(GST_BIN(m_pipeline), e);
6169 }
6170 }
6171
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 LogInfo(VB_MEDIAOUT, "GStreamer: removed failed branch %s\n",
6172 b.elements.empty() ? "?" : GST_OBJECT_NAME(b.elements.front()));
6173
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (GstElement* e : b.elements) {
6174
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 gst_object_unref(e);
6175 }
6176
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_object_unref(b.teePad);
6177
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 gst_object_unref(b.tee);
6178 }
6179 2 }
6180
6181 3943 void GStreamerOutput::ReleaseFailedBranches() {
6182 3943 std::vector<FailedBranch> branches;
6183 {
6184
1/2
✓ Branch 0 taken 3943 times.
✗ Branch 1 not taken.
3943 std::lock_guard<std::mutex> lock(m_failedBranchLock);
6185 3943 branches.swap(m_failedBranches);
6186 3943 m_haveFailedBranches.store(false, std::memory_order_release);
6187 3943 }
6188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3943 times.
3943 for (auto& b : branches) {
6189 ✗ for (GstElement* e : b.elements) {
6190 ✗ gst_object_unref(e);
6191 }
6192 ✗ gst_object_unref(b.teePad);
6193 ✗ gst_object_unref(b.tee);
6194 }
6195 3943 }
6196
6197 1882 void GStreamerOutput::SetVolumeAdjustment(int volAdj) {
6198 1882 m_volumeAdjust = volAdj;
6199
1/2
✓ Branch 0 taken 1882 times.
✗ Branch 1 not taken.
1882 if (!m_playing) {
6200 1882 return;
6201 }
6202 ✗ if (GstElement* vol = RefVolumeElement()) {
6203 ✗ double linearVol = std::min(m_fileGain * pow(10.0, volAdj / 2000.0), 10.0);
6204 ✗ g_object_set(vol, "volume", linearVol, NULL);
6205 ✗ gst_object_unref(vol);
6206 }
6207 }
6208
6209 // Static video overlay methods — called from Sequence.cpp and channeloutputthread.cpp
6210 // These walk every output driving a PixelOverlay model rather than the single
6211 // m_currentInstance they used to. m_currentInstance is claimed by slot 1
6212 // whenever slot 1 is playing, so a companion stream pointed at a model would
6213 // register the model, receive frames, and then have them go nowhere -- the
6214 // model simply never updated. m_currentInstance still selects the WLED audio
6215 // tap, which genuinely is a single-source thing.
6216 581164 bool GStreamerOutput::IsOverlayingVideo() {
6217
1/2
✓ Branch 0 taken 581164 times.
✗ Branch 1 not taken.
581164 std::lock_guard<std::mutex> lock(m_overlayOutputsLock);
6218
2/2
✓ Branch 0 taken 1369 times.
✓ Branch 1 taken 579820 times.
581189 for (GStreamerOutput* o : m_overlayOutputs) {
6219
6/6
✓ Branch 0 taken 1353 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 1348 times.
✓ Branch 4 taken 21 times.
✓ Branch 5 taken 1348 times.
1369 if (!o->m_hasVideoStream || !o->m_playing)
6220 21 continue;
6221 // The model listener swaps the pointer under this lock on an output
6222 // reload. Taken inside m_overlayOutputsLock, the order
6223 // ProcessVideoOverlay() uses.
6224
1/2
✓ Branch 0 taken 1348 times.
✗ Branch 1 not taken.
1348 std::lock_guard<std::mutex> modelLock(o->m_videoOverlayModelLock);
6225
2/2
✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 4 times.
1348 if (o->m_videoOverlayModel)
6226 1344 return true;
6227
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1344 times.
1348 }
6228 579820 return false;
6229 581164 }
6230
6231 1319 bool GStreamerOutput::ProcessVideoOverlay(unsigned int msTimestamp) {
6232
1/2
✓ Branch 0 taken 1319 times.
✗ Branch 1 not taken.
1319 std::lock_guard<std::mutex> lock(m_overlayOutputsLock);
6233
2/2
✓ Branch 0 taken 1323 times.
✓ Branch 1 taken 1319 times.
2642 for (GStreamerOutput* o : m_overlayOutputs) {
6234
1/2
✓ Branch 0 taken 1323 times.
✗ Branch 1 not taken.
1323 o->PushVideoOverlayFrame();
6235 }
6236 1319 return false;
6237 1319 }
6238
6239 1323 bool GStreamerOutput::PushVideoOverlayFrame() {
6240 1323 GStreamerOutput* self = this;
6241
3/6
✓ Branch 0 taken 1323 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1323 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1323 times.
1323 if (!self->m_playing || !self->m_hasVideoStream)
6242 ✗ return false;
6243
6244 // Take the latest frame by swapping buffers with the sample callback
6245 // rather than copying it: this runs on the channel output thread every
6246 // frame, and a frame is the whole model (~855 KB for 534x534). Both
6247 // buffers keep their capacity, so after the first frames nothing here or
6248 // in the callback allocates.
6249 1323 std::vector<uint8_t>& frameData = self->m_videoOverlayFrame;
6250 int frameW, frameH;
6251 {
6252
1/2
✓ Branch 0 taken 1323 times.
✗ Branch 1 not taken.
1323 std::lock_guard<std::mutex> lock(self->m_videoFrameMutex);
6253
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 1232 times.
1323 if (!self->m_videoFrameReady)
6254 91 return false;
6255 1232 frameData.swap(self->m_videoFrameData);
6256 1232 frameW = self->m_videoFrameWidth;
6257 1232 frameH = self->m_videoFrameHeight;
6258 1232 self->m_videoFrameReady = false;
6259
2/2
✓ Branch 0 taken 1232 times.
✓ Branch 1 taken 91 times.
1323 }
6260
6261 // Push the RGB frame to the PixelOverlayModel. blitData(), not
6262 // setData(): setData() reads width*height*bytesPerPixel from the buffer,
6263 // and an RGBW model has 4 bytes per pixel to this frame's 3 -- it read a
6264 // third past the end of every frame. blitData() converts to the model's
6265 // format and clips, which also covers a model replaced by one of another
6266 // size mid-play.
6267
1/2
✓ Branch 0 taken 1232 times.
✗ Branch 1 not taken.
1232 std::lock_guard<std::mutex> lock(self->m_videoOverlayModelLock);
6268
3/6
✓ Branch 0 taken 1232 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1232 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1232 times.
✗ Branch 5 not taken.
2464 if (self->m_videoOverlayModel && !frameData.empty() &&
6269
1/2
✓ Branch 0 taken 1232 times.
✗ Branch 1 not taken.
1232 frameData.size() >= (size_t)frameW * frameH * 3) {
6270
1/2
✓ Branch 0 taken 1232 times.
✗ Branch 1 not taken.
1232 self->m_videoOverlayModel->blitData(frameData.data(), frameW, frameH, 3, 0, 0,
6271 1232 PixelOverlayState(PixelOverlayState::Enabled));
6272 1232 self->m_videoFramesDelivered++;
6273
6274
4/4
✓ Branch 0 taken 1222 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 1212 times.
1232 if (self->m_videoFramesDelivered == 1 || (self->m_videoFramesDelivered % 100) == 0) {
6275
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 LogExcess(VB_MEDIAOUT, "GStreamer: video frame %lu delivered to overlay (%zu bytes)\n",
6276 (unsigned long)self->m_videoFramesDelivered, frameData.size());
6277 }
6278
6279 // Auto-enable model if it was disabled (same as SDLOutput behavior)
6280
3/4
✓ Branch 0 taken 1232 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 1221 times.
1232 if (self->m_videoOverlayModel->getState() == PixelOverlayState::Disabled) {
6281 11 self->m_wasOverlayDisabled = true;
6282
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 self->m_videoOverlayModel->setState(PixelOverlayState(PixelOverlayState::Enabled));
6283 }
6284 }
6285 1232 return false;
6286 1232 }
6287
6288 1877 GstFlowReturn GStreamerOutput::OnNewVideoSample(GstAppSink* appsink, gpointer userData) {
6289 1877 GStreamerOutput* self = static_cast<GStreamerOutput*>(userData);
6290
3/6
✓ Branch 0 taken 1877 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1877 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1877 times.
1877 if (!self || self->m_shutdownFlag.load(std::memory_order_acquire))
6291 ✗ return GST_FLOW_EOS;
6292
6293
1/2
✓ Branch 0 taken 1877 times.
✗ Branch 1 not taken.
1877 GstSample* sample = gst_app_sink_pull_sample(appsink);
6294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1877 times.
1877 if (!sample)
6295 ✗ return GST_FLOW_OK;
6296
6297
1/2
✓ Branch 0 taken 1877 times.
✗ Branch 1 not taken.
1877 GstBuffer* buffer = gst_sample_get_buffer(sample);
6298 // The frame's own size, from its caps: the appsink caps pin it to the
6299 // overlay model's size at Start(), but this is what the buffer holds.
6300 1877 int width = self->m_videoOverlayWidth;
6301 1877 int height = self->m_videoOverlayHeight;
6302
2/4
✓ Branch 0 taken 1877 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1877 times.
✗ Branch 3 not taken.
1877 if (GstCaps* caps = gst_sample_get_caps(sample)) {
6303
2/4
✓ Branch 0 taken 1877 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1877 times.
✗ Branch 3 not taken.
1877 if (const GstStructure* st = gst_caps_get_structure(caps, 0)) {
6304
1/2
✓ Branch 0 taken 1877 times.
✗ Branch 1 not taken.
1877 gst_structure_get_int(st, "width", &width);
6305
1/2
✓ Branch 0 taken 1877 times.
✗ Branch 1 not taken.
1877 gst_structure_get_int(st, "height", &height);
6306 }
6307 }
6308 GstMapInfo map;
6309
5/10
✓ Branch 0 taken 1877 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1877 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1877 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1877 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1877 times.
✗ Branch 9 not taken.
1877 if (width > 0 && height > 0 && gst_buffer_map(buffer, &map, GST_MAP_READ)) {
6310 1877 const size_t rowBytes = (size_t)width * 3; // RGB = 3 bytes/pixel
6311 1877 const size_t expectedSize = rowBytes * height;
6312 // Packed RGB rows are padded to 4 bytes unless the buffer is exactly
6313 // the tightly packed size.
6314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1877 times.
1877 const size_t stride = (map.size == expectedSize) ? rowBytes : ((rowBytes + 3) & ~(size_t)3);
6315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1877 times.
1877 if (map.size < stride * (height - 1) + rowBytes) {
6316 // Shorter than the frame its caps describe: drop it rather than
6317 // read past the buffer.
6318 ✗ gst_buffer_unmap(buffer, &map);
6319 ✗ gst_sample_unref(sample);
6320 ✗ return GST_FLOW_OK;
6321 }
6322
6323
1/2
✓ Branch 0 taken 1877 times.
✗ Branch 1 not taken.
1877 std::lock_guard<std::mutex> lock(self->m_videoFrameMutex);
6324
1/2
✓ Branch 0 taken 1877 times.
✗ Branch 1 not taken.
1877 if (stride == rowBytes) {
6325 // No padding — direct copy
6326
1/2
✓ Branch 0 taken 1877 times.
✗ Branch 1 not taken.
1877 self->m_videoFrameData.assign(map.data, map.data + expectedSize);
6327 } else {
6328 // GStreamer pads rows to 4-byte alignment — strip padding
6329 ✗ self->m_videoFrameData.resize(expectedSize);
6330 ✗ for (int y = 0; y < height; y++) {
6331 ✗ memcpy(&self->m_videoFrameData[(size_t)y * rowBytes], map.data + (size_t)y * stride, rowBytes);
6332 }
6333 }
6334 1877 self->m_videoFrameWidth = width;
6335 1877 self->m_videoFrameHeight = height;
6336 1877 self->m_videoFrameReady = true;
6337 1877 self->m_videoFramesReceived++;
6338
4/4
✓ Branch 0 taken 1867 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 1850 times.
1877 if (self->m_videoFramesReceived == 1 || (self->m_videoFramesReceived % 100) == 0) {
6339
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 LogExcess(VB_MEDIAOUT, "GStreamer: video frame %lu received (%zu bytes, stride=%zu, rowBytes=%zu)\n",
6340 (unsigned long)self->m_videoFramesReceived, map.size, stride, rowBytes);
6341 }
6342
1/2
✓ Branch 0 taken 1877 times.
✗ Branch 1 not taken.
1877 gst_buffer_unmap(buffer, &map);
6343 1877 }
6344
6345
1/2
✓ Branch 0 taken 1877 times.
✗ Branch 1 not taken.
1877 gst_sample_unref(sample);
6346 1877 return GST_FLOW_OK;
6347 }
6348
6349 2172 void GStreamerOutput::ReleaseCallbackGuard(gpointer data, GClosure* closure) {
6350
1/2
✓ Branch 0 taken 2172 times.
✗ Branch 1 not taken.
2172 delete static_cast<std::shared_ptr<CallbackGuard>*>(data);
6351 2172 }
6352
6353 2099 void GStreamerOutput::ReleaseBusCallbackGuard(gpointer data) {
6354
1/2
✓ Branch 0 taken 2099 times.
✗ Branch 1 not taken.
2099 delete static_cast<std::shared_ptr<CallbackGuard>*>(data);
6355 2099 }
6356
6357 5404 GStreamerOutput::BusCallbackScope::BusCallbackScope(gpointer userData) {
6358 5404 auto* held = static_cast<std::shared_ptr<CallbackGuard>*>(userData);
6359
3/6
✓ Branch 0 taken 5404 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5404 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5404 times.
5404 if (!held || !*held)
6360 27 return;
6361
1/2
✓ Branch 0 taken 5404 times.
✗ Branch 1 not taken.
5404 std::lock_guard<std::recursive_mutex> lock((*held)->mtx);
6362
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 5377 times.
5404 if (!(*held)->self)
6363 27 return;
6364 5377 guard = *held;
6365 5377 self = guard->self;
6366 5377 guard->busInFlight++;
6367
2/2
✓ Branch 0 taken 5377 times.
✓ Branch 1 taken 27 times.
5404 }
6368
6369 2 void GStreamerOutput::ScheduleLoopSeek(const std::shared_ptr<CallbackGuard>& guard) {
6370 static std::atomic<unsigned> seq{ 0 };
6371 2 std::shared_ptr<CallbackGuard> g = guard;
6372
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 Timers::INSTANCE.addTimer("GStreamerLoop" + std::to_string(seq++), GetTimeMS(), [g]() {
6373 // The guard says whether the output still exists; Close() clears
6374 // `self` under `mtx`. Seek on our own pipeline ref, outside the lock,
6375 // so a Close() on another thread never waits on the seek.
6376 2 GstElement* pipeline = nullptr;
6377 {
6378
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::lock_guard<std::recursive_mutex> lock(g->mtx);
6379 2 GStreamerOutput* self = g->self;
6380
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
2 if (!self || self->m_shutdownFlag.load() || !self->m_pipeline)
6381 ✗ return;
6382
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 pipeline = GST_ELEMENT(gst_object_ref(self->m_pipeline));
6383
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 self->EndTimelineShift();
6384
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 }
6385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!gst_element_seek_simple(pipeline, GST_FORMAT_TIME,
6386 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),
6387 0)) {
6388 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: seek back to the start for the next loop failed\n");
6389 }
6390 2 gst_object_unref(pipeline);
6391 });
6392 2 }
6393
6394 10781 GStreamerOutput::BusCallbackScope::~BusCallbackScope() {
6395
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 5377 times.
5404 if (!guard)
6396 27 return;
6397 5377 std::lock_guard<std::recursive_mutex> lock(guard->mtx);
6398
1/2
✓ Branch 0 taken 5377 times.
✗ Branch 1 not taken.
5377 if (--guard->busInFlight == 0)
6399 5377 guard->busIdle.notify_all();
6400
2/2
✓ Branch 0 taken 5377 times.
✓ Branch 1 taken 27 times.
5404 }
6401
6402 2175 void GStreamerOutput::ConnectPadSignals(GstElement* decoder, bool wantNoMorePads) {
6403
1/2
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
2175 std::shared_ptr<CallbackGuard> guard = m_cbGuard.load();
6404
3/6
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2175 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2175 times.
2175 if (!decoder || !guard)
6405 ✗ return;
6406 // The closure owns its own shared_ptr to the guard, released by
6407 // ReleaseCallbackGuard when the signal connection dies with the decoder --
6408 // which can be well after this output is gone.
6409
2/6
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2175 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2175 g_signal_connect_data(decoder, "pad-added", G_CALLBACK(OnPadAdded),
6410
1/2
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
2175 new std::shared_ptr<CallbackGuard>(guard),
6411 ReleaseCallbackGuard, (GConnectFlags)0);
6412
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 2054 times.
2175 if (wantNoMorePads) {
6413
2/6
✓ Branch 0 taken 121 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 121 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
121 g_signal_connect_data(decoder, "no-more-pads", G_CALLBACK(OnNoMorePads),
6414
1/2
✓ Branch 0 taken 121 times.
✗ Branch 1 not taken.
121 new std::shared_ptr<CallbackGuard>(guard),
6415 ReleaseCallbackGuard, (GConnectFlags)0);
6416 }
6417
1/2
✓ Branch 0 taken 2175 times.
✗ Branch 1 not taken.
2175 }
6418
6419 2281 GStreamerOutput* GStreamerOutput::LockCallbackGuard(gpointer userData,
6420 std::shared_ptr<CallbackGuard>& guard,
6421 std::unique_lock<std::recursive_mutex>& lock) {
6422 2281 auto* held = static_cast<std::shared_ptr<CallbackGuard>*>(userData);
6423
3/6
✓ Branch 0 taken 2281 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2281 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2281 times.
2281 if (!held || !*held)
6424 ✗ return nullptr;
6425 2281 guard = *held;
6426
1/2
✓ Branch 0 taken 2281 times.
✗ Branch 1 not taken.
2281 lock = std::unique_lock<std::recursive_mutex>(guard->mtx);
6427
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 2224 times.
2281 if (!guard->self) {
6428 // Close() already ran: the pipeline this callback belongs to is being
6429 // (or has been) torn down and every member below is stale.
6430 57 lock.unlock();
6431 57 return nullptr;
6432 }
6433 2224 return guard->self;
6434 }
6435
6436 2164 void GStreamerOutput::OnPadAdded(GstElement* element, GstPad* pad, gpointer userData) {
6437 2164 std::shared_ptr<CallbackGuard> guard;
6438 2164 std::unique_lock<std::recursive_mutex> guardLock;
6439
1/2
✓ Branch 0 taken 2164 times.
✗ Branch 1 not taken.
2164 GStreamerOutput* self = LockCallbackGuard(userData, guard, guardLock);
6440
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 2108 times.
2164 if (!self)
6441 56 return;
6442
6443
1/2
✓ Branch 0 taken 2108 times.
✗ Branch 1 not taken.
2108 GstCaps* caps = gst_pad_get_current_caps(pad);
6444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2108 times.
2108 if (!caps)
6445 ✗ caps = gst_pad_query_caps(pad, nullptr);
6446
6447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2108 times.
2108 if (!caps)
6448 ✗ return;
6449
6450 // Log all structures in the caps for debugging
6451
3/4
✓ Branch 0 taken 4216 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2108 times.
✓ Branch 3 taken 2108 times.
4216 for (guint i = 0; i < gst_caps_get_size(caps); i++) {
6452
2/4
✓ Branch 0 taken 2108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2108 times.
✗ Branch 3 not taken.
2108 const gchar* sname = gst_structure_get_name(gst_caps_get_structure(caps, i));
6453
1/2
✓ Branch 0 taken 2108 times.
✗ Branch 1 not taken.
2108 LogDebug(VB_MEDIAOUT, "GStreamer decodebin pad-added caps[%u]: %s\n", i, sname);
6454 }
6455
6456
2/4
✓ Branch 0 taken 2108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2108 times.
✗ Branch 3 not taken.
2108 const gchar* name = gst_structure_get_name(gst_caps_get_structure(caps, 0));
6457
1/2
✓ Branch 0 taken 2108 times.
✗ Branch 1 not taken.
2108 LogDebug(VB_MEDIAOUT, "GStreamer decodebin pad-added: %s\n", name);
6458
6459 // Record that the file yielded decodable audio, independently of whether
6460 // this pipeline has an audio chain to link it into. The audio-only fallback
6461 // links its pads through gst_parse_launch rather than here, so m_audioChain
6462 // is null there and the branch below is skipped -- but that pipeline is
6463 // exactly the one that needs to know, so the flag is set before the check.
6464
4/8
✗ Branch 0 not taken.
✓ Branch 1 taken 2108 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2108 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1995 times.
✓ Branch 7 taken 113 times.
2108 if (g_str_has_prefix(name, "audio/")) {
6465 1995 self->m_sawAudioPad.store(true, std::memory_order_release);
6466 1995 int channels = 0;
6467
4/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1968 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1992 times.
2022 if (self->m_prepareCheckChannels &&
6468
4/8
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 27 times.
✗ Branch 7 not taken.
27 (!gst_structure_get_int(gst_caps_get_structure(caps, 0), "channels", &channels) || channels < 1 ||
6469
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 24 times.
2022 channels > 2)) {
6470 // Prepared with its sound sent to the output stream on a guess
6471 // (Start()); the stream can't take this.
6472
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogDebug(VB_MEDIAOUT, "GStreamer: %s has %d audio channels; it will be built again when it starts\n",
6473 self->m_mediaFilename.c_str(), channels);
6474 3 self->m_pipelineFailed = true;
6475 }
6476 }
6477
6478
8/12
✗ Branch 0 not taken.
✓ Branch 1 taken 2108 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2108 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1995 times.
✓ Branch 7 taken 113 times.
✓ Branch 8 taken 102 times.
✓ Branch 9 taken 1893 times.
✓ Branch 10 taken 102 times.
✓ Branch 11 taken 2006 times.
2108 if (g_str_has_prefix(name, "audio/") && self->m_audioChain) {
6479
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 GstPad* sinkPad = gst_element_get_static_pad(self->m_audioChain, "sink");
6480
4/8
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 102 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 102 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 102 times.
✗ Branch 7 not taken.
102 if (sinkPad && !gst_pad_is_linked(sinkPad)) {
6481
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 GstPadLinkReturn ret = gst_pad_link(pad, sinkPad);
6482
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 if (GST_PAD_LINK_FAILED(ret)) {
6483 ✗ LogErr(VB_MEDIAOUT, "GStreamer: Failed to link audio pad: %d\n", ret);
6484 } else {
6485
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 LogDebug(VB_MEDIAOUT, "GStreamer: Linked audio pad successfully\n");
6486 102 self->m_audioLinked = true;
6487 }
6488 } else {
6489 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: Audio pad already linked or sink pad unavailable\n");
6490 }
6491
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 if (sinkPad)
6492
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 gst_object_unref(sinkPad);
6493
7/12
✗ Branch 0 not taken.
✓ Branch 1 taken 2006 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2006 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 113 times.
✓ Branch 7 taken 1893 times.
✓ Branch 8 taken 113 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 113 times.
✓ Branch 11 taken 1893 times.
2006 } else if (g_str_has_prefix(name, "video/") && self->m_videoChain) {
6494
1/2
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
113 GstPad* sinkPad = gst_element_get_static_pad(self->m_videoChain, "sink");
6495
4/8
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 113 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 113 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 113 times.
✗ Branch 7 not taken.
113 if (sinkPad && !gst_pad_is_linked(sinkPad)) {
6496
1/2
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
113 GstPadLinkReturn ret = gst_pad_link(pad, sinkPad);
6497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
113 if (GST_PAD_LINK_FAILED(ret)) {
6498 ✗ LogErr(VB_MEDIAOUT, "GStreamer: Failed to link video pad: %d\n", ret);
6499 } else {
6500
1/2
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
113 LogDebug(VB_MEDIAOUT, "GStreamer: Linked video pad successfully\n");
6501 113 self->m_videoLinked = true;
6502 }
6503 } else {
6504 ✗ LogWarn(VB_MEDIAOUT, "GStreamer: Video pad already linked or sink pad unavailable\n");
6505 }
6506
1/2
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
113 if (sinkPad)
6507
1/2
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
113 gst_object_unref(sinkPad);
6508 } else {
6509
1/2
✓ Branch 0 taken 1893 times.
✗ Branch 1 not taken.
1893 LogDebug(VB_MEDIAOUT, "GStreamer: Ignoring pad with caps: %s\n", name);
6510 }
6511
6512
1/2
✓ Branch 0 taken 2108 times.
✗ Branch 1 not taken.
2108 gst_caps_unref(caps);
6513
4/4
✓ Branch 0 taken 2108 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 2108 times.
✓ Branch 3 taken 56 times.
2220 }
6514
6515 117 void GStreamerOutput::OnNoMorePads(GstElement* element, gpointer userData) {
6516 117 std::shared_ptr<CallbackGuard> guard;
6517 117 std::unique_lock<std::recursive_mutex> guardLock;
6518
1/2
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
117 GStreamerOutput* self = LockCallbackGuard(userData, guard, guardLock);
6519
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 116 times.
117 if (!self)
6520 1 return;
6521
6522
5/6
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 102 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 116 times.
✗ Branch 5 not taken.
116 LogDebug(VB_MEDIAOUT, "GStreamer: no-more-pads (audio=%s, video=%s)\n",
6523 self->m_audioLinked ? "linked" : "not linked",
6524 self->m_videoLinked ? "linked" : "not linked");
6525
6526 // If audio chain was never connected, remove orphaned audio elements from the bin.
6527 // Without this, the pipeline will never reach EOS because the audio sinks
6528 // never receive data and never post their individual EOS events.
6529
4/6
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 102 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
116 if (!self->m_audioLinked && self->m_audioChain && self->m_pipeline) {
6530
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 LogDebug(VB_MEDIAOUT, "GStreamer: Removing unconnected audio chain (video-only media)\n");
6531
6532 // Get all audio elements by name and remove them
6533 14 const char* audioNames[] = {"aconv", "aresample", "t", "q1", "vol", "pwsink", "audiosink", "pwfeed",
6534 "q2", "aconv2", "acapsf", "sampletap", nullptr};
6535
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 14 times.
182 for (int i = 0; audioNames[i]; i++) {
6536
3/6
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 168 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 168 times.
✗ Branch 5 not taken.
168 GstElement* el = gst_bin_get_by_name(GST_BIN(self->m_pipeline), audioNames[i]);
6537
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 28 times.
168 if (el) {
6538
1/2
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
140 gst_element_set_state(el, GST_STATE_NULL);
6539
3/6
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 140 times.
✗ Branch 5 not taken.
140 gst_bin_remove(GST_BIN(self->m_pipeline), el);
6540
1/2
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
140 gst_object_unref(el);
6541 }
6542 }
6543
6544 // Release our refs and clear audio pointers
6545
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (self->m_appsink) {
6546
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 gst_object_unref(self->m_appsink);
6547 14 self->m_appsink = nullptr;
6548 }
6549
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (self->m_volume) {
6550
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 gst_object_unref(self->m_volume);
6551 14 self->m_volume = nullptr;
6552 }
6553 14 self->m_audioChain = nullptr;
6554 14 self->m_appsinkSignalId = 0;
6555 // A video that turned out to have no sound fed the output stream
6556 // nothing: Process() hands the stream back (on the main loop, where
6557 // the feed is used).
6558
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11 times.
14 if (self->m_pwFeed) {
6559 3 self->m_pwFeedOrphaned = true;
6560 }
6561 }
6562
6563 // If video chain was never connected, remove orphaned video elements.
6564
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
116 if (!self->m_videoLinked && self->m_videoChain && self->m_pipeline) {
6565
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogDebug(VB_MEDIAOUT, "GStreamer: Removing unconnected video chain (audio-only media)\n");
6566
6567 3 const char* videoNames[] = {"vq", "vconv", "vscale", "vcapsf", "vtee", "vkmsq", "vpwq", "videosink", "kmsvideosink", "displayfeed", "pwvideosink", nullptr};
6568
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 3 times.
36 for (int i = 0; videoNames[i]; i++) {
6569
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* el = gst_bin_get_by_name(GST_BIN(self->m_pipeline), videoNames[i]);
6570
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 25 times.
33 if (el) {
6571
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 gst_element_set_state(el, GST_STATE_NULL);
6572
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 gst_bin_remove(GST_BIN(self->m_pipeline), el);
6573
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 gst_object_unref(el);
6574 }
6575 }
6576
6577 // The consumers' direct kmssink chains hang off vtee, gone now, and
6578 // their elements are unnamed but for the sink. Left in, a sink that
6579 // never negotiates never posts EOS, and the bin waits for one from
6580 // every sink. Walk each up from its sink until the link runs out.
6581 // m_directConnectorIds stays: Close() releases their DRM leases.
6582
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 for (int cid : self->m_directConnectorIds) {
6583
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::string name = "dkms_" + std::to_string(cid);
6584
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 GstElement* el = gst_bin_get_by_name(GST_BIN(self->m_pipeline), name.c_str());
6585
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 while (el) {
6586 4 GstElement* up = nullptr;
6587
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (GstPad* sp = gst_element_get_static_pad(el, "sink")) {
6588
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
4 if (GstPad* peer = gst_pad_get_peer(sp)) {
6589
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 up = gst_pad_get_parent_element(peer);
6590
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 gst_object_unref(peer);
6591 }
6592
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 gst_object_unref(sp);
6593 }
6594
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 gst_element_set_state(el, GST_STATE_NULL);
6595
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 gst_bin_remove(GST_BIN(self->m_pipeline), el);
6596
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 gst_object_unref(el);
6597 4 el = up;
6598 }
6599 1 }
6600
6601 // Release our ref and clear video pointers
6602
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (self->m_videoAppsink) {
6603
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 gst_object_unref(self->m_videoAppsink);
6604 1 self->m_videoAppsink = nullptr;
6605 }
6606 3 self->m_videoChain = nullptr;
6607 3 self->m_videoAppsinkSignalId = 0;
6608 3 self->m_hasVideoStream = false;
6609 3 self->m_kmssink = nullptr; // owned by pipeline bin, already removed above
6610 // No picture of its own, so the last item's must not stay up behind
6611 // its sound. Given back by the main loop, which owns m_display (a
6612 // claim of this output there can be reading it right now).
6613 3 self->m_releaseDisplayPending = true;
6614 }
6615
4/4
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 116 times.
✓ Branch 3 taken 1 times.
118 }
6616
6617 777 GstFlowReturn GStreamerOutput::OnNewSample(GstAppSink* appsink, gpointer userData) {
6618 777 GStreamerOutput* self = static_cast<GStreamerOutput*>(userData);
6619
3/6
✓ Branch 0 taken 777 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 777 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 777 times.
777 if (!self || self->m_shutdownFlag.load(std::memory_order_acquire))
6620 ✗ return GST_FLOW_EOS;
6621
6622
1/2
✓ Branch 0 taken 777 times.
✗ Branch 1 not taken.
777 GstSample* sample = gst_app_sink_pull_sample(appsink);
6623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 777 times.
777 if (!sample)
6624 ✗ return GST_FLOW_OK;
6625
6626
1/2
✓ Branch 0 taken 777 times.
✗ Branch 1 not taken.
777 GstBuffer* buffer = gst_sample_get_buffer(sample);
6627
1/2
✓ Branch 0 taken 777 times.
✗ Branch 1 not taken.
777 GstCaps* caps = gst_sample_get_caps(sample);
6628
6629 777 int rate = 0;
6630
1/2
✓ Branch 0 taken 777 times.
✗ Branch 1 not taken.
777 if (caps) {
6631
1/2
✓ Branch 0 taken 777 times.
✗ Branch 1 not taken.
777 GstStructure* s = gst_caps_get_structure(caps, 0);
6632
1/2
✓ Branch 0 taken 777 times.
✗ Branch 1 not taken.
777 gst_structure_get_int(s, "rate", &rate);
6633 }
6634
6635 GstMapInfo map;
6636
2/4
✓ Branch 0 taken 777 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 777 times.
✗ Branch 3 not taken.
777 if (gst_buffer_map(buffer, &map, GST_MAP_READ)) {
6637 777 int numFloats = map.size / sizeof(float);
6638 777 const float* src = reinterpret_cast<const float*>(map.data);
6639
6640
1/2
✓ Branch 0 taken 777 times.
✗ Branch 1 not taken.
777 std::lock_guard<std::mutex> lock(s_sampleMutex);
6641 // Every playing slot has a tap; only the owner's audio goes in the
6642 // ring, or two slots interleave into noise.
6643
2/2
✓ Branch 0 taken 664 times.
✓ Branch 1 taken 113 times.
777 if (self == m_currentInstance) {
6644
1/2
✓ Branch 0 taken 664 times.
✗ Branch 1 not taken.
664 if (rate > 0) {
6645 664 s_sampleRate = rate;
6646 }
6647
2/2
✓ Branch 0 taken 1306880 times.
✓ Branch 1 taken 664 times.
1307544 for (int i = 0; i < numFloats; i++) {
6648 1306880 s_sampleBuffer[s_sampleWritePos] = src[i];
6649 1306880 s_sampleWritePos = (s_sampleWritePos + 1) % SAMPLE_BUFFER_SIZE;
6650 }
6651
1/2
✓ Branch 0 taken 664 times.
✗ Branch 1 not taken.
664 s_sampleLastWriteMs = GetMonotonicTimeMS();
6652 }
6653
6654
1/2
✓ Branch 0 taken 777 times.
✗ Branch 1 not taken.
777 gst_buffer_unmap(buffer, &map);
6655 777 }
6656
6657
1/2
✓ Branch 0 taken 777 times.
✗ Branch 1 not taken.
777 gst_sample_unref(sample);
6658 777 return GST_FLOW_OK;
6659 }
6660
6661 113492 GstPadProbeReturn GStreamerOutput::GateSampleTap(GstPad* pad, GstPadProbeInfo* info, gpointer userData) {
6662 113492 uint64_t lastRead = s_sampleTapReadMs.load(std::memory_order_relaxed);
6663
5/6
✓ Branch 0 taken 1059 times.
✓ Branch 1 taken 112433 times.
✓ Branch 2 taken 1059 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1059 times.
✓ Branch 5 taken 112433 times.
113492 if (lastRead && (uint64_t)GetMonotonicTimeMS() - lastRead < SAMPLE_TAP_IDLE_MS) {
6664 1059 return GST_PAD_PROBE_OK;
6665 }
6666 112433 return GST_PAD_PROBE_DROP;
6667 }
6668
6669 2458 void GStreamerOutput::SetSampleTapOwnerLocked(GStreamerOutput* o) {
6670 2458 m_currentInstance = o;
6671
1/2
✓ Branch 0 taken 2458 times.
✗ Branch 1 not taken.
2458 s_sampleBuffer.fill(0.0f);
6672 2458 s_sampleWritePos = 0;
6673 2458 s_sampleRate = 0;
6674 2458 s_sampleLastWriteMs = 0;
6675 2458 }
6676
6677 3441 bool GStreamerOutput::GetAudioSamples(float* samples, int numSamples, int& sampleRate) {
6678
2/4
✓ Branch 0 taken 3441 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3441 times.
3441 if (numSamples <= 0 || numSamples > SAMPLE_BUFFER_SIZE)
6679 ✗ return false; // the read position below would leave the ring
6680
1/2
✓ Branch 0 taken 3441 times.
✗ Branch 1 not taken.
3441 std::lock_guard<std::mutex> lock(s_sampleMutex);
6681
2/2
✓ Branch 0 taken 1192 times.
✓ Branch 1 taken 2249 times.
3441 if (!m_currentInstance)
6682 1192 return false;
6683
6684
1/2
✓ Branch 0 taken 2249 times.
✗ Branch 1 not taken.
2249 uint64_t now = GetMonotonicTimeMS();
6685 2249 uint64_t lastRead = s_sampleTapReadMs.exchange(now, std::memory_order_relaxed);
6686
6687
3/4
✓ Branch 0 taken 2245 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2245 times.
2249 if (!lastRead || now - lastRead >= SAMPLE_TAP_IDLE_MS) {
6688 // The tap has been closed, so what the buffer holds is from whenever
6689 // it last ran. Its audio starts flowing again from here.
6690
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 s_sampleBuffer.fill(0.0f);
6691 4 s_sampleWritePos = 0;
6692 4 return false;
6693 }
6694
3/4
✓ Branch 0 taken 2234 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2234 times.
2245 if (s_sampleRate == 0 || now - s_sampleLastWriteMs > SAMPLE_STALE_MS)
6695 11 return false;
6696
6697 2234 sampleRate = s_sampleRate;
6698
6699 // Read the most recent numSamples from the circular buffer
6700 2234 int readPos = (s_sampleWritePos - numSamples + SAMPLE_BUFFER_SIZE) % SAMPLE_BUFFER_SIZE;
6701
2/2
✓ Branch 0 taken 2287616 times.
✓ Branch 1 taken 2234 times.
2289850 for (int i = 0; i < numSamples; i++) {
6702 2287616 samples[i] = s_sampleBuffer[readPos];
6703 2287616 readPos = (readPos + 1) % SAMPLE_BUFFER_SIZE;
6704 }
6705 2234 return true;
6706 3441 }
6707
6708 // ──────────────────────────────────────────────────────────────────────────────
6709 // AES67 zero-hop RTP branch helpers (Phase 7.9)
6710 // ──────────────────────────────────────────────────────────────────────────────
6711 #ifdef HAS_AES67_GSTREAMER
6712 2177 void GStreamerOutput::AttachAES67Branches(GstElement* tee) {
6713 // Inline zero-hop branches are disabled. They create a second RTP stream
6714 // (different SSRC) alongside the standalone pipewiresrc→udpsink pipeline,
6715 // which confuses AES67 receivers causing repeated/out-of-time audio.
6716 // The standalone pipeline with sync=false already provides low latency.
6717 (void)tee;
6718 2177 return;
6719 }
6720
6721 2399 void GStreamerOutput::DetachAES67Branches() {
6722 // No-op: inline branches are disabled.
6723 2399 }
6724 #endif // HAS_AES67_GSTREAMER
6725
6726 // ──────────────────────────────────────────────────────────────────────────────
6727 // PipeWire filter-chain delay buffer flush
6728 // ──────────────────────────────────────────────────────────────────────────────
6729 // A delayed group member plays through a filter-chain of builtin delay nodes,
6730 // and their ring buffers hold the last delaySec of whatever went in. When a
6731 // track stops, that tail would play on for the length of the delay, and
6732 // after an idle spell a stalled chain plays it ahead of the next track.
6733 //
6734 // Nothing in PipeWire empties a delay node's ring buffer: it has no reset,
6735 // and changing its "Delay (s)" only moves the read point, so zeroing the
6736 // delay and restoring it reads the same old samples back. Instead the
6737 // chain's output (<fx>_out, a stream FPP itself never mutes -- a member's
6738 // mute is on the chain's sink) is muted for the length of the delay: by then
6739 // everything in the buffer came in after the flush, and the next track,
6740 // itself delayed that long, has not come through yet.
6741
6742 static const char* const kPipeWireEnv =
6743 "PIPEWIRE_RUNTIME_DIR=/run/pipewire-fpp XDG_RUNTIME_DIR=/run/pipewire-fpp";
6744
6745 // The id of the newest PipeWire node whose property `key` is `value`, or -1.
6746 // From pw-dump: pw-cli ls lists only a few properties of each node, and a
6747 // grep -B1 on its node.name line found the "=" of the node.description line
6748 // above it. Newest by object.serial: a pipeline still closing can hold a
6749 // node of the same name.
6750 54 int GStreamerOutput::FindPipeWireNodeId(const std::string& key, const std::string& value) {
6751
2/4
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
54 std::string cmd = std::string(kPipeWireEnv) + " timeout 3 pw-dump 2>/dev/null";
6752
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 FILE* fp = popen(cmd.c_str(), "r");
6753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (!fp) {
6754 ✗ return -1;
6755 }
6756 54 std::string out;
6757 char buf[8192];
6758 size_t r;
6759
3/4
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 670 times.
✓ Branch 3 taken 54 times.
724 while ((r = fread(buf, 1, sizeof(buf), fp)) > 0) {
6760
1/2
✓ Branch 0 taken 670 times.
✗ Branch 1 not taken.
670 out.append(buf, r);
6761 }
6762
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 pclose(fp);
6763
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 Json::Value dump;
6764
5/10
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 54 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 54 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 54 times.
54 if (!LoadJsonFromString(out, dump) || !dump.isArray()) {
6765 ✗ return -1;
6766 }
6767 54 int best = -1;
6768 54 Json::Int64 bestSerial = -1;
6769
7/12
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3113 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3113 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3167 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3113 times.
✓ Branch 11 taken 54 times.
3167 for (const auto& obj : dump) {
6770
13/28
✓ Branch 0 taken 3113 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3113 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3113 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3113 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3113 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3113 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2835 times.
✓ Branch 13 taken 278 times.
✓ Branch 14 taken 3113 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 3113 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 3113 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 2835 times.
✓ Branch 21 taken 278 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
3113 if (!obj.isObject() || obj.get("type", "").asString() != "PipeWire:Interface:Node") {
6771 2835 continue;
6772 }
6773
2/4
✓ Branch 0 taken 278 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 278 times.
✗ Branch 3 not taken.
278 const Json::Value& props = obj["info"]["props"];
6774
13/20
✓ Branch 0 taken 278 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 278 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 278 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 85 times.
✓ Branch 7 taken 193 times.
✓ Branch 8 taken 85 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 85 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 33 times.
✓ Branch 13 taken 52 times.
✓ Branch 14 taken 85 times.
✓ Branch 15 taken 193 times.
✓ Branch 16 taken 226 times.
✓ Branch 17 taken 52 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
278 if (!props.isObject() || !props.isMember(key) || props[key].asString() != value) {
6775 226 continue;
6776 }
6777
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 const Json::Value& serial = props["object.serial"];
6778
3/6
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 52 times.
✗ Branch 5 not taken.
52 Json::Int64 n = serial.isIntegral() ? serial.asInt64() : 0;
6779
5/10
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 52 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 52 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 52 times.
✗ Branch 9 not taken.
52 if (n >= bestSerial && obj["id"].isIntegral()) {
6780
2/4
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
52 best = obj["id"].asInt();
6781 52 bestSerial = n;
6782 }
6783 }
6784 54 return best;
6785 54 }
6786
6787 // No result: fppd reaps its children itself (SA_NOCLDWAIT), so system()
6788 // returns -1 whatever pw-cli did.
6789 54 static void SetPipeWireNodeProps(int nodeId, const std::string& props) {
6790
3/6
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 54 times.
✗ Branch 5 not taken.
216 std::string cmd = std::string(kPipeWireEnv) + " timeout 3 pw-cli set-param " + std::to_string(nodeId) +
6791
3/6
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 54 times.
✗ Branch 5 not taken.
54 " Props '" + props + "' >/dev/null 2>&1";
6792
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 system(cmd.c_str());
6793 54 }
6794
6795 // Sets the adapter rate of the stream tagged fpp.stream.id = `streamId`.
6796 // Detached, since AdjustSpeed() runs on the main loop with mediaOutputLock
6797 // held, and serialized: of the requests queued behind one in flight only the
6798 // newest is sent.
6799 50 void GStreamerOutput::SetPipeWireStreamRate(const std::string& streamId, float rate) {
6800 static std::mutex lock;
6801 static std::mutex sendLock;
6802
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 47 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
50 static std::map<std::string, uint64_t> latest;
6803 uint64_t seq;
6804 {
6805
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 std::lock_guard<std::mutex> l(lock);
6806
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 seq = ++latest[streamId];
6807 50 }
6808
1/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
150 std::thread([streamId, rate, seq]() {
6809
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 SetThreadName("FPP-PWRate");
6810
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 std::lock_guard<std::mutex> sending(sendLock);
6811 {
6812
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 std::lock_guard<std::mutex> l(lock);
6813
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 auto it = latest.find(streamId);
6814
3/6
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 50 times.
50 if (it == latest.end() || it->second != seq) {
6815 ✗ return;
6816 }
6817
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 }
6818
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
100 int nodeId = FindPipeWireNodeId("fpp.stream.id", streamId);
6819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (nodeId < 0) {
6820 ✗ LogDebug(VB_MEDIAOUT, "GStreamer: no PipeWire node for stream %s, rate %0.3f not set\n",
6821 streamId.c_str(), rate);
6822 ✗ std::lock_guard<std::mutex> l(lock);
6823 ✗ auto it = latest.find(streamId);
6824 ✗ if (it != latest.end() && it->second == seq) {
6825 ✗ latest.erase(it);
6826 }
6827 ✗ return;
6828 ✗ }
6829 char props[64];
6830 50 snprintf(props, sizeof(props), "{ rate = %.6f }", rate);
6831
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 SetPipeWireNodeProps(nodeId, props);
6832
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 LogDebug(VB_MEDIAOUT, "GStreamer: PipeWire stream rate %0.3f (node %d)\n", rate, nodeId);
6833
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 std::lock_guard<std::mutex> l(lock);
6834
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 auto it = latest.find(streamId);
6835
3/6
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
✗ Branch 5 not taken.
50 if (it != latest.end() && it->second == seq) {
6836
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 latest.erase(it); // one id per track: don't keep them all
6837 }
6838
3/6
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
✗ Branch 5 not taken.
100 }).detach();
6839 50 }
6840
6841 2405 bool GStreamerOutput::OwnsSlotStatus() {
6842
4/4
✓ Branch 0 taken 2391 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 2385 times.
✓ Branch 3 taken 6 times.
2405 return m_claimedSlot && StreamSlotManager::Instance().ClaimedByOrFree(m_streamSlot, this);
6843 }
6844
6845 605 void GStreamerOutput::StartFlushes() {
6846 605 FlushPipeWireDelayBuffers();
6847 #ifdef HAS_AES67_GSTREAMER
6848
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 603 times.
605 if (AES67Manager::INSTANCE.IsActive()) {
6849 2 AES67Manager::INSTANCE.FlushSendPipelines();
6850 }
6851 #endif
6852 605 }
6853
6854 3460 void GStreamerOutput::FlushPipeWireDelayBuffers() {
6855 // Read audio groups config to find delay values
6856
3/6
✓ Branch 0 taken 3460 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3460 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3460 times.
✗ Branch 5 not taken.
3460 std::string configPath = FPP_DIR_CONFIG("/pipewire-audio-groups.json");
6857
3/4
✓ Branch 0 taken 3460 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3452 times.
✓ Branch 3 taken 8 times.
3460 if (!FileExists(configPath)) {
6858 3452 return;
6859 }
6860
6861
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Json::Value root;
6862
7/10
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 5 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 5 times.
8 if (!LoadJsonFromFile(configPath, root, JsonRoot::Object) || !root.isMember("groups")) {
6863 3 return;
6864 }
6865
6866 struct DelayInfo {
6867 std::string fxOutName; // e.g. fpp_fx_g1_s3_out
6868 double delaySec;
6869 };
6870 5 std::vector<DelayInfo> delays;
6871
6872 // Values of the wrong JSON type threw from here, on media start, to
6873 // std::terminate; flush what could be read.
6874 try {
6875
8/14
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 9 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 5 times.
✓ Branch 13 taken 4 times.
9 for (const auto& group : root["groups"]) {
6876
4/6
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 1 times.
7 int groupId = group.get("id", 0).asInt();
6877
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (!group.isMember("members"))
6878 ✗ continue;
6879
8/14
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 4 times.
8 for (const auto& member : group["members"]) {
6880
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 std::string cardId = member.get("cardId", "").asString();
6881
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 double delayMs = member.get("delayMs", 0.0).asDouble();
6882
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
4 if (cardId.empty() || delayMs <= 0)
6883 ✗ continue; // no delay buffer to flush
6884
6885 // Normalize card ID to match PHP: preg_replace('/[^a-zA-Z0-9_]/', '_', strtolower($cardId))
6886 4 std::string cardIdNorm;
6887
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 4 times.
40 for (char c : cardId) {
6888
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
36 if (std::isalnum(c) || c == '_')
6889
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 cardIdNorm += std::tolower(c);
6890 else
6891 ✗ cardIdNorm += '_';
6892 }
6893
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 std::string fxNodeName = "fpp_fx_g" + std::to_string(groupId) + "_" + cardIdNorm;
6894 4 delays.push_back({ fxNodeName + "_out", delayMs / 1000.0 });
6895
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 }
6896 }
6897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } catch (const std::exception& e) {
6898
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
3 WarningHolder::InvalidConfigEntry("pipewire-audio-groups.json", e.what());
6899 1 }
6900
6901
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 if (delays.empty())
6902 1 return;
6903
6904 // Start() and Close() both flush, often within a few ms of each other:
6905 // only the newest flush of a chain unmutes it.
6906 static std::mutex flushLock;
6907
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
4 static std::map<std::string, uint64_t> flushGeneration;
6908
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (const auto& d : delays) {
6909 uint64_t generation;
6910 {
6911
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 std::lock_guard<std::mutex> lock(flushLock);
6912
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 generation = ++flushGeneration[d.fxOutName];
6913 4 }
6914 // Detached: pw-cli takes ~100 ms, and this runs from Start() and
6915 // Close() with media locks held.
6916
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 uint64_t unmuteAt = GetMonotonicTimeMS() + (uint64_t)(d.delaySec * 1000.0);
6917
1/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12 std::thread([d, generation, unmuteAt]() {
6918
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
6 SetThreadName("FPP-PWFlush");
6919
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
6 int nodeId = FindPipeWireNodeId("node.name", d.fxOutName);
6920
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (nodeId < 0) {
6921 2 LogDebug(VB_MEDIAOUT, "PipeWire delay flush: no node %s\n", d.fxOutName.c_str());
6922 2 return;
6923 }
6924
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 SetPipeWireNodeProps(nodeId, "{ mute = true }");
6925 2 LogDebug(VB_MEDIAOUT, "PipeWire delay flush: muted %s (node %d)\n", d.fxOutName.c_str(), nodeId);
6926 2 uint64_t now = GetMonotonicTimeMS();
6927
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (unmuteAt > now) {
6928
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::this_thread::sleep_for(std::chrono::milliseconds(unmuteAt - now));
6929 }
6930 {
6931
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::lock_guard<std::mutex> lock(flushLock);
6932
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (flushGeneration[d.fxOutName] != generation) {
6933 ✗ return; // a later flush holds the mute now
6934 }
6935
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 }
6936
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 SetPipeWireNodeProps(nodeId, "{ mute = false }");
6937 2 LogDebug(VB_MEDIAOUT, "PipeWire delay buffer of %s flushed (%.0f ms)\n", d.fxOutName.c_str(),
6938 d.delaySec * 1000.0);
6939
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 }).detach();
6940 }
6941
8/12
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 3456 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
3469 }
6942
6943 #endif // HAS_GSTREAMER
6944