2 * Copyright (C) 2005 Stefan Kost <ensonic@users.sf.net>
4 * gstchildproxy.c: interface for multi child elements
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 * SECTION:gstchildproxy
23 * @short_description: Interface for multi child elements.
26 * This interface abstracts handling of property sets for child elements.
27 * Imagine elements such as mixers or polyphonic generators. They all have
28 * multiple #GstPads or some kind of voice objects. The element acts as a parent
29 * for those child objects. Each child has the same properties.
31 * By implementing this interface the child properties can be accessed from the
32 * parent element by using gst_child_proxy_get() and gst_child_proxy_set().
34 * Property names are written as "child-name::property-name". The whole naming
35 * scheme is recursive. Thus "child1::child2::property" is valid too, if
36 * "child1" also implements the #GstChildProxy interface.
39 #include "gst_private.h"
41 #include "gstchildproxy.h"
42 #include "gstmarshal.h"
43 #include <gobject/gvaluecollector.h>
53 static guint signals[LAST_SIGNAL] = { 0 };
56 * gst_child_proxy_get_child_by_name:
57 * @parent: the parent object to get the child from
58 * @name: the childs name
60 * Looks up a child element by the given name.
62 * Implementors can use #GstObject together with gst_object_get_name()
64 * Returns: the child object or %NULL if not found
67 gst_child_proxy_get_child_by_name (GstChildProxy * parent, const gchar * name)
71 const gchar *object_name;
73 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), NULL);
74 g_return_val_if_fail (name != NULL, NULL);
76 count = gst_child_proxy_get_children_count (parent);
77 for (i = 0; i < count; i++) {
78 object = gst_child_proxy_get_child_by_index (parent, i);
79 object_name = gst_object_get_name (object);
80 if (object_name == NULL) {
81 g_warning ("child %u of parent %s has no name", i,
82 GST_OBJECT_NAME (parent));
85 if (g_str_equal (object_name, name))
92 * gst_child_proxy_get_child_by_index:
93 * @parent: the parent object to get the child from
94 * @index: the childs position in the child list
96 * Fetches a child by its number.
98 * Returns: the child object or %NULL if not found (index too high)
101 gst_child_proxy_get_child_by_index (GstChildProxy * parent, guint index)
103 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), NULL);
105 return (GST_CHILD_PROXY_GET_INTERFACE (parent)->get_child_by_index (parent,
110 * gst_child_proxy_get_children_count:
111 * @parent: the parent object
113 * Gets the number of child objects this parent contains.
115 * Returns: the number of child objects
118 gst_child_proxy_get_children_count (GstChildProxy * parent)
120 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), 0);
122 return (GST_CHILD_PROXY_GET_INTERFACE (parent)->get_children_count (parent));
126 * gst_child_proxy_lookup:
127 * @object: object to lookup the property in
128 * @name: name of the property to look up
129 * @target: pointer to a #GstObject that takes the real object to set property on
130 * @pspec: pointer to take the #GParamSpec describing the property
132 * Looks up which object and #GParamSpec would be effected by the given @name.
134 * Returns: TRUE if @target and @pspec could be found. FALSE otherwise. In that
135 * case the values for @pspec and @target are not modified
138 gst_child_proxy_lookup (GstObject * object, const gchar * name,
139 GstObject ** target, GParamSpec ** pspec)
141 gboolean res = FALSE;
142 gchar **names, **current;
144 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
145 g_return_val_if_fail (name != NULL, FALSE);
147 current = names = g_strsplit (name, "::", -1);
149 if (!GST_IS_CHILD_PROXY (object)) {
151 ("object %s is not a parent, so you cannot request a child by name %s",
152 GST_OBJECT_NAME (object), current[0]);
156 gst_child_proxy_get_child_by_name (GST_CHILD_PROXY (object),
159 GST_INFO ("no such object %s", current[0]);
164 if (current[1] == NULL) {
166 g_object_class_find_property (G_OBJECT_GET_CLASS (object), current[0]);
168 GST_INFO ("no param spec named %s", current[0]);
182 * gst_child_proxy_get_property:
183 * @object: object to query
184 * @name: name of the property
185 * @value: an uninitialized #GValue that should take the result.
187 * Gets a single property using the GstChildProxy mechanism.
188 * You are responsible for for freeing it by calling g_value_unset()
191 gst_child_proxy_get_property (GstObject * object, const gchar * name,
197 g_return_if_fail (GST_IS_OBJECT (object));
198 g_return_if_fail (name != NULL);
199 g_return_if_fail (!G_IS_VALUE (value));
201 if (!gst_child_proxy_lookup (object, name, &target, &pspec)) {
202 g_warning ("cannot get property %s from object %s", name,
203 GST_OBJECT_NAME (object));
206 g_object_get_property (G_OBJECT (target), pspec->name, value);
210 * gst_child_proxy_get_valist:
211 * @object: the object to query
212 * @first_property_name: name of the first property to get
213 * @var_args: return location for the first property, followed optionally by more name/return location pairs, followed by NULL
215 * Gets properties of the parent object and its children.
218 gst_child_proxy_get_valist (GstObject * object,
219 const gchar * first_property_name, va_list var_args)
223 GValue value = { 0, };
225 g_return_if_fail (G_IS_OBJECT (object));
227 g_object_ref (object);
229 name = first_property_name;
231 /* iterate over pairs */
233 gst_child_proxy_get_property (object, name, &value);
234 G_VALUE_LCOPY (&value, var_args, 0, &error);
236 g_warning ("error copying value: %s", error);
239 g_value_unset (&value);
240 name = va_arg (var_args, gchar *);
243 g_object_unref (object);
247 * gst_child_proxy_get:
248 * @object: the parent object
249 * @first_property_name: name of the first property to get
250 * @...: return location for the first property, followed optionally by more name/return location pairs, followed by NULL
252 * Gets properties of the parent object and its children.
255 gst_child_proxy_get (GstObject * object, const gchar * first_property_name, ...)
259 g_return_if_fail (GST_IS_OBJECT (object));
261 va_start (var_args, first_property_name);
262 gst_child_proxy_get_valist (object, first_property_name, var_args);
267 * gst_child_proxy_set_property:
268 * @object: the parent object
269 * @name: name of the property to set
270 * @value: new #GValue for the property
272 * Sets a single property using the GstChildProxy mechanism.
275 gst_child_proxy_set_property (GstObject * object, const gchar * name,
276 const GValue * value)
281 g_return_if_fail (GST_IS_OBJECT (object));
282 g_return_if_fail (name != NULL);
283 g_return_if_fail (!G_IS_VALUE (value));
285 if (!gst_child_proxy_lookup (object, name, &target, &pspec)) {
286 g_warning ("cannot set property %s on object %s", name,
287 GST_OBJECT_NAME (object));
290 g_object_set_property (G_OBJECT (target), pspec->name, value);
294 * gst_child_proxy_set_valist:
295 * @object: the parent object
296 * @first_property_name: name of the first property to set
297 * @var_args: value for the first property, followed optionally by more name/value pairs, followed by NULL
299 * Sets properties of the parent object and its children.
302 gst_child_proxy_set_valist (GstObject * object,
303 const gchar * first_property_name, va_list var_args)
307 GValue value = { 0, };
309 g_return_if_fail (G_IS_OBJECT (object));
311 g_object_ref (object);
313 name = first_property_name;
315 /* iterate over pairs */
320 if (!gst_child_proxy_lookup (object, name, &target, &pspec)) {
321 g_warning ("no such property %s in object %s", name,
322 GST_OBJECT_NAME (object));
323 g_object_unref (object);
325 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
326 G_VALUE_COLLECT (&value, var_args, G_VALUE_NOCOPY_CONTENTS, &error);
328 g_warning ("error copying value: %s", error);
329 g_object_unref (object);
332 g_object_set_property (G_OBJECT (target), pspec->name, &value);
333 g_value_unset (&value);
334 name = va_arg (var_args, gchar *);
337 g_object_unref (object);
341 * gst_child_proxy_set:
342 * @object: the parent object
343 * @first_property_name: name of the first property to set
344 * @...: value for the first property, followed optionally by more name/value pairs, followed by NULL
346 * Sets properties of the parent object and its children.
349 gst_child_proxy_set (GstObject * object, const gchar * first_property_name, ...)
353 g_return_if_fail (GST_IS_OBJECT (object));
355 va_start (var_args, first_property_name);
356 gst_child_proxy_set_valist (object, first_property_name, var_args);
361 * gst_child_proxy_child_added:
362 * @object: the parent object
363 * @child: the newly added child
365 * Emits the "child-added" signal.
368 gst_child_proxy_child_added (GstObject * object, GstObject * child)
370 g_signal_emit (G_OBJECT (object), signals[CHILD_ADDED], 0, child);
374 * gst_child_proxy_child_removed:
375 * @object: the parent object
376 * @child: the newly added child
378 * Emits the "child-removed" signal.
381 gst_child_proxy_child_removed (GstObject * object, GstObject * child)
383 g_signal_emit (G_OBJECT (object), signals[CHILD_REMOVED], 0, child);
386 /* gobject methods */
389 gst_child_proxy_base_init (gpointer g_class)
391 static gboolean initialized = FALSE;
394 /* create interface signals and properties here. */
395 signals[CHILD_ADDED] =
396 g_signal_new ("child-added", G_TYPE_FROM_CLASS (g_class),
397 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstChildProxyInterface,
398 child_added), NULL, NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
401 signals[CHILD_REMOVED] =
402 g_signal_new ("child-removed", G_TYPE_FROM_CLASS (g_class),
403 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstChildProxyInterface,
404 child_removed), NULL, NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE,
412 gst_child_proxy_get_type (void)
414 static GType type = 0;
417 static const GTypeInfo info = {
418 sizeof (GstChildProxyInterface),
419 gst_child_proxy_base_init, /* base_init */
420 NULL, /* base_finalize */
421 NULL, /* class_init */
422 NULL, /* class_finalize */
423 NULL, /* class_data */
426 NULL /* instance_init */
428 type = g_type_register_static (G_TYPE_INTERFACE, "GstChildProxy", &info, 0);
430 g_type_interface_add_prerequisite (type, GST_TYPE_OBJECT);