QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstQgcFrameDelivery.cc
Go to the documentation of this file.
2
3#include <utility>
4
5#include <QtCore/QMetaObject>
6#include <QtCore/QMutexLocker>
7#include <QtMultimedia/QVideoFrame>
8#include <QtMultimedia/QVideoSink>
9
10namespace GstQgc {
11
13{
14 QMutex mutex;
15 QVideoFrame frame;
17 bool posted = false;
18
19 QVideoFrame take()
20 {
21 QMutexLocker locker(&mutex);
22 return std::exchange(frame, QVideoFrame());
23 }
24};
25
27 : _slot(std::make_shared<Slot>())
28{}
29
31
32QVideoSink* FrameDelivery::sink() const
33{
34 const QMutexLocker locker(&_mutex);
35 return _sink;
36}
37
39{
40 // Released after the lock: it may hand its decoder buffer back to a pool.
41 QVideoFrame undelivered;
42 const QMutexLocker locker(&_mutex);
43 if (sink != _sink) {
44 undelivered = _slot->take();
45 _slot = std::make_shared<Slot>();
46 }
47 _sink = sink;
48}
49
51{
52 QVideoFrame superseded;
53 const QMutexLocker locker(&_mutex);
54 QVideoSink* const sink = _sink;
55 if (!sink) {
56 return {};
57 }
58 const std::shared_ptr<Slot> slot = _slot;
59 bool needPost = false;
60 {
61 QMutexLocker slotLocker(&slot->mutex);
62 superseded = std::exchange(slot->frame, std::move(frame));
63 needPost = !std::exchange(slot->posted, true);
64 }
65 if (needPost) {
66 QMetaObject::invokeMethod(
67 sink,
68 [sink, slot]() {
69 QVideoFrame latest;
70 {
71 QMutexLocker slotLocker(&slot->mutex);
72 latest = std::exchange(slot->frame, QVideoFrame());
73 slot->posted = false;
74 }
75 if (latest.isValid()) {
76 sink->setVideoFrame(latest);
77 }
78 },
79 Qt::QueuedConnection);
80 }
81 return {.delivered = true, .superseded = superseded.isValid()};
82}
83
85{
86 QVideoFrame dropped;
87 const QMutexLocker locker(&_mutex);
88 dropped = _slot->take();
89}
90
91} // namespace GstQgc
void setSink(QVideoSink *sink)
QVideoSink * sink() const
The QVideoSink the frames go to: caller-owned, never dereferenced here but to queue the hand-off.
Result deliver(QVideoFrame &&frame)
Hands frame over as the newest.
void dropPending()
Drops an undelivered frame, releasing the decoder buffer it pins.
GstVideoSink that pushes decoded frames into a Qt QVideoSink; sole sink in qgcvideosinkbin....
bool posted
An invoke is queued to the QVideoSink.