2 * (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
3 * (c) 2006 Jan Schmidt <thaytan@noraisin.net>
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.
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.
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.
22 * SECTION:element-autoaudiosink
23 * @see_also: autovideosink, alsasink, osssink
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.
31 * <title>Example launch line</title>
33 * gst-launch -v -m audiotestsrc ! audioconvert ! audioresample ! autoaudiosink
44 #include "gstautoaudiosink.h"
45 #include "gstautodetect.h"
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);
64 #define gst_auto_audio_sink_parent_class parent_class
65 G_DEFINE_TYPE (GstAutoAudioSink, gst_auto_audio_sink, GST_TYPE_BIN);
67 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
73 gst_auto_audio_sink_class_init (GstAutoAudioSinkClass * klass)
75 GObjectClass *gobject_class;
76 GstElementClass *eklass;
78 gobject_class = G_OBJECT_CLASS (klass);
79 eklass = GST_ELEMENT_CLASS (klass);
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;
85 eklass->change_state = GST_DEBUG_FUNCPTR (gst_auto_audio_sink_change_state);
88 * GstAutoAudioSink:filter-caps
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.
94 * This property can only be set before the element goes to the READY state.
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));
103 gst_element_class_add_pad_template (eklass,
104 gst_static_pad_template_get (&sink_template));
106 gst_element_class_set_details_simple (eklass, "Auto audio sink",
108 "Wrapper audio sink for automatically detected audio sink",
109 "Jan Schmidt <thaytan@noraisin.net>");
113 gst_auto_audio_sink_dispose (GstAutoAudioSink * sink)
115 gst_auto_audio_sink_clear_kid (sink);
117 if (sink->filter_caps)
118 gst_caps_unref (sink->filter_caps);
119 sink->filter_caps = NULL;
121 G_OBJECT_CLASS (parent_class)->dispose ((GObject *) sink);
125 gst_auto_audio_sink_clear_kid (GstAutoAudioSink * sink)
128 gst_element_set_state (sink->kid, GST_STATE_NULL);
129 gst_bin_remove (GST_BIN (sink), sink->kid);
131 /* Don't lose the SINK flag */
132 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
137 * Hack to make initial linking work; ideally, this'd work even when
138 * no target has been assigned to the ghostpad yet.
141 gst_auto_audio_sink_reset (GstAutoAudioSink * sink)
145 gst_auto_audio_sink_clear_kid (sink);
147 /* fakesink placeholder */
148 sink->kid = gst_element_factory_make ("fakesink", "tempsink");
149 gst_bin_add (GST_BIN (sink), sink->kid);
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);
157 static GstStaticCaps raw_caps =
158 GST_STATIC_CAPS ("audio/x-raw-int; audio/x-raw-float");
161 gst_auto_audio_sink_init (GstAutoAudioSink * sink)
163 sink->pad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
164 gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
166 gst_auto_audio_sink_reset (sink);
168 /* set the default raw audio caps */
169 sink->filter_caps = gst_static_caps_get (&raw_caps);
172 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
176 gst_auto_audio_sink_factory_filter (GstPluginFeature * feature, gpointer data)
181 /* we only care about element factories */
182 if (!GST_IS_ELEMENT_FACTORY (feature))
186 klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
187 if (!(strstr (klass, "Sink") && strstr (klass, "Audio")))
190 /* only select elements with autoplugging rank */
191 rank = gst_plugin_feature_get_rank (feature);
192 if (rank < GST_RANK_MARGINAL)
199 gst_auto_audio_sink_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
203 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
206 return strcmp (gst_plugin_feature_get_name (f2),
207 gst_plugin_feature_get_name (f1));
211 gst_auto_audio_sink_create_element_with_pretty_name (GstAutoAudioSink * sink,
212 GstElementFactory * factory)
215 gchar *name, *marker;
217 marker = g_strdup (GST_OBJECT_NAME (factory));
218 if (g_str_has_suffix (marker, "sink"))
219 marker[strlen (marker) - 4] = '\0';
220 if (g_str_has_prefix (marker, "gst"))
221 g_memmove (marker, marker + 3, strlen (marker + 3) + 1);
222 name = g_strdup_printf ("%s-actual-sink-%s", GST_OBJECT_NAME (sink), marker);
225 element = gst_element_factory_create (factory, name);
232 gst_auto_audio_sink_find_best (GstAutoAudioSink * sink)
235 GstElement *choice = NULL;
236 GstMessage *message = NULL;
237 GSList *errors = NULL;
238 GstBus *bus = gst_bus_new ();
239 GstPad *el_pad = NULL;
240 GstCaps *el_caps = NULL;
241 gboolean no_match = TRUE;
243 list = gst_registry_feature_filter (gst_registry_get_default (),
244 (GstPluginFeatureFilter) gst_auto_audio_sink_factory_filter, FALSE, sink);
245 list = g_list_sort (list, (GCompareFunc) gst_auto_audio_sink_compare_ranks);
247 /* We don't treat sound server sinks special. Our policy is that sound
248 * server sinks that have a rank must not auto-spawn a daemon under any
249 * circumstances, so there's nothing for us to worry about here */
250 GST_LOG_OBJECT (sink, "Trying to find usable audio devices ...");
252 for (item = list; item != NULL; item = item->next) {
253 GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
256 if ((el = gst_auto_audio_sink_create_element_with_pretty_name (sink, f))) {
257 GstStateChangeReturn ret;
259 GST_DEBUG_OBJECT (sink, "Testing %s", GST_OBJECT_NAME (f));
261 /* If autoaudiosink has been provided with filter caps,
262 * accept only sinks that match with the filter caps */
263 if (sink->filter_caps) {
264 el_pad = gst_element_get_static_pad (GST_ELEMENT (el), "sink");
265 el_caps = gst_pad_get_caps (el_pad, NULL);
266 gst_object_unref (el_pad);
267 GST_DEBUG_OBJECT (sink,
268 "Checking caps: %" GST_PTR_FORMAT " vs. %" GST_PTR_FORMAT,
269 sink->filter_caps, el_caps);
270 no_match = !gst_caps_can_intersect (sink->filter_caps, el_caps);
271 gst_caps_unref (el_caps);
274 GST_DEBUG_OBJECT (sink, "Incompatible caps");
275 gst_object_unref (el);
278 GST_DEBUG_OBJECT (sink, "Found compatible caps");
282 gst_element_set_bus (el, bus);
283 ret = gst_element_set_state (el, GST_STATE_READY);
284 if (ret == GST_STATE_CHANGE_SUCCESS) {
285 GST_DEBUG_OBJECT (sink, "This worked!");
290 /* collect all error messages */
291 while ((message = gst_bus_pop_filtered (bus, GST_MESSAGE_ERROR))) {
292 GST_DEBUG_OBJECT (sink, "error message %" GST_PTR_FORMAT, message);
293 errors = g_slist_append (errors, message);
296 gst_element_set_state (el, GST_STATE_NULL);
297 gst_object_unref (el);
301 GST_DEBUG_OBJECT (sink, "done trying");
304 /* FIXME: we forward the first error for now; but later on it might make
305 * sense to actually analyse them */
306 gst_message_ref (GST_MESSAGE (errors->data));
307 GST_DEBUG_OBJECT (sink, "reposting message %p", errors->data);
308 gst_element_post_message (GST_ELEMENT (sink), GST_MESSAGE (errors->data));
310 /* send warning message to application and use a fakesink */
311 GST_ELEMENT_WARNING (sink, RESOURCE, NOT_FOUND, (NULL),
312 ("Failed to find a usable audio sink"));
313 choice = gst_element_factory_make ("fakesink", "fake-audio-sink");
314 if (g_object_class_find_property (G_OBJECT_GET_CLASS (choice), "sync"))
315 g_object_set (choice, "sync", TRUE, NULL);
316 gst_element_set_state (choice, GST_STATE_READY);
319 gst_object_unref (bus);
320 gst_plugin_feature_list_free (list);
321 g_slist_foreach (errors, (GFunc) gst_mini_object_unref, NULL);
322 g_slist_free (errors);
328 gst_auto_audio_sink_detect (GstAutoAudioSink * sink)
333 gst_auto_audio_sink_clear_kid (sink);
336 GST_DEBUG_OBJECT (sink, "Creating new kid");
337 if (!(esink = gst_auto_audio_sink_find_best (sink)))
341 /* Ensure the child is brought up to the right state to match the parent
342 * although it's currently always in READY and
343 * we're always doing NULL->READY. */
344 if (GST_STATE (sink->kid) < GST_STATE (sink))
345 gst_element_set_state (sink->kid, GST_STATE (sink));
347 gst_bin_add (GST_BIN (sink), esink);
349 /* attach ghost pad */
350 GST_DEBUG_OBJECT (sink, "Re-assigning ghostpad");
351 targetpad = gst_element_get_static_pad (sink->kid, "sink");
352 if (!gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad))
355 gst_object_unref (targetpad);
356 GST_DEBUG_OBJECT (sink, "done changing auto audio sink");
363 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
364 ("Failed to find a supported audio sink"));
369 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
370 ("Failed to set target pad"));
371 gst_object_unref (targetpad);
376 static GstStateChangeReturn
377 gst_auto_audio_sink_change_state (GstElement * element,
378 GstStateChange transition)
380 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
381 GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (element);
383 switch (transition) {
384 case GST_STATE_CHANGE_NULL_TO_READY:
385 if (!gst_auto_audio_sink_detect (sink))
386 return GST_STATE_CHANGE_FAILURE;
392 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
393 if (ret == GST_STATE_CHANGE_FAILURE)
396 switch (transition) {
397 case GST_STATE_CHANGE_READY_TO_NULL:
398 gst_auto_audio_sink_reset (sink);
408 gst_auto_audio_sink_set_property (GObject * object, guint prop_id,
409 const GValue * value, GParamSpec * pspec)
411 GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (object);
415 if (sink->filter_caps)
416 gst_caps_unref (sink->filter_caps);
417 sink->filter_caps = gst_caps_copy (gst_value_get_caps (value));
420 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
426 gst_auto_audio_sink_get_property (GObject * object, guint prop_id,
427 GValue * value, GParamSpec * pspec)
429 GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (object);
433 gst_value_set_caps (value, sink->filter_caps);
437 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);