From e21242aba68c950df7ecec8aa26d700dbf2079a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Sat, 5 Aug 2023 17:27:31 +0100 Subject: [PATCH] rtspsink: use version template in user-agent property Avoids documentation churn when the version changes. Part-of: --- .../docs/plugins/gst_plugins_cache.json | 2 +- .../gst-rtsp-server/gst/glib-compat-private.h | 83 +++++++++++++++++++ .../gst/rtsp-sink/gstrtspclientsink.c | 14 +++- 3 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 subprojects/gst-rtsp-server/gst/glib-compat-private.h diff --git a/subprojects/gst-rtsp-server/docs/plugins/gst_plugins_cache.json b/subprojects/gst-rtsp-server/docs/plugins/gst_plugins_cache.json index 79c807b475..f1c82a497d 100644 --- a/subprojects/gst-rtsp-server/docs/plugins/gst_plugins_cache.json +++ b/subprojects/gst-rtsp-server/docs/plugins/gst_plugins_cache.json @@ -333,7 +333,7 @@ "construct": false, "construct-only": false, "controllable": false, - "default": "GStreamer/1.23.0.1", + "default": "GStreamer/{VERSION}", "mutable": "null", "readable": true, "type": "gchararray", diff --git a/subprojects/gst-rtsp-server/gst/glib-compat-private.h b/subprojects/gst-rtsp-server/gst/glib-compat-private.h new file mode 100644 index 0000000000..3d8885100c --- /dev/null +++ b/subprojects/gst-rtsp-server/gst/glib-compat-private.h @@ -0,0 +1,83 @@ +/* + * glib-compat.c + * Functions copied from glib 2.10 + * + * Copyright 2005 David Schleef + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __GLIB_COMPAT_PRIVATE_H__ +#define __GLIB_COMPAT_PRIVATE_H__ + +#include + +G_BEGIN_DECLS + +/* copies */ + +#if !GLIB_CHECK_VERSION(2,68,0) +#ifndef g_string_replace +#define g_string_replace gst_g_string_replace +#endif + +static inline guint +gst_g_string_replace (GString *string, + const gchar *find, + const gchar *replace, + guint limit) +{ + gsize f_len, r_len, pos; + gchar *cur, *next; + guint n = 0; + + g_return_val_if_fail (string != NULL, 0); + g_return_val_if_fail (find != NULL, 0); + g_return_val_if_fail (replace != NULL, 0); + + f_len = strlen (find); + r_len = strlen (replace); + cur = string->str; + + while ((next = strstr (cur, find)) != NULL) + { + pos = next - string->str; + g_string_erase (string, pos, f_len); + g_string_insert (string, pos, replace); + cur = string->str + pos + r_len; + n++; + /* Only match the empty string once at any given position, to + * avoid infinite loops */ + if (f_len == 0) + { + if (cur[0] == '\0') + break; + else + cur++; + } + if (n == limit) + break; + } + + return n; +} +#endif /* GLIB_CHECK_VERSION */ + +/* adaptations */ + +G_END_DECLS + +#endif diff --git a/subprojects/gst-rtsp-server/gst/rtsp-sink/gstrtspclientsink.c b/subprojects/gst-rtsp-server/gst/rtsp-sink/gstrtspclientsink.c index d2ce00d226..e60767d181 100644 --- a/subprojects/gst-rtsp-server/gst/rtsp-sink/gstrtspclientsink.c +++ b/subprojects/gst-rtsp-server/gst/rtsp-sink/gstrtspclientsink.c @@ -101,6 +101,8 @@ #include "gstrtspclientsink.h" +#include "../glib-compat-private.h" + typedef struct _GstRtspClientSinkPad GstRtspClientSinkPad; typedef GstGhostPadClass GstRtspClientSinkPadClass; @@ -301,7 +303,7 @@ gst_rtsp_client_sink_ntp_time_source_get_type (void) #define DEFAULT_TLS_DATABASE NULL #define DEFAULT_TLS_INTERACTION NULL #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP -#define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION +#define DEFAULT_USER_AGENT "GStreamer/{VERSION}" #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP #define DEFAULT_RTX_TIME_MS 500 #define DEFAULT_PUBLISH_CLOCK_MODE GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK @@ -2166,9 +2168,13 @@ gst_rtsp_client_sink_init_request (GstRTSPClientSink * sink, return res; /* set user-agent */ - if (sink->user_agent) - gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, - sink->user_agent); + if (sink->user_agent) { + GString *user_agent = g_string_new (sink->user_agent); + + g_string_replace (user_agent, "{VERSION}", PACKAGE_VERSION, 0); + gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, user_agent->str); + g_string_free (user_agent, TRUE); + } return res; }