gst_plugins_good/gst/autodetect/gstautovideosink.c
changeset 26 69c7080681bf
parent 24 bc39b352897e
child 28 4ed5253bb6ba
equal deleted inserted replaced
24:bc39b352897e 26:69c7080681bf
     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  * autovideosink is a video sink that automatically detects an appropriate
       
    26  * video sink to use.  It does so by scanning the registry for all elements
       
    27  * that have <quote>Sink</quote> and <quote>Video</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 videotestsrc ! autovideosink
       
    34  * ]|
       
    35  * </refsect2>
       
    36  */
       
    37 
       
    38 #ifdef HAVE_CONFIG_H
       
    39 #include "config.h"
       
    40 #endif
       
    41 
       
    42 #include <string.h>
       
    43 
       
    44 #include "gstautovideosink.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_video_sink_change_state (GstElement * element,
       
    56     GstStateChange transition);
       
    57 static void gst_auto_video_sink_dispose (GstAutoVideoSink * sink);
       
    58 static void gst_auto_video_sink_clear_kid (GstAutoVideoSink * sink);
       
    59 
       
    60 static void gst_auto_video_sink_set_property (GObject * object, guint prop_id,
       
    61     const GValue * value, GParamSpec * pspec);
       
    62 static void gst_auto_video_sink_get_property (GObject * object, guint prop_id,
       
    63     GValue * value, GParamSpec * pspec);
       
    64 
       
    65 GST_BOILERPLATE (GstAutoVideoSink, gst_auto_video_sink, GstBin, GST_TYPE_BIN);
       
    66 
       
    67 static const GstElementDetails gst_auto_video_sink_details =
       
    68 GST_ELEMENT_DETAILS ("Auto video sink",
       
    69     "Sink/Video",
       
    70     "Wrapper video sink for automatically detected video sink",
       
    71     "Ronald Bultje <rbultje@ronald.bitfreak.net>\n"
       
    72     "Jan Schmidt <thaytan@noraisin.net>");
       
    73 
       
    74 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
       
    75     GST_PAD_SINK,
       
    76     GST_PAD_ALWAYS,
       
    77     GST_STATIC_CAPS_ANY);
       
    78 
       
    79 static void
       
    80 gst_auto_video_sink_base_init (gpointer klass)
       
    81 {
       
    82   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
       
    83 
       
    84   gst_element_class_add_pad_template (eklass,
       
    85       gst_static_pad_template_get (&sink_template));
       
    86   gst_element_class_set_details (eklass, &gst_auto_video_sink_details);
       
    87 }
       
    88 
       
    89 static void
       
    90 gst_auto_video_sink_class_init (GstAutoVideoSinkClass * klass)
       
    91 {
       
    92   GObjectClass *gobject_class;
       
    93   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
       
    94 
       
    95   gobject_class = G_OBJECT_CLASS (klass);
       
    96   gobject_class->dispose =
       
    97       (GObjectFinalizeFunc) GST_DEBUG_FUNCPTR (gst_auto_video_sink_dispose);
       
    98   eklass->change_state = GST_DEBUG_FUNCPTR (gst_auto_video_sink_change_state);
       
    99   gobject_class->set_property =
       
   100       GST_DEBUG_FUNCPTR (gst_auto_video_sink_set_property);
       
   101   gobject_class->get_property =
       
   102       GST_DEBUG_FUNCPTR (gst_auto_video_sink_get_property);
       
   103 
       
   104   /**
       
   105    * GstAutoVideoSink:filter-caps
       
   106    *
       
   107    * This property will filter out candidate sinks that can handle the specified
       
   108    * caps. By default only video sinks that support raw rgb and yuv video
       
   109    * are selected.
       
   110    *
       
   111    * This property can only be set before the element goes to the READY state.
       
   112    *
       
   113    * Since: 0.10.7
       
   114    **/
       
   115   g_object_class_install_property (gobject_class, PROP_CAPS,
       
   116       g_param_spec_boxed ("filter-caps", "Filter caps",
       
   117           "Filter sink candidates using these caps.", GST_TYPE_CAPS,
       
   118           G_PARAM_READWRITE));
       
   119 }
       
   120 
       
   121 static void
       
   122 gst_auto_video_sink_dispose (GstAutoVideoSink * sink)
       
   123 {
       
   124   gst_auto_video_sink_clear_kid (sink);
       
   125 
       
   126   if (sink->filter_caps)
       
   127     gst_caps_unref (sink->filter_caps);
       
   128   sink->filter_caps = NULL;
       
   129 
       
   130   G_OBJECT_CLASS (parent_class)->dispose ((GObject *) sink);
       
   131 }
       
   132 
       
   133 static void
       
   134 gst_auto_video_sink_clear_kid (GstAutoVideoSink * sink)
       
   135 {
       
   136   if (sink->kid) {
       
   137     gst_element_set_state (sink->kid, GST_STATE_NULL);
       
   138     gst_bin_remove (GST_BIN (sink), sink->kid);
       
   139     sink->kid = NULL;
       
   140   }
       
   141 }
       
   142 
       
   143 /*
       
   144  * Hack to make initial linking work; ideally, this'd work even when
       
   145  * no target has been assigned to the ghostpad yet.
       
   146  */
       
   147 
       
   148 static void
       
   149 gst_auto_video_sink_reset (GstAutoVideoSink * sink)
       
   150 {
       
   151   GstPad *targetpad;
       
   152 
       
   153   /* Remove any existing element */
       
   154   gst_auto_video_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, setting this target should always work */
       
   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 ("video/x-raw-yuv; video/x-raw-rgb");
       
   168 
       
   169 static void
       
   170 gst_auto_video_sink_init (GstAutoVideoSink * sink,
       
   171     GstAutoVideoSinkClass * 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_video_sink_reset (sink);
       
   177 
       
   178   /* set the default raw video 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_video_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   /* video sinks */
       
   196   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
       
   197   if (!(strstr (klass, "Sink") && strstr (klass, "Video")))
       
   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_video_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_video_sink_create_element_with_pretty_name (GstAutoVideoSink * 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_video_sink_find_best (GstAutoVideoSink * 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_video_sink_factory_filter, FALSE, sink);
       
   255   list = g_list_sort (list, (GCompareFunc) gst_auto_video_sink_compare_ranks);
       
   256 
       
   257   GST_LOG_OBJECT (sink, "Trying to find usable video devices ...");
       
   258 
       
   259   for (item = list; item != NULL; item = item->next) {
       
   260     GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
       
   261     GstElement *el;
       
   262 
       
   263     if ((el = gst_auto_video_sink_create_element_with_pretty_name (sink, f))) {
       
   264       GstStateChangeReturn ret;
       
   265 
       
   266       GST_DEBUG_OBJECT (sink, "Testing %s", GST_PLUGIN_FEATURE (f)->name);
       
   267 
       
   268       /* If autovideosink has been provided with filter caps,
       
   269        * accept only sinks that match with the filter caps */
       
   270       if (sink->filter_caps) {
       
   271         el_pad = gst_element_get_static_pad (GST_ELEMENT (el), "sink");
       
   272         el_caps = gst_pad_get_caps (el_pad);
       
   273         gst_object_unref (el_pad);
       
   274         GST_DEBUG_OBJECT (sink,
       
   275             "Checking caps: %" GST_PTR_FORMAT " vs. %" GST_PTR_FORMAT,
       
   276             sink->filter_caps, el_caps);
       
   277         intersect = gst_caps_intersect (sink->filter_caps, el_caps);
       
   278         no_match = gst_caps_is_empty (intersect);
       
   279         gst_caps_unref (el_caps);
       
   280         gst_caps_unref (intersect);
       
   281 
       
   282         if (no_match) {
       
   283           GST_DEBUG_OBJECT (sink, "Incompatible caps");
       
   284           gst_object_unref (el);
       
   285           continue;
       
   286         } else {
       
   287           GST_DEBUG_OBJECT (sink, "Found compatible caps");
       
   288         }
       
   289       }
       
   290 
       
   291       gst_element_set_bus (el, bus);
       
   292       ret = gst_element_set_state (el, GST_STATE_READY);
       
   293       if (ret == GST_STATE_CHANGE_SUCCESS) {
       
   294         GST_DEBUG_OBJECT (sink, "This worked!");
       
   295         choice = el;
       
   296         break;
       
   297       }
       
   298 
       
   299       /* collect all error messages */
       
   300       while ((message = gst_bus_pop_filtered (bus, GST_MESSAGE_ERROR))) {
       
   301         GST_DEBUG_OBJECT (sink, "error message %" GST_PTR_FORMAT, message);
       
   302         errors = g_slist_append (errors, message);
       
   303       }
       
   304 
       
   305       gst_element_set_state (el, GST_STATE_NULL);
       
   306       gst_object_unref (el);
       
   307     }
       
   308   }
       
   309 
       
   310   GST_DEBUG_OBJECT (sink, "done trying");
       
   311   if (!choice) {
       
   312     if (errors) {
       
   313       /* FIXME: we forward the first error for now; but later on it might make
       
   314        * sense to actually analyse them */
       
   315       gst_message_ref (GST_MESSAGE (errors->data));
       
   316       GST_DEBUG_OBJECT (sink, "reposting message %p", errors->data);
       
   317       gst_element_post_message (GST_ELEMENT (sink), GST_MESSAGE (errors->data));
       
   318     } else {
       
   319       /* send warning message to application and use a fakesink */
       
   320       GST_ELEMENT_WARNING (sink, RESOURCE, NOT_FOUND, (NULL),
       
   321           ("Failed to find a usable video sink"));
       
   322       choice = gst_element_factory_make ("fakesink", "fake-video-sink");
       
   323       if (g_object_class_find_property (G_OBJECT_GET_CLASS (choice), "sync"))
       
   324         g_object_set (choice, "sync", TRUE, NULL);
       
   325       gst_element_set_state (choice, GST_STATE_READY);
       
   326     }
       
   327   }
       
   328   gst_object_unref (bus);
       
   329   gst_plugin_feature_list_free (list);
       
   330   g_slist_foreach (errors, (GFunc) gst_mini_object_unref, NULL);
       
   331   g_slist_free (errors);
       
   332 
       
   333   return choice;
       
   334 }
       
   335 
       
   336 static gboolean
       
   337 gst_auto_video_sink_detect (GstAutoVideoSink * sink)
       
   338 {
       
   339   GstElement *esink;
       
   340   GstPad *targetpad;
       
   341 
       
   342   gst_auto_video_sink_clear_kid (sink);
       
   343 
       
   344   /* find element */
       
   345   GST_DEBUG_OBJECT (sink, "Creating new kid");
       
   346   if (!(esink = gst_auto_video_sink_find_best (sink)))
       
   347     goto no_sink;
       
   348 
       
   349   sink->kid = esink;
       
   350   gst_bin_add (GST_BIN (sink), esink);
       
   351 
       
   352   /* attach ghost pad */
       
   353   GST_DEBUG_OBJECT (sink, "Re-assigning ghostpad");
       
   354   targetpad = gst_element_get_static_pad (sink->kid, "sink");
       
   355   if (!gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad))
       
   356     goto target_failed;
       
   357 
       
   358   gst_object_unref (targetpad);
       
   359   GST_DEBUG_OBJECT (sink, "done changing auto video sink");
       
   360 
       
   361   return TRUE;
       
   362 
       
   363   /* ERRORS */
       
   364 no_sink:
       
   365   {
       
   366     GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
       
   367         ("Failed to find a supported video sink"));
       
   368     return FALSE;
       
   369   }
       
   370 target_failed:
       
   371   {
       
   372     GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
       
   373         ("Failed to set target pad"));
       
   374     gst_object_unref (targetpad);
       
   375     return FALSE;
       
   376   }
       
   377 }
       
   378 
       
   379 static GstStateChangeReturn
       
   380 gst_auto_video_sink_change_state (GstElement * element,
       
   381     GstStateChange transition)
       
   382 {
       
   383   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
       
   384   GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (element);
       
   385 
       
   386   switch (transition) {
       
   387     case GST_STATE_CHANGE_NULL_TO_READY:
       
   388       if (!gst_auto_video_sink_detect (sink))
       
   389         return GST_STATE_CHANGE_FAILURE;
       
   390       break;
       
   391     default:
       
   392       break;
       
   393   }
       
   394 
       
   395   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
       
   396 
       
   397   switch (transition) {
       
   398     case GST_STATE_CHANGE_READY_TO_NULL:
       
   399       gst_auto_video_sink_reset (sink);
       
   400       break;
       
   401     default:
       
   402       break;
       
   403   }
       
   404 
       
   405   return ret;
       
   406 }
       
   407 
       
   408 static void
       
   409 gst_auto_video_sink_set_property (GObject * object, guint prop_id,
       
   410     const GValue * value, GParamSpec * pspec)
       
   411 {
       
   412   GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (object);
       
   413 
       
   414   switch (prop_id) {
       
   415     case PROP_CAPS:
       
   416       if (sink->filter_caps)
       
   417         gst_caps_unref (sink->filter_caps);
       
   418       sink->filter_caps = gst_caps_copy (gst_value_get_caps (value));
       
   419       break;
       
   420     default:
       
   421       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       
   422       break;
       
   423   }
       
   424 }
       
   425 
       
   426 static void
       
   427 gst_auto_video_sink_get_property (GObject * object, guint prop_id,
       
   428     GValue * value, GParamSpec * pspec)
       
   429 {
       
   430   GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (object);
       
   431 
       
   432   switch (prop_id) {
       
   433     case PROP_CAPS:{
       
   434       gst_value_set_caps (value, sink->filter_caps);
       
   435       break;
       
   436     }
       
   437     default:
       
   438       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       
   439       break;
       
   440   }
       
   441 }