3#ifdef QGC_GST_RTSP_SERVER
7#pragma push_macro("signals")
9#include <gst/rtsp-server/rtsp-server.h>
10#pragma pop_macro("signals")
24constexpr const char* kTestSource =
25 "videotestsrc is-live=true pattern=ball ! "
26 "video/x-raw,width=1280,height=720,framerate=30/1 ! "
27 "videoconvert ! video/x-raw,format=I420";
31constexpr const char* kH264InBandHeaders =
"h264parse config-interval=-1";
37constexpr const char* kOpenH264Encoder =
"openh264enc complexity=low rate-control=bitrate bitrate=2000000 gop-size=30";
38constexpr const char* kX264Encoder =
"x264enc tune=zerolatency speed-preset=ultrafast bitrate=2000 key-int-max=30";
41constexpr const char* kH265Encoder =
42 "x265enc tune=zerolatency speed-preset=ultrafast bitrate=2000 key-int-max=30 option-string=pools=none";
45const char* h264Encoder()
48 return kOpenH264Encoder;
61 if (!gst_is_initialized()) {
62 qCWarning(MockVideoStreamServerLog)
63 <<
"GStreamer not initialized; QGC's central GStreamer init must run before serving a mock stream";
81#ifndef QGC_GST_RTSP_SERVER
90 return h264Encoder() !=
nullptr;
109 qCWarning(MockVideoStreamServerLog) <<
"Requested stream type is not available in this GStreamer install";
114#ifdef QGC_GST_RTSP_SERVER
115 const int boundPort = _startRtsp(type, host, port);
116 if (boundPort <= 0) {
119 _servedUri = QStringLiteral(
"rtsp://%1:%2/test").arg(host, QString::number(boundPort));
120 qCDebug(MockVideoStreamServerLog) <<
"Serving" << _servedUri;
123 qCWarning(MockVideoStreamServerLog) <<
"RTSP requested but gst-rtsp-server is not available";
130 const QString portString = QString::number(port);
133 launch = QStringLiteral(
"%1 ! %2 ! rtph264pay config-interval=1 pt=96 ! udpsink host=%3 port=%4")
134 .arg(kTestSource, h264Encoder(), host, portString);
135 uri = QStringLiteral(
"udp://%1:%2").arg(host, portString);
138 launch = QStringLiteral(
"%1 ! %2 ! rtph265pay config-interval=1 pt=96 ! udpsink host=%3 port=%4")
139 .arg(kTestSource, kH265Encoder, host, portString);
140 uri = QStringLiteral(
"udp265://%1:%2").arg(host, portString);
143 launch = QStringLiteral(
"%1 ! %2 ! %3 ! mpegtsmux ! udpsink host=%4 port=%5")
144 .arg(kTestSource, h264Encoder(), kH264InBandHeaders, host, portString);
145 uri = QStringLiteral(
"mpegts://%1:%2").arg(host, portString);
148 launch = QStringLiteral(
"%1 ! %2 ! %3 ! mpegtsmux ! tcpserversink name=server host=%4 port=%5")
149 .arg(kTestSource, h264Encoder(), kH264InBandHeaders, host, portString);
150 uri = QStringLiteral(
"tcp://%1:%2").arg(host, portString);
157 if (!_startPipeline(launch)) {
162 GstElement*
const server = gst_bin_get_by_name(GST_BIN(_pipeline),
"server");
164 g_object_get(server,
"current-port", &boundPort,
nullptr);
165 gst_object_unref(server);
166 uri = QStringLiteral(
"tcp://%1:%2").arg(host, QString::number(boundPort));
170 qCDebug(MockVideoStreamServerLog) <<
"Serving" << _servedUri <<
"via" << launch;
174bool MockVideoStreamServer::_startPipeline(
const QString& launch)
176 GError* error =
nullptr;
177 GstElement* pipeline = gst_parse_launch(launch.toUtf8().constData(), &error);
179 qCWarning(MockVideoStreamServerLog) <<
"Failed to build pipeline:" << error->message;
182 gst_object_unref(pipeline);
187 if (gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
188 qCWarning(MockVideoStreamServerLog) <<
"Failed to set pipeline to PLAYING";
189 gst_object_unref(pipeline);
193 _pipeline = pipeline;
200 gst_element_set_state(_pipeline, GST_STATE_NULL);
201 gst_object_unref(_pipeline);
205#ifdef QGC_GST_RTSP_SERVER
212#ifdef QGC_GST_RTSP_SERVER
213int MockVideoStreamServer::_startRtsp(StreamType type,
const QString& host, quint16 port)
215 _rtspContext = g_main_context_new();
217 GstRTSPServer* server = gst_rtsp_server_new();
218 const QByteArray address = host.toUtf8();
219 const QByteArray service = QByteArray::number(port);
220 gst_rtsp_server_set_address(server, address.constData());
221 gst_rtsp_server_set_service(server, service.constData());
223 GstRTSPMountPoints* mounts = gst_rtsp_server_get_mount_points(server);
224 GstRTSPMediaFactory* factory = gst_rtsp_media_factory_new();
225 const QString launch =
227 ? QStringLiteral(
"( %1 ! %2 ! rtph265pay name=pay0 pt=96 )").arg(kTestSource, kH265Encoder)
228 : QStringLiteral(
"( %1 ! %2 ! rtph264pay name=pay0 pt=96 )").arg(kTestSource, h264Encoder());
229 gst_rtsp_media_factory_set_launch(factory, launch.toUtf8().constData());
230 gst_rtsp_media_factory_set_shared(factory, TRUE);
231 gst_rtsp_mount_points_add_factory(mounts,
"/test", factory);
232 g_object_unref(mounts);
234 if (gst_rtsp_server_attach(server, _rtspContext) == 0) {
235 qCWarning(MockVideoStreamServerLog) <<
"Failed to attach RTSP server on port" << port;
236 gst_object_unref(server);
237 g_main_context_unref(_rtspContext);
238 _rtspContext =
nullptr;
241 const int boundPort = gst_rtsp_server_get_bound_port(server);
243 _rtspServer = server;
244 _rtspLoop = g_main_loop_new(_rtspContext, FALSE);
246 _rtspThread = std::thread([
this]() {
247 g_main_context_push_thread_default(_rtspContext);
248 g_main_loop_run(_rtspLoop);
249 g_main_context_pop_thread_default(_rtspContext);
255void MockVideoStreamServer::_stopRtsp()
260 g_main_context_invoke(
262 [](gpointer loop) -> gboolean {
263 g_main_loop_quit(
static_cast<GMainLoop*
>(loop));
264 return G_SOURCE_REMOVE;
268 if (_rtspThread.joinable()) {
272 g_main_loop_unref(_rtspLoop);
276 gst_object_unref(_rtspServer);
277 _rtspServer =
nullptr;
280 g_main_context_unref(_rtspContext);
281 _rtspContext =
nullptr;
struct _GstElement GstElement
#define QGC_LOGGING_CATEGORY(name, categoryStr)
@ MpegTsTcp
MPEG-TS over TCP -> tcp://.
@ RtspH265
RTSP (H.265) -> rtsp://.
@ RtpUdpH265
RTP/H.265 over UDP -> udp265://.
@ RtpUdpH264
RTP/H.264 over UDP -> udp://.
@ RtspH264
RTSP (H.264) -> rtsp://.
@ MpegTsUdp
MPEG-TS over UDP -> mpegts://.
void stop()
Stop the stream and release all resources. Safe to call when not running.
static bool isStreamTypeAvailable(StreamType type)
bool start(StreamType type, const QString &host, quint16 port)
bool hasElementFactory(const char *name)