permissions: more bindings-friendly API
authorMathieu Duponchelle <mathieu@centricular.com>
Fri, 2 Mar 2018 00:36:23 +0000 (01:36 +0100)
committerMathieu Duponchelle <mathieu@centricular.com>
Fri, 2 Mar 2018 15:21:37 +0000 (16:21 +0100)
https://bugzilla.gnome.org/show_bug.cgi?id=793975

docs/libs/gst-rtsp-server-sections.txt
gst/rtsp-server/rtsp-media-factory.c
gst/rtsp-server/rtsp-media-factory.h
gst/rtsp-server/rtsp-permissions.c
gst/rtsp-server/rtsp-permissions.h
tests/check/gst/permissions.c

index 86e3f25..103cd4a 100644 (file)
@@ -248,6 +248,7 @@ gst_rtsp_media_factory_set_launch
 gst_rtsp_media_factory_get_permissions
 gst_rtsp_media_factory_set_permissions
 gst_rtsp_media_factory_add_role
+gst_rtsp_media_factory_add_role_from_structure
 
 gst_rtsp_media_factory_set_shared
 gst_rtsp_media_factory_is_shared
@@ -361,6 +362,7 @@ gst_rtsp_permissions_unref
 gst_rtsp_permissions_add_role
 gst_rtsp_permissions_add_role_valist
 gst_rtsp_permissions_add_role_empty
+gst_rtsp_permissions_add_role_from_structure
 gst_rtsp_permissions_add_permission_for_role
 gst_rtsp_permissions_remove_role
 gst_rtsp_permissions_get_role
index 9093847..b7ba295 100644 (file)
@@ -495,6 +495,31 @@ gst_rtsp_media_factory_add_role (GstRTSPMediaFactory * factory,
 }
 
 /**
+ * gst_rtsp_media_factory_add_role_from_structure:
+ *
+ * A convenience wrapper around gst_rtsp_permissions_add_role_from_structure().
+ * If @factory had no permissions, new permissions will be created and the
+ * role will be added to it.
+ */
+void
+gst_rtsp_media_factory_add_role_from_structure (GstRTSPMediaFactory * factory,
+    GstStructure * structure)
+{
+  GstRTSPMediaFactoryPrivate *priv;
+  g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY (factory));
+  g_return_if_fail (GST_IS_STRUCTURE (structure));
+
+  priv = factory->priv;
+
+  GST_RTSP_MEDIA_FACTORY_LOCK (factory);
+  if (priv->permissions == NULL)
+    priv->permissions = gst_rtsp_permissions_new ();
+
+  gst_rtsp_permissions_add_role_from_structure (priv->permissions, structure);
+  GST_RTSP_MEDIA_FACTORY_UNLOCK (factory);
+}
+
+/**
  * gst_rtsp_media_factory_set_launch:
  * @factory: a #GstRTSPMediaFactory
  * @launch: the launch description
index 57c8031..c76a240 100644 (file)
@@ -127,6 +127,9 @@ void                  gst_rtsp_media_factory_add_role         (GstRTSPMediaFacto
                                                                const gchar *fieldname, ...);
 
 GST_EXPORT
+void                  gst_rtsp_media_factory_add_role_from_structure (GstRTSPMediaFactory * factory,
+                                                               GstStructure *structure);
+GST_EXPORT
 void                  gst_rtsp_media_factory_set_shared       (GstRTSPMediaFactory *factory,
                                                                gboolean shared);
 
index 34cf42a..9575048 100644 (file)
@@ -105,6 +105,28 @@ gst_rtsp_permissions_init (GstRTSPPermissionsImpl * permissions)
       g_ptr_array_new_with_free_func ((GDestroyNotify) free_structure);
 }
 
+static void
+add_role_from_structure (GstRTSPPermissionsImpl * impl,
+    GstStructure * structure)
+{
+  guint i, len;
+  const gchar *role = gst_structure_get_name (structure);
+
+  len = impl->roles->len;
+  for (i = 0; i < len; i++) {
+    GstStructure *entry = g_ptr_array_index (impl->roles, i);
+
+    if (gst_structure_has_name (entry, role)) {
+      g_ptr_array_remove_index_fast (impl->roles, i);
+      break;
+    }
+  }
+
+  gst_structure_set_parent_refcount (structure,
+      &impl->permissions.mini_object.refcount);
+  g_ptr_array_add (impl->roles, structure);
+}
+
 /**
  * gst_rtsp_permissions_new:
  *
@@ -214,7 +236,6 @@ gst_rtsp_permissions_add_role_valist (GstRTSPPermissions * permissions,
 {
   GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
   GstStructure *structure;
-  guint i, len;
 
   g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
   g_return_if_fail (gst_mini_object_is_writable (&permissions->mini_object));
@@ -223,19 +244,27 @@ gst_rtsp_permissions_add_role_valist (GstRTSPPermissions * permissions,
   structure = gst_structure_new_valist (role, fieldname, var_args);
   g_return_if_fail (structure != NULL);
 
-  len = impl->roles->len;
-  for (i = 0; i < len; i++) {
-    GstStructure *entry = g_ptr_array_index (impl->roles, i);
+  add_role_from_structure (impl, structure);
+}
 
-    if (gst_structure_has_name (entry, role)) {
-      g_ptr_array_remove_index_fast (impl->roles, i);
-      break;
-    }
-  }
+/**
+ * gst_rtsp_permissions_add_role_from_structure:
+ *
+ * Add a new role to @permissions based on @structure
+ */
+void
+gst_rtsp_permissions_add_role_from_structure (GstRTSPPermissions * permissions,
+    GstStructure * structure)
+{
+  GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
+  GstStructure *copy;
 
-  gst_structure_set_parent_refcount (structure,
-      &impl->permissions.mini_object.refcount);
-  g_ptr_array_add (impl->roles, structure);
+  g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
+  g_return_if_fail (GST_IS_STRUCTURE (structure));
+
+  copy = gst_structure_copy (structure);
+
+  add_role_from_structure (impl, copy);
 }
 
 /**
index 4bdff46..792e397 100644 (file)
@@ -91,6 +91,9 @@ void                  gst_rtsp_permissions_add_role_empty  (GstRTSPPermissions *
                                                             const gchar * role);
 
 GST_EXPORT
+void                  gst_rtsp_permissions_add_role_from_structure (GstRTSPPermissions * permissions,
+                                                            GstStructure *structure);
+GST_EXPORT
 void                  gst_rtsp_permissions_add_permission_for_role (GstRTSPPermissions * permissions,
                                                             const gchar * role,
                                                             const gchar * permission,
index 8083b1d..6f5ccf0 100644 (file)
@@ -25,6 +25,7 @@ GST_START_TEST (test_permissions)
 {
   GstRTSPPermissions *perms;
   GstRTSPPermissions *copy;
+  GstStructure *role_structure;
 
   perms = gst_rtsp_permissions_new ();
   fail_if (gst_rtsp_permissions_is_allowed (perms, "missing", "permission1"));
@@ -110,6 +111,14 @@ GST_START_TEST (test_permissions)
   gst_rtsp_permissions_add_role_empty (perms, "noone");
   fail_if (gst_rtsp_permissions_is_allowed (perms, "noone", "permission1"));
 
+  role_structure = gst_structure_new ("tester", "permission1", G_TYPE_BOOLEAN,
+      TRUE, NULL);
+  gst_rtsp_permissions_add_role_from_structure (perms, role_structure);
+  gst_structure_free (role_structure);
+  fail_unless (gst_rtsp_permissions_is_allowed (perms, "tester",
+          "permission1"));
+  fail_if (gst_rtsp_permissions_is_allowed (perms, "tester", "permission2"));
+
   gst_rtsp_permissions_unref (perms);
 }