0d2e73f07b33bd7a4e5bd0482c28e2d0e7c43251
[platform/upstream/gstreamer.git] / gst / gstutils.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstutils.c: Utility functions: gtk_get_property stuff, etc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
26
27 #include "gst_private.h"
28 #include "gstutils.h"
29 #include "gsturitype.h"
30 #include "gstlog.h"
31
32 /**
33  * gst_util_dump_mem:
34  * @mem: a pointer to the memory to dump
35  * @size: the size of the memory block to dump
36  *
37  * Dumps the memory block into a hex representation. Useful for debugging.
38  */
39 void
40 gst_util_dump_mem (guchar *mem, guint size)
41 {
42   guint i, j;
43   GString *string = g_string_sized_new (50);
44   GString *chars = g_string_sized_new (18);
45
46   i = j = 0;
47   while (i < size) {
48     if (g_ascii_isprint (mem[i]))
49       g_string_append_printf (chars, "%c", mem[i]);
50     else 
51       g_string_append_printf (chars, ".");
52
53     g_string_append_printf (string, "%02x ", mem[i]);
54
55     j++;
56     i++;
57
58     if (j == 16 || i == size) {
59       g_print ("%08x (%p): %-48.48s %-16.16s\n", i-j, mem+i-j, string->str, chars->str);
60       g_string_set_size (string, 0);
61       g_string_set_size (chars, 0);
62       j = 0;
63     }
64   }
65   g_string_free (string, TRUE);
66   g_string_free (chars, TRUE);
67 }
68
69
70 /**
71  * gst_util_set_value_from_string:
72  * @value: the value to set
73  * @value_str: the string to get the value from
74  *
75  * Converts the string to the type of the value and
76  * sets the value with it.
77  */
78 void
79 gst_util_set_value_from_string(GValue *value, const gchar *value_str)
80 {
81
82         g_return_if_fail(value != NULL);
83         g_return_if_fail(value_str != NULL);
84         
85         GST_DEBUG(GST_CAT_PARAMS, "parsing '%s' to type %s", value_str, g_type_name(G_VALUE_TYPE(value)));
86
87         switch (G_VALUE_TYPE(value)) {
88                 case G_TYPE_STRING:
89                         g_value_set_string(value, g_strdup(value_str));
90                         break;
91                 case G_TYPE_ENUM: 
92                 case G_TYPE_INT: {
93                         gint i;
94                         sscanf (value_str, "%d", &i);
95                         g_value_set_int(value, i);
96                         break;
97                 }
98                 case G_TYPE_UINT: {
99                         guint i;
100                         sscanf (value_str, "%u", &i);
101                         g_value_set_uint(value, i);
102                         break;
103                 }
104                 case G_TYPE_LONG: {
105                         glong i;
106                         sscanf (value_str, "%ld", &i);
107                         g_value_set_long(value, i);
108                         break;
109                 }
110                 case G_TYPE_ULONG: {
111                         gulong i;
112                         sscanf (value_str, "%lu", &i);
113                         g_value_set_ulong(value, i);
114                         break;
115                 }
116                 case G_TYPE_BOOLEAN: {
117                         gboolean i = FALSE;
118                         if (!strncmp ("true", value_str, 4)) i = TRUE;
119                         g_value_set_boolean(value, i);
120                         break;
121                 }
122                 case G_TYPE_CHAR: {
123                         gchar i;
124                         sscanf (value_str, "%c", &i);
125                         g_value_set_char(value, i);
126                         break;
127                 }
128                 case G_TYPE_UCHAR: {
129                         guchar i;
130                         sscanf (value_str, "%c", &i);
131                         g_value_set_uchar(value, i);
132                         break;
133                 }
134                 case G_TYPE_FLOAT: {
135                         gfloat i;
136                         sscanf (value_str, "%f", &i);
137                         g_value_set_float(value, i);
138                         break;
139                 }
140                 case G_TYPE_DOUBLE: {
141                         gfloat i;
142                         sscanf (value_str, "%g", &i);
143                         g_value_set_double(value, (gdouble)i);
144                         break;
145                 }
146                 default:
147                         break;
148         }
149 }
150
151 /**
152  * gst_util_set_object_arg:
153  * @object: the object to set the argument of
154  * @name: the name of the argument to set
155  * @value: the string value to set
156  *
157  * Convertes the string value to the type of the objects argument and
158  * sets the argument with it.
159  */
160 void
161 gst_util_set_object_arg (GObject * object, const gchar * name, const gchar * value)
162 {
163   if (name && value) {
164     GParamSpec *paramspec;
165
166     paramspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
167
168     if (!paramspec) {
169       return;
170     }
171
172     GST_DEBUG (0, "paramspec->flags is %d, paramspec->value_type is %d",
173                paramspec->flags, (gint) paramspec->value_type);
174
175     if (paramspec->flags & G_PARAM_WRITABLE) {
176       switch (paramspec->value_type) {
177         case G_TYPE_STRING:
178           g_object_set (G_OBJECT (object), name, value, NULL);
179           break;
180         case G_TYPE_ENUM:
181         case G_TYPE_INT:{
182           gint i;
183
184           sscanf (value, "%d", &i);
185           g_object_set (G_OBJECT (object), name, i, NULL);
186           break;
187         }
188         case G_TYPE_UINT:{
189           guint i;
190
191           sscanf (value, "%u", &i);
192           g_object_set (G_OBJECT (object), name, i, NULL);
193           break;
194         }
195         case G_TYPE_LONG:{
196           glong i;
197
198           sscanf (value, "%ld", &i);
199           g_object_set (G_OBJECT (object), name, i, NULL);
200           break;
201         }
202         case G_TYPE_ULONG:{
203           gulong i;
204
205           sscanf (value, "%lu", &i);
206           g_object_set (G_OBJECT (object), name, i, NULL);
207           break;
208         }
209         case G_TYPE_BOOLEAN:{
210           gboolean i = FALSE;
211
212           if (!strncmp ("true", value, 4))
213             i = TRUE;
214           g_object_set (G_OBJECT (object), name, i, NULL);
215           break;
216         }
217         case G_TYPE_CHAR:{
218           gchar i;
219
220           sscanf (value, "%c", &i);
221           g_object_set (G_OBJECT (object), name, i, NULL);
222           break;
223         }
224         case G_TYPE_UCHAR:{
225           guchar i;
226
227           sscanf (value, "%c", &i);
228           g_object_set (G_OBJECT (object), name, i, NULL);
229           break;
230         }
231         case G_TYPE_FLOAT:{
232           gfloat i;
233
234           sscanf (value, "%f", &i);
235           g_object_set (G_OBJECT (object), name, i, NULL);
236           break;
237         }
238         case G_TYPE_DOUBLE:{
239           gfloat i;
240
241           sscanf (value, "%g", &i);
242           g_object_set (G_OBJECT (object), name, (gdouble) i, NULL);
243           break;
244         }
245         default:
246           if (G_IS_PARAM_SPEC_ENUM (paramspec)) {
247             gint i;
248
249             sscanf (value, "%d", &i);
250             g_object_set (G_OBJECT (object), name, i, NULL);
251           }
252           else if (paramspec->value_type == GST_TYPE_URI) {
253             g_object_set (G_OBJECT (object), name, value, NULL);
254           }
255           break;
256       }
257     }
258   }
259 }
260
261 /* -----------------------------------------------------
262  *
263  *  The following code will be moved out of the main
264  * gstreamer library someday.
265  */
266
267 #include "gstpad.h"
268 #include "gsttype.h"
269 #include "gstprops.h"
270
271 static void
272 string_append_indent (GString * str, gint count)
273 {
274   gint xx;
275
276   for (xx = 0; xx < count; xx++)
277     g_string_append_c (str, ' ');
278 }
279
280 static void
281 gst_print_props (GString *buf, gint indent, GList *props, gboolean showname)
282 {
283   GList *elem;
284   guint width = 0;
285   GstPropsType type;
286
287   if (showname)
288     for (elem = props; elem; elem = g_list_next (elem)) {
289       GstPropsEntry *prop = elem->data;
290       const gchar *name = gst_props_entry_get_name (prop);
291
292       if (width < strlen (name))
293         width = strlen (name);
294     }
295
296   for (elem = props; elem; elem = g_list_next (elem)) {
297     GstPropsEntry *prop = elem->data;
298
299     string_append_indent (buf, indent);
300     if (showname) {
301       const gchar *name = gst_props_entry_get_name (prop);
302
303       g_string_append (buf, name);
304       string_append_indent (buf, 2 + width - strlen (name));
305     }
306
307     type = gst_props_entry_get_type (prop);
308     switch (type) {
309       case GST_PROPS_INT_TYPE:
310       {
311         gint val;
312         gst_props_entry_get_int (prop, &val);
313         g_string_append_printf (buf, "%d (int)\n", val);
314         break;
315       }
316       case GST_PROPS_INT_RANGE_TYPE:
317       {
318         gint min, max;
319         gst_props_entry_get_int_range (prop, &min, &max);
320         g_string_append_printf (buf, "%d - %d (int)\n", min, max);
321         break;
322       }
323       case GST_PROPS_FLOAT_TYPE:
324       {
325         gfloat val;
326         gst_props_entry_get_float (prop, &val);
327         g_string_append_printf (buf, "%f (float)\n", val);
328         break;
329       }
330       case GST_PROPS_FLOAT_RANGE_TYPE:
331       {
332         gfloat min, max;
333         gst_props_entry_get_float_range (prop, &min, &max);
334         g_string_append_printf (buf, "%f - %f (float)\n", min, max);
335         break;
336       }
337       case GST_PROPS_BOOLEAN_TYPE:
338       {
339         gboolean val;
340         gst_props_entry_get_boolean (prop, &val);
341         g_string_append_printf (buf, "%s\n", val ? "TRUE" : "FALSE");
342         break;
343       }
344       case GST_PROPS_STRING_TYPE:
345       {
346         const gchar *val;
347         gst_props_entry_get_string (prop, &val);
348         g_string_append_printf (buf, "\"%s\"\n", val);
349         break;
350       }
351       case GST_PROPS_FOURCC_TYPE:
352       {
353         guint32 val;
354         gst_props_entry_get_fourcc_int (prop, &val);
355         g_string_append_printf (buf, "'%c%c%c%c' (fourcc)\n",
356                                 (gchar)( val        & 0xff),
357                                 (gchar)((val >> 8)  & 0xff),
358                                 (gchar)((val >> 16) & 0xff),
359                                 (gchar)((val >> 24) & 0xff));
360         break;
361       }
362       case GST_PROPS_LIST_TYPE:
363       {
364         const GList *list;
365         gst_props_entry_get_list (prop, &list);
366         gst_print_props (buf, indent + 2, (GList *)list, FALSE);
367         break;
368       }
369       default:
370         g_string_append_printf (buf, "unknown proptype %d\n", type);
371         break;
372     }
373   }
374 }
375
376 /**
377  * gst_print_pad_caps:
378  * @buf: the buffer to print the caps in
379  * @indent: initial indentation
380  * @pad: the pad to print the caps from
381  *
382  * Write the pad capabilities in a human readable format into
383  * the given GString.
384  */
385 void
386 gst_print_pad_caps (GString * buf, gint indent, GstPad * pad)
387 {
388   GstRealPad *realpad;
389   GstCaps *caps;
390
391   realpad = GST_PAD_REALIZE (pad);
392   caps = realpad->caps;
393
394   if (!caps) {
395     string_append_indent (buf, indent);
396     g_string_printf (buf, "%s:%s has no capabilities", GST_DEBUG_PAD_NAME (pad));
397   }
398   else {
399     gint capx = 0;
400
401     while (caps) {
402       GstType *type;
403
404       string_append_indent (buf, indent);
405       g_string_append_printf (buf, "Cap[%d]: %s\n", capx++, caps->name);
406
407       type = gst_type_find_by_id (caps->id);
408       string_append_indent (buf, indent + 2);
409       g_string_append_printf (buf, "MIME type: %s\n", type->mime ? type->mime : "unknown/unknown");
410
411       if (caps->properties)
412         gst_print_props (buf, indent + 4, caps->properties->properties, TRUE);
413
414       caps = caps->next;
415     }
416   }
417 }
418
419 /**
420  * gst_print_element_args:
421  * @buf: the buffer to print the args in
422  * @indent: initial indentation
423  * @element: the element to print the args of
424  *
425  * Print the element argument in a human readable format in the given
426  * GString.
427  */
428 void
429 gst_print_element_args (GString * buf, gint indent, GstElement * element)
430 {
431   guint width;
432   GValue value = { 0, }; /* the important thing is that value.type = 0 */
433   gchar *str = 0;
434   GParamSpec *spec, **specs, **walk;
435
436   specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element), NULL);
437   
438   width = 0;
439   for (walk = specs; *walk; walk++) {
440     spec = *walk;
441     if (width < strlen (spec->name))
442       width = strlen (spec->name);
443   }
444
445   for (walk = specs; *walk; walk++) {
446     spec = *walk;
447     
448     if (spec->flags & G_PARAM_READABLE) {
449       g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE (spec));
450       g_object_get_property (G_OBJECT (element), spec->name, &value);
451       str = g_strdup_value_contents (&value);
452       g_value_unset(&value);
453     } else {
454       str = g_strdup ("Parameter not readable.");
455     }
456
457     string_append_indent (buf, indent);
458     g_string_append (buf, spec->name);
459     string_append_indent (buf, 2 + width - strlen (spec->name));
460     g_string_append (buf, str);
461     g_string_append_c (buf, '\n');
462     
463     g_free (str);
464   }
465
466   g_free (specs);
467 }