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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:gstchildproxy
24 * @title: GstChildProxy
25 * @short_description: Interface for multi child elements.
28 * This interface abstracts handling of property sets for elements with
29 * children. Imagine elements such as mixers or polyphonic generators. They all
30 * have multiple #GstPad or some kind of voice objects. Another use case are
31 * container elements like #GstBin.
32 * The element implementing the interface acts as a parent for those child
35 * By implementing this interface the child properties can be accessed from the
36 * parent element by using gst_child_proxy_get() and gst_child_proxy_set().
38 * Property names are written as `child-name::property-name`. The whole naming
39 * scheme is recursive. Thus `child1::child2::property` is valid too, if
40 * `child1` and `child2` implement the #GstChildProxy interface.
43 #include "gst_private.h"
45 #include "gstchildproxy.h"
46 #include <gobject/gvaluecollector.h>
56 static guint signals[LAST_SIGNAL] = { 0 };
59 gst_child_proxy_default_get_child_by_name (GstChildProxy * parent,
63 GObject *object, *result;
66 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), NULL);
67 g_return_val_if_fail (name != NULL, NULL);
71 count = gst_child_proxy_get_children_count (parent);
72 for (i = 0; i < count; i++) {
75 if (!(object = gst_child_proxy_get_child_by_index (parent, i)))
78 if (!GST_IS_OBJECT (object)) {
81 object_name = gst_object_get_name (GST_OBJECT_CAST (object));
82 if (object_name == NULL) {
83 g_warning ("child %u of parent %s has no name", i,
84 GST_OBJECT_NAME (parent));
87 eq = g_str_equal (object_name, name);
95 gst_object_unref (object);
102 * gst_child_proxy_get_child_by_name:
103 * @parent: the parent object to get the child from
104 * @name: the child's name
106 * Looks up a child element by the given name.
108 * This virtual method has a default implementation that uses #GstObject
109 * together with gst_object_get_name(). If the interface is to be used with
110 * #GObjects, this methods needs to be overridden.
112 * Returns: (transfer full) (nullable): the child object or %NULL if
116 gst_child_proxy_get_child_by_name (GstChildProxy * parent, const gchar * name)
118 GstChildProxyInterface *iface;
120 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), 0);
122 iface = GST_CHILD_PROXY_GET_INTERFACE (parent);
124 if (iface->get_child_by_name != NULL)
125 return iface->get_child_by_name (parent, name);
131 * gst_child_proxy_get_child_by_index:
132 * @parent: the parent object to get the child from
133 * @index: the child's position in the child list
135 * Fetches a child by its number.
137 * Returns: (transfer full) (nullable): the child object or %NULL if
138 * not found (index too high).
141 gst_child_proxy_get_child_by_index (GstChildProxy * parent, guint index)
143 GstChildProxyInterface *iface;
145 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), NULL);
147 iface = GST_CHILD_PROXY_GET_INTERFACE (parent);
149 if (iface->get_child_by_index != NULL)
150 return iface->get_child_by_index (parent, index);
156 * gst_child_proxy_get_children_count:
157 * @parent: the parent object
159 * Gets the number of child objects this parent contains.
161 * Returns: the number of child objects
164 gst_child_proxy_get_children_count (GstChildProxy * parent)
166 GstChildProxyInterface *iface;
168 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), 0);
170 iface = GST_CHILD_PROXY_GET_INTERFACE (parent);
172 if (iface->get_children_count != NULL)
173 return iface->get_children_count (parent);
179 * gst_child_proxy_lookup:
180 * @object: child proxy object to lookup the property in
181 * @name: name of the property to look up
182 * @target: (out) (allow-none) (transfer full): pointer to a #GObject that
183 * takes the real object to set property on
184 * @pspec: (out) (allow-none) (transfer none): pointer to take the #GParamSpec
185 * describing the property
187 * Looks up which object and #GParamSpec would be effected by the given @name.
189 * Returns: %TRUE if @target and @pspec could be found. %FALSE otherwise. In that
190 * case the values for @pspec and @target are not modified. Unref @target after
191 * usage. For plain #GObject @target is the same as @object.
194 gst_child_proxy_lookup (GstChildProxy * object, const gchar * name,
195 GObject ** target, GParamSpec ** pspec)
198 gboolean res = FALSE;
199 gchar **names, **current;
201 g_return_val_if_fail (GST_IS_CHILD_PROXY (object), FALSE);
202 g_return_val_if_fail (name != NULL, FALSE);
204 obj = G_OBJECT (g_object_ref (object));
206 current = names = g_strsplit (name, "::", -1);
207 /* find the owner of the property */
211 if (!GST_IS_CHILD_PROXY (obj)) {
213 ("object %s is not a parent, so you cannot request a child by name %s",
214 (GST_IS_OBJECT (obj) ? GST_OBJECT_NAME (obj) : ""), current[0]);
217 next = gst_child_proxy_get_child_by_name (GST_CHILD_PROXY (obj),
220 GST_INFO ("no such object %s", current[0]);
223 gst_object_unref (obj);
229 if (current[1] == NULL) {
231 g_object_class_find_property (G_OBJECT_GET_CLASS (obj), current[0]);
233 GST_INFO ("no param spec named %s", current[0]);
244 gst_object_unref (obj);
250 * gst_child_proxy_get_property:
251 * @object: object to query
252 * @name: name of the property
253 * @value: (out caller-allocates): a #GValue that should take the result.
255 * Gets a single property using the GstChildProxy mechanism.
256 * You are responsible for freeing it by calling g_value_unset()
259 gst_child_proxy_get_property (GstChildProxy * object, const gchar * name,
265 g_return_if_fail (GST_IS_CHILD_PROXY (object));
266 g_return_if_fail (name != NULL);
267 g_return_if_fail (value != NULL);
269 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
272 if (!G_IS_VALUE (value)) {
273 g_value_init (value, pspec->value_type);
276 g_object_get_property (target, pspec->name, value);
277 gst_object_unref (target);
283 g_warning ("no property %s in object %s", name,
284 (GST_IS_OBJECT (object) ? GST_OBJECT_NAME (object) : ""));
290 * gst_child_proxy_get_valist:
291 * @object: the object to query
292 * @first_property_name: name of the first property to get
293 * @var_args: return location for the first property, followed optionally by more name/return location pairs, followed by %NULL
295 * Gets properties of the parent object and its children.
298 gst_child_proxy_get_valist (GstChildProxy * object,
299 const gchar * first_property_name, va_list var_args)
303 GValue value = { 0, };
307 g_return_if_fail (GST_IS_CHILD_PROXY (object));
309 name = first_property_name;
311 /* iterate over pairs */
313 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
316 g_value_init (&value, pspec->value_type);
317 g_object_get_property (target, pspec->name, &value);
318 gst_object_unref (target);
320 G_VALUE_LCOPY (&value, var_args, 0, &error);
323 g_value_unset (&value);
324 name = va_arg (var_args, gchar *);
330 g_warning ("no property %s in object %s", name,
331 (GST_IS_OBJECT (object) ? GST_OBJECT_NAME (object) : ""));
336 g_warning ("error copying value %s in object %s: %s", pspec->name,
337 (GST_IS_OBJECT (object) ? GST_OBJECT_NAME (object) : ""), error);
338 g_value_unset (&value);
344 * gst_child_proxy_get:
345 * @object: the parent object
346 * @first_property_name: name of the first property to get
347 * @...: return location for the first property, followed optionally by more name/return location pairs, followed by %NULL
349 * Gets properties of the parent object and its children.
352 gst_child_proxy_get (GstChildProxy * object, const gchar * first_property_name,
357 g_return_if_fail (GST_IS_CHILD_PROXY (object));
359 va_start (var_args, first_property_name);
360 gst_child_proxy_get_valist (object, first_property_name, var_args);
365 * gst_child_proxy_set_property:
366 * @object: the parent object
367 * @name: name of the property to set
368 * @value: new #GValue for the property
370 * Sets a single property using the GstChildProxy mechanism.
373 gst_child_proxy_set_property (GstChildProxy * object, const gchar * name,
374 const GValue * value)
379 g_return_if_fail (GST_IS_CHILD_PROXY (object));
380 g_return_if_fail (name != NULL);
381 g_return_if_fail (G_IS_VALUE (value));
383 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
386 g_object_set_property (target, pspec->name, value);
387 gst_object_unref (target);
392 g_warning ("cannot set property %s on object %s", name,
393 (GST_IS_OBJECT (object) ? GST_OBJECT_NAME (object) : ""));
399 * gst_child_proxy_set_valist:
400 * @object: the parent object
401 * @first_property_name: name of the first property to set
402 * @var_args: value for the first property, followed optionally by more name/value pairs, followed by %NULL
404 * Sets properties of the parent object and its children.
407 gst_child_proxy_set_valist (GstChildProxy * object,
408 const gchar * first_property_name, va_list var_args)
412 GValue value = { 0, };
416 g_return_if_fail (GST_IS_CHILD_PROXY (object));
418 name = first_property_name;
420 /* iterate over pairs */
422 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
425 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
426 G_VALUE_NOCOPY_CONTENTS, &error);
431 g_object_set_property (target, pspec->name, &value);
432 gst_object_unref (target);
434 g_value_unset (&value);
435 name = va_arg (var_args, gchar *);
441 g_warning ("no property %s in object %s", name,
442 (GST_IS_OBJECT (object) ? GST_OBJECT_NAME (object) : ""));
447 g_warning ("error copying value %s in object %s: %s", pspec->name,
448 (GST_IS_OBJECT (object) ? GST_OBJECT_NAME (object) : ""), error);
449 g_value_unset (&value);
450 gst_object_unref (target);
457 * gst_child_proxy_set:
458 * @object: the parent object
459 * @first_property_name: name of the first property to set
460 * @...: value for the first property, followed optionally by more name/value pairs, followed by %NULL
462 * Sets properties of the parent object and its children.
465 gst_child_proxy_set (GstChildProxy * object, const gchar * first_property_name,
470 g_return_if_fail (GST_IS_CHILD_PROXY (object));
472 va_start (var_args, first_property_name);
473 gst_child_proxy_set_valist (object, first_property_name, var_args);
478 * gst_child_proxy_child_added:
479 * @parent: the parent object
480 * @child: the newly added child
481 * @name: the name of the new child
483 * Emits the #GstChildProxy::child-added signal.
486 gst_child_proxy_child_added (GstChildProxy * parent, GObject * child,
489 g_signal_emit (parent, signals[CHILD_ADDED], 0, child, name);
493 * gst_child_proxy_child_removed:
494 * @parent: the parent object
495 * @child: the removed child
496 * @name: the name of the old child
498 * Emits the #GstChildProxy::child-removed signal.
501 gst_child_proxy_child_removed (GstChildProxy * parent, GObject * child,
504 g_signal_emit (parent, signals[CHILD_REMOVED], 0, child, name);
507 /* gobject methods */
510 gst_child_proxy_class_init (gpointer g_class, gpointer class_data)
512 GstChildProxyInterface *iface = (GstChildProxyInterface *) g_class;
514 iface->get_child_by_name = gst_child_proxy_default_get_child_by_name;
518 gst_child_proxy_base_init (gpointer g_class)
520 static gboolean initialized = FALSE;
523 /* create interface signals and properties here. */
525 * GstChildProxy::child-added:
526 * @self: the #GstChildProxy
527 * @object: the #GObject that was added
528 * @name: the name of the new child
530 * Will be emitted after the @object was added to the @child_proxy.
532 signals[CHILD_ADDED] =
533 g_signal_new ("child-added", G_TYPE_FROM_CLASS (g_class),
534 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstChildProxyInterface,
535 child_added), NULL, NULL, NULL, G_TYPE_NONE,
536 2, G_TYPE_OBJECT, G_TYPE_STRING);
539 * GstChildProxy::child-removed:
540 * @self: the #GstChildProxy
541 * @object: the #GObject that was removed
542 * @name: the name of the old child
544 * Will be emitted after the @object was removed from the @child_proxy.
546 signals[CHILD_REMOVED] =
547 g_signal_new ("child-removed", G_TYPE_FROM_CLASS (g_class),
548 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstChildProxyInterface,
549 child_removed), NULL, NULL, NULL, G_TYPE_NONE,
550 2, G_TYPE_OBJECT, G_TYPE_STRING);
557 gst_child_proxy_get_type (void)
559 static gsize type = 0;
561 if (g_once_init_enter (&type)) {
563 static const GTypeInfo info = {
564 sizeof (GstChildProxyInterface),
565 gst_child_proxy_base_init, /* base_init */
566 NULL, /* base_finalize */
567 gst_child_proxy_class_init, /* class_init */
568 NULL, /* class_finalize */
569 NULL, /* class_data */
572 NULL /* instance_init */
576 g_type_register_static (G_TYPE_INTERFACE, "GstChildProxy", &info, 0);
578 g_type_interface_add_prerequisite (_type, G_TYPE_OBJECT);
579 g_once_init_leave (&type, (gsize) _type);