meson: Add Autotools changes that weren't mirrored
[platform/upstream/gstreamer.git] / plugins / elements / gstoutputselector.c
index b3b6c36..15d5478 100644 (file)
@@ -13,8 +13,8 @@
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 /**
@@ -22,8 +22,6 @@
  * @see_also: #GstOutputSelector, #GstInputSelector
  *
  * Direct input stream to one out of N output pads.
- *
- * Since: 0.10.32
  */
 
 #ifdef HAVE_CONFIG_H
@@ -44,23 +42,17 @@ GST_STATIC_PAD_TEMPLATE ("sink",
     GST_STATIC_CAPS_ANY);
 
 static GstStaticPadTemplate gst_output_selector_src_factory =
-GST_STATIC_PAD_TEMPLATE ("src%d",
+GST_STATIC_PAD_TEMPLATE ("src_%u",
     GST_PAD_SRC,
     GST_PAD_REQUEST,
     GST_STATIC_CAPS_ANY);
 
-enum GstOutputSelectorPadNegotiationMode
-{
-  GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE,
-  GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL,
-  GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ACTIVE
-};
 #define GST_TYPE_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE (gst_output_selector_pad_negotiation_mode_get_type())
 static GType
 gst_output_selector_pad_negotiation_mode_get_type (void)
 {
   static GType pad_negotiation_mode_type = 0;
-  static GEnumValue pad_negotiation_modes[] = {
+  static const GEnumValue pad_negotiation_modes[] = {
     {GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE, "None", "none"},
     {GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL, "All", "all"},
     {GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ACTIVE, "Active", "active"},
@@ -86,11 +78,11 @@ enum
 
 #define DEFAULT_PAD_NEGOTIATION_MODE GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL
 
-#define _do_init(bla) \
+#define _do_init \
 GST_DEBUG_CATEGORY_INIT (output_selector_debug, \
         "output-selector", 0, "Output stream selector");
-
-GST_BOILERPLATE_FULL (GstOutputSelector, gst_output_selector, GstElement,
+#define gst_output_selector_parent_class parent_class
+G_DEFINE_TYPE_WITH_CODE (GstOutputSelector, gst_output_selector,
     GST_TYPE_ELEMENT, _do_init);
 
 static void gst_output_selector_dispose (GObject * object);
@@ -99,34 +91,21 @@ static void gst_output_selector_set_property (GObject * object,
 static void gst_output_selector_get_property (GObject * object,
     guint prop_id, GValue * value, GParamSpec * pspec);
 static GstPad *gst_output_selector_request_new_pad (GstElement * element,
-    GstPadTemplate * templ, const gchar * unused);
+    GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
 static void gst_output_selector_release_pad (GstElement * element,
     GstPad * pad);
-static GstFlowReturn gst_output_selector_chain (GstPad * pad, GstBuffer * buf);
-static GstFlowReturn gst_output_selector_buffer_alloc (GstPad * pad,
-    guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
+static GstFlowReturn gst_output_selector_chain (GstPad * pad,
+    GstObject * parent, GstBuffer * buf);
 static GstStateChangeReturn gst_output_selector_change_state (GstElement *
     element, GstStateChange transition);
-static gboolean gst_output_selector_handle_sink_event (GstPad * pad,
+static gboolean gst_output_selector_event (GstPad * pad, GstObject * parent,
     GstEvent * event);
+static gboolean gst_output_selector_query (GstPad * pad, GstObject * parent,
+    GstQuery * query);
 static void gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector *
     sel, gint mode);
 
 static void
-gst_output_selector_base_init (gpointer g_class)
-{
-  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
-
-  gst_element_class_set_details_simple (element_class, "Output selector",
-      "Generic", "1-to-N output stream selector",
-      "Stefan Kost <stefan.kost@nokia.com>");
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&gst_output_selector_sink_factory));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&gst_output_selector_src_factory));
-}
-
-static void
 gst_output_selector_class_init (GstOutputSelectorClass * klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
@@ -140,7 +119,8 @@ gst_output_selector_class_init (GstOutputSelectorClass * klass)
   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
       g_param_spec_object ("active-pad", "Active pad",
           "Currently active src pad", GST_TYPE_PAD,
-          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+          G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
+          G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (gobject_class, PROP_RESEND_LATEST,
       g_param_spec_boolean ("resend-latest", "Resend latest buffer",
           "Resend latest buffer after a switch to a new pad", FALSE,
@@ -152,6 +132,14 @@ gst_output_selector_class_init (GstOutputSelectorClass * klass)
           DEFAULT_PAD_NEGOTIATION_MODE,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  gst_element_class_set_static_metadata (gstelement_class, "Output selector",
+      "Generic", "1-to-N output stream selector",
+      "Stefan Kost <stefan.kost@nokia.com>");
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_output_selector_sink_factory);
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_output_selector_src_factory);
+
   gstelement_class->request_new_pad =
       GST_DEBUG_FUNCPTR (gst_output_selector_request_new_pad);
   gstelement_class->release_pad =
@@ -161,8 +149,7 @@ gst_output_selector_class_init (GstOutputSelectorClass * klass)
 }
 
 static void
-gst_output_selector_init (GstOutputSelector * sel,
-    GstOutputSelectorClass * g_class)
+gst_output_selector_init (GstOutputSelector * sel)
 {
   sel->sinkpad =
       gst_pad_new_from_static_template (&gst_output_selector_sink_factory,
@@ -170,9 +157,9 @@ gst_output_selector_init (GstOutputSelector * sel,
   gst_pad_set_chain_function (sel->sinkpad,
       GST_DEBUG_FUNCPTR (gst_output_selector_chain));
   gst_pad_set_event_function (sel->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_output_selector_handle_sink_event));
-  gst_pad_set_bufferalloc_function (sel->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_output_selector_buffer_alloc));
+      GST_DEBUG_FUNCPTR (gst_output_selector_event));
+  gst_pad_set_query_function (sel->sinkpad,
+      GST_DEBUG_FUNCPTR (gst_output_selector_query));
 
   gst_element_add_pad (GST_ELEMENT (sel), sel->sinkpad);
 
@@ -191,14 +178,17 @@ gst_output_selector_init (GstOutputSelector * sel,
 static void
 gst_output_selector_reset (GstOutputSelector * osel)
 {
+  GST_OBJECT_LOCK (osel);
   if (osel->pending_srcpad != NULL) {
     gst_object_unref (osel->pending_srcpad);
     osel->pending_srcpad = NULL;
   }
+
   if (osel->latest_buffer != NULL) {
     gst_buffer_unref (osel->latest_buffer);
     osel->latest_buffer = NULL;
   }
+  GST_OBJECT_UNLOCK (osel);
   gst_segment_init (&osel->segment, GST_FORMAT_UNDEFINED);
 }
 
@@ -291,41 +281,10 @@ gst_output_selector_get_property (GObject * object, guint prop_id,
   }
 }
 
-static GstCaps *
-gst_output_selector_sink_getcaps (GstPad * pad)
-{
-  GstOutputSelector *sel = GST_OUTPUT_SELECTOR (gst_pad_get_parent (pad));
-  GstPad *active = NULL;
-  GstCaps *caps = NULL;
-
-  if (G_UNLIKELY (sel == NULL))
-    return gst_caps_new_any ();
-
-  GST_OBJECT_LOCK (sel);
-  if (sel->pending_srcpad)
-    active = gst_object_ref (sel->pending_srcpad);
-  else if (sel->active_srcpad)
-    active = gst_object_ref (sel->active_srcpad);
-  GST_OBJECT_UNLOCK (sel);
-
-  if (active) {
-    caps = gst_pad_peer_get_caps_reffed (active);
-    gst_object_unref (active);
-  }
-  if (caps == NULL) {
-    caps = gst_caps_new_any ();
-  }
-
-  gst_object_unref (sel);
-  return caps;
-}
-
-static gboolean
-gst_output_selector_sink_setcaps (GstPad * pad, GstCaps * caps)
+static GstPad *
+gst_output_selector_get_active (GstOutputSelector * sel)
 {
-  GstOutputSelector *sel = GST_OUTPUT_SELECTOR (GST_PAD_PARENT (pad));
   GstPad *active = NULL;
-  gboolean ret = TRUE;
 
   GST_OBJECT_LOCK (sel);
   if (sel->pending_srcpad)
@@ -334,11 +293,7 @@ gst_output_selector_sink_setcaps (GstPad * pad, GstCaps * caps)
     active = gst_object_ref (sel->active_srcpad);
   GST_OBJECT_UNLOCK (sel);
 
-  if (active) {
-    ret = gst_pad_set_caps (active, caps);
-    gst_object_unref (active);
-  }
-  return ret;
+  return active;
 }
 
 static void
@@ -346,63 +301,21 @@ gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector * sel,
     gint mode)
 {
   sel->pad_negotiation_mode = mode;
-  if (mode == GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL) {
-    gst_pad_set_getcaps_function (sel->sinkpad, gst_pad_proxy_getcaps);
-    gst_pad_set_setcaps_function (sel->sinkpad, gst_pad_proxy_setcaps);
-  } else if (mode == GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE) {
-    gst_pad_set_getcaps_function (sel->sinkpad, NULL);
-    gst_pad_set_setcaps_function (sel->sinkpad, NULL);
-  } else {                      /* active */
-    gst_pad_set_getcaps_function (sel->sinkpad,
-        gst_output_selector_sink_getcaps);
-    gst_pad_set_setcaps_function (sel->sinkpad,
-        gst_output_selector_sink_setcaps);
-  }
 }
 
-static GstFlowReturn
-gst_output_selector_buffer_alloc (GstPad * pad, guint64 offset, guint size,
-    GstCaps * caps, GstBuffer ** buf)
+static gboolean
+forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
 {
-  GstOutputSelector *sel;
-  GstFlowReturn res;
-  GstPad *allocpad;
+  GstPad *srcpad = GST_PAD_CAST (user_data);
 
-  sel = GST_OUTPUT_SELECTOR (gst_pad_get_parent (pad));
-  if (G_UNLIKELY (sel == NULL))
-    return GST_FLOW_WRONG_STATE;
+  gst_pad_push_event (srcpad, gst_event_ref (*event));
 
-  GST_OBJECT_LOCK (sel);
-  allocpad = sel->pending_srcpad ? sel->pending_srcpad : sel->active_srcpad;
-  if (allocpad) {
-    /* if we had a previous pad we used for allocating a buffer, continue using
-     * it. */
-    GST_DEBUG_OBJECT (sel, "using pad %s:%s for alloc",
-        GST_DEBUG_PAD_NAME (allocpad));
-    gst_object_ref (allocpad);
-    GST_OBJECT_UNLOCK (sel);
-
-    res = gst_pad_alloc_buffer (allocpad, offset, size, caps, buf);
-    gst_object_unref (allocpad);
-
-    GST_OBJECT_LOCK (sel);
-  } else {
-    /* fallback case, allocate a buffer of our own, add pad caps. */
-    GST_DEBUG_OBJECT (pad, "fallback buffer alloc");
-    *buf = NULL;
-    res = GST_FLOW_OK;
-  }
-  GST_OBJECT_UNLOCK (sel);
-
-  GST_DEBUG_OBJECT (sel, "buffer alloc finished: %s", gst_flow_get_name (res));
-
-  gst_object_unref (sel);
-  return res;
+  return TRUE;
 }
 
 static GstPad *
 gst_output_selector_request_new_pad (GstElement * element,
-    GstPadTemplate * templ, const gchar * name)
+    GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
 {
   gchar *padname;
   GstPad *srcpad;
@@ -413,16 +326,25 @@ gst_output_selector_request_new_pad (GstElement * element,
   GST_DEBUG_OBJECT (osel, "requesting pad");
 
   GST_OBJECT_LOCK (osel);
-  padname = g_strdup_printf ("src%d", osel->nb_srcpads++);
+  padname = g_strdup_printf ("src_%u", osel->nb_srcpads++);
   srcpad = gst_pad_new_from_template (templ, padname);
   GST_OBJECT_UNLOCK (osel);
 
   gst_pad_set_active (srcpad, TRUE);
+
+  /* Forward sticky events to the new srcpad */
+  gst_pad_sticky_events_foreach (osel->sinkpad, forward_sticky_events, srcpad);
+
   gst_element_add_pad (GST_ELEMENT (osel), srcpad);
 
   /* Set the first requested src pad as active by default */
+  GST_OBJECT_LOCK (osel);
   if (osel->active_srcpad == NULL) {
     osel->active_srcpad = srcpad;
+    GST_OBJECT_UNLOCK (osel);
+    g_object_notify (G_OBJECT (osel), "active-pad");
+  } else {
+    GST_OBJECT_UNLOCK (osel);
   }
   g_free (padname);
 
@@ -438,6 +360,16 @@ gst_output_selector_release_pad (GstElement * element, GstPad * pad)
 
   GST_DEBUG_OBJECT (osel, "releasing pad");
 
+  /* Disable active pad if it's the to be removed pad */
+  GST_OBJECT_LOCK (osel);
+  if (osel->active_srcpad == pad) {
+    osel->active_srcpad = NULL;
+    GST_OBJECT_UNLOCK (osel);
+    g_object_notify (G_OBJECT (osel), "active-pad");
+  } else {
+    GST_OBJECT_UNLOCK (osel);
+  }
+
   gst_pad_set_active (pad, FALSE);
 
   gst_element_remove_pad (GST_ELEMENT_CAST (osel), pad);
@@ -449,48 +381,68 @@ gst_output_selector_switch (GstOutputSelector * osel)
   gboolean res = FALSE;
   GstEvent *ev = NULL;
   GstSegment *seg = NULL;
-  gint64 start = 0, position = 0;
+  GstPad *active_srcpad;
 
   /* Switch */
-  GST_OBJECT_LOCK (GST_OBJECT (osel));
+  GST_OBJECT_LOCK (osel);
   GST_INFO_OBJECT (osel, "switching to pad %" GST_PTR_FORMAT,
       osel->pending_srcpad);
+  if (!osel->pending_srcpad) {
+    GST_OBJECT_UNLOCK (osel);
+    return TRUE;
+  }
+
   if (gst_pad_is_linked (osel->pending_srcpad)) {
     osel->active_srcpad = osel->pending_srcpad;
     res = TRUE;
   }
   gst_object_unref (osel->pending_srcpad);
   osel->pending_srcpad = NULL;
-  GST_OBJECT_UNLOCK (GST_OBJECT (osel));
+  active_srcpad = res ? gst_object_ref (osel->active_srcpad) : NULL;
+  GST_OBJECT_UNLOCK (osel);
 
-  /* Send NEWSEGMENT event and latest buffer if switching succeeded
+  /* Send SEGMENT event and latest buffer if switching succeeded
    * and we already have a valid segment configured */
   if (res) {
+    GstBuffer *latest_buffer;
+
+    g_object_notify (G_OBJECT (osel), "active-pad");
+
+    GST_OBJECT_LOCK (osel);
+    latest_buffer =
+        osel->latest_buffer ? gst_buffer_ref (osel->latest_buffer) : NULL;
+    GST_OBJECT_UNLOCK (osel);
+
+    gst_pad_sticky_events_foreach (osel->sinkpad, forward_sticky_events,
+        active_srcpad);
+
+    /* update segment if required */
     if (osel->segment.format != GST_FORMAT_UNDEFINED) {
-      /* Send NEWSEGMENT to the pad we are going to switch to */
+      /* Send SEGMENT to the pad we are going to switch to */
       seg = &osel->segment;
-
-      /* If resending then mark newsegment start and position accordingly */
-      if (osel->resend_latest && osel->latest_buffer &&
-          GST_BUFFER_TIMESTAMP_IS_VALID (osel->latest_buffer)) {
-        start = position = GST_BUFFER_TIMESTAMP (osel->latest_buffer);
-      } else {
-        start = position = seg->last_stop;
+      /* If resending then mark segment start and position accordingly */
+      if (osel->resend_latest && latest_buffer &&
+          GST_BUFFER_TIMESTAMP_IS_VALID (latest_buffer)) {
+        seg->position = GST_BUFFER_TIMESTAMP (latest_buffer);
       }
-      ev = gst_event_new_new_segment (TRUE, seg->rate,
-          seg->format, start, seg->stop, position);
-      if (!gst_pad_push_event (osel->active_srcpad, ev)) {
+
+      ev = gst_event_new_segment (seg);
+
+      if (!gst_pad_push_event (active_srcpad, ev)) {
         GST_WARNING_OBJECT (osel,
-            "newsegment handling failed in %" GST_PTR_FORMAT,
-            osel->active_srcpad);
+            "newsegment handling failed in %" GST_PTR_FORMAT, active_srcpad);
       }
     }
 
     /* Resend latest buffer to newly switched pad */
-    if (osel->resend_latest && osel->latest_buffer) {
+    if (osel->resend_latest && latest_buffer) {
       GST_INFO ("resending latest buffer");
-      gst_pad_push (osel->active_srcpad, gst_buffer_ref (osel->latest_buffer));
+      gst_pad_push (active_srcpad, latest_buffer);
+    } else if (latest_buffer) {
+      gst_buffer_unref (latest_buffer);
     }
+
+    gst_object_unref (active_srcpad);
   } else {
     GST_WARNING_OBJECT (osel, "switch failed, pad not linked");
   }
@@ -499,13 +451,14 @@ gst_output_selector_switch (GstOutputSelector * osel)
 }
 
 static GstFlowReturn
-gst_output_selector_chain (GstPad * pad, GstBuffer * buf)
+gst_output_selector_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
 {
   GstFlowReturn res;
   GstOutputSelector *osel;
-  GstClockTime last_stop, duration;
+  GstClockTime position, duration;
+  GstPad *active_srcpad;
 
-  osel = GST_OUTPUT_SELECTOR (gst_pad_get_parent (pad));
+  osel = GST_OUTPUT_SELECTOR (parent);
 
   /*
    * The _switch function might push a buffer if 'resend-latest' is true.
@@ -523,6 +476,14 @@ gst_output_selector_chain (GstPad * pad, GstBuffer * buf)
     gst_output_selector_switch (osel);
   }
 
+  active_srcpad = gst_output_selector_get_active (osel);
+  if (!active_srcpad) {
+    GST_DEBUG_OBJECT (osel, "No active srcpad");
+    gst_buffer_unref (buf);
+    return GST_FLOW_OK;
+  }
+
+  GST_OBJECT_LOCK (osel);
   if (osel->latest_buffer) {
     gst_buffer_unref (osel->latest_buffer);
     osel->latest_buffer = NULL;
@@ -532,24 +493,25 @@ gst_output_selector_chain (GstPad * pad, GstBuffer * buf)
     /* Keep reference to latest buffer to resend it after switch */
     osel->latest_buffer = gst_buffer_ref (buf);
   }
+  GST_OBJECT_UNLOCK (osel);
 
-  /* Keep track of last stop and use it in NEWSEGMENT start after 
+  /* Keep track of last stop and use it in SEGMENT start after
      switching to a new src pad */
-  last_stop = GST_BUFFER_TIMESTAMP (buf);
-  if (GST_CLOCK_TIME_IS_VALID (last_stop)) {
+  position = GST_BUFFER_TIMESTAMP (buf);
+  if (GST_CLOCK_TIME_IS_VALID (position)) {
     duration = GST_BUFFER_DURATION (buf);
     if (GST_CLOCK_TIME_IS_VALID (duration)) {
-      last_stop += duration;
+      position += duration;
     }
     GST_LOG_OBJECT (osel, "setting last stop %" GST_TIME_FORMAT,
-        GST_TIME_ARGS (last_stop));
-    gst_segment_set_last_stop (&osel->segment, osel->segment.format, last_stop);
+        GST_TIME_ARGS (position));
+    osel->segment.position = position;
   }
 
-  GST_LOG_OBJECT (osel, "pushing buffer to %" GST_PTR_FORMAT,
-      osel->active_srcpad);
-  res = gst_pad_push (osel->active_srcpad, buf);
-  gst_object_unref (osel);
+  GST_LOG_OBJECT (osel, "pushing buffer to %" GST_PTR_FORMAT, active_srcpad);
+  res = gst_pad_push (active_srcpad, buf);
+
+  gst_object_unref (active_srcpad);
 
   return res;
 }
@@ -584,54 +546,113 @@ gst_output_selector_change_state (GstElement * element,
 }
 
 static gboolean
-gst_output_selector_handle_sink_event (GstPad * pad, GstEvent * event)
+gst_output_selector_forward_event (GstOutputSelector * sel, GstEvent * event)
 {
   gboolean res = TRUE;
-  GstOutputSelector *sel;
-  GstPad *output_pad = NULL;
+  GstPad *active;
 
-  sel = GST_OUTPUT_SELECTOR (gst_pad_get_parent (pad));
-  if (G_UNLIKELY (sel == NULL)) {
-    gst_event_unref (event);
-    return FALSE;
+  switch (sel->pad_negotiation_mode) {
+    case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL:
+      /* Send to all src pads */
+      res = gst_pad_event_default (sel->sinkpad, GST_OBJECT_CAST (sel), event);
+      break;
+    case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE:
+      gst_event_unref (event);
+      break;
+    default:
+      active = gst_output_selector_get_active (sel);
+      if (active) {
+        res = gst_pad_push_event (active, event);
+        gst_object_unref (active);
+      } else {
+        gst_event_unref (event);
+      }
+      break;
   }
 
+  return res;
+}
+
+static gboolean
+gst_output_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
+{
+  gboolean res = TRUE;
+  GstOutputSelector *sel;
+  GstPad *active = NULL;
+
+  sel = GST_OUTPUT_SELECTOR (parent);
+
   switch (GST_EVENT_TYPE (event)) {
-    case GST_EVENT_NEWSEGMENT:
+    case GST_EVENT_EOS:
+    {
+      res = gst_output_selector_forward_event (sel, event);
+      break;
+    }
+    case GST_EVENT_SEGMENT:
     {
-      gboolean update;
-      GstFormat format;
-      gdouble rate, arate;
-      gint64 start, stop, time;
+      gst_event_copy_segment (event, &sel->segment);
+      GST_DEBUG_OBJECT (sel, "configured SEGMENT %" GST_SEGMENT_FORMAT,
+          &sel->segment);
+      /* fall through */
+    }
+    default:
+    {
+      active = gst_output_selector_get_active (sel);
+      if (active) {
+        res = gst_pad_push_event (active, event);
+        gst_object_unref (active);
+      } else {
+        gst_event_unref (event);
+      }
+      break;
+    }
+  }
 
-      gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
-          &start, &stop, &time);
+  return res;
+}
 
-      GST_DEBUG_OBJECT (sel,
-          "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
-          "format %d, " "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
-          G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
+static gboolean
+gst_output_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
+{
+  gboolean res = TRUE;
+  GstOutputSelector *sel;
+  GstPad *active = NULL;
 
-      gst_segment_set_newsegment_full (&sel->segment, update,
-          rate, arate, format, start, stop, time);
+  sel = GST_OUTPUT_SELECTOR (parent);
 
-      /* Send newsegment to all src pads */
-      gst_pad_event_default (pad, event);
+  switch (GST_QUERY_TYPE (query)) {
+    case GST_QUERY_CAPS:
+    {
+      switch (sel->pad_negotiation_mode) {
+        case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL:
+          /* Send caps to all src pads */
+          res = gst_pad_proxy_query_caps (pad, query);
+          break;
+        case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE:
+          res = FALSE;
+          break;
+        default:
+          active = gst_output_selector_get_active (sel);
+          if (active) {
+            res = gst_pad_peer_query (active, query);
+            gst_object_unref (active);
+          } else {
+            res = FALSE;
+          }
+          break;
+      }
       break;
     }
-    case GST_EVENT_EOS:
-      /* Send eos to all src pads */
-      gst_pad_event_default (pad, event);
-      break;
+    case GST_QUERY_DRAIN:
+      if (sel->latest_buffer) {
+        gst_buffer_unref (sel->latest_buffer);
+        sel->latest_buffer = NULL;
+      }
+      /* fall through */
     default:
-      /* Send other events to pending or active src pad */
-      output_pad =
-          sel->pending_srcpad ? sel->pending_srcpad : sel->active_srcpad;
-      res = gst_pad_push_event (output_pad, event);
+      res = gst_pad_query_default (pad, parent, query);
       break;
   }
 
-  gst_object_unref (sel);
-
   return res;
 }