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