QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstBusPump.cc
Go to the documentation of this file.
1#include "GstBusPump.h"
2
3#include <utility>
4
5#include <QtCore/QDeadlineTimer>
6#ifdef Q_OS_WIN
7#include <QtCore/QWinEventNotifier>
8#else
9#include <QtCore/QSocketNotifier>
10#endif
11
12#include "GstVideoReceiver.h"
13
14GstBusPump::GstBusPump(QObject* parent)
15 : QObject(parent)
16{}
17
22
23bool GstBusPump::attach(GstElement* pipeline, SyncHandler syncHandler, Handler handler)
24{
25 detach();
26 _bus = GStreamer::adoptBus(gst_pipeline_get_bus(GST_PIPELINE(pipeline)));
27 if (!_bus) {
28 qCCritical(GstVideoReceiverLog) << "gst_pipeline_get_bus() failed";
29 return false;
30 }
31 _syncHandler = std::move(syncHandler);
32 _handler = std::move(handler);
33
34 gst_bus_set_sync_handler(_bus.get(), _onSync, this,
35 [](gpointer pump) { static_cast<GstBusPump*>(pump)->_syncReleased.release(); });
36 _syncInstalled = true;
37
38 // Readable while messages are queued, like Qt's QGstBusObserver.
39 GPollFD pollFd{};
40 gst_bus_get_pollfd(_bus.get(), &pollFd);
41#ifdef Q_OS_WIN
42 auto* const notifier = new QWinEventNotifier(reinterpret_cast<Qt::HANDLE>(pollFd.fd), this);
43 (void) connect(notifier, &QWinEventNotifier::activated, this, &GstBusPump::drain);
44#else
45 auto* const notifier = new QSocketNotifier(pollFd.fd, QSocketNotifier::Read, this);
46 (void) connect(notifier, &QSocketNotifier::activated, this, &GstBusPump::drain);
47#endif
48 _notifier = notifier;
49 return true;
50}
51
53{
54 // Before the bus, which owns the fd it watches.
55 delete _notifier;
56 _notifier = nullptr;
57
58 if (!_bus) {
59 return;
60 }
61 if (_syncInstalled) {
62 gst_bus_set_sync_handler(_bus.get(), nullptr, nullptr, nullptr);
63 // GStreamer refcounts the handler per post and releases it once the last in-flight call has returned.
64 _syncReleased.acquire();
65 _syncInstalled = false;
66 }
67 // Drops what is still queued.
68 gst_bus_set_flushing(_bus.get(), TRUE);
69 _bus.reset();
70}
71
73{
74 while (_bus) {
75 const GStreamer::GstMessagePtr message = GStreamer::adoptMessage(gst_bus_pop(_bus.get()));
76 if (!message) {
77 return;
78 }
79 _handler(message.get());
80 }
81}
82
83void GstBusPump::pumpUntil(GstMessageType types, const std::function<bool()>& done, const QDeadlineTimer& deadline)
84{
85 while (_bus && !done() && !deadline.hasExpired()) {
87 gst_bus_timed_pop_filtered(_bus.get(), static_cast<GstClockTime>(deadline.remainingTimeNSecs()), types));
88 if (!message) {
89 return;
90 }
91 _handler(message.get());
92 }
93}
94
96{
97 return GStreamer::adoptMessage(_bus ? gst_bus_pop_filtered(_bus.get(), type) : nullptr);
98}
99
100GstBusSyncReply GstBusPump::_onSync(GstBus* /*bus*/, GstMessage* message, gpointer data)
101{
102 return static_cast<GstBusPump*>(data)->_syncHandler(message);
103}
struct _GstElement GstElement
A pipeline's bus, handled from the thread the pump lives on.
Definition GstBusPump.h:20
void pumpUntil(GstMessageType types, const std::function< bool()> &done, const QDeadlineTimer &deadline)
Definition GstBusPump.cc:83
void drain()
Handles the messages queued now, as the poll fd does.
Definition GstBusPump.cc:72
std::function< void(GstMessage *message)> Handler
The pump's thread.
Definition GstBusPump.h:27
GstBusPump(QObject *parent=nullptr)
Definition GstBusPump.cc:14
~GstBusPump() override
detach()es.
Definition GstBusPump.cc:18
bool attach(GstElement *pipeline, SyncHandler syncHandler, Handler handler)
Definition GstBusPump.cc:23
GStreamer::GstMessagePtr pop(GstMessageType type)
Definition GstBusPump.cc:95
void detach()
Idempotent.
Definition GstBusPump.cc:52
std::function< GstBusSyncReply(GstMessage *message)> SyncHandler
Posting threads. Returns GST_BUS_DROP for a message it took (and unreffed), GST_BUS_PASS to queue it.
Definition GstBusPump.h:25
GstBusPtr adoptBus(GstBus *bus) noexcept
Definition GstScoped.h:89
std::unique_ptr< GstMessage, GstMessageDeleter > GstMessagePtr
Definition GstScoped.h:67
GstMessagePtr adoptMessage(GstMessage *message) noexcept
Definition GstScoped.h:114