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.
23 * SECTION:gstchildproxy
24 * @short_description: Interface for multi child elements.
27 * This interface abstracts handling of property sets for elements with
28 * children. Imagine elements such as mixers or polyphonic generators. They all
29 * have multiple #GstPad or some kind of voice objects. Another use case are
30 * container elements like #GstBin.
31 * The element implementing the interface acts as a parent for those child
34 * By implementing this interface the child properties can be accessed from the
35 * parent element by using gst_child_proxy_get() and gst_child_proxy_set().
37 * Property names are written as "child-name::property-name". The whole naming
38 * scheme is recursive. Thus "child1::child2::property" is valid too, if
39 * "child1" and "child2" implement the #GstChildProxy interface.
42 * it would be nice to make gst_child_proxy_get_child_by_name virtual too and
43 * use GObject instead of GstObject. We could eventually provide the current
44 * implementation as a default if children are GstObjects.
45 * This change would allow to propose the interface for inclusion with
46 * glib/gobject. IMHO this is useful for GtkContainer and compound widgets too.
49 #include "gst_private.h"
51 #include "gstchildproxy.h"
52 #include <gobject/gvaluecollector.h>
62 static guint signals[LAST_SIGNAL] = { 0 };
65 * gst_child_proxy_get_child_by_name:
66 * @parent: the parent object to get the child from
67 * @name: the childs name
69 * Looks up a child element by the given name.
71 * Implementors can use #GstObject together with gst_object_get_name()
73 * Returns: (transfer full): the child object or %NULL if not found. Unref
79 gst_child_proxy_get_child_by_name (GstChildProxy * parent, const gchar * name)
82 GstObject *object, *result;
85 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), NULL);
86 g_return_val_if_fail (name != NULL, NULL);
90 count = gst_child_proxy_get_children_count (parent);
91 for (i = 0; i < count; i++) {
94 if (!(object = gst_child_proxy_get_child_by_index (parent, i)))
97 object_name = gst_object_get_name (object);
98 if (object_name == NULL) {
99 g_warning ("child %u of parent %s has no name", i,
100 GST_OBJECT_NAME (parent));
103 eq = g_str_equal (object_name, name);
104 g_free (object_name);
111 gst_object_unref (object);
117 * gst_child_proxy_get_child_by_index:
118 * @parent: the parent object to get the child from
119 * @index: the childs position in the child list
121 * Fetches a child by its number.
123 * Returns: (transfer full): the child object or %NULL if not found (index
124 * too high). Unref after usage.
129 gst_child_proxy_get_child_by_index (GstChildProxy * parent, guint index)
131 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), NULL);
133 return (GST_CHILD_PROXY_GET_INTERFACE (parent)->get_child_by_index (parent,
138 * gst_child_proxy_get_children_count:
139 * @parent: the parent object
141 * Gets the number of child objects this parent contains.
143 * Returns: the number of child objects
148 gst_child_proxy_get_children_count (GstChildProxy * parent)
150 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), 0);
152 return (GST_CHILD_PROXY_GET_INTERFACE (parent)->get_children_count (parent));
156 * gst_child_proxy_lookup:
157 * @object: object to lookup the property in
158 * @name: name of the property to look up
159 * @target: (out) (allow-none) (transfer full): pointer to a #GstObject that
160 * takes the real object to set property on
161 * @pspec: (out) (allow-none) (transfer full): pointer to take the #GParamSpec
162 * describing the property
164 * Looks up which object and #GParamSpec would be effected by the given @name.
166 * Returns: TRUE if @target and @pspec could be found. FALSE otherwise. In that
167 * case the values for @pspec and @target are not modified. Unref @target after
173 gst_child_proxy_lookup (GstObject * object, const gchar * name,
174 GstObject ** target, GParamSpec ** pspec)
176 gboolean res = FALSE;
177 gchar **names, **current;
179 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
180 g_return_val_if_fail (name != NULL, FALSE);
182 gst_object_ref (object);
184 current = names = g_strsplit (name, "::", -1);
188 if (!GST_IS_CHILD_PROXY (object)) {
190 ("object %s is not a parent, so you cannot request a child by name %s",
191 GST_OBJECT_NAME (object), current[0]);
194 next = gst_child_proxy_get_child_by_name (GST_CHILD_PROXY (object),
197 GST_INFO ("no such object %s", current[0]);
200 gst_object_unref (object);
204 if (current[1] == NULL) {
206 g_object_class_find_property (G_OBJECT_GET_CLASS (object), current[0]);
208 GST_INFO ("no param spec named %s", current[0]);
213 gst_object_ref (object);
219 gst_object_unref (object);
225 * gst_child_proxy_get_property:
226 * @object: object to query
227 * @name: name of the property
228 * @value: (out caller-allocates): a #GValue that should take the result.
230 * Gets a single property using the GstChildProxy mechanism.
231 * You are responsible for freeing it by calling g_value_unset()
234 gst_child_proxy_get_property (GstObject * object, const gchar * name,
240 g_return_if_fail (GST_IS_OBJECT (object));
241 g_return_if_fail (name != NULL);
242 g_return_if_fail (G_IS_VALUE (value));
244 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
247 g_object_get_property (G_OBJECT (target), pspec->name, value);
248 gst_object_unref (target);
254 g_warning ("no property %s in object %s", name, GST_OBJECT_NAME (object));
260 * gst_child_proxy_get_valist:
261 * @object: the object to query
262 * @first_property_name: name of the first property to get
263 * @var_args: return location for the first property, followed optionally by more name/return location pairs, followed by NULL
265 * Gets properties of the parent object and its children.
268 gst_child_proxy_get_valist (GstObject * object,
269 const gchar * first_property_name, va_list var_args)
273 GValue value = { 0, };
277 g_return_if_fail (G_IS_OBJECT (object));
279 name = first_property_name;
281 /* iterate over pairs */
283 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
286 g_value_init (&value, pspec->value_type);
287 g_object_get_property (G_OBJECT (target), pspec->name, &value);
288 gst_object_unref (target);
290 G_VALUE_LCOPY (&value, var_args, 0, &error);
293 g_value_unset (&value);
294 name = va_arg (var_args, gchar *);
300 g_warning ("no property %s in object %s", name, GST_OBJECT_NAME (object));
305 g_warning ("error copying value %s in object %s: %s", pspec->name,
306 GST_OBJECT_NAME (object), error);
307 g_value_unset (&value);
313 * gst_child_proxy_get:
314 * @object: the parent object
315 * @first_property_name: name of the first property to get
316 * @...: return location for the first property, followed optionally by more name/return location pairs, followed by NULL
318 * Gets properties of the parent object and its children.
321 gst_child_proxy_get (GstObject * object, const gchar * first_property_name, ...)
325 g_return_if_fail (GST_IS_OBJECT (object));
327 va_start (var_args, first_property_name);
328 gst_child_proxy_get_valist (object, first_property_name, var_args);
333 * gst_child_proxy_set_property:
334 * @object: the parent object
335 * @name: name of the property to set
336 * @value: new #GValue for the property
338 * Sets a single property using the GstChildProxy mechanism.
341 gst_child_proxy_set_property (GstObject * object, const gchar * name,
342 const GValue * value)
347 g_return_if_fail (GST_IS_OBJECT (object));
348 g_return_if_fail (name != NULL);
349 g_return_if_fail (G_IS_VALUE (value));
351 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
354 g_object_set_property (G_OBJECT (target), pspec->name, value);
355 gst_object_unref (target);
360 g_warning ("cannot set property %s on object %s", name,
361 GST_OBJECT_NAME (object));
367 * gst_child_proxy_set_valist:
368 * @object: the parent object
369 * @first_property_name: name of the first property to set
370 * @var_args: value for the first property, followed optionally by more name/value pairs, followed by NULL
372 * Sets properties of the parent object and its children.
375 gst_child_proxy_set_valist (GstObject * object,
376 const gchar * first_property_name, va_list var_args)
380 GValue value = { 0, };
384 g_return_if_fail (G_IS_OBJECT (object));
386 name = first_property_name;
388 /* iterate over pairs */
390 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
393 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
394 G_VALUE_NOCOPY_CONTENTS, &error);
399 g_object_set_property (G_OBJECT (target), pspec->name, &value);
400 gst_object_unref (target);
402 g_value_unset (&value);
403 name = va_arg (var_args, gchar *);
409 g_warning ("no property %s in object %s", name, GST_OBJECT_NAME (object));
414 g_warning ("error copying value %s in object %s: %s", pspec->name,
415 GST_OBJECT_NAME (object), error);
416 g_value_unset (&value);
417 gst_object_unref (target);
423 * gst_child_proxy_set:
424 * @object: the parent object
425 * @first_property_name: name of the first property to set
426 * @...: value for the first property, followed optionally by more name/value pairs, followed by NULL
428 * Sets properties of the parent object and its children.
431 gst_child_proxy_set (GstObject * object, const gchar * first_property_name, ...)
435 g_return_if_fail (GST_IS_OBJECT (object));
437 va_start (var_args, first_property_name);
438 gst_child_proxy_set_valist (object, first_property_name, var_args);
443 * gst_child_proxy_child_added:
444 * @object: the parent object
445 * @child: the newly added child
447 * Emits the "child-added" signal.
450 gst_child_proxy_child_added (GstObject * object, GstObject * child)
452 g_signal_emit (G_OBJECT (object), signals[CHILD_ADDED], 0, child);
456 * gst_child_proxy_child_removed:
457 * @object: the parent object
458 * @child: the removed child
460 * Emits the "child-removed" signal.
463 gst_child_proxy_child_removed (GstObject * object, GstObject * child)
465 g_signal_emit (G_OBJECT (object), signals[CHILD_REMOVED], 0, child);
468 /* gobject methods */
471 gst_child_proxy_base_init (gpointer g_class)
473 static gboolean initialized = FALSE;
476 /* create interface signals and properties here. */
478 * GstChildProxy::child-added:
479 * @child_proxy: the #GstChildProxy
480 * @object: the #GObject that was added
482 * Will be emitted after the @object was added to the @child_proxy.
484 signals[CHILD_ADDED] =
485 g_signal_new ("child-added", G_TYPE_FROM_CLASS (g_class),
486 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstChildProxyInterface,
487 child_added), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
491 * GstChildProxy::child-removed:
492 * @child_proxy: the #GstChildProxy
493 * @object: the #GObject that was removed
495 * Will be emitted after the @object was removed from the @child_proxy.
497 signals[CHILD_REMOVED] =
498 g_signal_new ("child-removed", G_TYPE_FROM_CLASS (g_class),
499 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstChildProxyInterface,
500 child_removed), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
508 gst_child_proxy_get_type (void)
510 static volatile gsize type = 0;
512 if (g_once_init_enter (&type)) {
514 static const GTypeInfo info = {
515 sizeof (GstChildProxyInterface),
516 gst_child_proxy_base_init, /* base_init */
517 NULL, /* base_finalize */
518 NULL, /* class_init */
519 NULL, /* class_finalize */
520 NULL, /* class_data */
523 NULL /* instance_init */
527 g_type_register_static (G_TYPE_INTERFACE, "GstChildProxy", &info, 0);
529 g_type_interface_add_prerequisite (_type, GST_TYPE_OBJECT);
530 g_once_init_leave (&type, (gsize) _type);