Merge branch 'master' into 0.11
[platform/upstream/gstreamer.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 #define gst_auto_audio_sink_parent_class parent_class
65 G_DEFINE_TYPE (GstAutoAudioSink, gst_auto_audio_sink, GST_TYPE_BIN);
66
67 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
68     GST_PAD_SINK,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS_ANY);
71
72 static void
73 gst_auto_audio_sink_class_init (GstAutoAudioSinkClass * klass)
74 {
75   GObjectClass *gobject_class;
76   GstElementClass *eklass;
77
78   gobject_class = G_OBJECT_CLASS (klass);
79   eklass = GST_ELEMENT_CLASS (klass);
80
81   gobject_class->dispose = (GObjectFinalizeFunc) gst_auto_audio_sink_dispose;
82   gobject_class->set_property = gst_auto_audio_sink_set_property;
83   gobject_class->get_property = gst_auto_audio_sink_get_property;
84
85   eklass->change_state = GST_DEBUG_FUNCPTR (gst_auto_audio_sink_change_state);
86
87   /**
88    * GstAutoAudioSink:filter-caps
89    *
90    * This property will filter out candidate sinks that can handle the specified
91    * caps. By default only audio sinks that support raw floating point and
92    * integer audio are selected.
93    *
94    * This property can only be set before the element goes to the READY state.
95    *
96    * Since: 0.10.7
97    **/
98   g_object_class_install_property (gobject_class, PROP_CAPS,
99       g_param_spec_boxed ("filter-caps", "Filter caps",
100           "Filter sink candidates using these caps.", GST_TYPE_CAPS,
101           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
102
103   gst_element_class_add_pad_template (eklass,
104       gst_static_pad_template_get (&sink_template));
105
106   gst_element_class_set_details_simple (eklass, "Auto audio sink",
107       "Sink/Audio",
108       "Wrapper audio sink for automatically detected audio sink",
109       "Jan Schmidt <thaytan@noraisin.net>");
110 }
111
112 static void
113 gst_auto_audio_sink_dispose (GstAutoAudioSink * sink)
114 {
115   gst_auto_audio_sink_clear_kid (sink);
116
117   if (sink->filter_caps)
118     gst_caps_unref (sink->filter_caps);
119   sink->filter_caps = NULL;
120
121   G_OBJECT_CLASS (parent_class)->dispose ((GObject *) sink);
122 }
123
124 static void
125 gst_auto_audio_sink_clear_kid (GstAutoAudioSink * sink)
126 {
127   if (sink->kid) {
128     gst_element_set_state (sink->kid, GST_STATE_NULL);
129     gst_bin_remove (GST_BIN (sink), sink->kid);
130     sink->kid = NULL;
131     /* Don't lose the SINK flag */
132     GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
133   }
134 }
135
136 /*
137  * Hack to make initial linking work; ideally, this'd work even when
138  * no target has been assigned to the ghostpad yet.
139  */
140 static void
141 gst_auto_audio_sink_reset (GstAutoAudioSink * sink)
142 {
143   GstPad *targetpad;
144
145   gst_auto_audio_sink_clear_kid (sink);
146
147   /* fakesink placeholder */
148   sink->kid = gst_element_factory_make ("fakesink", "tempsink");
149   gst_bin_add (GST_BIN (sink), sink->kid);
150
151   /* pad */
152   targetpad = gst_element_get_static_pad (sink->kid, "sink");
153   gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
154   gst_object_unref (targetpad);
155 }
156
157 static GstStaticCaps raw_caps = GST_STATIC_CAPS ("audio/x-raw");
158
159 static void
160 gst_auto_audio_sink_init (GstAutoAudioSink * sink)
161 {
162   sink->pad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
163   gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
164
165   gst_auto_audio_sink_reset (sink);
166
167   /* set the default raw audio caps */
168   sink->filter_caps = gst_static_caps_get (&raw_caps);
169
170   /* mark as sink */
171   GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
172 }
173
174 static gboolean
175 gst_auto_audio_sink_factory_filter (GstPluginFeature * feature, gpointer data)
176 {
177   guint rank;
178   const gchar *klass;
179
180   /* we only care about element factories */
181   if (!GST_IS_ELEMENT_FACTORY (feature))
182     return FALSE;
183
184   /* audio sinks */
185   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
186   if (!(strstr (klass, "Sink") && strstr (klass, "Audio")))
187     return FALSE;
188
189   /* only select elements with autoplugging rank */
190   rank = gst_plugin_feature_get_rank (feature);
191   if (rank < GST_RANK_MARGINAL)
192     return FALSE;
193
194   return TRUE;
195 }
196
197 static gint
198 gst_auto_audio_sink_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
199 {
200   gint diff;
201
202   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
203   if (diff != 0)
204     return diff;
205   return strcmp (gst_plugin_feature_get_name (f2),
206       gst_plugin_feature_get_name (f1));
207 }
208
209 static GstElement *
210 gst_auto_audio_sink_create_element_with_pretty_name (GstAutoAudioSink * sink,
211     GstElementFactory * factory)
212 {
213   GstElement *element;
214   gchar *name, *marker;
215
216   marker = g_strdup (GST_OBJECT_NAME (factory));
217   if (g_str_has_suffix (marker, "sink"))
218     marker[strlen (marker) - 4] = '\0';
219   if (g_str_has_prefix (marker, "gst"))
220     g_memmove (marker, marker + 3, strlen (marker + 3) + 1);
221   name = g_strdup_printf ("%s-actual-sink-%s", GST_OBJECT_NAME (sink), marker);
222   g_free (marker);
223
224   element = gst_element_factory_create (factory, name);
225   g_free (name);
226
227   return element;
228 }
229
230 static GstElement *
231 gst_auto_audio_sink_find_best (GstAutoAudioSink * sink)
232 {
233   GList *list, *item;
234   GstElement *choice = NULL;
235   GstMessage *message = NULL;
236   GSList *errors = NULL;
237   GstBus *bus = gst_bus_new ();
238   GstPad *el_pad = NULL;
239   GstCaps *el_caps = NULL;
240   gboolean no_match = TRUE;
241
242   list = gst_registry_feature_filter (gst_registry_get_default (),
243       (GstPluginFeatureFilter) gst_auto_audio_sink_factory_filter, FALSE, sink);
244   list = g_list_sort (list, (GCompareFunc) gst_auto_audio_sink_compare_ranks);
245
246   /* We don't treat sound server sinks special. Our policy is that sound
247    * server sinks that have a rank must not auto-spawn a daemon under any
248    * circumstances, so there's nothing for us to worry about here */
249   GST_LOG_OBJECT (sink, "Trying to find usable audio devices ...");
250
251   for (item = list; item != NULL; item = item->next) {
252     GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
253     GstElement *el;
254
255     if ((el = gst_auto_audio_sink_create_element_with_pretty_name (sink, f))) {
256       GstStateChangeReturn ret;
257
258       GST_DEBUG_OBJECT (sink, "Testing %s", GST_OBJECT_NAME (f));
259
260       /* If autoaudiosink has been provided with filter caps,
261        * accept only sinks that match with the filter caps */
262       if (sink->filter_caps) {
263         el_pad = gst_element_get_static_pad (GST_ELEMENT (el), "sink");
264         el_caps = gst_pad_get_caps (el_pad, NULL);
265         gst_object_unref (el_pad);
266         GST_DEBUG_OBJECT (sink,
267             "Checking caps: %" GST_PTR_FORMAT " vs. %" GST_PTR_FORMAT,
268             sink->filter_caps, el_caps);
269         no_match = !gst_caps_can_intersect (sink->filter_caps, el_caps);
270         gst_caps_unref (el_caps);
271
272         if (no_match) {
273           GST_DEBUG_OBJECT (sink, "Incompatible caps");
274           gst_object_unref (el);
275           continue;
276         } else {
277           GST_DEBUG_OBJECT (sink, "Found compatible caps");
278         }
279       }
280
281       gst_element_set_bus (el, bus);
282       ret = gst_element_set_state (el, GST_STATE_READY);
283       if (ret == GST_STATE_CHANGE_SUCCESS) {
284         GST_DEBUG_OBJECT (sink, "This worked!");
285         choice = el;
286         break;
287       }
288
289       /* collect all error messages */
290       while ((message = gst_bus_pop_filtered (bus, GST_MESSAGE_ERROR))) {
291         GST_DEBUG_OBJECT (sink, "error message %" GST_PTR_FORMAT, message);
292         errors = g_slist_append (errors, message);
293       }
294
295       gst_element_set_state (el, GST_STATE_NULL);
296       gst_object_unref (el);
297     }
298   }
299
300   GST_DEBUG_OBJECT (sink, "done trying");
301   if (!choice) {
302     if (errors) {
303       /* FIXME: we forward the first error for now; but later on it might make
304        * sense to actually analyse them */
305       gst_message_ref (GST_MESSAGE (errors->data));
306       GST_DEBUG_OBJECT (sink, "reposting message %p", errors->data);
307       gst_element_post_message (GST_ELEMENT (sink), GST_MESSAGE (errors->data));
308     } else {
309       /* send warning message to application and use a fakesink */
310       GST_ELEMENT_WARNING (sink, RESOURCE, NOT_FOUND, (NULL),
311           ("Failed to find a usable audio sink"));
312       choice = gst_element_factory_make ("fakesink", "fake-audio-sink");
313       if (g_object_class_find_property (G_OBJECT_GET_CLASS (choice), "sync"))
314         g_object_set (choice, "sync", TRUE, NULL);
315       gst_element_set_state (choice, GST_STATE_READY);
316     }
317   }
318   gst_object_unref (bus);
319   gst_plugin_feature_list_free (list);
320   g_slist_foreach (errors, (GFunc) gst_mini_object_unref, NULL);
321   g_slist_free (errors);
322
323   return choice;
324 }
325
326 static gboolean
327 gst_auto_audio_sink_detect (GstAutoAudioSink * sink)
328 {
329   GstElement *esink;
330   GstPad *targetpad;
331
332   gst_auto_audio_sink_clear_kid (sink);
333
334   /* find element */
335   GST_DEBUG_OBJECT (sink, "Creating new kid");
336   if (!(esink = gst_auto_audio_sink_find_best (sink)))
337     goto no_sink;
338
339   sink->kid = esink;
340   /* Ensure the child is brought up to the right state to match the parent
341    * although it's currently always in READY and 
342    * we're always doing NULL->READY. */
343   if (GST_STATE (sink->kid) < GST_STATE (sink))
344     gst_element_set_state (sink->kid, GST_STATE (sink));
345
346   gst_bin_add (GST_BIN (sink), esink);
347
348   /* attach ghost pad */
349   GST_DEBUG_OBJECT (sink, "Re-assigning ghostpad");
350   targetpad = gst_element_get_static_pad (sink->kid, "sink");
351   if (!gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad))
352     goto target_failed;
353
354   gst_object_unref (targetpad);
355   GST_DEBUG_OBJECT (sink, "done changing auto audio sink");
356
357   return TRUE;
358
359   /* ERRORS */
360 no_sink:
361   {
362     GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
363         ("Failed to find a supported audio sink"));
364     return FALSE;
365   }
366 target_failed:
367   {
368     GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
369         ("Failed to set target pad"));
370     gst_object_unref (targetpad);
371     return FALSE;
372   }
373 }
374
375 static GstStateChangeReturn
376 gst_auto_audio_sink_change_state (GstElement * element,
377     GstStateChange transition)
378 {
379   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
380   GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (element);
381
382   switch (transition) {
383     case GST_STATE_CHANGE_NULL_TO_READY:
384       if (!gst_auto_audio_sink_detect (sink))
385         return GST_STATE_CHANGE_FAILURE;
386       break;
387     default:
388       break;
389   }
390
391   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
392   if (ret == GST_STATE_CHANGE_FAILURE)
393     return ret;
394
395   switch (transition) {
396     case GST_STATE_CHANGE_READY_TO_NULL:
397       gst_auto_audio_sink_reset (sink);
398       break;
399     default:
400       break;
401   }
402
403   return ret;
404 }
405
406 static void
407 gst_auto_audio_sink_set_property (GObject * object, guint prop_id,
408     const GValue * value, GParamSpec * pspec)
409 {
410   GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (object);
411
412   switch (prop_id) {
413     case PROP_CAPS:
414       if (sink->filter_caps)
415         gst_caps_unref (sink->filter_caps);
416       sink->filter_caps = gst_caps_copy (gst_value_get_caps (value));
417       break;
418     default:
419       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
420       break;
421   }
422 }
423
424 static void
425 gst_auto_audio_sink_get_property (GObject * object, guint prop_id,
426     GValue * value, GParamSpec * pspec)
427 {
428   GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (object);
429
430   switch (prop_id) {
431     case PROP_CAPS:{
432       gst_value_set_caps (value, sink->filter_caps);
433       break;
434     }
435     default:
436       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
437       break;
438   }
439 }