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