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 GST_BOILERPLATE (GstAutoAudioSink, gst_auto_audio_sink, GstBin, GST_TYPE_BIN);
66 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
72 gst_auto_audio_sink_base_init (gpointer klass)
74 GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
76 gst_element_class_add_static_pad_template (eklass, &sink_template);
78 gst_element_class_set_details_simple (eklass, "Auto audio sink",
80 "Wrapper audio sink for automatically detected audio sink",
81 "Jan Schmidt <thaytan@noraisin.net>");
85 gst_auto_audio_sink_class_init (GstAutoAudioSinkClass * klass)
87 GObjectClass *gobject_class;
88 GstElementClass *eklass;
90 gobject_class = G_OBJECT_CLASS (klass);
91 eklass = GST_ELEMENT_CLASS (klass);
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;
97 eklass->change_state = GST_DEBUG_FUNCPTR (gst_auto_audio_sink_change_state);
100 * GstAutoAudioSink:filter-caps
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.
106 * This property can only be set before the element goes to the READY state.
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));
117 gst_auto_audio_sink_dispose (GstAutoAudioSink * sink)
119 gst_auto_audio_sink_clear_kid (sink);
121 if (sink->filter_caps)
122 gst_caps_unref (sink->filter_caps);
123 sink->filter_caps = NULL;
125 G_OBJECT_CLASS (parent_class)->dispose ((GObject *) sink);
129 gst_auto_audio_sink_clear_kid (GstAutoAudioSink * sink)
132 gst_element_set_state (sink->kid, GST_STATE_NULL);
133 gst_bin_remove (GST_BIN (sink), sink->kid);
135 /* Don't lose the SINK flag */
136 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
141 * Hack to make initial linking work; ideally, this'd work even when
142 * no target has been assigned to the ghostpad yet.
145 gst_auto_audio_sink_reset (GstAutoAudioSink * sink)
149 gst_auto_audio_sink_clear_kid (sink);
151 /* fakesink placeholder */
152 sink->kid = gst_element_factory_make ("fakesink", "tempsink");
153 gst_bin_add (GST_BIN (sink), sink->kid);
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);
161 static GstStaticCaps raw_caps =
162 GST_STATIC_CAPS ("audio/x-raw-int; audio/x-raw-float");
165 gst_auto_audio_sink_init (GstAutoAudioSink * sink,
166 GstAutoAudioSinkClass * g_class)
168 sink->pad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
169 gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
171 gst_auto_audio_sink_reset (sink);
173 /* set the default raw audio caps */
174 sink->filter_caps = gst_static_caps_get (&raw_caps);
177 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
181 gst_auto_audio_sink_factory_filter (GstPluginFeature * feature, gpointer data)
186 /* we only care about element factories */
187 if (!GST_IS_ELEMENT_FACTORY (feature))
191 klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
192 if (!(strstr (klass, "Sink") && strstr (klass, "Audio")))
195 /* only select elements with autoplugging rank */
196 rank = gst_plugin_feature_get_rank (feature);
197 if (rank < GST_RANK_MARGINAL)
204 gst_auto_audio_sink_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
208 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
211 return strcmp (gst_plugin_feature_get_name (f2),
212 gst_plugin_feature_get_name (f1));
216 gst_auto_audio_sink_create_element_with_pretty_name (GstAutoAudioSink * sink,
217 GstElementFactory * factory)
220 gchar *name, *marker;
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);
230 element = gst_element_factory_create (factory, name);
237 gst_auto_audio_sink_find_best (GstAutoAudioSink * sink)
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;
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);
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 ...");
257 for (item = list; item != NULL; item = item->next) {
258 GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
261 if ((el = gst_auto_audio_sink_create_element_with_pretty_name (sink, f))) {
262 GstStateChangeReturn ret;
264 GST_DEBUG_OBJECT (sink, "Testing %s", GST_PLUGIN_FEATURE (f)->name);
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);
279 GST_DEBUG_OBJECT (sink, "Incompatible caps");
280 gst_object_unref (el);
283 GST_DEBUG_OBJECT (sink, "Found compatible caps");
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!");
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);
301 gst_element_set_state (el, GST_STATE_NULL);
302 gst_object_unref (el);
306 GST_DEBUG_OBJECT (sink, "done trying");
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));
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);
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);
333 gst_auto_audio_sink_detect (GstAutoAudioSink * sink)
338 gst_auto_audio_sink_clear_kid (sink);
341 GST_DEBUG_OBJECT (sink, "Creating new kid");
342 if (!(esink = gst_auto_audio_sink_find_best (sink)))
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));
352 gst_bin_add (GST_BIN (sink), esink);
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))
360 gst_object_unref (targetpad);
361 GST_DEBUG_OBJECT (sink, "done changing auto audio sink");
368 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
369 ("Failed to find a supported audio sink"));
374 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
375 ("Failed to set target pad"));
376 gst_object_unref (targetpad);
381 static GstStateChangeReturn
382 gst_auto_audio_sink_change_state (GstElement * element,
383 GstStateChange transition)
385 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
386 GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (element);
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;
397 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
398 if (ret == GST_STATE_CHANGE_FAILURE)
401 switch (transition) {
402 case GST_STATE_CHANGE_READY_TO_NULL:
403 gst_auto_audio_sink_reset (sink);
413 gst_auto_audio_sink_set_property (GObject * object, guint prop_id,
414 const GValue * value, GParamSpec * pspec)
416 GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (object);
420 if (sink->filter_caps)
421 gst_caps_unref (sink->filter_caps);
422 sink->filter_caps = gst_caps_copy (gst_value_get_caps (value));
425 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
431 gst_auto_audio_sink_get_property (GObject * object, guint prop_id,
432 GValue * value, GParamSpec * pspec)
434 GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (object);
438 gst_value_set_caps (value, sink->filter_caps);
442 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);