rtspurl: add API method to create request uri combined with control url
authorAaron Boxer <aaron.boxer@collabora.com>
Fri, 6 Dec 2019 13:47:14 +0000 (08:47 -0500)
committerOlivier CrĂȘte <olivier.crete@ocrete.ca>
Fri, 27 Dec 2019 16:57:08 +0000 (16:57 +0000)
code logic very similar to gst_rtsp_url_get_request_uri ()

gst-libs/gst/rtsp/gstrtspurl.c
gst-libs/gst/rtsp/gstrtspurl.h
tests/check/libs/rtsp.c

index dfb76df..6a7dd11 100644 (file)
@@ -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;
index f493fa4..8e95a15 100644 (file)
@@ -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
index 733d74b..eb9543a 100644 (file)
@@ -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);