merging autodetect from 0.8 branch
[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 void gst_auto_audio_sink_base_init (GstAutoAudioSinkClass * klass);
30 static void gst_auto_audio_sink_class_init (GstAutoAudioSinkClass * klass);
31 static void gst_auto_audio_sink_init (GstAutoAudioSink * sink);
32 static void gst_auto_audio_sink_detect (GstAutoAudioSink * sink, gboolean fake);
33 static GstElementStateReturn
34 gst_auto_audio_sink_change_state (GstElement * element);
35
36 static GstBinClass *parent_class = NULL;
37
38 GType
39 gst_auto_audio_sink_get_type (void)
40 {
41   static GType gst_auto_audio_sink_type = 0;
42
43   if (!gst_auto_audio_sink_type) {
44     static const GTypeInfo gst_auto_audio_sink_info = {
45       sizeof (GstAutoAudioSinkClass),
46       (GBaseInitFunc) gst_auto_audio_sink_base_init,
47       NULL,
48       (GClassInitFunc) gst_auto_audio_sink_class_init,
49       NULL,
50       NULL,
51       sizeof (GstAutoAudioSink),
52       0,
53       (GInstanceInitFunc) gst_auto_audio_sink_init,
54     };
55
56     gst_auto_audio_sink_type = g_type_register_static (GST_TYPE_BIN,
57         "GstAutoAudioSink", &gst_auto_audio_sink_info, 0);
58   }
59
60   return gst_auto_audio_sink_type;
61 }
62
63 static void
64 gst_auto_audio_sink_base_init (GstAutoAudioSinkClass * klass)
65 {
66   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
67   GstElementDetails gst_auto_audio_sink_details = {
68     "Auto audio sink",
69     "Sink/Audio",
70     "Audio sink embedding the Auto-settings for audio output",
71     "Ronald Bultje <rbultje@ronald.bitfreak.net>"
72   };
73   GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
74       GST_PAD_SINK,
75       GST_PAD_ALWAYS,
76       GST_STATIC_CAPS_ANY);
77
78   gst_element_class_add_pad_template (eklass,
79       gst_static_pad_template_get (&sink_template));
80   gst_element_class_set_details (eklass, &gst_auto_audio_sink_details);
81 }
82
83 static void
84 gst_auto_audio_sink_class_init (GstAutoAudioSinkClass * klass)
85 {
86   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
87
88   parent_class = g_type_class_ref (GST_TYPE_BIN);
89
90   eklass->change_state = gst_auto_audio_sink_change_state;
91 }
92
93 static void
94 gst_auto_audio_sink_init (GstAutoAudioSink * sink)
95 {
96   sink->pad = NULL;
97   sink->kid = NULL;
98   gst_auto_audio_sink_detect (sink, TRUE);
99   sink->init = FALSE;
100 }
101
102 static gboolean
103 gst_auto_audio_sink_factory_filter (GstPluginFeature * feature, gpointer data)
104 {
105   guint rank;
106   const gchar *klass;
107
108   /* we only care about element factories */
109   if (!GST_IS_ELEMENT_FACTORY (feature))
110     return FALSE;
111
112   /* audio sinks */
113   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
114   if (strcmp (klass, "Sink/Audio") != 0)
115     return FALSE;
116
117   /* only select elements with autoplugging rank */
118   rank = gst_plugin_feature_get_rank (feature);
119   if (rank < GST_RANK_MARGINAL)
120     return FALSE;
121
122   return TRUE;
123 }
124
125 static gint
126 gst_auto_audio_sink_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
127 {
128   gint diff;
129
130   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
131   if (diff != 0)
132     return diff;
133   return strcmp (gst_plugin_feature_get_name (f2),
134       gst_plugin_feature_get_name (f1));
135 }
136
137 static GstElement *
138 gst_auto_audio_sink_find_best (GstAutoAudioSink * sink)
139 {
140   GList *list, *item;
141   GstElement *choice = NULL;
142   gboolean ss = TRUE;
143
144   list = gst_registry_pool_feature_filter (
145       (GstPluginFeatureFilter) gst_auto_audio_sink_factory_filter, FALSE, sink);
146   list = g_list_sort (list, (GCompareFunc) gst_auto_audio_sink_compare_ranks);
147
148   /* FIXME:
149    * - soundservers have no priority yet.
150    * - soundserversinks should only be chosen if already running, or if
151    *    the user explicitely wants this to run... That is not easy.
152    */
153
154   while (1) {
155     GST_LOG ("Trying to find %s", ss ? "soundservers" : "audio devices");
156
157     for (item = list; item != NULL; item = item->next) {
158       GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
159       GstElement *el;
160
161       if ((el = gst_element_factory_create (f, "actual-sink"))) {
162         gboolean has_p = g_object_class_find_property (G_OBJECT_GET_CLASS (el),
163             "soundserver-running") ? TRUE : FALSE;
164
165         if (ss == has_p) {
166           if (ss) {
167             gboolean r;
168
169             g_object_get (G_OBJECT (el), "soundserver-running", &r, NULL);
170             if (r) {
171               GST_LOG ("%s - soundserver is running",
172                   GST_PLUGIN_FEATURE (f)->name);
173             } else {
174               GST_LOG ("%s - Soundserver is not running",
175                   GST_PLUGIN_FEATURE (f)->name);
176               goto next;
177             }
178           }
179           GST_LOG ("Testing %s", GST_PLUGIN_FEATURE (f)->name);
180           if (gst_element_set_state (el, GST_STATE_READY) == GST_STATE_SUCCESS) {
181             gst_element_set_state (el, GST_STATE_NULL);
182             GST_LOG ("This worked!");
183             choice = el;
184             goto done;
185           }
186         }
187
188       next:
189         gst_object_unref (GST_OBJECT (el));
190       }
191     }
192
193     if (!ss)
194       break;
195     ss = FALSE;
196   }
197
198 done:
199   g_list_free (list);
200
201   return choice;
202 }
203
204 static void
205 gst_auto_audio_sink_detect (GstAutoAudioSink * sink, gboolean fake)
206 {
207   GstElement *esink;
208   GstPad *peer = NULL;
209
210   /* save ghostpad */
211   if (sink->pad) {
212     gst_object_ref (GST_OBJECT (sink->pad));
213     peer = GST_PAD_PEER (GST_PAD_REALIZE (sink->pad));
214     if (peer)
215       gst_pad_unlink (peer, sink->pad);
216   }
217
218   /* kill old element */
219   if (sink->kid) {
220     GST_DEBUG_OBJECT (sink, "Removing old kid");
221     gst_bin_remove (GST_BIN (sink), sink->kid);
222     sink->kid = NULL;
223   }
224
225   /* find element */
226   GST_DEBUG_OBJECT (sink, "Creating new kid (%ssink)", fake ? "fake" : "audio");
227   if (fake) {
228     esink = gst_element_factory_make ("fakesink", "temporary-sink");
229   } else if (!(esink = gst_auto_audio_sink_find_best (sink))) {
230     GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
231         ("Failed to find a supported audio sink"));
232     return;
233   }
234   sink->kid = esink;
235   gst_bin_add (GST_BIN (sink), esink);
236
237   /* attach ghost pad */
238   if (sink->pad) {
239     GST_DEBUG_OBJECT (sink, "Re-doing existing ghostpad");
240     gst_pad_add_ghost_pad (gst_element_get_pad (sink->kid, "sink"), sink->pad);
241     if (GST_ELEMENT (sink)->pads == NULL)
242       gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
243   } else {
244     GST_DEBUG_OBJECT (sink, "Creating new ghostpad");
245     sink->pad = gst_ghost_pad_new ("sink",
246         gst_element_get_pad (sink->kid, "sink"));
247     gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
248   }
249   if (peer) {
250     GST_DEBUG_OBJECT (sink, "Linking...");
251     gst_pad_link (peer, sink->pad);
252   }
253
254   GST_DEBUG_OBJECT (sink, "done changing auto audio sink");
255   sink->init = TRUE;
256 }
257
258 static GstElementStateReturn
259 gst_auto_audio_sink_change_state (GstElement * element)
260 {
261   GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (element);
262
263   if (GST_STATE_TRANSITION (element) == GST_STATE_NULL_TO_READY && !sink->init) {
264     gst_auto_audio_sink_detect (sink, FALSE);
265     if (!sink->init)
266       return GST_STATE_FAILURE;
267   }
268
269   return GST_ELEMENT_CLASS (parent_class)->change_state (element);
270 }