aggregator: use new gst_aggregator_pad_drop_buffer()
[platform/upstream/gstreamer.git] / libs / gst / base / gstaggregator.c
index 3b95e2b..da3a530 100644 (file)
@@ -78,19 +78,19 @@ GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
 #define GST_CAT_DEFAULT aggregator_debug
 
 /* GstAggregatorPad definitions */
-#define PAD_LOCK_EVENT(pad)   G_STMT_START {                            \
-  GST_TRACE_OBJECT (pad, "Taking EVENT lock from thread %p",            \
+#define PAD_LOCK(pad)   G_STMT_START {                            \
+  GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p",            \
         g_thread_self());                                               \
-  g_mutex_lock(&pad->priv->event_lock);                                 \
-  GST_TRACE_OBJECT (pad, "Took EVENT lock from thread %p",              \
+  g_mutex_lock(&pad->priv->lock);                                \
+  GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p",              \
         g_thread_self());                                               \
   } G_STMT_END
 
-#define PAD_UNLOCK_EVENT(pad)  G_STMT_START {                           \
-  GST_TRACE_OBJECT (pad, "Releasing EVENT lock from thread %p",         \
+#define PAD_UNLOCK(pad)  G_STMT_START {                           \
+  GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p",         \
         g_thread_self());                                               \
-  g_mutex_unlock(&pad->priv->event_lock);                               \
-  GST_TRACE_OBJECT (pad, "Release EVENT lock from thread %p",           \
+  g_mutex_unlock(&pad->priv->lock);                                \
+  GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p",           \
         g_thread_self());                                               \
   } G_STMT_END
 
@@ -99,7 +99,7 @@ GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
   GST_LOG_OBJECT (pad, "Waiting for EVENT on thread %p",                \
         g_thread_self());                                               \
   g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond),           \
-      &(pad->priv->event_lock));                                        \
+      (&((GstAggregatorPad*)pad)->priv->lock));                            \
   GST_LOG_OBJECT (pad, "DONE Waiting for EVENT on thread %p",           \
         g_thread_self());                                               \
   } G_STMT_END
@@ -110,21 +110,6 @@ GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
   g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond));    \
   } G_STMT_END
 
-#define GST_AGGREGATOR_SETCAPS_LOCK(self)   G_STMT_START {        \
-  GST_TRACE_OBJECT (self, "Taking SETCAPS lock from thread %p",   \
-        g_thread_self());                                         \
-  g_mutex_lock(&self->priv->setcaps_lock);                        \
-  GST_TRACE_OBJECT (self, "Took SETCAPS lock from thread %p",     \
-        g_thread_self());                                         \
-  } G_STMT_END
-
-#define GST_AGGREGATOR_SETCAPS_UNLOCK(self)   G_STMT_START {        \
-  GST_TRACE_OBJECT (self, "Releasing SETCAPS lock from thread %p",  \
-        g_thread_self());                                           \
-  g_mutex_unlock(&self->priv->setcaps_lock);                        \
-  GST_TRACE_OBJECT (self, "Took SETCAPS lock from thread %p",       \
-        g_thread_self());                                           \
-  } G_STMT_END
 
 #define PAD_STREAM_LOCK(pad)   G_STMT_START {                           \
   GST_TRACE_OBJECT (pad, "Taking lock from thread %p",                  \
@@ -176,14 +161,19 @@ GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
 
 struct _GstAggregatorPadPrivate
 {
+  /* To always be used atomically */
+  gboolean flushing;
+
+  /* Following fields are protected by the PAD_LOCK */
   gboolean pending_flush_start;
   gboolean pending_flush_stop;
   gboolean pending_eos;
-  gboolean flushing;
 
-  GMutex event_lock;
-  GCond event_cond;
+  GstBuffer *buffer;
+  gboolean eos;
 
+  GMutex lock;
+  GCond event_cond;
   GMutex stream_lock;
 };
 
@@ -192,8 +182,10 @@ gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
 {
   GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
 
-  aggpad->eos = FALSE;
+  PAD_LOCK (aggpad);
+  aggpad->priv->eos = FALSE;
   aggpad->priv->flushing = FALSE;
+  PAD_UNLOCK (aggpad);
 
   if (klass->flush)
     return klass->flush (aggpad, agg);
@@ -206,6 +198,8 @@ gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
  *************************************/
 static GstElementClass *aggregator_parent_class = NULL;
 
+/* All members are protected by the object lock unless otherwise noted */
+
 struct _GstAggregatorPrivate
 {
   gint padcount;
@@ -213,23 +207,19 @@ struct _GstAggregatorPrivate
   /* Our state is >= PAUSED */
   gboolean running;             /* protected by SRC_STREAM_LOCK */
 
-
   gint seqnum;
-  gboolean send_stream_start;
+  gboolean send_stream_start;   /* protected by srcpad stream lock */
   gboolean send_segment;
   gboolean flush_seeking;
   gboolean pending_flush_start;
-  gboolean send_eos;
+  gboolean send_eos;            /* protected by srcpad stream lock */
   GstFlowReturn flow_return;
 
-  GstCaps *srccaps;
+  GstCaps *srccaps;             /* protected by the srcpad stream lock */
 
   GstTagList *tags;
   gboolean tags_changed;
 
-  /* Lock to prevent two src setcaps from happening at the same time  */
-  GMutex setcaps_lock;
-
   gboolean latency_live;
   GstClockTime latency_min;
   GstClockTime latency_max;
@@ -238,7 +228,7 @@ struct _GstAggregatorPrivate
   GstClockTime sub_latency_max;
 
   /* aggregate */
-  GstClockID aggregate_id;
+  GstClockID aggregate_id;      /* protected by src_lock */
   GMutex src_lock;
   GCond src_cond;
 
@@ -360,8 +350,13 @@ gst_aggregator_check_pads_ready (GstAggregator * self)
   for (l = sinkpads; l != NULL; l = l->next) {
     pad = l->data;
 
-    if (pad->buffer == NULL && !pad->eos)
+    PAD_LOCK (pad);
+    if (pad->priv->buffer == NULL && !pad->priv->eos) {
+      PAD_UNLOCK (pad);
       goto pad_not_ready;
+    }
+    PAD_UNLOCK (pad);
+
   }
 
   GST_OBJECT_UNLOCK (self);
@@ -385,16 +380,20 @@ pad_not_ready:
 static void
 gst_aggregator_reset_flow_values (GstAggregator * self)
 {
+  GST_OBJECT_LOCK (self);
   self->priv->flow_return = GST_FLOW_FLUSHING;
   self->priv->send_stream_start = TRUE;
   self->priv->send_segment = TRUE;
   gst_segment_init (&self->segment, GST_FORMAT_TIME);
+  GST_OBJECT_UNLOCK (self);
 }
 
 static inline void
 gst_aggregator_push_mandatory_events (GstAggregator * self)
 {
   GstAggregatorPrivate *priv = self->priv;
+  GstEvent *segment = NULL;
+  GstEvent *tags = NULL;
 
   if (self->priv->send_stream_start) {
     gchar s_id[32];
@@ -420,26 +419,30 @@ gst_aggregator_push_mandatory_events (GstAggregator * self)
     self->priv->srccaps = NULL;
   }
 
-  if (g_atomic_int_get (&self->priv->send_segment)) {
-    if (!g_atomic_int_get (&self->priv->flush_seeking)) {
-      GstEvent *segev = gst_event_new_segment (&self->segment);
+  GST_OBJECT_LOCK (self);
+  if (self->priv->send_segment && !self->priv->flush_seeking) {
+    segment = gst_event_new_segment (&self->segment);
 
-      if (!self->priv->seqnum)
-        self->priv->seqnum = gst_event_get_seqnum (segev);
-      else
-        gst_event_set_seqnum (segev, self->priv->seqnum);
+    if (!self->priv->seqnum)
+      self->priv->seqnum = gst_event_get_seqnum (segment);
+    else
+      gst_event_set_seqnum (segment, self->priv->seqnum);
+    self->priv->send_segment = FALSE;
 
-      GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segev);
-      gst_pad_push_event (self->srcpad, segev);
-      g_atomic_int_set (&self->priv->send_segment, FALSE);
-    }
+    GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
   }
 
   if (priv->tags && priv->tags_changed) {
-    gst_pad_push_event (self->srcpad,
-        gst_event_new_tag (gst_tag_list_ref (priv->tags)));
+    tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
     priv->tags_changed = FALSE;
   }
+  GST_OBJECT_UNLOCK (self);
+
+  if (segment)
+    gst_pad_push_event (self->srcpad, segment);
+  if (tags)
+    gst_pad_push_event (self->srcpad, tags);
+
 }
 
 /**
@@ -452,10 +455,10 @@ gst_aggregator_push_mandatory_events (GstAggregator * self)
 void
 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
 {
-  GST_AGGREGATOR_SETCAPS_LOCK (self);
+  GST_PAD_STREAM_LOCK (self->srcpad);
   gst_caps_replace (&self->priv->srccaps, caps);
   gst_aggregator_push_mandatory_events (self);
-  GST_AGGREGATOR_SETCAPS_UNLOCK (self);
+  GST_PAD_STREAM_UNLOCK (self->srcpad);
 }
 
 /**
@@ -472,14 +475,15 @@ gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
 {
   gst_aggregator_push_mandatory_events (self);
 
-  if (!g_atomic_int_get (&self->priv->flush_seeking) &&
-      gst_pad_is_active (self->srcpad)) {
+  GST_OBJECT_LOCK (self);
+  if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
     GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
+    GST_OBJECT_UNLOCK (self);
     return gst_pad_push (self->srcpad, buffer);
   } else {
     GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
-        g_atomic_int_get (&self->priv->flush_seeking),
-        gst_pad_is_active (self->srcpad));
+        self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
+    GST_OBJECT_UNLOCK (self);
     gst_buffer_unref (buffer);
     return GST_FLOW_OK;
   }
@@ -491,9 +495,13 @@ gst_aggregator_push_eos (GstAggregator * self)
   GstEvent *event;
   gst_aggregator_push_mandatory_events (self);
 
-  self->priv->send_eos = FALSE;
   event = gst_event_new_eos ();
+
+  GST_OBJECT_LOCK (self);
+  self->priv->send_eos = FALSE;
   gst_event_set_seqnum (event, self->priv->seqnum);
+  GST_OBJECT_UNLOCK (self);
+
   gst_pad_push_event (self->srcpad, event);
 }
 
@@ -520,7 +528,9 @@ gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
 
   SRC_STREAM_LOCK (self);
 
-  gst_aggregator_get_latency (self, &live, &latency_min, &latency_max);
+  GST_OBJECT_LOCK (self);
+  gst_aggregator_get_latency_unlocked (self, &live, &latency_min, &latency_max);
+  GST_OBJECT_UNLOCK (self);
 
   if (gst_aggregator_check_pads_ready (self)) {
     GST_DEBUG_OBJECT (self, "all pads have data");
@@ -559,15 +569,9 @@ gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
     clock = GST_ELEMENT_CLOCK (self);
     if (clock)
       gst_object_ref (clock);
-    GST_OBJECT_UNLOCK (self);
 
     time = base_time + start;
-
-    if (GST_CLOCK_TIME_IS_VALID (latency_min)) {
-      time += latency_min;
-    } else {
-      time += self->priv->latency;
-    }
+    time += latency_min;
 
     GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
         GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
@@ -578,6 +582,8 @@ gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
         GST_TIME_ARGS (latency_min),
         GST_TIME_ARGS (gst_clock_get_time (clock)));
 
+    GST_OBJECT_UNLOCK (self);
+
     self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
     gst_object_unref (clock);
     SRC_STREAM_UNLOCK (self);
@@ -624,27 +630,40 @@ gst_aggregator_aggregate_func (GstAggregator * self)
 
   GST_LOG_OBJECT (self, "Checking aggregate");
   while (priv->send_eos && priv->running) {
+    GstFlowReturn flow_return;
+
     if (!gst_aggregator_wait_and_check (self, &timeout))
       continue;
 
     GST_TRACE_OBJECT (self, "Actually aggregating!");
 
-    priv->flow_return = klass->aggregate (self, timeout);
+    flow_return = klass->aggregate (self, timeout);
 
-    if (priv->flow_return == GST_FLOW_EOS) {
+    GST_OBJECT_LOCK (self);
+    if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking)
+      priv->flow_return = GST_FLOW_OK;
+    else
+      priv->flow_return = flow_return;
+    GST_OBJECT_UNLOCK (self);
+
+    if (flow_return == GST_FLOW_EOS) {
       gst_aggregator_push_eos (self);
     }
 
-    if (priv->flow_return == GST_FLOW_FLUSHING &&
-        g_atomic_int_get (&priv->flush_seeking))
-      priv->flow_return = GST_FLOW_OK;
-
-    GST_LOG_OBJECT (self, "flow return is %s",
-        gst_flow_get_name (priv->flow_return));
+    GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
 
-    if (priv->flow_return != GST_FLOW_OK)
+    if (flow_return != GST_FLOW_OK)
       break;
   }
+
+  /* Pause the task here, the only ways to get here are:
+   * 1) We're stopping, in which case the task is stopped anyway
+   * 2) We got a flow error above, in which case it might take
+   *    some time to forward the flow return upstream and we
+   *    would otherwise call the task function over and over
+   *    again without doing anything
+   */
+  gst_pad_pause_task (self->srcpad);
 }
 
 static gboolean
@@ -673,7 +692,13 @@ gst_aggregator_start (GstAggregator * self)
 static gboolean
 _check_pending_flush_stop (GstAggregatorPad * pad)
 {
-  return (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
+  gboolean res;
+
+  PAD_LOCK (pad);
+  res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
+  PAD_UNLOCK (pad);
+
+  return res;
 }
 
 static gboolean
@@ -716,33 +741,35 @@ gst_aggregator_flush (GstAggregator * self)
   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
 
   GST_DEBUG_OBJECT (self, "Flushing everything");
-  g_atomic_int_set (&priv->send_segment, TRUE);
-  g_atomic_int_set (&priv->flush_seeking, FALSE);
-  g_atomic_int_set (&priv->tags_changed, FALSE);
+  GST_OBJECT_LOCK (self);
+  priv->send_segment = TRUE;
+  priv->flush_seeking = FALSE;
+  priv->tags_changed = FALSE;
+  GST_OBJECT_UNLOCK (self);
   if (klass->flush)
     ret = klass->flush (self);
 
   return ret;
 }
 
+
+/* Called with GstAggregator's object lock held */
+
 static gboolean
-gst_aggregator_all_flush_stop_received (GstAggregator * self)
+gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
 {
   GList *tmp;
   GstAggregatorPad *tmppad;
 
-  GST_OBJECT_LOCK (self);
   for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
     tmppad = (GstAggregatorPad *) tmp->data;
 
     if (_check_pending_flush_stop (tmppad) == FALSE) {
       GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
           tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
-      GST_OBJECT_UNLOCK (self);
       return FALSE;
     }
   }
-  GST_OBJECT_UNLOCK (self);
 
   return TRUE;
 }
@@ -751,25 +778,30 @@ static void
 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
     GstEvent * event)
 {
-  GstBuffer *tmpbuf;
   GstAggregatorPrivate *priv = self->priv;
   GstAggregatorPadPrivate *padpriv = aggpad->priv;
 
   g_atomic_int_set (&aggpad->priv->flushing, TRUE);
+
   /*  Remove pad buffer and wake up the streaming thread */
-  tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
-  gst_buffer_replace (&tmpbuf, NULL);
+  gst_aggregator_pad_drop_buffer (aggpad);
+
   PAD_STREAM_LOCK (aggpad);
-  if (g_atomic_int_compare_and_exchange (&padpriv->pending_flush_start,
-          TRUE, FALSE) == TRUE) {
+  PAD_LOCK (aggpad);
+  if (padpriv->pending_flush_start) {
     GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
-    g_atomic_int_set (&padpriv->pending_flush_stop, TRUE);
+
+    padpriv->pending_flush_start = FALSE;
+    padpriv->pending_flush_stop = TRUE;
   }
+  PAD_UNLOCK (aggpad);
 
-  if (g_atomic_int_get (&priv->flush_seeking)) {
+  GST_OBJECT_LOCK (self);
+  if (priv->flush_seeking) {
     /* If flush_seeking we forward the first FLUSH_START */
-    if (g_atomic_int_compare_and_exchange (&priv->pending_flush_start,
-            TRUE, FALSE) == TRUE) {
+    if (priv->pending_flush_start) {
+      priv->pending_flush_start = FALSE;
+      GST_OBJECT_UNLOCK (self);
 
       GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
       gst_aggregator_stop_srcpad_task (self, event);
@@ -780,15 +812,16 @@ gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
       GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
       event = NULL;
     } else {
+      GST_OBJECT_UNLOCK (self);
       gst_event_unref (event);
     }
   } else {
+    GST_OBJECT_UNLOCK (self);
     gst_event_unref (event);
   }
   PAD_STREAM_UNLOCK (aggpad);
 
-  tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
-  gst_buffer_replace (&tmpbuf, NULL);
+  gst_aggregator_pad_drop_buffer (aggpad);
 }
 
 /* GstAggregator vmethods default implementations */
@@ -813,26 +846,29 @@ gst_aggregator_default_sink_event (GstAggregator * self,
       GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
 
       gst_aggregator_pad_flush (aggpad, self);
-      if (g_atomic_int_get (&priv->flush_seeking)) {
+      GST_OBJECT_LOCK (self);
+      if (priv->flush_seeking) {
         g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
-
-        if (g_atomic_int_get (&priv->flush_seeking)) {
-          if (gst_aggregator_all_flush_stop_received (self)) {
-            /* That means we received FLUSH_STOP/FLUSH_STOP on
-             * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
-            gst_aggregator_flush (self);
-            gst_pad_push_event (self->srcpad, event);
-            event = NULL;
-            SRC_STREAM_LOCK (self);
-            priv->send_eos = TRUE;
-            SRC_STREAM_BROADCAST (self);
-            SRC_STREAM_UNLOCK (self);
-
-            GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
-            GST_PAD_STREAM_UNLOCK (self->srcpad);
-            gst_aggregator_start_srcpad_task (self);
-          }
+        if (gst_aggregator_all_flush_stop_received_locked (self)) {
+          GST_OBJECT_UNLOCK (self);
+          /* That means we received FLUSH_STOP/FLUSH_STOP on
+           * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
+          gst_aggregator_flush (self);
+          gst_pad_push_event (self->srcpad, event);
+          event = NULL;
+          SRC_STREAM_LOCK (self);
+          priv->send_eos = TRUE;
+          SRC_STREAM_BROADCAST (self);
+          SRC_STREAM_UNLOCK (self);
+
+          GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
+          GST_PAD_STREAM_UNLOCK (self->srcpad);
+          gst_aggregator_start_srcpad_task (self);
+        } else {
+          GST_OBJECT_UNLOCK (self);
         }
+      } else {
+        GST_OBJECT_UNLOCK (self);
       }
 
       /* We never forward the event */
@@ -847,13 +883,13 @@ gst_aggregator_default_sink_event (GstAggregator * self,
        * called
        */
       SRC_STREAM_LOCK (self);
-      PAD_LOCK_EVENT (aggpad);
-      if (!aggpad->buffer) {
-        aggpad->eos = TRUE;
+      PAD_LOCK (aggpad);
+      if (!aggpad->priv->buffer) {
+        aggpad->priv->eos = TRUE;
       } else {
         aggpad->priv->pending_eos = TRUE;
       }
-      PAD_UNLOCK_EVENT (aggpad);
+      PAD_UNLOCK (aggpad);
 
       SRC_STREAM_BROADCAST (self);
       SRC_STREAM_UNLOCK (self);
@@ -861,10 +897,13 @@ gst_aggregator_default_sink_event (GstAggregator * self,
     }
     case GST_EVENT_SEGMENT:
     {
-      PAD_LOCK_EVENT (aggpad);
+      GST_OBJECT_LOCK (aggpad);
       gst_event_copy_segment (event, &aggpad->segment);
+      GST_OBJECT_UNLOCK (aggpad);
+
+      GST_OBJECT_LOCK (self);
       self->priv->seqnum = gst_event_get_seqnum (event);
-      PAD_UNLOCK_EVENT (aggpad);
+      GST_OBJECT_UNLOCK (self);
       goto eat;
     }
     case GST_EVENT_STREAM_START:
@@ -921,10 +960,6 @@ gst_aggregator_stop (GstAggregator * agg)
 
   gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
 
-  if (agg->priv->tags)
-    gst_tag_list_unref (agg->priv->tags);
-  agg->priv->tags = NULL;
-
   klass = GST_AGGREGATOR_GET_CLASS (agg);
 
   if (klass->stop)
@@ -932,6 +967,10 @@ gst_aggregator_stop (GstAggregator * agg)
   else
     result = TRUE;
 
+  if (agg->priv->tags)
+    gst_tag_list_unref (agg->priv->tags);
+  agg->priv->tags = NULL;
+
   return result;
 }
 
@@ -987,7 +1026,6 @@ static void
 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
 {
   GstAggregator *self = GST_AGGREGATOR (element);
-  GstBuffer *tmpbuf;
 
   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
 
@@ -995,8 +1033,7 @@ gst_aggregator_release_pad (GstElement * element, GstPad * pad)
 
   SRC_STREAM_LOCK (self);
   g_atomic_int_set (&aggpad->priv->flushing, TRUE);
-  tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
-  gst_buffer_replace (&tmpbuf, NULL);
+  gst_aggregator_pad_drop_buffer (aggpad);
   gst_element_remove_pad (element, pad);
 
   SRC_STREAM_BROADCAST (self);
@@ -1077,15 +1114,17 @@ gst_aggregator_query_sink_latency_foreach (GstAggregator * self,
     GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
         " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
 
-    if (min != GST_CLOCK_TIME_NONE && min > data->min)
-      data->min = min;
+    if (live) {
+      if (min > data->min)
+        data->min = min;
 
-    if (max != GST_CLOCK_TIME_NONE &&
-        ((data->max != GST_CLOCK_TIME_NONE && max < data->max) ||
-            (data->max == GST_CLOCK_TIME_NONE)))
-      data->max = max;
+      if (data->max == GST_CLOCK_TIME_NONE)
+        data->max = max;
+      else if (max < data->max)
+        data->max = max;
 
-    data->live |= live;
+      data->live = TRUE;
+    }
   }
 
   gst_query_unref (query);
@@ -1094,7 +1133,7 @@ gst_aggregator_query_sink_latency_foreach (GstAggregator * self,
 }
 
 /**
- * gst_aggregator_get_latency:
+ * gst_aggregator_get_latency_unlocked:
  * @self: a #GstAggregator
  * @live: (out) (allow-none): whether @self is live
  * @min_latency: (out) (allow-none): the configured minimum latency of @self
@@ -1104,12 +1143,13 @@ gst_aggregator_query_sink_latency_foreach (GstAggregator * self,
  * query.
  *
  * Typically only called by subclasses.
+ *
+ * MUST be called with the object lock held.
  */
 void
-gst_aggregator_get_latency (GstAggregator * self, gboolean * live,
+gst_aggregator_get_latency_unlocked (GstAggregator * self, gboolean * live,
     GstClockTime * min_latency, GstClockTime * max_latency)
 {
-  GstClockTime our_latency;
   GstClockTime min, max;
 
   g_return_if_fail (GST_IS_AGGREGATOR (self));
@@ -1118,17 +1158,14 @@ gst_aggregator_get_latency (GstAggregator * self, gboolean * live,
   min = self->priv->latency_min;
   max = self->priv->latency_max;
 
+  /* add our own */
+  min += self->priv->latency;
   min += self->priv->sub_latency_min;
   if (GST_CLOCK_TIME_IS_VALID (max)
       && GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max))
     max += self->priv->sub_latency_max;
-
-  our_latency = self->priv->latency;
-  if (GST_CLOCK_TIME_IS_VALID (our_latency)) {
-    min += our_latency;
-    if (GST_CLOCK_TIME_IS_VALID (max))
-      max += our_latency;
-  }
+  else
+    max = GST_CLOCK_TIME_NONE;
 
   if (live)
     *live = self->priv->latency_live;
@@ -1154,18 +1191,9 @@ gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
       gst_aggregator_query_sink_latency_foreach, &data);
   SRC_STREAM_UNLOCK (self);
 
+  GST_OBJECT_LOCK (self);
   our_latency = self->priv->latency;
 
-  if (data.live && GST_CLOCK_TIME_IS_VALID (our_latency) &&
-      our_latency > data.max) {
-    GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
-        ("%s", "Latency too big"),
-        ("The requested latency value is too big for the current pipeline.  "
-            "Limiting to %" G_GINT64_FORMAT, data.max));
-    self->priv->latency = data.max;
-    /* FIXME: shouldn't we g_object_notify() the change here? */
-  }
-
   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (data.min))) {
     GST_WARNING_OBJECT (self, "Invalid minimum latency, using 0");
     data.min = 0;
@@ -1183,19 +1211,27 @@ gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
   self->priv->latency_max = data.max;
 
   /* add our own */
-  if (GST_CLOCK_TIME_IS_VALID (our_latency)) {
-    if (GST_CLOCK_TIME_IS_VALID (data.min))
-      data.min += our_latency;
-    if (GST_CLOCK_TIME_IS_VALID (data.max))
-      data.max += our_latency;
-  }
-
-  if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_min)
-      && GST_CLOCK_TIME_IS_VALID (data.min))
-    data.min += self->priv->sub_latency_min;
+  data.min += our_latency;
+  data.min += self->priv->sub_latency_min;
   if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
       && GST_CLOCK_TIME_IS_VALID (data.max))
     data.max += self->priv->sub_latency_max;
+  else
+    data.max = GST_CLOCK_TIME_NONE;
+
+  if (data.live && data.min > data.max) {
+    GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
+        ("%s", "Latency too big"),
+        ("The requested latency value is too big for the current pipeline.  "
+            "Limiting to %" G_GINT64_FORMAT, data.max));
+    data.min = data.max;
+    /* FIXME: This could in theory become negative, but in
+     * that case all is lost anyway */
+    self->priv->latency -= data.min - data.max;
+    /* FIXME: shouldn't we g_object_notify() the change here? */
+  }
+
+  GST_OBJECT_UNLOCK (self);
 
   GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
       " max:%" G_GINT64_FORMAT, data.live ? "true" : "false", data.min,
@@ -1222,10 +1258,13 @@ gst_aggregator_send_event (GstElement * element, GstEvent * event)
 
     gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
         &start, &stop_type, &stop);
+
+    GST_OBJECT_LOCK (self);
     gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
         stop_type, stop, NULL);
-
     self->priv->seqnum = gst_event_get_seqnum (event);
+    GST_OBJECT_UNLOCK (self);
+
     GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
   }
   GST_STATE_UNLOCK (element);
@@ -1285,7 +1324,7 @@ gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
   EventData *evdata = user_data;
   gboolean ret = TRUE;
   GstPad *peer = gst_pad_get_peer (pad);
-  GstAggregatorPadPrivate *padpriv = GST_AGGREGATOR_PAD (pad)->priv;
+  GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
 
   if (peer) {
     ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
@@ -1319,8 +1358,10 @@ gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
     }
 
     if (evdata->flush) {
-      padpriv->pending_flush_start = FALSE;
-      padpriv->pending_flush_stop = FALSE;
+      PAD_LOCK (aggpad);
+      aggpad->priv->pending_flush_start = FALSE;
+      aggpad->priv->pending_flush_stop = FALSE;
+      PAD_UNLOCK (aggpad);
     }
   } else {
     evdata->one_actually_seeked = TRUE;
@@ -1353,8 +1394,10 @@ gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
     for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
       GstAggregatorPad *pad = l->data;
 
+      PAD_LOCK (pad);
       pad->priv->pending_flush_start = TRUE;
       pad->priv->pending_flush_stop = FALSE;
+      PAD_UNLOCK (pad);
     }
     GST_OBJECT_UNLOCK (self);
   }
@@ -1385,21 +1428,25 @@ gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
 
   flush = flags & GST_SEEK_FLAG_FLUSH;
 
+  GST_OBJECT_LOCK (self);
   if (flush) {
-    g_atomic_int_set (&priv->pending_flush_start, TRUE);
-    g_atomic_int_set (&priv->flush_seeking, TRUE);
+    priv->pending_flush_start = TRUE;
+    priv->flush_seeking = TRUE;
   }
 
   gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
       stop_type, stop, NULL);
+  GST_OBJECT_UNLOCK (self);
 
   /* forward the seek upstream */
   evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, flush);
   event = NULL;
 
   if (!evdata.result || !evdata.one_actually_seeked) {
-    g_atomic_int_set (&priv->flush_seeking, FALSE);
-    g_atomic_int_set (&priv->pending_flush_start, FALSE);
+    GST_OBJECT_LOCK (self);
+    priv->flush_seeking = FALSE;
+    priv->pending_flush_start = FALSE;
+    GST_OBJECT_UNLOCK (self);
   }
 
   GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
@@ -1510,7 +1557,6 @@ gst_aggregator_finalize (GObject * object)
 {
   GstAggregator *self = (GstAggregator *) object;
 
-  g_mutex_clear (&self->priv->setcaps_lock);
   g_mutex_clear (&self->priv->src_lock);
   g_cond_clear (&self->priv->src_cond);
 
@@ -1530,19 +1576,34 @@ static void
 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
 {
   gboolean changed;
+  GstClockTime min, max;
 
   g_return_if_fail (GST_IS_AGGREGATOR (self));
+  g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
 
   GST_OBJECT_LOCK (self);
-
-  if (self->priv->latency_live && self->priv->latency_max != 0 &&
-      GST_CLOCK_TIME_IS_VALID (latency) && latency > self->priv->latency_max) {
-    GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
-        ("%s", "Latency too big"),
-        ("The requested latency value is too big for the latency in the "
-            "current pipeline.  Limiting to %" G_GINT64_FORMAT,
-            self->priv->latency_max));
-    latency = self->priv->latency_max;
+  if (self->priv->latency_live) {
+    min = self->priv->latency_min;
+    max = self->priv->latency_max;
+    /* add our own */
+    min += latency;
+    min += self->priv->sub_latency_min;
+    if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
+        && GST_CLOCK_TIME_IS_VALID (max))
+      max += self->priv->sub_latency_max;
+    else
+      max = GST_CLOCK_TIME_NONE;
+
+    if (GST_CLOCK_TIME_IS_VALID (max) && min > max) {
+      GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
+          ("%s", "Latency too big"),
+          ("The requested latency value is too big for the latency in the "
+              "current pipeline.  Limiting to %" G_GINT64_FORMAT, max));
+      /* FIXME: This could in theory become negative, but in
+       * that case all is lost anyway */
+      latency -= min - max;
+      /* FIXME: shouldn't we g_object_notify() the change here? */
+    }
   }
 
   changed = (self->priv->latency != latency);
@@ -1679,7 +1740,7 @@ gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
 
   self->priv->latency_live = FALSE;
   self->priv->latency_min = self->priv->sub_latency_min = 0;
-  self->priv->latency_max = self->priv->sub_latency_max = GST_CLOCK_TIME_NONE;
+  self->priv->latency_max = self->priv->sub_latency_max = 0;
   gst_aggregator_reset_flow_values (self);
 
   self->srcpad = gst_pad_new_from_template (pad_template, "src");
@@ -1695,7 +1756,6 @@ gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
 
   self->priv->latency = DEFAULT_LATENCY;
 
-  g_mutex_init (&self->priv->setcaps_lock);
   g_mutex_init (&self->priv->src_lock);
   g_cond_init (&self->priv->src_cond);
 }
@@ -1736,6 +1796,7 @@ gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
   GstAggregatorPrivate *priv = self->priv;
   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
   GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
+  GstFlowReturn flow_return;
 
   GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
 
@@ -1744,16 +1805,16 @@ gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
   if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
     goto flushing;
 
-  if (g_atomic_int_get (&aggpad->priv->pending_eos) == TRUE)
+  PAD_LOCK (aggpad);
+  if (aggpad->priv->pending_eos == TRUE)
     goto eos;
 
-  PAD_LOCK_EVENT (aggpad);
-
-  while (aggpad->buffer && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
+  while (aggpad->priv->buffer
+      && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
     GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
     PAD_WAIT_EVENT (aggpad);
   }
-  PAD_UNLOCK_EVENT (aggpad);
+  PAD_UNLOCK (aggpad);
 
   if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
     goto flushing;
@@ -1763,20 +1824,23 @@ gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
   }
 
   SRC_STREAM_LOCK (self);
-  PAD_LOCK_EVENT (aggpad);
-  if (aggpad->buffer)
-    gst_buffer_unref (aggpad->buffer);
-  aggpad->buffer = actual_buf;
-  PAD_UNLOCK_EVENT (aggpad);
+  PAD_LOCK (aggpad);
+  if (aggpad->priv->buffer)
+    gst_buffer_unref (aggpad->priv->buffer);
+  aggpad->priv->buffer = actual_buf;
+  PAD_UNLOCK (aggpad);
   PAD_STREAM_UNLOCK (aggpad);
 
-  if (gst_aggregator_check_pads_ready (self))
-    SRC_STREAM_BROADCAST (self);
+  SRC_STREAM_BROADCAST (self);
   SRC_STREAM_UNLOCK (self);
 
   GST_DEBUG_OBJECT (aggpad, "Done chaining");
 
-  return priv->flow_return;
+  GST_OBJECT_LOCK (self);
+  flow_return = priv->flow_return;
+  GST_OBJECT_UNLOCK (self);
+
+  return flow_return;
 
 flushing:
   PAD_STREAM_UNLOCK (aggpad);
@@ -1787,6 +1851,7 @@ flushing:
   return GST_FLOW_FLUSHING;
 
 eos:
+  PAD_UNLOCK (aggpad);
   PAD_STREAM_UNLOCK (aggpad);
 
   gst_buffer_unref (buffer);
@@ -1803,19 +1868,19 @@ gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
 
   if (GST_QUERY_IS_SERIALIZED (query)) {
-    PAD_LOCK_EVENT (aggpad);
+    PAD_LOCK (aggpad);
 
     if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
-      PAD_UNLOCK_EVENT (aggpad);
+      PAD_UNLOCK (aggpad);
       goto flushing;
     }
 
-    while (aggpad->buffer
+    while (aggpad->priv->buffer
         && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
       GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
       PAD_WAIT_EVENT (aggpad);
     }
-    PAD_UNLOCK_EVENT (aggpad);
+    PAD_UNLOCK (aggpad);
 
     if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
       goto flushing;
@@ -1838,20 +1903,20 @@ gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
 
   if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
       && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
-    PAD_LOCK_EVENT (aggpad);
+    PAD_LOCK (aggpad);
 
     if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
         && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
-      PAD_UNLOCK_EVENT (aggpad);
+      PAD_UNLOCK (aggpad);
       goto flushing;
     }
 
-    while (aggpad->buffer
+    while (aggpad->priv->buffer
         && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
       GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
       PAD_WAIT_EVENT (aggpad);
     }
-    PAD_UNLOCK_EVENT (aggpad);
+    PAD_UNLOCK (aggpad);
 
     if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
         && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
@@ -1876,16 +1941,16 @@ gst_aggregator_pad_activate_mode_func (GstPad * pad,
   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
 
   if (active == FALSE) {
-    PAD_LOCK_EVENT (aggpad);
+    PAD_LOCK (aggpad);
     g_atomic_int_set (&aggpad->priv->flushing, TRUE);
-    gst_buffer_replace (&aggpad->buffer, NULL);
+    gst_buffer_replace (&aggpad->priv->buffer, NULL);
     PAD_BROADCAST_EVENT (aggpad);
-    PAD_UNLOCK_EVENT (aggpad);
+    PAD_UNLOCK (aggpad);
   } else {
-    PAD_LOCK_EVENT (aggpad);
+    PAD_LOCK (aggpad);
     g_atomic_int_set (&aggpad->priv->flushing, FALSE);
     PAD_BROADCAST_EVENT (aggpad);
-    PAD_UNLOCK_EVENT (aggpad);
+    PAD_UNLOCK (aggpad);
   }
 
   return TRUE;
@@ -1916,9 +1981,9 @@ gst_aggregator_pad_finalize (GObject * object)
 {
   GstAggregatorPad *pad = (GstAggregatorPad *) object;
 
-  g_mutex_clear (&pad->priv->event_lock);
   g_cond_clear (&pad->priv->event_cond);
   g_mutex_clear (&pad->priv->stream_lock);
+  g_mutex_clear (&pad->priv->lock);
 
   G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
 }
@@ -1927,11 +1992,8 @@ static void
 gst_aggregator_pad_dispose (GObject * object)
 {
   GstAggregatorPad *pad = (GstAggregatorPad *) object;
-  GstBuffer *buf;
 
-  buf = gst_aggregator_pad_steal_buffer (pad);
-  if (buf)
-    gst_buffer_unref (buf);
+  gst_aggregator_pad_drop_buffer (pad);
 
   G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
 }
@@ -1955,11 +2017,11 @@ gst_aggregator_pad_init (GstAggregatorPad * pad)
       G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
       GstAggregatorPadPrivate);
 
-  pad->buffer = NULL;
-  g_mutex_init (&pad->priv->event_lock);
+  pad->priv->buffer = NULL;
   g_cond_init (&pad->priv->event_cond);
 
   g_mutex_init (&pad->priv->stream_lock);
+  g_mutex_init (&pad->priv->lock);
 }
 
 /**
@@ -1976,24 +2038,46 @@ gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
 {
   GstBuffer *buffer = NULL;
 
-  PAD_LOCK_EVENT (pad);
-  if (pad->buffer) {
+  PAD_LOCK (pad);
+  if (pad->priv->buffer) {
     GST_TRACE_OBJECT (pad, "Consuming buffer");
-    buffer = pad->buffer;
-    pad->buffer = NULL;
+    buffer = pad->priv->buffer;
+    pad->priv->buffer = NULL;
     if (pad->priv->pending_eos) {
       pad->priv->pending_eos = FALSE;
-      pad->eos = TRUE;
+      pad->priv->eos = TRUE;
     }
     PAD_BROADCAST_EVENT (pad);
     GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
   }
-  PAD_UNLOCK_EVENT (pad);
+  PAD_UNLOCK (pad);
 
   return buffer;
 }
 
 /**
+ * gst_aggregator_pad_drop_buffer:
+ * @pad: the pad where to drop any pending buffer
+ *
+ * Drop the buffer currently queued in @pad.
+ *
+ * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
+ */
+gboolean
+gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
+{
+  GstBuffer *buf;
+
+  buf = gst_aggregator_pad_steal_buffer (pad);
+
+  if (buf == NULL)
+    return FALSE;
+
+  gst_buffer_unref (buf);
+  return TRUE;
+}
+
+/**
  * gst_aggregator_pad_get_buffer:
  * @pad: the pad to get buffer from
  *
@@ -2006,14 +2090,26 @@ gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
 {
   GstBuffer *buffer = NULL;
 
-  PAD_LOCK_EVENT (pad);
-  if (pad->buffer)
-    buffer = gst_buffer_ref (pad->buffer);
-  PAD_UNLOCK_EVENT (pad);
+  PAD_LOCK (pad);
+  if (pad->priv->buffer)
+    buffer = gst_buffer_ref (pad->priv->buffer);
+  PAD_UNLOCK (pad);
 
   return buffer;
 }
 
+gboolean
+gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
+{
+  gboolean is_eos;
+
+  PAD_LOCK (pad);
+  is_eos = pad->priv->eos;
+  PAD_UNLOCK (pad);
+
+  return is_eos;
+}
+
 /**
  * gst_aggregator_merge_tags:
  * @self: a #GstAggregator
@@ -2063,15 +2159,25 @@ void
 gst_aggregator_set_latency (GstAggregator * self,
     GstClockTime min_latency, GstClockTime max_latency)
 {
+  gboolean changed = FALSE;
+
   g_return_if_fail (GST_IS_AGGREGATOR (self));
   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
   g_return_if_fail (max_latency >= min_latency);
 
   GST_OBJECT_LOCK (self);
-  self->priv->sub_latency_min = min_latency;
-  self->priv->sub_latency_max = max_latency;
+  if (self->priv->sub_latency_min != min_latency) {
+    self->priv->sub_latency_min = min_latency;
+    changed = TRUE;
+  }
+  if (self->priv->sub_latency_max != max_latency) {
+    self->priv->sub_latency_max = max_latency;
+    changed = TRUE;
+  }
   GST_OBJECT_UNLOCK (self);
 
-  gst_element_post_message (GST_ELEMENT_CAST (self),
-      gst_message_new_latency (GST_OBJECT_CAST (self)));
+  if (changed) {
+    gst_element_post_message (GST_ELEMENT_CAST (self),
+        gst_message_new_latency (GST_OBJECT_CAST (self)));
+  }
 }