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