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