aggregator: Set to running in a single place
[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 padcount;
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   if (!GST_CLOCK_TIME_IS_VALID (latency) ||
653       !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
654       !GST_CLOCK_TIME_IS_VALID (start) ||
655       (self->priv->first_buffer
656           && self->priv->start_time_selection ==
657           GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
658     /* We wake up here when something happened, and below
659      * then check if we're ready now. If we return FALSE,
660      * we will be directly called again.
661      */
662     SRC_WAIT (self);
663   } else {
664     GstClockTime base_time, time;
665     GstClock *clock;
666     GstClockReturn status;
667     GstClockTimeDiff jitter;
668
669     GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
670         GST_TIME_ARGS (start));
671
672     GST_OBJECT_LOCK (self);
673     base_time = GST_ELEMENT_CAST (self)->base_time;
674     clock = GST_ELEMENT_CLOCK (self);
675     if (clock)
676       gst_object_ref (clock);
677     GST_OBJECT_UNLOCK (self);
678
679     time = base_time + start;
680     time += latency;
681
682     GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
683         GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
684         " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
685         GST_TIME_ARGS (time),
686         GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
687         GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
688         GST_TIME_ARGS (gst_clock_get_time (clock)));
689
690     self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
691     gst_object_unref (clock);
692     SRC_UNLOCK (self);
693
694     jitter = 0;
695     status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
696
697     SRC_LOCK (self);
698     if (self->priv->aggregate_id) {
699       gst_clock_id_unref (self->priv->aggregate_id);
700       self->priv->aggregate_id = NULL;
701     }
702
703     GST_DEBUG_OBJECT (self,
704         "clock returned %d (jitter: %s%" GST_TIME_FORMAT ")",
705         status, (jitter < 0 ? "-" : " "),
706         GST_TIME_ARGS ((jitter < 0 ? -jitter : jitter)));
707
708     /* we timed out */
709     if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
710       SRC_UNLOCK (self);
711       *timeout = TRUE;
712       return TRUE;
713     }
714   }
715
716   res = gst_aggregator_check_pads_ready (self);
717   SRC_UNLOCK (self);
718
719   return res;
720 }
721
722 static gboolean
723 check_events (GstAggregator * self, GstAggregatorPad * pad, gpointer user_data)
724 {
725   GstEvent *event = NULL;
726   GstAggregatorClass *klass = NULL;
727   gboolean *processed_event = user_data;
728
729   do {
730     event = NULL;
731
732     PAD_LOCK (pad);
733     if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
734       pad->priv->pending_eos = FALSE;
735       pad->priv->eos = TRUE;
736     }
737     if (GST_IS_EVENT (g_queue_peek_tail (&pad->priv->buffers))) {
738       event = g_queue_pop_tail (&pad->priv->buffers);
739       PAD_BROADCAST_EVENT (pad);
740     }
741     PAD_UNLOCK (pad);
742     if (event) {
743       if (processed_event)
744         *processed_event = TRUE;
745       if (klass == NULL)
746         klass = GST_AGGREGATOR_GET_CLASS (self);
747
748       GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
749       klass->sink_event (self, pad, event);
750     }
751   } while (event != NULL);
752
753   return TRUE;
754 }
755
756 static void
757 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
758     GstFlowReturn flow_return, gboolean full)
759 {
760   GList *item;
761
762   PAD_LOCK (aggpad);
763   if (flow_return == GST_FLOW_NOT_LINKED)
764     aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
765   else
766     aggpad->priv->flow_return = flow_return;
767
768   item = g_queue_peek_head_link (&aggpad->priv->buffers);
769   while (item) {
770     GList *next = item->next;
771
772     /* In partial flush, we do like the pad, we get rid of non-sticky events
773      * and EOS/SEGMENT.
774      */
775     if (full || GST_IS_BUFFER (item->data) ||
776         GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
777         GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
778         !GST_EVENT_IS_STICKY (item->data)) {
779       gst_mini_object_unref (item->data);
780       g_queue_delete_link (&aggpad->priv->buffers, item);
781     }
782     item = next;
783   }
784   aggpad->priv->num_buffers = 0;
785
786   PAD_BROADCAST_EVENT (aggpad);
787   PAD_UNLOCK (aggpad);
788 }
789
790 static void
791 gst_aggregator_aggregate_func (GstAggregator * self)
792 {
793   GstAggregatorPrivate *priv = self->priv;
794   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
795   gboolean timeout = FALSE;
796
797   if (self->priv->running == FALSE) {
798     GST_DEBUG_OBJECT (self, "Not running anymore");
799     return;
800   }
801
802   GST_LOG_OBJECT (self, "Checking aggregate");
803   while (priv->send_eos && priv->running) {
804     GstFlowReturn flow_return;
805     gboolean processed_event = FALSE;
806
807     gst_aggregator_iterate_sinkpads (self, check_events, NULL);
808
809     if (!gst_aggregator_wait_and_check (self, &timeout))
810       continue;
811
812     gst_aggregator_iterate_sinkpads (self, check_events, &processed_event);
813     if (processed_event)
814       continue;
815
816     GST_TRACE_OBJECT (self, "Actually aggregating!");
817     flow_return = klass->aggregate (self, timeout);
818
819     GST_OBJECT_LOCK (self);
820     if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
821       /* We don't want to set the pads to flushing, but we want to
822        * stop the thread, so just break here */
823       GST_OBJECT_UNLOCK (self);
824       break;
825     }
826     GST_OBJECT_UNLOCK (self);
827
828     if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
829       gst_aggregator_push_eos (self);
830     }
831
832     GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
833
834     if (flow_return != GST_FLOW_OK) {
835       GList *item;
836
837       GST_OBJECT_LOCK (self);
838       for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
839         GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
840
841         gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
842       }
843       GST_OBJECT_UNLOCK (self);
844       break;
845     }
846   }
847
848   /* Pause the task here, the only ways to get here are:
849    * 1) We're stopping, in which case the task is stopped anyway
850    * 2) We got a flow error above, in which case it might take
851    *    some time to forward the flow return upstream and we
852    *    would otherwise call the task function over and over
853    *    again without doing anything
854    */
855   gst_pad_pause_task (self->srcpad);
856 }
857
858 static gboolean
859 gst_aggregator_start (GstAggregator * self)
860 {
861   GstAggregatorClass *klass;
862   gboolean result;
863
864   self->priv->send_stream_start = TRUE;
865   self->priv->send_segment = TRUE;
866   self->priv->send_eos = TRUE;
867   self->priv->srccaps = NULL;
868
869   klass = GST_AGGREGATOR_GET_CLASS (self);
870
871   if (klass->start)
872     result = klass->start (self);
873   else
874     result = TRUE;
875
876   return result;
877 }
878
879 static gboolean
880 _check_pending_flush_stop (GstAggregatorPad * pad)
881 {
882   gboolean res;
883
884   PAD_LOCK (pad);
885   res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
886   PAD_UNLOCK (pad);
887
888   return res;
889 }
890
891 static gboolean
892 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
893 {
894   gboolean res = TRUE;
895
896   GST_INFO_OBJECT (self, "%s srcpad task",
897       flush_start ? "Pausing" : "Stopping");
898
899   SRC_LOCK (self);
900   self->priv->running = FALSE;
901   SRC_BROADCAST (self);
902   SRC_UNLOCK (self);
903
904   if (flush_start) {
905     res = gst_pad_push_event (self->srcpad, flush_start);
906   }
907
908   gst_pad_stop_task (self->srcpad);
909
910   return res;
911 }
912
913 static void
914 gst_aggregator_start_srcpad_task (GstAggregator * self)
915 {
916   GST_INFO_OBJECT (self, "Starting srcpad task");
917
918   self->priv->running = TRUE;
919   gst_pad_start_task (GST_PAD (self->srcpad),
920       (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
921 }
922
923 static GstFlowReturn
924 gst_aggregator_flush (GstAggregator * self)
925 {
926   GstFlowReturn ret = GST_FLOW_OK;
927   GstAggregatorPrivate *priv = self->priv;
928   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
929
930   GST_DEBUG_OBJECT (self, "Flushing everything");
931   GST_OBJECT_LOCK (self);
932   priv->send_segment = TRUE;
933   priv->flush_seeking = FALSE;
934   priv->tags_changed = FALSE;
935   GST_OBJECT_UNLOCK (self);
936   if (klass->flush)
937     ret = klass->flush (self);
938
939   return ret;
940 }
941
942
943 /* Called with GstAggregator's object lock held */
944
945 static gboolean
946 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
947 {
948   GList *tmp;
949   GstAggregatorPad *tmppad;
950
951   for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
952     tmppad = (GstAggregatorPad *) tmp->data;
953
954     if (_check_pending_flush_stop (tmppad) == FALSE) {
955       GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
956           tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
957       return FALSE;
958     }
959   }
960
961   return TRUE;
962 }
963
964 static void
965 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
966     GstEvent * event)
967 {
968   GstAggregatorPrivate *priv = self->priv;
969   GstAggregatorPadPrivate *padpriv = aggpad->priv;
970
971   gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
972
973   PAD_FLUSH_LOCK (aggpad);
974   PAD_LOCK (aggpad);
975   if (padpriv->pending_flush_start) {
976     GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
977
978     padpriv->pending_flush_start = FALSE;
979     padpriv->pending_flush_stop = TRUE;
980   }
981   PAD_UNLOCK (aggpad);
982
983   GST_OBJECT_LOCK (self);
984   if (priv->flush_seeking) {
985     /* If flush_seeking we forward the first FLUSH_START */
986     if (priv->pending_flush_start) {
987       priv->pending_flush_start = FALSE;
988       GST_OBJECT_UNLOCK (self);
989
990       GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
991       gst_aggregator_stop_srcpad_task (self, event);
992
993       GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
994       GST_PAD_STREAM_LOCK (self->srcpad);
995       GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
996       event = NULL;
997     } else {
998       GST_OBJECT_UNLOCK (self);
999       gst_event_unref (event);
1000     }
1001   } else {
1002     GST_OBJECT_UNLOCK (self);
1003     gst_event_unref (event);
1004   }
1005   PAD_FLUSH_UNLOCK (aggpad);
1006 }
1007
1008 /* Must be called with the the PAD_LOCK held */
1009 static void
1010 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1011 {
1012   if (head) {
1013     if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->head_position) &&
1014         aggpad->clip_segment.format == GST_FORMAT_TIME)
1015       aggpad->priv->head_time =
1016           gst_segment_to_running_time (&aggpad->clip_segment,
1017           GST_FORMAT_TIME, aggpad->priv->head_position);
1018     else
1019       aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
1020   } else {
1021     if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_position) &&
1022         aggpad->segment.format == GST_FORMAT_TIME)
1023       aggpad->priv->tail_time =
1024           gst_segment_to_running_time (&aggpad->segment,
1025           GST_FORMAT_TIME, aggpad->priv->tail_position);
1026     else
1027       aggpad->priv->tail_time = aggpad->priv->head_time;
1028   }
1029
1030   if (aggpad->priv->head_time == GST_CLOCK_TIME_NONE ||
1031       aggpad->priv->tail_time == GST_CLOCK_TIME_NONE) {
1032     aggpad->priv->time_level = 0;
1033     return;
1034   }
1035
1036   if (aggpad->priv->tail_time > aggpad->priv->head_time)
1037     aggpad->priv->time_level = 0;
1038   else
1039     aggpad->priv->time_level = aggpad->priv->head_time -
1040         aggpad->priv->tail_time;
1041 }
1042
1043
1044 /* GstAggregator vmethods default implementations */
1045 static gboolean
1046 gst_aggregator_default_sink_event (GstAggregator * self,
1047     GstAggregatorPad * aggpad, GstEvent * event)
1048 {
1049   gboolean res = TRUE;
1050   GstPad *pad = GST_PAD (aggpad);
1051   GstAggregatorPrivate *priv = self->priv;
1052
1053   switch (GST_EVENT_TYPE (event)) {
1054     case GST_EVENT_FLUSH_START:
1055     {
1056       gst_aggregator_flush_start (self, aggpad, event);
1057       /* We forward only in one case: right after flush_seeking */
1058       event = NULL;
1059       goto eat;
1060     }
1061     case GST_EVENT_FLUSH_STOP:
1062     {
1063       GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
1064
1065       gst_aggregator_pad_flush (aggpad, self);
1066       GST_OBJECT_LOCK (self);
1067       if (priv->flush_seeking) {
1068         g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1069         if (gst_aggregator_all_flush_stop_received_locked (self)) {
1070           GST_OBJECT_UNLOCK (self);
1071           /* That means we received FLUSH_STOP/FLUSH_STOP on
1072            * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1073           gst_aggregator_flush (self);
1074           gst_pad_push_event (self->srcpad, event);
1075           event = NULL;
1076           SRC_LOCK (self);
1077           priv->send_eos = TRUE;
1078           SRC_BROADCAST (self);
1079           SRC_UNLOCK (self);
1080
1081           GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1082           GST_PAD_STREAM_UNLOCK (self->srcpad);
1083           gst_aggregator_start_srcpad_task (self);
1084         } else {
1085           GST_OBJECT_UNLOCK (self);
1086         }
1087       } else {
1088         GST_OBJECT_UNLOCK (self);
1089       }
1090
1091       aggpad->priv->first_buffer = TRUE;
1092
1093       /* We never forward the event */
1094       goto eat;
1095     }
1096     case GST_EVENT_EOS:
1097     {
1098       GST_DEBUG_OBJECT (aggpad, "EOS");
1099
1100       /* We still have a buffer, and we don't want the subclass to have to
1101        * check for it. Mark pending_eos, eos will be set when steal_buffer is
1102        * called
1103        */
1104       SRC_LOCK (self);
1105       PAD_LOCK (aggpad);
1106       if (gst_aggregator_pad_queue_is_empty (aggpad)) {
1107         aggpad->priv->eos = TRUE;
1108       } else {
1109         aggpad->priv->pending_eos = TRUE;
1110       }
1111       PAD_UNLOCK (aggpad);
1112
1113       SRC_BROADCAST (self);
1114       SRC_UNLOCK (self);
1115       goto eat;
1116     }
1117     case GST_EVENT_SEGMENT:
1118     {
1119       PAD_LOCK (aggpad);
1120       GST_OBJECT_LOCK (aggpad);
1121       gst_event_copy_segment (event, &aggpad->segment);
1122       update_time_level (aggpad, FALSE);
1123       GST_OBJECT_UNLOCK (aggpad);
1124       PAD_UNLOCK (aggpad);
1125
1126       GST_OBJECT_LOCK (self);
1127       self->priv->seqnum = gst_event_get_seqnum (event);
1128       GST_OBJECT_UNLOCK (self);
1129       goto eat;
1130     }
1131     case GST_EVENT_STREAM_START:
1132     {
1133       goto eat;
1134     }
1135     case GST_EVENT_GAP:
1136     {
1137       GstClockTime pts, endpts;
1138       GstClockTime duration;
1139       GstBuffer *gapbuf;
1140
1141       gst_event_parse_gap (event, &pts, &duration);
1142       gapbuf = gst_buffer_new ();
1143
1144       if (GST_CLOCK_TIME_IS_VALID (duration))
1145         endpts = pts + duration;
1146       else
1147         endpts = GST_CLOCK_TIME_NONE;
1148
1149       GST_OBJECT_LOCK (aggpad);
1150       res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1151           &pts, &endpts);
1152       GST_OBJECT_UNLOCK (aggpad);
1153
1154       if (!res) {
1155         GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1156         goto eat;
1157       }
1158
1159       if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1160         duration = endpts - pts;
1161       else
1162         duration = GST_CLOCK_TIME_NONE;
1163
1164       GST_BUFFER_PTS (gapbuf) = pts;
1165       GST_BUFFER_DURATION (gapbuf) = duration;
1166       GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1167       GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1168
1169       if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1170           GST_FLOW_OK) {
1171         GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1172         res = FALSE;
1173       }
1174
1175       goto eat;
1176     }
1177     case GST_EVENT_TAG:
1178     {
1179       GstTagList *tags;
1180
1181       gst_event_parse_tag (event, &tags);
1182
1183       if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1184         gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1185         gst_event_unref (event);
1186         event = NULL;
1187         goto eat;
1188       }
1189       break;
1190     }
1191     default:
1192     {
1193       break;
1194     }
1195   }
1196
1197   GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1198   return gst_pad_event_default (pad, GST_OBJECT (self), event);
1199
1200 eat:
1201   GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1202   if (event)
1203     gst_event_unref (event);
1204
1205   return res;
1206 }
1207
1208 static inline gboolean
1209 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1210     gpointer unused_udata)
1211 {
1212   gst_aggregator_pad_flush (pad, self);
1213
1214   return TRUE;
1215 }
1216
1217 static gboolean
1218 gst_aggregator_stop (GstAggregator * agg)
1219 {
1220   GstAggregatorClass *klass;
1221   gboolean result;
1222
1223   gst_aggregator_reset_flow_values (agg);
1224
1225   gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1226
1227   klass = GST_AGGREGATOR_GET_CLASS (agg);
1228
1229   if (klass->stop)
1230     result = klass->stop (agg);
1231   else
1232     result = TRUE;
1233
1234   agg->priv->has_peer_latency = FALSE;
1235   agg->priv->peer_latency_live = FALSE;
1236   agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1237
1238   if (agg->priv->tags)
1239     gst_tag_list_unref (agg->priv->tags);
1240   agg->priv->tags = NULL;
1241
1242   return result;
1243 }
1244
1245 /* GstElement vmethods implementations */
1246 static GstStateChangeReturn
1247 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1248 {
1249   GstStateChangeReturn ret;
1250   GstAggregator *self = GST_AGGREGATOR (element);
1251
1252   switch (transition) {
1253     case GST_STATE_CHANGE_READY_TO_PAUSED:
1254       if (!gst_aggregator_start (self))
1255         goto error_start;
1256       break;
1257     default:
1258       break;
1259   }
1260
1261   if ((ret =
1262           GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1263               transition)) == GST_STATE_CHANGE_FAILURE)
1264     goto failure;
1265
1266
1267   switch (transition) {
1268     case GST_STATE_CHANGE_PAUSED_TO_READY:
1269       if (!gst_aggregator_stop (self)) {
1270         /* What to do in this case? Error out? */
1271         GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1272       }
1273       break;
1274     default:
1275       break;
1276   }
1277
1278   return ret;
1279
1280 /* ERRORS */
1281 failure:
1282   {
1283     GST_ERROR_OBJECT (element, "parent failed state change");
1284     return ret;
1285   }
1286 error_start:
1287   {
1288     GST_ERROR_OBJECT (element, "Subclass failed to start");
1289     return GST_STATE_CHANGE_FAILURE;
1290   }
1291 }
1292
1293 static void
1294 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1295 {
1296   GstAggregator *self = GST_AGGREGATOR (element);
1297   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1298
1299   GST_INFO_OBJECT (pad, "Removing pad");
1300
1301   SRC_LOCK (self);
1302   gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1303   gst_element_remove_pad (element, pad);
1304
1305   self->priv->has_peer_latency = FALSE;
1306   SRC_BROADCAST (self);
1307   SRC_UNLOCK (self);
1308 }
1309
1310 static GstAggregatorPad *
1311 gst_aggregator_default_create_new_pad (GstAggregator * self,
1312     GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1313 {
1314   GstAggregatorPad *agg_pad;
1315   GstElementClass *klass = GST_ELEMENT_GET_CLASS (self);
1316   GstAggregatorPrivate *priv = self->priv;
1317
1318   if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1319     gint serial = 0;
1320     gchar *name = NULL;
1321
1322     GST_OBJECT_LOCK (self);
1323     if (req_name == NULL || strlen (req_name) < 6
1324         || !g_str_has_prefix (req_name, "sink_")) {
1325       /* no name given when requesting the pad, use next available int */
1326       priv->padcount++;
1327     } else {
1328       /* parse serial number from requested padname */
1329       serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1330       if (serial >= priv->padcount)
1331         priv->padcount = serial;
1332     }
1333
1334     name = g_strdup_printf ("sink_%u", priv->padcount);
1335     agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1336         "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1337     g_free (name);
1338
1339     GST_OBJECT_UNLOCK (self);
1340
1341     return agg_pad;
1342   } else {
1343     return NULL;
1344   }
1345 }
1346
1347 static GstPad *
1348 gst_aggregator_request_new_pad (GstElement * element,
1349     GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1350 {
1351   GstAggregator *self;
1352   GstAggregatorPad *agg_pad;
1353   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1354   GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1355
1356   self = GST_AGGREGATOR (element);
1357
1358   agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1359   if (!agg_pad) {
1360     GST_ERROR_OBJECT (element, "Couldn't create new pad");
1361     return NULL;
1362   }
1363
1364   GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1365   self->priv->has_peer_latency = FALSE;
1366
1367   if (priv->running)
1368     gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1369
1370   /* add the pad to the element */
1371   gst_element_add_pad (element, GST_PAD (agg_pad));
1372
1373   return GST_PAD (agg_pad);
1374 }
1375
1376 /* Must be called with SRC_LOCK held */
1377
1378 static gboolean
1379 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1380 {
1381   gboolean query_ret, live;
1382   GstClockTime our_latency, min, max;
1383
1384   query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1385
1386   if (!query_ret) {
1387     GST_WARNING_OBJECT (self, "Latency query failed");
1388     return FALSE;
1389   }
1390
1391   gst_query_parse_latency (query, &live, &min, &max);
1392
1393   our_latency = self->priv->latency;
1394
1395   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1396     GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1397         ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1398     return FALSE;
1399   }
1400
1401   if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1402     GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1403         ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1404             GST_TIME_FORMAT ". Add queues or other buffering elements.",
1405             GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1406     return FALSE;
1407   }
1408
1409   self->priv->peer_latency_live = live;
1410   self->priv->peer_latency_min = min;
1411   self->priv->peer_latency_max = max;
1412   self->priv->has_peer_latency = TRUE;
1413
1414   /* add our own */
1415   min += our_latency;
1416   min += self->priv->sub_latency_min;
1417   if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1418       && GST_CLOCK_TIME_IS_VALID (max))
1419     max += self->priv->sub_latency_max + our_latency;
1420   else
1421     max = GST_CLOCK_TIME_NONE;
1422
1423   SRC_BROADCAST (self);
1424
1425   GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1426       " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1427
1428   gst_query_set_latency (query, live, min, max);
1429
1430   return query_ret;
1431 }
1432
1433 /*
1434  * MUST be called with the src_lock held.
1435  *
1436  * See  gst_aggregator_get_latency() for doc
1437  */
1438 static GstClockTime
1439 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1440 {
1441   GstClockTime latency;
1442
1443   g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1444
1445   if (!self->priv->has_peer_latency) {
1446     GstQuery *query = gst_query_new_latency ();
1447     gboolean ret;
1448
1449     ret = gst_aggregator_query_latency_unlocked (self, query);
1450     gst_query_unref (query);
1451     if (!ret)
1452       return GST_CLOCK_TIME_NONE;
1453   }
1454
1455   if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1456     return GST_CLOCK_TIME_NONE;
1457
1458   /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1459   latency = self->priv->peer_latency_min;
1460
1461   /* add our own */
1462   latency += self->priv->latency;
1463   latency += self->priv->sub_latency_min;
1464
1465   return latency;
1466 }
1467
1468 /**
1469  * gst_aggregator_get_latency:
1470  * @self: a #GstAggregator
1471  *
1472  * Retrieves the latency values reported by @self in response to the latency
1473  * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1474  * will not wait for the clock.
1475  *
1476  * Typically only called by subclasses.
1477  *
1478  * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1479  */
1480 GstClockTime
1481 gst_aggregator_get_latency (GstAggregator * self)
1482 {
1483   GstClockTime ret;
1484
1485   SRC_LOCK (self);
1486   ret = gst_aggregator_get_latency_unlocked (self);
1487   SRC_UNLOCK (self);
1488
1489   return ret;
1490 }
1491
1492 static gboolean
1493 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1494 {
1495   GstAggregator *self = GST_AGGREGATOR (element);
1496
1497   GST_STATE_LOCK (element);
1498   if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1499       GST_STATE (element) < GST_STATE_PAUSED) {
1500     gdouble rate;
1501     GstFormat fmt;
1502     GstSeekFlags flags;
1503     GstSeekType start_type, stop_type;
1504     gint64 start, stop;
1505
1506     gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1507         &start, &stop_type, &stop);
1508
1509     GST_OBJECT_LOCK (self);
1510     gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1511         stop_type, stop, NULL);
1512     self->priv->seqnum = gst_event_get_seqnum (event);
1513     self->priv->first_buffer = FALSE;
1514     GST_OBJECT_UNLOCK (self);
1515
1516     GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1517   }
1518   GST_STATE_UNLOCK (element);
1519
1520
1521   return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1522       event);
1523 }
1524
1525 static gboolean
1526 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1527 {
1528   gboolean res = TRUE;
1529
1530   switch (GST_QUERY_TYPE (query)) {
1531     case GST_QUERY_SEEKING:
1532     {
1533       GstFormat format;
1534
1535       /* don't pass it along as some (file)sink might claim it does
1536        * whereas with a collectpads in between that will not likely work */
1537       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1538       gst_query_set_seeking (query, format, FALSE, 0, -1);
1539       res = TRUE;
1540
1541       break;
1542     }
1543     case GST_QUERY_LATENCY:
1544       SRC_LOCK (self);
1545       res = gst_aggregator_query_latency_unlocked (self, query);
1546       SRC_UNLOCK (self);
1547       break;
1548     default:
1549       return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1550   }
1551
1552   return res;
1553 }
1554
1555 static gboolean
1556 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1557 {
1558   EventData *evdata = user_data;
1559   gboolean ret = TRUE;
1560   GstPad *peer = gst_pad_get_peer (pad);
1561   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1562
1563   if (peer) {
1564     if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1565       GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1566       ret = TRUE;
1567     } else {
1568       ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1569       GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1570       gst_object_unref (peer);
1571     }
1572   }
1573
1574   if (ret == FALSE) {
1575     if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1576       GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1577
1578       GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1579
1580       if (gst_pad_query (peer, seeking)) {
1581         gboolean seekable;
1582
1583         gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1584
1585         if (seekable == FALSE) {
1586           GST_INFO_OBJECT (pad,
1587               "Source not seekable, We failed but it does not matter!");
1588
1589           ret = TRUE;
1590         }
1591       } else {
1592         GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1593       }
1594
1595       gst_query_unref (seeking);
1596     }
1597
1598     if (evdata->flush) {
1599       PAD_LOCK (aggpad);
1600       aggpad->priv->pending_flush_start = FALSE;
1601       aggpad->priv->pending_flush_stop = FALSE;
1602       PAD_UNLOCK (aggpad);
1603     }
1604   } else {
1605     evdata->one_actually_seeked = TRUE;
1606   }
1607
1608   evdata->result &= ret;
1609
1610   /* Always send to all pads */
1611   return FALSE;
1612 }
1613
1614 static EventData
1615 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1616     GstEvent * event, gboolean flush, gboolean only_to_active_pads)
1617 {
1618   EventData evdata;
1619
1620   evdata.event = event;
1621   evdata.result = TRUE;
1622   evdata.flush = flush;
1623   evdata.one_actually_seeked = FALSE;
1624   evdata.only_to_active_pads = only_to_active_pads;
1625
1626   /* We first need to set all pads as flushing in a first pass
1627    * as flush_start flush_stop is sometimes sent synchronously
1628    * while we send the seek event */
1629   if (flush) {
1630     GList *l;
1631
1632     GST_OBJECT_LOCK (self);
1633     for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1634       GstAggregatorPad *pad = l->data;
1635
1636       PAD_LOCK (pad);
1637       pad->priv->pending_flush_start = TRUE;
1638       pad->priv->pending_flush_stop = FALSE;
1639       PAD_UNLOCK (pad);
1640     }
1641     GST_OBJECT_UNLOCK (self);
1642   }
1643
1644   gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1645
1646   gst_event_unref (event);
1647
1648   return evdata;
1649 }
1650
1651 static gboolean
1652 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1653 {
1654   gdouble rate;
1655   GstFormat fmt;
1656   GstSeekFlags flags;
1657   GstSeekType start_type, stop_type;
1658   gint64 start, stop;
1659   gboolean flush;
1660   EventData evdata;
1661   GstAggregatorPrivate *priv = self->priv;
1662
1663   gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1664       &start, &stop_type, &stop);
1665
1666   GST_INFO_OBJECT (self, "starting SEEK");
1667
1668   flush = flags & GST_SEEK_FLAG_FLUSH;
1669
1670   GST_OBJECT_LOCK (self);
1671   if (flush) {
1672     priv->pending_flush_start = TRUE;
1673     priv->flush_seeking = TRUE;
1674   }
1675
1676   gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1677       stop_type, stop, NULL);
1678
1679   /* Seeking sets a position */
1680   self->priv->first_buffer = FALSE;
1681   GST_OBJECT_UNLOCK (self);
1682
1683   /* forward the seek upstream */
1684   evdata =
1685       gst_aggregator_forward_event_to_all_sinkpads (self, event, flush, FALSE);
1686   event = NULL;
1687
1688   if (!evdata.result || !evdata.one_actually_seeked) {
1689     GST_OBJECT_LOCK (self);
1690     priv->flush_seeking = FALSE;
1691     priv->pending_flush_start = FALSE;
1692     GST_OBJECT_UNLOCK (self);
1693   }
1694
1695   GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1696
1697   return evdata.result;
1698 }
1699
1700 static gboolean
1701 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1702 {
1703   EventData evdata;
1704   gboolean res = TRUE;
1705
1706   switch (GST_EVENT_TYPE (event)) {
1707     case GST_EVENT_SEEK:
1708     {
1709       gst_event_ref (event);
1710       res = gst_aggregator_do_seek (self, event);
1711       gst_event_unref (event);
1712       event = NULL;
1713       goto done;
1714     }
1715     case GST_EVENT_NAVIGATION:
1716     {
1717       /* navigation is rather pointless. */
1718       res = FALSE;
1719       gst_event_unref (event);
1720       goto done;
1721     }
1722     default:
1723     {
1724       break;
1725     }
1726   }
1727
1728   /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
1729    * they will receive a QOS event that has earliest_time=0 (because we can't
1730    * have negative timestamps), and consider their buffer as too late */
1731   evdata =
1732       gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE,
1733       GST_EVENT_TYPE (event) == GST_EVENT_QOS);
1734   res = evdata.result;
1735
1736 done:
1737   return res;
1738 }
1739
1740 static gboolean
1741 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1742     GstEvent * event)
1743 {
1744   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1745
1746   return klass->src_event (GST_AGGREGATOR (parent), event);
1747 }
1748
1749 static gboolean
1750 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1751     GstQuery * query)
1752 {
1753   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1754
1755   return klass->src_query (GST_AGGREGATOR (parent), query);
1756 }
1757
1758 static gboolean
1759 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1760     GstObject * parent, GstPadMode mode, gboolean active)
1761 {
1762   GstAggregator *self = GST_AGGREGATOR (parent);
1763   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1764
1765   if (klass->src_activate) {
1766     if (klass->src_activate (self, mode, active) == FALSE) {
1767       return FALSE;
1768     }
1769   }
1770
1771   if (active == TRUE) {
1772     switch (mode) {
1773       case GST_PAD_MODE_PUSH:
1774       {
1775         GST_INFO_OBJECT (pad, "Activating pad!");
1776         gst_aggregator_start_srcpad_task (self);
1777         return TRUE;
1778       }
1779       default:
1780       {
1781         GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1782         return FALSE;
1783       }
1784     }
1785   }
1786
1787   /* deactivating */
1788   GST_INFO_OBJECT (self, "Deactivating srcpad");
1789   gst_aggregator_stop_srcpad_task (self, FALSE);
1790
1791   return TRUE;
1792 }
1793
1794 static gboolean
1795 gst_aggregator_default_sink_query (GstAggregator * self,
1796     GstAggregatorPad * aggpad, GstQuery * query)
1797 {
1798   GstPad *pad = GST_PAD (aggpad);
1799
1800   return gst_pad_query_default (pad, GST_OBJECT (self), query);
1801 }
1802
1803 static void
1804 gst_aggregator_finalize (GObject * object)
1805 {
1806   GstAggregator *self = (GstAggregator *) object;
1807
1808   g_mutex_clear (&self->priv->src_lock);
1809   g_cond_clear (&self->priv->src_cond);
1810
1811   G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1812 }
1813
1814 /*
1815  * gst_aggregator_set_latency_property:
1816  * @agg: a #GstAggregator
1817  * @latency: the new latency value (in nanoseconds).
1818  *
1819  * Sets the new latency value to @latency. This value is used to limit the
1820  * amount of time a pad waits for data to appear before considering the pad
1821  * as unresponsive.
1822  */
1823 static void
1824 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1825 {
1826   gboolean changed;
1827
1828   g_return_if_fail (GST_IS_AGGREGATOR (self));
1829   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1830
1831   SRC_LOCK (self);
1832   changed = (self->priv->latency != latency);
1833
1834   if (changed) {
1835     GList *item;
1836
1837     GST_OBJECT_LOCK (self);
1838     /* First lock all the pads */
1839     for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1840       GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1841       PAD_LOCK (aggpad);
1842     }
1843
1844     self->priv->latency = latency;
1845
1846     SRC_BROADCAST (self);
1847
1848     /* Now wake up the pads */
1849     for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1850       GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1851       PAD_BROADCAST_EVENT (aggpad);
1852       PAD_UNLOCK (aggpad);
1853     }
1854     GST_OBJECT_UNLOCK (self);
1855   }
1856
1857   SRC_UNLOCK (self);
1858
1859   if (changed)
1860     gst_element_post_message (GST_ELEMENT_CAST (self),
1861         gst_message_new_latency (GST_OBJECT_CAST (self)));
1862 }
1863
1864 /*
1865  * gst_aggregator_get_latency_property:
1866  * @agg: a #GstAggregator
1867  *
1868  * Gets the latency value. See gst_aggregator_set_latency for
1869  * more details.
1870  *
1871  * Returns: The time in nanoseconds to wait for data to arrive on a sink pad 
1872  * before a pad is deemed unresponsive. A value of -1 means an
1873  * unlimited time.
1874  */
1875 static gint64
1876 gst_aggregator_get_latency_property (GstAggregator * agg)
1877 {
1878   gint64 res;
1879
1880   g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1881
1882   GST_OBJECT_LOCK (agg);
1883   res = agg->priv->latency;
1884   GST_OBJECT_UNLOCK (agg);
1885
1886   return res;
1887 }
1888
1889 static void
1890 gst_aggregator_set_property (GObject * object, guint prop_id,
1891     const GValue * value, GParamSpec * pspec)
1892 {
1893   GstAggregator *agg = GST_AGGREGATOR (object);
1894
1895   switch (prop_id) {
1896     case PROP_LATENCY:
1897       gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1898       break;
1899     case PROP_START_TIME_SELECTION:
1900       agg->priv->start_time_selection = g_value_get_enum (value);
1901       break;
1902     case PROP_START_TIME:
1903       agg->priv->start_time = g_value_get_uint64 (value);
1904       break;
1905     default:
1906       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1907       break;
1908   }
1909 }
1910
1911 static void
1912 gst_aggregator_get_property (GObject * object, guint prop_id,
1913     GValue * value, GParamSpec * pspec)
1914 {
1915   GstAggregator *agg = GST_AGGREGATOR (object);
1916
1917   switch (prop_id) {
1918     case PROP_LATENCY:
1919       g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1920       break;
1921     case PROP_START_TIME_SELECTION:
1922       g_value_set_enum (value, agg->priv->start_time_selection);
1923       break;
1924     case PROP_START_TIME:
1925       g_value_set_uint64 (value, agg->priv->start_time);
1926       break;
1927     default:
1928       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1929       break;
1930   }
1931 }
1932
1933 /* GObject vmethods implementations */
1934 static void
1935 gst_aggregator_class_init (GstAggregatorClass * klass)
1936 {
1937   GObjectClass *gobject_class = (GObjectClass *) klass;
1938   GstElementClass *gstelement_class = (GstElementClass *) klass;
1939
1940   aggregator_parent_class = g_type_class_peek_parent (klass);
1941   g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1942
1943   GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1944       GST_DEBUG_FG_MAGENTA, "GstAggregator");
1945
1946   klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1947
1948   klass->sink_event = gst_aggregator_default_sink_event;
1949   klass->sink_query = gst_aggregator_default_sink_query;
1950
1951   klass->src_event = gst_aggregator_default_src_event;
1952   klass->src_query = gst_aggregator_default_src_query;
1953
1954   klass->create_new_pad = gst_aggregator_default_create_new_pad;
1955
1956   gstelement_class->request_new_pad =
1957       GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1958   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1959   gstelement_class->release_pad =
1960       GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1961   gstelement_class->change_state =
1962       GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1963
1964   gobject_class->set_property = gst_aggregator_set_property;
1965   gobject_class->get_property = gst_aggregator_get_property;
1966   gobject_class->finalize = gst_aggregator_finalize;
1967
1968   g_object_class_install_property (gobject_class, PROP_LATENCY,
1969       g_param_spec_int64 ("latency", "Buffer latency",
1970           "Additional latency in live mode to allow upstream "
1971           "to take longer to produce buffers for the current "
1972           "position (in nanoseconds)", 0,
1973           (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1974           DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1975
1976   g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
1977       g_param_spec_enum ("start-time-selection", "Start Time Selection",
1978           "Decides which start time is output",
1979           gst_aggregator_start_time_selection_get_type (),
1980           DEFAULT_START_TIME_SELECTION,
1981           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1982
1983   g_object_class_install_property (gobject_class, PROP_START_TIME,
1984       g_param_spec_uint64 ("start-time", "Start Time",
1985           "Start time to use if start-time-selection=set", 0,
1986           G_MAXUINT64,
1987           DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1988
1989   GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1990 }
1991
1992 static void
1993 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1994 {
1995   GstPadTemplate *pad_template;
1996   GstAggregatorPrivate *priv;
1997
1998   g_return_if_fail (klass->aggregate != NULL);
1999
2000   self->priv =
2001       G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2002       GstAggregatorPrivate);
2003
2004   priv = self->priv;
2005
2006   pad_template =
2007       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2008   g_return_if_fail (pad_template != NULL);
2009
2010   priv->padcount = -1;
2011   priv->tags_changed = FALSE;
2012
2013   self->priv->peer_latency_live = FALSE;
2014   self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2015   self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2016   self->priv->has_peer_latency = FALSE;
2017   gst_aggregator_reset_flow_values (self);
2018
2019   self->srcpad = gst_pad_new_from_template (pad_template, "src");
2020
2021   gst_pad_set_event_function (self->srcpad,
2022       GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2023   gst_pad_set_query_function (self->srcpad,
2024       GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2025   gst_pad_set_activatemode_function (self->srcpad,
2026       GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2027
2028   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2029
2030   self->priv->latency = DEFAULT_LATENCY;
2031   self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2032   self->priv->start_time = DEFAULT_START_TIME;
2033
2034   g_mutex_init (&self->priv->src_lock);
2035   g_cond_init (&self->priv->src_cond);
2036 }
2037
2038 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2039  * method to get to the padtemplates */
2040 GType
2041 gst_aggregator_get_type (void)
2042 {
2043   static volatile gsize type = 0;
2044
2045   if (g_once_init_enter (&type)) {
2046     GType _type;
2047     static const GTypeInfo info = {
2048       sizeof (GstAggregatorClass),
2049       NULL,
2050       NULL,
2051       (GClassInitFunc) gst_aggregator_class_init,
2052       NULL,
2053       NULL,
2054       sizeof (GstAggregator),
2055       0,
2056       (GInstanceInitFunc) gst_aggregator_init,
2057     };
2058
2059     _type = g_type_register_static (GST_TYPE_ELEMENT,
2060         "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2061     g_once_init_leave (&type, _type);
2062   }
2063   return type;
2064 }
2065
2066 /* Must be called with SRC lock and PAD lock held */
2067 static gboolean
2068 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2069 {
2070   /* Empty queue always has space */
2071   if (g_queue_get_length (&aggpad->priv->buffers) == 0)
2072     return TRUE;
2073
2074   /* We also want at least two buffers, one is being processed and one is ready
2075    * for the next iteration when we operate in live mode. */
2076   if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2077     return TRUE;
2078
2079   /* zero latency, if there is a buffer, it's full */
2080   if (self->priv->latency == 0)
2081     return FALSE;
2082
2083   /* Allow no more buffers than the latency */
2084   return (aggpad->priv->time_level <= self->priv->latency);
2085 }
2086
2087 /* Must be called with the PAD_LOCK held */
2088 static void
2089 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2090 {
2091   GstClockTime timestamp;
2092
2093   if (GST_BUFFER_DTS_IS_VALID (buffer))
2094     timestamp = GST_BUFFER_DTS (buffer);
2095   else
2096     timestamp = GST_BUFFER_PTS (buffer);
2097
2098   if (timestamp == GST_CLOCK_TIME_NONE) {
2099     if (head)
2100       timestamp = aggpad->priv->head_position;
2101     else
2102       timestamp = aggpad->priv->tail_position;
2103   }
2104
2105   /* add duration */
2106   if (GST_BUFFER_DURATION_IS_VALID (buffer))
2107     timestamp += GST_BUFFER_DURATION (buffer);
2108
2109   if (head)
2110     aggpad->priv->head_position = timestamp;
2111   else
2112     aggpad->priv->tail_position = timestamp;
2113
2114   update_time_level (aggpad, head);
2115 }
2116
2117 static GstFlowReturn
2118 gst_aggregator_pad_chain_internal (GstAggregator * self,
2119     GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2120 {
2121   GstBuffer *actual_buf = buffer;
2122   GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
2123   GstFlowReturn flow_return;
2124   GstClockTime buf_pts;
2125
2126   GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2127
2128   PAD_FLUSH_LOCK (aggpad);
2129
2130   PAD_LOCK (aggpad);
2131   flow_return = aggpad->priv->flow_return;
2132   if (flow_return != GST_FLOW_OK)
2133     goto flushing;
2134
2135   if (aggpad->priv->pending_eos == TRUE)
2136     goto eos;
2137
2138   flow_return = aggpad->priv->flow_return;
2139   if (flow_return != GST_FLOW_OK)
2140     goto flushing;
2141
2142   PAD_UNLOCK (aggpad);
2143
2144   if (aggclass->clip && head) {
2145     aggclass->clip (self, aggpad, buffer, &actual_buf);
2146   }
2147
2148   if (actual_buf == NULL) {
2149     GST_LOG_OBJECT (actual_buf, "Buffer dropped by clip function");
2150     goto done;
2151   }
2152
2153   buf_pts = GST_BUFFER_PTS (actual_buf);
2154
2155   aggpad->priv->first_buffer = FALSE;
2156
2157   for (;;) {
2158     SRC_LOCK (self);
2159     GST_OBJECT_LOCK (self);
2160     PAD_LOCK (aggpad);
2161     if (gst_aggregator_pad_has_space (self, aggpad)
2162         && aggpad->priv->flow_return == GST_FLOW_OK) {
2163       if (head)
2164         g_queue_push_head (&aggpad->priv->buffers, actual_buf);
2165       else
2166         g_queue_push_tail (&aggpad->priv->buffers, actual_buf);
2167       apply_buffer (aggpad, actual_buf, head);
2168       aggpad->priv->num_buffers++;
2169       actual_buf = buffer = NULL;
2170       SRC_BROADCAST (self);
2171       break;
2172     }
2173
2174     flow_return = aggpad->priv->flow_return;
2175     if (flow_return != GST_FLOW_OK) {
2176       GST_OBJECT_UNLOCK (self);
2177       SRC_UNLOCK (self);
2178       goto flushing;
2179     }
2180     GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2181     GST_OBJECT_UNLOCK (self);
2182     SRC_UNLOCK (self);
2183     PAD_WAIT_EVENT (aggpad);
2184
2185     PAD_UNLOCK (aggpad);
2186   }
2187
2188   if (self->priv->first_buffer) {
2189     GstClockTime start_time;
2190
2191     switch (self->priv->start_time_selection) {
2192       case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2193       default:
2194         start_time = 0;
2195         break;
2196       case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2197         GST_OBJECT_LOCK (aggpad);
2198         if (aggpad->segment.format == GST_FORMAT_TIME) {
2199           start_time = buf_pts;
2200           if (start_time != -1) {
2201             start_time = MAX (start_time, aggpad->segment.start);
2202             start_time =
2203                 gst_segment_to_running_time (&aggpad->segment, GST_FORMAT_TIME,
2204                 start_time);
2205           }
2206         } else {
2207           start_time = 0;
2208           GST_WARNING_OBJECT (aggpad,
2209               "Ignoring request of selecting the first start time "
2210               "as the segment is a %s segment instead of a time segment",
2211               gst_format_get_name (aggpad->segment.format));
2212         }
2213         GST_OBJECT_UNLOCK (aggpad);
2214         break;
2215       case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2216         start_time = self->priv->start_time;
2217         if (start_time == -1)
2218           start_time = 0;
2219         break;
2220     }
2221
2222     if (start_time != -1) {
2223       if (self->segment.position == -1)
2224         self->segment.position = start_time;
2225       else
2226         self->segment.position = MIN (start_time, self->segment.position);
2227
2228       GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2229           GST_TIME_ARGS (start_time));
2230     }
2231   }
2232
2233   PAD_UNLOCK (aggpad);
2234   GST_OBJECT_UNLOCK (self);
2235   SRC_UNLOCK (self);
2236
2237 done:
2238
2239   PAD_FLUSH_UNLOCK (aggpad);
2240
2241   GST_DEBUG_OBJECT (aggpad, "Done chaining");
2242
2243   return flow_return;
2244
2245 flushing:
2246   PAD_UNLOCK (aggpad);
2247   PAD_FLUSH_UNLOCK (aggpad);
2248
2249   GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2250       gst_flow_get_name (flow_return));
2251   if (buffer)
2252     gst_buffer_unref (buffer);
2253
2254   return flow_return;
2255
2256 eos:
2257   PAD_UNLOCK (aggpad);
2258   PAD_FLUSH_UNLOCK (aggpad);
2259
2260   gst_buffer_unref (buffer);
2261   GST_DEBUG_OBJECT (aggpad, "We are EOS already...");
2262
2263   return GST_FLOW_EOS;
2264 }
2265
2266 static GstFlowReturn
2267 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2268 {
2269   return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2270       GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2271 }
2272
2273 static gboolean
2274 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2275     GstQuery * query)
2276 {
2277   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2278   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2279
2280   if (GST_QUERY_IS_SERIALIZED (query)) {
2281     PAD_LOCK (aggpad);
2282
2283     while (!gst_aggregator_pad_queue_is_empty (aggpad)
2284         && aggpad->priv->flow_return == GST_FLOW_OK) {
2285       GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2286       PAD_WAIT_EVENT (aggpad);
2287     }
2288
2289     if (aggpad->priv->flow_return != GST_FLOW_OK)
2290       goto flushing;
2291
2292     PAD_UNLOCK (aggpad);
2293   }
2294
2295   return klass->sink_query (GST_AGGREGATOR (parent),
2296       GST_AGGREGATOR_PAD (pad), query);
2297
2298 flushing:
2299   GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2300       gst_flow_get_name (aggpad->priv->flow_return));
2301   PAD_UNLOCK (aggpad);
2302   return FALSE;
2303 }
2304
2305 static gboolean
2306 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2307     GstEvent * event)
2308 {
2309   GstAggregator *self = GST_AGGREGATOR (parent);
2310   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2311   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2312
2313   if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
2314       /* && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE */ ) {
2315     SRC_LOCK (self);
2316     PAD_LOCK (aggpad);
2317
2318     if (aggpad->priv->flow_return != GST_FLOW_OK
2319         && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
2320       goto flushing;
2321
2322     if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2323       GST_OBJECT_LOCK (aggpad);
2324       gst_event_copy_segment (event, &aggpad->clip_segment);
2325       aggpad->priv->head_position = aggpad->clip_segment.position;
2326       update_time_level (aggpad, TRUE);
2327       GST_OBJECT_UNLOCK (aggpad);
2328     }
2329
2330     if (!gst_aggregator_pad_queue_is_empty (aggpad) &&
2331         GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2332       GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT,
2333           event);
2334       g_queue_push_head (&aggpad->priv->buffers, event);
2335       event = NULL;
2336       SRC_BROADCAST (self);
2337     }
2338     PAD_UNLOCK (aggpad);
2339     SRC_UNLOCK (self);
2340   }
2341
2342   if (event)
2343     return klass->sink_event (self, aggpad, event);
2344   else
2345     return TRUE;
2346
2347 flushing:
2348   GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2349       gst_flow_get_name (aggpad->priv->flow_return));
2350   PAD_UNLOCK (aggpad);
2351   SRC_UNLOCK (self);
2352   if (GST_EVENT_IS_STICKY (event))
2353     gst_pad_store_sticky_event (pad, event);
2354   gst_event_unref (event);
2355   return FALSE;
2356 }
2357
2358 static gboolean
2359 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2360     GstObject * parent, GstPadMode mode, gboolean active)
2361 {
2362   GstAggregator *self = GST_AGGREGATOR (parent);
2363   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2364
2365   if (active == FALSE) {
2366     SRC_LOCK (self);
2367     gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2368     SRC_BROADCAST (self);
2369     SRC_UNLOCK (self);
2370   } else {
2371     PAD_LOCK (aggpad);
2372     aggpad->priv->flow_return = GST_FLOW_OK;
2373     PAD_BROADCAST_EVENT (aggpad);
2374     PAD_UNLOCK (aggpad);
2375   }
2376
2377   return TRUE;
2378 }
2379
2380 /***********************************
2381  * GstAggregatorPad implementation  *
2382  ************************************/
2383 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2384
2385 static void
2386 gst_aggregator_pad_constructed (GObject * object)
2387 {
2388   GstPad *pad = GST_PAD (object);
2389
2390   gst_pad_set_chain_function (pad,
2391       GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2392   gst_pad_set_event_function (pad,
2393       GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
2394   gst_pad_set_query_function (pad,
2395       GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2396   gst_pad_set_activatemode_function (pad,
2397       GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2398 }
2399
2400 static void
2401 gst_aggregator_pad_finalize (GObject * object)
2402 {
2403   GstAggregatorPad *pad = (GstAggregatorPad *) object;
2404
2405   g_cond_clear (&pad->priv->event_cond);
2406   g_mutex_clear (&pad->priv->flush_lock);
2407   g_mutex_clear (&pad->priv->lock);
2408
2409   G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2410 }
2411
2412 static void
2413 gst_aggregator_pad_dispose (GObject * object)
2414 {
2415   GstAggregatorPad *pad = (GstAggregatorPad *) object;
2416
2417   gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2418
2419   G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2420 }
2421
2422 static void
2423 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2424 {
2425   GObjectClass *gobject_class = (GObjectClass *) klass;
2426
2427   g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2428
2429   gobject_class->constructed = gst_aggregator_pad_constructed;
2430   gobject_class->finalize = gst_aggregator_pad_finalize;
2431   gobject_class->dispose = gst_aggregator_pad_dispose;
2432 }
2433
2434 static void
2435 gst_aggregator_pad_init (GstAggregatorPad * pad)
2436 {
2437   pad->priv =
2438       G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2439       GstAggregatorPadPrivate);
2440
2441   g_queue_init (&pad->priv->buffers);
2442   g_cond_init (&pad->priv->event_cond);
2443
2444   g_mutex_init (&pad->priv->flush_lock);
2445   g_mutex_init (&pad->priv->lock);
2446
2447   pad->priv->first_buffer = TRUE;
2448 }
2449
2450 /**
2451  * gst_aggregator_pad_steal_buffer:
2452  * @pad: the pad to get buffer from
2453  *
2454  * Steal the ref to the buffer currently queued in @pad.
2455  *
2456  * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2457  *   queued. You should unref the buffer after usage.
2458  */
2459 GstBuffer *
2460 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2461 {
2462   GstBuffer *buffer = NULL;
2463
2464   PAD_LOCK (pad);
2465   if (GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->buffers)))
2466     buffer = g_queue_pop_tail (&pad->priv->buffers);
2467
2468   if (buffer) {
2469     apply_buffer (pad, buffer, FALSE);
2470     pad->priv->num_buffers--;
2471     GST_TRACE_OBJECT (pad, "Consuming buffer");
2472     if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
2473       pad->priv->pending_eos = FALSE;
2474       pad->priv->eos = TRUE;
2475     }
2476     PAD_BROADCAST_EVENT (pad);
2477     GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2478   }
2479   PAD_UNLOCK (pad);
2480
2481   return buffer;
2482 }
2483
2484 /**
2485  * gst_aggregator_pad_drop_buffer:
2486  * @pad: the pad where to drop any pending buffer
2487  *
2488  * Drop the buffer currently queued in @pad.
2489  *
2490  * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2491  */
2492 gboolean
2493 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2494 {
2495   GstBuffer *buf;
2496
2497   buf = gst_aggregator_pad_steal_buffer (pad);
2498
2499   if (buf == NULL)
2500     return FALSE;
2501
2502   gst_buffer_unref (buf);
2503   return TRUE;
2504 }
2505
2506 /**
2507  * gst_aggregator_pad_get_buffer:
2508  * @pad: the pad to get buffer from
2509  *
2510  * Returns: (transfer full): A reference to the buffer in @pad or
2511  * NULL if no buffer was queued. You should unref the buffer after
2512  * usage.
2513  */
2514 GstBuffer *
2515 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2516 {
2517   GstBuffer *buffer = NULL;
2518
2519   PAD_LOCK (pad);
2520   buffer = g_queue_peek_tail (&pad->priv->buffers);
2521   /* The tail should always be a buffer, because if it is an event,
2522    * it will be consumed immeditaly in gst_aggregator_steal_buffer */
2523
2524   if (GST_IS_BUFFER (buffer))
2525     gst_buffer_ref (buffer);
2526   else
2527     buffer = NULL;
2528   PAD_UNLOCK (pad);
2529
2530   return buffer;
2531 }
2532
2533 gboolean
2534 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2535 {
2536   gboolean is_eos;
2537
2538   PAD_LOCK (pad);
2539   is_eos = pad->priv->eos;
2540   PAD_UNLOCK (pad);
2541
2542   return is_eos;
2543 }
2544
2545 /**
2546  * gst_aggregator_merge_tags:
2547  * @self: a #GstAggregator
2548  * @tags: a #GstTagList to merge
2549  * @mode: the #GstTagMergeMode to use
2550  *
2551  * Adds tags to so-called pending tags, which will be processed
2552  * before pushing out data downstream.
2553  *
2554  * Note that this is provided for convenience, and the subclass is
2555  * not required to use this and can still do tag handling on its own.
2556  *
2557  * MT safe.
2558  */
2559 void
2560 gst_aggregator_merge_tags (GstAggregator * self,
2561     const GstTagList * tags, GstTagMergeMode mode)
2562 {
2563   GstTagList *otags;
2564
2565   g_return_if_fail (GST_IS_AGGREGATOR (self));
2566   g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2567
2568   /* FIXME Check if we can use OBJECT lock here! */
2569   GST_OBJECT_LOCK (self);
2570   if (tags)
2571     GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2572   otags = self->priv->tags;
2573   self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2574   if (otags)
2575     gst_tag_list_unref (otags);
2576   self->priv->tags_changed = TRUE;
2577   GST_OBJECT_UNLOCK (self);
2578 }
2579
2580 /**
2581  * gst_aggregator_set_latency:
2582  * @self: a #GstAggregator
2583  * @min_latency: minimum latency
2584  * @max_latency: maximum latency
2585  *
2586  * Lets #GstAggregator sub-classes tell the baseclass what their internal
2587  * latency is. Will also post a LATENCY message on the bus so the pipeline
2588  * can reconfigure its global latency.
2589  */
2590 void
2591 gst_aggregator_set_latency (GstAggregator * self,
2592     GstClockTime min_latency, GstClockTime max_latency)
2593 {
2594   gboolean changed = FALSE;
2595
2596   g_return_if_fail (GST_IS_AGGREGATOR (self));
2597   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2598   g_return_if_fail (max_latency >= min_latency);
2599
2600   SRC_LOCK (self);
2601   if (self->priv->sub_latency_min != min_latency) {
2602     self->priv->sub_latency_min = min_latency;
2603     changed = TRUE;
2604   }
2605   if (self->priv->sub_latency_max != max_latency) {
2606     self->priv->sub_latency_max = max_latency;
2607     changed = TRUE;
2608   }
2609
2610   if (changed)
2611     SRC_BROADCAST (self);
2612   SRC_UNLOCK (self);
2613
2614   if (changed) {
2615     gst_element_post_message (GST_ELEMENT_CAST (self),
2616         gst_message_new_latency (GST_OBJECT_CAST (self)));
2617   }
2618 }