GCC Code Coverage Report


Directory: ./
File: mediaoutput/VideoDisplay.h
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 1 1 100.0%
Functions: 1 1 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 #pragma once
2 /*
3 * This file is part of the Falcon Player (FPP) and is Copyright (C)
4 * 2013-2026 by the Falcon Player Developers.
5 *
6 * The Falcon Player (FPP) is free software, and is covered under
7 * multiple Open Source licenses. Please see the included 'LICENSES'
8 * file for descriptions of what files are covered by each license.
9 *
10 * This source file is covered under the LGPL v2.1 as described in the
11 * included LICENSE.LGPL file.
12 */
13
14 #include "VideoOutputManager.h"
15
16 #if defined(HAS_GSTREAMER_VIDEO_OUTPUT) && !defined(PLATFORM_OSX)
17 #define HAS_VIDEO_DISPLAY
18
19 #include <gst/gst.h>
20
21 #include <condition_variable>
22 #include <cstdint>
23 #include <memory>
24 #include <mutex>
25 #include <string>
26
27 // A display connector's kmssink, kept running across the media played on it.
28 //
29 // When each track's pipeline owned its kmssink, every track change released
30 // the display: the video plane went dark and the console underneath showed
31 // until the next track's first frame, about a third of a second on a Pi 5 --
32 // at every pass of a looping video. Most of that is fixed cost: a DRM
33 // release delay, building the next pipeline, and decoding its first frame.
34 //
35 // Now a track's pipeline ends in an appsink (AttachFeed()), which keeps the
36 // track's timing -- it syncs on the track's own clock and drops late frames as
37 // a video sink does -- and hands each frame here as it falls due. This
38 // pipeline (appsrc -> crop/scale -> kmssink) shows it at once. Between tracks
39 // the last frame stays on screen, and the next track's frames replace it.
40 //
41 // It closes as soon as its last user releases it, and the console -- or a
42 // Virtual Matrix on the same connector, which stands aside while media holds
43 // it -- comes back at once. The next video in a playlist keeps it up by
44 // holding it before the current one lets go (it is prepared ahead).
45 class VideoDisplay : public std::enable_shared_from_this<VideoDisplay> {
46 public:
47 struct Config {
48 std::string cardPath; // "/dev/dri/card1"
49 int connectorId = -1;
50 std::string connectorName; // "HDMI-A-1": for the log and warnings
51 int width = 0; // the display's mode
52 int height = 0;
53 VideoCropRect crop;
54 std::string scaling = "fit";
55
56 bool SameOutput(const Config& o) const;
57 };
58
59 // The running display for cfg's connector, built if there is none. Every
60 // display returned needs a Release(). nullptr if it could not be built.
61 static std::shared_ptr<VideoDisplay> Acquire(const Config& cfg);
62 // The same, but only a display already up with these settings; nullptr
63 // rather than build one. For a track prepared while another plays: it
64 // must not take the connector from what is on it now (a Virtual Matrix
65 // stands aside for as long as a display holds it).
66 static std::shared_ptr<VideoDisplay> AcquireRunning(const Config& cfg);
67 // A user is done. The display closes when the last one is.
68 void Release();
69 // One more use of a display already held (Release() it after).
70 void AddUser();
71
72 // Make `appsink` a feed of this display: the caps it must deliver, its
73 // timing, and the callback that hands its frames over. With activate,
74 // it becomes the feed shown, and frames from a feed attached earlier are
75 // dropped from then on, so a track still tearing down can't flash up
76 // between the next one's frames. Without, its frames are dropped until
77 // ActivateFeed() (a track prepared while another plays). Returns the
78 // feed's token.
79 uint64_t AttachFeed(GstElement* appsink, bool activate = true);
80 // Pairs with AttachFeed()'s publishing of the display to a feed's
81 // streaming threads (see FeedRefDisplay()).
82 1811 void SyncWithAttach() const { m_attached.load(std::memory_order_acquire); }
83 void ActivateFeed(uint64_t token);
84 // That feed has stopped: its frames are no longer shown (the last one
85 // stays on screen). No-op if another feed has been attached since.
86 void DetachFeed(uint64_t token);
87
88 // Close every display now (fppd shutdown).
89 static void ShutdownAll();
90
91 ~VideoDisplay();
92
93 private:
94 explicit VideoDisplay(const Config& cfg);
95
96 bool Build();
97 // Takes the pipeline down and gives back the lease and the connector.
98 // Called with m_lock not held.
99 void Teardown(const char* why);
100 void Push(uint64_t token, GstSample* sample);
101
102 static GstFlowReturn OnFeedSample(GstElement* appsink, gpointer userData);
103 static GstPadProbeReturn OnFeedQuery(GstPad* pad, GstPadProbeInfo* info, gpointer userData);
104 // Answer a feed's ALLOCATION query as the kmssink would.
105 bool AnswerAllocation(GstQuery* query);
106 static GstBusSyncReply OnBusMessage(GstBus* bus, GstMessage* msg, gpointer userData);
107 static void WorkerLoop();
108
109 const Config m_cfg;
110 std::mutex m_lock;
111 // Bumped (release) as each feed is attached, and read (acquire) by its
112 // probe before it first touches the display: GStreamer hands the probe
113 // over under its own locks, which the sanitizer can't see.
114 std::atomic<uint64_t> m_attached{ 0 };
115 // Held across a frame's feed check, caps change and push (Push()), so
116 // that one feed's frame can't land, under its caps or the other's, after
117 // the next feed's first. Taken before m_lock; the push never blocks.
118 std::mutex m_pushLock;
119 int m_users = 0;
120 uint64_t m_idleSinceMs = 0; // when m_users last fell to 0 (GetMonotonicTimeMS())
121 bool m_failed = false; // the sink errored: rebuild on the next Acquire()
122 bool m_closed = false; // Teardown() has run
123 // Build() is still running: a second Acquire() of the display waits for
124 // it (m_builtCv) rather than attach a feed with no caps to keep to.
125 bool m_building = true;
126 std::condition_variable m_builtCv;
127 // Torn down (it failed) while tracks still held it: the lease, which a
128 // decoder's buffers from the display's pool may still be on, is closed
129 // when the last of them lets go (Release()).
130 bool m_releaseLeaseWhenUnused = false;
131 void ReleaseLeaseAndConnector();
132 uint64_t m_feedToken = 0; // the feed allowed to show frames, 0 = none
133 uint64_t m_lastToken = 0;
134
135 GstElement* m_pipeline = nullptr;
136 GstElement* m_src = nullptr; // appsrc (ref held)
137 GstCaps* m_feedCaps = nullptr; // what the sink accepts, for each feed
138 GstCaps* m_srcCaps = nullptr; // caps last set on m_src
139 DrmCards::Lease m_lease;
140 bool m_claimedConnector = false;
141 uint64_t m_framesShown = 0;
142 };
143
144 #endif
145