gst_plugins_base/gst/playback/gststreamselector.c
changeset 0 0e761a78d257
child 8 4a7fac7dd34a
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer
       
     2  * Copyright (C) 2003 Julien Moutte <julien@moutte.net>
       
     3  * Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
       
     4  * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
       
     5  * Copyright (C) 2007 Wim Taymans <wim.taymans@gmail.com>
       
     6  *
       
     7  * This library is free software; you can redistribute it and/or
       
     8  * modify it under the terms of the GNU Library General Public
       
     9  * License as published by the Free Software Foundation; either
       
    10  * version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  * This library is distributed in the hope that it will be useful,
       
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    15  * Library General Public License for more details.
       
    16  *
       
    17  * You should have received a copy of the GNU Library General Public
       
    18  * License along with this library; if not, write to the
       
    19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    20  * Boston, MA 02111-1307, USA.
       
    21  */
       
    22 
       
    23 #ifdef HAVE_CONFIG_H
       
    24 #include "config.h"
       
    25 #endif
       
    26 
       
    27 #include <string.h>
       
    28 
       
    29 #include "gststreamselector.h"
       
    30 
       
    31 #ifdef __SYMBIAN32__
       
    32 #include <glib_global.h>
       
    33 #endif
       
    34 
       
    35 GST_DEBUG_CATEGORY_STATIC (stream_selector_debug);
       
    36 #define GST_CAT_DEFAULT stream_selector_debug
       
    37 
       
    38 static const GstElementDetails gst_stream_selector_details =
       
    39 GST_ELEMENT_DETAILS ("StreamSelector",
       
    40     "Generic",
       
    41     "N-to-1 input stream_selectoring",
       
    42     "Julien Moutte <julien@moutte.net>\n"
       
    43     "Ronald S. Bultje <rbultje@ronald.bitfreak.net>\n"
       
    44     "Jan Schmidt <thaytan@mad.scientist.com>\n"
       
    45     "Wim Taymans <wim.taymans@gmail.com>");
       
    46 
       
    47 static GstStaticPadTemplate gst_stream_selector_sink_factory =
       
    48 GST_STATIC_PAD_TEMPLATE ("sink%d",
       
    49     GST_PAD_SINK,
       
    50     GST_PAD_REQUEST,
       
    51     GST_STATIC_CAPS_ANY);
       
    52 
       
    53 static GstStaticPadTemplate gst_stream_selector_src_factory =
       
    54 GST_STATIC_PAD_TEMPLATE ("src",
       
    55     GST_PAD_SRC,
       
    56     GST_PAD_ALWAYS,
       
    57     GST_STATIC_CAPS_ANY);
       
    58 
       
    59 enum
       
    60 {
       
    61   PROP_0,
       
    62   PROP_N_PADS,
       
    63   PROP_ACTIVE_PAD,
       
    64   PROP_LAST
       
    65 };
       
    66 
       
    67 static gboolean gst_stream_selector_is_active_sinkpad (GstStreamSelector * sel,
       
    68     GstPad * pad);
       
    69 static GstPad *gst_stream_selector_activate_sinkpad (GstStreamSelector * sel,
       
    70     GstPad * pad);
       
    71 static GstPad *gst_stream_selector_get_linked_pad (GstPad * pad,
       
    72     gboolean strict);
       
    73 
       
    74 #define GST_TYPE_SELECTOR_PAD \
       
    75   (gst_selector_pad_get_type())
       
    76 #define GST_SELECTOR_PAD(obj) \
       
    77   (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
       
    78 #define GST_SELECTOR_PAD_CLASS(klass) \
       
    79   (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
       
    80 #define GST_IS_SELECTOR_PAD(obj) \
       
    81   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
       
    82 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
       
    83   (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
       
    84 #define GST_SELECTOR_PAD_CAST(obj) \
       
    85   ((GstSelectorPad *)(obj))
       
    86 
       
    87 typedef struct _GstSelectorPad GstSelectorPad;
       
    88 typedef struct _GstSelectorPadClass GstSelectorPadClass;
       
    89 
       
    90 struct _GstSelectorPad
       
    91 {
       
    92   GstPad parent;
       
    93 
       
    94   gboolean active;
       
    95   gboolean eos;
       
    96   gboolean segment_pending;
       
    97   GstSegment segment;
       
    98   GstTagList *tags;
       
    99 };
       
   100 
       
   101 struct _GstSelectorPadClass
       
   102 {
       
   103   GstPadClass parent;
       
   104 };
       
   105 
       
   106 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
       
   107 static void gst_selector_pad_init (GstSelectorPad * pad);
       
   108 static void gst_selector_pad_finalize (GObject * object);
       
   109 
       
   110 static void gst_selector_pad_get_property (GObject * object,
       
   111     guint prop_id, GValue * value, GParamSpec * pspec);
       
   112 
       
   113 static GstPadClass *selector_pad_parent_class = NULL;
       
   114 
       
   115 static void gst_selector_pad_reset (GstSelectorPad * pad);
       
   116 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
       
   117 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
       
   118 static GList *gst_selector_pad_get_linked_pads (GstPad * pad);
       
   119 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
       
   120 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
       
   121     guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
       
   122 
       
   123 enum
       
   124 {
       
   125   PROP_PAD_0,
       
   126   PROP_PAD_TAGS,
       
   127   PROP_PAD_ACTIVE,
       
   128   PROP_PAD_LAST
       
   129 };
       
   130 
       
   131 static GType
       
   132 gst_selector_pad_get_type (void)
       
   133 {
       
   134   static GType selector_pad_type = 0;
       
   135 
       
   136   if (!selector_pad_type) {
       
   137     static const GTypeInfo selector_pad_info = {
       
   138       sizeof (GstSelectorPadClass),
       
   139       NULL,
       
   140       NULL,
       
   141       (GClassInitFunc) gst_selector_pad_class_init,
       
   142       NULL,
       
   143       NULL,
       
   144       sizeof (GstSelectorPad),
       
   145       0,
       
   146       (GInstanceInitFunc) gst_selector_pad_init,
       
   147     };
       
   148 
       
   149     selector_pad_type =
       
   150         g_type_register_static (GST_TYPE_PAD, "GstSelectorPad",
       
   151         &selector_pad_info, 0);
       
   152   }
       
   153   return selector_pad_type;
       
   154 }
       
   155 
       
   156 static void
       
   157 gst_selector_pad_class_init (GstSelectorPadClass * klass)
       
   158 {
       
   159   GObjectClass *gobject_class;
       
   160 
       
   161   gobject_class = (GObjectClass *) klass;
       
   162 
       
   163   selector_pad_parent_class = g_type_class_peek_parent (klass);
       
   164 
       
   165   gobject_class->finalize = gst_selector_pad_finalize;
       
   166   gobject_class->get_property =
       
   167       GST_DEBUG_FUNCPTR (gst_selector_pad_get_property);
       
   168 
       
   169   g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
       
   170       g_param_spec_boxed ("tags", "Tags",
       
   171           "The currently active tags on the pad", GST_TYPE_TAG_LIST,
       
   172           G_PARAM_READABLE));
       
   173 
       
   174   g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
       
   175       g_param_spec_boolean ("active", "Active",
       
   176           "If the pad is currently active", FALSE, G_PARAM_READABLE));
       
   177 }
       
   178 
       
   179 static void
       
   180 gst_selector_pad_init (GstSelectorPad * pad)
       
   181 {
       
   182   gst_selector_pad_reset (pad);
       
   183 }
       
   184 
       
   185 static void
       
   186 gst_selector_pad_finalize (GObject * object)
       
   187 {
       
   188   GstSelectorPad *pad;
       
   189 
       
   190   pad = GST_SELECTOR_PAD_CAST (object);
       
   191 
       
   192   if (pad->tags)
       
   193     gst_tag_list_free (pad->tags);
       
   194 
       
   195   G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
       
   196 }
       
   197 
       
   198 static void
       
   199 gst_selector_pad_get_property (GObject * object,
       
   200     guint prop_id, GValue * value, GParamSpec * pspec)
       
   201 {
       
   202   GstSelectorPad *pad;
       
   203 
       
   204   pad = GST_SELECTOR_PAD (object);
       
   205 
       
   206   switch (prop_id) {
       
   207     case PROP_PAD_TAGS:
       
   208       GST_OBJECT_LOCK (object);
       
   209       g_value_set_boxed (value, pad->tags);
       
   210       GST_OBJECT_UNLOCK (object);
       
   211       break;
       
   212     case PROP_PAD_ACTIVE:
       
   213     {
       
   214       GstStreamSelector *sel;
       
   215 
       
   216       sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
       
   217       g_value_set_boolean (value, gst_stream_selector_is_active_sinkpad (sel,
       
   218               GST_PAD_CAST (pad)));
       
   219       gst_object_unref (sel);
       
   220       break;
       
   221     }
       
   222     default:
       
   223       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       
   224       break;
       
   225   }
       
   226 }
       
   227 
       
   228 static void
       
   229 gst_selector_pad_reset (GstSelectorPad * pad)
       
   230 {
       
   231   pad->active = FALSE;
       
   232   pad->eos = FALSE;
       
   233   gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
       
   234 }
       
   235 
       
   236 /* strictly get the linked pad from the sinkpad. If the pad is active we return
       
   237  * the srcpad else we return NULL */
       
   238 static GList *
       
   239 gst_selector_pad_get_linked_pads (GstPad * pad)
       
   240 {
       
   241   GstPad *otherpad;
       
   242 
       
   243   otherpad = gst_stream_selector_get_linked_pad (pad, TRUE);
       
   244   if (!otherpad)
       
   245     return NULL;
       
   246 
       
   247   /* need to drop the ref, internal linked pads is not MT safe */
       
   248   gst_object_unref (otherpad);
       
   249 
       
   250   return g_list_append (NULL, otherpad);
       
   251 }
       
   252 
       
   253 static gboolean
       
   254 gst_selector_pad_event (GstPad * pad, GstEvent * event)
       
   255 {
       
   256   gboolean res = TRUE;
       
   257   gboolean forward = TRUE;
       
   258   GstStreamSelector *sel;
       
   259   GstSelectorPad *selpad;
       
   260 
       
   261   sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
       
   262   selpad = GST_SELECTOR_PAD_CAST (pad);
       
   263 
       
   264   /* only forward if we are dealing with the active sinkpad */
       
   265   forward = gst_stream_selector_is_active_sinkpad (sel, pad);
       
   266 
       
   267   switch (GST_EVENT_TYPE (event)) {
       
   268     case GST_EVENT_FLUSH_STOP:
       
   269       gst_selector_pad_reset (selpad);
       
   270       break;
       
   271     case GST_EVENT_NEWSEGMENT:
       
   272     {
       
   273       gboolean update;
       
   274       GstFormat format;
       
   275       gdouble rate, arate;
       
   276       gint64 start, stop, time;
       
   277 
       
   278       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
       
   279           &start, &stop, &time);
       
   280 
       
   281       GST_DEBUG_OBJECT (selpad,
       
   282           "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
       
   283           "format %d, "
       
   284           "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
       
   285           G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
       
   286 
       
   287       gst_segment_set_newsegment_full (&selpad->segment, update,
       
   288           rate, arate, format, start, stop, time);
       
   289       /* if we are not going to forward the segment, mark the segment as
       
   290        * pending */
       
   291       if (!forward)
       
   292         selpad->segment_pending = TRUE;
       
   293       break;
       
   294     }
       
   295     case GST_EVENT_TAG:
       
   296     {
       
   297       GstTagList *tags;
       
   298 
       
   299       GST_OBJECT_LOCK (selpad);
       
   300       if (selpad->tags)
       
   301         gst_tag_list_free (selpad->tags);
       
   302       gst_event_parse_tag (event, &tags);
       
   303       if (tags)
       
   304         tags = gst_tag_list_copy (tags);
       
   305       selpad->tags = tags;
       
   306       GST_DEBUG_OBJECT (sel, "received tags %" GST_PTR_FORMAT, selpad->tags);
       
   307       GST_OBJECT_UNLOCK (selpad);
       
   308       break;
       
   309     }
       
   310     case GST_EVENT_EOS:
       
   311       selpad->eos = TRUE;
       
   312       break;
       
   313     default:
       
   314       break;
       
   315   }
       
   316   if (forward)
       
   317     res = gst_pad_push_event (sel->srcpad, event);
       
   318   else
       
   319     gst_event_unref (event);
       
   320 
       
   321   gst_object_unref (sel);
       
   322 
       
   323   return res;
       
   324 }
       
   325 
       
   326 static GstCaps *
       
   327 gst_selector_pad_getcaps (GstPad * pad)
       
   328 {
       
   329   GstStreamSelector *sel;
       
   330   GstCaps *caps;
       
   331 
       
   332   sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
       
   333 
       
   334   GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
       
   335   caps = gst_pad_peer_get_caps (sel->srcpad);
       
   336   if (caps == NULL)
       
   337     caps = gst_caps_new_any ();
       
   338 
       
   339   gst_object_unref (sel);
       
   340 
       
   341   return caps;
       
   342 }
       
   343 
       
   344 static GstFlowReturn
       
   345 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
       
   346     guint size, GstCaps * caps, GstBuffer ** buf)
       
   347 {
       
   348   GstStreamSelector *sel;
       
   349   GstFlowReturn result;
       
   350   GstPad *active_sinkpad;
       
   351 
       
   352   sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
       
   353 
       
   354   active_sinkpad = gst_stream_selector_activate_sinkpad (sel, pad);
       
   355 
       
   356   /* Fallback allocation for buffers from pads except the selected one */
       
   357   if (pad != active_sinkpad) {
       
   358     GST_DEBUG_OBJECT (sel,
       
   359         "Pad %s:%s is not selected. Performing fallback allocation",
       
   360         GST_DEBUG_PAD_NAME (pad));
       
   361 
       
   362     *buf = NULL;
       
   363     result = GST_FLOW_OK;
       
   364   } else {
       
   365     result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
       
   366 
       
   367     /* FIXME: HACK. If buffer alloc returns not-linked, perform a fallback
       
   368      * allocation.  This should NOT be necessary, because playbin should
       
   369      * properly block the source pad from running until it's finished hooking 
       
   370      * everything up, but playbin needs refactoring first. */
       
   371     if (result == GST_FLOW_NOT_LINKED) {
       
   372       GST_DEBUG_OBJECT (sel,
       
   373           "No peer pad yet - performing fallback allocation for pad %s:%s",
       
   374           GST_DEBUG_PAD_NAME (pad));
       
   375 
       
   376       *buf = NULL;
       
   377       result = GST_FLOW_OK;
       
   378     }
       
   379   }
       
   380 
       
   381   gst_object_unref (sel);
       
   382 
       
   383   return result;
       
   384 }
       
   385 
       
   386 static GstFlowReturn
       
   387 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
       
   388 {
       
   389   GstStreamSelector *sel;
       
   390   GstFlowReturn res;
       
   391   GstPad *active_sinkpad;
       
   392   GstSelectorPad *selpad;
       
   393   GstClockTime timestamp;
       
   394   GstSegment *seg;
       
   395 
       
   396   sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
       
   397   selpad = GST_SELECTOR_PAD_CAST (pad);
       
   398   seg = &selpad->segment;
       
   399 
       
   400   active_sinkpad = gst_stream_selector_activate_sinkpad (sel, pad);
       
   401 
       
   402   timestamp = GST_BUFFER_TIMESTAMP (buf);
       
   403   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
       
   404     GST_DEBUG_OBJECT (sel, "received timestamp %" GST_TIME_FORMAT,
       
   405         GST_TIME_ARGS (timestamp));
       
   406     gst_segment_set_last_stop (seg, seg->format, timestamp);
       
   407   }
       
   408 
       
   409   /* Ignore buffers from pads except the selected one */
       
   410   if (pad != active_sinkpad)
       
   411     goto ignore;
       
   412 
       
   413   /* if we have a pending segment, push it out now */
       
   414   if (selpad->segment_pending) {
       
   415     gst_pad_push_event (sel->srcpad, gst_event_new_new_segment_full (FALSE,
       
   416             seg->rate, seg->applied_rate, seg->format, seg->start, seg->stop,
       
   417             seg->time));
       
   418 
       
   419     selpad->segment_pending = FALSE;
       
   420   }
       
   421 
       
   422   /* forward */
       
   423   GST_DEBUG_OBJECT (sel, "Forwarding buffer %p from pad %s:%s", buf,
       
   424       GST_DEBUG_PAD_NAME (pad));
       
   425   res = gst_pad_push (sel->srcpad, buf);
       
   426 done:
       
   427   gst_object_unref (sel);
       
   428   return res;
       
   429   /* dropped buffers */
       
   430 ignore:
       
   431   {
       
   432     GST_DEBUG_OBJECT (sel, "Ignoring buffer %p from pad %s:%s",
       
   433         buf, GST_DEBUG_PAD_NAME (pad));
       
   434     gst_buffer_unref (buf);
       
   435     res = GST_FLOW_NOT_LINKED;
       
   436     goto done;
       
   437   }
       
   438 }
       
   439 
       
   440 static void gst_stream_selector_dispose (GObject * object);
       
   441 static void gst_stream_selector_finalize (GObject * object);
       
   442 
       
   443 static void gst_stream_selector_init (GstStreamSelector * sel);
       
   444 static void gst_stream_selector_base_init (GstStreamSelectorClass * klass);
       
   445 static void gst_stream_selector_class_init (GstStreamSelectorClass * klass);
       
   446 
       
   447 static void gst_stream_selector_set_property (GObject * object,
       
   448     guint prop_id, const GValue * value, GParamSpec * pspec);
       
   449 static void gst_stream_selector_get_property (GObject * object,
       
   450     guint prop_id, GValue * value, GParamSpec * pspec);
       
   451 
       
   452 static GstPad *gst_stream_selector_request_new_pad (GstElement * element,
       
   453     GstPadTemplate * templ, const gchar * unused);
       
   454 static void gst_stream_selector_release_pad (GstElement * element,
       
   455     GstPad * pad);
       
   456 static GList *gst_stream_selector_get_linked_pads (GstPad * pad);
       
   457 static GstCaps *gst_stream_selector_getcaps (GstPad * pad);
       
   458 
       
   459 static GstElementClass *parent_class = NULL;
       
   460 #ifdef __SYMBIAN32__
       
   461 EXPORT_C
       
   462 #endif
       
   463 
       
   464 
       
   465 GType
       
   466 gst_stream_selector_get_type (void)
       
   467 {
       
   468   static GType stream_selector_type = 0;
       
   469 
       
   470   if (!stream_selector_type) {
       
   471     static const GTypeInfo stream_selector_info = {
       
   472       sizeof (GstStreamSelectorClass),
       
   473       (GBaseInitFunc) gst_stream_selector_base_init,
       
   474       NULL,
       
   475       (GClassInitFunc) gst_stream_selector_class_init,
       
   476       NULL,
       
   477       NULL,
       
   478       sizeof (GstStreamSelector),
       
   479       0,
       
   480       (GInstanceInitFunc) gst_stream_selector_init,
       
   481     };
       
   482     stream_selector_type =
       
   483         g_type_register_static (GST_TYPE_ELEMENT,
       
   484         "GstStreamSelector", &stream_selector_info, 0);
       
   485     GST_DEBUG_CATEGORY_INIT (stream_selector_debug,
       
   486         "streamselector", 0, "A stream-selector element");
       
   487   }
       
   488 
       
   489   return stream_selector_type;
       
   490 }
       
   491 
       
   492 static void
       
   493 gst_stream_selector_base_init (GstStreamSelectorClass * klass)
       
   494 {
       
   495   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
       
   496 
       
   497   gst_element_class_set_details (element_class, &gst_stream_selector_details);
       
   498   gst_element_class_add_pad_template (element_class,
       
   499       gst_static_pad_template_get (&gst_stream_selector_sink_factory));
       
   500   gst_element_class_add_pad_template (element_class,
       
   501       gst_static_pad_template_get (&gst_stream_selector_src_factory));
       
   502 }
       
   503 
       
   504 static void
       
   505 gst_stream_selector_class_init (GstStreamSelectorClass * klass)
       
   506 {
       
   507   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
       
   508   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
       
   509 
       
   510   parent_class = g_type_class_peek_parent (klass);
       
   511 
       
   512   gobject_class->dispose = gst_stream_selector_dispose;
       
   513   gobject_class->finalize = gst_stream_selector_finalize;
       
   514 
       
   515   gobject_class->set_property =
       
   516       GST_DEBUG_FUNCPTR (gst_stream_selector_set_property);
       
   517   gobject_class->get_property =
       
   518       GST_DEBUG_FUNCPTR (gst_stream_selector_get_property);
       
   519 
       
   520   g_object_class_install_property (gobject_class, PROP_N_PADS,
       
   521       g_param_spec_uint ("n-pads", "Number of Pads",
       
   522           "The number of sink pads", 0, G_MAXUINT, 0, G_PARAM_READABLE));
       
   523   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
       
   524       g_param_spec_object ("active-pad", "Active Pad",
       
   525           "The currently active sink pad", GST_TYPE_PAD, G_PARAM_READWRITE));
       
   526 
       
   527   gstelement_class->request_new_pad = gst_stream_selector_request_new_pad;
       
   528   gstelement_class->release_pad = gst_stream_selector_release_pad;
       
   529 }
       
   530 
       
   531 static void
       
   532 gst_stream_selector_init (GstStreamSelector * sel)
       
   533 {
       
   534   sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
       
   535   gst_pad_set_internal_link_function (sel->srcpad,
       
   536       GST_DEBUG_FUNCPTR (gst_stream_selector_get_linked_pads));
       
   537   gst_pad_set_getcaps_function (sel->srcpad,
       
   538       GST_DEBUG_FUNCPTR (gst_stream_selector_getcaps));
       
   539   gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
       
   540   /* sinkpad management */
       
   541   sel->padcount = 0;
       
   542   sel->active_sinkpad = NULL;
       
   543   gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
       
   544 }
       
   545 
       
   546 static void
       
   547 gst_stream_selector_dispose (GObject * object)
       
   548 {
       
   549   GstStreamSelector *sel = GST_STREAM_SELECTOR (object);
       
   550 
       
   551   if (sel->active_sinkpad) {
       
   552     gst_object_unref (sel->active_sinkpad);
       
   553     sel->active_sinkpad = NULL;
       
   554   }
       
   555 
       
   556   G_OBJECT_CLASS (parent_class)->dispose (object);
       
   557 }
       
   558 
       
   559 static void
       
   560 gst_stream_selector_finalize (GObject * object)
       
   561 {
       
   562   GstStreamSelector *sel;
       
   563 
       
   564   sel = GST_STREAM_SELECTOR (object);
       
   565 
       
   566   G_OBJECT_CLASS (parent_class)->finalize (object);
       
   567 }
       
   568 
       
   569 static void
       
   570 gst_stream_selector_set_property (GObject * object, guint prop_id,
       
   571     const GValue * value, GParamSpec * pspec)
       
   572 {
       
   573   GstStreamSelector *sel = GST_STREAM_SELECTOR (object);
       
   574 
       
   575   switch (prop_id) {
       
   576     case PROP_ACTIVE_PAD:
       
   577     {
       
   578       GstPad *pad = NULL;
       
   579       GstPad **active_pad_p;
       
   580 
       
   581       pad = g_value_get_object (value);
       
   582 
       
   583       GST_OBJECT_LOCK (object);
       
   584       if (pad != sel->active_sinkpad) {
       
   585         GstSelectorPad *selpad;
       
   586 
       
   587         selpad = GST_SELECTOR_PAD_CAST (pad);
       
   588         /* we can only activate pads that have data received */
       
   589         if (selpad && !selpad->active) {
       
   590           GST_DEBUG_OBJECT (sel, "No data received on pad %" GST_PTR_FORMAT,
       
   591               pad);
       
   592         } else {
       
   593           active_pad_p = &sel->active_sinkpad;
       
   594           gst_object_replace ((GstObject **) active_pad_p,
       
   595               GST_OBJECT_CAST (pad));
       
   596           GST_DEBUG_OBJECT (sel, "New active pad is %" GST_PTR_FORMAT,
       
   597               sel->active_sinkpad);
       
   598         }
       
   599       }
       
   600       GST_OBJECT_UNLOCK (object);
       
   601       break;
       
   602     }
       
   603     default:
       
   604       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       
   605       break;
       
   606   }
       
   607 }
       
   608 
       
   609 static void
       
   610 gst_stream_selector_get_property (GObject * object, guint prop_id,
       
   611     GValue * value, GParamSpec * pspec)
       
   612 {
       
   613   GstStreamSelector *sel = GST_STREAM_SELECTOR (object);
       
   614 
       
   615   switch (prop_id) {
       
   616     case PROP_N_PADS:
       
   617       GST_OBJECT_LOCK (object);
       
   618       g_value_set_uint (value, sel->n_pads);
       
   619       GST_OBJECT_UNLOCK (object);
       
   620       break;
       
   621     case PROP_ACTIVE_PAD:{
       
   622       GST_OBJECT_LOCK (object);
       
   623       g_value_set_object (value, sel->active_sinkpad);
       
   624       GST_OBJECT_UNLOCK (object);
       
   625       break;
       
   626     }
       
   627     default:
       
   628       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       
   629       break;
       
   630   }
       
   631 }
       
   632 
       
   633 static GstPad *
       
   634 gst_stream_selector_get_linked_pad (GstPad * pad, gboolean strict)
       
   635 {
       
   636   GstStreamSelector *sel;
       
   637   GstPad *otherpad = NULL;
       
   638 
       
   639   sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
       
   640 
       
   641   GST_OBJECT_LOCK (sel);
       
   642   if (pad == sel->srcpad)
       
   643     otherpad = sel->active_sinkpad;
       
   644   else if (pad == sel->active_sinkpad || !strict)
       
   645     otherpad = sel->srcpad;
       
   646   if (otherpad)
       
   647     gst_object_ref (otherpad);
       
   648   GST_OBJECT_UNLOCK (sel);
       
   649   gst_object_unref (sel);
       
   650   return otherpad;
       
   651 }
       
   652 
       
   653 static GstCaps *
       
   654 gst_stream_selector_getcaps (GstPad * pad)
       
   655 {
       
   656   GstPad *otherpad;
       
   657   GstObject *parent;
       
   658   GstCaps *caps;
       
   659 
       
   660   otherpad = gst_stream_selector_get_linked_pad (pad, FALSE);
       
   661   parent = gst_object_get_parent (GST_OBJECT (pad));
       
   662   if (!otherpad) {
       
   663     GST_DEBUG_OBJECT (parent,
       
   664         "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
       
   665     caps = gst_caps_new_any ();
       
   666   } else {
       
   667     GST_DEBUG_OBJECT (parent,
       
   668         "Pad %s:%s is linked (to %s:%s), returning peer caps",
       
   669         GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
       
   670     /* if the peer has caps, use those. If the pad is not linked, this function
       
   671      * returns NULL and we return ANY */
       
   672     if (!(caps = gst_pad_peer_get_caps (otherpad)))
       
   673       caps = gst_caps_new_any ();
       
   674     gst_object_unref (otherpad);
       
   675   }
       
   676 
       
   677   gst_object_unref (parent);
       
   678   return caps;
       
   679 }
       
   680 
       
   681 /* check if the pad is the active sinkpad */
       
   682 static gboolean
       
   683 gst_stream_selector_is_active_sinkpad (GstStreamSelector * sel, GstPad * pad)
       
   684 {
       
   685   GstSelectorPad *selpad;
       
   686   gboolean res;
       
   687 
       
   688   selpad = GST_SELECTOR_PAD_CAST (pad);
       
   689 
       
   690   GST_OBJECT_LOCK (sel);
       
   691   res = (pad == sel->active_sinkpad);
       
   692   GST_OBJECT_UNLOCK (sel);
       
   693 
       
   694   return res;
       
   695 }
       
   696 
       
   697 /* Get or create the active sinkpad */
       
   698 static GstPad *
       
   699 gst_stream_selector_activate_sinkpad (GstStreamSelector * sel, GstPad * pad)
       
   700 {
       
   701   GstPad *active_sinkpad;
       
   702   GstSelectorPad *selpad;
       
   703 
       
   704   selpad = GST_SELECTOR_PAD_CAST (pad);
       
   705 
       
   706   GST_OBJECT_LOCK (sel);
       
   707   selpad->active = TRUE;
       
   708   active_sinkpad = sel->active_sinkpad;
       
   709   if (active_sinkpad == NULL) {
       
   710     /* first pad we get an alloc on becomes the activated pad by default */
       
   711     active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
       
   712     GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
       
   713   }
       
   714   GST_OBJECT_UNLOCK (sel);
       
   715 
       
   716   return active_sinkpad;
       
   717 }
       
   718 
       
   719 static GList *
       
   720 gst_stream_selector_get_linked_pads (GstPad * pad)
       
   721 {
       
   722   GstPad *otherpad;
       
   723 
       
   724   otherpad = gst_stream_selector_get_linked_pad (pad, TRUE);
       
   725   if (!otherpad)
       
   726     return NULL;
       
   727   /* need to drop the ref, internal linked pads is not MT safe */
       
   728   gst_object_unref (otherpad);
       
   729   return g_list_append (NULL, otherpad);
       
   730 }
       
   731 
       
   732 static GstPad *
       
   733 gst_stream_selector_request_new_pad (GstElement * element,
       
   734     GstPadTemplate * templ, const gchar * unused)
       
   735 {
       
   736   GstStreamSelector *sel;
       
   737   gchar *name = NULL;
       
   738   GstPad *sinkpad = NULL;
       
   739 
       
   740   sel = GST_STREAM_SELECTOR (element);
       
   741   g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
       
   742   GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
       
   743   GST_OBJECT_LOCK (sel);
       
   744   name = g_strdup_printf ("sink%d", sel->padcount++);
       
   745   sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
       
   746       "name", name, "direction", templ->direction, "template", templ, NULL);
       
   747   g_free (name);
       
   748   sel->n_pads++;
       
   749   GST_OBJECT_UNLOCK (sel);
       
   750 
       
   751   gst_pad_set_event_function (sinkpad,
       
   752       GST_DEBUG_FUNCPTR (gst_selector_pad_event));
       
   753   gst_pad_set_getcaps_function (sinkpad,
       
   754       GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
       
   755   gst_pad_set_chain_function (sinkpad,
       
   756       GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
       
   757   gst_pad_set_internal_link_function (sinkpad,
       
   758       GST_DEBUG_FUNCPTR (gst_selector_pad_get_linked_pads));
       
   759   gst_pad_set_bufferalloc_function (sinkpad,
       
   760       GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
       
   761 
       
   762   gst_pad_set_active (sinkpad, TRUE);
       
   763   gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
       
   764   return sinkpad;
       
   765 }
       
   766 
       
   767 static void
       
   768 gst_stream_selector_release_pad (GstElement * element, GstPad * pad)
       
   769 {
       
   770   GstStreamSelector *sel;
       
   771 
       
   772   sel = GST_STREAM_SELECTOR (element);
       
   773   GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
       
   774 
       
   775   GST_OBJECT_LOCK (sel);
       
   776   /* if the pad was the active pad, makes us select a new one */
       
   777   if (sel->active_sinkpad == pad) {
       
   778     GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
       
   779     sel->active_sinkpad = NULL;
       
   780   }
       
   781   sel->n_pads--;
       
   782   GST_OBJECT_UNLOCK (sel);
       
   783 
       
   784   gst_pad_set_active (pad, FALSE);
       
   785   gst_element_remove_pad (GST_ELEMENT (sel), pad);
       
   786 }