QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
RecordingBranch.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <chrono>
5#include <optional>
6
7#include <QtCore/QElapsedTimer>
8#include <QtCore/QString>
9#include <QtCore/QTimer>
10
11#include "BranchContext.h"
12#include "GstScoped.h"
13#include "TeeBranchHead.h"
14#include "VideoReceiver.h"
15
33{
34public:
36 {
37 public:
39 virtual void onRecordingFileOpened(const QString& file) = 0;
41 virtual void onRecordingFileClosed(const QString& file, const QString& error) = 0;
44 virtual void onRecordingFailed(const QString& file, const QString& error, bool fileOpened) = 0;
48 virtual void onRecordingNeedsNewFile(const QString& file) = 0;
49
50 protected:
51 ~Listener() = default;
52 };
53
54 struct Request
55 {
56 QString file;
58
59 friend bool operator==(const Request&, const Request&) = default;
60 };
61
64 static constexpr std::chrono::seconds kFinalizeTimeout{3};
66 static constexpr guint64 kQueueMaxBytes = 64 * 1024 * 1024;
69 static constexpr std::chrono::seconds kStartTimeout{5};
72 static constexpr std::chrono::seconds kHeaderSpaceMargin{60};
75 static constexpr std::chrono::seconds kHeaderEstimateDelay{10};
76
79 static GStreamer::GstElementPtr makeFileSink(const QString& file, VideoReceiver::FileFormat format,
80 const GstCaps* inputCaps);
81
82 RecordingBranch(const BranchContext& context, Listener& listener);
84
87
89 [[nodiscard]] bool build();
91 void reset();
92
95 void setRequest(const std::optional<Request>& request);
97 void onSourceStreaming();
98
101 bool beginTeardown();
102
103 bool finalizing() const { return _state == State::Finalizing; }
104
106 void completeTeardown();
107
110 void onBusError(GstMessage* message, const GError* error, const QString& text, const char* debug);
112 void onBusElement(GstMessage* message);
113
114 GstElement* valve() const { return _head.valve(); }
115
116private:
117 enum class State
118 {
119 Idle,
120 Recording,
121 Finalizing,
122 };
123
124 void _reconcile();
125 void _start(const Request& request);
128 void _switchAtKeyframe();
130 void _onSwitchKeyframe(quint64 hold);
132 void _releaseSwitchHold();
135 void _end(const QString& failure = QString());
138 void _finish(const QString& error, bool fileClosed = false);
141 bool _fromRecorder(GstObject* source) const;
142 void _onFileOpened(quint64 recording);
143 void _onOverrun(quint64 recording);
146 void _onCapsChanged(quint64 recording);
148 void _checkStarted();
150 void _checkHeaderSpace();
151 void _reportOpened();
152
153 static GstPadProbeReturn _keyframeProbe(GstPad* pad, GstPadProbeInfo* info, gpointer data);
154 static GstPadProbeReturn _startProbe(GstPad* pad, GstPadProbeInfo* info, gpointer data);
155 static GstPadProbeReturn _capsProbe(GstPad* pad, GstPadProbeInfo* info, gpointer data);
156 static GstPadProbeReturn _switchProbe(GstPad* pad, GstPadProbeInfo* info, gpointer data);
157 static void _onQueueOverrun(GstElement* queue, gpointer data);
158
159 const BranchContext _context;
160 Listener& _listener;
161 QTimer _finalizeTimer;
163 QTimer _watchTimer;
164
165 TeeBranchHead _head;
166
167 State _state = State::Idle;
168 bool _streaming = false;
169 std::optional<Request> _requested;
170 Request _current;
172 std::atomic<quint64> _recording = 0;
174 GStreamer::GstElementPtr _fileSink;
176 GStreamer::PadProbe _keyframeWatch;
179 GStreamer::PadProbe _startWatch;
181 GStreamer::PadProbe _capsWatch;
184 std::atomic<gulong> _capsHold = 0;
186 GStreamer::PadProbe _switchHold;
187 bool _switching = false;
189 quint64 _switchHoldId = 0;
191 std::atomic<gint64> _fedAt = 0;
193 std::atomic<bool> _fileStarted = false;
194 bool _openedReported = false;
196 QElapsedTimer _openedFor;
198 QString _failure;
199};
struct _GstElement GstElement
virtual void onRecordingFileClosed(const QString &file, const QString &error)=0
file is closed; error is empty when it was finalized cleanly.
virtual void onRecordingNeedsNewFile(const QString &file)=0
virtual void onRecordingFileOpened(const QString &file)=0
file got its first frame.
virtual void onRecordingFailed(const QString &file, const QString &error, bool fileOpened)=0
static constexpr std::chrono::seconds kFinalizeTimeout
static constexpr std::chrono::seconds kHeaderEstimateDelay
static constexpr guint64 kQueueMaxBytes
Backlog the recording queue holds for slow storage before the recording gives up.
void completeTeardown()
Closes a file whose finalization is still outstanding by flushing the recorder, and reports it.
void onBusError(GstMessage *message, const GError *error, const QString &text, const char *debug)
RecordingBranch(const BranchContext &context, Listener &listener)
void setRequest(const std::optional< Request > &request)
GstElement * valve() const
static constexpr std::chrono::seconds kHeaderSpaceMargin
void onBusElement(GstMessage *message)
A bus ELEMENT message: the current recorder's EOS, as the pipeline forwards it, means its file is clo...
bool finalizing() const
RecordingBranch & operator=(const RecordingBranch &)=delete
RecordingBranch(const RecordingBranch &)=delete
bool build()
Adds the branch's queue and closed valve behind the tee.
void onSourceStreaming()
The first frame reached the tee, so the input caps are known: a waiting request starts.
void reset()
Drops the recording, if any, without reports; the pipeline must be in NULL.
static GStreamer::GstElementPtr makeFileSink(const QString &file, VideoReceiver::FileFormat format, const GstCaps *inputCaps)
static constexpr std::chrono::seconds kStartTimeout
FileFormat
Values are persisted by VideoSettings::recordingFormat.
std::unique_ptr< GstElement, GstObjectDeleter > GstElementPtr
Definition GstScoped.h:60
VideoReceiver::FileFormat format
friend bool operator==(const Request &, const Request &)=default