From 5bbd8c2d71081b78499c570ff099485b3fab2cfe 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] rtspsrc: use version template in user-agent property Avoids documentation churn when the version changes. Part-of: --- .../docs/gst_plugins_cache.json | 2 +- .../gst-libs/gst/glib-compat-private.h | 47 +++++++++++++++++++ .../gst-plugins-good/gst/rtsp/gstrtspsrc.c | 16 +++++-- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/subprojects/gst-plugins-good/docs/gst_plugins_cache.json b/subprojects/gst-plugins-good/docs/gst_plugins_cache.json index 4321001f8b..c657c3d1bf 100644 --- a/subprojects/gst-plugins-good/docs/gst_plugins_cache.json +++ b/subprojects/gst-plugins-good/docs/gst_plugins_cache.json @@ -21628,7 +21628,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-plugins-good/gst-libs/gst/glib-compat-private.h b/subprojects/gst-plugins-good/gst-libs/gst/glib-compat-private.h index 8f37de205e..3d8885100c 100644 --- a/subprojects/gst-plugins-good/gst-libs/gst/glib-compat-private.h +++ b/subprojects/gst-plugins-good/gst-libs/gst/glib-compat-private.h @@ -29,6 +29,53 @@ 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 diff --git a/subprojects/gst-plugins-good/gst/rtsp/gstrtspsrc.c b/subprojects/gst-plugins-good/gst/rtsp/gstrtspsrc.c index 391a92d43b..2a6751d586 100644 --- a/subprojects/gst-plugins-good/gst/rtsp/gstrtspsrc.c +++ b/subprojects/gst-plugins-good/gst/rtsp/gstrtspsrc.c @@ -117,6 +117,8 @@ #include "gstrtspelements.h" #include "gstrtspsrc.h" +#include + GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug); #define GST_CAT_DEFAULT (rtspsrc_debug) @@ -299,7 +301,7 @@ gst_rtsp_backchannel_get_type (void) #define DEFAULT_TLS_INTERACTION NULL #define DEFAULT_DO_RETRANSMISSION TRUE #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP -#define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION +#define DEFAULT_USER_AGENT "GStreamer/{VERSION}" #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000 #define DEFAULT_RFC7273_SYNC FALSE #define DEFAULT_ADD_REFERENCE_TIMESTAMP_META FALSE @@ -895,6 +897,9 @@ gst_rtspsrc_class_init (GstRTSPSrcClass * klass) * * The string to set in the User-Agent header. * + * If the string contains `{VERSION}` that will be replaced with the + * GStreamer version at runtime (since GStreamer 1.24). + * * Since: 1.6 */ g_object_class_install_property (gobject_class, PROP_USER_AGENT, @@ -5467,8 +5472,13 @@ gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg, return res; /* set user-agent */ - if (src->user_agent) - gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent); + if (src->user_agent) { + GString *user_agent = g_string_new (src->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; }