Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / icles / test-oss4.c
1 /* GStreamer OSS4 audio tests
2  * Copyright (C) 2007-2008 Tim-Philipp Müller <tim centricular net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <gst/gst.h>
28 #include <gst/interfaces/propertyprobe.h>
29 #include <gst/interfaces/mixer.h>
30
31 static gboolean opt_show_mixer_messages = FALSE;
32
33 #define WAIT_TIME  60.0         /* in seconds */
34
35 static void
36 show_mixer_messages (GstElement * element)
37 {
38   GstMessage *msg;
39   GstBus *bus;
40   GTimer *t;
41
42   t = g_timer_new ();
43
44   bus = gst_bus_new ();
45   gst_element_set_bus (element, bus);
46
47   g_print ("\nShowing mixer messages for %u seconds ...\n", (guint) WAIT_TIME);
48
49   while (g_timer_elapsed (t, NULL) < WAIT_TIME) {
50     gdouble remaining = WAIT_TIME - g_timer_elapsed (t, NULL);
51     gint64 maxwait =
52         GST_SECOND * gst_util_gdouble_to_guint64 (MAX (0.0, remaining));
53     gchar *s = NULL;
54
55     msg = gst_bus_timed_pop (bus, maxwait);
56     if (!msg)
57       break;
58
59     if (msg->structure)
60       s = gst_structure_to_string (msg->structure);
61     g_print ("%s message: %s\n", GST_MESSAGE_TYPE_NAME (msg), s);
62     gst_message_unref (msg);
63     g_free (s);
64   }
65
66   gst_element_set_bus (element, NULL);
67   gst_object_unref (bus);
68   g_timer_destroy (t);
69 }
70
71 static void
72 probe_mixer_tracks (GstElement * element)
73 {
74   const GList *tracks, *t;
75   GstMixer *mixer;
76   guint count;
77
78   if (!GST_IS_MIXER (element))
79     return;
80
81   mixer = GST_MIXER (element);
82   tracks = gst_mixer_list_tracks (mixer);
83   count = g_list_length ((GList *) tracks);
84   g_print ("  %d mixer tracks%c\n", count, (count == 0) ? '.' : ':');
85
86   for (t = tracks; t != NULL; t = t->next) {
87     GstMixerTrack *track;
88     gchar *label = NULL;
89     guint flags = 0;
90
91     track = GST_MIXER_TRACK (t->data);
92     g_object_get (track, "label", &label, "flags", &flags, NULL);
93
94     if (GST_IS_MIXER_OPTIONS (track)) {
95       GString *s;
96       GList *vals, *v;
97
98       vals = gst_mixer_options_get_values (GST_MIXER_OPTIONS (track));
99       s = g_string_new ("options: ");
100       for (v = vals; v != NULL; v = v->next) {
101         if (v->prev != NULL)
102           g_string_append (s, ", ");
103         g_string_append (s, (const gchar *) v->data);
104       }
105
106       g_print ("    [%s] flags=0x%08x, %s\n", label, flags, s->str);
107       g_string_free (s, TRUE);
108     } else if (track->num_channels == 0) {
109       g_print ("    [%s] flags=0x%08x, switch\n", label, flags);
110     } else if (track->num_channels > 0) {
111       g_print ("    [%s] flags=0x%08x, slider (%d channels)\n", label, flags,
112           track->num_channels);
113     } else {
114       g_print ("    [%s] flags=0x%08x, UNKNOWN TYPE\n", label, flags);
115     }
116
117     g_free (label);
118   }
119
120   /* for testing the mixer watch thread / auto-notifications */
121   if (strstr (GST_ELEMENT_NAME (element), "mixer") != NULL &&
122       opt_show_mixer_messages) {
123     show_mixer_messages (element);
124   }
125 }
126
127 static void
128 probe_pad (GstElement * element, const gchar * pad_name)
129 {
130   GstCaps *caps = NULL;
131   GstPad *pad;
132   guint i;
133
134   pad = gst_element_get_static_pad (element, pad_name);
135   if (pad == NULL)
136     return;
137
138   caps = gst_pad_get_caps (pad);
139   g_return_if_fail (caps != NULL);
140
141   for (i = 0; i < gst_caps_get_size (caps); ++i) {
142     gchar *s;
143
144     s = gst_structure_to_string (gst_caps_get_structure (caps, i));
145     g_print ("  %4s[%d]: %s\n", GST_PAD_NAME (pad), i, s);
146     g_free (s);
147   }
148   gst_caps_unref (caps);
149   gst_object_unref (pad);
150 }
151
152 static void
153 probe_details (GstElement * element)
154 {
155   GstStateChangeReturn ret;
156
157   ret = gst_element_set_state (element, GST_STATE_READY);
158   if (ret == GST_STATE_CHANGE_FAILURE) {
159     g_print ("Could not set element %s to READY.", GST_ELEMENT_NAME (element));
160     return;
161   }
162
163   probe_pad (element, "sink");
164   probe_pad (element, "src");
165
166   probe_mixer_tracks (element);
167
168   gst_element_set_state (element, GST_STATE_NULL);
169 }
170
171 static void
172 probe_element (const gchar * name)
173 {
174   GstPropertyProbe *probe;
175   GValueArray *arr;
176   GstElement *element;
177   gchar *devname = NULL;
178   gint i;
179
180   element = gst_element_factory_make (name, name);
181
182   /* make sure we don't deadlock on GST_ELEMENT_ERROR or do other silly things
183    * if we try to query the "device-name" property when the device isn't open */
184   g_object_set (element, "device", "/dev/does/not/exist", NULL);
185   g_object_get (element, "device-name", &devname, NULL);
186   GST_LOG ("devname: '%s'", GST_STR_NULL (devname));
187   g_assert (devname == NULL || *devname == '\0');
188
189   /* and now for real */
190
191   probe = GST_PROPERTY_PROBE (element);
192   arr = gst_property_probe_probe_and_get_values_name (probe, "device");
193
194   for (i = 0; arr != NULL && i < arr->n_values; ++i) {
195     GValue *val;
196     gchar *dev_name = NULL;
197
198     g_print ("\n");
199     /* we assume the element supports getting the device-name in NULL state */
200     val = g_value_array_get_nth (arr, i);
201     g_object_set (element, "device", g_value_get_string (val), NULL);
202     g_object_get (element, "device-name", &dev_name, NULL);
203     g_print ("%-10s device[%d] = %s (%s)\n", GST_OBJECT_NAME (element),
204         i, g_value_get_string (val), dev_name);
205     if (strstr (dev_name, "/usb")) {
206       g_print ("\n\nWARNING: going to probe USB audio device. OSS4 USB support"
207           " is still\npretty shaky, so bad things may happen (e.g. kernel "
208           "lockup).\nPress Control-C NOW if you don't want to continue. "
209           "(waiting 5secs)\n\n");
210       g_usleep (5 * G_USEC_PER_SEC);
211     }
212     g_free (dev_name);
213
214     probe_details (element);
215   }
216
217   if (arr) {
218     g_value_array_free (arr);
219   }
220
221   gst_object_unref (element);
222 }
223
224 int
225 main (int argc, char **argv)
226 {
227   GOptionEntry options[] = {
228     {"show-mixer-messages", 'm', 0, G_OPTION_ARG_NONE, &opt_show_mixer_messages,
229         "For mixer elements, wait 60 seconds and show any mixer messages "
230           "(for debugging auto-notifications)", NULL},
231     {NULL,}
232   };
233   GOptionContext *ctx;
234   GError *err = NULL;
235
236 #if !GLIB_CHECK_VERSION (2, 31, 0)
237   if (!g_thread_supported ())
238     g_thread_init (NULL);
239 #endif
240
241   ctx = g_option_context_new ("");
242   g_option_context_add_main_entries (ctx, options, NULL);
243   g_option_context_add_group (ctx, gst_init_get_option_group ());
244   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
245     g_print ("Error initializing: %s\n", err->message);
246     exit (1);
247   }
248   g_option_context_free (ctx);
249
250   probe_element ("oss4sink");
251   probe_element ("oss4src");
252   probe_element ("oss4mixer");
253
254   return 0;
255 }