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