aggregator: Delay clipping to output thread
[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  * @title: GstAggregator
25  * @short_description: manages a set of pads with the purpose of
26  * aggregating their buffers.
27  * @see_also: gstcollectpads for historical reasons.
28  *
29  * Manages a set of pads with the purpose of aggregating their buffers.
30  * Control is given to the subclass when all pads have data.
31  *
32  *  * Base class for mixers and muxers. Subclasses should at least implement
33  *    the #GstAggregatorClass.aggregate() virtual method.
34  *
35  *  * When data is queued on all pads, tha aggregate vmethod is called.
36  *
37  *  * One can peek at the data on any given GstAggregatorPad with the
38  *    gst_aggregator_pad_get_buffer () method, and take ownership of it
39  *    with the gst_aggregator_pad_steal_buffer () method. When a buffer
40  *    has been taken with steal_buffer (), a new buffer can be queued
41  *    on that pad.
42  *
43  *  * If the subclass wishes to push a buffer downstream in its aggregate
44  *    implementation, it should do so through the
45  *    gst_aggregator_finish_buffer () method. This method will take care
46  *    of sending and ordering mandatory events such as stream start, caps
47  *    and segment.
48  *
49  *  * Same goes for EOS events, which should not be pushed directly by the
50  *    subclass, it should instead return GST_FLOW_EOS in its aggregate
51  *    implementation.
52  *
53  *  * Note that the aggregator logic regarding gap event handling is to turn
54  *    these into gap buffers with matching PTS and duration. It will also
55  *    flag these buffers with GST_BUFFER_FLAG_GAP and GST_BUFFER_FLAG_DROPPABLE
56  *    to ease their identification and subsequent processing.
57  *
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 typedef enum
69 {
70   GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
71   GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
72   GST_AGGREGATOR_START_TIME_SELECTION_SET
73 } GstAggregatorStartTimeSelection;
74
75 static GType
76 gst_aggregator_start_time_selection_get_type (void)
77 {
78   static GType gtype = 0;
79
80   if (gtype == 0) {
81     static const GEnumValue values[] = {
82       {GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
83           "Start at 0 running time (default)", "zero"},
84       {GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
85           "Start at first observed input running time", "first"},
86       {GST_AGGREGATOR_START_TIME_SELECTION_SET,
87           "Set start time with start-time property", "set"},
88       {0, NULL, NULL}
89     };
90
91     gtype = g_enum_register_static ("GstAggregatorStartTimeSelection", values);
92   }
93   return gtype;
94 }
95
96 /*  Might become API */
97 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
98     const GstTagList * tags, GstTagMergeMode mode);
99 static void gst_aggregator_set_latency_property (GstAggregator * agg,
100     gint64 latency);
101 static gint64 gst_aggregator_get_latency_property (GstAggregator * agg);
102
103
104 /* Locking order, locks in this element must always be taken in this order
105  *
106  * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
107  * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
108  * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
109  * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
110  * standard element object lock -> GST_OBJECT_LOCK(agg)
111  * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
112  * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
113  * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
114  */
115
116
117 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
118
119 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
120 #define GST_CAT_DEFAULT aggregator_debug
121
122 /* GstAggregatorPad definitions */
123 #define PAD_LOCK(pad)   G_STMT_START {                                  \
124   GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p",              \
125         g_thread_self());                                               \
126   g_mutex_lock(&pad->priv->lock);                                       \
127   GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p",                \
128         g_thread_self());                                               \
129   } G_STMT_END
130
131 #define PAD_UNLOCK(pad)  G_STMT_START {                                 \
132   GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p",           \
133       g_thread_self());                                                 \
134   g_mutex_unlock(&pad->priv->lock);                                     \
135   GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p",             \
136         g_thread_self());                                               \
137   } G_STMT_END
138
139
140 #define PAD_WAIT_EVENT(pad)   G_STMT_START {                            \
141   GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p",   \
142         g_thread_self());                                               \
143   g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond),           \
144       (&((GstAggregatorPad*)pad)->priv->lock));                         \
145   GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
146         g_thread_self());                                               \
147   } G_STMT_END
148
149 #define PAD_BROADCAST_EVENT(pad) G_STMT_START {                        \
150   GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p",     \
151         g_thread_self());                                              \
152   g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond));    \
153   } G_STMT_END
154
155
156 #define PAD_FLUSH_LOCK(pad)     G_STMT_START {                          \
157   GST_TRACE_OBJECT (pad, "Taking lock from thread %p",                  \
158         g_thread_self());                                               \
159   g_mutex_lock(&pad->priv->flush_lock);                                 \
160   GST_TRACE_OBJECT (pad, "Took lock from thread %p",                    \
161         g_thread_self());                                               \
162   } G_STMT_END
163
164 #define PAD_FLUSH_UNLOCK(pad)   G_STMT_START {                          \
165   GST_TRACE_OBJECT (pad, "Releasing lock from thread %p",               \
166         g_thread_self());                                               \
167   g_mutex_unlock(&pad->priv->flush_lock);                               \
168   GST_TRACE_OBJECT (pad, "Release lock from thread %p",                 \
169         g_thread_self());                                               \
170   } G_STMT_END
171
172 #define SRC_LOCK(self)   G_STMT_START {                             \
173   GST_TRACE_OBJECT (self, "Taking src lock from thread %p",         \
174       g_thread_self());                                             \
175   g_mutex_lock(&self->priv->src_lock);                              \
176   GST_TRACE_OBJECT (self, "Took src lock from thread %p",           \
177         g_thread_self());                                           \
178   } G_STMT_END
179
180 #define SRC_UNLOCK(self)  G_STMT_START {                            \
181   GST_TRACE_OBJECT (self, "Releasing src lock from thread %p",      \
182         g_thread_self());                                           \
183   g_mutex_unlock(&self->priv->src_lock);                            \
184   GST_TRACE_OBJECT (self, "Released src lock from thread %p",       \
185         g_thread_self());                                           \
186   } G_STMT_END
187
188 #define SRC_WAIT(self) G_STMT_START {                               \
189   GST_LOG_OBJECT (self, "Waiting for src on thread %p",             \
190         g_thread_self());                                           \
191   g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock));    \
192   GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p",        \
193         g_thread_self());                                           \
194   } G_STMT_END
195
196 #define SRC_BROADCAST(self) G_STMT_START {                          \
197     GST_LOG_OBJECT (self, "Signaling src from thread %p",           \
198         g_thread_self());                                           \
199     if (self->priv->aggregate_id)                                   \
200       gst_clock_id_unschedule (self->priv->aggregate_id);           \
201     g_cond_broadcast(&(self->priv->src_cond));                      \
202   } G_STMT_END
203
204 struct _GstAggregatorPadPrivate
205 {
206   /* Following fields are protected by the PAD_LOCK */
207   GstFlowReturn flow_return;
208   gboolean pending_flush_start;
209   gboolean pending_flush_stop;
210   gboolean pending_eos;
211
212   gboolean first_buffer;
213
214   GQueue buffers;
215   GstBuffer *clipped_buffer;
216   guint num_buffers;
217   GstClockTime head_position;
218   GstClockTime tail_position;
219   GstClockTime head_time;
220   GstClockTime tail_time;
221   GstClockTime time_level;
222   GstSegment head_segment;      /* segment before the queue */
223
224   gboolean eos;
225
226   GMutex lock;
227   GCond event_cond;
228   /* This lock prevents a flush start processing happening while
229    * the chain function is also happening.
230    */
231   GMutex flush_lock;
232 };
233
234 /* Must be called with PAD_LOCK held */
235 static void
236 gst_aggregator_pad_reset_unlocked (GstAggregatorPad * aggpad)
237 {
238   aggpad->priv->pending_eos = FALSE;
239   aggpad->priv->eos = FALSE;
240   aggpad->priv->flow_return = GST_FLOW_OK;
241   GST_OBJECT_LOCK (aggpad);
242   gst_segment_init (&aggpad->segment, GST_FORMAT_UNDEFINED);
243   gst_segment_init (&aggpad->priv->head_segment, GST_FORMAT_UNDEFINED);
244   GST_OBJECT_UNLOCK (aggpad);
245   aggpad->priv->head_position = GST_CLOCK_TIME_NONE;
246   aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
247   aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
248   aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
249   aggpad->priv->time_level = 0;
250   aggpad->priv->first_buffer = TRUE;
251 }
252
253 static gboolean
254 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
255 {
256   GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
257
258   PAD_LOCK (aggpad);
259   gst_aggregator_pad_reset_unlocked (aggpad);
260   PAD_UNLOCK (aggpad);
261
262   if (klass->flush)
263     return klass->flush (aggpad, agg);
264
265   return TRUE;
266 }
267
268 /*************************************
269  * GstAggregator implementation  *
270  *************************************/
271 static GstElementClass *aggregator_parent_class = NULL;
272
273 /* All members are protected by the object lock unless otherwise noted */
274
275 struct _GstAggregatorPrivate
276 {
277   gint max_padserial;
278
279   /* Our state is >= PAUSED */
280   gboolean running;             /* protected by src_lock */
281
282   gint seqnum;
283   gboolean send_stream_start;   /* protected by srcpad stream lock */
284   gboolean send_segment;
285   gboolean flush_seeking;
286   gboolean pending_flush_start;
287   gboolean send_eos;            /* protected by srcpad stream lock */
288
289   GstCaps *srccaps;             /* protected by the srcpad stream lock */
290
291   GstTagList *tags;
292   gboolean tags_changed;
293
294   gboolean peer_latency_live;   /* protected by src_lock */
295   GstClockTime peer_latency_min;        /* protected by src_lock */
296   GstClockTime peer_latency_max;        /* protected by src_lock */
297   gboolean has_peer_latency;    /* protected by src_lock */
298
299   GstClockTime sub_latency_min; /* protected by src_lock */
300   GstClockTime sub_latency_max; /* protected by src_lock */
301
302   /* aggregate */
303   GstClockID aggregate_id;      /* protected by src_lock */
304   GMutex src_lock;
305   GCond src_cond;
306
307   gboolean first_buffer;        /* protected by object lock */
308   GstAggregatorStartTimeSelection start_time_selection;
309   GstClockTime start_time;
310
311   /* properties */
312   gint64 latency;               /* protected by both src_lock and all pad locks */
313 };
314
315 typedef struct
316 {
317   GstEvent *event;
318   gboolean result;
319   gboolean flush;
320   gboolean only_to_active_pads;
321
322   gboolean one_actually_seeked;
323 } EventData;
324
325 #define DEFAULT_LATENCY              0
326 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
327 #define DEFAULT_START_TIME           (-1)
328
329 enum
330 {
331   PROP_0,
332   PROP_LATENCY,
333   PROP_START_TIME_SELECTION,
334   PROP_START_TIME,
335   PROP_LAST
336 };
337
338 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
339     GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
340
341 /**
342  * gst_aggregator_iterate_sinkpads:
343  * @self: The #GstAggregator
344  * @func: (scope call): The function to call.
345  * @user_data: (closure): The data to pass to @func.
346  *
347  * Iterate the sinkpads of aggregator to call a function on them.
348  *
349  * This method guarantees that @func will be called only once for each
350  * sink pad.
351  */
352 gboolean
353 gst_aggregator_iterate_sinkpads (GstAggregator * self,
354     GstAggregatorPadForeachFunc func, gpointer user_data)
355 {
356   gboolean result = FALSE;
357   GstIterator *iter;
358   gboolean done = FALSE;
359   GValue item = { 0, };
360   GList *seen_pads = NULL;
361
362   iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
363
364   if (!iter)
365     goto no_iter;
366
367   while (!done) {
368     switch (gst_iterator_next (iter, &item)) {
369       case GST_ITERATOR_OK:
370       {
371         GstAggregatorPad *pad;
372
373         pad = g_value_get_object (&item);
374
375         /* if already pushed, skip. FIXME, find something faster to tag pads */
376         if (pad == NULL || g_list_find (seen_pads, pad)) {
377           g_value_reset (&item);
378           break;
379         }
380
381         GST_LOG_OBJECT (pad, "calling function %s on pad",
382             GST_DEBUG_FUNCPTR_NAME (func));
383
384         result = func (self, pad, user_data);
385
386         done = !result;
387
388         seen_pads = g_list_prepend (seen_pads, pad);
389
390         g_value_reset (&item);
391         break;
392       }
393       case GST_ITERATOR_RESYNC:
394         gst_iterator_resync (iter);
395         break;
396       case GST_ITERATOR_ERROR:
397         GST_ERROR_OBJECT (self,
398             "Could not iterate over internally linked pads");
399         done = TRUE;
400         break;
401       case GST_ITERATOR_DONE:
402         done = TRUE;
403         break;
404     }
405   }
406   g_value_unset (&item);
407   gst_iterator_free (iter);
408
409   if (seen_pads == NULL) {
410     GST_DEBUG_OBJECT (self, "No pad seen");
411     return FALSE;
412   }
413
414   g_list_free (seen_pads);
415
416 no_iter:
417   return result;
418 }
419
420 static gboolean
421 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
422 {
423   return (g_queue_peek_tail (&pad->priv->buffers) == NULL &&
424       pad->priv->clipped_buffer == NULL);
425 }
426
427 static gboolean
428 gst_aggregator_check_pads_ready (GstAggregator * self)
429 {
430   GstAggregatorPad *pad;
431   GList *l, *sinkpads;
432   gboolean have_buffer = TRUE;
433   gboolean have_event = FALSE;
434
435   GST_LOG_OBJECT (self, "checking pads");
436
437   GST_OBJECT_LOCK (self);
438
439   sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
440   if (sinkpads == NULL)
441     goto no_sinkpads;
442
443   for (l = sinkpads; l != NULL; l = l->next) {
444     pad = l->data;
445
446     PAD_LOCK (pad);
447
448     if (pad->priv->num_buffers == 0) {
449       if (!gst_aggregator_pad_queue_is_empty (pad))
450         have_event = TRUE;
451       if (!pad->priv->eos) {
452         have_buffer = FALSE;
453
454         /* If not live we need data on all pads, so leave the loop */
455         if (!self->priv->peer_latency_live) {
456           PAD_UNLOCK (pad);
457           goto pad_not_ready;
458         }
459       }
460     } else if (self->priv->peer_latency_live) {
461       /* In live mode, having a single pad with buffers is enough to
462        * generate a start time from it. In non-live mode all pads need
463        * to have a buffer
464        */
465       self->priv->first_buffer = FALSE;
466     }
467
468     PAD_UNLOCK (pad);
469   }
470
471   if (!have_buffer && !have_event)
472     goto pad_not_ready;
473
474   if (have_buffer)
475     self->priv->first_buffer = FALSE;
476
477   GST_OBJECT_UNLOCK (self);
478   GST_LOG_OBJECT (self, "pads are ready");
479   return TRUE;
480
481 no_sinkpads:
482   {
483     GST_LOG_OBJECT (self, "pads not ready: no sink pads");
484     GST_OBJECT_UNLOCK (self);
485     return FALSE;
486   }
487 pad_not_ready:
488   {
489     if (have_event)
490       GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet,"
491           " but waking up for serialized event");
492     else
493       GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
494     GST_OBJECT_UNLOCK (self);
495     return have_event;
496   }
497 }
498
499 static void
500 gst_aggregator_reset_flow_values (GstAggregator * self)
501 {
502   GST_OBJECT_LOCK (self);
503   self->priv->send_stream_start = TRUE;
504   self->priv->send_segment = TRUE;
505   gst_segment_init (&self->segment, GST_FORMAT_TIME);
506   self->priv->first_buffer = TRUE;
507   GST_OBJECT_UNLOCK (self);
508 }
509
510 static inline void
511 gst_aggregator_push_mandatory_events (GstAggregator * self)
512 {
513   GstAggregatorPrivate *priv = self->priv;
514   GstEvent *segment = NULL;
515   GstEvent *tags = NULL;
516
517   if (self->priv->send_stream_start) {
518     gchar s_id[32];
519
520     GST_INFO_OBJECT (self, "pushing stream start");
521     /* stream-start (FIXME: create id based on input ids) */
522     g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
523     if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
524       GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
525     }
526     self->priv->send_stream_start = FALSE;
527   }
528
529   if (self->priv->srccaps) {
530
531     GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
532         self->priv->srccaps);
533     if (!gst_pad_push_event (self->srcpad,
534             gst_event_new_caps (self->priv->srccaps))) {
535       GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
536     }
537     gst_caps_unref (self->priv->srccaps);
538     self->priv->srccaps = NULL;
539   }
540
541   GST_OBJECT_LOCK (self);
542   if (self->priv->send_segment && !self->priv->flush_seeking) {
543     segment = gst_event_new_segment (&self->segment);
544
545     if (!self->priv->seqnum)
546       self->priv->seqnum = gst_event_get_seqnum (segment);
547     else
548       gst_event_set_seqnum (segment, self->priv->seqnum);
549     self->priv->send_segment = FALSE;
550
551     GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
552   }
553
554   if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
555     tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
556     priv->tags_changed = FALSE;
557   }
558   GST_OBJECT_UNLOCK (self);
559
560   if (segment)
561     gst_pad_push_event (self->srcpad, segment);
562   if (tags)
563     gst_pad_push_event (self->srcpad, tags);
564
565 }
566
567 /**
568  * gst_aggregator_set_src_caps:
569  * @self: The #GstAggregator
570  * @caps: The #GstCaps to set on the src pad.
571  *
572  * Sets the caps to be used on the src pad.
573  */
574 void
575 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
576 {
577   GST_PAD_STREAM_LOCK (self->srcpad);
578   gst_caps_replace (&self->priv->srccaps, caps);
579   gst_aggregator_push_mandatory_events (self);
580   GST_PAD_STREAM_UNLOCK (self->srcpad);
581 }
582
583 /**
584  * gst_aggregator_finish_buffer:
585  * @self: The #GstAggregator
586  * @buffer: (transfer full): the #GstBuffer to push.
587  *
588  * This method will push the provided output buffer downstream. If needed,
589  * mandatory events such as stream-start, caps, and segment events will be
590  * sent before pushing the buffer.
591  */
592 GstFlowReturn
593 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
594 {
595   gst_aggregator_push_mandatory_events (self);
596
597   GST_OBJECT_LOCK (self);
598   if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
599     GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
600     GST_OBJECT_UNLOCK (self);
601     return gst_pad_push (self->srcpad, buffer);
602   } else {
603     GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
604         self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
605     GST_OBJECT_UNLOCK (self);
606     gst_buffer_unref (buffer);
607     return GST_FLOW_OK;
608   }
609 }
610
611 static void
612 gst_aggregator_push_eos (GstAggregator * self)
613 {
614   GstEvent *event;
615   gst_aggregator_push_mandatory_events (self);
616
617   event = gst_event_new_eos ();
618
619   GST_OBJECT_LOCK (self);
620   self->priv->send_eos = FALSE;
621   gst_event_set_seqnum (event, self->priv->seqnum);
622   GST_OBJECT_UNLOCK (self);
623
624   gst_pad_push_event (self->srcpad, event);
625 }
626
627 static GstClockTime
628 gst_aggregator_get_next_time (GstAggregator * self)
629 {
630   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
631
632   if (klass->get_next_time)
633     return klass->get_next_time (self);
634
635   return GST_CLOCK_TIME_NONE;
636 }
637
638 static gboolean
639 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
640 {
641   GstClockTime latency;
642   GstClockTime start;
643   gboolean res;
644
645   *timeout = FALSE;
646
647   SRC_LOCK (self);
648
649   latency = gst_aggregator_get_latency_unlocked (self);
650
651   if (gst_aggregator_check_pads_ready (self)) {
652     GST_DEBUG_OBJECT (self, "all pads have data");
653     SRC_UNLOCK (self);
654
655     return TRUE;
656   }
657
658   /* Before waiting, check if we're actually still running */
659   if (!self->priv->running || !self->priv->send_eos) {
660     SRC_UNLOCK (self);
661
662     return FALSE;
663   }
664
665   start = gst_aggregator_get_next_time (self);
666
667   /* If we're not live, or if we use the running time
668    * of the first buffer as start time, we wait until
669    * all pads have buffers.
670    * Otherwise (i.e. if we are live!), we wait on the clock
671    * and if a pad does not have a buffer in time we ignore
672    * that pad.
673    */
674   GST_OBJECT_LOCK (self);
675   if (!GST_CLOCK_TIME_IS_VALID (latency) ||
676       !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
677       !GST_CLOCK_TIME_IS_VALID (start) ||
678       (self->priv->first_buffer
679           && self->priv->start_time_selection ==
680           GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
681     /* We wake up here when something happened, and below
682      * then check if we're ready now. If we return FALSE,
683      * we will be directly called again.
684      */
685     GST_OBJECT_UNLOCK (self);
686     SRC_WAIT (self);
687   } else {
688     GstClockTime base_time, time;
689     GstClock *clock;
690     GstClockReturn status;
691     GstClockTimeDiff jitter;
692
693     GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
694         GST_TIME_ARGS (start));
695
696     base_time = GST_ELEMENT_CAST (self)->base_time;
697     clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
698     GST_OBJECT_UNLOCK (self);
699
700     time = base_time + start;
701     time += latency;
702
703     GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
704         GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
705         " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
706         GST_TIME_ARGS (time),
707         GST_TIME_ARGS (base_time),
708         GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
709         GST_TIME_ARGS (gst_clock_get_time (clock)));
710
711     self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
712     gst_object_unref (clock);
713     SRC_UNLOCK (self);
714
715     jitter = 0;
716     status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
717
718     SRC_LOCK (self);
719     if (self->priv->aggregate_id) {
720       gst_clock_id_unref (self->priv->aggregate_id);
721       self->priv->aggregate_id = NULL;
722     }
723
724     GST_DEBUG_OBJECT (self,
725         "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
726         status, GST_STIME_ARGS (jitter));
727
728     /* we timed out */
729     if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
730       SRC_UNLOCK (self);
731       *timeout = TRUE;
732       return TRUE;
733     }
734   }
735
736   res = gst_aggregator_check_pads_ready (self);
737   SRC_UNLOCK (self);
738
739   return res;
740 }
741
742 static gboolean
743 check_events (GstAggregator * self, GstAggregatorPad * pad, gpointer user_data)
744 {
745   GstEvent *event = NULL;
746   GstAggregatorClass *klass = NULL;
747   gboolean *processed_event = user_data;
748
749   do {
750     event = NULL;
751
752     PAD_LOCK (pad);
753     if (pad->priv->num_buffers == 0 && pad->priv->pending_eos) {
754       pad->priv->pending_eos = FALSE;
755       pad->priv->eos = TRUE;
756     }
757     if (pad->priv->clipped_buffer == NULL &&
758         GST_IS_EVENT (g_queue_peek_tail (&pad->priv->buffers))) {
759       event = g_queue_pop_tail (&pad->priv->buffers);
760       PAD_BROADCAST_EVENT (pad);
761     }
762     PAD_UNLOCK (pad);
763     if (event) {
764       if (processed_event)
765         *processed_event = TRUE;
766       if (klass == NULL)
767         klass = GST_AGGREGATOR_GET_CLASS (self);
768
769       GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
770       klass->sink_event (self, pad, event);
771     }
772   } while (event != NULL);
773
774   return TRUE;
775 }
776
777 static void
778 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
779     GstFlowReturn flow_return, gboolean full)
780 {
781   GList *item;
782
783   PAD_LOCK (aggpad);
784   if (flow_return == GST_FLOW_NOT_LINKED)
785     aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
786   else
787     aggpad->priv->flow_return = flow_return;
788
789   item = g_queue_peek_head_link (&aggpad->priv->buffers);
790   while (item) {
791     GList *next = item->next;
792
793     /* In partial flush, we do like the pad, we get rid of non-sticky events
794      * and EOS/SEGMENT.
795      */
796     if (full || GST_IS_BUFFER (item->data) ||
797         GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
798         GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
799         !GST_EVENT_IS_STICKY (item->data)) {
800       gst_mini_object_unref (item->data);
801       g_queue_delete_link (&aggpad->priv->buffers, item);
802     }
803     item = next;
804   }
805   aggpad->priv->num_buffers = 0;
806   gst_buffer_replace (&aggpad->priv->clipped_buffer, NULL);
807
808   PAD_BROADCAST_EVENT (aggpad);
809   PAD_UNLOCK (aggpad);
810 }
811
812 static void
813 gst_aggregator_aggregate_func (GstAggregator * self)
814 {
815   GstAggregatorPrivate *priv = self->priv;
816   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
817   gboolean timeout = FALSE;
818
819   if (self->priv->running == FALSE) {
820     GST_DEBUG_OBJECT (self, "Not running anymore");
821     return;
822   }
823
824   GST_LOG_OBJECT (self, "Checking aggregate");
825   while (priv->send_eos && priv->running) {
826     GstFlowReturn flow_return;
827     gboolean processed_event = FALSE;
828
829     gst_aggregator_iterate_sinkpads (self, check_events, NULL);
830
831     if (!gst_aggregator_wait_and_check (self, &timeout))
832       continue;
833
834     gst_aggregator_iterate_sinkpads (self, check_events, &processed_event);
835     if (processed_event)
836       continue;
837
838     GST_TRACE_OBJECT (self, "Actually aggregating!");
839     flow_return = klass->aggregate (self, timeout);
840
841     GST_OBJECT_LOCK (self);
842     if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
843       /* We don't want to set the pads to flushing, but we want to
844        * stop the thread, so just break here */
845       GST_OBJECT_UNLOCK (self);
846       break;
847     }
848     GST_OBJECT_UNLOCK (self);
849
850     if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
851       gst_aggregator_push_eos (self);
852     }
853
854     GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
855
856     if (flow_return != GST_FLOW_OK) {
857       GList *item;
858
859       GST_OBJECT_LOCK (self);
860       for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
861         GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
862
863         gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
864       }
865       GST_OBJECT_UNLOCK (self);
866       break;
867     }
868   }
869
870   /* Pause the task here, the only ways to get here are:
871    * 1) We're stopping, in which case the task is stopped anyway
872    * 2) We got a flow error above, in which case it might take
873    *    some time to forward the flow return upstream and we
874    *    would otherwise call the task function over and over
875    *    again without doing anything
876    */
877   gst_pad_pause_task (self->srcpad);
878 }
879
880 static gboolean
881 gst_aggregator_start (GstAggregator * self)
882 {
883   GstAggregatorClass *klass;
884   gboolean result;
885
886   self->priv->send_stream_start = TRUE;
887   self->priv->send_segment = TRUE;
888   self->priv->send_eos = TRUE;
889   self->priv->srccaps = NULL;
890
891   klass = GST_AGGREGATOR_GET_CLASS (self);
892
893   if (klass->start)
894     result = klass->start (self);
895   else
896     result = TRUE;
897
898   return result;
899 }
900
901 static gboolean
902 _check_pending_flush_stop (GstAggregatorPad * pad)
903 {
904   gboolean res;
905
906   PAD_LOCK (pad);
907   res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
908   PAD_UNLOCK (pad);
909
910   return res;
911 }
912
913 static gboolean
914 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
915 {
916   gboolean res = TRUE;
917
918   GST_INFO_OBJECT (self, "%s srcpad task",
919       flush_start ? "Pausing" : "Stopping");
920
921   SRC_LOCK (self);
922   self->priv->running = FALSE;
923   SRC_BROADCAST (self);
924   SRC_UNLOCK (self);
925
926   if (flush_start) {
927     res = gst_pad_push_event (self->srcpad, flush_start);
928   }
929
930   gst_pad_stop_task (self->srcpad);
931
932   return res;
933 }
934
935 static void
936 gst_aggregator_start_srcpad_task (GstAggregator * self)
937 {
938   GST_INFO_OBJECT (self, "Starting srcpad task");
939
940   self->priv->running = TRUE;
941   gst_pad_start_task (GST_PAD (self->srcpad),
942       (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
943 }
944
945 static GstFlowReturn
946 gst_aggregator_flush (GstAggregator * self)
947 {
948   GstFlowReturn ret = GST_FLOW_OK;
949   GstAggregatorPrivate *priv = self->priv;
950   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
951
952   GST_DEBUG_OBJECT (self, "Flushing everything");
953   GST_OBJECT_LOCK (self);
954   priv->send_segment = TRUE;
955   priv->flush_seeking = FALSE;
956   priv->tags_changed = FALSE;
957   GST_OBJECT_UNLOCK (self);
958   if (klass->flush)
959     ret = klass->flush (self);
960
961   return ret;
962 }
963
964
965 /* Called with GstAggregator's object lock held */
966
967 static gboolean
968 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
969 {
970   GList *tmp;
971   GstAggregatorPad *tmppad;
972
973   for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
974     tmppad = (GstAggregatorPad *) tmp->data;
975
976     if (_check_pending_flush_stop (tmppad) == FALSE) {
977       GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
978           tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
979       return FALSE;
980     }
981   }
982
983   return TRUE;
984 }
985
986 static void
987 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
988     GstEvent * event)
989 {
990   GstAggregatorPrivate *priv = self->priv;
991   GstAggregatorPadPrivate *padpriv = aggpad->priv;
992
993   gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
994
995   PAD_FLUSH_LOCK (aggpad);
996   PAD_LOCK (aggpad);
997   if (padpriv->pending_flush_start) {
998     GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
999
1000     padpriv->pending_flush_start = FALSE;
1001     padpriv->pending_flush_stop = TRUE;
1002   }
1003   PAD_UNLOCK (aggpad);
1004
1005   GST_OBJECT_LOCK (self);
1006   if (priv->flush_seeking) {
1007     /* If flush_seeking we forward the first FLUSH_START */
1008     if (priv->pending_flush_start) {
1009       priv->pending_flush_start = FALSE;
1010       GST_OBJECT_UNLOCK (self);
1011
1012       GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
1013       gst_aggregator_stop_srcpad_task (self, event);
1014
1015       GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
1016       GST_PAD_STREAM_LOCK (self->srcpad);
1017       GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
1018       event = NULL;
1019     } else {
1020       GST_OBJECT_UNLOCK (self);
1021       gst_event_unref (event);
1022     }
1023   } else {
1024     GST_OBJECT_UNLOCK (self);
1025     gst_event_unref (event);
1026   }
1027   PAD_FLUSH_UNLOCK (aggpad);
1028 }
1029
1030 /* Must be called with the the PAD_LOCK held */
1031 static void
1032 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1033 {
1034   if (head) {
1035     if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->head_position) &&
1036         aggpad->priv->head_segment.format == GST_FORMAT_TIME)
1037       aggpad->priv->head_time =
1038           gst_segment_to_running_time (&aggpad->priv->head_segment,
1039           GST_FORMAT_TIME, aggpad->priv->head_position);
1040     else
1041       aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
1042
1043     if (!GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_time))
1044       aggpad->priv->tail_time = aggpad->priv->head_time;
1045   } else {
1046     if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_position) &&
1047         aggpad->segment.format == GST_FORMAT_TIME)
1048       aggpad->priv->tail_time =
1049           gst_segment_to_running_time (&aggpad->segment,
1050           GST_FORMAT_TIME, aggpad->priv->tail_position);
1051     else
1052       aggpad->priv->tail_time = aggpad->priv->head_time;
1053   }
1054
1055   if (aggpad->priv->head_time == GST_CLOCK_TIME_NONE ||
1056       aggpad->priv->tail_time == GST_CLOCK_TIME_NONE) {
1057     aggpad->priv->time_level = 0;
1058     return;
1059   }
1060
1061   if (aggpad->priv->tail_time > aggpad->priv->head_time)
1062     aggpad->priv->time_level = 0;
1063   else
1064     aggpad->priv->time_level = aggpad->priv->head_time -
1065         aggpad->priv->tail_time;
1066 }
1067
1068
1069 /* GstAggregator vmethods default implementations */
1070 static gboolean
1071 gst_aggregator_default_sink_event (GstAggregator * self,
1072     GstAggregatorPad * aggpad, GstEvent * event)
1073 {
1074   gboolean res = TRUE;
1075   GstPad *pad = GST_PAD (aggpad);
1076   GstAggregatorPrivate *priv = self->priv;
1077
1078   switch (GST_EVENT_TYPE (event)) {
1079     case GST_EVENT_FLUSH_START:
1080     {
1081       gst_aggregator_flush_start (self, aggpad, event);
1082       /* We forward only in one case: right after flush_seeking */
1083       event = NULL;
1084       goto eat;
1085     }
1086     case GST_EVENT_FLUSH_STOP:
1087     {
1088       GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
1089
1090       gst_aggregator_pad_flush (aggpad, self);
1091       GST_OBJECT_LOCK (self);
1092       if (priv->flush_seeking) {
1093         g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1094         if (gst_aggregator_all_flush_stop_received_locked (self)) {
1095           GST_OBJECT_UNLOCK (self);
1096           /* That means we received FLUSH_STOP/FLUSH_STOP on
1097            * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1098           gst_aggregator_flush (self);
1099           gst_pad_push_event (self->srcpad, event);
1100           event = NULL;
1101           SRC_LOCK (self);
1102           priv->send_eos = TRUE;
1103           SRC_BROADCAST (self);
1104           SRC_UNLOCK (self);
1105
1106           GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1107           GST_PAD_STREAM_UNLOCK (self->srcpad);
1108           gst_aggregator_start_srcpad_task (self);
1109         } else {
1110           GST_OBJECT_UNLOCK (self);
1111         }
1112       } else {
1113         GST_OBJECT_UNLOCK (self);
1114       }
1115
1116       /* We never forward the event */
1117       goto eat;
1118     }
1119     case GST_EVENT_EOS:
1120     {
1121       GST_DEBUG_OBJECT (aggpad, "EOS");
1122
1123       /* We still have a buffer, and we don't want the subclass to have to
1124        * check for it. Mark pending_eos, eos will be set when steal_buffer is
1125        * called
1126        */
1127       SRC_LOCK (self);
1128       PAD_LOCK (aggpad);
1129       if (aggpad->priv->num_buffers == 0) {
1130         aggpad->priv->eos = TRUE;
1131       } else {
1132         aggpad->priv->pending_eos = TRUE;
1133       }
1134       PAD_UNLOCK (aggpad);
1135
1136       SRC_BROADCAST (self);
1137       SRC_UNLOCK (self);
1138       goto eat;
1139     }
1140     case GST_EVENT_SEGMENT:
1141     {
1142       PAD_LOCK (aggpad);
1143       GST_OBJECT_LOCK (aggpad);
1144       gst_event_copy_segment (event, &aggpad->segment);
1145       update_time_level (aggpad, FALSE);
1146       GST_OBJECT_UNLOCK (aggpad);
1147       PAD_UNLOCK (aggpad);
1148
1149       GST_OBJECT_LOCK (self);
1150       self->priv->seqnum = gst_event_get_seqnum (event);
1151       GST_OBJECT_UNLOCK (self);
1152       goto eat;
1153     }
1154     case GST_EVENT_STREAM_START:
1155     {
1156       goto eat;
1157     }
1158     case GST_EVENT_GAP:
1159     {
1160       GstClockTime pts, endpts;
1161       GstClockTime duration;
1162       GstBuffer *gapbuf;
1163
1164       gst_event_parse_gap (event, &pts, &duration);
1165       gapbuf = gst_buffer_new ();
1166
1167       if (GST_CLOCK_TIME_IS_VALID (duration))
1168         endpts = pts + duration;
1169       else
1170         endpts = GST_CLOCK_TIME_NONE;
1171
1172       GST_OBJECT_LOCK (aggpad);
1173       res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1174           &pts, &endpts);
1175       GST_OBJECT_UNLOCK (aggpad);
1176
1177       if (!res) {
1178         GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1179         goto eat;
1180       }
1181
1182       if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1183         duration = endpts - pts;
1184       else
1185         duration = GST_CLOCK_TIME_NONE;
1186
1187       GST_BUFFER_PTS (gapbuf) = pts;
1188       GST_BUFFER_DURATION (gapbuf) = duration;
1189       GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1190       GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1191
1192       if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1193           GST_FLOW_OK) {
1194         GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1195         res = FALSE;
1196       }
1197
1198       goto eat;
1199     }
1200     case GST_EVENT_TAG:
1201     {
1202       GstTagList *tags;
1203
1204       gst_event_parse_tag (event, &tags);
1205
1206       if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1207         gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1208         gst_event_unref (event);
1209         event = NULL;
1210         goto eat;
1211       }
1212       break;
1213     }
1214     default:
1215     {
1216       break;
1217     }
1218   }
1219
1220   GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1221   return gst_pad_event_default (pad, GST_OBJECT (self), event);
1222
1223 eat:
1224   GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1225   if (event)
1226     gst_event_unref (event);
1227
1228   return res;
1229 }
1230
1231 static inline gboolean
1232 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1233     gpointer unused_udata)
1234 {
1235   gst_aggregator_pad_flush (pad, self);
1236
1237   return TRUE;
1238 }
1239
1240 static gboolean
1241 gst_aggregator_stop (GstAggregator * agg)
1242 {
1243   GstAggregatorClass *klass;
1244   gboolean result;
1245
1246   gst_aggregator_reset_flow_values (agg);
1247
1248   gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1249
1250   klass = GST_AGGREGATOR_GET_CLASS (agg);
1251
1252   if (klass->stop)
1253     result = klass->stop (agg);
1254   else
1255     result = TRUE;
1256
1257   agg->priv->has_peer_latency = FALSE;
1258   agg->priv->peer_latency_live = FALSE;
1259   agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1260
1261   if (agg->priv->tags)
1262     gst_tag_list_unref (agg->priv->tags);
1263   agg->priv->tags = NULL;
1264
1265   return result;
1266 }
1267
1268 /* GstElement vmethods implementations */
1269 static GstStateChangeReturn
1270 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1271 {
1272   GstStateChangeReturn ret;
1273   GstAggregator *self = GST_AGGREGATOR (element);
1274
1275   switch (transition) {
1276     case GST_STATE_CHANGE_READY_TO_PAUSED:
1277       if (!gst_aggregator_start (self))
1278         goto error_start;
1279       break;
1280     default:
1281       break;
1282   }
1283
1284   if ((ret =
1285           GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1286               transition)) == GST_STATE_CHANGE_FAILURE)
1287     goto failure;
1288
1289
1290   switch (transition) {
1291     case GST_STATE_CHANGE_PAUSED_TO_READY:
1292       if (!gst_aggregator_stop (self)) {
1293         /* What to do in this case? Error out? */
1294         GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1295       }
1296       break;
1297     default:
1298       break;
1299   }
1300
1301   return ret;
1302
1303 /* ERRORS */
1304 failure:
1305   {
1306     GST_ERROR_OBJECT (element, "parent failed state change");
1307     return ret;
1308   }
1309 error_start:
1310   {
1311     GST_ERROR_OBJECT (element, "Subclass failed to start");
1312     return GST_STATE_CHANGE_FAILURE;
1313   }
1314 }
1315
1316 static void
1317 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1318 {
1319   GstAggregator *self = GST_AGGREGATOR (element);
1320   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1321
1322   GST_INFO_OBJECT (pad, "Removing pad");
1323
1324   SRC_LOCK (self);
1325   gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1326   gst_element_remove_pad (element, pad);
1327
1328   self->priv->has_peer_latency = FALSE;
1329   SRC_BROADCAST (self);
1330   SRC_UNLOCK (self);
1331 }
1332
1333 static GstAggregatorPad *
1334 gst_aggregator_default_create_new_pad (GstAggregator * self,
1335     GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1336 {
1337   GstAggregatorPad *agg_pad;
1338   GstAggregatorPrivate *priv = self->priv;
1339   gint serial = 0;
1340   gchar *name = NULL;
1341
1342   if (templ->direction != GST_PAD_SINK ||
1343       g_strcmp0 (templ->name_template, "sink_%u") != 0)
1344     goto not_sink;
1345
1346   GST_OBJECT_LOCK (self);
1347   if (req_name == NULL || strlen (req_name) < 6
1348       || !g_str_has_prefix (req_name, "sink_")) {
1349     /* no name given when requesting the pad, use next available int */
1350     serial = ++priv->max_padserial;
1351   } else {
1352     /* parse serial number from requested padname */
1353     serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1354     if (serial > priv->max_padserial)
1355       priv->max_padserial = serial;
1356   }
1357
1358   name = g_strdup_printf ("sink_%u", serial);
1359   agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1360       "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1361   g_free (name);
1362
1363   GST_OBJECT_UNLOCK (self);
1364
1365   return agg_pad;
1366
1367   /* errors */
1368 not_sink:
1369   {
1370     GST_WARNING_OBJECT (self, "request new pad that is not a SINK pad\n");
1371     return NULL;
1372   }
1373 }
1374
1375 static GstPad *
1376 gst_aggregator_request_new_pad (GstElement * element,
1377     GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1378 {
1379   GstAggregator *self;
1380   GstAggregatorPad *agg_pad;
1381   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1382   GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1383
1384   self = GST_AGGREGATOR (element);
1385
1386   agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1387   if (!agg_pad) {
1388     GST_ERROR_OBJECT (element, "Couldn't create new pad");
1389     return NULL;
1390   }
1391
1392   GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1393   self->priv->has_peer_latency = FALSE;
1394
1395   if (priv->running)
1396     gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1397
1398   /* add the pad to the element */
1399   gst_element_add_pad (element, GST_PAD (agg_pad));
1400
1401   return GST_PAD (agg_pad);
1402 }
1403
1404 /* Must be called with SRC_LOCK held */
1405
1406 static gboolean
1407 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1408 {
1409   gboolean query_ret, live;
1410   GstClockTime our_latency, min, max;
1411
1412   query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1413
1414   if (!query_ret) {
1415     GST_WARNING_OBJECT (self, "Latency query failed");
1416     return FALSE;
1417   }
1418
1419   gst_query_parse_latency (query, &live, &min, &max);
1420
1421   our_latency = self->priv->latency;
1422
1423   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1424     GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1425         ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1426     return FALSE;
1427   }
1428
1429   if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1430     GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1431         ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1432             GST_TIME_FORMAT ". Add queues or other buffering elements.",
1433             GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1434     return FALSE;
1435   }
1436
1437   self->priv->peer_latency_live = live;
1438   self->priv->peer_latency_min = min;
1439   self->priv->peer_latency_max = max;
1440   self->priv->has_peer_latency = TRUE;
1441
1442   /* add our own */
1443   min += our_latency;
1444   min += self->priv->sub_latency_min;
1445   if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1446       && GST_CLOCK_TIME_IS_VALID (max))
1447     max += self->priv->sub_latency_max + our_latency;
1448   else
1449     max = GST_CLOCK_TIME_NONE;
1450
1451   SRC_BROADCAST (self);
1452
1453   GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1454       " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1455
1456   gst_query_set_latency (query, live, min, max);
1457
1458   return query_ret;
1459 }
1460
1461 /*
1462  * MUST be called with the src_lock held.
1463  *
1464  * See  gst_aggregator_get_latency() for doc
1465  */
1466 static GstClockTime
1467 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1468 {
1469   GstClockTime latency;
1470
1471   g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1472
1473   if (!self->priv->has_peer_latency) {
1474     GstQuery *query = gst_query_new_latency ();
1475     gboolean ret;
1476
1477     ret = gst_aggregator_query_latency_unlocked (self, query);
1478     gst_query_unref (query);
1479     if (!ret)
1480       return GST_CLOCK_TIME_NONE;
1481   }
1482
1483   if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1484     return GST_CLOCK_TIME_NONE;
1485
1486   /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1487   latency = self->priv->peer_latency_min;
1488
1489   /* add our own */
1490   latency += self->priv->latency;
1491   latency += self->priv->sub_latency_min;
1492
1493   return latency;
1494 }
1495
1496 /**
1497  * gst_aggregator_get_latency:
1498  * @self: a #GstAggregator
1499  *
1500  * Retrieves the latency values reported by @self in response to the latency
1501  * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1502  * will not wait for the clock.
1503  *
1504  * Typically only called by subclasses.
1505  *
1506  * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1507  */
1508 GstClockTime
1509 gst_aggregator_get_latency (GstAggregator * self)
1510 {
1511   GstClockTime ret;
1512
1513   SRC_LOCK (self);
1514   ret = gst_aggregator_get_latency_unlocked (self);
1515   SRC_UNLOCK (self);
1516
1517   return ret;
1518 }
1519
1520 static gboolean
1521 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1522 {
1523   GstAggregator *self = GST_AGGREGATOR (element);
1524
1525   GST_STATE_LOCK (element);
1526   if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1527       GST_STATE (element) < GST_STATE_PAUSED) {
1528     gdouble rate;
1529     GstFormat fmt;
1530     GstSeekFlags flags;
1531     GstSeekType start_type, stop_type;
1532     gint64 start, stop;
1533
1534     gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1535         &start, &stop_type, &stop);
1536
1537     GST_OBJECT_LOCK (self);
1538     gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1539         stop_type, stop, NULL);
1540     self->priv->seqnum = gst_event_get_seqnum (event);
1541     self->priv->first_buffer = FALSE;
1542     GST_OBJECT_UNLOCK (self);
1543
1544     GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1545   }
1546   GST_STATE_UNLOCK (element);
1547
1548
1549   return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1550       event);
1551 }
1552
1553 static gboolean
1554 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1555 {
1556   gboolean res = TRUE;
1557
1558   switch (GST_QUERY_TYPE (query)) {
1559     case GST_QUERY_SEEKING:
1560     {
1561       GstFormat format;
1562
1563       /* don't pass it along as some (file)sink might claim it does
1564        * whereas with a collectpads in between that will not likely work */
1565       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1566       gst_query_set_seeking (query, format, FALSE, 0, -1);
1567       res = TRUE;
1568
1569       break;
1570     }
1571     case GST_QUERY_LATENCY:
1572       SRC_LOCK (self);
1573       res = gst_aggregator_query_latency_unlocked (self, query);
1574       SRC_UNLOCK (self);
1575       break;
1576     default:
1577       return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1578   }
1579
1580   return res;
1581 }
1582
1583 static gboolean
1584 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1585 {
1586   EventData *evdata = user_data;
1587   gboolean ret = TRUE;
1588   GstPad *peer = gst_pad_get_peer (pad);
1589   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1590
1591   if (peer) {
1592     if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1593       GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1594       ret = TRUE;
1595     } else {
1596       ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1597       GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1598       gst_object_unref (peer);
1599     }
1600   }
1601
1602   if (ret == FALSE) {
1603     if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1604       GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1605
1606       GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1607
1608       if (gst_pad_query (peer, seeking)) {
1609         gboolean seekable;
1610
1611         gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1612
1613         if (seekable == FALSE) {
1614           GST_INFO_OBJECT (pad,
1615               "Source not seekable, We failed but it does not matter!");
1616
1617           ret = TRUE;
1618         }
1619       } else {
1620         GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1621       }
1622
1623       gst_query_unref (seeking);
1624     }
1625
1626     if (evdata->flush) {
1627       PAD_LOCK (aggpad);
1628       aggpad->priv->pending_flush_start = FALSE;
1629       aggpad->priv->pending_flush_stop = FALSE;
1630       PAD_UNLOCK (aggpad);
1631     }
1632   } else {
1633     evdata->one_actually_seeked = TRUE;
1634   }
1635
1636   evdata->result &= ret;
1637
1638   /* Always send to all pads */
1639   return FALSE;
1640 }
1641
1642 static EventData
1643 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1644     GstEvent * event, gboolean flush, gboolean only_to_active_pads)
1645 {
1646   EventData evdata;
1647
1648   evdata.event = event;
1649   evdata.result = TRUE;
1650   evdata.flush = flush;
1651   evdata.one_actually_seeked = FALSE;
1652   evdata.only_to_active_pads = only_to_active_pads;
1653
1654   /* We first need to set all pads as flushing in a first pass
1655    * as flush_start flush_stop is sometimes sent synchronously
1656    * while we send the seek event */
1657   if (flush) {
1658     GList *l;
1659
1660     GST_OBJECT_LOCK (self);
1661     for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1662       GstAggregatorPad *pad = l->data;
1663
1664       PAD_LOCK (pad);
1665       pad->priv->pending_flush_start = TRUE;
1666       pad->priv->pending_flush_stop = FALSE;
1667       PAD_UNLOCK (pad);
1668     }
1669     GST_OBJECT_UNLOCK (self);
1670   }
1671
1672   gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1673
1674   gst_event_unref (event);
1675
1676   return evdata;
1677 }
1678
1679 static gboolean
1680 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1681 {
1682   gdouble rate;
1683   GstFormat fmt;
1684   GstSeekFlags flags;
1685   GstSeekType start_type, stop_type;
1686   gint64 start, stop;
1687   gboolean flush;
1688   EventData evdata;
1689   GstAggregatorPrivate *priv = self->priv;
1690
1691   gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1692       &start, &stop_type, &stop);
1693
1694   GST_INFO_OBJECT (self, "starting SEEK");
1695
1696   flush = flags & GST_SEEK_FLAG_FLUSH;
1697
1698   GST_OBJECT_LOCK (self);
1699   if (flush) {
1700     priv->pending_flush_start = TRUE;
1701     priv->flush_seeking = TRUE;
1702   }
1703
1704   gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1705       stop_type, stop, NULL);
1706
1707   /* Seeking sets a position */
1708   self->priv->first_buffer = FALSE;
1709   GST_OBJECT_UNLOCK (self);
1710
1711   /* forward the seek upstream */
1712   evdata =
1713       gst_aggregator_forward_event_to_all_sinkpads (self, event, flush, FALSE);
1714   event = NULL;
1715
1716   if (!evdata.result || !evdata.one_actually_seeked) {
1717     GST_OBJECT_LOCK (self);
1718     priv->flush_seeking = FALSE;
1719     priv->pending_flush_start = FALSE;
1720     GST_OBJECT_UNLOCK (self);
1721   }
1722
1723   GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1724
1725   return evdata.result;
1726 }
1727
1728 static gboolean
1729 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1730 {
1731   EventData evdata;
1732   gboolean res = TRUE;
1733
1734   switch (GST_EVENT_TYPE (event)) {
1735     case GST_EVENT_SEEK:
1736     {
1737       gst_event_ref (event);
1738       res = gst_aggregator_do_seek (self, event);
1739       gst_event_unref (event);
1740       event = NULL;
1741       goto done;
1742     }
1743     case GST_EVENT_NAVIGATION:
1744     {
1745       /* navigation is rather pointless. */
1746       res = FALSE;
1747       gst_event_unref (event);
1748       goto done;
1749     }
1750     default:
1751     {
1752       break;
1753     }
1754   }
1755
1756   /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
1757    * they will receive a QOS event that has earliest_time=0 (because we can't
1758    * have negative timestamps), and consider their buffer as too late */
1759   evdata =
1760       gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE,
1761       GST_EVENT_TYPE (event) == GST_EVENT_QOS);
1762   res = evdata.result;
1763
1764 done:
1765   return res;
1766 }
1767
1768 static gboolean
1769 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1770     GstEvent * event)
1771 {
1772   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1773
1774   return klass->src_event (GST_AGGREGATOR (parent), event);
1775 }
1776
1777 static gboolean
1778 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1779     GstQuery * query)
1780 {
1781   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1782
1783   return klass->src_query (GST_AGGREGATOR (parent), query);
1784 }
1785
1786 static gboolean
1787 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1788     GstObject * parent, GstPadMode mode, gboolean active)
1789 {
1790   GstAggregator *self = GST_AGGREGATOR (parent);
1791   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1792
1793   if (klass->src_activate) {
1794     if (klass->src_activate (self, mode, active) == FALSE) {
1795       return FALSE;
1796     }
1797   }
1798
1799   if (active == TRUE) {
1800     switch (mode) {
1801       case GST_PAD_MODE_PUSH:
1802       {
1803         GST_INFO_OBJECT (pad, "Activating pad!");
1804         gst_aggregator_start_srcpad_task (self);
1805         return TRUE;
1806       }
1807       default:
1808       {
1809         GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1810         return FALSE;
1811       }
1812     }
1813   }
1814
1815   /* deactivating */
1816   GST_INFO_OBJECT (self, "Deactivating srcpad");
1817   gst_aggregator_stop_srcpad_task (self, FALSE);
1818
1819   return TRUE;
1820 }
1821
1822 static gboolean
1823 gst_aggregator_default_sink_query (GstAggregator * self,
1824     GstAggregatorPad * aggpad, GstQuery * query)
1825 {
1826   GstPad *pad = GST_PAD (aggpad);
1827
1828   return gst_pad_query_default (pad, GST_OBJECT (self), query);
1829 }
1830
1831 static void
1832 gst_aggregator_finalize (GObject * object)
1833 {
1834   GstAggregator *self = (GstAggregator *) object;
1835
1836   g_mutex_clear (&self->priv->src_lock);
1837   g_cond_clear (&self->priv->src_cond);
1838
1839   G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1840 }
1841
1842 /*
1843  * gst_aggregator_set_latency_property:
1844  * @agg: a #GstAggregator
1845  * @latency: the new latency value (in nanoseconds).
1846  *
1847  * Sets the new latency value to @latency. This value is used to limit the
1848  * amount of time a pad waits for data to appear before considering the pad
1849  * as unresponsive.
1850  */
1851 static void
1852 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1853 {
1854   gboolean changed;
1855
1856   g_return_if_fail (GST_IS_AGGREGATOR (self));
1857   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1858
1859   SRC_LOCK (self);
1860   changed = (self->priv->latency != latency);
1861
1862   if (changed) {
1863     GList *item;
1864
1865     GST_OBJECT_LOCK (self);
1866     /* First lock all the pads */
1867     for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1868       GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1869       PAD_LOCK (aggpad);
1870     }
1871
1872     self->priv->latency = latency;
1873
1874     SRC_BROADCAST (self);
1875
1876     /* Now wake up the pads */
1877     for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1878       GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1879       PAD_BROADCAST_EVENT (aggpad);
1880       PAD_UNLOCK (aggpad);
1881     }
1882     GST_OBJECT_UNLOCK (self);
1883   }
1884
1885   SRC_UNLOCK (self);
1886
1887   if (changed)
1888     gst_element_post_message (GST_ELEMENT_CAST (self),
1889         gst_message_new_latency (GST_OBJECT_CAST (self)));
1890 }
1891
1892 /*
1893  * gst_aggregator_get_latency_property:
1894  * @agg: a #GstAggregator
1895  *
1896  * Gets the latency value. See gst_aggregator_set_latency for
1897  * more details.
1898  *
1899  * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1900  * before a pad is deemed unresponsive. A value of -1 means an
1901  * unlimited time.
1902  */
1903 static gint64
1904 gst_aggregator_get_latency_property (GstAggregator * agg)
1905 {
1906   gint64 res;
1907
1908   g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1909
1910   GST_OBJECT_LOCK (agg);
1911   res = agg->priv->latency;
1912   GST_OBJECT_UNLOCK (agg);
1913
1914   return res;
1915 }
1916
1917 static void
1918 gst_aggregator_set_property (GObject * object, guint prop_id,
1919     const GValue * value, GParamSpec * pspec)
1920 {
1921   GstAggregator *agg = GST_AGGREGATOR (object);
1922
1923   switch (prop_id) {
1924     case PROP_LATENCY:
1925       gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1926       break;
1927     case PROP_START_TIME_SELECTION:
1928       agg->priv->start_time_selection = g_value_get_enum (value);
1929       break;
1930     case PROP_START_TIME:
1931       agg->priv->start_time = g_value_get_uint64 (value);
1932       break;
1933     default:
1934       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1935       break;
1936   }
1937 }
1938
1939 static void
1940 gst_aggregator_get_property (GObject * object, guint prop_id,
1941     GValue * value, GParamSpec * pspec)
1942 {
1943   GstAggregator *agg = GST_AGGREGATOR (object);
1944
1945   switch (prop_id) {
1946     case PROP_LATENCY:
1947       g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1948       break;
1949     case PROP_START_TIME_SELECTION:
1950       g_value_set_enum (value, agg->priv->start_time_selection);
1951       break;
1952     case PROP_START_TIME:
1953       g_value_set_uint64 (value, agg->priv->start_time);
1954       break;
1955     default:
1956       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1957       break;
1958   }
1959 }
1960
1961 /* GObject vmethods implementations */
1962 static void
1963 gst_aggregator_class_init (GstAggregatorClass * klass)
1964 {
1965   GObjectClass *gobject_class = (GObjectClass *) klass;
1966   GstElementClass *gstelement_class = (GstElementClass *) klass;
1967
1968   aggregator_parent_class = g_type_class_peek_parent (klass);
1969   g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1970
1971   GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1972       GST_DEBUG_FG_MAGENTA, "GstAggregator");
1973
1974   klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1975
1976   klass->sink_event = gst_aggregator_default_sink_event;
1977   klass->sink_query = gst_aggregator_default_sink_query;
1978
1979   klass->src_event = gst_aggregator_default_src_event;
1980   klass->src_query = gst_aggregator_default_src_query;
1981
1982   klass->create_new_pad = gst_aggregator_default_create_new_pad;
1983
1984   gstelement_class->request_new_pad =
1985       GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1986   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1987   gstelement_class->release_pad =
1988       GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1989   gstelement_class->change_state =
1990       GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1991
1992   gobject_class->set_property = gst_aggregator_set_property;
1993   gobject_class->get_property = gst_aggregator_get_property;
1994   gobject_class->finalize = gst_aggregator_finalize;
1995
1996   g_object_class_install_property (gobject_class, PROP_LATENCY,
1997       g_param_spec_int64 ("latency", "Buffer latency",
1998           "Additional latency in live mode to allow upstream "
1999           "to take longer to produce buffers for the current "
2000           "position (in nanoseconds)", 0,
2001           (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
2002           DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2003
2004   g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
2005       g_param_spec_enum ("start-time-selection", "Start Time Selection",
2006           "Decides which start time is output",
2007           gst_aggregator_start_time_selection_get_type (),
2008           DEFAULT_START_TIME_SELECTION,
2009           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2010
2011   g_object_class_install_property (gobject_class, PROP_START_TIME,
2012       g_param_spec_uint64 ("start-time", "Start Time",
2013           "Start time to use if start-time-selection=set", 0,
2014           G_MAXUINT64,
2015           DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2016
2017   GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
2018 }
2019
2020 static void
2021 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
2022 {
2023   GstPadTemplate *pad_template;
2024   GstAggregatorPrivate *priv;
2025
2026   g_return_if_fail (klass->aggregate != NULL);
2027
2028   self->priv =
2029       G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2030       GstAggregatorPrivate);
2031
2032   priv = self->priv;
2033
2034   pad_template =
2035       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2036   g_return_if_fail (pad_template != NULL);
2037
2038   priv->max_padserial = -1;
2039   priv->tags_changed = FALSE;
2040
2041   self->priv->peer_latency_live = FALSE;
2042   self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2043   self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2044   self->priv->has_peer_latency = FALSE;
2045   gst_aggregator_reset_flow_values (self);
2046
2047   self->srcpad = gst_pad_new_from_template (pad_template, "src");
2048
2049   gst_pad_set_event_function (self->srcpad,
2050       GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2051   gst_pad_set_query_function (self->srcpad,
2052       GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2053   gst_pad_set_activatemode_function (self->srcpad,
2054       GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2055
2056   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2057
2058   self->priv->latency = DEFAULT_LATENCY;
2059   self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2060   self->priv->start_time = DEFAULT_START_TIME;
2061
2062   g_mutex_init (&self->priv->src_lock);
2063   g_cond_init (&self->priv->src_cond);
2064 }
2065
2066 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2067  * method to get to the padtemplates */
2068 GType
2069 gst_aggregator_get_type (void)
2070 {
2071   static volatile gsize type = 0;
2072
2073   if (g_once_init_enter (&type)) {
2074     GType _type;
2075     static const GTypeInfo info = {
2076       sizeof (GstAggregatorClass),
2077       NULL,
2078       NULL,
2079       (GClassInitFunc) gst_aggregator_class_init,
2080       NULL,
2081       NULL,
2082       sizeof (GstAggregator),
2083       0,
2084       (GInstanceInitFunc) gst_aggregator_init,
2085     };
2086
2087     _type = g_type_register_static (GST_TYPE_ELEMENT,
2088         "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2089     g_once_init_leave (&type, _type);
2090   }
2091   return type;
2092 }
2093
2094 /* Must be called with SRC lock and PAD lock held */
2095 static gboolean
2096 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2097 {
2098   /* Empty queue always has space */
2099   if (aggpad->priv->num_buffers == 0 && aggpad->priv->clipped_buffer == NULL)
2100     return TRUE;
2101
2102   /* We also want at least two buffers, one is being processed and one is ready
2103    * for the next iteration when we operate in live mode. */
2104   if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2105     return TRUE;
2106
2107   /* zero latency, if there is a buffer, it's full */
2108   if (self->priv->latency == 0)
2109     return FALSE;
2110
2111   /* Allow no more buffers than the latency */
2112   return (aggpad->priv->time_level <= self->priv->latency);
2113 }
2114
2115 /* Must be called with the PAD_LOCK held */
2116 static void
2117 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2118 {
2119   GstClockTime timestamp;
2120
2121   if (GST_BUFFER_DTS_IS_VALID (buffer))
2122     timestamp = GST_BUFFER_DTS (buffer);
2123   else
2124     timestamp = GST_BUFFER_PTS (buffer);
2125
2126   if (timestamp == GST_CLOCK_TIME_NONE) {
2127     if (head)
2128       timestamp = aggpad->priv->head_position;
2129     else
2130       timestamp = aggpad->priv->tail_position;
2131   }
2132
2133   /* add duration */
2134   if (GST_BUFFER_DURATION_IS_VALID (buffer))
2135     timestamp += GST_BUFFER_DURATION (buffer);
2136
2137   if (head)
2138     aggpad->priv->head_position = timestamp;
2139   else
2140     aggpad->priv->tail_position = timestamp;
2141
2142   update_time_level (aggpad, head);
2143 }
2144
2145 static GstFlowReturn
2146 gst_aggregator_pad_chain_internal (GstAggregator * self,
2147     GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2148 {
2149   GstFlowReturn flow_return;
2150   GstClockTime buf_pts;
2151
2152   GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2153
2154   PAD_FLUSH_LOCK (aggpad);
2155
2156   PAD_LOCK (aggpad);
2157   flow_return = aggpad->priv->flow_return;
2158   if (flow_return != GST_FLOW_OK)
2159     goto flushing;
2160
2161   if (aggpad->priv->pending_eos == TRUE)
2162     goto eos;
2163
2164   PAD_UNLOCK (aggpad);
2165
2166   buf_pts = GST_BUFFER_PTS (buffer);
2167
2168   aggpad->priv->first_buffer = FALSE;
2169
2170   for (;;) {
2171     SRC_LOCK (self);
2172     GST_OBJECT_LOCK (self);
2173     PAD_LOCK (aggpad);
2174     if (gst_aggregator_pad_has_space (self, aggpad)
2175         && aggpad->priv->flow_return == GST_FLOW_OK) {
2176       if (head)
2177         g_queue_push_head (&aggpad->priv->buffers, buffer);
2178       else
2179         g_queue_push_tail (&aggpad->priv->buffers, buffer);
2180       apply_buffer (aggpad, buffer, head);
2181       aggpad->priv->num_buffers++;
2182       buffer = NULL;
2183       SRC_BROADCAST (self);
2184       break;
2185     }
2186
2187     flow_return = aggpad->priv->flow_return;
2188     if (flow_return != GST_FLOW_OK) {
2189       GST_OBJECT_UNLOCK (self);
2190       SRC_UNLOCK (self);
2191       goto flushing;
2192     }
2193     GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2194     GST_OBJECT_UNLOCK (self);
2195     SRC_UNLOCK (self);
2196     PAD_WAIT_EVENT (aggpad);
2197
2198     PAD_UNLOCK (aggpad);
2199   }
2200
2201   if (self->priv->first_buffer) {
2202     GstClockTime start_time;
2203
2204     switch (self->priv->start_time_selection) {
2205       case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2206       default:
2207         start_time = 0;
2208         break;
2209       case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2210         GST_OBJECT_LOCK (aggpad);
2211         if (aggpad->priv->head_segment.format == GST_FORMAT_TIME) {
2212           start_time = buf_pts;
2213           if (start_time != -1) {
2214             start_time = MAX (start_time, aggpad->priv->head_segment.start);
2215             start_time =
2216                 gst_segment_to_running_time (&aggpad->priv->head_segment,
2217                 GST_FORMAT_TIME, start_time);
2218           }
2219         } else {
2220           start_time = 0;
2221           GST_WARNING_OBJECT (aggpad,
2222               "Ignoring request of selecting the first start time "
2223               "as the segment is a %s segment instead of a time segment",
2224               gst_format_get_name (aggpad->segment.format));
2225         }
2226         GST_OBJECT_UNLOCK (aggpad);
2227         break;
2228       case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2229         start_time = self->priv->start_time;
2230         if (start_time == -1)
2231           start_time = 0;
2232         break;
2233     }
2234
2235     if (start_time != -1) {
2236       if (self->segment.position == -1)
2237         self->segment.position = start_time;
2238       else
2239         self->segment.position = MIN (start_time, self->segment.position);
2240
2241       GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2242           GST_TIME_ARGS (start_time));
2243     }
2244   }
2245
2246   PAD_UNLOCK (aggpad);
2247   GST_OBJECT_UNLOCK (self);
2248   SRC_UNLOCK (self);
2249
2250   PAD_FLUSH_UNLOCK (aggpad);
2251
2252   GST_DEBUG_OBJECT (aggpad, "Done chaining");
2253
2254   return flow_return;
2255
2256 flushing:
2257   PAD_UNLOCK (aggpad);
2258   PAD_FLUSH_UNLOCK (aggpad);
2259
2260   GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2261       gst_flow_get_name (flow_return));
2262   if (buffer)
2263     gst_buffer_unref (buffer);
2264
2265   return flow_return;
2266
2267 eos:
2268   PAD_UNLOCK (aggpad);
2269   PAD_FLUSH_UNLOCK (aggpad);
2270
2271   gst_buffer_unref (buffer);
2272   GST_DEBUG_OBJECT (aggpad, "We are EOS already...");
2273
2274   return GST_FLOW_EOS;
2275 }
2276
2277 static GstFlowReturn
2278 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2279 {
2280   return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2281       GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2282 }
2283
2284 static gboolean
2285 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2286     GstQuery * query)
2287 {
2288   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2289   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2290
2291   if (GST_QUERY_IS_SERIALIZED (query)) {
2292     PAD_LOCK (aggpad);
2293
2294     while (!gst_aggregator_pad_queue_is_empty (aggpad)
2295         && aggpad->priv->flow_return == GST_FLOW_OK) {
2296       GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2297       PAD_WAIT_EVENT (aggpad);
2298     }
2299
2300     if (aggpad->priv->flow_return != GST_FLOW_OK)
2301       goto flushing;
2302
2303     PAD_UNLOCK (aggpad);
2304   }
2305
2306   return klass->sink_query (GST_AGGREGATOR (parent),
2307       GST_AGGREGATOR_PAD (pad), query);
2308
2309 flushing:
2310   GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2311       gst_flow_get_name (aggpad->priv->flow_return));
2312   PAD_UNLOCK (aggpad);
2313   return FALSE;
2314 }
2315
2316 static GstFlowReturn
2317 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2318     GstEvent * event)
2319 {
2320   GstFlowReturn ret = GST_FLOW_OK;
2321   GstAggregator *self = GST_AGGREGATOR (parent);
2322   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2323   GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2324
2325   if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
2326       /* && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE */ ) {
2327     SRC_LOCK (self);
2328     PAD_LOCK (aggpad);
2329
2330     if (aggpad->priv->flow_return != GST_FLOW_OK
2331         && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2332       ret = aggpad->priv->flow_return;
2333       goto flushing;
2334     }
2335
2336     if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2337       GST_OBJECT_LOCK (aggpad);
2338       gst_event_copy_segment (event, &aggpad->priv->head_segment);
2339       aggpad->priv->head_position = aggpad->priv->head_segment.position;
2340       update_time_level (aggpad, TRUE);
2341       GST_OBJECT_UNLOCK (aggpad);
2342     }
2343
2344     if (!gst_aggregator_pad_queue_is_empty (aggpad) &&
2345         GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2346       GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT,
2347           event);
2348       g_queue_push_head (&aggpad->priv->buffers, event);
2349       event = NULL;
2350       SRC_BROADCAST (self);
2351     }
2352     PAD_UNLOCK (aggpad);
2353     SRC_UNLOCK (self);
2354   }
2355
2356   if (event) {
2357     gboolean is_caps = (GST_EVENT_TYPE (event) == GST_EVENT_CAPS);
2358
2359     if (!klass->sink_event (self, aggpad, event)) {
2360       /* Copied from GstPad to convert boolean to a GstFlowReturn in
2361        * the event handling func */
2362       ret = is_caps ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_ERROR;
2363     }
2364   }
2365
2366   return ret;
2367
2368 flushing:
2369   GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2370       gst_flow_get_name (aggpad->priv->flow_return));
2371   PAD_UNLOCK (aggpad);
2372   SRC_UNLOCK (self);
2373   if (GST_EVENT_IS_STICKY (event))
2374     gst_pad_store_sticky_event (pad, event);
2375   gst_event_unref (event);
2376
2377   return ret;
2378 }
2379
2380 static gboolean
2381 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2382     GstObject * parent, GstPadMode mode, gboolean active)
2383 {
2384   GstAggregator *self = GST_AGGREGATOR (parent);
2385   GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2386
2387   if (active == FALSE) {
2388     SRC_LOCK (self);
2389     gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2390     SRC_BROADCAST (self);
2391     SRC_UNLOCK (self);
2392   } else {
2393     PAD_LOCK (aggpad);
2394     aggpad->priv->flow_return = GST_FLOW_OK;
2395     PAD_BROADCAST_EVENT (aggpad);
2396     PAD_UNLOCK (aggpad);
2397   }
2398
2399   return TRUE;
2400 }
2401
2402 /***********************************
2403  * GstAggregatorPad implementation  *
2404  ************************************/
2405 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2406
2407 static void
2408 gst_aggregator_pad_constructed (GObject * object)
2409 {
2410   GstPad *pad = GST_PAD (object);
2411
2412   gst_pad_set_chain_function (pad,
2413       GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2414   gst_pad_set_event_full_function_full (pad,
2415       GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
2416   gst_pad_set_query_function (pad,
2417       GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2418   gst_pad_set_activatemode_function (pad,
2419       GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2420 }
2421
2422 static void
2423 gst_aggregator_pad_finalize (GObject * object)
2424 {
2425   GstAggregatorPad *pad = (GstAggregatorPad *) object;
2426
2427   g_cond_clear (&pad->priv->event_cond);
2428   g_mutex_clear (&pad->priv->flush_lock);
2429   g_mutex_clear (&pad->priv->lock);
2430
2431   G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2432 }
2433
2434 static void
2435 gst_aggregator_pad_dispose (GObject * object)
2436 {
2437   GstAggregatorPad *pad = (GstAggregatorPad *) object;
2438
2439   gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2440
2441   G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2442 }
2443
2444 static void
2445 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2446 {
2447   GObjectClass *gobject_class = (GObjectClass *) klass;
2448
2449   g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2450
2451   gobject_class->constructed = gst_aggregator_pad_constructed;
2452   gobject_class->finalize = gst_aggregator_pad_finalize;
2453   gobject_class->dispose = gst_aggregator_pad_dispose;
2454 }
2455
2456 static void
2457 gst_aggregator_pad_init (GstAggregatorPad * pad)
2458 {
2459   pad->priv =
2460       G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2461       GstAggregatorPadPrivate);
2462
2463   g_queue_init (&pad->priv->buffers);
2464   g_cond_init (&pad->priv->event_cond);
2465
2466   g_mutex_init (&pad->priv->flush_lock);
2467   g_mutex_init (&pad->priv->lock);
2468
2469   gst_aggregator_pad_reset_unlocked (pad);
2470 }
2471
2472 /* Must be called with the PAD_LOCK held */
2473 static void
2474 gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad)
2475 {
2476   pad->priv->num_buffers--;
2477   GST_TRACE_OBJECT (pad, "Consuming buffer");
2478   if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
2479     pad->priv->pending_eos = FALSE;
2480     pad->priv->eos = TRUE;
2481   }
2482   PAD_BROADCAST_EVENT (pad);
2483 }
2484
2485 /* Must be called with the PAD_LOCK held */
2486 static void
2487 gst_aggregator_pad_clip_buffer_unlocked (GstAggregatorPad * pad)
2488 {
2489   GstAggregator *self = NULL;
2490   GstAggregatorClass *aggclass;
2491   GstBuffer *buffer = NULL;
2492
2493   while (pad->priv->clipped_buffer == NULL &&
2494       GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->buffers))) {
2495     buffer = g_queue_pop_tail (&pad->priv->buffers);
2496
2497     apply_buffer (pad, buffer, FALSE);
2498
2499     /* We only take the parent here so that it's not taken if the buffer is
2500      * already clipped or if the queue is empty.
2501      */
2502     if (self == NULL) {
2503       self = GST_AGGREGATOR (gst_pad_get_parent_element (GST_PAD (pad)));
2504       if (self == NULL) {
2505         gst_buffer_unref (buffer);
2506         return;
2507       }
2508
2509       aggclass = GST_AGGREGATOR_GET_CLASS (self);
2510     }
2511
2512     if (aggclass->clip) {
2513       GST_TRACE_OBJECT (pad, "Clipping: %" GST_PTR_FORMAT, buffer);
2514
2515       buffer = aggclass->clip (self, pad, buffer);
2516
2517       if (buffer == NULL) {
2518         gst_aggregator_pad_buffer_consumed (pad);
2519         GST_TRACE_OBJECT (pad, "Clipping consumed the buffer");
2520       }
2521     }
2522
2523     pad->priv->clipped_buffer = buffer;
2524   }
2525
2526   if (self)
2527     gst_object_unref (self);
2528 }
2529
2530 /**
2531  * gst_aggregator_pad_steal_buffer:
2532  * @pad: the pad to get buffer from
2533  *
2534  * Steal the ref to the buffer currently queued in @pad.
2535  *
2536  * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2537  *   queued. You should unref the buffer after usage.
2538  */
2539 GstBuffer *
2540 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2541 {
2542   GstBuffer *buffer;
2543
2544   PAD_LOCK (pad);
2545
2546   gst_aggregator_pad_clip_buffer_unlocked (pad);
2547
2548   buffer = pad->priv->clipped_buffer;
2549   pad->priv->clipped_buffer = NULL;
2550
2551   if (buffer) {
2552     gst_aggregator_pad_buffer_consumed (pad);
2553     GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2554   }
2555
2556   PAD_UNLOCK (pad);
2557
2558   return buffer;
2559 }
2560
2561 /**
2562  * gst_aggregator_pad_drop_buffer:
2563  * @pad: the pad where to drop any pending buffer
2564  *
2565  * Drop the buffer currently queued in @pad.
2566  *
2567  * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2568  */
2569 gboolean
2570 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2571 {
2572   GstBuffer *buf;
2573
2574   buf = gst_aggregator_pad_steal_buffer (pad);
2575
2576   if (buf == NULL)
2577     return FALSE;
2578
2579   gst_buffer_unref (buf);
2580   return TRUE;
2581 }
2582
2583 /**
2584  * gst_aggregator_pad_get_buffer:
2585  * @pad: the pad to get buffer from
2586  *
2587  * Returns: (transfer full): A reference to the buffer in @pad or
2588  * NULL if no buffer was queued. You should unref the buffer after
2589  * usage.
2590  */
2591 GstBuffer *
2592 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2593 {
2594   GstBuffer *buffer;
2595
2596   PAD_LOCK (pad);
2597
2598   gst_aggregator_pad_clip_buffer_unlocked (pad);
2599
2600   if (pad->priv->clipped_buffer) {
2601     buffer = gst_buffer_ref (pad->priv->clipped_buffer);
2602   } else {
2603     buffer = NULL;
2604   }
2605   PAD_UNLOCK (pad);
2606
2607   return buffer;
2608 }
2609
2610 gboolean
2611 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2612 {
2613   gboolean is_eos;
2614
2615   PAD_LOCK (pad);
2616   is_eos = pad->priv->eos;
2617   PAD_UNLOCK (pad);
2618
2619   return is_eos;
2620 }
2621
2622 /**
2623  * gst_aggregator_merge_tags:
2624  * @self: a #GstAggregator
2625  * @tags: a #GstTagList to merge
2626  * @mode: the #GstTagMergeMode to use
2627  *
2628  * Adds tags to so-called pending tags, which will be processed
2629  * before pushing out data downstream.
2630  *
2631  * Note that this is provided for convenience, and the subclass is
2632  * not required to use this and can still do tag handling on its own.
2633  *
2634  * MT safe.
2635  */
2636 void
2637 gst_aggregator_merge_tags (GstAggregator * self,
2638     const GstTagList * tags, GstTagMergeMode mode)
2639 {
2640   GstTagList *otags;
2641
2642   g_return_if_fail (GST_IS_AGGREGATOR (self));
2643   g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2644
2645   /* FIXME Check if we can use OBJECT lock here! */
2646   GST_OBJECT_LOCK (self);
2647   if (tags)
2648     GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2649   otags = self->priv->tags;
2650   self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2651   if (otags)
2652     gst_tag_list_unref (otags);
2653   self->priv->tags_changed = TRUE;
2654   GST_OBJECT_UNLOCK (self);
2655 }
2656
2657 /**
2658  * gst_aggregator_set_latency:
2659  * @self: a #GstAggregator
2660  * @min_latency: minimum latency
2661  * @max_latency: maximum latency
2662  *
2663  * Lets #GstAggregator sub-classes tell the baseclass what their internal
2664  * latency is. Will also post a LATENCY message on the bus so the pipeline
2665  * can reconfigure its global latency.
2666  */
2667 void
2668 gst_aggregator_set_latency (GstAggregator * self,
2669     GstClockTime min_latency, GstClockTime max_latency)
2670 {
2671   gboolean changed = FALSE;
2672
2673   g_return_if_fail (GST_IS_AGGREGATOR (self));
2674   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2675   g_return_if_fail (max_latency >= min_latency);
2676
2677   SRC_LOCK (self);
2678   if (self->priv->sub_latency_min != min_latency) {
2679     self->priv->sub_latency_min = min_latency;
2680     changed = TRUE;
2681   }
2682   if (self->priv->sub_latency_max != max_latency) {
2683     self->priv->sub_latency_max = max_latency;
2684     changed = TRUE;
2685   }
2686
2687   if (changed)
2688     SRC_BROADCAST (self);
2689   SRC_UNLOCK (self);
2690
2691   if (changed) {
2692     gst_element_post_message (GST_ELEMENT_CAST (self),
2693         gst_message_new_latency (GST_OBJECT_CAST (self)));
2694   }
2695 }