From 807418894bb14f698d2f6f26e19eb61c2ea093a9 Mon Sep 17 00:00:00 2001 From: Aaron Boxer Date: Fri, 6 Dec 2019 08:47:14 -0500 Subject: [PATCH] rtspurl: add API method to create request uri combined with control url code logic very similar to gst_rtsp_url_get_request_uri () --- gst-libs/gst/rtsp/gstrtspurl.c | 60 +++++++++++++++++++++++++++++++++++++++--- gst-libs/gst/rtsp/gstrtspurl.h | 4 +++ tests/check/libs/rtsp.c | 34 ++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 4 deletions(-) diff --git a/gst-libs/gst/rtsp/gstrtspurl.c b/gst-libs/gst/rtsp/gstrtspurl.c index dfb76df..6a7dd11 100644 --- a/gst-libs/gst/rtsp/gstrtspurl.c +++ b/gst-libs/gst/rtsp/gstrtspurl.c @@ -319,25 +319,77 @@ gst_rtsp_url_get_port (const GstRTSPUrl * url, guint16 * port) gchar * gst_rtsp_url_get_request_uri (const GstRTSPUrl * url) { + + return gst_rtsp_url_get_request_uri_with_control (url, NULL); +} + +/** + * gst_rtsp_url_get_request_uri_with_control: + * @url: a #GstRTSPUrl + * @control_path: an RTSP aggregate control path + * + * Get a newly allocated string describing the request URI for @url + * combined with the control path for @control_path + * + * Returns: a string with the request URI combined with the control path. + * g_free() after usage. + * + * Since 1.18 + */ +gchar * +gst_rtsp_url_get_request_uri_with_control (const GstRTSPUrl * url, + const gchar * control_path) +{ + gchar *uri; const gchar *pre_host; const gchar *post_host; const gchar *pre_query; const gchar *query; + gboolean has_slash; + const gchar *slash; + const gchar *actual_control_path = NULL; g_return_val_if_fail (url != NULL, NULL); + has_slash = g_str_has_suffix (url->abspath, "/"); + + if (control_path && strlen (control_path) > 0) { + gboolean control_has_slash; + + /* treat wild card as empty control path */ + if (g_strcmp0 (control_path, "*") == 0) + control_path = ""; + control_has_slash = g_str_has_prefix (control_path, "/"); + actual_control_path = control_path; + if (has_slash && control_has_slash) { + if (strlen (control_path) == 1) { + actual_control_path = NULL; + } else { + actual_control_path = control_path + 1; + } + } else { + has_slash = has_slash || control_has_slash; + } + } + slash = (!has_slash && (actual_control_path != NULL)) ? "/" : ""; + if (!actual_control_path) + actual_control_path = ""; + pre_host = url->family == GST_RTSP_FAM_INET6 ? "[" : ""; post_host = url->family == GST_RTSP_FAM_INET6 ? "]" : ""; pre_query = url->query ? "?" : ""; query = url->query ? url->query : ""; if (url->port != 0) { - uri = g_strdup_printf ("rtsp://%s%s%s:%u%s%s%s", pre_host, url->host, - post_host, url->port, url->abspath, pre_query, query); + uri = + g_strdup_printf ("rtsp://%s%s%s:%u%s%s%s%s%s", pre_host, + url->host, post_host, url->port, url->abspath, + slash, actual_control_path, pre_query, query); } else { - uri = g_strdup_printf ("rtsp://%s%s%s%s%s%s", pre_host, url->host, - post_host, url->abspath, pre_query, query); + uri = + g_strdup_printf ("rtsp://%s%s%s%s%s%s%s%s", pre_host, url->host, + post_host, url->abspath, slash, actual_control_path, pre_query, query); } return uri; diff --git a/gst-libs/gst/rtsp/gstrtspurl.h b/gst-libs/gst/rtsp/gstrtspurl.h index f493fa4..8e95a15 100644 --- a/gst-libs/gst/rtsp/gstrtspurl.h +++ b/gst-libs/gst/rtsp/gstrtspurl.h @@ -102,6 +102,10 @@ GST_RTSP_API gchar* gst_rtsp_url_get_request_uri (const GstRTSPUrl *url); GST_RTSP_API +gchar * gst_rtsp_url_get_request_uri_with_control (const GstRTSPUrl * url, + const gchar * control_path); + +GST_RTSP_API gchar** gst_rtsp_url_decode_path_components (const GstRTSPUrl *url); GST_RTSP_API diff --git a/tests/check/libs/rtsp.c b/tests/check/libs/rtsp.c index 733d74b..eb9543a 100644 --- a/tests/check/libs/rtsp.c +++ b/tests/check/libs/rtsp.c @@ -51,6 +51,39 @@ GST_START_TEST (test_rtsp_url_basic) GST_END_TEST; +GST_START_TEST (test_rtsp_url_query) +{ + GstRTSPUrl *url = NULL; + GstRTSPResult res; + gchar *uri; + const gchar *original_uri = "rtsp://localhost/foo/bar/?baz=fooo"; + const gchar *original_uri_with_control = + "rtsp://localhost/foo/bar/video/stream1?baz=fooo"; + + res = gst_rtsp_url_parse (original_uri, &url); + fail_unless (res == GST_RTSP_OK); + fail_unless (url != NULL); + fail_unless (url->transports & GST_RTSP_LOWER_TRANS_TCP); + fail_unless (url->transports & GST_RTSP_LOWER_TRANS_UDP); + fail_unless (url->transports & GST_RTSP_LOWER_TRANS_UDP_MCAST); + fail_unless (url->family == GST_RTSP_FAM_INET); + fail_unless (!url->user); + fail_unless (!url->passwd); + fail_unless (!strcmp (url->host, "localhost")); + fail_unless (!strcmp (url->abspath, "/foo/bar/")); + fail_unless (!strcmp (url->query, "baz=fooo")); + uri = gst_rtsp_url_get_request_uri (url); + fail_unless (!strcmp (uri, original_uri)); + g_free (uri); + uri = gst_rtsp_url_get_request_uri_with_control (url, "/video/stream1"); + fail_unless (!strcmp (uri, original_uri_with_control)); + g_free (uri); + + gst_rtsp_url_free (url); +} + +GST_END_TEST; + GST_START_TEST (test_rtsp_url_components_1) { GstRTSPUrl *url = NULL; @@ -975,6 +1008,7 @@ rtsp_suite (void) suite_add_tcase (s, tc_chain); tcase_add_test (tc_chain, test_rtsp_url_basic); + tcase_add_test (tc_chain, test_rtsp_url_query); tcase_add_test (tc_chain, test_rtsp_url_components_1); tcase_add_test (tc_chain, test_rtsp_url_components_2); tcase_add_test (tc_chain, test_rtsp_url_components_3); -- 2.7.4