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