gst_plugins_good/gst/autodetect/gstautovideosink.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-autovideosink
       
    23  * @see_also: autoaudiosink, ximagesink, xvimagesink, sdlvideosink
       
    24  *
       
    25  * <refsect2>
       
    26  * <para>
       
    27  * autovideosink is a video sink that automatically detects an appropriate
       
    28  * video sink to use.  It does so by scanning the registry for all elements
       
    29  * that have <quote>Sink</quote> and <quote>Video</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 videotestsrc ! autovideosink
       
    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 "gstautovideosink.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_video_sink_change_state (GstElement * element,
       
    59     GstStateChange transition);
       
    60 static void gst_auto_video_sink_dispose (GstAutoVideoSink * sink);
       
    61 static void gst_auto_video_sink_clear_kid (GstAutoVideoSink * sink);
       
    62 
       
    63 static void gst_auto_video_sink_set_property (GObject * object, guint prop_id,
       
    64     const GValue * value, GParamSpec * pspec);
       
    65 static void gst_auto_video_sink_get_property (GObject * object, guint prop_id,
       
    66     GValue * value, GParamSpec * pspec);
       
    67 
       
    68 GST_BOILERPLATE (GstAutoVideoSink, gst_auto_video_sink, GstBin, GST_TYPE_BIN);
       
    69 
       
    70 static const GstElementDetails gst_auto_video_sink_details =
       
    71 GST_ELEMENT_DETAILS ("Auto video sink",
       
    72     "Sink/Video",
       
    73     "Wrapper video sink for automatically detected video sink",
       
    74     "Ronald Bultje <rbultje@ronald.bitfreak.net>\n"
       
    75     "Jan Schmidt <thaytan@noraisin.net");
       
    76 
       
    77 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
       
    78     GST_PAD_SINK,
       
    79     GST_PAD_ALWAYS,
       
    80     GST_STATIC_CAPS_ANY);
       
    81 
       
    82 static void
       
    83 gst_auto_video_sink_base_init (gpointer klass)
       
    84 {
       
    85   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
       
    86 
       
    87   gst_element_class_add_pad_template (eklass,
       
    88       gst_static_pad_template_get (&sink_template));
       
    89   gst_element_class_set_details (eklass, &gst_auto_video_sink_details);
       
    90 }
       
    91 
       
    92 static void
       
    93 gst_auto_video_sink_class_init (GstAutoVideoSinkClass * klass)
       
    94 {
       
    95   GObjectClass *gobject_class;
       
    96   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
       
    97 
       
    98   gobject_class = G_OBJECT_CLASS (klass);
       
    99   gobject_class->dispose =
       
   100       (GObjectFinalizeFunc) GST_DEBUG_FUNCPTR (gst_auto_video_sink_dispose);
       
   101   eklass->change_state = GST_DEBUG_FUNCPTR (gst_auto_video_sink_change_state);
       
   102   gobject_class->set_property =
       
   103       GST_DEBUG_FUNCPTR (gst_auto_video_sink_set_property);
       
   104   gobject_class->get_property =
       
   105       GST_DEBUG_FUNCPTR (gst_auto_video_sink_get_property);
       
   106 
       
   107   /**
       
   108    * GstAutoVideoSink:filter-caps
       
   109    *
       
   110    * This property will filter out candidate sinks that can handle the specified
       
   111    * caps. By default only video sinks that support raw rgb and yuv video
       
   112    * are selected.
       
   113    *
       
   114    * This property can only be set before the element goes to the READY state.
       
   115    *
       
   116    * Since: 0.10.7
       
   117    **/
       
   118   g_object_class_install_property (gobject_class, PROP_CAPS,
       
   119       g_param_spec_boxed ("filter-caps", "Filter caps",
       
   120           "Filter sink candidates using these caps.", GST_TYPE_CAPS,
       
   121           G_PARAM_READWRITE));
       
   122 }
       
   123 
       
   124 static void
       
   125 gst_auto_video_sink_dispose (GstAutoVideoSink * sink)
       
   126 {
       
   127   gst_auto_video_sink_clear_kid (sink);
       
   128 
       
   129   if (sink->filter_caps)
       
   130     gst_caps_unref (sink->filter_caps);
       
   131   sink->filter_caps = NULL;
       
   132 
       
   133   G_OBJECT_CLASS (parent_class)->dispose ((GObject *) sink);
       
   134 }
       
   135 
       
   136 static void
       
   137 gst_auto_video_sink_clear_kid (GstAutoVideoSink * sink)
       
   138 {
       
   139   if (sink->kid) {
       
   140     gst_element_set_state (sink->kid, GST_STATE_NULL);
       
   141     gst_bin_remove (GST_BIN (sink), sink->kid);
       
   142     sink->kid = NULL;
       
   143   }
       
   144 }
       
   145 
       
   146 /*
       
   147  * Hack to make initial linking work; ideally, this'd work even when
       
   148  * no target has been assigned to the ghostpad yet.
       
   149  */
       
   150 
       
   151 static void
       
   152 gst_auto_video_sink_reset (GstAutoVideoSink * sink)
       
   153 {
       
   154   GstPad *targetpad;
       
   155 
       
   156   /* Remove any existing element */
       
   157   gst_auto_video_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 ("video/x-raw-yuv; video/x-raw-rgb");
       
   171 
       
   172 static void
       
   173 gst_auto_video_sink_init (GstAutoVideoSink * sink,
       
   174     GstAutoVideoSinkClass * 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_video_sink_reset (sink);
       
   180 
       
   181   /* set the default raw video 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_video_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   /* video sinks */
       
   199   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
       
   200   if (!(strstr (klass, "Sink") && strstr (klass, "Video")))
       
   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_video_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_video_sink_create_element_with_pretty_name (GstAutoVideoSink * 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_video_sink_find_best (GstAutoVideoSink * 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_video_sink_factory_filter, FALSE, sink);
       
   258   list = g_list_sort (list, (GCompareFunc) gst_auto_video_sink_compare_ranks);
       
   259 
       
   260   GST_LOG_OBJECT (sink, "Trying to find usable video 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_video_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 autovideosink 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 video sink"));
       
   325       choice = gst_element_factory_make ("fakesink", "fake-video-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_video_sink_detect (GstAutoVideoSink * sink)
       
   341 {
       
   342   GstElement *esink;
       
   343   GstPad *targetpad;
       
   344 
       
   345   gst_auto_video_sink_clear_kid (sink);
       
   346 
       
   347   /* find element */
       
   348   GST_DEBUG_OBJECT (sink, "Creating new kid");
       
   349   if (!(esink = gst_auto_video_sink_find_best (sink)))
       
   350     goto no_sink;
       
   351 
       
   352   sink->kid = esink;
       
   353   gst_bin_add (GST_BIN (sink), esink);
       
   354 
       
   355   /* attach ghost pad */
       
   356   GST_DEBUG_OBJECT (sink, "Re-assigning ghostpad");
       
   357   targetpad = gst_element_get_pad (sink->kid, "sink");
       
   358   gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
       
   359   gst_object_unref (targetpad);
       
   360   GST_DEBUG_OBJECT (sink, "done changing auto video sink");
       
   361 
       
   362   return TRUE;
       
   363 
       
   364   /* ERRORS */
       
   365 no_sink:
       
   366   {
       
   367     GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
       
   368         ("Failed to find a supported video sink"));
       
   369     return FALSE;
       
   370   }
       
   371 }
       
   372 
       
   373 static GstStateChangeReturn
       
   374 gst_auto_video_sink_change_state (GstElement * element,
       
   375     GstStateChange transition)
       
   376 {
       
   377   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
       
   378   GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (element);
       
   379 
       
   380   switch (transition) {
       
   381     case GST_STATE_CHANGE_NULL_TO_READY:
       
   382       if (!gst_auto_video_sink_detect (sink))
       
   383         return GST_STATE_CHANGE_FAILURE;
       
   384       break;
       
   385     default:
       
   386       break;
       
   387   }
       
   388 
       
   389   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
       
   390 
       
   391   switch (transition) {
       
   392     case GST_STATE_CHANGE_READY_TO_NULL:
       
   393       gst_auto_video_sink_reset (sink);
       
   394       break;
       
   395     default:
       
   396       break;
       
   397   }
       
   398 
       
   399   return ret;
       
   400 }
       
   401 
       
   402 static void
       
   403 gst_auto_video_sink_set_property (GObject * object, guint prop_id,
       
   404     const GValue * value, GParamSpec * pspec)
       
   405 {
       
   406   GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (object);
       
   407 
       
   408   switch (prop_id) {
       
   409     case PROP_CAPS:
       
   410       if (sink->filter_caps)
       
   411         gst_caps_unref (sink->filter_caps);
       
   412       sink->filter_caps = gst_caps_copy (gst_value_get_caps (value));
       
   413       break;
       
   414     default:
       
   415       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       
   416       break;
       
   417   }
       
   418 }
       
   419 
       
   420 static void
       
   421 gst_auto_video_sink_get_property (GObject * object, guint prop_id,
       
   422     GValue * value, GParamSpec * pspec)
       
   423 {
       
   424   GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (object);
       
   425 
       
   426   switch (prop_id) {
       
   427     case PROP_CAPS:{
       
   428       gst_value_set_caps (value, sink->filter_caps);
       
   429       break;
       
   430     }
       
   431     default:
       
   432       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       
   433       break;
       
   434   }
       
   435 }