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