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