aggregator: Set initial position on first buffer
[platform/upstream/gstreamer.git] / libs / gst / base / gstaggregator.c
index e057278..cd146fd 100644 (file)
  */
 /**
  * SECTION: gstaggregator
+ * @title: GstAggregator
  * @short_description: manages a set of pads with the purpose of
  * aggregating their buffers.
  * @see_also: gstcollectpads for historical reasons.
  *
  * Manages a set of pads with the purpose of aggregating their buffers.
  * Control is given to the subclass when all pads have data.
- * <itemizedlist>
- *  <listitem><para>
- *    Base class for mixers and muxers. Subclasses should at least implement
+ *
+ *  * Base class for mixers and muxers. Subclasses should at least implement
  *    the #GstAggregatorClass.aggregate() virtual method.
- *  </para></listitem>
- *  <listitem><para>
- *    When data is queued on all pads, tha aggregate vmethod is called.
- *  </para></listitem>
- *  <listitem><para>
- *    One can peek at the data on any given GstAggregatorPad with the
+ *
+ *  * When data is queued on all pads, tha aggregate vmethod is called.
+ *
+ *  * One can peek at the data on any given GstAggregatorPad with the
  *    gst_aggregator_pad_get_buffer () method, and take ownership of it
  *    with the gst_aggregator_pad_steal_buffer () method. When a buffer
  *    has been taken with steal_buffer (), a new buffer can be queued
  *    on that pad.
- *  </para></listitem>
- *  <listitem><para>
- *    If the subclass wishes to push a buffer downstream in its aggregate
+ *
+ *  * If the subclass wishes to push a buffer downstream in its aggregate
  *    implementation, it should do so through the
  *    gst_aggregator_finish_buffer () method. This method will take care
  *    of sending and ordering mandatory events such as stream start, caps
  *    and segment.
- *  </para></listitem>
- *  <listitem><para>
- *    Same goes for EOS events, which should not be pushed directly by the
+ *
+ *  * Same goes for EOS events, which should not be pushed directly by the
  *    subclass, it should instead return GST_FLOW_EOS in its aggregate
  *    implementation.
- *  </para></listitem>
- *  <listitem><para>
- *    Note that the aggregator logic regarding gap event handling is to turn
+ *
+ *  * Note that the aggregator logic regarding gap event handling is to turn
  *    these into gap buffers with matching PTS and duration. It will also
  *    flag these buffers with GST_BUFFER_FLAG_GAP and GST_BUFFER_FLAG_DROPPABLE
  *    to ease their identification and subsequent processing.
- *  </para></listitem>
- * </itemizedlist>
+ *
  */
 
 #ifdef HAVE_CONFIG_H
@@ -235,12 +229,10 @@ struct _GstAggregatorPadPrivate
   GMutex flush_lock;
 };
 
-static gboolean
-gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
+/* Must be called with PAD_LOCK held */
+static void
+gst_aggregator_pad_reset_unlocked (GstAggregatorPad * aggpad)
 {
-  GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
-
-  PAD_LOCK (aggpad);
   aggpad->priv->pending_eos = FALSE;
   aggpad->priv->eos = FALSE;
   aggpad->priv->flow_return = GST_FLOW_OK;
@@ -253,6 +245,16 @@ gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
   aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
   aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
   aggpad->priv->time_level = 0;
+  aggpad->priv->first_buffer = TRUE;
+}
+
+static gboolean
+gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
+{
+  GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
+
+  PAD_LOCK (aggpad);
+  gst_aggregator_pad_reset_unlocked (aggpad);
   PAD_UNLOCK (aggpad);
 
   if (klass->flush)
@@ -922,11 +924,9 @@ gst_aggregator_start_srcpad_task (GstAggregator * self)
 {
   GST_INFO_OBJECT (self, "Starting srcpad task");
 
-  if (gst_pad_is_active (self->srcpad)) {
-    self->priv->running = TRUE;
-    gst_pad_start_task (GST_PAD (self->srcpad),
-        (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
-  }
+  self->priv->running = TRUE;
+  gst_pad_start_task (GST_PAD (self->srcpad),
+      (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
 }
 
 static GstFlowReturn
@@ -1026,6 +1026,9 @@ update_time_level (GstAggregatorPad * aggpad, gboolean head)
           GST_FORMAT_TIME, aggpad->priv->head_position);
     else
       aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
+
+    if (!GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_time))
+      aggpad->priv->tail_time = aggpad->priv->head_time;
   } else {
     if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_position) &&
         aggpad->segment.format == GST_FORMAT_TIME)
@@ -1097,8 +1100,6 @@ gst_aggregator_default_sink_event (GstAggregator * self,
         GST_OBJECT_UNLOCK (self);
       }
 
-      aggpad->priv->first_buffer = TRUE;
-
       /* We never forward the event */
       goto eat;
     }
@@ -1139,7 +1140,6 @@ gst_aggregator_default_sink_event (GstAggregator * self,
     }
     case GST_EVENT_STREAM_START:
     {
-      gst_aggregator_start_srcpad_task (self);
       goto eat;
     }
     case GST_EVENT_GAP:
@@ -1366,6 +1366,7 @@ gst_aggregator_request_new_pad (GstElement * element,
   GstAggregator *self;
   GstAggregatorPad *agg_pad;
   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
+  GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
 
   self = GST_AGGREGATOR (element);
 
@@ -1378,7 +1379,7 @@ gst_aggregator_request_new_pad (GstElement * element,
   GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
   self->priv->has_peer_latency = FALSE;
 
-  if (gst_pad_is_active (self->srcpad))
+  if (priv->running)
     gst_pad_set_active (GST_PAD (agg_pad), TRUE);
 
   /* add the pad to the element */
@@ -1787,6 +1788,7 @@ gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
       case GST_PAD_MODE_PUSH:
       {
         GST_INFO_OBJECT (pad, "Activating pad!");
+        gst_aggregator_start_srcpad_task (self);
         return TRUE;
       }
       default:
@@ -2311,10 +2313,11 @@ flushing:
   return FALSE;
 }
 
-static gboolean
+static GstFlowReturn
 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
     GstEvent * event)
 {
+  GstFlowReturn ret = GST_FLOW_OK;
   GstAggregator *self = GST_AGGREGATOR (parent);
   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
@@ -2325,8 +2328,10 @@ gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
     PAD_LOCK (aggpad);
 
     if (aggpad->priv->flow_return != GST_FLOW_OK
-        && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
+        && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
+      ret = aggpad->priv->flow_return;
       goto flushing;
+    }
 
     if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
       GST_OBJECT_LOCK (aggpad);
@@ -2348,10 +2353,17 @@ gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
     SRC_UNLOCK (self);
   }
 
-  if (event)
-    return klass->sink_event (self, aggpad, event);
-  else
-    return TRUE;
+  if (event) {
+    gboolean is_caps = (GST_EVENT_TYPE (event) == GST_EVENT_CAPS);
+
+    if (!klass->sink_event (self, aggpad, event)) {
+      /* Copied from GstPad to convert boolean to a GstFlowReturn in
+       * the event handling func */
+      ret = is_caps ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_ERROR;
+    }
+  }
+
+  return ret;
 
 flushing:
   GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
@@ -2361,7 +2373,8 @@ flushing:
   if (GST_EVENT_IS_STICKY (event))
     gst_pad_store_sticky_event (pad, event);
   gst_event_unref (event);
-  return FALSE;
+
+  return ret;
 }
 
 static gboolean
@@ -2398,8 +2411,8 @@ gst_aggregator_pad_constructed (GObject * object)
 
   gst_pad_set_chain_function (pad,
       GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
-  gst_pad_set_event_function (pad,
-      GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
+  gst_pad_set_event_full_function_full (pad,
+      GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
   gst_pad_set_query_function (pad,
       GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
   gst_pad_set_activatemode_function (pad,
@@ -2453,7 +2466,7 @@ gst_aggregator_pad_init (GstAggregatorPad * pad)
   g_mutex_init (&pad->priv->flush_lock);
   g_mutex_init (&pad->priv->lock);
 
-  pad->priv->first_buffer = TRUE;
+  gst_aggregator_pad_reset_unlocked (pad);
 }
 
 /**