aggregator: Fix locking when using the clock
[platform/upstream/gstreamer.git] / libs / gst / base / gstaggregator.c
1 /* GStreamer aggregator base class
2  * Copyright (C) 2014 Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
3  * Copyright (C) 2014 Thibault Saunier <tsaunier@gnome.org>
4  *
5  * gstaggregator.c:
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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 /**
23  * SECTION: gstaggregator
24  * @short_description: manages a set of pads with the purpose of
25  * aggregating their buffers.
26  * @see_also: gstcollectpads for historical reasons.
27  *
28  * Manages a set of pads with the purpose of aggregating their buffers.
29  * Control is given to the subclass when all pads have data.
30  * <itemizedlist>
31  *  <listitem><para>
32  *    Base class for mixers and muxers. Subclasses should at least implement
33  *    the #GstAggregatorClass.aggregate() virtual method.
34  *  </para></listitem>
35  *  <listitem><para>
36  *    When data is queued on all pads, tha aggregate vmethod is called.
37  *  </para></listitem>
38  *  <listitem><para>
39  *    One can peek at the data on any given GstAggregatorPad with the
40  *    gst_aggregator_pad_get_buffer () method, and take ownership of it
41  *    with the gst_aggregator_pad_steal_buffer () method. When a buffer
42  *    has been taken with steal_buffer (), a new buffer can be queued
43  *    on that pad.
44  *  </para></listitem>
45  *  <listitem><para>
46  *    If the subclass wishes to push a buffer downstream in its aggregate
47  *    implementation, it should do so through the
48  *    gst_aggregator_finish_buffer () method. This method will take care
49  *    of sending and ordering mandatory events such as stream start, caps
50  *    and segment.
51  *  </para></listitem>
52  *  <listitem><para>
53  *    Same goes for EOS events, which should not be pushed directly by the
54  *    subclass, it should instead return GST_FLOW_EOS in its aggregate
55  *    implementation.
56  *  </para></listitem>
57  *  <listitem><para>
58  *    Note that the aggregator logic regarding gap event handling is to turn
59  *    these into gap buffers with matching PTS and duration. It will also
60  *    flag these buffers with GST_BUFFER_FLAG_GAP and GST_BUFFER_FLAG_DROPPABLE
61  *    to ease their identification and subsequent processing.
62  *  </para></listitem>
63  * </itemizedlist>
64  */
65
66 #ifdef HAVE_CONFIG_H
67 #  include "config.h"
68 #endif
69
70 #include <string.h>             /* strlen */
71
72 #include "gstaggregator.h"
73
74 typedef enum
75 {
76   GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
77   GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
78   GST_AGGREGATOR_START_TIME_SELECTION_SET
79 } GstAggregatorStartTimeSelection;
80
81 static GType
82 gst_aggregator_start_time_selection_get_type (void)
83 {
84   static GType gtype = 0;
85
86   if (gtype == 0) {
87     static const GEnumValue values[] = {
88       {GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
89           "Start at 0 running time (default)", "zero"},
90       {GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
91           "Start at first observed input running time", "first"},
92       {GST_AGGREGATOR_START_TIME_SELECTION_SET,
93           "Set start time with start-time property", "set"},
94       {0, NULL, NULL}
95     };
96
97     gtype = g_enum_register_static ("GstAggregatorStartTimeSelection", values);
98   }
99   return gtype;
100 }
101
102 /*  Might become API */
103 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
104     const GstTagList * tags, GstTagMergeMode mode);
105 static void gst_aggregator_set_latency_property (GstAggregator * agg,
106     gint64 latency);
107 static gint64 gst_aggregator_get_latency_property (GstAggregator * agg);
108
109
110 /* Locking order, locks in this element must always be taken in this order
111  *
112  * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
113  * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
114  * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
115  * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
116  * standard element object lock -> GST_OBJECT_LOCK(agg)
117  * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
118  * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
119  * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
120  */
121
122
123 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
124
125 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
126 #define GST_CAT_DEFAULT aggregator_debug
127
128 /* GstAggregatorPad definitions */
129 #define PAD_LOCK(pad)   G_STMT_START {                                  \
130   GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p",              \
131         g_thread_self());                                               \
132   g_mutex_lock(&pad->priv->lock);                                       \
133   GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p",                \
134         g_thread_self());                                               \
135   } G_STMT_END
136
137 #define PAD_UNLOCK(pad)  G_STMT_START {                                 \
138   GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p",           \
139       g_thread_self());                                                 \
140   g_mutex_unlock(&pad->priv->lock);                                     \
141   GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p",             \
142         g_thread_self());                                               \
143   } G_STMT_END
144
145
146 #define PAD_WAIT_EVENT(pad)   G_STMT_START {                            \
147   GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p",   \
148         g_thread_self());                                               \
149   g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond),           \
150       (&((GstAggregatorPad*)pad)->priv->lock));                         \
151   GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
152         g_thread_self());                                               \
153   } G_STMT_END
154
155 #define PAD_BROADCAST_EVENT(pad) G_STMT_START {                        \
156   GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p",     \
157         g_thread_self());                                              \
158   g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond));    \
159   } G_STMT_END
160
161
162 #define PAD_FLUSH_LOCK(pad)     G_STMT_START {                          \
163   GST_TRACE_OBJECT (pad, "Taking lock from thread %p",                  \
164         g_thread_self());                                               \
165   g_mutex_lock(&pad->priv->flush_lock);                                 \
166   GST_TRACE_OBJECT (pad, "Took lock from thread %p",                    \
167         g_thread_self());                                               \
168   } G_STMT_END
169
170 #define PAD_FLUSH_UNLOCK(pad)   G_STMT_START {                          \
171   GST_TRACE_OBJECT (pad, "Releasing lock from thread %p",               \
172         g_thread_self());                                               \
173   g_mutex_unlock(&pad->priv->flush_lock);                               \
174   GST_TRACE_OBJECT (pad, "Release lock from thread %p",                 \
175         g_thread_self());                                               \
176   } G_STMT_END
177
178 #define SRC_LOCK(self)   G_STMT_START {                             \
179   GST_TRACE_OBJECT (self, "Taking src lock from thread %p",         \
180       g_thread_self());                                             \
181   g_mutex_lock(&self->priv->src_lock);                              \
182   GST_TRACE_OBJECT (self, "Took src lock from thread %p",           \
183         g_thread_self());                                           \
184   } G_STMT_END
185
186 #define SRC_UNLOCK(self)  G_STMT_START {                            \
187   GST_TRACE_OBJECT (self, "Releasing src lock from thread %p",      \
188         g_thread_self());                                           \
189   g_mutex_unlock(&self->priv->src_lock);                            \
190   GST_TRACE_OBJECT (self, "Released src lock from thread %p",       \
191         g_thread_self());                                           \
192   } G_STMT_END
193
194 #define SRC_WAIT(self) G_STMT_START {                               \
195   GST_LOG_OBJECT (self, "Waiting for src on thread %p",             \
196         g_thread_self());                                           \
197   g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock));    \
198   GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p",        \
199         g_thread_self());                                           \
200   } G_STMT_END
201
202 #define SRC_BROADCAST(self) G_STMT_START {                          \
203     GST_LOG_OBJECT (self, "Signaling src from thread %p",           \
204         g_thread_self());                                           \
205     if (self->priv->aggregate_id)                                   \
206       gst_clock_id_unschedule (self->priv->aggregate_id);           \
207     g_cond_broadcast(&(self->priv->src_cond));                      \
208   } G_STMT_END
209
210 struct _GstAggregatorPadPrivate
211 {
212   /* Following fields are protected by the PAD_LOCK */
213   GstFlowReturn flow_return;
214   gboolean pending_flush_start;
215   gboolean pending_flush_stop;
216   gboolean pending_eos;
217
218   gboolean first_buffer;
219
220   GQueue buffers;
221   guint num_buffers;
222   GstClockTime head_position;
223   GstClockTime tail_position;
224   GstClockTime head_time;
225   GstClockTime tail_time;
226   GstClockTime time_level;
227
228   gboolean eos;
229
230   GMutex lock;
231   GCond event_cond;
232   /* This lock prevents a flush start processing happening while
233    * the chain function is also happening.
234    */
235   GMutex flush_lock;
236 };
237
238 static gboolean
239 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
240 {
241   GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
242
243   PAD_LOCK (aggpad);
244   aggpad->priv->pending_eos = FALSE;
245   aggpad->priv->eos = FALSE;
246   aggpad->priv->flow_return = GST_FLOW_OK;
247   GST_OBJECT_LOCK (aggpad);
248   gst_segment_init (&aggpad->segment, GST_FORMAT_UNDEFINED);
249   gst_segment_init (&aggpad->clip_segment, GST_FORMAT_UNDEFINED);
250   GST_OBJECT_UNLOCK (aggpad);
251   aggpad->priv->head_position = GST_CLOCK_TIME_NONE;
252   aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
253   aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
254   aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
255   aggpad->priv->time_level = 0;
256   PAD_UNLOCK (aggpad);
257
258   if (klass->flush)
259     return klass->flush (aggpad, agg);
260
261   return TRUE;
262 }
263
264 /*************************************
265  * GstAggregator implementation  *
266  *************************************/
267 static GstElementClass *aggregator_parent_class = NULL;
268
269 /* All members are protected by the object lock unless otherwise noted */
270
271 struct _GstAggregatorPrivate
272 {
273   gint max_padserial;
274
275   /* Our state is >= PAUSED */
276   gboolean running;             /* protected by src_lock */
277
278   gint seqnum;
279   gboolean send_stream_start;   /* protected by srcpad stream lock */
280   gboolean send_segment;
281   gboolean flush_seeking;
282   gboolean pending_flush_start;
283   gboolean send_eos;            /* protected by srcpad stream lock */
284
285   GstCaps *srccaps;             /* protected by the srcpad stream lock */
286
287   GstTagList *tags;
288   gboolean tags_changed;
289
290   gboolean peer_latency_live;   /* protected by src_lock */
291   GstClockTime peer_latency_min;        /* protected by src_lock */
292   GstClockTime peer_latency_max;        /* protected by src_lock */
293   gboolean has_peer_latency;    /* protected by src_lock */
294
295   GstClockTime sub_latency_min; /* protected by src_lock */
296   GstClockTime sub_latency_max; /* protected by src_lock */
297
298   /* aggregate */
299   GstClockID aggregate_id;      /* protected by src_lock */
300   GMutex src_lock;
301   GCond src_cond;
302
303   gboolean first_buffer;        /* protected by object lock */
304   GstAggregatorStartTimeSelection start_time_selection;
305   GstClockTime start_time;
306
307   /* properties */
308   gint64 latency;               /* protected by both src_lock and all pad locks */
309 };
310
311 typedef struct
312 {
313   GstEvent *event;
314   gboolean result;
315   gboolean flush;
316   gboolean only_to_active_pads;
317
318   gboolean one_actually_seeked;
319 } EventData;
320
321 #define DEFAULT_LATENCY              0
322 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
323 #define DEFAULT_START_TIME           (-1)
324
325 enum
326 {
327   PROP_0,
328   PROP_LATENCY,
329   PROP_START_TIME_SELECTION,
330   PROP_START_TIME,
331   PROP_LAST
332 };
333
334 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
335     GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
336
337 /**
338  * gst_aggregator_iterate_sinkpads:
339  * @self: The #GstAggregator
340  * @func: (scope call): The function to call.
341  * @user_data: (closure): The data to pass to @func.
342  *
343  * Iterate the sinkpads of aggregator to call a function on them.
344  *
345  * This method guarantees that @func will be called only once for each
346  * sink pad.
347  */
348 gboolean
349 gst_aggregator_iterate_sinkpads (GstAggregator * self,
350     GstAggregatorPadForeachFunc func, gpointer user_data)
351 {
352   gboolean result = FALSE;
353   GstIterator *iter;
354   gboolean done = FALSE;
355   GValue item = { 0, };
356   GList *seen_pads = NULL;
357
358   iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
359
360   if (!iter)
361     goto no_iter;
362
363   while (!done) {
364     switch (gst_iterator_next (iter, &item)) {
365       case GST_ITERATOR_OK:
366       {
367         GstAggregatorPad *pad;
368
369         pad = g_value_get_object (&item);
370
371         /* if already pushed, skip. FIXME, find something faster to tag pads */
372         if (pad == NULL || g_list_find (seen_pads, pad)) {
373           g_value_reset (&item);
374           break;
375         }
376
377         GST_LOG_OBJECT (pad, "calling function %s on pad",
378             GST_DEBUG_FUNCPTR_NAME (func));
379
380         result = func (self, pad, user_data);
381
382         done = !result;
383
384         seen_pads = g_list_prepend (seen_pads, pad);
385
386         g_value_reset (&item);
387         break;
388       }
389       case GST_ITERATOR_RESYNC:
390         gst_iterator_resync (iter);
391         break;
392       case GST_ITERATOR_ERROR:
393         GST_ERROR_OBJECT (self,
394             "Could not iterate over internally linked pads");
395         done = TRUE;
396         break;
397       case GST_ITERATOR_DONE:
398         done = TRUE;
399         break;
400     }
401   }
402   g_value_unset (&item);
403   gst_iterator_free (iter);
404
405   if (seen_pads == NULL) {
406     GST_DEBUG_OBJECT (self, "No pad seen");
407     return FALSE;
408   }
409
410   g_list_free (seen_pads);
411
412 no_iter:
413   return result;
414 }
415
416 static gboolean
417 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
418 {
419   return (g_queue_peek_tail (&pad->priv->buffers) == NULL);
420 }
421
422 static gboolean
423 gst_aggregator_check_pads_ready (GstAggregator * self)
424 {
425   GstAggregatorPad *pad;
426   GList *l, *sinkpads;
427
428   GST_LOG_OBJECT (self, "checking pads");
429
430   GST_OBJECT_LOCK (self);
431
432   sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
433   if (sinkpads == NULL)
434     goto no_sinkpads;
435
436   for (l = sinkpads; l != NULL; l = l->next) {
437     pad = l->data;
438
439     PAD_LOCK (pad);
440
441     /* In live mode, having a single pad with buffers is enough to
442      * generate a start time from it. In non-live mode all pads need
443      * to have a buffer
444      */
445     if (self->priv->peer_latency_live &&
446         !gst_aggregator_pad_queue_is_empty (pad))
447       self->priv->first_buffer = FALSE;
448
449     if (gst_aggregator_pad_queue_is_empty (pad) && !pad->priv->eos) {
450       PAD_UNLOCK (pad);
451       goto pad_not_ready;
452     }
453     PAD_UNLOCK (pad);
454
455   }
456
457   self->priv->first_buffer = FALSE;
458
459   GST_OBJECT_UNLOCK (self);
460   GST_LOG_OBJECT (self, "pads are ready");
461   return TRUE;
462
463 no_sinkpads:
464   {
465     GST_LOG_OBJECT (self, "pads not ready: no sink pads");
466     GST_OBJECT_UNLOCK (self);
467     return FALSE;
468   }
469 pad_not_ready:
470   {
471     GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
472     GST_OBJECT_UNLOCK (self);
473     return FALSE;
474   }
475 }
476
477 static void
478 gst_aggregator_reset_flow_values (GstAggregator * self)
479 {
480   GST_OBJECT_LOCK (self);
481   self->priv->send_stream_start = TRUE;
482   self->priv->send_segment = TRUE;
483   gst_segment_init (&self->segment, GST_FORMAT_TIME);
484   self->priv->first_buffer = TRUE;
485   GST_OBJECT_UNLOCK (self);
486 }
487
488 static inline void
489 gst_aggregator_push_mandatory_events (GstAggregator * self)
490 {
491   GstAggregatorPrivate *priv = self->priv;
492   GstEvent *segment = NULL;
493   GstEvent *tags = NULL;
494
495   if (self->priv->send_stream_start) {
496     gchar s_id[32];
497
498     GST_INFO_OBJECT (self, "pushing stream start");
499     /* stream-start (FIXME: create id based on input ids) */
500     g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
501     if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
502       GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
503     }
504     self->priv->send_stream_start = FALSE;
505   }
506
507   if (self->priv->srccaps) {
508
509     GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
510         self->priv->srccaps);
511     if (!gst_pad_push_event (self->srcpad,
512             gst_event_new_caps (self->priv->srccaps))) {
513       GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
514     }
515     gst_caps_unref (self->priv->srccaps);
516     self->priv->srccaps = NULL;
517   }
518
519   GST_OBJECT_LOCK (self);
520   if (self->priv->send_segment && !self->priv->flush_seeking) {
521     segment = gst_event_new_segment (&self->segment);
522
523     if (!self->priv->seqnum)
524       self->priv->seqnum = gst_event_get_seqnum (segment);
525     else
526       gst_event_set_seqnum (segment, self->priv->seqnum);
527     self->priv->send_segment = FALSE;
528
529     GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
530   }
531
532   if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
533     tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
534     priv->tags_changed = FALSE;
535   }
536   GST_OBJECT_UNLOCK (self);
537
538   if (segment)
539     gst_pad_push_event (self->srcpad, segment);
540   if (tags)
541     gst_pad_push_event (self->srcpad, tags);
542
543 }
544
545 /**
546  * gst_aggregator_set_src_caps:
547  * @self: The #GstAggregator
548  * @caps: The #GstCaps to set on the src pad.
549  *
550  * Sets the caps to be used on the src pad.
551  */
552 void
553 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
554 {
555   GST_PAD_STREAM_LOCK (self->srcpad);
556   gst_caps_replace (&self->priv->srccaps, caps);
557   gst_aggregator_push_mandatory_events (self);
558   GST_PAD_STREAM_UNLOCK (self->srcpad);
559 }
560
561 /**
562  * gst_aggregator_finish_buffer:
563  * @self: The #GstAggregator
564  * @buffer: (transfer full): the #GstBuffer to push.
565  *
566  * This method will push the provided output buffer downstream. If needed,
567  * mandatory events such as stream-start, caps, and segment events will be
568  * sent before pushing the buffer.
569  */
570 GstFlowReturn
571 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
572 {
573   gst_aggregator_push_mandatory_events (self);
574
575   GST_OBJECT_LOCK (self);
576   if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
577     GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
578     GST_OBJECT_UNLOCK (self);
579     return gst_pad_push (self->srcpad, buffer);
580   } else {
581     GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
582         self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
583     GST_OBJECT_UNLOCK (self);
584     gst_buffer_unref (buffer);
585     return GST_FLOW_OK;
586   }
587 }
588
589 static void
590 gst_aggregator_push_eos (GstAggregator * self)
591 {
592   GstEvent *event;
593   gst_aggregator_push_mandatory_events (self);
594
595   event = gst_event_new_eos ();
596
597   GST_OBJECT_LOCK (self);
598   self->priv->send_eos = FALSE;
599   gst_event_set_seqnum (event, self->priv->seqnum);
600   GST_OBJECT_UNLOCK (self);
601
602   gst_pad_push_event (self->srcpad, event);
603 }
604
605 static GstClockTime
606 gst_aggregator_get_next_time (GstAggregator * self)
607 {
608   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
609
610   if (klass->get_next_time)
611     return klass->get_next_time (self);
612
613   return GST_CLOCK_TIME_NONE;
614 }
615
616 static gboolean
617 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
618 {
619   GstClockTime latency;
620   GstClockTime start;
621   gboolean res;
622
623   *timeout = FALSE;
624
625   SRC_LOCK (self);
626
627   latency = gst_aggregator_get_latency_unlocked (self);
628
629   if (gst_aggregator_check_pads_ready (self)) {
630     GST_DEBUG_OBJECT (self, "all pads have data");
631     SRC_UNLOCK (self);
632
633     return TRUE;
634   }
635
636   /* Before waiting, check if we're actually still running */
637   if (!self->priv->running || !self->priv->send_eos) {
638     SRC_UNLOCK (self);
639
640     return FALSE;
641   }
642
643   start = gst_aggregator_get_next_time (self);
644
645   /* If we're not live, or if we use the running time
646    * of the first buffer as start time, we wait until
647    * all pads have buffers.
648    * Otherwise (i.e. if we are live!), we wait on the clock
649    * and if a pad does not have a buffer in time we ignore
650    * that pad.
651    */
652   GST_OBJECT_LOCK (self);
653   if (!GST_CLOCK_TIME_IS_VALID (latency) ||
654       !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
655       !GST_CLOCK_TIME_IS_VALID (start) ||
656       (self->priv->first_buffer
657           && self->priv->start_time_selection ==
658           GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
659     /* We wake up here when something happened, and below
660      * then check if we're ready now. If we return FALSE,
661      * we will be directly called again.
662      */
663     GST_OBJECT_UNLOCK (self);
664     SRC_WAIT (self);
665   } else {
666     GstClockTime base_time, time;
667     GstClock *clock;
668     GstClockReturn status;
669     GstClockTimeDiff jitter;
670
671     GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
672         GST_TIME_ARGS (start));
673
674     base_time = GST_ELEMENT_CAST (self)->base_time;
675     clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
676     GST_OBJECT_UNLOCK (self);
677
678     time = base_time + start;
679     time += latency;
680
681     GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
682         GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
683         " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
684         GST_TIME_ARGS (time),
685         GST_TIME_ARGS (base_time),
686         GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
687         GST_TIME_ARGS (gst_clock_get_time (clock)));
688
689     self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
690     gst_object_unref (clock);
691     SRC_UNLOCK (self);
692
693     jitter = 0;
694     status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
695
696     SRC_LOCK (self);
697     if (self->priv->aggregate_id) {
698       gst_clock_id_unref (self->priv->aggregate_id);
699       self->priv->aggregate_id = NULL;
700     }
701
702     GST_DEBUG_OBJECT (self,
703         "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
704         status, GST_STIME_ARGS (jitter));
705
706     /* we timed out */
707     if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
708       SRC_UNLOCK (self);
709       *timeout = TRUE;
710       return TRUE;
711     }
712   }
713
714   res = gst_aggregator_check_pads_ready (self);
715   SRC_UNLOCK (self);
716
717   return res;
718 }
719
720 static gboolean
721 check_events (GstAggregator * self, GstAggregatorPad * pad, gpointer user_data)
722 {
723   GstEvent *event = NULL;
724   GstAggregatorClass *klass = NULL;
725   gboolean *processed_event = user_data;
726
727   do {
728     event = NULL;
729
730     PAD_LOCK (pad);
731     if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
732       pad->priv->pending_eos = FALSE;
733       pad->priv->eos = TRUE;
734     }
735     if (GST_IS_EVENT (g_queue_peek_tail (&pad->priv->buffers))) {
736       event = g_queue_pop_tail (&pad->priv->buffers);
737       PAD_BROADCAST_EVENT (pad);
738     }
739     PAD_UNLOCK (pad);
740     if (event) {
741       if (processed_event)
742         *processed_event = TRUE;
743       if (klass == NULL)
744         klass = GST_AGGREGATOR_GET_CLASS (self);
745
746       GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
747       klass->sink_event (self, pad, event);
748     }
749   } while (event != NULL);
750
751   return TRUE;
752 }
753
754 static void
755 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
756     GstFlowReturn flow_return, gboolean full)
757 {
758   GList *item;
759
760   PAD_LOCK (aggpad);
761   if (flow_return == GST_FLOW_NOT_LINKED)
762     aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
763   else
764     aggpad->priv->flow_return = flow_return;
765
766   item = g_queue_peek_head_link (&aggpad->priv->buffers);
767   while (item) {
768     GList *next = item->next;
769
770     /* In partial flush, we do like the pad, we get rid of non-sticky events
771      * and EOS/SEGMENT.
772      */
773     if (full || GST_IS_BUFFER (item->data) ||
774         GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
775         GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
776         !GST_EVENT_IS_STICKY (item->data)) {
777       gst_mini_object_unref (item->data);
778       g_queue_delete_link (&aggpad->priv->buffers, item);
779     }
780     item = next;
781   }
782   aggpad->priv->num_buffers = 0;
783
784   PAD_BROADCAST_EVENT (aggpad);
785   PAD_UNLOCK (aggpad);
786 }
787
788 static void
789 gst_aggregator_aggregate_func (GstAggregator * self)
790 {
791   GstAggregatorPrivate *priv = self->priv;
792   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
793   gboolean timeout = FALSE;
794
795   if (self->priv->running == FALSE) {
796     GST_DEBUG_OBJECT (self, "Not running anymore");
797     return;
798   }
799
800   GST_LOG_OBJECT (self, "Checking aggregate");
801   while (priv->send_eos && priv->running) {
802     GstFlowReturn flow_return;
803     gboolean processed_event = FALSE;
804
805     gst_aggregator_iterate_sinkpads (self, check_events, NULL);
806
807     if (!gst_aggregator_wait_and_check (self, &timeout))
808       continue;
809
810     gst_aggregator_iterate_sinkpads (self, check_events, &processed_event);
811     if (processed_event)
812       continue;
813
814     GST_TRACE_OBJECT (self, "Actually aggregating!");
815     flow_return = klass->aggregate (self, timeout);
816
817     GST_OBJECT_LOCK (self);
818     if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
819       /* We don't want to set the pads to flushing, but we want to
820        * stop the thread, so just break here */
821       GST_OBJECT_UNLOCK (self);
822       break;
823     }
824     GST_OBJECT_UNLOCK (self);
825
826     if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
827       gst_aggregator_push_eos (self);
828     }
829
830     GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
831
832     if (flow_return != GST_FLOW_OK) {
833       GList *item;
834
835       GST_OBJECT_LOCK (self);
836       for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
837         GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
838
839         gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
840       }
841       GST_OBJECT_UNLOCK (self);
842       break;
843     }
844   }
845
846   /* Pause the task here, the only ways to get here are:
847    * 1) We're stopping, in which case the task is stopped anyway
848    * 2) We got a flow error above, in which case it might take
849    *    some time to forward the flow return upstream and we
850    *    would otherwise call the task function over and over
851    *    again without doing anything
852    */
853   gst_pad_pause_task (self->srcpad);
854 }
855
856 static gboolean
857 gst_aggregator_start (GstAggregator * self)
858 {
859   GstAggregatorClass *klass;
860   gboolean result;
861
862   self->priv->send_stream_start = TRUE;
863   self->priv->send_segment = TRUE;
864   self->priv->send_eos = TRUE;
865   self->priv->srccaps = NULL;
866
867   klass = GST_AGGREGATOR_GET_CLASS (self);
868
869   if (klass->start)
870     result = klass->start (self);
871   else
872     result = TRUE;
873
874   return result;
875 }
876
877 static gboolean
878 _check_pending_flush_stop (GstAggregatorPad * pad)
879 {
880   gboolean res;
881
882   PAD_LOCK (pad);
883   res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
884   PAD_UNLOCK (pad);
885
886   return res;
887 }
888
889 static gboolean
890 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
891 {
892   gboolean res = TRUE;
893
894   GST_INFO_OBJECT (self, "%s srcpad task",
895       flush_start ? "Pausing" : "Stopping");
896
897   SRC_LOCK (self);
898   self->priv->running = FALSE;
899   SRC_BROADCAST (self);
900   SRC_UNLOCK (self);
901
902   if (flush_start) {
903     res = gst_pad_push_event (self->srcpad, flush_start);
904   }
905
906   gst_pad_stop_task (self->srcpad);
907
908   return res;
909 }
910
911 static void
912 gst_aggregator_start_srcpad_task (GstAggregator * self)
913 {
914   GST_INFO_OBJECT (self, "Starting srcpad task");
915
916   self->priv->running = TRUE;
917   gst_pad_start_task (GST_PAD (self->srcpad),
918       (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
919 }
920
921 static GstFlowReturn
922 gst_aggregator_flush (GstAggregator * self)
923 {
924   GstFlowReturn ret = GST_FLOW_OK;
925   GstAggregatorPrivate *priv = self->priv;
926   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
927
928   GST_DEBUG_OBJECT (self, "Flushing everything");
929   GST_OBJECT_LOCK (self);
930   priv->send_segment = TRUE;
931   priv->flush_seeking = FALSE;
932   priv->tags_changed = FALSE;
933   GST_OBJECT_UNLOCK (self);
934   if (klass->flush)
935     ret = klass->flush (self);
936
937   return ret;
938 }
939
940
941 /* Called with GstAggregator's object lock held */
942
943 static gboolean
944 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
945 {
946   GList *tmp;
947   GstAggregatorPad *tmppad;
948
949   for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
950     tmppad = (GstAggregatorPad *) tmp->data;
951
952     if (_check_pending_flush_stop (tmppad) == FALSE) {
953       GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
954           tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
955       return FALSE;
956     }
957   }
958
959   return TRUE;
960 }
961
962 static void
963 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
964     GstEvent * event)
965 {
966   GstAggregatorPrivate *priv = self->priv;
967   GstAggregatorPadPrivate *padpriv = aggpad->priv;
968
969   gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
970
971   PAD_FLUSH_LOCK (aggpad);
972   PAD_LOCK (aggpad);
973   if (padpriv->pending_flush_start) {
974     GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
975
976     padpriv->pending_flush_start = FALSE;
977     padpriv->pending_flush_stop = TRUE;
978   }
979   PAD_UNLOCK (aggpad);
980
981   GST_OBJECT_LOCK (self);
982   if (priv->flush_seeking) {
983     /* If flush_seeking we forward the first FLUSH_START */
984     if (priv->pending_flush_start) {
985       priv->pending_flush_start = FALSE;
986       GST_OBJECT_UNLOCK (self);
987
988       GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
989       gst_aggregator_stop_srcpad_task (self, event);
990
991       GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
992       GST_PAD_STREAM_LOCK (self->srcpad);
993       GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
994       event = NULL;
995     } else {
996       GST_OBJECT_UNLOCK (self);
997       gst_event_unref (event);
998     }
999   } else {
1000     GST_OBJECT_UNLOCK (self);
1001     gst_event_unref (event);
1002   }
1003   PAD_FLUSH_UNLOCK (aggpad);
1004 }
1005
1006 /* Must be called with the the PAD_LOCK held */
1007 static void
1008 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1009 {
1010   if (head) {
1011     if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->head_position) &&
1012         aggpad->clip_segment.format == GST_FORMAT_TIME)
1013       aggpad->priv->head_time =
1014           gst_segment_to_running_time (&aggpad->clip_segment,
1015           GST_FORMAT_TIME, aggpad->priv->head_position);
1016     else
1017       aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
1018   } else {
1019     if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_position) &&
1020         aggpad->segment.format == GST_FORMAT_TIME)
1021       aggpad->priv->tail_time =
1022           gst_segment_to_running_time (&aggpad->segment,
1023           GST_FORMAT_TIME, aggpad->priv->tail_position);
1024     else
1025       aggpad->priv->tail_time = aggpad->priv->head_time;
1026   }
1027
1028   if (aggpad->priv->head_time == GST_CLOCK_TIME_NONE ||
1029       aggpad->priv->tail_time == GST_CLOCK_TIME_NONE) {
1030     aggpad->priv->time_level = 0;
1031     return;
1032   }
1033
1034   if (aggpad->priv->tail_time > aggpad->priv->head_time)
1035     aggpad->priv->time_level = 0;
1036   else
1037     aggpad->priv->time_level = aggpad->priv->head_time -
1038         aggpad->priv->tail_time;
1039 }
1040
1041
1042 /* GstAggregator vmethods default implementations */
1043 static gboolean
1044 gst_aggregator_default_sink_event (GstAggregator * self,
1045     GstAggregatorPad * aggpad, GstEvent * event)
1046 {
1047   gboolean res = TRUE;
1048   GstPad *pad = GST_PAD (aggpad);
1049   GstAggregatorPrivate *priv = self->priv;
1050
1051   switch (GST_EVENT_TYPE (event)) {
1052     case GST_EVENT_FLUSH_START:
1053     {
1054       gst_aggregator_flush_start (self, aggpad, event);
1055       /* We forward only in one case: right after flush_seeking */
1056       event = NULL;
1057       goto eat;
1058     }
1059     case GST_EVENT_FLUSH_STOP:
1060     {
1061       GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
1062
1063       gst_aggregator_pad_flush (aggpad, self);
1064       GST_OBJECT_LOCK (self);
1065       if (priv->flush_seeking) {
1066         g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1067         if (gst_aggregator_all_flush_stop_received_locked (self)) {
1068           GST_OBJECT_UNLOCK (self);
1069           /* That means we received FLUSH_STOP/FLUSH_STOP on
1070            * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1071           gst_aggregator_flush (self);
1072           gst_pad_push_event (self->srcpad, event);
1073           event = NULL;
1074           SRC_LOCK (self);
1075           priv->send_eos = TRUE;
1076           SRC_BROADCAST (self);
1077           SRC_UNLOCK (self);
1078
1079           GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1080           GST_PAD_STREAM_UNLOCK (self->srcpad);
1081           gst_aggregator_start_srcpad_task (self);
1082         } else {
1083           GST_OBJECT_UNLOCK (self);
1084         }
1085       } else {
1086         GST_OBJECT_UNLOCK (self);
1087       }
1088
1089       aggpad->priv->first_buffer = TRUE;
1090
1091       /* We never forward the event */
1092       goto eat;
1093     }
1094     case GST_EVENT_EOS:
1095     {
1096       GST_DEBUG_OBJECT (aggpad, "EOS");
1097
1098       /* We still have a buffer, and we don't want the subclass to have to
1099        * check for it. Mark pending_eos, eos will be set when steal_buffer is
1100        * called
1101        */
1102       SRC_LOCK (self);
1103       PAD_LOCK (aggpad);
1104       if (gst_aggregator_pad_queue_is_empty (aggpad)) {
1105         aggpad->priv->eos = TRUE;
1106       } else {
1107         aggpad->priv->pending_eos = TRUE;
1108       }
1109       PAD_UNLOCK (aggpad);
1110
1111       SRC_BROADCAST (self);
1112       SRC_UNLOCK (self);
1113       goto eat;
1114     }
1115     case GST_EVENT_SEGMENT:
1116     {
1117       PAD_LOCK (aggpad);
1118       GST_OBJECT_LOCK (aggpad);
1119       gst_event_copy_segment (event, &aggpad->segment);
1120       update_time_level (aggpad, FALSE);
1121       GST_OBJECT_UNLOCK (aggpad);
1122       PAD_UNLOCK (aggpad);
1123
1124       GST_OBJECT_LOCK (self);
1125       self->priv->seqnum = gst_event_get_seqnum (event);
1126       GST_OBJECT_UNLOCK (self);
1127       goto eat;
1128     }
1129     case GST_EVENT_STREAM_START:
1130     {
1131       goto eat;
1132     }
1133     case GST_EVENT_GAP:
1134     {
1135       GstClockTime pts, endpts;
1136       GstClockTime duration;
1137       GstBuffer *gapbuf;
1138
1139       gst_event_parse_gap (event, &pts, &duration);
1140       gapbuf = gst_buffer_new ();
1141
1142       if (GST_CLOCK_TIME_IS_VALID (duration))
1143         endpts = pts + duration;
1144       else
1145         endpts = GST_CLOCK_TIME_NONE;
1146
1147       GST_OBJECT_LOCK (aggpad);
1148       res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1149           &pts, &endpts);
1150       GST_OBJECT_UNLOCK (aggpad);
1151
1152       if (!res) {
1153         GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1154         goto eat;
1155       }
1156
1157       if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1158         duration = endpts - pts;
1159       else
1160         duration = GST_CLOCK_TIME_NONE;
1161
1162       GST_BUFFER_PTS (gapbuf) = pts;
1163       GST_BUFFER_DURATION (gapbuf) = duration;
1164       GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1165       GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1166
1167       if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1168           GST_FLOW_OK) {
1169         GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1170         res = FALSE;
1171       }
1172
1173       goto eat;
1174     }
1175     case GST_EVENT_TAG:
1176     {
1177       GstTagList *tags;
1178
1179       gst_event_parse_tag (event, &tags);
1180
1181       if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1182         gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1183         gst_event_unref (event);
1184         event = NULL;
1185         goto eat;
1186       }
1187       break;
1188     }
1189     default:
1190     {
1191       break;
1192     }
1193   }
1194
1195   GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1196   return gst_pad_event_default (pad, GST_OBJECT (self), event);
1197
1198 eat:
1199   GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1200   if (event)
1201     gst_event_unref (event);
1202
1203   return res;
1204 }
1205
1206 static inline gboolean
1207 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1208     gpointer unused_udata)
1209 {
1210   gst_aggregator_pad_flush (pad, self);
1211
1212   return TRUE;
1213 }
1214
1215 static gboolean
1216 gst_aggregator_stop (GstAggregator * agg)
1217 {
1218   GstAggregatorClass *klass;
1219   gboolean result;
1220
1221   gst_aggregator_reset_flow_values (agg);
1222
1223   gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1224
1225   klass = GST_AGGREGATOR_GET_CLASS (agg);
1226
1227   if (klass->stop)
1228     result = klass->stop (agg);
1229   else
1230     result = TRUE;
1231
1232   agg->priv->has_peer_latency = FALSE;
1233   agg->priv->peer_latency_live = FALSE;
1234   agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1235
1236   if (agg->priv->tags)
1237     gst_tag_list_unref (agg->priv->tags);
1238   agg->priv->tags = NULL;
1239
1240   return result;
1241 }
1242
1243 /* GstElement vmethods implementations */
1244 static GstStateChangeReturn
1245 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1246 {
1247   GstStateChangeReturn ret;
1248   GstAggregator *self = GST_AGGREGATOR (element);
1249
1250   switch (transition) {
1251     case GST_STATE_CHANGE_READY_TO_PAUSED:
1252       if (!gst_aggregator_start (self))
1253         goto error_start;
1254       break;
1255     default:
1256       break;
1257   }
1258
1259   if ((ret =
1260           GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1261               transition)) == GST_STATE_CHANGE_FAILURE)
1262     goto failure;
1263
1264
1265   switch (transition) {
1266     case GST_STATE_CHANGE_PAUSED_TO_READY:
1267       if (!gst_aggregator_stop (self)) {
1268         /* What to do in this case? Error out? */
1269         GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1270       }
1271       break;
1272     default:
1273       break;
1274   }
1275
1276   return ret;
1277
1278 /* ERRORS */
1279 failure:
1280   {
1281     GST_ERROR_OBJECT (element, "parent failed state change");
1282     return ret;
1283   }
1284 error_start:
1285   {
1286     GST_ERROR_OBJECT (element, "Subclass failed to start");
1287     return GST_STATE_CHANGE_FAILURE;
1288   }
1289 }
1290
1291 static void
1292 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1293 {
1294   GstAggregator *self = GST_AGGREGATOR (element);
1295   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1296
1297   GST_INFO_OBJECT (pad, "Removing pad");
1298
1299   SRC_LOCK (self);
1300   gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1301   gst_element_remove_pad (element, pad);
1302
1303   self->priv->has_peer_latency = FALSE;
1304   SRC_BROADCAST (self);
1305   SRC_UNLOCK (self);
1306 }
1307
1308 static GstAggregatorPad *
1309 gst_aggregator_default_create_new_pad (GstAggregator * self,
1310     GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1311 {
1312   GstAggregatorPad *agg_pad;
1313   GstAggregatorPrivate *priv = self->priv;
1314   gint serial = 0;
1315   gchar *name = NULL;
1316
1317   if (templ->direction != GST_PAD_SINK ||
1318       g_strcmp0 (templ->name_template, "sink_%u") != 0)
1319     goto not_sink;
1320
1321   GST_OBJECT_LOCK (self);
1322   if (req_name == NULL || strlen (req_name) < 6
1323       || !g_str_has_prefix (req_name, "sink_")) {
1324     /* no name given when requesting the pad, use next available int */
1325     serial = ++priv->max_padserial;
1326   } else {
1327     /* parse serial number from requested padname */
1328     serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1329     if (serial > priv->max_padserial)
1330       priv->max_padserial = serial;
1331   }
1332
1333   name = g_strdup_printf ("sink_%u", serial);
1334   agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1335       "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1336   g_free (name);
1337
1338   GST_OBJECT_UNLOCK (self);
1339
1340   return agg_pad;
1341
1342   /* errors */
1343 not_sink:
1344   {
1345     GST_WARNING_OBJECT (self, "request new pad that is not a SINK pad\n");
1346     return NULL;
1347   }
1348 }
1349
1350 static GstPad *
1351 gst_aggregator_request_new_pad (GstElement * element,
1352     GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1353 {
1354   GstAggregator *self;
1355   GstAggregatorPad *agg_pad;
1356   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1357   GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1358
1359   self = GST_AGGREGATOR (element);
1360
1361   agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1362   if (!agg_pad) {
1363     GST_ERROR_OBJECT (element, "Couldn't create new pad");
1364     return NULL;
1365   }
1366
1367   GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1368   self->priv->has_peer_latency = FALSE;
1369
1370   if (priv->running)
1371     gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1372
1373   /* add the pad to the element */
1374   gst_element_add_pad (element, GST_PAD (agg_pad));
1375
1376   return GST_PAD (agg_pad);
1377 }
1378
1379 /* Must be called with SRC_LOCK held */
1380
1381 static gboolean
1382 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1383 {
1384   gboolean query_ret, live;
1385   GstClockTime our_latency, min, max;
1386
1387   query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1388
1389   if (!query_ret) {
1390     GST_WARNING_OBJECT (self, "Latency query failed");
1391     return FALSE;
1392   }
1393
1394   gst_query_parse_latency (query, &live, &min, &max);
1395
1396   our_latency = self->priv->latency;
1397
1398   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1399     GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1400         ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1401     return FALSE;
1402   }
1403
1404   if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1405     GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1406         ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1407             GST_TIME_FORMAT ". Add queues or other buffering elements.",
1408             GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1409     return FALSE;
1410   }
1411
1412   self->priv->peer_latency_live = live;
1413   self->priv->peer_latency_min = min;
1414   self->priv->peer_latency_max = max;
1415   self->priv->has_peer_latency = TRUE;
1416
1417   /* add our own */
1418   min += our_latency;
1419   min += self->priv->sub_latency_min;
1420   if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1421       && GST_CLOCK_TIME_IS_VALID (max))
1422     max += self->priv->sub_latency_max + our_latency;
1423   else
1424     max = GST_CLOCK_TIME_NONE;
1425
1426   SRC_BROADCAST (self);
1427
1428   GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1429       " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1430
1431   gst_query_set_latency (query, live, min, max);
1432
1433   return query_ret;
1434 }
1435
1436 /*
1437  * MUST be called with the src_lock held.
1438  *
1439  * See  gst_aggregator_get_latency() for doc
1440  */
1441 static GstClockTime
1442 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1443 {
1444   GstClockTime latency;
1445
1446   g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1447
1448   if (!self->priv->has_peer_latency) {
1449     GstQuery *query = gst_query_new_latency ();
1450     gboolean ret;
1451
1452     ret = gst_aggregator_query_latency_unlocked (self, query);
1453     gst_query_unref (query);
1454     if (!ret)
1455       return GST_CLOCK_TIME_NONE;
1456   }
1457
1458   if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1459     return GST_CLOCK_TIME_NONE;
1460
1461   /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1462   latency = self->priv->peer_latency_min;
1463
1464   /* add our own */
1465   latency += self->priv->latency;
1466   latency += self->priv->sub_latency_min;
1467
1468   return latency;
1469 }
1470
1471 /**
1472  * gst_aggregator_get_latency:
1473  * @self: a #GstAggregator
1474  *
1475  * Retrieves the latency values reported by @self in response to the latency
1476  * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1477  * will not wait for the clock.
1478  *
1479  * Typically only called by subclasses.
1480  *
1481  * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1482  */
1483 GstClockTime
1484 gst_aggregator_get_latency (GstAggregator * self)
1485 {
1486   GstClockTime ret;
1487
1488   SRC_LOCK (self);
1489   ret = gst_aggregator_get_latency_unlocked (self);
1490   SRC_UNLOCK (self);
1491
1492   return ret;
1493 }
1494
1495 static gboolean
1496 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1497 {
1498   GstAggregator *self = GST_AGGREGATOR (element);
1499
1500   GST_STATE_LOCK (element);
1501   if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1502       GST_STATE (element) < GST_STATE_PAUSED) {
1503     gdouble rate;
1504     GstFormat fmt;
1505     GstSeekFlags flags;
1506     GstSeekType start_type, stop_type;
1507     gint64 start, stop;
1508
1509     gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1510         &start, &stop_type, &stop);
1511
1512     GST_OBJECT_LOCK (self);
1513     gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1514         stop_type, stop, NULL);
1515     self->priv->seqnum = gst_event_get_seqnum (event);
1516     self->priv->first_buffer = FALSE;
1517     GST_OBJECT_UNLOCK (self);
1518
1519     GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1520   }
1521   GST_STATE_UNLOCK (element);
1522
1523
1524   return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1525       event);
1526 }
1527
1528 static gboolean
1529 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1530 {
1531   gboolean res = TRUE;
1532
1533   switch (GST_QUERY_TYPE (query)) {
1534     case GST_QUERY_SEEKING:
1535     {
1536       GstFormat format;
1537
1538       /* don't pass it along as some (file)sink might claim it does
1539        * whereas with a collectpads in between that will not likely work */
1540       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1541       gst_query_set_seeking (query, format, FALSE, 0, -1);
1542       res = TRUE;
1543
1544       break;
1545     }
1546     case GST_QUERY_LATENCY:
1547       SRC_LOCK (self);
1548       res = gst_aggregator_query_latency_unlocked (self, query);
1549       SRC_UNLOCK (self);
1550       break;
1551     default:
1552       return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1553   }
1554
1555   return res;
1556 }
1557
1558 static gboolean
1559 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1560 {
1561   EventData *evdata = user_data;
1562   gboolean ret = TRUE;
1563   GstPad *peer = gst_pad_get_peer (pad);
1564   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1565
1566   if (peer) {
1567     if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1568       GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1569       ret = TRUE;
1570     } else {
1571       ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1572       GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1573       gst_object_unref (peer);
1574     }
1575   }
1576
1577   if (ret == FALSE) {
1578     if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1579       GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1580
1581       GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1582
1583       if (gst_pad_query (peer, seeking)) {
1584         gboolean seekable;
1585
1586         gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1587
1588         if (seekable == FALSE) {
1589           GST_INFO_OBJECT (pad,
1590               "Source not seekable, We failed but it does not matter!");
1591
1592           ret = TRUE;
1593         }
1594       } else {
1595         GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1596       }
1597
1598       gst_query_unref (seeking);
1599     }
1600
1601     if (evdata->flush) {
1602       PAD_LOCK (aggpad);
1603       aggpad->priv->pending_flush_start = FALSE;
1604       aggpad->priv->pending_flush_stop = FALSE;
1605       PAD_UNLOCK (aggpad);
1606     }
1607   } else {
1608     evdata->one_actually_seeked = TRUE;
1609   }
1610
1611   evdata->result &= ret;
1612
1613   /* Always send to all pads */
1614   return FALSE;
1615 }
1616
1617 static EventData
1618 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1619     GstEvent * event, gboolean flush, gboolean only_to_active_pads)
1620 {
1621   EventData evdata;
1622
1623   evdata.event = event;
1624   evdata.result = TRUE;
1625   evdata.flush = flush;
1626   evdata.one_actually_seeked = FALSE;
1627   evdata.only_to_active_pads = only_to_active_pads;
1628
1629   /* We first need to set all pads as flushing in a first pass
1630    * as flush_start flush_stop is sometimes sent synchronously
1631    * while we send the seek event */
1632   if (flush) {
1633     GList *l;
1634
1635     GST_OBJECT_LOCK (self);
1636     for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1637       GstAggregatorPad *pad = l->data;
1638
1639       PAD_LOCK (pad);
1640       pad->priv->pending_flush_start = TRUE;
1641       pad->priv->pending_flush_stop = FALSE;
1642       PAD_UNLOCK (pad);
1643     }
1644     GST_OBJECT_UNLOCK (self);
1645   }
1646
1647   gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1648
1649   gst_event_unref (event);
1650
1651   return evdata;
1652 }
1653
1654 static gboolean
1655 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1656 {
1657   gdouble rate;
1658   GstFormat fmt;
1659   GstSeekFlags flags;
1660   GstSeekType start_type, stop_type;
1661   gint64 start, stop;
1662   gboolean flush;
1663   EventData evdata;
1664   GstAggregatorPrivate *priv = self->priv;
1665
1666   gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1667       &start, &stop_type, &stop);
1668
1669   GST_INFO_OBJECT (self, "starting SEEK");
1670
1671   flush = flags & GST_SEEK_FLAG_FLUSH;
1672
1673   GST_OBJECT_LOCK (self);
1674   if (flush) {
1675     priv->pending_flush_start = TRUE;
1676     priv->flush_seeking = TRUE;
1677   }
1678
1679   gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1680       stop_type, stop, NULL);
1681
1682   /* Seeking sets a position */
1683   self->priv->first_buffer = FALSE;
1684   GST_OBJECT_UNLOCK (self);
1685
1686   /* forward the seek upstream */
1687   evdata =
1688       gst_aggregator_forward_event_to_all_sinkpads (self, event, flush, FALSE);
1689   event = NULL;
1690
1691   if (!evdata.result || !evdata.one_actually_seeked) {
1692     GST_OBJECT_LOCK (self);
1693     priv->flush_seeking = FALSE;
1694     priv->pending_flush_start = FALSE;
1695     GST_OBJECT_UNLOCK (self);
1696   }
1697
1698   GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1699
1700   return evdata.result;
1701 }
1702
1703 static gboolean
1704 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1705 {
1706   EventData evdata;
1707   gboolean res = TRUE;
1708
1709   switch (GST_EVENT_TYPE (event)) {
1710     case GST_EVENT_SEEK:
1711     {
1712       gst_event_ref (event);
1713       res = gst_aggregator_do_seek (self, event);
1714       gst_event_unref (event);
1715       event = NULL;
1716       goto done;
1717     }
1718     case GST_EVENT_NAVIGATION:
1719     {
1720       /* navigation is rather pointless. */
1721       res = FALSE;
1722       gst_event_unref (event);
1723       goto done;
1724     }
1725     default:
1726     {
1727       break;
1728     }
1729   }
1730
1731   /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
1732    * they will receive a QOS event that has earliest_time=0 (because we can't
1733    * have negative timestamps), and consider their buffer as too late */
1734   evdata =
1735       gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE,
1736       GST_EVENT_TYPE (event) == GST_EVENT_QOS);
1737   res = evdata.result;
1738
1739 done:
1740   return res;
1741 }
1742
1743 static gboolean
1744 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1745     GstEvent * event)
1746 {
1747   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1748
1749   return klass->src_event (GST_AGGREGATOR (parent), event);
1750 }
1751
1752 static gboolean
1753 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1754     GstQuery * query)
1755 {
1756   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1757
1758   return klass->src_query (GST_AGGREGATOR (parent), query);
1759 }
1760
1761 static gboolean
1762 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1763     GstObject * parent, GstPadMode mode, gboolean active)
1764 {
1765   GstAggregator *self = GST_AGGREGATOR (parent);
1766   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1767
1768   if (klass->src_activate) {
1769     if (klass->src_activate (self, mode, active) == FALSE) {
1770       return FALSE;
1771     }
1772   }
1773
1774   if (active == TRUE) {
1775     switch (mode) {
1776       case GST_PAD_MODE_PUSH:
1777       {
1778         GST_INFO_OBJECT (pad, "Activating pad!");
1779         gst_aggregator_start_srcpad_task (self);
1780         return TRUE;
1781       }
1782       default:
1783       {
1784         GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1785         return FALSE;
1786       }
1787     }
1788   }
1789
1790   /* deactivating */
1791   GST_INFO_OBJECT (self, "Deactivating srcpad");
1792   gst_aggregator_stop_srcpad_task (self, FALSE);
1793
1794   return TRUE;
1795 }
1796
1797 static gboolean
1798 gst_aggregator_default_sink_query (GstAggregator * self,
1799     GstAggregatorPad * aggpad, GstQuery * query)
1800 {
1801   GstPad *pad = GST_PAD (aggpad);
1802
1803   return gst_pad_query_default (pad, GST_OBJECT (self), query);
1804 }
1805
1806 static void
1807 gst_aggregator_finalize (GObject * object)
1808 {
1809   GstAggregator *self = (GstAggregator *) object;
1810
1811   g_mutex_clear (&self->priv->src_lock);
1812   g_cond_clear (&self->priv->src_cond);
1813
1814   G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1815 }
1816
1817 /*
1818  * gst_aggregator_set_latency_property:
1819  * @agg: a #GstAggregator
1820  * @latency: the new latency value (in nanoseconds).
1821  *
1822  * Sets the new latency value to @latency. This value is used to limit the
1823  * amount of time a pad waits for data to appear before considering the pad
1824  * as unresponsive.
1825  */
1826 static void
1827 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1828 {
1829   gboolean changed;
1830
1831   g_return_if_fail (GST_IS_AGGREGATOR (self));
1832   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1833
1834   SRC_LOCK (self);
1835   changed = (self->priv->latency != latency);
1836
1837   if (changed) {
1838     GList *item;
1839
1840     GST_OBJECT_LOCK (self);
1841     /* First lock all the pads */
1842     for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1843       GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1844       PAD_LOCK (aggpad);
1845     }
1846
1847     self->priv->latency = latency;
1848
1849     SRC_BROADCAST (self);
1850
1851     /* Now wake up the pads */
1852     for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1853       GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1854       PAD_BROADCAST_EVENT (aggpad);
1855       PAD_UNLOCK (aggpad);
1856     }
1857     GST_OBJECT_UNLOCK (self);
1858   }
1859
1860   SRC_UNLOCK (self);
1861
1862   if (changed)
1863     gst_element_post_message (GST_ELEMENT_CAST (self),
1864         gst_message_new_latency (GST_OBJECT_CAST (self)));
1865 }
1866
1867 /*
1868  * gst_aggregator_get_latency_property:
1869  * @agg: a #GstAggregator
1870  *
1871  * Gets the latency value. See gst_aggregator_set_latency for
1872  * more details.
1873  *
1874  * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1875  * before a pad is deemed unresponsive. A value of -1 means an
1876  * unlimited time.
1877  */
1878 static gint64
1879 gst_aggregator_get_latency_property (GstAggregator * agg)
1880 {
1881   gint64 res;
1882
1883   g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1884
1885   GST_OBJECT_LOCK (agg);
1886   res = agg->priv->latency;
1887   GST_OBJECT_UNLOCK (agg);
1888
1889   return res;
1890 }
1891
1892 static void
1893 gst_aggregator_set_property (GObject * object, guint prop_id,
1894     const GValue * value, GParamSpec * pspec)
1895 {
1896   GstAggregator *agg = GST_AGGREGATOR (object);
1897
1898   switch (prop_id) {
1899     case PROP_LATENCY:
1900       gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1901       break;
1902     case PROP_START_TIME_SELECTION:
1903       agg->priv->start_time_selection = g_value_get_enum (value);
1904       break;
1905     case PROP_START_TIME:
1906       agg->priv->start_time = g_value_get_uint64 (value);
1907       break;
1908     default:
1909       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1910       break;
1911   }
1912 }
1913
1914 static void
1915 gst_aggregator_get_property (GObject * object, guint prop_id,
1916     GValue * value, GParamSpec * pspec)
1917 {
1918   GstAggregator *agg = GST_AGGREGATOR (object);
1919
1920   switch (prop_id) {
1921     case PROP_LATENCY:
1922       g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1923       break;
1924     case PROP_START_TIME_SELECTION:
1925       g_value_set_enum (value, agg->priv->start_time_selection);
1926       break;
1927     case PROP_START_TIME:
1928       g_value_set_uint64 (value, agg->priv->start_time);
1929       break;
1930     default:
1931       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1932       break;
1933   }
1934 }
1935
1936 /* GObject vmethods implementations */
1937 static void
1938 gst_aggregator_class_init (GstAggregatorClass * klass)
1939 {
1940   GObjectClass *gobject_class = (GObjectClass *) klass;
1941   GstElementClass *gstelement_class = (GstElementClass *) klass;
1942
1943   aggregator_parent_class = g_type_class_peek_parent (klass);
1944   g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1945
1946   GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1947       GST_DEBUG_FG_MAGENTA, "GstAggregator");
1948
1949   klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1950
1951   klass->sink_event = gst_aggregator_default_sink_event;
1952   klass->sink_query = gst_aggregator_default_sink_query;
1953
1954   klass->src_event = gst_aggregator_default_src_event;
1955   klass->src_query = gst_aggregator_default_src_query;
1956
1957   klass->create_new_pad = gst_aggregator_default_create_new_pad;
1958
1959   gstelement_class->request_new_pad =
1960       GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1961   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1962   gstelement_class->release_pad =
1963       GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1964   gstelement_class->change_state =
1965       GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1966
1967   gobject_class->set_property = gst_aggregator_set_property;
1968   gobject_class->get_property = gst_aggregator_get_property;
1969   gobject_class->finalize = gst_aggregator_finalize;
1970
1971   g_object_class_install_property (gobject_class, PROP_LATENCY,
1972       g_param_spec_int64 ("latency", "Buffer latency",
1973           "Additional latency in live mode to allow upstream "
1974           "to take longer to produce buffers for the current "
1975           "position (in nanoseconds)", 0,
1976           (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1977           DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1978
1979   g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
1980       g_param_spec_enum ("start-time-selection", "Start Time Selection",
1981           "Decides which start time is output",
1982           gst_aggregator_start_time_selection_get_type (),
1983           DEFAULT_START_TIME_SELECTION,
1984           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1985
1986   g_object_class_install_property (gobject_class, PROP_START_TIME,
1987       g_param_spec_uint64 ("start-time", "Start Time",
1988           "Start time to use if start-time-selection=set", 0,
1989           G_MAXUINT64,
1990           DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1991
1992   GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1993 }
1994
1995 static void
1996 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1997 {
1998   GstPadTemplate *pad_template;
1999   GstAggregatorPrivate *priv;
2000
2001   g_return_if_fail (klass->aggregate != NULL);
2002
2003   self->priv =
2004       G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2005       GstAggregatorPrivate);
2006
2007   priv = self->priv;
2008
2009   pad_template =
2010       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2011   g_return_if_fail (pad_template != NULL);
2012
2013   priv->max_padserial = -1;
2014   priv->tags_changed = FALSE;
2015
2016   self->priv->peer_latency_live = FALSE;
2017   self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2018   self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2019   self->priv->has_peer_latency = FALSE;
2020   gst_aggregator_reset_flow_values (self);
2021
2022   self->srcpad = gst_pad_new_from_template (pad_template, "src");
2023
2024   gst_pad_set_event_function (self->srcpad,
2025       GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2026   gst_pad_set_query_function (self->srcpad,
2027       GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2028   gst_pad_set_activatemode_function (self->srcpad,
2029       GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2030
2031   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2032
2033   self->priv->latency = DEFAULT_LATENCY;
2034   self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2035   self->priv->start_time = DEFAULT_START_TIME;
2036
2037   g_mutex_init (&self->priv->src_lock);
2038   g_cond_init (&self->priv->src_cond);
2039 }
2040
2041 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2042  * method to get to the padtemplates */
2043 GType
2044 gst_aggregator_get_type (void)
2045 {
2046   static volatile gsize type = 0;
2047
2048   if (g_once_init_enter (&type)) {
2049     GType _type;
2050     static const GTypeInfo info = {
2051       sizeof (GstAggregatorClass),
2052       NULL,
2053       NULL,
2054       (GClassInitFunc) gst_aggregator_class_init,
2055       NULL,
2056       NULL,
2057       sizeof (GstAggregator),
2058       0,
2059       (GInstanceInitFunc) gst_aggregator_init,
2060     };
2061
2062     _type = g_type_register_static (GST_TYPE_ELEMENT,
2063         "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2064     g_once_init_leave (&type, _type);
2065   }
2066   return type;
2067 }
2068
2069 /* Must be called with SRC lock and PAD lock held */
2070 static gboolean
2071 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2072 {
2073   /* Empty queue always has space */
2074   if (g_queue_get_length (&aggpad->priv->buffers) == 0)
2075     return TRUE;
2076
2077   /* We also want at least two buffers, one is being processed and one is ready
2078    * for the next iteration when we operate in live mode. */
2079   if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2080     return TRUE;
2081
2082   /* zero latency, if there is a buffer, it's full */
2083   if (self->priv->latency == 0)
2084     return FALSE;
2085
2086   /* Allow no more buffers than the latency */
2087   return (aggpad->priv->time_level <= self->priv->latency);
2088 }
2089
2090 /* Must be called with the PAD_LOCK held */
2091 static void
2092 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2093 {
2094   GstClockTime timestamp;
2095
2096   if (GST_BUFFER_DTS_IS_VALID (buffer))
2097     timestamp = GST_BUFFER_DTS (buffer);
2098   else
2099     timestamp = GST_BUFFER_PTS (buffer);
2100
2101   if (timestamp == GST_CLOCK_TIME_NONE) {
2102     if (head)
2103       timestamp = aggpad->priv->head_position;
2104     else
2105       timestamp = aggpad->priv->tail_position;
2106   }
2107
2108   /* add duration */
2109   if (GST_BUFFER_DURATION_IS_VALID (buffer))
2110     timestamp += GST_BUFFER_DURATION (buffer);
2111
2112   if (head)
2113     aggpad->priv->head_position = timestamp;
2114   else
2115     aggpad->priv->tail_position = timestamp;
2116
2117   update_time_level (aggpad, head);
2118 }
2119
2120 static GstFlowReturn
2121 gst_aggregator_pad_chain_internal (GstAggregator * self,
2122     GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2123 {
2124   GstBuffer *actual_buf = buffer;
2125   GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
2126   GstFlowReturn flow_return;
2127   GstClockTime buf_pts;
2128
2129   GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2130
2131   PAD_FLUSH_LOCK (aggpad);
2132
2133   PAD_LOCK (aggpad);
2134   flow_return = aggpad->priv->flow_return;
2135   if (flow_return != GST_FLOW_OK)
2136     goto flushing;
2137
2138   if (aggpad->priv->pending_eos == TRUE)
2139     goto eos;
2140
2141   PAD_UNLOCK (aggpad);
2142
2143   if (aggclass->clip && head) {
2144     aggclass->clip (self, aggpad, buffer, &actual_buf);
2145   }
2146
2147   if (actual_buf == NULL) {
2148     GST_LOG_OBJECT (actual_buf, "Buffer dropped by clip function");
2149     goto done;
2150   }
2151
2152   buf_pts = GST_BUFFER_PTS (actual_buf);
2153
2154   aggpad->priv->first_buffer = FALSE;
2155
2156   for (;;) {
2157     SRC_LOCK (self);
2158     GST_OBJECT_LOCK (self);
2159     PAD_LOCK (aggpad);
2160     if (gst_aggregator_pad_has_space (self, aggpad)
2161         && aggpad->priv->flow_return == GST_FLOW_OK) {
2162       if (head)
2163         g_queue_push_head (&aggpad->priv->buffers, actual_buf);
2164       else
2165         g_queue_push_tail (&aggpad->priv->buffers, actual_buf);
2166       apply_buffer (aggpad, actual_buf, head);
2167       aggpad->priv->num_buffers++;
2168       actual_buf = buffer = NULL;
2169       SRC_BROADCAST (self);
2170       break;
2171     }
2172
2173     flow_return = aggpad->priv->flow_return;
2174     if (flow_return != GST_FLOW_OK) {
2175       GST_OBJECT_UNLOCK (self);
2176       SRC_UNLOCK (self);
2177       goto flushing;
2178     }
2179     GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2180     GST_OBJECT_UNLOCK (self);
2181     SRC_UNLOCK (self);
2182     PAD_WAIT_EVENT (aggpad);
2183
2184     PAD_UNLOCK (aggpad);
2185   }
2186
2187   if (self->priv->first_buffer) {
2188     GstClockTime start_time;
2189
2190     switch (self->priv->start_time_selection) {
2191       case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2192       default:
2193         start_time = 0;
2194         break;
2195       case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2196         GST_OBJECT_LOCK (aggpad);
2197         if (aggpad->segment.format == GST_FORMAT_TIME) {
2198           start_time = buf_pts;
2199           if (start_time != -1) {
2200             start_time = MAX (start_time, aggpad->segment.start);
2201             start_time =
2202                 gst_segment_to_running_time (&aggpad->segment, GST_FORMAT_TIME,
2203                 start_time);
2204           }
2205         } else {
2206           start_time = 0;
2207           GST_WARNING_OBJECT (aggpad,
2208               "Ignoring request of selecting the first start time "
2209               "as the segment is a %s segment instead of a time segment",
2210               gst_format_get_name (aggpad->segment.format));
2211         }
2212         GST_OBJECT_UNLOCK (aggpad);
2213         break;
2214       case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2215         start_time = self->priv->start_time;
2216         if (start_time == -1)
2217           start_time = 0;
2218         break;
2219     }
2220
2221     if (start_time != -1) {
2222       if (self->segment.position == -1)
2223         self->segment.position = start_time;
2224       else
2225         self->segment.position = MIN (start_time, self->segment.position);
2226
2227       GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2228           GST_TIME_ARGS (start_time));
2229     }
2230   }
2231
2232   PAD_UNLOCK (aggpad);
2233   GST_OBJECT_UNLOCK (self);
2234   SRC_UNLOCK (self);
2235
2236 done:
2237
2238   PAD_FLUSH_UNLOCK (aggpad);
2239
2240   GST_DEBUG_OBJECT (aggpad, "Done chaining");
2241
2242   return flow_return;
2243
2244 flushing:
2245   PAD_UNLOCK (aggpad);
2246   PAD_FLUSH_UNLOCK (aggpad);
2247
2248   GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2249       gst_flow_get_name (flow_return));
2250   if (buffer)
2251     gst_buffer_unref (buffer);
2252
2253   return flow_return;
2254
2255 eos:
2256   PAD_UNLOCK (aggpad);
2257   PAD_FLUSH_UNLOCK (aggpad);
2258
2259   gst_buffer_unref (buffer);
2260   GST_DEBUG_OBJECT (aggpad, "We are EOS already...");
2261
2262   return GST_FLOW_EOS;
2263 }
2264
2265 static GstFlowReturn
2266 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2267 {
2268   return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2269       GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2270 }
2271
2272 static gboolean
2273 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2274     GstQuery * query)
2275 {
2276   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2277   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2278
2279   if (GST_QUERY_IS_SERIALIZED (query)) {
2280     PAD_LOCK (aggpad);
2281
2282     while (!gst_aggregator_pad_queue_is_empty (aggpad)
2283         && aggpad->priv->flow_return == GST_FLOW_OK) {
2284       GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2285       PAD_WAIT_EVENT (aggpad);
2286     }
2287
2288     if (aggpad->priv->flow_return != GST_FLOW_OK)
2289       goto flushing;
2290
2291     PAD_UNLOCK (aggpad);
2292   }
2293
2294   return klass->sink_query (GST_AGGREGATOR (parent),
2295       GST_AGGREGATOR_PAD (pad), query);
2296
2297 flushing:
2298   GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2299       gst_flow_get_name (aggpad->priv->flow_return));
2300   PAD_UNLOCK (aggpad);
2301   return FALSE;
2302 }
2303
2304 static gboolean
2305 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2306     GstEvent * event)
2307 {
2308   GstAggregator *self = GST_AGGREGATOR (parent);
2309   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2310   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2311
2312   if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
2313       /* && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE */ ) {
2314     SRC_LOCK (self);
2315     PAD_LOCK (aggpad);
2316
2317     if (aggpad->priv->flow_return != GST_FLOW_OK
2318         && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
2319       goto flushing;
2320
2321     if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2322       GST_OBJECT_LOCK (aggpad);
2323       gst_event_copy_segment (event, &aggpad->clip_segment);
2324       aggpad->priv->head_position = aggpad->clip_segment.position;
2325       update_time_level (aggpad, TRUE);
2326       GST_OBJECT_UNLOCK (aggpad);
2327     }
2328
2329     if (!gst_aggregator_pad_queue_is_empty (aggpad) &&
2330         GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2331       GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT,
2332           event);
2333       g_queue_push_head (&aggpad->priv->buffers, event);
2334       event = NULL;
2335       SRC_BROADCAST (self);
2336     }
2337     PAD_UNLOCK (aggpad);
2338     SRC_UNLOCK (self);
2339   }
2340
2341   if (event)
2342     return klass->sink_event (self, aggpad, event);
2343   else
2344     return TRUE;
2345
2346 flushing:
2347   GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2348       gst_flow_get_name (aggpad->priv->flow_return));
2349   PAD_UNLOCK (aggpad);
2350   SRC_UNLOCK (self);
2351   if (GST_EVENT_IS_STICKY (event))
2352     gst_pad_store_sticky_event (pad, event);
2353   gst_event_unref (event);
2354   return FALSE;
2355 }
2356
2357 static gboolean
2358 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2359     GstObject * parent, GstPadMode mode, gboolean active)
2360 {
2361   GstAggregator *self = GST_AGGREGATOR (parent);
2362   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2363
2364   if (active == FALSE) {
2365     SRC_LOCK (self);
2366     gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2367     SRC_BROADCAST (self);
2368     SRC_UNLOCK (self);
2369   } else {
2370     PAD_LOCK (aggpad);
2371     aggpad->priv->flow_return = GST_FLOW_OK;
2372     PAD_BROADCAST_EVENT (aggpad);
2373     PAD_UNLOCK (aggpad);
2374   }
2375
2376   return TRUE;
2377 }
2378
2379 /***********************************
2380  * GstAggregatorPad implementation  *
2381  ************************************/
2382 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2383
2384 static void
2385 gst_aggregator_pad_constructed (GObject * object)
2386 {
2387   GstPad *pad = GST_PAD (object);
2388
2389   gst_pad_set_chain_function (pad,
2390       GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2391   gst_pad_set_event_function (pad,
2392       GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
2393   gst_pad_set_query_function (pad,
2394       GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2395   gst_pad_set_activatemode_function (pad,
2396       GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2397 }
2398
2399 static void
2400 gst_aggregator_pad_finalize (GObject * object)
2401 {
2402   GstAggregatorPad *pad = (GstAggregatorPad *) object;
2403
2404   g_cond_clear (&pad->priv->event_cond);
2405   g_mutex_clear (&pad->priv->flush_lock);
2406   g_mutex_clear (&pad->priv->lock);
2407
2408   G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2409 }
2410
2411 static void
2412 gst_aggregator_pad_dispose (GObject * object)
2413 {
2414   GstAggregatorPad *pad = (GstAggregatorPad *) object;
2415
2416   gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2417
2418   G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2419 }
2420
2421 static void
2422 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2423 {
2424   GObjectClass *gobject_class = (GObjectClass *) klass;
2425
2426   g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2427
2428   gobject_class->constructed = gst_aggregator_pad_constructed;
2429   gobject_class->finalize = gst_aggregator_pad_finalize;
2430   gobject_class->dispose = gst_aggregator_pad_dispose;
2431 }
2432
2433 static void
2434 gst_aggregator_pad_init (GstAggregatorPad * pad)
2435 {
2436   pad->priv =
2437       G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2438       GstAggregatorPadPrivate);
2439
2440   g_queue_init (&pad->priv->buffers);
2441   g_cond_init (&pad->priv->event_cond);
2442
2443   g_mutex_init (&pad->priv->flush_lock);
2444   g_mutex_init (&pad->priv->lock);
2445
2446   pad->priv->first_buffer = TRUE;
2447 }
2448
2449 /**
2450  * gst_aggregator_pad_steal_buffer:
2451  * @pad: the pad to get buffer from
2452  *
2453  * Steal the ref to the buffer currently queued in @pad.
2454  *
2455  * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2456  *   queued. You should unref the buffer after usage.
2457  */
2458 GstBuffer *
2459 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2460 {
2461   GstBuffer *buffer = NULL;
2462
2463   PAD_LOCK (pad);
2464   if (GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->buffers)))
2465     buffer = g_queue_pop_tail (&pad->priv->buffers);
2466
2467   if (buffer) {
2468     apply_buffer (pad, buffer, FALSE);
2469     pad->priv->num_buffers--;
2470     GST_TRACE_OBJECT (pad, "Consuming buffer");
2471     if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
2472       pad->priv->pending_eos = FALSE;
2473       pad->priv->eos = TRUE;
2474     }
2475     PAD_BROADCAST_EVENT (pad);
2476     GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2477   }
2478   PAD_UNLOCK (pad);
2479
2480   return buffer;
2481 }
2482
2483 /**
2484  * gst_aggregator_pad_drop_buffer:
2485  * @pad: the pad where to drop any pending buffer
2486  *
2487  * Drop the buffer currently queued in @pad.
2488  *
2489  * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2490  */
2491 gboolean
2492 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2493 {
2494   GstBuffer *buf;
2495
2496   buf = gst_aggregator_pad_steal_buffer (pad);
2497
2498   if (buf == NULL)
2499     return FALSE;
2500
2501   gst_buffer_unref (buf);
2502   return TRUE;
2503 }
2504
2505 /**
2506  * gst_aggregator_pad_get_buffer:
2507  * @pad: the pad to get buffer from
2508  *
2509  * Returns: (transfer full): A reference to the buffer in @pad or
2510  * NULL if no buffer was queued. You should unref the buffer after
2511  * usage.
2512  */
2513 GstBuffer *
2514 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2515 {
2516   GstBuffer *buffer = NULL;
2517
2518   PAD_LOCK (pad);
2519   buffer = g_queue_peek_tail (&pad->priv->buffers);
2520   /* The tail should always be a buffer, because if it is an event,
2521    * it will be consumed immeditaly in gst_aggregator_steal_buffer */
2522
2523   if (GST_IS_BUFFER (buffer))
2524     gst_buffer_ref (buffer);
2525   else
2526     buffer = NULL;
2527   PAD_UNLOCK (pad);
2528
2529   return buffer;
2530 }
2531
2532 gboolean
2533 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2534 {
2535   gboolean is_eos;
2536
2537   PAD_LOCK (pad);
2538   is_eos = pad->priv->eos;
2539   PAD_UNLOCK (pad);
2540
2541   return is_eos;
2542 }
2543
2544 /**
2545  * gst_aggregator_merge_tags:
2546  * @self: a #GstAggregator
2547  * @tags: a #GstTagList to merge
2548  * @mode: the #GstTagMergeMode to use
2549  *
2550  * Adds tags to so-called pending tags, which will be processed
2551  * before pushing out data downstream.
2552  *
2553  * Note that this is provided for convenience, and the subclass is
2554  * not required to use this and can still do tag handling on its own.
2555  *
2556  * MT safe.
2557  */
2558 void
2559 gst_aggregator_merge_tags (GstAggregator * self,
2560     const GstTagList * tags, GstTagMergeMode mode)
2561 {
2562   GstTagList *otags;
2563
2564   g_return_if_fail (GST_IS_AGGREGATOR (self));
2565   g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2566
2567   /* FIXME Check if we can use OBJECT lock here! */
2568   GST_OBJECT_LOCK (self);
2569   if (tags)
2570     GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2571   otags = self->priv->tags;
2572   self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2573   if (otags)
2574     gst_tag_list_unref (otags);
2575   self->priv->tags_changed = TRUE;
2576   GST_OBJECT_UNLOCK (self);
2577 }
2578
2579 /**
2580  * gst_aggregator_set_latency:
2581  * @self: a #GstAggregator
2582  * @min_latency: minimum latency
2583  * @max_latency: maximum latency
2584  *
2585  * Lets #GstAggregator sub-classes tell the baseclass what their internal
2586  * latency is. Will also post a LATENCY message on the bus so the pipeline
2587  * can reconfigure its global latency.
2588  */
2589 void
2590 gst_aggregator_set_latency (GstAggregator * self,
2591     GstClockTime min_latency, GstClockTime max_latency)
2592 {
2593   gboolean changed = FALSE;
2594
2595   g_return_if_fail (GST_IS_AGGREGATOR (self));
2596   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2597   g_return_if_fail (max_latency >= min_latency);
2598
2599   SRC_LOCK (self);
2600   if (self->priv->sub_latency_min != min_latency) {
2601     self->priv->sub_latency_min = min_latency;
2602     changed = TRUE;
2603   }
2604   if (self->priv->sub_latency_max != max_latency) {
2605     self->priv->sub_latency_max = max_latency;
2606     changed = TRUE;
2607   }
2608
2609   if (changed)
2610     SRC_BROADCAST (self);
2611   SRC_UNLOCK (self);
2612
2613   if (changed) {
2614     gst_element_post_message (GST_ELEMENT_CAST (self),
2615         gst_message_new_latency (GST_OBJECT_CAST (self)));
2616   }
2617 }