Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / autodetect / gstautoaudiosink.c
1 /* GStreamer
2  * (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
3  * (c) 2006 Jan Schmidt <thaytan@noraisin.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * SECTION:element-autoaudiosink
23  * @see_also: autovideosink, alsasink, osssink
24  *
25  * autoaudiosink is an audio sink that automatically detects an appropriate
26  * audio sink to use.  It does so by scanning the registry for all elements
27  * that have <quote>Sink</quote> and <quote>Audio</quote> in the class field
28  * of their element information, and also have a non-zero autoplugging rank.
29  *
30  * <refsect2>
31  * <title>Example launch line</title>
32  * |[
33  * gst-launch -v -m audiotestsrc ! audioconvert ! audioresample ! autoaudiosink
34  * ]|
35  * </refsect2>
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <string.h>
43
44 #include "gstautoaudiosink.h"
45 #include "gstautodetect.h"
46
47 /* Properties */
48 enum
49 {
50   PROP_0,
51   PROP_CAPS,
52 };
53
54 static GstStateChangeReturn
55 gst_auto_audio_sink_change_state (GstElement * element,
56     GstStateChange transition);
57 static void gst_auto_audio_sink_dispose (GstAutoAudioSink * sink);
58 static void gst_auto_audio_sink_clear_kid (GstAutoAudioSink * sink);
59 static void gst_auto_audio_sink_set_property (GObject * object, guint prop_id,
60     const GValue * value, GParamSpec * pspec);
61 static void gst_auto_audio_sink_get_property (GObject * object, guint prop_id,
62     GValue * value, GParamSpec * pspec);
63
64 GST_BOILERPLATE (GstAutoAudioSink, gst_auto_audio_sink, GstBin, GST_TYPE_BIN);
65
66 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
67     GST_PAD_SINK,
68     GST_PAD_ALWAYS,
69     GST_STATIC_CAPS_ANY);
70
71 static void
72 gst_auto_audio_sink_base_init (gpointer klass)
73 {
74   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
75
76   gst_element_class_add_static_pad_template (eklass, &sink_template);
77
78   gst_element_class_set_details_simple (eklass, "Auto audio sink",
79       "Sink/Audio",
80       "Wrapper audio sink for automatically detected audio sink",
81       "Jan Schmidt <thaytan@noraisin.net>");
82 }
83
84 static void
85 gst_auto_audio_sink_class_init (GstAutoAudioSinkClass * klass)
86 {
87   GObjectClass *gobject_class;
88   GstElementClass *eklass;
89
90   gobject_class = G_OBJECT_CLASS (klass);
91   eklass = GST_ELEMENT_CLASS (klass);
92
93   gobject_class->dispose = (GObjectFinalizeFunc) gst_auto_audio_sink_dispose;
94   gobject_class->set_property = gst_auto_audio_sink_set_property;
95   gobject_class->get_property = gst_auto_audio_sink_get_property;
96
97   eklass->change_state = GST_DEBUG_FUNCPTR (gst_auto_audio_sink_change_state);
98
99   /**
100    * GstAutoAudioSink:filter-caps
101    *
102    * This property will filter out candidate sinks that can handle the specified
103    * caps. By default only audio sinks that support raw floating point and
104    * integer audio are selected.
105    *
106    * This property can only be set before the element goes to the READY state.
107    *
108    * Since: 0.10.7
109    **/
110   g_object_class_install_property (gobject_class, PROP_CAPS,
111       g_param_spec_boxed ("filter-caps", "Filter caps",
112           "Filter sink candidates using these caps.", GST_TYPE_CAPS,
113           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
114 }
115
116 static void
117 gst_auto_audio_sink_dispose (GstAutoAudioSink * sink)
118 {
119   gst_auto_audio_sink_clear_kid (sink);
120
121   if (sink->filter_caps)
122     gst_caps_unref (sink->filter_caps);
123   sink->filter_caps = NULL;
124
125   G_OBJECT_CLASS (parent_class)->dispose ((GObject *) sink);
126 }
127
128 static void
129 gst_auto_audio_sink_clear_kid (GstAutoAudioSink * sink)
130 {
131   if (sink->kid) {
132     gst_element_set_state (sink->kid, GST_STATE_NULL);
133     gst_bin_remove (GST_BIN (sink), sink->kid);
134     sink->kid = NULL;
135     /* Don't lose the SINK flag */
136     GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
137   }
138 }
139
140 /*
141  * Hack to make initial linking work; ideally, this'd work even when
142  * no target has been assigned to the ghostpad yet.
143  */
144 static void
145 gst_auto_audio_sink_reset (GstAutoAudioSink * sink)
146 {
147   GstPad *targetpad;
148
149   gst_auto_audio_sink_clear_kid (sink);
150
151   /* fakesink placeholder */
152   sink->kid = gst_element_factory_make ("fakesink", "tempsink");
153   gst_bin_add (GST_BIN (sink), sink->kid);
154
155   /* pad */
156   targetpad = gst_element_get_static_pad (sink->kid, "sink");
157   gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
158   gst_object_unref (targetpad);
159 }
160
161 static GstStaticCaps raw_caps =
162     GST_STATIC_CAPS ("audio/x-raw-int; audio/x-raw-float");
163
164 static void
165 gst_auto_audio_sink_init (GstAutoAudioSink * sink,
166     GstAutoAudioSinkClass * g_class)
167 {
168   sink->pad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
169   gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
170
171   gst_auto_audio_sink_reset (sink);
172
173   /* set the default raw audio caps */
174   sink->filter_caps = gst_static_caps_get (&raw_caps);
175
176   /* mark as sink */
177   GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
178 }
179
180 static gboolean
181 gst_auto_audio_sink_factory_filter (GstPluginFeature * feature, gpointer data)
182 {
183   guint rank;
184   const gchar *klass;
185
186   /* we only care about element factories */
187   if (!GST_IS_ELEMENT_FACTORY (feature))
188     return FALSE;
189
190   /* audio sinks */
191   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
192   if (!(strstr (klass, "Sink") && strstr (klass, "Audio")))
193     return FALSE;
194
195   /* only select elements with autoplugging rank */
196   rank = gst_plugin_feature_get_rank (feature);
197   if (rank < GST_RANK_MARGINAL)
198     return FALSE;
199
200   return TRUE;
201 }
202
203 static gint
204 gst_auto_audio_sink_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
205 {
206   gint diff;
207
208   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
209   if (diff != 0)
210     return diff;
211   return strcmp (gst_plugin_feature_get_name (f2),
212       gst_plugin_feature_get_name (f1));
213 }
214
215 static GstElement *
216 gst_auto_audio_sink_create_element_with_pretty_name (GstAutoAudioSink * sink,
217     GstElementFactory * factory)
218 {
219   GstElement *element;
220   gchar *name, *marker;
221
222   marker = g_strdup (GST_PLUGIN_FEATURE (factory)->name);
223   if (g_str_has_suffix (marker, "sink"))
224     marker[strlen (marker) - 4] = '\0';
225   if (g_str_has_prefix (marker, "gst"))
226     g_memmove (marker, marker + 3, strlen (marker + 3) + 1);
227   name = g_strdup_printf ("%s-actual-sink-%s", GST_OBJECT_NAME (sink), marker);
228   g_free (marker);
229
230   element = gst_element_factory_create (factory, name);
231   g_free (name);
232
233   return element;
234 }
235
236 static GstElement *
237 gst_auto_audio_sink_find_best (GstAutoAudioSink * sink)
238 {
239   GList *list, *item;
240   GstElement *choice = NULL;
241   GstMessage *message = NULL;
242   GSList *errors = NULL;
243   GstBus *bus = gst_bus_new ();
244   GstPad *el_pad = NULL;
245   GstCaps *el_caps = NULL;
246   gboolean no_match = TRUE;
247
248   list = gst_registry_feature_filter (gst_registry_get_default (),
249       (GstPluginFeatureFilter) gst_auto_audio_sink_factory_filter, FALSE, sink);
250   list = g_list_sort (list, (GCompareFunc) gst_auto_audio_sink_compare_ranks);
251
252   /* We don't treat sound server sinks special. Our policy is that sound
253    * server sinks that have a rank must not auto-spawn a daemon under any
254    * circumstances, so there's nothing for us to worry about here */
255   GST_LOG_OBJECT (sink, "Trying to find usable audio devices ...");
256
257   for (item = list; item != NULL; item = item->next) {
258     GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
259     GstElement *el;
260
261     if ((el = gst_auto_audio_sink_create_element_with_pretty_name (sink, f))) {
262       GstStateChangeReturn ret;
263
264       GST_DEBUG_OBJECT (sink, "Testing %s", GST_PLUGIN_FEATURE (f)->name);
265
266       /* If autoaudiosink has been provided with filter caps,
267        * accept only sinks that match with the filter caps */
268       if (sink->filter_caps) {
269         el_pad = gst_element_get_static_pad (GST_ELEMENT (el), "sink");
270         el_caps = gst_pad_get_caps (el_pad);
271         gst_object_unref (el_pad);
272         GST_DEBUG_OBJECT (sink,
273             "Checking caps: %" GST_PTR_FORMAT " vs. %" GST_PTR_FORMAT,
274             sink->filter_caps, el_caps);
275         no_match = !gst_caps_can_intersect (sink->filter_caps, el_caps);
276         gst_caps_unref (el_caps);
277
278         if (no_match) {
279           GST_DEBUG_OBJECT (sink, "Incompatible caps");
280           gst_object_unref (el);
281           continue;
282         } else {
283           GST_DEBUG_OBJECT (sink, "Found compatible caps");
284         }
285       }
286
287       gst_element_set_bus (el, bus);
288       ret = gst_element_set_state (el, GST_STATE_READY);
289       if (ret == GST_STATE_CHANGE_SUCCESS) {
290         GST_DEBUG_OBJECT (sink, "This worked!");
291         choice = el;
292         break;
293       }
294
295       /* collect all error messages */
296       while ((message = gst_bus_pop_filtered (bus, GST_MESSAGE_ERROR))) {
297         GST_DEBUG_OBJECT (sink, "error message %" GST_PTR_FORMAT, message);
298         errors = g_slist_append (errors, message);
299       }
300
301       gst_element_set_state (el, GST_STATE_NULL);
302       gst_object_unref (el);
303     }
304   }
305
306   GST_DEBUG_OBJECT (sink, "done trying");
307   if (!choice) {
308     if (errors) {
309       /* FIXME: we forward the first error for now; but later on it might make
310        * sense to actually analyse them */
311       gst_message_ref (GST_MESSAGE (errors->data));
312       GST_DEBUG_OBJECT (sink, "reposting message %p", errors->data);
313       gst_element_post_message (GST_ELEMENT (sink), GST_MESSAGE (errors->data));
314     } else {
315       /* send warning message to application and use a fakesink */
316       GST_ELEMENT_WARNING (sink, RESOURCE, NOT_FOUND, (NULL),
317           ("Failed to find a usable audio sink"));
318       choice = gst_element_factory_make ("fakesink", "fake-audio-sink");
319       if (g_object_class_find_property (G_OBJECT_GET_CLASS (choice), "sync"))
320         g_object_set (choice, "sync", TRUE, NULL);
321       gst_element_set_state (choice, GST_STATE_READY);
322     }
323   }
324   gst_object_unref (bus);
325   gst_plugin_feature_list_free (list);
326   g_slist_foreach (errors, (GFunc) gst_mini_object_unref, NULL);
327   g_slist_free (errors);
328
329   return choice;
330 }
331
332 static gboolean
333 gst_auto_audio_sink_detect (GstAutoAudioSink * sink)
334 {
335   GstElement *esink;
336   GstPad *targetpad;
337
338   gst_auto_audio_sink_clear_kid (sink);
339
340   /* find element */
341   GST_DEBUG_OBJECT (sink, "Creating new kid");
342   if (!(esink = gst_auto_audio_sink_find_best (sink)))
343     goto no_sink;
344
345   sink->kid = esink;
346   /* Ensure the child is brought up to the right state to match the parent
347    * although it's currently always in READY and 
348    * we're always doing NULL->READY. */
349   if (GST_STATE (sink->kid) < GST_STATE (sink))
350     gst_element_set_state (sink->kid, GST_STATE (sink));
351
352   gst_bin_add (GST_BIN (sink), esink);
353
354   /* attach ghost pad */
355   GST_DEBUG_OBJECT (sink, "Re-assigning ghostpad");
356   targetpad = gst_element_get_static_pad (sink->kid, "sink");
357   if (!gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad))
358     goto target_failed;
359
360   gst_object_unref (targetpad);
361   GST_DEBUG_OBJECT (sink, "done changing auto audio sink");
362
363   return TRUE;
364
365   /* ERRORS */
366 no_sink:
367   {
368     GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
369         ("Failed to find a supported audio sink"));
370     return FALSE;
371   }
372 target_failed:
373   {
374     GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
375         ("Failed to set target pad"));
376     gst_object_unref (targetpad);
377     return FALSE;
378   }
379 }
380
381 static GstStateChangeReturn
382 gst_auto_audio_sink_change_state (GstElement * element,
383     GstStateChange transition)
384 {
385   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
386   GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (element);
387
388   switch (transition) {
389     case GST_STATE_CHANGE_NULL_TO_READY:
390       if (!gst_auto_audio_sink_detect (sink))
391         return GST_STATE_CHANGE_FAILURE;
392       break;
393     default:
394       break;
395   }
396
397   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
398   if (ret == GST_STATE_CHANGE_FAILURE)
399     return ret;
400
401   switch (transition) {
402     case GST_STATE_CHANGE_READY_TO_NULL:
403       gst_auto_audio_sink_reset (sink);
404       break;
405     default:
406       break;
407   }
408
409   return ret;
410 }
411
412 static void
413 gst_auto_audio_sink_set_property (GObject * object, guint prop_id,
414     const GValue * value, GParamSpec * pspec)
415 {
416   GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (object);
417
418   switch (prop_id) {
419     case PROP_CAPS:
420       if (sink->filter_caps)
421         gst_caps_unref (sink->filter_caps);
422       sink->filter_caps = gst_caps_copy (gst_value_get_caps (value));
423       break;
424     default:
425       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
426       break;
427   }
428 }
429
430 static void
431 gst_auto_audio_sink_get_property (GObject * object, guint prop_id,
432     GValue * value, GParamSpec * pspec)
433 {
434   GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (object);
435
436   switch (prop_id) {
437     case PROP_CAPS:{
438       gst_value_set_caps (value, sink->filter_caps);
439       break;
440     }
441     default:
442       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
443       break;
444   }
445 }