QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
VideoStreamResolver.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <climits>
5
6#include <QtCore/QUrl>
7
8#include "Fact.h"
10#include "VideoDecoderOptions.h"
11#include "VideoSettings.h"
12#include "VideoStreamUri.h"
13
14namespace {
15
16QString addressWithScheme(QLatin1StringView scheme, const QString& address)
17{
18 return address.isEmpty() ? QString() : (scheme + address);
19}
20
24QString listenUri(QLatin1StringView scheme, const QString& advertised)
25{
26 bool isNumber = false;
27 const int port = advertised.toInt(&isNumber);
28 if (isNumber) {
29 return VideoStreamUri::isValidPort(port) ? QStringLiteral("%1://0.0.0.0:%2").arg(scheme).arg(port) : advertised;
30 }
31
32 QUrl url(advertised);
33 const std::optional<VideoStreamUri::Transport> transport = VideoStreamUri::transportFor(url);
34 if (!transport || !VideoStreamUri::listens(*transport) || !VideoStreamUri::isValidPort(url.port())) {
35 return advertised;
36 }
37 url.setScheme(scheme);
38 if (url.host().isEmpty()) {
39 url.setHost(QStringLiteral("0.0.0.0"));
40 }
41 return url.toString();
42}
43
46QLatin1StringView rtpUdpScheme(const QGCVideoStreamInfo& streamInfo)
47{
48 const bool namedH265 = (streamInfo.encoding() == VIDEO_STREAM_ENCODING_UNKNOWN) &&
49 streamInfo.uri().startsWith(QLatin1StringView("udp265://"), Qt::CaseInsensitive);
50 return ((streamInfo.encoding() == VIDEO_STREAM_ENCODING_H265) || namedH265) ? QLatin1StringView("udp265")
51 : QLatin1StringView("udp");
52}
53
54QString autoStreamUri(const QGCVideoStreamInfo& streamInfo)
55{
56 const QString uri = streamInfo.uri();
57 switch (streamInfo.type()) {
58 case VIDEO_STREAM_TYPE_RTPUDP:
59 return listenUri(rtpUdpScheme(streamInfo), uri);
60 case VIDEO_STREAM_TYPE_MPEG_TS:
61 return listenUri(QLatin1StringView("mpegts"), uri);
62 case VIDEO_STREAM_TYPE_RTSP:
63 case VIDEO_STREAM_TYPE_TCP_MPEG:
64 default:
65 return uri;
66 }
67}
68
70struct ManualSource
71{
72 const char* videoSource;
74 const char* uri;
75 QString VideoStreamResolver::Settings::* url;
76};
77
78constexpr ManualSource kManualSources[] = {
84 {VideoSettings::videoSource3DRSolo, "udp://0.0.0.0:5600", nullptr},
85 {VideoSettings::videoSourceParrotDiscovery, "udp://0.0.0.0:8888", nullptr},
86 {VideoSettings::videoSourceYuneecMantisG, "rtsp://192.168.42.1:554/live", nullptr},
87 {VideoSettings::videoSourceHerelinkAirUnit, "rtsp://192.168.0.10:8554/H264Video", nullptr},
88 {VideoSettings::videoSourceHerelinkHotspot, "rtsp://192.168.43.1:8554/fpv_stream", nullptr},
89};
90
91const ManualSource* findManualSource(const QString& videoSource)
92{
93 for (const ManualSource& source : kManualSources) {
94 if (videoSource == QLatin1StringView(source.videoSource)) {
95 return &source;
96 }
97 }
98 return nullptr;
99}
100
101QString manualUri(const VideoStreamResolver::Settings& settings)
102{
103 const ManualSource* const source = findManualSource(settings.videoSource);
104 if (!source) {
105 return QString();
106 }
107 const QLatin1StringView uri(source->uri);
108 return source->url ? addressWithScheme(uri, settings.*(source->url)) : QString(uri);
109}
110
111} // namespace
112
114{
115 Settings settings;
116 settings.videoSource = videoSettings->videoSource()->rawValue().toString();
117 settings.udpUrl = videoSettings->udpUrl()->rawValue().toString();
118 settings.tcpUrl = videoSettings->tcpUrl()->rawValue().toString();
119 settings.rtspUrl = videoSettings->rtspUrl()->rawValue().toString();
120 settings.rtspTimeoutSec = videoSettings->rtspTimeout()->rawValue().toUInt();
121 settings.lowLatency = videoSettings->lowLatencyMode()->rawValue().toBool();
122 settings.rtpJitterLatencyMs = static_cast<int>(
123 std::min(videoSettings->rtpJitterLatencyMs()->rawValue().toUInt(), static_cast<uint>(INT_MAX)));
124 settings.autoReconnect = videoSettings->rtspAutoReconnect()->rawValue().toBool();
125 settings.conversionElement = videoSettings->videoConversionElement()->rawValue().toString().toUtf8();
126 settings.disablePixelAspectRatio = videoSettings->disablePixelAspectRatio()->rawValue().toBool();
127 settings.forceCpuVideoPath = videoSettings->forceCpuVideoPath()->rawValue().toBool();
128 settings.forceVideoDecoder = videoDecoderOptionFromSetting(videoSettings->forceVideoDecoder()->rawValue().toInt());
129 return settings;
130}
131
133{
134 return {
135 .conversionElement = settings.conversionElement,
136 .disablePixelAspectRatio = settings.disablePixelAspectRatio,
137 .gpuZeroCopy = !settings.forceCpuVideoPath && (settings.forceVideoDecoder != VideoDecoderOption::Software),
138 };
139}
140
141std::optional<VideoReceiver::Config> VideoStreamResolver::resolve(const Settings& settings,
142 const QGCVideoStreamInfo* streamInfo, Role role)
143{
144 QString uri;
145 if (isAutoStream(streamInfo)) {
146 uri = autoStreamUri(*streamInfo);
147 } else if (role == Role::Primary) {
148 uri = manualUri(settings);
149 }
150 if (uri.isEmpty()) {
151 return std::nullopt;
152 }
153
155 config.uri = uri;
156 // Only RTSP's stall timeout is a setting: the other streams keep the receiver's default.
158 config.timeout = std::chrono::seconds(settings.rtspTimeoutSec);
159 }
160 config.lowLatency = settings.lowLatency;
161 config.rtpJitterLatencyMs = settings.rtpJitterLatencyMs;
162 config.autoReconnect = settings.autoReconnect;
163 return config;
164}
165
167{
168 return streamInfo && !streamInfo->uri().isEmpty();
169}
170
171bool VideoStreamResolver::isNetworkSource(const QString& videoSource)
172{
173 return findManualSource(videoSource) != nullptr;
174}
175
176QString VideoStreamResolver::transportName(const QString& uri)
177{
179
180 const QUrl url(uri);
181 const std::optional<Transport> transport = VideoStreamUri::transportFor(url);
182 if (!transport) {
183 return url.scheme().toUpper();
184 }
185 switch (*transport) {
186 case Transport::Rtsp:
187 return QStringLiteral("RTSP");
188 case Transport::RtpUdpH264:
189 return QStringLiteral("RTP/UDP (H.264)");
190 case Transport::RtpUdpH265:
191 return QStringLiteral("RTP/UDP (H.265)");
192 case Transport::MpegTsUdp:
193 return QStringLiteral("MPEG-TS/UDP");
194 case Transport::MpegTsTcp:
195 return QStringLiteral("MPEG-TS/TCP");
196 }
197 return QString();
198}
constexpr VideoDecoderOption videoDecoderOptionFromSetting(int value)
The option a stored forceVideoDecoder value names: the default for a value that names none.
Encapsulates the contents of a VIDEO_STREAM_INFORMATION message.
quint8 encoding() const
static constexpr const char * videoSource3DRSolo
static constexpr const char * videoSourceParrotDiscovery
static constexpr const char * videoSourceTCP
static constexpr const char * videoSourceUDPH264
static constexpr const char * videoSourceHerelinkHotspot
static constexpr const char * videoSourceRTSP
static constexpr const char * videoSourceYuneecMantisG
static constexpr const char * videoSourceUDPH265
static constexpr const char * videoSourceMPEGTS
static constexpr const char * videoSourceHerelinkAirUnit
GPSCommandSequence port(QByteArrayView port, unsigned baud)
Clears the SBF output of port, keeps a serial port at the link's baud, and selects SBF and the WGS84 ...
Definition SBFPlan.cc:34
Settings snapshot(VideoSettings *videoSettings)
bool isNetworkSource(const QString &videoSource)
True for the manual sources that are network streams (not "disabled", not a local camera).
VideoReceiver::DisplayConfig displayConfig(const Settings &settings)
The display sink settings: GPU zero-copy unless the CPU path or a software decoder is forced.
Role
What a video source, each with a VideoStreamController, is. VideoSourceRole.Primary,...
@ Primary
The stream to display: a vehicle's current camera's current one, else the one the settings configure.
QString transportName(const QString &uri)
bool isAutoStream(const QGCVideoStreamInfo *streamInfo)
True when streamInfo advertises a URI, which then takes precedence over the manual settings.
std::optional< VideoReceiver::Config > resolve(const Settings &settings, const QGCVideoStreamInfo *streamInfo, Role role)
@ Rtsp
Any rtsp* scheme: rtsp://, rtsps://, rtspt://, ...
constexpr bool isValidPort(int port)
A TCP or UDP port a source can name: 1 to 65535.
constexpr bool listens(Transport transport)
The receiver listens on a local UDP port for transport, rather than connect to a server.
std::optional< Transport > transportFor(const QUrl &url)
The transport url's scheme selects, case-insensitively; nullopt for any other scheme.
std::chrono::seconds timeout
bool autoReconnect
Reconnect with backoff after a failure. Applies at once; changing any other field restarts the stream...
How the display sink presents frames. Construct-only: a change applies when restart() rebuilds the si...
The video settings resolve() reads, and what they depend on.