e9056e35259a16ff6826fb828bfb9119da42ee6d
[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
575     if (GST_CLOCK_TIME_IS_VALID (latency_min)) {
576       time += latency_min;
577     } else {
578       time += self->priv->latency;
579     }
580
581     GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
582         GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
583         " latency max %" GST_TIME_FORMAT " latency min %" GST_TIME_FORMAT
584         " current %" GST_TIME_FORMAT ")", GST_TIME_ARGS (time),
585         GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
586         GST_TIME_ARGS (start), GST_TIME_ARGS (latency_max),
587         GST_TIME_ARGS (latency_min),
588         GST_TIME_ARGS (gst_clock_get_time (clock)));
589
590     GST_OBJECT_UNLOCK (self);
591
592     self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
593     gst_object_unref (clock);
594     SRC_STREAM_UNLOCK (self);
595
596     jitter = 0;
597     status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
598
599     SRC_STREAM_LOCK (self);
600     if (self->priv->aggregate_id) {
601       gst_clock_id_unref (self->priv->aggregate_id);
602       self->priv->aggregate_id = NULL;
603     }
604
605     GST_DEBUG_OBJECT (self,
606         "clock returned %d (jitter: %s%" GST_TIME_FORMAT ")",
607         status, (jitter < 0 ? "-" : " "),
608         GST_TIME_ARGS ((jitter < 0 ? -jitter : jitter)));
609
610     /* we timed out */
611     if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
612       SRC_STREAM_UNLOCK (self);
613       *timeout = TRUE;
614       return TRUE;
615     }
616   }
617
618   res = gst_aggregator_check_pads_ready (self);
619   SRC_STREAM_UNLOCK (self);
620
621   return res;
622 }
623
624 static void
625 gst_aggregator_aggregate_func (GstAggregator * self)
626 {
627   GstAggregatorPrivate *priv = self->priv;
628   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
629   gboolean timeout = FALSE;
630
631   if (self->priv->running == FALSE) {
632     GST_DEBUG_OBJECT (self, "Not running anymore");
633     return;
634   }
635
636   GST_LOG_OBJECT (self, "Checking aggregate");
637   while (priv->send_eos && priv->running) {
638     GstFlowReturn flow_return;
639
640     if (!gst_aggregator_wait_and_check (self, &timeout))
641       continue;
642
643     GST_TRACE_OBJECT (self, "Actually aggregating!");
644
645     flow_return = klass->aggregate (self, timeout);
646
647     GST_OBJECT_LOCK (self);
648     if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking)
649       priv->flow_return = GST_FLOW_OK;
650     else
651       priv->flow_return = flow_return;
652     GST_OBJECT_UNLOCK (self);
653
654     if (flow_return == GST_FLOW_EOS) {
655       gst_aggregator_push_eos (self);
656     }
657
658     GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
659
660     if (flow_return != GST_FLOW_OK)
661       break;
662   }
663 }
664
665 static gboolean
666 gst_aggregator_start (GstAggregator * self)
667 {
668   GstAggregatorClass *klass;
669   gboolean result;
670
671   self->priv->running = TRUE;
672   self->priv->send_stream_start = TRUE;
673   self->priv->send_segment = TRUE;
674   self->priv->send_eos = TRUE;
675   self->priv->srccaps = NULL;
676   self->priv->flow_return = GST_FLOW_OK;
677
678   klass = GST_AGGREGATOR_GET_CLASS (self);
679
680   if (klass->start)
681     result = klass->start (self);
682   else
683     result = TRUE;
684
685   return result;
686 }
687
688 static gboolean
689 _check_pending_flush_stop (GstAggregatorPad * pad)
690 {
691   gboolean res;
692
693   PAD_LOCK (pad);
694   res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
695   PAD_UNLOCK (pad);
696
697   return res;
698 }
699
700 static gboolean
701 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
702 {
703   gboolean res = TRUE;
704
705   GST_INFO_OBJECT (self, "%s srcpad task",
706       flush_start ? "Pausing" : "Stopping");
707
708   SRC_STREAM_LOCK (self);
709   self->priv->running = FALSE;
710   SRC_STREAM_BROADCAST (self);
711   SRC_STREAM_UNLOCK (self);
712
713   if (flush_start) {
714     res = gst_pad_push_event (self->srcpad, flush_start);
715   }
716
717   gst_pad_stop_task (self->srcpad);
718
719   return res;
720 }
721
722 static void
723 gst_aggregator_start_srcpad_task (GstAggregator * self)
724 {
725   GST_INFO_OBJECT (self, "Starting srcpad task");
726
727   self->priv->running = TRUE;
728   gst_pad_start_task (GST_PAD (self->srcpad),
729       (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
730 }
731
732 static GstFlowReturn
733 gst_aggregator_flush (GstAggregator * self)
734 {
735   GstFlowReturn ret = GST_FLOW_OK;
736   GstAggregatorPrivate *priv = self->priv;
737   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
738
739   GST_DEBUG_OBJECT (self, "Flushing everything");
740   GST_OBJECT_LOCK (self);
741   priv->send_segment = TRUE;
742   priv->flush_seeking = FALSE;
743   priv->tags_changed = FALSE;
744   GST_OBJECT_UNLOCK (self);
745   if (klass->flush)
746     ret = klass->flush (self);
747
748   return ret;
749 }
750
751
752 /* Called with GstAggregator's object lock held */
753
754 static gboolean
755 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
756 {
757   GList *tmp;
758   GstAggregatorPad *tmppad;
759
760   for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
761     tmppad = (GstAggregatorPad *) tmp->data;
762
763     if (_check_pending_flush_stop (tmppad) == FALSE) {
764       GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
765           tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
766       return FALSE;
767     }
768   }
769
770   return TRUE;
771 }
772
773 static void
774 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
775     GstEvent * event)
776 {
777   GstBuffer *tmpbuf;
778   GstAggregatorPrivate *priv = self->priv;
779   GstAggregatorPadPrivate *padpriv = aggpad->priv;
780
781   g_atomic_int_set (&aggpad->priv->flushing, TRUE);
782   /*  Remove pad buffer and wake up the streaming thread */
783   tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
784   gst_buffer_replace (&tmpbuf, NULL);
785
786   PAD_STREAM_LOCK (aggpad);
787   PAD_LOCK (aggpad);
788   if (padpriv->pending_flush_start) {
789     GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
790
791     padpriv->pending_flush_start = FALSE;
792     padpriv->pending_flush_stop = TRUE;
793   }
794   PAD_UNLOCK (aggpad);
795
796   GST_OBJECT_LOCK (self);
797   if (priv->flush_seeking) {
798     /* If flush_seeking we forward the first FLUSH_START */
799     if (priv->pending_flush_start) {
800       priv->pending_flush_start = FALSE;
801       GST_OBJECT_UNLOCK (self);
802
803       GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
804       gst_aggregator_stop_srcpad_task (self, event);
805       priv->flow_return = GST_FLOW_OK;
806
807       GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
808       GST_PAD_STREAM_LOCK (self->srcpad);
809       GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
810       event = NULL;
811     } else {
812       GST_OBJECT_UNLOCK (self);
813       gst_event_unref (event);
814     }
815   } else {
816     GST_OBJECT_UNLOCK (self);
817     gst_event_unref (event);
818   }
819   PAD_STREAM_UNLOCK (aggpad);
820
821   tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
822   gst_buffer_replace (&tmpbuf, NULL);
823 }
824
825 /* GstAggregator vmethods default implementations */
826 static gboolean
827 gst_aggregator_default_sink_event (GstAggregator * self,
828     GstAggregatorPad * aggpad, GstEvent * event)
829 {
830   gboolean res = TRUE;
831   GstPad *pad = GST_PAD (aggpad);
832   GstAggregatorPrivate *priv = self->priv;
833
834   switch (GST_EVENT_TYPE (event)) {
835     case GST_EVENT_FLUSH_START:
836     {
837       gst_aggregator_flush_start (self, aggpad, event);
838       /* We forward only in one case: right after flush_seeking */
839       event = NULL;
840       goto eat;
841     }
842     case GST_EVENT_FLUSH_STOP:
843     {
844       GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
845
846       gst_aggregator_pad_flush (aggpad, self);
847       GST_OBJECT_LOCK (self);
848       if (priv->flush_seeking) {
849         g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
850         if (gst_aggregator_all_flush_stop_received_locked (self)) {
851           GST_OBJECT_UNLOCK (self);
852           /* That means we received FLUSH_STOP/FLUSH_STOP on
853            * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
854           gst_aggregator_flush (self);
855           gst_pad_push_event (self->srcpad, event);
856           event = NULL;
857           SRC_STREAM_LOCK (self);
858           priv->send_eos = TRUE;
859           SRC_STREAM_BROADCAST (self);
860           SRC_STREAM_UNLOCK (self);
861
862           GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
863           GST_PAD_STREAM_UNLOCK (self->srcpad);
864           gst_aggregator_start_srcpad_task (self);
865         } else {
866           GST_OBJECT_UNLOCK (self);
867         }
868       } else {
869         GST_OBJECT_UNLOCK (self);
870       }
871
872       /* We never forward the event */
873       goto eat;
874     }
875     case GST_EVENT_EOS:
876     {
877       GST_DEBUG_OBJECT (aggpad, "EOS");
878
879       /* We still have a buffer, and we don't want the subclass to have to
880        * check for it. Mark pending_eos, eos will be set when steal_buffer is
881        * called
882        */
883       SRC_STREAM_LOCK (self);
884       PAD_LOCK (aggpad);
885       if (!aggpad->priv->buffer) {
886         aggpad->priv->eos = TRUE;
887       } else {
888         aggpad->priv->pending_eos = TRUE;
889       }
890       PAD_UNLOCK (aggpad);
891
892       SRC_STREAM_BROADCAST (self);
893       SRC_STREAM_UNLOCK (self);
894       goto eat;
895     }
896     case GST_EVENT_SEGMENT:
897     {
898       GST_OBJECT_LOCK (aggpad);
899       gst_event_copy_segment (event, &aggpad->segment);
900       GST_OBJECT_UNLOCK (aggpad);
901
902       GST_OBJECT_LOCK (self);
903       self->priv->seqnum = gst_event_get_seqnum (event);
904       GST_OBJECT_UNLOCK (self);
905       goto eat;
906     }
907     case GST_EVENT_STREAM_START:
908     {
909       goto eat;
910     }
911     case GST_EVENT_TAG:
912     {
913       GstTagList *tags;
914
915       gst_event_parse_tag (event, &tags);
916
917       if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
918         gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
919         gst_event_unref (event);
920         event = NULL;
921         goto eat;
922       }
923       break;
924     }
925     default:
926     {
927       break;
928     }
929   }
930
931   GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
932   return gst_pad_event_default (pad, GST_OBJECT (self), event);
933
934 eat:
935   GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
936   if (event)
937     gst_event_unref (event);
938
939   return res;
940 }
941
942 static inline gboolean
943 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
944     gpointer unused_udata)
945 {
946   gst_aggregator_pad_flush (pad, self);
947
948   return TRUE;
949 }
950
951 static gboolean
952 gst_aggregator_stop (GstAggregator * agg)
953 {
954   GstAggregatorClass *klass;
955   gboolean result;
956
957   gst_aggregator_reset_flow_values (agg);
958
959   gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
960
961   klass = GST_AGGREGATOR_GET_CLASS (agg);
962
963   if (klass->stop)
964     result = klass->stop (agg);
965   else
966     result = TRUE;
967
968   if (agg->priv->tags)
969     gst_tag_list_unref (agg->priv->tags);
970   agg->priv->tags = NULL;
971
972   return result;
973 }
974
975 /* GstElement vmethods implementations */
976 static GstStateChangeReturn
977 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
978 {
979   GstStateChangeReturn ret;
980   GstAggregator *self = GST_AGGREGATOR (element);
981
982   switch (transition) {
983     case GST_STATE_CHANGE_READY_TO_PAUSED:
984       if (!gst_aggregator_start (self))
985         goto error_start;
986       break;
987     default:
988       break;
989   }
990
991   if ((ret =
992           GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
993               transition)) == GST_STATE_CHANGE_FAILURE)
994     goto failure;
995
996
997   switch (transition) {
998     case GST_STATE_CHANGE_PAUSED_TO_READY:
999       if (!gst_aggregator_stop (self)) {
1000         /* What to do in this case? Error out? */
1001         GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1002       }
1003       break;
1004     default:
1005       break;
1006   }
1007
1008   return ret;
1009
1010 /* ERRORS */
1011 failure:
1012   {
1013     GST_ERROR_OBJECT (element, "parent failed state change");
1014     return ret;
1015   }
1016 error_start:
1017   {
1018     GST_ERROR_OBJECT (element, "Subclass failed to start");
1019     return GST_STATE_CHANGE_FAILURE;
1020   }
1021 }
1022
1023 static void
1024 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1025 {
1026   GstAggregator *self = GST_AGGREGATOR (element);
1027   GstBuffer *tmpbuf;
1028
1029   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1030
1031   GST_INFO_OBJECT (pad, "Removing pad");
1032
1033   SRC_STREAM_LOCK (self);
1034   g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1035   tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
1036   gst_buffer_replace (&tmpbuf, NULL);
1037   gst_element_remove_pad (element, pad);
1038
1039   SRC_STREAM_BROADCAST (self);
1040   SRC_STREAM_UNLOCK (self);
1041 }
1042
1043 static GstPad *
1044 gst_aggregator_request_new_pad (GstElement * element,
1045     GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1046 {
1047   GstAggregator *self;
1048   GstAggregatorPad *agg_pad;
1049
1050   GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1051   GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1052
1053   self = GST_AGGREGATOR (element);
1054
1055   if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1056     gint serial = 0;
1057     gchar *name = NULL;
1058
1059     GST_OBJECT_LOCK (element);
1060     if (req_name == NULL || strlen (req_name) < 6
1061         || !g_str_has_prefix (req_name, "sink_")) {
1062       /* no name given when requesting the pad, use next available int */
1063       priv->padcount++;
1064     } else {
1065       /* parse serial number from requested padname */
1066       serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1067       if (serial >= priv->padcount)
1068         priv->padcount = serial;
1069     }
1070
1071     name = g_strdup_printf ("sink_%u", priv->padcount);
1072     agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1073         "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1074     g_free (name);
1075
1076     GST_OBJECT_UNLOCK (element);
1077
1078   } else {
1079     return NULL;
1080   }
1081
1082   GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1083
1084   if (priv->running)
1085     gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1086
1087   /* add the pad to the element */
1088   gst_element_add_pad (element, GST_PAD (agg_pad));
1089
1090   return GST_PAD (agg_pad);
1091 }
1092
1093 typedef struct
1094 {
1095   GstClockTime min, max;
1096   gboolean live;
1097 } LatencyData;
1098
1099 static gboolean
1100 gst_aggregator_query_sink_latency_foreach (GstAggregator * self,
1101     GstAggregatorPad * pad, gpointer user_data)
1102 {
1103   LatencyData *data = user_data;
1104   GstClockTime min, max;
1105   GstQuery *query;
1106   gboolean live, res;
1107
1108   query = gst_query_new_latency ();
1109   res = gst_pad_peer_query (GST_PAD_CAST (pad), query);
1110
1111   if (res) {
1112     gst_query_parse_latency (query, &live, &min, &max);
1113
1114     GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
1115         " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1116
1117     if (min != GST_CLOCK_TIME_NONE && min > data->min)
1118       data->min = min;
1119
1120     if (max != GST_CLOCK_TIME_NONE &&
1121         ((data->max != GST_CLOCK_TIME_NONE && max < data->max) ||
1122             (data->max == GST_CLOCK_TIME_NONE)))
1123       data->max = max;
1124
1125     data->live |= live;
1126   }
1127
1128   gst_query_unref (query);
1129
1130   return TRUE;
1131 }
1132
1133 /**
1134  * gst_aggregator_get_latency_unlocked:
1135  * @self: a #GstAggregator
1136  * @live: (out) (allow-none): whether @self is live
1137  * @min_latency: (out) (allow-none): the configured minimum latency of @self
1138  * @max_latency: (out) (allow-none): the configured maximum latency of @self
1139  *
1140  * Retreives the latency values reported by @self in response to the latency
1141  * query.
1142  *
1143  * Typically only called by subclasses.
1144  *
1145  * MUST be called with the object lock held.
1146  */
1147 void
1148 gst_aggregator_get_latency_unlocked (GstAggregator * self, gboolean * live,
1149     GstClockTime * min_latency, GstClockTime * max_latency)
1150 {
1151   GstClockTime our_latency;
1152   GstClockTime min, max;
1153
1154   g_return_if_fail (GST_IS_AGGREGATOR (self));
1155
1156   /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1157   min = self->priv->latency_min;
1158   max = self->priv->latency_max;
1159
1160   min += self->priv->sub_latency_min;
1161   if (GST_CLOCK_TIME_IS_VALID (max)
1162       && GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max))
1163     max += self->priv->sub_latency_max;
1164
1165   our_latency = self->priv->latency;
1166   if (GST_CLOCK_TIME_IS_VALID (our_latency)) {
1167     min += our_latency;
1168     if (GST_CLOCK_TIME_IS_VALID (max))
1169       max += our_latency;
1170   }
1171
1172   if (live)
1173     *live = self->priv->latency_live;
1174   if (min_latency)
1175     *min_latency = min;
1176   if (max_latency)
1177     *max_latency = max;
1178 }
1179
1180 static gboolean
1181 gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
1182 {
1183   GstClockTime our_latency;
1184   LatencyData data;
1185
1186   data.min = 0;
1187   data.max = GST_CLOCK_TIME_NONE;
1188   data.live = FALSE;
1189
1190   /* query upstream's latency */
1191   SRC_STREAM_LOCK (self);
1192   gst_aggregator_iterate_sinkpads (self,
1193       gst_aggregator_query_sink_latency_foreach, &data);
1194   SRC_STREAM_UNLOCK (self);
1195
1196   GST_OBJECT_LOCK (self);
1197   our_latency = self->priv->latency;
1198
1199   if (data.live && GST_CLOCK_TIME_IS_VALID (our_latency) &&
1200       our_latency > data.max) {
1201     GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1202         ("%s", "Latency too big"),
1203         ("The requested latency value is too big for the current pipeline.  "
1204             "Limiting to %" G_GINT64_FORMAT, data.max));
1205     self->priv->latency = data.max;
1206     /* FIXME: shouldn't we g_object_notify() the change here? */
1207   }
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   if (GST_CLOCK_TIME_IS_VALID (our_latency)) {
1227     if (GST_CLOCK_TIME_IS_VALID (data.min))
1228       data.min += our_latency;
1229     if (GST_CLOCK_TIME_IS_VALID (data.max))
1230       data.max += our_latency;
1231   }
1232
1233   if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_min)
1234       && GST_CLOCK_TIME_IS_VALID (data.min))
1235     data.min += self->priv->sub_latency_min;
1236   if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1237       && GST_CLOCK_TIME_IS_VALID (data.max))
1238     data.max += self->priv->sub_latency_max;
1239
1240   GST_OBJECT_UNLOCK (self);
1241
1242   GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1243       " max:%" G_GINT64_FORMAT, data.live ? "true" : "false", data.min,
1244       data.max);
1245
1246   gst_query_set_latency (query, data.live, data.min, data.max);
1247
1248   return TRUE;
1249 }
1250
1251 static gboolean
1252 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1253 {
1254   GstAggregator *self = GST_AGGREGATOR (element);
1255
1256   GST_STATE_LOCK (element);
1257   if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1258       GST_STATE (element) < GST_STATE_PAUSED) {
1259     gdouble rate;
1260     GstFormat fmt;
1261     GstSeekFlags flags;
1262     GstSeekType start_type, stop_type;
1263     gint64 start, stop;
1264
1265     gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1266         &start, &stop_type, &stop);
1267
1268     GST_OBJECT_LOCK (self);
1269     gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1270         stop_type, stop, NULL);
1271     self->priv->seqnum = gst_event_get_seqnum (event);
1272     GST_OBJECT_UNLOCK (self);
1273
1274     GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1275   }
1276   GST_STATE_UNLOCK (element);
1277
1278
1279   return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1280       event);
1281 }
1282
1283 static gboolean
1284 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1285 {
1286   gboolean res = TRUE;
1287
1288   switch (GST_QUERY_TYPE (query)) {
1289     case GST_QUERY_SEEKING:
1290     {
1291       GstFormat format;
1292
1293       /* don't pass it along as some (file)sink might claim it does
1294        * whereas with a collectpads in between that will not likely work */
1295       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1296       gst_query_set_seeking (query, format, FALSE, 0, -1);
1297       res = TRUE;
1298
1299       goto discard;
1300     }
1301     case GST_QUERY_LATENCY:
1302     {
1303       gboolean ret;
1304
1305       ret = gst_aggregator_query_latency (self, query);
1306       /* Wake up the src thread again, due to changed latencies
1307        * or changed live-ness we might have to adjust if we wait
1308        * on a deadline at all and how long.
1309        * This is only to unschedule the clock id, we don't really care
1310        * about the GCond here.
1311        */
1312       SRC_STREAM_LOCK (self);
1313       SRC_STREAM_BROADCAST (self);
1314       SRC_STREAM_UNLOCK (self);
1315       return ret;
1316     }
1317     default:
1318       break;
1319   }
1320
1321   return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1322
1323 discard:
1324   return res;
1325 }
1326
1327 static gboolean
1328 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1329 {
1330   EventData *evdata = user_data;
1331   gboolean ret = TRUE;
1332   GstPad *peer = gst_pad_get_peer (pad);
1333   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1334
1335   if (peer) {
1336     ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1337     GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1338     gst_object_unref (peer);
1339   }
1340
1341   if (ret == FALSE) {
1342     if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1343       GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1344
1345     if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1346       GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1347
1348       if (gst_pad_query (peer, seeking)) {
1349         gboolean seekable;
1350
1351         gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1352
1353         if (seekable == FALSE) {
1354           GST_INFO_OBJECT (pad,
1355               "Source not seekable, We failed but it does not matter!");
1356
1357           ret = TRUE;
1358         }
1359       } else {
1360         GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1361       }
1362
1363       gst_query_unref (seeking);
1364     }
1365
1366     if (evdata->flush) {
1367       PAD_LOCK (aggpad);
1368       aggpad->priv->pending_flush_start = FALSE;
1369       aggpad->priv->pending_flush_stop = FALSE;
1370       PAD_UNLOCK (aggpad);
1371     }
1372   } else {
1373     evdata->one_actually_seeked = TRUE;
1374   }
1375
1376   evdata->result &= ret;
1377
1378   /* Always send to all pads */
1379   return FALSE;
1380 }
1381
1382 static EventData
1383 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1384     GstEvent * event, gboolean flush)
1385 {
1386   EventData evdata;
1387
1388   evdata.event = event;
1389   evdata.result = TRUE;
1390   evdata.flush = flush;
1391   evdata.one_actually_seeked = FALSE;
1392
1393   /* We first need to set all pads as flushing in a first pass
1394    * as flush_start flush_stop is sometimes sent synchronously
1395    * while we send the seek event */
1396   if (flush) {
1397     GList *l;
1398
1399     GST_OBJECT_LOCK (self);
1400     for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1401       GstAggregatorPad *pad = l->data;
1402
1403       PAD_LOCK (pad);
1404       pad->priv->pending_flush_start = TRUE;
1405       pad->priv->pending_flush_stop = FALSE;
1406       PAD_UNLOCK (pad);
1407     }
1408     GST_OBJECT_UNLOCK (self);
1409   }
1410
1411   gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1412
1413   gst_event_unref (event);
1414
1415   return evdata;
1416 }
1417
1418 static gboolean
1419 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1420 {
1421   gdouble rate;
1422   GstFormat fmt;
1423   GstSeekFlags flags;
1424   GstSeekType start_type, stop_type;
1425   gint64 start, stop;
1426   gboolean flush;
1427   EventData evdata;
1428   GstAggregatorPrivate *priv = self->priv;
1429
1430   gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1431       &start, &stop_type, &stop);
1432
1433   GST_INFO_OBJECT (self, "starting SEEK");
1434
1435   flush = flags & GST_SEEK_FLAG_FLUSH;
1436
1437   GST_OBJECT_LOCK (self);
1438   if (flush) {
1439     priv->pending_flush_start = TRUE;
1440     priv->flush_seeking = TRUE;
1441   }
1442
1443   gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1444       stop_type, stop, NULL);
1445   GST_OBJECT_UNLOCK (self);
1446
1447   /* forward the seek upstream */
1448   evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, flush);
1449   event = NULL;
1450
1451   if (!evdata.result || !evdata.one_actually_seeked) {
1452     GST_OBJECT_LOCK (self);
1453     priv->flush_seeking = FALSE;
1454     priv->pending_flush_start = FALSE;
1455     GST_OBJECT_UNLOCK (self);
1456   }
1457
1458   GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1459
1460   return evdata.result;
1461 }
1462
1463 static gboolean
1464 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1465 {
1466   EventData evdata;
1467   gboolean res = TRUE;
1468
1469   switch (GST_EVENT_TYPE (event)) {
1470     case GST_EVENT_SEEK:
1471     {
1472       gst_event_ref (event);
1473       res = gst_aggregator_do_seek (self, event);
1474       gst_event_unref (event);
1475       event = NULL;
1476       goto done;
1477     }
1478     case GST_EVENT_NAVIGATION:
1479     {
1480       /* navigation is rather pointless. */
1481       res = FALSE;
1482       gst_event_unref (event);
1483       goto done;
1484     }
1485     default:
1486     {
1487       break;
1488     }
1489   }
1490
1491   evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE);
1492   res = evdata.result;
1493
1494 done:
1495   return res;
1496 }
1497
1498 static gboolean
1499 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1500     GstEvent * event)
1501 {
1502   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1503
1504   return klass->src_event (GST_AGGREGATOR (parent), event);
1505 }
1506
1507 static gboolean
1508 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1509     GstQuery * query)
1510 {
1511   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1512
1513   return klass->src_query (GST_AGGREGATOR (parent), query);
1514 }
1515
1516 static gboolean
1517 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1518     GstObject * parent, GstPadMode mode, gboolean active)
1519 {
1520   GstAggregator *self = GST_AGGREGATOR (parent);
1521   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1522
1523   if (klass->src_activate) {
1524     if (klass->src_activate (self, mode, active) == FALSE) {
1525       return FALSE;
1526     }
1527   }
1528
1529   if (active == TRUE) {
1530     switch (mode) {
1531       case GST_PAD_MODE_PUSH:
1532       {
1533         GST_INFO_OBJECT (pad, "Activating pad!");
1534         gst_aggregator_start_srcpad_task (self);
1535         return TRUE;
1536       }
1537       default:
1538       {
1539         GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1540         return FALSE;
1541       }
1542     }
1543   }
1544
1545   /* deactivating */
1546   GST_INFO_OBJECT (self, "Deactivating srcpad");
1547   gst_aggregator_stop_srcpad_task (self, FALSE);
1548
1549   return TRUE;
1550 }
1551
1552 static gboolean
1553 gst_aggregator_default_sink_query (GstAggregator * self,
1554     GstAggregatorPad * aggpad, GstQuery * query)
1555 {
1556   GstPad *pad = GST_PAD (aggpad);
1557
1558   return gst_pad_query_default (pad, GST_OBJECT (self), query);
1559 }
1560
1561 static void
1562 gst_aggregator_finalize (GObject * object)
1563 {
1564   GstAggregator *self = (GstAggregator *) object;
1565
1566   g_mutex_clear (&self->priv->src_lock);
1567   g_cond_clear (&self->priv->src_cond);
1568
1569   G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1570 }
1571
1572 /*
1573  * gst_aggregator_set_latency_property:
1574  * @agg: a #GstAggregator
1575  * @latency: the new latency value.
1576  *
1577  * Sets the new latency value to @latency. This value is used to limit the
1578  * amount of time a pad waits for data to appear before considering the pad
1579  * as unresponsive.
1580  */
1581 static void
1582 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1583 {
1584   gboolean changed;
1585
1586   g_return_if_fail (GST_IS_AGGREGATOR (self));
1587
1588   GST_OBJECT_LOCK (self);
1589
1590   if (self->priv->latency_live && self->priv->latency_max != 0 &&
1591       GST_CLOCK_TIME_IS_VALID (latency) && latency > self->priv->latency_max) {
1592     GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1593         ("%s", "Latency too big"),
1594         ("The requested latency value is too big for the latency in the "
1595             "current pipeline.  Limiting to %" G_GINT64_FORMAT,
1596             self->priv->latency_max));
1597     latency = self->priv->latency_max;
1598   }
1599
1600   changed = (self->priv->latency != latency);
1601   self->priv->latency = latency;
1602   GST_OBJECT_UNLOCK (self);
1603
1604   if (changed)
1605     gst_element_post_message (GST_ELEMENT_CAST (self),
1606         gst_message_new_latency (GST_OBJECT_CAST (self)));
1607 }
1608
1609 /*
1610  * gst_aggregator_get_latency_property:
1611  * @agg: a #GstAggregator
1612  *
1613  * Gets the latency value. See gst_aggregator_set_latency for
1614  * more details.
1615  *
1616  * Returns: The time in nanoseconds to wait for data to arrive on a sink pad 
1617  * before a pad is deemed unresponsive. A value of -1 means an
1618  * unlimited time.
1619  */
1620 static gint64
1621 gst_aggregator_get_latency_property (GstAggregator * agg)
1622 {
1623   gint64 res;
1624
1625   g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1626
1627   GST_OBJECT_LOCK (agg);
1628   res = agg->priv->latency;
1629   GST_OBJECT_UNLOCK (agg);
1630
1631   return res;
1632 }
1633
1634 static void
1635 gst_aggregator_set_property (GObject * object, guint prop_id,
1636     const GValue * value, GParamSpec * pspec)
1637 {
1638   GstAggregator *agg = GST_AGGREGATOR (object);
1639
1640   switch (prop_id) {
1641     case PROP_LATENCY:
1642       gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1643       break;
1644     default:
1645       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1646       break;
1647   }
1648 }
1649
1650 static void
1651 gst_aggregator_get_property (GObject * object, guint prop_id,
1652     GValue * value, GParamSpec * pspec)
1653 {
1654   GstAggregator *agg = GST_AGGREGATOR (object);
1655
1656   switch (prop_id) {
1657     case PROP_LATENCY:
1658       g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1659       break;
1660     default:
1661       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1662       break;
1663   }
1664 }
1665
1666 /* GObject vmethods implementations */
1667 static void
1668 gst_aggregator_class_init (GstAggregatorClass * klass)
1669 {
1670   GObjectClass *gobject_class = (GObjectClass *) klass;
1671   GstElementClass *gstelement_class = (GstElementClass *) klass;
1672
1673   aggregator_parent_class = g_type_class_peek_parent (klass);
1674   g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1675
1676   GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1677       GST_DEBUG_FG_MAGENTA, "GstAggregator");
1678
1679   klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1680
1681   klass->sink_event = gst_aggregator_default_sink_event;
1682   klass->sink_query = gst_aggregator_default_sink_query;
1683
1684   klass->src_event = gst_aggregator_default_src_event;
1685   klass->src_query = gst_aggregator_default_src_query;
1686
1687   gstelement_class->request_new_pad =
1688       GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1689   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1690   gstelement_class->release_pad =
1691       GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1692   gstelement_class->change_state =
1693       GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1694
1695   gobject_class->set_property = gst_aggregator_set_property;
1696   gobject_class->get_property = gst_aggregator_get_property;
1697   gobject_class->finalize = gst_aggregator_finalize;
1698
1699   g_object_class_install_property (gobject_class, PROP_LATENCY,
1700       g_param_spec_int64 ("latency", "Buffer latency",
1701           "Additional latency in live mode to allow upstream "
1702           "to take longer to produce buffers for the current "
1703           "position", 0,
1704           (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1705           DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1706
1707   GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1708   GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_query_sink_latency_foreach);
1709 }
1710
1711 static void
1712 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1713 {
1714   GstPadTemplate *pad_template;
1715   GstAggregatorPrivate *priv;
1716
1717   g_return_if_fail (klass->aggregate != NULL);
1718
1719   self->priv =
1720       G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1721       GstAggregatorPrivate);
1722
1723   priv = self->priv;
1724
1725   pad_template =
1726       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1727   g_return_if_fail (pad_template != NULL);
1728
1729   priv->padcount = -1;
1730   priv->tags_changed = FALSE;
1731
1732   self->priv->latency_live = FALSE;
1733   self->priv->latency_min = self->priv->sub_latency_min = 0;
1734   self->priv->latency_max = self->priv->sub_latency_max = GST_CLOCK_TIME_NONE;
1735   gst_aggregator_reset_flow_values (self);
1736
1737   self->srcpad = gst_pad_new_from_template (pad_template, "src");
1738
1739   gst_pad_set_event_function (self->srcpad,
1740       GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
1741   gst_pad_set_query_function (self->srcpad,
1742       GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
1743   gst_pad_set_activatemode_function (self->srcpad,
1744       GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
1745
1746   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1747
1748   self->priv->latency = DEFAULT_LATENCY;
1749
1750   g_mutex_init (&self->priv->src_lock);
1751   g_cond_init (&self->priv->src_cond);
1752 }
1753
1754 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1755  * method to get to the padtemplates */
1756 GType
1757 gst_aggregator_get_type (void)
1758 {
1759   static volatile gsize type = 0;
1760
1761   if (g_once_init_enter (&type)) {
1762     GType _type;
1763     static const GTypeInfo info = {
1764       sizeof (GstAggregatorClass),
1765       NULL,
1766       NULL,
1767       (GClassInitFunc) gst_aggregator_class_init,
1768       NULL,
1769       NULL,
1770       sizeof (GstAggregator),
1771       0,
1772       (GInstanceInitFunc) gst_aggregator_init,
1773     };
1774
1775     _type = g_type_register_static (GST_TYPE_ELEMENT,
1776         "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1777     g_once_init_leave (&type, _type);
1778   }
1779   return type;
1780 }
1781
1782 static GstFlowReturn
1783 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1784 {
1785   GstBuffer *actual_buf = buffer;
1786   GstAggregator *self = GST_AGGREGATOR (object);
1787   GstAggregatorPrivate *priv = self->priv;
1788   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1789   GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1790   GstFlowReturn flow_return;
1791
1792   GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1793
1794   PAD_STREAM_LOCK (aggpad);
1795
1796   if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1797     goto flushing;
1798
1799   PAD_LOCK (aggpad);
1800   if (aggpad->priv->pending_eos == TRUE)
1801     goto eos;
1802
1803   while (aggpad->priv->buffer
1804       && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1805     GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1806     PAD_WAIT_EVENT (aggpad);
1807   }
1808   PAD_UNLOCK (aggpad);
1809
1810   if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1811     goto flushing;
1812
1813   if (aggclass->clip) {
1814     aggclass->clip (self, aggpad, buffer, &actual_buf);
1815   }
1816
1817   SRC_STREAM_LOCK (self);
1818   PAD_LOCK (aggpad);
1819   if (aggpad->priv->buffer)
1820     gst_buffer_unref (aggpad->priv->buffer);
1821   aggpad->priv->buffer = actual_buf;
1822   PAD_UNLOCK (aggpad);
1823   PAD_STREAM_UNLOCK (aggpad);
1824
1825   SRC_STREAM_BROADCAST (self);
1826   SRC_STREAM_UNLOCK (self);
1827
1828   GST_DEBUG_OBJECT (aggpad, "Done chaining");
1829
1830   GST_OBJECT_LOCK (self);
1831   flow_return = priv->flow_return;
1832   GST_OBJECT_UNLOCK (self);
1833
1834   return flow_return;
1835
1836 flushing:
1837   PAD_STREAM_UNLOCK (aggpad);
1838
1839   gst_buffer_unref (buffer);
1840   GST_DEBUG_OBJECT (aggpad, "We are flushing");
1841
1842   return GST_FLOW_FLUSHING;
1843
1844 eos:
1845   PAD_UNLOCK (aggpad);
1846   PAD_STREAM_UNLOCK (aggpad);
1847
1848   gst_buffer_unref (buffer);
1849   GST_DEBUG_OBJECT (pad, "We are EOS already...");
1850
1851   return GST_FLOW_EOS;
1852 }
1853
1854 static gboolean
1855 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
1856     GstQuery * query)
1857 {
1858   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1859   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1860
1861   if (GST_QUERY_IS_SERIALIZED (query)) {
1862     PAD_LOCK (aggpad);
1863
1864     if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
1865       PAD_UNLOCK (aggpad);
1866       goto flushing;
1867     }
1868
1869     while (aggpad->priv->buffer
1870         && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1871       GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1872       PAD_WAIT_EVENT (aggpad);
1873     }
1874     PAD_UNLOCK (aggpad);
1875
1876     if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1877       goto flushing;
1878   }
1879
1880   return klass->sink_query (GST_AGGREGATOR (parent),
1881       GST_AGGREGATOR_PAD (pad), query);
1882
1883 flushing:
1884   GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping query");
1885   return FALSE;
1886 }
1887
1888 static gboolean
1889 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
1890     GstEvent * event)
1891 {
1892   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1893   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1894
1895   if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1896       && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1897     PAD_LOCK (aggpad);
1898
1899     if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1900         && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
1901       PAD_UNLOCK (aggpad);
1902       goto flushing;
1903     }
1904
1905     while (aggpad->priv->buffer
1906         && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1907       GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1908       PAD_WAIT_EVENT (aggpad);
1909     }
1910     PAD_UNLOCK (aggpad);
1911
1912     if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1913         && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1914       goto flushing;
1915   }
1916
1917   return klass->sink_event (GST_AGGREGATOR (parent),
1918       GST_AGGREGATOR_PAD (pad), event);
1919
1920 flushing:
1921   GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping event");
1922   if (GST_EVENT_IS_STICKY (event))
1923     gst_pad_store_sticky_event (pad, event);
1924   gst_event_unref (event);
1925   return FALSE;
1926 }
1927
1928 static gboolean
1929 gst_aggregator_pad_activate_mode_func (GstPad * pad,
1930     GstObject * parent, GstPadMode mode, gboolean active)
1931 {
1932   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1933
1934   if (active == FALSE) {
1935     PAD_LOCK (aggpad);
1936     g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1937     gst_buffer_replace (&aggpad->priv->buffer, NULL);
1938     PAD_BROADCAST_EVENT (aggpad);
1939     PAD_UNLOCK (aggpad);
1940   } else {
1941     PAD_LOCK (aggpad);
1942     g_atomic_int_set (&aggpad->priv->flushing, FALSE);
1943     PAD_BROADCAST_EVENT (aggpad);
1944     PAD_UNLOCK (aggpad);
1945   }
1946
1947   return TRUE;
1948 }
1949
1950 /***********************************
1951  * GstAggregatorPad implementation  *
1952  ************************************/
1953 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1954
1955 static void
1956 gst_aggregator_pad_constructed (GObject * object)
1957 {
1958   GstPad *pad = GST_PAD (object);
1959
1960   gst_pad_set_chain_function (pad,
1961       GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
1962   gst_pad_set_event_function (pad,
1963       GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
1964   gst_pad_set_query_function (pad,
1965       GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
1966   gst_pad_set_activatemode_function (pad,
1967       GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
1968 }
1969
1970 static void
1971 gst_aggregator_pad_finalize (GObject * object)
1972 {
1973   GstAggregatorPad *pad = (GstAggregatorPad *) object;
1974
1975   g_cond_clear (&pad->priv->event_cond);
1976   g_mutex_clear (&pad->priv->stream_lock);
1977   g_mutex_clear (&pad->priv->lock);
1978
1979   G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
1980 }
1981
1982 static void
1983 gst_aggregator_pad_dispose (GObject * object)
1984 {
1985   GstAggregatorPad *pad = (GstAggregatorPad *) object;
1986   GstBuffer *buf;
1987
1988   buf = gst_aggregator_pad_steal_buffer (pad);
1989   if (buf)
1990     gst_buffer_unref (buf);
1991
1992   G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
1993 }
1994
1995 static void
1996 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
1997 {
1998   GObjectClass *gobject_class = (GObjectClass *) klass;
1999
2000   g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2001
2002   gobject_class->constructed = gst_aggregator_pad_constructed;
2003   gobject_class->finalize = gst_aggregator_pad_finalize;
2004   gobject_class->dispose = gst_aggregator_pad_dispose;
2005 }
2006
2007 static void
2008 gst_aggregator_pad_init (GstAggregatorPad * pad)
2009 {
2010   pad->priv =
2011       G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2012       GstAggregatorPadPrivate);
2013
2014   pad->priv->buffer = NULL;
2015   g_cond_init (&pad->priv->event_cond);
2016
2017   g_mutex_init (&pad->priv->stream_lock);
2018   g_mutex_init (&pad->priv->lock);
2019 }
2020
2021 /**
2022  * gst_aggregator_pad_steal_buffer:
2023  * @pad: the pad to get buffer from
2024  *
2025  * Steal the ref to the buffer currently queued in @pad.
2026  *
2027  * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2028  *   queued. You should unref the buffer after usage.
2029  */
2030 GstBuffer *
2031 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2032 {
2033   GstBuffer *buffer = NULL;
2034
2035   PAD_LOCK (pad);
2036   if (pad->priv->buffer) {
2037     GST_TRACE_OBJECT (pad, "Consuming buffer");
2038     buffer = pad->priv->buffer;
2039     pad->priv->buffer = NULL;
2040     if (pad->priv->pending_eos) {
2041       pad->priv->pending_eos = FALSE;
2042       pad->priv->eos = TRUE;
2043     }
2044     PAD_BROADCAST_EVENT (pad);
2045     GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2046   }
2047   PAD_UNLOCK (pad);
2048
2049   return buffer;
2050 }
2051
2052 /**
2053  * gst_aggregator_pad_get_buffer:
2054  * @pad: the pad to get buffer from
2055  *
2056  * Returns: (transfer full): A reference to the buffer in @pad or
2057  * NULL if no buffer was queued. You should unref the buffer after
2058  * usage.
2059  */
2060 GstBuffer *
2061 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2062 {
2063   GstBuffer *buffer = NULL;
2064
2065   PAD_LOCK (pad);
2066   if (pad->priv->buffer)
2067     buffer = gst_buffer_ref (pad->priv->buffer);
2068   PAD_UNLOCK (pad);
2069
2070   return buffer;
2071 }
2072
2073 gboolean
2074 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2075 {
2076   gboolean is_eos;
2077
2078   PAD_LOCK (pad);
2079   is_eos = pad->priv->eos;
2080   PAD_UNLOCK (pad);
2081
2082   return is_eos;
2083 }
2084
2085 /**
2086  * gst_aggregator_merge_tags:
2087  * @self: a #GstAggregator
2088  * @tags: a #GstTagList to merge
2089  * @mode: the #GstTagMergeMode to use
2090  *
2091  * Adds tags to so-called pending tags, which will be processed
2092  * before pushing out data downstream.
2093  *
2094  * Note that this is provided for convenience, and the subclass is
2095  * not required to use this and can still do tag handling on its own.
2096  *
2097  * MT safe.
2098  */
2099 void
2100 gst_aggregator_merge_tags (GstAggregator * self,
2101     const GstTagList * tags, GstTagMergeMode mode)
2102 {
2103   GstTagList *otags;
2104
2105   g_return_if_fail (GST_IS_AGGREGATOR (self));
2106   g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2107
2108   /* FIXME Check if we can use OBJECT lock here! */
2109   GST_OBJECT_LOCK (self);
2110   if (tags)
2111     GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2112   otags = self->priv->tags;
2113   self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2114   if (otags)
2115     gst_tag_list_unref (otags);
2116   self->priv->tags_changed = TRUE;
2117   GST_OBJECT_UNLOCK (self);
2118 }
2119
2120 /**
2121  * gst_aggregator_set_latency:
2122  * @self: a #GstAggregator
2123  * @min_latency: minimum latency
2124  * @max_latency: maximum latency
2125  *
2126  * Lets #GstAggregator sub-classes tell the baseclass what their internal
2127  * latency is. Will also post a LATENCY message on the bus so the pipeline
2128  * can reconfigure its global latency.
2129  */
2130 void
2131 gst_aggregator_set_latency (GstAggregator * self,
2132     GstClockTime min_latency, GstClockTime max_latency)
2133 {
2134   g_return_if_fail (GST_IS_AGGREGATOR (self));
2135   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2136   g_return_if_fail (max_latency >= min_latency);
2137
2138   GST_OBJECT_LOCK (self);
2139   self->priv->sub_latency_min = min_latency;
2140   self->priv->sub_latency_max = max_latency;
2141   GST_OBJECT_UNLOCK (self);
2142
2143   gst_element_post_message (GST_ELEMENT_CAST (self),
2144       gst_message_new_latency (GST_OBJECT_CAST (self)));
2145 }