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