2 * (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
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.
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.
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.
26 #include "gstautoaudiosink.h"
27 #include "gstautodetect.h"
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);
36 static GstBinClass *parent_class = NULL;
39 gst_auto_audio_sink_get_type (void)
41 static GType gst_auto_audio_sink_type = 0;
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,
48 (GClassInitFunc) gst_auto_audio_sink_class_init,
51 sizeof (GstAutoAudioSink),
53 (GInstanceInitFunc) gst_auto_audio_sink_init,
56 gst_auto_audio_sink_type = g_type_register_static (GST_TYPE_BIN,
57 "GstAutoAudioSink", &gst_auto_audio_sink_info, 0);
60 return gst_auto_audio_sink_type;
64 gst_auto_audio_sink_base_init (GstAutoAudioSinkClass * klass)
66 GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
67 GstElementDetails gst_auto_audio_sink_details = {
70 "Audio sink embedding the Auto-settings for audio output",
71 "Ronald Bultje <rbultje@ronald.bitfreak.net>"
73 GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
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);
84 gst_auto_audio_sink_class_init (GstAutoAudioSinkClass * klass)
86 GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
88 parent_class = g_type_class_ref (GST_TYPE_BIN);
90 eklass->change_state = gst_auto_audio_sink_change_state;
94 gst_auto_audio_sink_init (GstAutoAudioSink * sink)
98 gst_auto_audio_sink_detect (sink, TRUE);
103 gst_auto_audio_sink_factory_filter (GstPluginFeature * feature, gpointer data)
108 /* we only care about element factories */
109 if (!GST_IS_ELEMENT_FACTORY (feature))
113 klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
114 if (strcmp (klass, "Sink/Audio") != 0)
117 /* only select elements with autoplugging rank */
118 rank = gst_plugin_feature_get_rank (feature);
119 if (rank < GST_RANK_MARGINAL)
126 gst_auto_audio_sink_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
130 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
133 return strcmp (gst_plugin_feature_get_name (f2),
134 gst_plugin_feature_get_name (f1));
138 gst_auto_audio_sink_find_best (GstAutoAudioSink * sink)
141 GstElement *choice = NULL;
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);
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.
155 GST_LOG ("Trying to find %s", ss ? "soundservers" : "audio devices");
157 for (item = list; item != NULL; item = item->next) {
158 GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
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;
169 g_object_get (G_OBJECT (el), "soundserver-running", &r, NULL);
171 GST_LOG ("%s - soundserver is running",
172 GST_PLUGIN_FEATURE (f)->name);
174 GST_LOG ("%s - Soundserver is not running",
175 GST_PLUGIN_FEATURE (f)->name);
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!");
189 gst_object_unref (GST_OBJECT (el));
205 gst_auto_audio_sink_detect (GstAutoAudioSink * sink, gboolean fake)
212 gst_object_ref (GST_OBJECT (sink->pad));
213 peer = GST_PAD_PEER (GST_PAD_REALIZE (sink->pad));
215 gst_pad_unlink (peer, sink->pad);
218 /* kill old element */
220 GST_DEBUG_OBJECT (sink, "Removing old kid");
221 gst_bin_remove (GST_BIN (sink), sink->kid);
226 GST_DEBUG_OBJECT (sink, "Creating new kid (%ssink)", fake ? "fake" : "audio");
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"));
235 gst_bin_add (GST_BIN (sink), esink);
237 /* attach ghost 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);
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);
250 GST_DEBUG_OBJECT (sink, "Linking...");
251 gst_pad_link (peer, sink->pad);
254 GST_DEBUG_OBJECT (sink, "done changing auto audio sink");
258 static GstElementStateReturn
259 gst_auto_audio_sink_change_state (GstElement * element)
261 GstAutoAudioSink *sink = GST_AUTO_AUDIO_SINK (element);
263 if (GST_STATE_TRANSITION (element) == GST_STATE_NULL_TO_READY && !sink->init) {
264 gst_auto_audio_sink_detect (sink, FALSE);
266 return GST_STATE_FAILURE;
269 return GST_ELEMENT_CLASS (parent_class)->change_state (element);