gst/autodetect/gstautoaudiosink.c (gst_auto_audio_sink_find_best): gst/autodetect...
[platform/upstream/gstreamer.git] / gst / autodetect / gstautoaudiosink.c
1 /* GStreamer
2  * (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include "gstautoaudiosink.h"
27 #include "gstautodetect.h"
28
29 static GstStateChangeReturn
30 gst_auto_audio_sink_change_state (GstElement * element,
31     GstStateChange transition);
32
33 GST_BOILERPLATE (GstAutoAudioSink, gst_auto_audio_sink, GstBin, GST_TYPE_BIN);
34
35 static void
36 gst_auto_audio_sink_base_init (gpointer klass)
37 {
38   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
39   GstElementDetails gst_auto_audio_sink_details = {
40     "Auto audio sink",
41     "Sink/Audio",
42     "Audio sink embedding the Auto-settings for audio output",
43     "Ronald Bultje <rbultje@ronald.bitfreak.net>"
44   };
45   GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
46       GST_PAD_SINK,
47       GST_PAD_ALWAYS,
48       GST_STATIC_CAPS_ANY);
49
50   gst_element_class_add_pad_template (eklass,
51       gst_static_pad_template_get (&sink_template));
52   gst_element_class_set_details (eklass, &gst_auto_audio_sink_details);
53 }
54
55 static void
56 gst_auto_audio_sink_class_init (GstAutoAudioSinkClass * klass)
57 {
58   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
59
60   eklass->change_state = gst_auto_audio_sink_change_state;
61 }
62
63 /*
64  * Hack to make initial linking work; ideally, this'd work even when
65  * no target has been assigned to the ghostpad yet.
66  */
67
68 static void
69 gst_auto_audio_sink_reset (GstAutoAudioSink * sink)
70 {
71   GstPad *targetpad;
72
73   /* fakesink placeholder */
74   if (sink->kid) {
75     gst_bin_remove (GST_BIN (sink), sink->kid);
76   }
77   sink->kid = gst_element_factory_make ("fakesink", "tempsink");
78   gst_bin_add (GST_BIN (sink), sink->kid);
79
80   /* pad */
81   targetpad = gst_element_get_pad (sink->kid, "sink");
82   gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
83   gst_object_unref (targetpad);
84 }
85
86 static void
87 gst_auto_audio_sink_init (GstAutoAudioSink * sink,
88     GstAutoAudioSinkClass * g_class)
89 {
90   sink->pad = gst_ghost_pad_new_notarget ("sink", GST_PAD_SINK);
91   gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
92
93   gst_auto_audio_sink_reset (sink);
94 }
95
96 static gboolean
97 gst_auto_audio_sink_factory_filter (GstPluginFeature * feature, gpointer data)
98 {
99   guint rank;
100   const gchar *klass;
101
102   /* we only care about element factories */
103   if (!GST_IS_ELEMENT_FACTORY (feature))
104     return FALSE;
105
106   /* audio sinks */
107   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
108   if (strcmp (klass, "Sink/Audio") != 0)
109     return FALSE;
110
111   /* only select elements with autoplugging rank */
112   rank = gst_plugin_feature_get_rank (feature);
113   if (rank < GST_RANK_MARGINAL)
114     return FALSE;
115
116   return TRUE;
117 }
118
119 static gint
120 gst_auto_audio_sink_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
121 {
122   gint diff;
123
124   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
125   if (diff != 0)
126     return diff;
127   return strcmp (gst_plugin_feature_get_name (f2),
128       gst_plugin_feature_get_name (f1));
129 }
130
131 static GstElement *
132 gst_auto_audio_sink_find_best (GstAutoAudioSink * sink)
133 {
134   GList *list, *item;
135   GstElement *choice = NULL;
136   gboolean ss = TRUE;
137
138   list = gst_registry_feature_filter (gst_registry_get_default (),
139       (GstPluginFeatureFilter) gst_auto_audio_sink_factory_filter, FALSE, sink);
140   list = g_list_sort (list, (GCompareFunc) gst_auto_audio_sink_compare_ranks);
141
142   /* FIXME:
143    * - soundservers have no priority yet.
144    * - soundserversinks should only be chosen if already running, or if
145    *    the user explicitely wants this to run... That is not easy.
146    */
147
148   while (1) {
149     GST_LOG ("Trying to find %s", ss ? "soundservers" : "audio devices");
150
151     for (item = list; item != NULL; item = item->next) {
152       GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
153       GstElement *el;
154
155       if ((el = gst_element_factory_create (f, "actual-sink"))) {
156         gboolean has_p = g_object_class_find_property (G_OBJECT_GET_CLASS (el),
157             "soundserver-running") ? TRUE : FALSE;
158
159         if (ss == has_p) {
160           if (ss) {
161             gboolean r;
162
163             g_object_get (G_OBJECT (el), "soundserver-running", &r, NULL);
164             if (r) {
165               GST_LOG ("%s - soundserver is running",
166                   GST_PLUGIN_FEATURE (f)->name);
167             } else {
168               GST_LOG ("%s - Soundserver is not running",
169                   GST_PLUGIN_FEATURE (f)->name);
170               goto next;
171             }
172           }
173           GST_LOG ("Testing %s", GST_PLUGIN_FEATURE (f)->name);
174           if (gst_element_set_state (el,
175                   GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS) {
176             gst_element_set_state (el, GST_STATE_NULL);
177             GST_LOG ("This worked!");
178             choice = el;
179             goto done;
180           }
181         }
182
183       next:
184         gst_object_unref (GST_OBJECT (el));
185       }
186     }
187
188     if (!ss)
189       break;
190     ss = FALSE;
191   }
192
193 done:
194   g_list_free (list);
195
196   return choice;
197 }
198
199 static gboolean
200 gst_auto_audio_sink_detect (GstAutoAudioSink * sink)
201 {
202   GstElement *esink;
203   GstPad *targetpad;
204
205   if (sink->kid) {
206     gst_bin_remove (GST_BIN (sink), sink->kid);
207     sink->kid = NULL;
208   }
209
210   /* find element */
211   GST_DEBUG_OBJECT (sink, "Creating new kid");
212   if (!(esink = gst_auto_audio_sink_find_best (sink))) {
213     GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
214         ("Failed to find a supported audio sink"));
215     return FALSE;
216   }
217   sink->kid = esink;
218   gst_bin_add (GST_BIN (sink), esink);
219
220   /* attach ghost pad */
221   GST_DEBUG_OBJECT (sink, "Re-assigning ghostpad");
222   targetpad = gst_element_get_pad (sink->kid, "sink");
223   gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
224   gst_object_unref (targetpad);
225   GST_DEBUG_OBJECT (sink, "done changing auto audio sink");
226
227   return TRUE;
228 }
229
230 static GstStateChangeReturn
231 gst_auto_audio_sink_change_state (GstElement * element,
232     GstStateChange transition)
233 {
234   GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (element);
235
236   GST_DEBUG_OBJECT (element, "Change state 0x%x", transition);
237
238   switch (transition) {
239     case GST_STATE_CHANGE_NULL_TO_READY:
240       if (!gst_auto_audio_sink_detect (sink))
241         return GST_STATE_CHANGE_FAILURE;
242       break;
243     case GST_STATE_CHANGE_READY_TO_NULL:
244       gst_auto_audio_sink_reset (sink);
245       break;
246     default:
247       break;
248   }
249
250   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
251 }