GCC Code Coverage Report


Directory: ./
File: mediaoutput/VideoDisplay.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 360 409 88.0%
Functions: 29 33 87.9%
Branches: 334 662 50.5%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2026 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12
13 #include "fpp-pch.h"
14
15 #include "VideoDisplay.h"
16
17 #ifdef HAS_VIDEO_DISPLAY
18
19 #include <gst/app/gstappsink.h>
20 #include <gst/app/gstappsrc.h>
21
22 #include <chrono>
23 #include <condition_variable>
24 #include <map>
25 #include <thread>
26 #include <vector>
27
28 #include "GStreamerOut.h"
29 #include "Warnings.h"
30 #include "common.h"
31 #include "log.h"
32
33 namespace {
34
35 // How long a video sink may be late before a frame is dropped: kmssink's own
36 // default (GstVideoSink), which the feed takes over along with the timing.
37 constexpr gint64 FEED_MAX_LATENESS_NS = 20 * GST_MSECOND;
38 // Bounds the display pipeline's NULL transition, as for a media pipeline.
39 constexpr int TEARDOWN_TIMEOUT_MS = 10000;
40 // The Pi's vc4 driver needs this long after a kmssink lets go of a connector
41 // before another can claim it ("general resource error" otherwise).
42 constexpr int DRM_RELEASE_DELAY_MS = 150;
43 // How long a display nothing uses stays up before it closes. A next track
44 // that was prepared holds it throughout; one that wasn't (more than two audio
45 // channels, AES67 sending, a pause between entries, a prepare that expired)
46 // acquires it again only as it starts, and closing at once put the console
47 // up between them. Short, so that what the display stands aside for -- a
48 // Virtual Matrix on the port, the console after the show -- comes back at
49 // once all the same.
50 constexpr int IDLE_LINGER_MS = 400;
51
52 std::mutex s_mapLock;
53 std::map<std::string, std::shared_ptr<VideoDisplay>> s_displays;
54 std::condition_variable s_wake;
55 bool s_workerStarted = false;
56 bool s_shutdown = false;
57 // Displays taken off s_displays and still being torn down on another thread
58 // (the worker, or a display replaced with other settings), which ShutdownAll()
59 // waits for: fppd marks every open descriptor close-on-exec once media is shut
60 // down, and a teardown still closing its lease then raced that.
61 int s_closing = 0;
62 std::condition_variable s_closed;
63
64 14 void DoneClosing() {
65 {
66
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 std::lock_guard<std::mutex> lock(s_mapLock);
67 14 s_closing--;
68 14 }
69 14 s_closed.notify_all();
70 14 }
71
72 92 std::string KeyFor(const VideoDisplay::Config& cfg) {
73
2/4
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
✗ Branch 3 not taken.
92 return cfg.cardPath + ":" + std::to_string(cfg.connectorId);
74 }
75
76 30 bool SameCrop(const VideoCropRect& a, const VideoCropRect& b) {
77
4/8
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
60 return a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height &&
78
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
60 a.targetAspect == b.targetAspect;
79 }
80
81 // What an appsink's callback is handed: the display, kept alive for as long as
82 // the appsink holds the callbacks, and which feed the appsink is.
83 struct FeedRef {
84 std::shared_ptr<VideoDisplay> display;
85 uint64_t token;
86 };
87
88 // The display a feed's FeedRef names. Its own function, never inlined, so
89 // that a sanitizer can be told about this one read alone: the FeedRef is
90 // handed to gst_pad_add_probe() and gst_app_sink_set_callbacks()
91 // (AttachFeed()) and first read here on the feed's streaming thread, ordered
92 // by the pad's or the appsink's lock, inside GStreamer where TSan can't see
93 // it. Nothing past it is excused: the acquire below orders everything
94 // AttachFeed() did before publishing it, the rest of the FeedRef included.
95 1811 __attribute__((noinline)) VideoDisplay* FeedRefDisplay(gpointer data) {
96 1811 VideoDisplay* display = static_cast<FeedRef*>(data)->display.get();
97 1811 display->SyncWithAttach();
98 1811 return display;
99 }
100
101 96 void FreeFeedRef(gpointer data) {
102
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 delete static_cast<FeedRef*>(data);
103 96 }
104
105 30 void FreeBusRef(gpointer data) {
106
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 delete static_cast<std::weak_ptr<VideoDisplay>*>(data);
107 30 }
108
109 } // namespace
110
111 30 bool VideoDisplay::Config::SameOutput(const Config& o) const {
112
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
60 return cardPath == o.cardPath && connectorId == o.connectorId && width == o.width &&
113
4/8
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
60 height == o.height && SameCrop(crop, o.crop) && scaling == o.scaling;
114 }
115
116 30 VideoDisplay::VideoDisplay(const Config& cfg) :
117
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 m_cfg(cfg) {
118 30 }
119
120 36 VideoDisplay::~VideoDisplay() {
121 // Teardown() normally ran on the worker; this only catches a display that
122 // never got that far (a failed Build()).
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!m_closed) {
124 ✗ Teardown("destroyed");
125 }
126 18 }
127
128 32 std::shared_ptr<VideoDisplay> VideoDisplay::Acquire(const Config& cfg) {
129
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 GStreamerOutput::EnsureGStreamerInit();
130 32 std::shared_ptr<VideoDisplay> stale;
131 32 std::shared_ptr<VideoDisplay> display;
132 32 std::shared_ptr<VideoDisplay> shared;
133 {
134
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 std::unique_lock<std::mutex> lock(s_mapLock);
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s_shutdown) {
136 ✗ return nullptr;
137 }
138
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 5 times.
32 if (!s_workerStarted) {
139 27 s_workerStarted = true;
140
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 std::thread(WorkerLoop).detach();
141 }
142
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 auto it = s_displays.find(KeyFor(cfg));
143
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 30 times.
32 if (it != s_displays.end()) {
144 2 std::shared_ptr<VideoDisplay> cur = it->second;
145
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::lock_guard<std::mutex> dl(cur->m_lock);
146
3/10
✓ 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 taken 2 times.
✗ Branch 9 not taken.
2 if (!cur->m_failed && (cur->m_users > 0 || cur->m_cfg.SameOutput(cfg))) {
147
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!cur->m_cfg.SameOutput(cfg)) {
148 // A second slot on the same display, with a different crop
149 // or scaling. It shows with the first one's until the
150 // display next closes.
151 ✗ LogWarn(VB_MEDIAOUT, "VideoDisplay %s: in use with other crop/scaling settings; keeping them\n",
152 cfg.connectorName.c_str());
153 }
154 2 cur->m_users++;
155 2 shared = cur;
156 } else {
157 // Idle with other settings, or failed: build a new one in its place.
158 ✗ stale = cur;
159 ✗ s_displays.erase(it);
160 ✗ s_closing++;
161 // The new display's lease waits for this one's release (the
162 // teardown below) rather than finding the connector held.
163 ✗ DrmCards::RetireLease(stale->m_lease);
164 }
165 2 }
166
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 2 times.
32 if (!shared) {
167
4/10
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 30 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
30 display = std::shared_ptr<VideoDisplay>(new VideoDisplay(cfg));
168 30 display->m_users = 1;
169
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 s_displays[KeyFor(cfg)] = display;
170 }
171
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 }
172
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 30 times.
32 if (shared) {
173 // Another slot is still building it: wait, or its feed would get no
174 // caps to keep to (m_feedCaps).
175 bool usable;
176 {
177
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::unique_lock<std::mutex> dl(shared->m_lock);
178
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 shared->m_builtCv.wait_for(dl, std::chrono::milliseconds(TEARDOWN_TIMEOUT_MS),
179 2 [&shared]() { return !shared->m_building; });
180
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 usable = !shared->m_building && !shared->m_closed && shared->m_feedCaps;
181 2 }
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!usable) {
183 ✗ shared->Release();
184 ✗ return nullptr;
185 }
186 2 return shared;
187 }
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (stale) {
189 // On its own thread: the new lease below waits for it.
190 ✗ std::thread([stale]() {
191 ✗ SetThreadName("FPP-VDispClose");
192 ✗ stale->Teardown("settings changed");
193 ✗ DoneClosing();
194 ✗ }).detach();
195 }
196
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 bool built = display->Build();
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (!built) {
198 {
199 ✗ std::lock_guard<std::mutex> lock(s_mapLock);
200 ✗ auto it = s_displays.find(KeyFor(cfg));
201 ✗ if (it != s_displays.end() && it->second == display) {
202 ✗ s_displays.erase(it);
203 }
204 ✗ }
205 {
206 // Not this caller's any more; anyone waiting to share it (above)
207 // finds it closed and lets go too.
208 ✗ std::lock_guard<std::mutex> dl(display->m_lock);
209 ✗ display->m_users--;
210 ✗ }
211 ✗ display->Teardown("build failed");
212 }
213 {
214
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 std::lock_guard<std::mutex> dl(display->m_lock);
215 30 display->m_building = false;
216 30 }
217 30 display->m_builtCv.notify_all();
218
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 return built ? display : nullptr;
219 32 }
220
221 30 std::shared_ptr<VideoDisplay> VideoDisplay::AcquireRunning(const Config& cfg) {
222
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 std::lock_guard<std::mutex> lock(s_mapLock);
223
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 auto it = s_displays.find(KeyFor(cfg));
224
5/6
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 28 times.
30 if (s_shutdown || it == s_displays.end()) {
225 2 return nullptr;
226 }
227 28 std::shared_ptr<VideoDisplay> cur = it->second;
228
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 std::lock_guard<std::mutex> dl(cur->m_lock);
229
7/14
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 28 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 28 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 28 times.
28 if (cur->m_failed || cur->m_closed || cur->m_building || !cur->m_pipeline || !cur->m_cfg.SameOutput(cfg)) {
230 ✗ return nullptr;
231 }
232 28 cur->m_users++;
233 28 return cur;
234 30 }
235
236 2 void VideoDisplay::AddUser() {
237
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::lock_guard<std::mutex> lock(m_lock);
238 2 m_users++;
239 2 }
240
241 50 void VideoDisplay::Release() {
242 bool last;
243 50 bool releaseLease = false;
244 {
245
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 std::lock_guard<std::mutex> lock(m_lock);
246
3/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 31 times.
50 last = m_users > 0 && --m_users == 0;
247
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 31 times.
50 if (last) {
248
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 m_idleSinceMs = GetMonotonicTimeMS();
249 }
250
4/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 18 times.
50 if (last && m_releaseLeaseWhenUnused) {
251 1 m_releaseLeaseWhenUnused = false;
252 1 releaseLease = true;
253 }
254 50 }
255
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 49 times.
50 if (releaseLease) {
256 1 ReleaseLeaseAndConnector();
257 }
258
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 31 times.
50 if (last) {
259 // Under the map lock, which the worker checks under: never between
260 // its look and its wait.
261
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 std::lock_guard<std::mutex> lock(s_mapLock);
262 19 s_wake.notify_all();
263 19 }
264 50 }
265
266 30 bool VideoDisplay::Build() {
267
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 VideoOutputManager::Instance().ClaimConnectorForMedia(m_cfg.connectorId);
268 30 m_claimedConnector = true;
269
270
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 GstElement* pipeline = gst_pipeline_new("vdisplay");
271
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 GstElement* src = gst_element_factory_make("appsrc", "vdsrc");
272
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 GstElement* sink = gst_element_factory_make("kmssink", "vdsink");
273
3/6
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 30 times.
30 if (!pipeline || !src || !sink) {
274 ✗ LogErr(VB_MEDIAOUT, "VideoDisplay %s: could not create appsrc/kmssink (is gstreamer1.0-plugins-bad installed?)\n",
275 m_cfg.connectorName.c_str());
276 ✗ for (GstElement* e : { pipeline, src, sink }) {
277 ✗ if (e) {
278 ✗ gst_object_unref(gst_object_ref_sink(e));
279 }
280 }
281 ✗ return false;
282 }
283
284 // Live, so the pipeline runs without waiting for a first frame, and never
285 // blocking the track that pushes into it: if the display falls behind, the
286 // older of two queued frames goes.
287
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 g_object_set(src, "is-live", TRUE, "format", GST_FORMAT_TIME, "do-timestamp", FALSE,
288 "block", FALSE, "max-buffers", (guint64)2, "max-bytes", (guint64)0, NULL);
289
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 if (g_object_class_find_property(G_OBJECT_GET_CLASS(src), "leaky-type")) {
290
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 g_object_set(src, "leaky-type", 2 /* GST_APP_LEAKY_TYPE_DOWNSTREAM */, NULL);
291 }
292
293 // The feed has already timed each frame: show it as it arrives.
294
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 g_object_set(sink, "sync", FALSE, "async", FALSE, "qos", FALSE,
295 "enable-last-sample", FALSE, NULL);
296
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 m_lease = DrmCards::AcquireLease(m_cfg.cardPath, m_cfg.connectorId);
297
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (m_lease.fd >= 0) {
298
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 g_object_set(sink, "fd", m_lease.fd, "connector-id", m_cfg.connectorId,
299 "restore-crtc", TRUE, "skip-vsync", TRUE, NULL);
300
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (m_lease.planeId >= 0) {
301
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 g_object_set(sink, "plane-id", m_lease.planeId, NULL);
302 }
303 } else {
304 ✗ g_object_set(sink, "driver-name", "vc4", "connector-id", m_cfg.connectorId,
305 "restore-crtc", TRUE, "skip-vsync", TRUE, NULL);
306 }
307
308 // Crop and scaling, between the source and the sink: kmssink advertises
309 // GstVideoCropMeta, so a crop right ahead of it costs nothing (see
310 // VideoOutputManager::CreateCropElement()).
311
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
89 std::vector<GstElement*> chain = { src };
312
3/6
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 30 times.
61 if (GstElement* crop = VideoOutputManager::CreateCropElement(m_cfg.crop, "vd_crop")) {
313 ✗ chain.push_back(crop);
314 }
315
3/6
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 30 times.
61 for (GstElement* e : VideoOutputManager::CreateScaleElements(m_cfg.scaling, m_cfg.width, m_cfg.height, "vd")) {
316 ✗ chain.push_back(e);
317 30 }
318
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 chain.push_back(sink);
319 30 bool ok = true;
320
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
90 for (GstElement* e : chain) {
321
5/10
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
✓ 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.
60 ok = ok && gst_bin_add(GST_BIN(pipeline), e);
322 }
323
5/6
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 30 times.
✓ Branch 5 taken 30 times.
60 for (size_t i = 1; ok && i < chain.size(); i++) {
324
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 ok = gst_element_link(chain[i - 1], chain[i]);
325 }
326 {
327
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 std::lock_guard<std::mutex> lock(m_lock);
328 30 m_pipeline = pipeline;
329
3/6
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
30 m_src = GST_ELEMENT(gst_object_ref(src));
330 30 }
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (!ok) {
332 ✗ LogErr(VB_MEDIAOUT, "VideoDisplay %s: could not link the display pipeline\n", m_cfg.connectorName.c_str());
333 ✗ return false;
334 }
335
336
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 GstBus* bus = gst_element_get_bus(pipeline);
337
3/8
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 30 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
30 gst_bus_set_sync_handler(bus, OnBusMessage, new std::weak_ptr<VideoDisplay>(weak_from_this()), FreeBusRef);
338
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 gst_object_unref(bus);
339
340
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
30 if (gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
341 ✗ LogErr(VB_MEDIAOUT, "VideoDisplay %s: kmssink would not start\n", m_cfg.connectorName.c_str());
342 ✗ return false;
343 }
344
345 // What the feeds must deliver: whatever the crop/scale chain and the
346 // display's planes accept, now that kmssink has the display open.
347
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 GstPad* srcPad = gst_element_get_static_pad(src, "src");
348
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 GstCaps* caps = srcPad ? gst_pad_peer_query_caps(srcPad, nullptr) : nullptr;
349
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (srcPad) {
350
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 gst_object_unref(srcPad);
351 }
352
4/8
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 30 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 30 times.
30 if (!caps || gst_caps_is_empty(caps)) {
353 ✗ LogErr(VB_MEDIAOUT, "VideoDisplay %s: the display accepts no video format\n", m_cfg.connectorName.c_str());
354 ✗ if (caps) {
355 ✗ gst_caps_unref(caps);
356 }
357 ✗ return false;
358 }
359 {
360
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 std::lock_guard<std::mutex> lock(m_lock);
361 30 m_feedCaps = caps;
362 30 }
363
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 LogDebug(VB_MEDIAOUT, "VideoDisplay %s: running on %s (lease fd=%d plane=%d)\n", m_cfg.connectorName.c_str(),
364 m_cfg.cardPath.c_str(), m_lease.fd, m_lease.planeId);
365 30 return true;
366 30 }
367
368 30 void VideoDisplay::Teardown(const char* why) {
369 30 GstElement* pipeline = nullptr;
370 30 GstElement* src = nullptr;
371 30 uint64_t shown = 0;
372 {
373
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 std::lock_guard<std::mutex> lock(m_lock);
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (m_closed) {
375 ✗ return;
376 }
377 30 m_closed = true;
378 30 m_feedToken = 0;
379 30 pipeline = m_pipeline;
380 30 src = m_src;
381 30 m_pipeline = nullptr;
382 30 m_src = nullptr;
383 30 shown = m_framesShown;
384
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (m_feedCaps) {
385
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 gst_caps_unref(m_feedCaps);
386 30 m_feedCaps = nullptr;
387 }
388
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (m_srcCaps) {
389
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 gst_caps_unref(m_srcCaps);
390 30 m_srcCaps = nullptr;
391 }
392
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 }
393 30 LogDebug(VB_MEDIAOUT, "VideoDisplay %s: closing (%s) after %llu frames\n", m_cfg.connectorName.c_str(), why,
394 (unsigned long long)shown);
395 30 bool released = true;
396
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (pipeline) {
397 30 GstBus* bus = gst_element_get_bus(pipeline);
398 30 gst_bus_set_sync_handler(bus, nullptr, nullptr, nullptr);
399 30 gst_object_unref(bus);
400 30 released = GStreamerOutput::SetPipelineToNullBounded(pipeline, TEARDOWN_TIMEOUT_MS);
401
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (released) {
402
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 std::this_thread::sleep_for(std::chrono::milliseconds(DRM_RELEASE_DELAY_MS));
403 } else {
404 ✗ LogErr(VB_MEDIAOUT, "VideoDisplay %s: kmssink would not stop; abandoning it with its lease\n",
405 m_cfg.connectorName.c_str());
406 }
407 }
408
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (src) {
409 30 gst_object_unref(src);
410 }
411
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (pipeline) {
412 30 gst_object_unref(pipeline);
413 }
414 // After the kmssink is gone (closing a lease under a live one is
415 // undefined), unless it never stopped: it still uses the lease then.
416
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (released) {
417 bool inUse;
418 {
419
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 std::lock_guard<std::mutex> lock(m_lock);
420 30 inUse = m_users > 0;
421 30 m_releaseLeaseWhenUnused = inUse;
422 30 }
423
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 17 times.
30 if (inUse) {
424 13 LogDebug(VB_MEDIAOUT, "VideoDisplay %s: keeping its lease until the tracks on it let go\n",
425 m_cfg.connectorName.c_str());
426 } else {
427 17 ReleaseLeaseAndConnector();
428 }
429 ✗ } else if (m_claimedConnector) {
430 ✗ m_claimedConnector = false;
431 ✗ VideoOutputManager::Instance().ReleaseConnectorFromMedia(m_cfg.connectorId);
432 }
433 }
434
435 18 void VideoDisplay::ReleaseLeaseAndConnector() {
436 18 DrmCards::ReleaseLease(m_lease);
437
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (m_claimedConnector) {
438 18 m_claimedConnector = false;
439 18 VideoOutputManager::Instance().ReleaseConnectorFromMedia(m_cfg.connectorId);
440 }
441 18 }
442
443 60 uint64_t VideoDisplay::AttachFeed(GstElement* appsink, bool activate) {
444 uint64_t token;
445 60 GstCaps* caps = nullptr;
446 {
447
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 std::lock_guard<std::mutex> lock(m_lock);
448 60 token = ++m_lastToken;
449
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 28 times.
60 if (activate) {
450 32 m_feedToken = token;
451 }
452
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (m_feedCaps) {
453
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 caps = gst_caps_ref(m_feedCaps);
454 }
455 60 }
456 // The timing kmssink would have applied, now applied here: sync on the
457 // track's clock, drop what is too late, and report it upstream (QoS) so
458 // the decoder can skip ahead.
459
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 g_object_set(appsink, "sync", TRUE, "qos", TRUE, "max-lateness", FEED_MAX_LATENESS_NS,
460 "emit-signals", FALSE, "enable-last-sample", FALSE, "max-buffers", (guint)1, NULL);
461
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (caps) {
462
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 g_object_set(appsink, "caps", caps, NULL);
463
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 gst_caps_unref(caps);
464 }
465 60 GstAppSinkCallbacks callbacks = {};
466 60 callbacks.new_sample = [](GstAppSink* sink, gpointer data) -> GstFlowReturn {
467 1693 return OnFeedSample(GST_ELEMENT(sink), data);
468 };
469 // Before the feed's threads can be handed the display (see m_attached).
470 60 m_attached.fetch_add(1, std::memory_order_release);
471
3/8
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 60 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
63 gst_app_sink_set_callbacks(GST_APP_SINK(appsink), &callbacks,
472
3/8
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 60 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
60 new FeedRef{ shared_from_this(), token }, FreeFeedRef);
473 // The decoder asks downstream how to allocate its frames. The appsink
474 // has no answer, so a hardware decoder allocated its minimum and, with
475 // the display holding a few of them, ran short and fell back to copying
476 // every frame into system memory -- which kmssink then copied again:
477 // half a Pi 4 core and dropped frames for a 1080p30 H.264 video. The
478 // kmssink answers, as it did when it was in the track's own pipeline.
479
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
60 if (GstPad* sinkPad = gst_element_get_static_pad(appsink, "sink")) {
480
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
60 gst_pad_add_probe(sinkPad, GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM, OnFeedQuery,
481
3/8
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 60 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
60 new FeedRef{ shared_from_this(), token }, FreeFeedRef);
482
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 gst_object_unref(sinkPad);
483 }
484 60 return token;
485 }
486
487 830 GstPadProbeReturn VideoDisplay::OnFeedQuery(GstPad*, GstPadProbeInfo* info, gpointer userData) {
488 830 GstQuery* query = GST_PAD_PROBE_INFO_QUERY(info);
489
3/4
✓ Branch 0 taken 830 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 712 times.
✓ Branch 3 taken 118 times.
830 if (!query || GST_QUERY_TYPE(query) != GST_QUERY_ALLOCATION) {
490 712 return GST_PAD_PROBE_OK;
491 }
492
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 108 times.
118 return FeedRefDisplay(userData)->AnswerAllocation(query) ? GST_PAD_PROBE_HANDLED : GST_PAD_PROBE_OK;
493 }
494
495 118 bool VideoDisplay::AnswerAllocation(GstQuery* query) {
496 118 GstElement* src = nullptr;
497 {
498
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 std::lock_guard<std::mutex> lock(m_lock);
499
2/4
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 118 times.
118 if (!m_src || m_failed) {
500 ✗ return false;
501 }
502
3/6
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 118 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 118 times.
✗ Branch 5 not taken.
118 src = GST_ELEMENT(gst_object_ref(m_src));
503
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 }
504 118 bool answered = false;
505
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 if (GstPad* srcPad = gst_element_get_static_pad(src, "src")) {
506 118 answered = gst_pad_peer_query(srcPad, query);
507 118 gst_object_unref(srcPad);
508 }
509 118 gst_object_unref(src);
510 118 return answered;
511 }
512
513 15 void VideoDisplay::ActivateFeed(uint64_t token) {
514
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 std::lock_guard<std::mutex> lock(m_lock);
515 15 m_feedToken = token;
516 15 }
517
518 95 void VideoDisplay::DetachFeed(uint64_t token) {
519
1/2
✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
95 std::lock_guard<std::mutex> lock(m_lock);
520
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 61 times.
95 if (m_feedToken == token) {
521 34 m_feedToken = 0;
522 }
523 95 }
524
525 1693 GstFlowReturn VideoDisplay::OnFeedSample(GstElement* appsink, gpointer userData) {
526 1693 FeedRef* ref = static_cast<FeedRef*>(userData);
527 1693 GstSample* sample = gst_app_sink_pull_sample(GST_APP_SINK(appsink));
528
1/2
✓ Branch 0 taken 1693 times.
✗ Branch 1 not taken.
1693 if (sample) {
529 // A reference of its own for the push: the display outlives it
530 // whatever happens to the feed's (FreeFeedRef()) meanwhile, and its
531 // release orders the push before the display's end where the
532 // sanitizer can see it -- otherwise only the pipeline's teardown,
533 // joining this thread inside GStreamer, does.
534 // Taken through FeedRefDisplay(), as the probe does: a pooled
535 // streaming thread can run this callback with nothing TSan sees
536 // ordering it after AttachFeed() published the FeedRef.
537
1/2
✓ Branch 0 taken 1693 times.
✗ Branch 1 not taken.
1693 std::shared_ptr<VideoDisplay> display = FeedRefDisplay(userData)->shared_from_this();
538
1/2
✓ Branch 0 taken 1693 times.
✗ Branch 1 not taken.
1693 display->Push(ref->token, sample);
539
1/2
✓ Branch 0 taken 1693 times.
✗ Branch 1 not taken.
1693 gst_sample_unref(sample);
540 1693 }
541 // Never an error: a display that can't show the frame must not end the
542 // track, whose sound and lights go on.
543 1693 return GST_FLOW_OK;
544 }
545
546 1693 void VideoDisplay::Push(uint64_t token, GstSample* sample) {
547
1/2
✓ Branch 0 taken 1693 times.
✗ Branch 1 not taken.
1693 GstBuffer* buf = gst_sample_get_buffer(sample);
548
1/2
✓ Branch 0 taken 1693 times.
✗ Branch 1 not taken.
1693 GstCaps* caps = gst_sample_get_caps(sample);
549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1693 times.
1693 if (!buf) {
550 71 return;
551 }
552 1693 GstElement* src = nullptr;
553 1693 GstCaps* newCaps = nullptr;
554
1/2
✓ Branch 0 taken 1693 times.
✗ Branch 1 not taken.
1693 std::lock_guard<std::mutex> pushLock(m_pushLock);
555 {
556
1/2
✓ Branch 0 taken 1693 times.
✗ Branch 1 not taken.
1693 std::lock_guard<std::mutex> lock(m_lock);
557
4/6
✓ Branch 0 taken 1622 times.
✓ Branch 1 taken 71 times.
✓ Branch 2 taken 1622 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1622 times.
1693 if (token != m_feedToken || !m_src || m_failed) {
558 71 return;
559 }
560
3/6
✓ Branch 0 taken 1622 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1622 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1622 times.
✗ Branch 5 not taken.
1622 src = GST_ELEMENT(gst_object_ref(m_src));
561
7/10
✓ Branch 0 taken 1622 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1592 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 1592 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1592 times.
✓ Branch 8 taken 30 times.
✓ Branch 9 taken 1592 times.
1622 if (caps && (!m_srcCaps || !gst_caps_is_equal(caps, m_srcCaps))) {
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (m_srcCaps) {
563 ✗ gst_caps_unref(m_srcCaps);
564 }
565
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 m_srcCaps = gst_caps_ref(caps);
566
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 newCaps = gst_caps_ref(caps);
567 }
568 1622 m_framesShown++;
569
2/2
✓ Branch 0 taken 1622 times.
✓ Branch 1 taken 71 times.
1693 }
570
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1592 times.
1622 if (newCaps) {
571
3/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 3 times.
30 if (WillLog(LOG_DEBUG, VB_MEDIAOUT)) {
572
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 gchar* str = gst_caps_to_string(newCaps);
573
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 LogDebug(VB_MEDIAOUT, "VideoDisplay %s: frames now %s\n", m_cfg.connectorName.c_str(), str);
574
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 g_free(str);
575 }
576
3/6
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
30 gst_app_src_set_caps(GST_APP_SRC(src), newCaps);
577
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 gst_caps_unref(newCaps);
578 }
579 // The decoder's own buffer, held as long as the sink shows it -- as it
580 // was when the sink was in the track's pipeline. A copy (same memory,
581 // new metadata) let the original go back to its pool at once: a hardware
582 // decoder's pool could then hand its memory to the driver again while it
583 // was on screen, and a pool that discards buffers whose memory is shared
584 // allocated a new one for every frame. The sink doesn't sync (it shows a
585 // frame as it comes), so the track's own timestamps do no harm.
586
4/8
✓ Branch 0 taken 1622 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1622 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1622 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1622 times.
✗ Branch 7 not taken.
1622 gst_app_src_push_buffer(GST_APP_SRC(src), gst_buffer_ref(buf));
587
1/2
✓ Branch 0 taken 1622 times.
✗ Branch 1 not taken.
1622 gst_object_unref(src);
588
2/2
✓ Branch 0 taken 1622 times.
✓ Branch 1 taken 71 times.
1693 }
589
590 459 GstBusSyncReply VideoDisplay::OnBusMessage(GstBus*, GstMessage* msg, gpointer userData) {
591
2/2
✓ Branch 0 taken 453 times.
✓ Branch 1 taken 6 times.
459 if (GST_MESSAGE_TYPE(msg) != GST_MESSAGE_ERROR) {
592 453 return GST_BUS_DROP;
593 }
594 6 std::shared_ptr<VideoDisplay> self = static_cast<std::weak_ptr<VideoDisplay>*>(userData)->lock();
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!self) {
596 ✗ return GST_BUS_DROP;
597 }
598 6 GError* err = nullptr;
599 6 gchar* debug = nullptr;
600
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 gst_message_parse_error(msg, &err, &debug);
601
3/10
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
6 std::string reason = err ? GStreamerOutput::ErrorReason(err, debug) : "unknown error";
602
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 LogErr(VB_MEDIAOUT, "VideoDisplay %s: %s\n", self->m_cfg.connectorName.c_str(), reason.c_str());
603
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 LogDebug(VB_MEDIAOUT, "VideoDisplay %s debug: %s\n", self->m_cfg.connectorName.c_str(), debug ? debug : "(none)");
604 // The same banner the track's own kmssink raised (ReportVideoOutputFailure()).
605 6 std::string warning = VideoOutputManager::AspectMismatchWarning(self->m_cfg.connectorName, self->m_cfg.cardPath,
606
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 self->m_cfg.connectorId, reason);
607
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 if (warning.empty()) {
608
4/8
✓ 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.
5 warning = "Video output to " + self->m_cfg.connectorName + " failed (" + reason + ")";
609 }
610
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 WarningHolder::AddWarningTimeout(300, 74, warning); // Display Refused Video
611
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (err) {
612
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 g_error_free(err);
613 }
614
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 g_free(debug);
615 {
616
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 std::lock_guard<std::mutex> lock(self->m_lock);
617 6 self->m_failed = true;
618 6 }
619 {
620 // Under the map lock, which the worker checks under: never between
621 // its look and its wait (or it waits out its whole interval).
622
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 std::lock_guard<std::mutex> lock(s_mapLock);
623 6 s_wake.notify_all();
624 6 }
625 6 return GST_BUS_DROP;
626 6 }
627
628 27 void VideoDisplay::WorkerLoop() {
629
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 SetThreadName("FPP-VDisplay");
630
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 std::unique_lock<std::mutex> lock(s_mapLock);
631
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 27 times.
279 while (!s_shutdown) {
632 // With why, decided under the display's lock: once it is out of the
633 // map its bus thread can still mark it failed, so m_failed isn't read
634 // again after the lock is let go.
635 252 std::vector<std::pair<std::shared_ptr<VideoDisplay>, const char*>> closing;
636
1/2
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
252 uint64_t now = GetMonotonicTimeMS();
637 252 uint64_t nextCheckMs = 1000;
638
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 252 times.
472 for (auto it = s_displays.begin(); it != s_displays.end();) {
639 220 std::shared_ptr<VideoDisplay> d = it->second;
640
1/2
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
220 std::lock_guard<std::mutex> dl(d->m_lock);
641
3/4
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 191 times.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
220 bool idle = d->m_users == 0 && !d->m_building;
642
7/8
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 191 times.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 11 times.
✓ Branch 6 taken 18 times.
✓ Branch 7 taken 202 times.
220 if (idle && !d->m_failed && now - d->m_idleSinceMs < (uint64_t)IDLE_LINGER_MS) {
643 // Not yet: a track starting next may still take it up.
644 18 nextCheckMs = std::min<uint64_t>(nextCheckMs, IDLE_LINGER_MS - (now - d->m_idleSinceMs));
645 18 ++it;
646 18 continue;
647 }
648
6/6
✓ Branch 0 taken 191 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 188 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 188 times.
202 if (idle || d->m_failed) {
649
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 DrmCards::RetireLease(d->m_lease);
650
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 closing.emplace_back(d, d->m_failed ? "failed" : "idle");
651
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 it = s_displays.erase(it);
652 14 s_closing++;
653 14 continue;
654 }
655 188 ++it;
656
4/4
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 188 times.
✓ Branch 3 taken 32 times.
252 }
657
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 238 times.
252 if (!closing.empty()) {
658
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 lock.unlock();
659
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
28 for (auto& [d, why] : closing) {
660
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 d->Teardown(why);
661
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 DoneClosing();
662 }
663 14 closing.clear();
664
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 lock.lock();
665 14 continue;
666 14 }
667
1/2
✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
238 s_wake.wait_for(lock, std::chrono::milliseconds(nextCheckMs));
668
2/2
✓ Branch 0 taken 238 times.
✓ Branch 1 taken 14 times.
252 }
669 27 }
670
671 2620 void VideoDisplay::ShutdownAll() {
672 2620 std::vector<std::shared_ptr<VideoDisplay>> closing;
673 {
674
1/2
✓ Branch 0 taken 2620 times.
✗ Branch 1 not taken.
2620 std::lock_guard<std::mutex> lock(s_mapLock);
675 2620 s_shutdown = true;
676
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2620 times.
2636 for (auto& [key, d] : s_displays) {
677
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 closing.push_back(d);
678 }
679 2620 s_displays.clear();
680 2620 }
681 2620 s_wake.notify_all();
682
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2620 times.
2636 for (auto& d : closing) {
683
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 d->Teardown("shutdown");
684 }
685 // And any that were already closing. Each teardown is itself bounded.
686
1/2
✓ Branch 0 taken 2620 times.
✗ Branch 1 not taken.
2620 std::unique_lock<std::mutex> lock(s_mapLock);
687
2/4
✓ Branch 0 taken 2620 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2620 times.
2620 if (!s_closed.wait_for(lock, std::chrono::milliseconds(TEARDOWN_TIMEOUT_MS + DRM_RELEASE_DELAY_MS + 1000),
688 2624 [] { return s_closing == 0; })) {
689 ✗ LogWarn(VB_MEDIAOUT, "VideoDisplay: %d display(s) still closing at shutdown\n", s_closing);
690 }
691 2620 }
692
693 #endif
694