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