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