childproxy: fix more missing GST_OBJECT -> G_OBJECT use
[platform/upstream/gstreamer.git] / gst / gstchildproxy.c
1 /* GStreamer
2  * Copyright (C) 2005 Stefan Kost <ensonic@users.sf.net>
3  *
4  * gstchildproxy.c: interface for multi child elements
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:gstchildproxy
24  * @short_description: Interface for multi child elements.
25  * @see_also: #GstBin
26  *
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
32  * objects.
33  *
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().
36  *
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.
40  */
41
42 #include "gst_private.h"
43
44 #include "gstchildproxy.h"
45 #include <gobject/gvaluecollector.h>
46
47 /* signals */
48 enum
49 {
50   CHILD_ADDED,
51   CHILD_REMOVED,
52   LAST_SIGNAL
53 };
54
55 static guint signals[LAST_SIGNAL] = { 0 };
56
57 static GObject *
58 gst_child_proxy_default_get_child_by_name (GstChildProxy * parent,
59     const gchar * name)
60 {
61   guint count, i;
62   GObject *object, *result;
63   gchar *object_name;
64
65   g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), NULL);
66   g_return_val_if_fail (name != NULL, NULL);
67
68   result = NULL;
69
70   count = gst_child_proxy_get_children_count (parent);
71   for (i = 0; i < count; i++) {
72     gboolean eq;
73
74     if (!(object = gst_child_proxy_get_child_by_index (parent, i)))
75       continue;
76
77     if (!GST_IS_OBJECT (object)) {
78       goto next;
79     }
80     object_name = gst_object_get_name (GST_OBJECT_CAST (object));
81     if (object_name == NULL) {
82       g_warning ("child %u of parent %s has no name", i,
83           GST_OBJECT_NAME (parent));
84       goto next;
85     }
86     eq = g_str_equal (object_name, name);
87     g_free (object_name);
88
89     if (eq) {
90       result = object;
91       break;
92     }
93   next:
94     g_object_unref (object);
95   }
96   return result;
97 }
98
99
100 /**
101  * gst_child_proxy_get_child_by_name:
102  * @parent: the parent object to get the child from
103  * @name: the childs name
104  *
105  * Looks up a child element by the given name.
106  *
107  * This virtual method has a default implementation that uses #GstObject
108  * together with gst_object_get_name(). If the interface is to be used with
109  * #GObjects, this methods needs to be overridden.
110  *
111  * Returns: (transfer full): the child object or %NULL if not found. Unref
112  *     after usage.
113  *
114  * MT safe.
115  */
116 GObject *
117 gst_child_proxy_get_child_by_name (GstChildProxy * parent, const gchar * name)
118 {
119   g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), 0);
120
121   return (GST_CHILD_PROXY_GET_INTERFACE (parent)->get_child_by_name (parent,
122           name));
123 }
124
125 /**
126  * gst_child_proxy_get_child_by_index:
127  * @parent: the parent object to get the child from
128  * @index: the childs position in the child list
129  *
130  * Fetches a child by its number.
131  *
132  * Returns: (transfer full): the child object or %NULL if not found (index
133  *     too high). Unref after usage.
134  *
135  * MT safe.
136  */
137 GObject *
138 gst_child_proxy_get_child_by_index (GstChildProxy * parent, guint index)
139 {
140   g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), NULL);
141
142   return (GST_CHILD_PROXY_GET_INTERFACE (parent)->get_child_by_index (parent,
143           index));
144 }
145
146 /**
147  * gst_child_proxy_get_children_count:
148  * @parent: the parent object
149  *
150  * Gets the number of child objects this parent contains.
151  *
152  * Returns: the number of child objects
153  *
154  * MT safe.
155  */
156 guint
157 gst_child_proxy_get_children_count (GstChildProxy * parent)
158 {
159   g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), 0);
160
161   return (GST_CHILD_PROXY_GET_INTERFACE (parent)->get_children_count (parent));
162 }
163
164 /**
165  * gst_child_proxy_lookup:
166  * @object: object to lookup the property in
167  * @name: name of the property to look up
168  * @target: (out) (allow-none) (transfer full): pointer to a #GObject that
169  *     takes the real object to set property on
170  * @pspec: (out) (allow-none) (transfer full): pointer to take the #GParamSpec
171  *     describing the property
172  *
173  * Looks up which object and #GParamSpec would be effected by the given @name.
174  *
175  * Returns: TRUE if @target and @pspec could be found. FALSE otherwise. In that
176  * case the values for @pspec and @target are not modified. Unref @target after
177  * usage.
178  *
179  * MT safe.
180  */
181 gboolean
182 gst_child_proxy_lookup (GObject * object, const gchar * name,
183     GObject ** target, GParamSpec ** pspec)
184 {
185   gboolean res = FALSE;
186   gchar **names, **current;
187
188   g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
189   g_return_val_if_fail (name != NULL, FALSE);
190
191   gst_object_ref (object);
192
193   current = names = g_strsplit (name, "::", -1);
194   while (current[1]) {
195     GObject *next;
196
197     if (!GST_IS_CHILD_PROXY (object)) {
198       GST_INFO
199           ("object %s is not a parent, so you cannot request a child by name %s",
200           (GST_IS_OBJECT (object) ? GST_OBJECT_NAME (object) : ""), current[0]);
201       break;
202     }
203     next = gst_child_proxy_get_child_by_name (GST_CHILD_PROXY (object),
204         current[0]);
205     if (!next) {
206       GST_INFO ("no such object %s", current[0]);
207       break;
208     }
209     gst_object_unref (object);
210     object = next;
211     current++;
212   }
213   if (current[1] == NULL) {
214     GParamSpec *spec =
215         g_object_class_find_property (G_OBJECT_GET_CLASS (object), current[0]);
216     if (spec == NULL) {
217       GST_INFO ("no param spec named %s", current[0]);
218     } else {
219       if (pspec)
220         *pspec = spec;
221       if (target) {
222         gst_object_ref (object);
223         *target = object;
224       }
225       res = TRUE;
226     }
227   }
228   gst_object_unref (object);
229   g_strfreev (names);
230   return res;
231 }
232
233 /**
234  * gst_child_proxy_get_property:
235  * @object: object to query
236  * @name: name of the property
237  * @value: (out caller-allocates): a #GValue that should take the result.
238  *
239  * Gets a single property using the GstChildProxy mechanism.
240  * You are responsible for freeing it by calling g_value_unset()
241  */
242 void
243 gst_child_proxy_get_property (GObject * object, const gchar * name,
244     GValue * value)
245 {
246   GParamSpec *pspec;
247   GObject *target;
248
249   g_return_if_fail (G_IS_OBJECT (object));
250   g_return_if_fail (name != NULL);
251   g_return_if_fail (G_IS_VALUE (value));
252
253   if (!gst_child_proxy_lookup (object, name, &target, &pspec))
254     goto not_found;
255
256   g_object_get_property (G_OBJECT (target), pspec->name, value);
257   gst_object_unref (target);
258
259   return;
260
261 not_found:
262   {
263     g_warning ("no property %s in object %s", name,
264         (GST_IS_OBJECT (object) ? GST_OBJECT_NAME (object) : ""));
265     return;
266   }
267 }
268
269 /**
270  * gst_child_proxy_get_valist:
271  * @object: the object to query
272  * @first_property_name: name of the first property to get
273  * @var_args: return location for the first property, followed optionally by more name/return location pairs, followed by NULL
274  *
275  * Gets properties of the parent object and its children.
276  */
277 void
278 gst_child_proxy_get_valist (GObject * object,
279     const gchar * first_property_name, va_list var_args)
280 {
281   const gchar *name;
282   gchar *error = NULL;
283   GValue value = { 0, };
284   GParamSpec *pspec;
285   GObject *target;
286
287   g_return_if_fail (G_IS_OBJECT (object));
288
289   name = first_property_name;
290
291   /* iterate over pairs */
292   while (name) {
293     if (!gst_child_proxy_lookup (object, name, &target, &pspec))
294       goto not_found;
295
296     g_value_init (&value, pspec->value_type);
297     g_object_get_property (G_OBJECT (target), pspec->name, &value);
298     gst_object_unref (target);
299
300     G_VALUE_LCOPY (&value, var_args, 0, &error);
301     if (error)
302       goto cant_copy;
303     g_value_unset (&value);
304     name = va_arg (var_args, gchar *);
305   }
306   return;
307
308 not_found:
309   {
310     g_warning ("no property %s in object %s", name,
311         (GST_IS_OBJECT (object) ? GST_OBJECT_NAME (object) : ""));
312     return;
313   }
314 cant_copy:
315   {
316     g_warning ("error copying value %s in object %s: %s", pspec->name,
317         (GST_IS_OBJECT (object) ? GST_OBJECT_NAME (object) : ""), error);
318     g_value_unset (&value);
319     return;
320   }
321 }
322
323 /**
324  * gst_child_proxy_get:
325  * @object: the parent object
326  * @first_property_name: name of the first property to get
327  * @...: return location for the first property, followed optionally by more name/return location pairs, followed by NULL
328  *
329  * Gets properties of the parent object and its children.
330  */
331 void
332 gst_child_proxy_get (GObject * object, const gchar * first_property_name, ...)
333 {
334   va_list var_args;
335
336   g_return_if_fail (G_IS_OBJECT (object));
337
338   va_start (var_args, first_property_name);
339   gst_child_proxy_get_valist (object, first_property_name, var_args);
340   va_end (var_args);
341 }
342
343 /**
344  * gst_child_proxy_set_property:
345  * @object: the parent object
346  * @name: name of the property to set
347  * @value: new #GValue for the property
348  *
349  * Sets a single property using the GstChildProxy mechanism.
350  */
351 void
352 gst_child_proxy_set_property (GObject * object, const gchar * name,
353     const GValue * value)
354 {
355   GParamSpec *pspec;
356   GObject *target;
357
358   g_return_if_fail (G_IS_OBJECT (object));
359   g_return_if_fail (name != NULL);
360   g_return_if_fail (G_IS_VALUE (value));
361
362   if (!gst_child_proxy_lookup (object, name, &target, &pspec))
363     goto not_found;
364
365   g_object_set_property (G_OBJECT (target), pspec->name, value);
366   gst_object_unref (target);
367   return;
368
369 not_found:
370   {
371     g_warning ("cannot set property %s on object %s", name,
372         (GST_IS_OBJECT (object) ? GST_OBJECT_NAME (object) : ""));
373     return;
374   }
375 }
376
377 /**
378  * gst_child_proxy_set_valist:
379  * @object: the parent object
380  * @first_property_name: name of the first property to set
381  * @var_args: value for the first property, followed optionally by more name/value pairs, followed by NULL
382  *
383  * Sets properties of the parent object and its children.
384  */
385 void
386 gst_child_proxy_set_valist (GObject * object,
387     const gchar * first_property_name, va_list var_args)
388 {
389   const gchar *name;
390   gchar *error = NULL;
391   GValue value = { 0, };
392   GParamSpec *pspec;
393   GObject *target;
394
395   g_return_if_fail (G_IS_OBJECT (object));
396
397   name = first_property_name;
398
399   /* iterate over pairs */
400   while (name) {
401     if (!gst_child_proxy_lookup (object, name, &target, &pspec))
402       goto not_found;
403
404     G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
405         G_VALUE_NOCOPY_CONTENTS, &error);
406
407     if (error)
408       goto cant_copy;
409
410     g_object_set_property (G_OBJECT (target), pspec->name, &value);
411     gst_object_unref (target);
412
413     g_value_unset (&value);
414     name = va_arg (var_args, gchar *);
415   }
416   return;
417
418 not_found:
419   {
420     g_warning ("no property %s in object %s", name,
421         (GST_IS_OBJECT (object) ? GST_OBJECT_NAME (object) : ""));
422     return;
423   }
424 cant_copy:
425   {
426     g_warning ("error copying value %s in object %s: %s", pspec->name,
427         (GST_IS_OBJECT (object) ? GST_OBJECT_NAME (object) : ""), error);
428     g_value_unset (&value);
429     gst_object_unref (target);
430     return;
431   }
432 }
433
434 /**
435  * gst_child_proxy_set:
436  * @object: the parent object
437  * @first_property_name: name of the first property to set
438  * @...: value for the first property, followed optionally by more name/value pairs, followed by NULL
439  *
440  * Sets properties of the parent object and its children.
441  */
442 void
443 gst_child_proxy_set (GObject * object, const gchar * first_property_name, ...)
444 {
445   va_list var_args;
446
447   g_return_if_fail (G_IS_OBJECT (object));
448
449   va_start (var_args, first_property_name);
450   gst_child_proxy_set_valist (object, first_property_name, var_args);
451   va_end (var_args);
452 }
453
454 /**
455  * gst_child_proxy_child_added:
456  * @object: the parent object
457  * @child: the newly added child
458  * @name: the name of the new child
459  *
460  * Emits the "child-added" signal.
461  */
462 void
463 gst_child_proxy_child_added (GObject * object, GObject * child,
464     const gchar * name)
465 {
466   g_signal_emit (G_OBJECT (object), signals[CHILD_ADDED], 0, child, name);
467 }
468
469 /**
470  * gst_child_proxy_child_removed:
471  * @object: the parent object
472  * @child: the removed child
473  * @name: the name of the old child
474  *
475  * Emits the "child-removed" signal.
476  */
477 void
478 gst_child_proxy_child_removed (GObject * object, GObject * child,
479     const gchar * name)
480 {
481   g_signal_emit (G_OBJECT (object), signals[CHILD_REMOVED], 0, child, name);
482 }
483
484 /* gobject methods */
485
486 static void
487 gst_child_proxy_class_init (gpointer g_class, gpointer class_data)
488 {
489   GstChildProxyInterface *iface = (GstChildProxyInterface *) g_class;
490
491   iface->get_child_by_name = gst_child_proxy_default_get_child_by_name;
492 }
493
494 static void
495 gst_child_proxy_base_init (gpointer g_class)
496 {
497   static gboolean initialized = FALSE;
498
499   if (!initialized) {
500     /* create interface signals and properties here. */
501     /**
502      * GstChildProxy::child-added:
503      * @child_proxy: the #GstChildProxy
504      * @object: the #GObject that was added
505      * @name: the name of the new child
506      *
507      * Will be emitted after the @object was added to the @child_proxy.
508      */
509     signals[CHILD_ADDED] =
510         g_signal_new ("child-added", G_TYPE_FROM_CLASS (g_class),
511         G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstChildProxyInterface,
512             child_added), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
513         2, G_TYPE_OBJECT, G_TYPE_STRING);
514
515     /**
516      * GstChildProxy::child-removed:
517      * @child_proxy: the #GstChildProxy
518      * @object: the #GObject that was removed
519      * @name: the name of the old child
520      *
521      * Will be emitted after the @object was removed from the @child_proxy.
522      */
523     signals[CHILD_REMOVED] =
524         g_signal_new ("child-removed", G_TYPE_FROM_CLASS (g_class),
525         G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstChildProxyInterface,
526             child_removed), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
527         2, G_TYPE_OBJECT, G_TYPE_STRING);
528
529     initialized = TRUE;
530   }
531 }
532
533 GType
534 gst_child_proxy_get_type (void)
535 {
536   static volatile gsize type = 0;
537
538   if (g_once_init_enter (&type)) {
539     GType _type;
540     static const GTypeInfo info = {
541       sizeof (GstChildProxyInterface),
542       gst_child_proxy_base_init,        /* base_init */
543       NULL,                     /* base_finalize */
544       gst_child_proxy_class_init,       /* class_init */
545       NULL,                     /* class_finalize */
546       NULL,                     /* class_data */
547       0,
548       0,                        /* n_preallocs */
549       NULL                      /* instance_init */
550     };
551
552     _type =
553         g_type_register_static (G_TYPE_INTERFACE, "GstChildProxy", &info, 0);
554
555     g_type_interface_add_prerequisite (_type, G_TYPE_OBJECT);
556     g_once_init_leave (&type, (gsize) _type);
557   }
558   return type;
559 }