1 /* GStreamer aggregator base class
2 * Copyright (C) 2014 Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
3 * Copyright (C) 2014 Thibault Saunier <tsaunier@gnome.org>
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.
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.
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.
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.
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.
32 * * Base class for mixers and muxers. Subclasses should at least implement
33 * the #GstAggregatorClass.aggregate() virtual method.
35 * * When data is queued on all pads, tha aggregate vmethod is called.
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
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
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
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.
64 #include <string.h> /* strlen */
66 #include "gstaggregator.h"
70 GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
71 GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
72 GST_AGGREGATOR_START_TIME_SELECTION_SET
73 } GstAggregatorStartTimeSelection;
76 gst_aggregator_start_time_selection_get_type (void)
78 static GType 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"},
91 gtype = g_enum_register_static ("GstAggregatorStartTimeSelection", values);
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,
101 static gint64 gst_aggregator_get_latency_property (GstAggregator * agg);
104 /* Locking order, locks in this element must always be taken in this order
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)
117 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
119 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
120 #define GST_CAT_DEFAULT aggregator_debug
122 /* GstAggregatorPad definitions */
123 #define PAD_LOCK(pad) G_STMT_START { \
124 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
126 g_mutex_lock(&pad->priv->lock); \
127 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
131 #define PAD_UNLOCK(pad) G_STMT_START { \
132 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
134 g_mutex_unlock(&pad->priv->lock); \
135 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
140 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
141 GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p", \
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", \
149 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
150 GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p", \
152 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
156 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
157 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
159 g_mutex_lock(&pad->priv->flush_lock); \
160 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
164 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
165 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
167 g_mutex_unlock(&pad->priv->flush_lock); \
168 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
172 #define SRC_LOCK(self) G_STMT_START { \
173 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
175 g_mutex_lock(&self->priv->src_lock); \
176 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
180 #define SRC_UNLOCK(self) G_STMT_START { \
181 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
183 g_mutex_unlock(&self->priv->src_lock); \
184 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
188 #define SRC_WAIT(self) G_STMT_START { \
189 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
191 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
192 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
196 #define SRC_BROADCAST(self) G_STMT_START { \
197 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
199 if (self->priv->aggregate_id) \
200 gst_clock_id_unschedule (self->priv->aggregate_id); \
201 g_cond_broadcast(&(self->priv->src_cond)); \
204 struct _GstAggregatorPadPrivate
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;
212 gboolean first_buffer;
215 GstBuffer *clipped_buffer;
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 */
230 /* This lock prevents a flush start processing happening while
231 * the chain function is also happening.
236 /* Must be called with PAD_LOCK held */
238 gst_aggregator_pad_reset_unlocked (GstAggregatorPad * aggpad)
240 aggpad->priv->pending_eos = FALSE;
241 aggpad->priv->eos = FALSE;
242 aggpad->priv->flow_return = GST_FLOW_OK;
243 GST_OBJECT_LOCK (aggpad);
244 gst_segment_init (&aggpad->segment, GST_FORMAT_UNDEFINED);
245 gst_segment_init (&aggpad->priv->head_segment, GST_FORMAT_UNDEFINED);
246 GST_OBJECT_UNLOCK (aggpad);
247 aggpad->priv->head_position = GST_CLOCK_TIME_NONE;
248 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
249 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
250 aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
251 aggpad->priv->time_level = 0;
252 aggpad->priv->first_buffer = TRUE;
256 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
258 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
261 gst_aggregator_pad_reset_unlocked (aggpad);
265 return klass->flush (aggpad, agg);
270 /*************************************
271 * GstAggregator implementation *
272 *************************************/
273 static GstElementClass *aggregator_parent_class = NULL;
275 /* All members are protected by the object lock unless otherwise noted */
277 struct _GstAggregatorPrivate
281 /* Our state is >= PAUSED */
282 gboolean running; /* protected by src_lock */
285 gboolean send_stream_start; /* protected by srcpad stream lock */
286 gboolean send_segment;
287 gboolean flush_seeking;
288 gboolean pending_flush_start;
289 gboolean send_eos; /* protected by srcpad stream lock */
291 GstCaps *srccaps; /* protected by the srcpad stream lock */
294 gboolean tags_changed;
296 gboolean peer_latency_live; /* protected by src_lock */
297 GstClockTime peer_latency_min; /* protected by src_lock */
298 GstClockTime peer_latency_max; /* protected by src_lock */
299 gboolean has_peer_latency; /* protected by src_lock */
301 GstClockTime sub_latency_min; /* protected by src_lock */
302 GstClockTime sub_latency_max; /* protected by src_lock */
305 GstClockID aggregate_id; /* protected by src_lock */
309 gboolean first_buffer; /* protected by object lock */
310 GstAggregatorStartTimeSelection start_time_selection;
311 GstClockTime start_time;
313 /* protected by the object lock */
314 GstQuery *allocation_query;
315 GstAllocator *allocator;
317 GstAllocationParams allocation_params;
320 gint64 latency; /* protected by both src_lock and all pad locks */
328 gboolean only_to_active_pads;
330 gboolean one_actually_seeked;
333 #define DEFAULT_LATENCY 0
334 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
335 #define DEFAULT_START_TIME (-1)
341 PROP_START_TIME_SELECTION,
346 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
347 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
350 * gst_aggregator_iterate_sinkpads:
351 * @self: The #GstAggregator
352 * @func: (scope call): The function to call.
353 * @user_data: (closure): The data to pass to @func.
355 * Iterate the sinkpads of aggregator to call a function on them.
357 * This method guarantees that @func will be called only once for each
361 gst_aggregator_iterate_sinkpads (GstAggregator * self,
362 GstAggregatorPadForeachFunc func, gpointer user_data)
364 gboolean result = FALSE;
366 gboolean done = FALSE;
367 GValue item = { 0, };
368 GList *seen_pads = NULL;
370 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
376 switch (gst_iterator_next (iter, &item)) {
377 case GST_ITERATOR_OK:
379 GstAggregatorPad *pad;
381 pad = g_value_get_object (&item);
383 /* if already pushed, skip. FIXME, find something faster to tag pads */
384 if (pad == NULL || g_list_find (seen_pads, pad)) {
385 g_value_reset (&item);
389 GST_LOG_OBJECT (pad, "calling function %s on pad",
390 GST_DEBUG_FUNCPTR_NAME (func));
392 result = func (self, pad, user_data);
396 seen_pads = g_list_prepend (seen_pads, pad);
398 g_value_reset (&item);
401 case GST_ITERATOR_RESYNC:
402 gst_iterator_resync (iter);
404 case GST_ITERATOR_ERROR:
405 GST_ERROR_OBJECT (self,
406 "Could not iterate over internally linked pads");
409 case GST_ITERATOR_DONE:
414 g_value_unset (&item);
415 gst_iterator_free (iter);
417 if (seen_pads == NULL) {
418 GST_DEBUG_OBJECT (self, "No pad seen");
422 g_list_free (seen_pads);
429 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
431 return (g_queue_peek_tail (&pad->priv->buffers) == NULL &&
432 pad->priv->clipped_buffer == NULL);
436 gst_aggregator_check_pads_ready (GstAggregator * self)
438 GstAggregatorPad *pad;
440 gboolean have_buffer = TRUE;
441 gboolean have_event = FALSE;
443 GST_LOG_OBJECT (self, "checking pads");
445 GST_OBJECT_LOCK (self);
447 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
448 if (sinkpads == NULL)
451 for (l = sinkpads; l != NULL; l = l->next) {
456 if (pad->priv->num_buffers == 0) {
457 if (!gst_aggregator_pad_queue_is_empty (pad))
459 if (!pad->priv->eos) {
462 /* If not live we need data on all pads, so leave the loop */
463 if (!self->priv->peer_latency_live) {
468 } else if (self->priv->peer_latency_live) {
469 /* In live mode, having a single pad with buffers is enough to
470 * generate a start time from it. In non-live mode all pads need
473 self->priv->first_buffer = FALSE;
479 if (!have_buffer && !have_event)
483 self->priv->first_buffer = FALSE;
485 GST_OBJECT_UNLOCK (self);
486 GST_LOG_OBJECT (self, "pads are ready");
491 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
492 GST_OBJECT_UNLOCK (self);
498 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet,"
499 " but waking up for serialized event");
501 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
502 GST_OBJECT_UNLOCK (self);
508 gst_aggregator_reset_flow_values (GstAggregator * self)
510 GST_OBJECT_LOCK (self);
511 self->priv->send_stream_start = TRUE;
512 self->priv->send_segment = TRUE;
513 gst_segment_init (&self->segment, GST_FORMAT_TIME);
514 self->priv->first_buffer = TRUE;
515 GST_OBJECT_UNLOCK (self);
519 gst_aggregator_push_mandatory_events (GstAggregator * self)
521 GstAggregatorPrivate *priv = self->priv;
522 GstEvent *segment = NULL;
523 GstEvent *tags = NULL;
525 if (self->priv->send_stream_start) {
528 GST_INFO_OBJECT (self, "pushing stream start");
529 /* stream-start (FIXME: create id based on input ids) */
530 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
531 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
532 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
534 self->priv->send_stream_start = FALSE;
537 if (self->priv->srccaps) {
539 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
540 self->priv->srccaps);
541 if (!gst_pad_push_event (self->srcpad,
542 gst_event_new_caps (self->priv->srccaps))) {
543 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
545 gst_caps_unref (self->priv->srccaps);
546 self->priv->srccaps = NULL;
549 GST_OBJECT_LOCK (self);
550 if (self->priv->send_segment && !self->priv->flush_seeking) {
551 segment = gst_event_new_segment (&self->segment);
553 if (!self->priv->seqnum)
554 self->priv->seqnum = gst_event_get_seqnum (segment);
556 gst_event_set_seqnum (segment, self->priv->seqnum);
557 self->priv->send_segment = FALSE;
559 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
562 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
563 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
564 priv->tags_changed = FALSE;
566 GST_OBJECT_UNLOCK (self);
569 gst_pad_push_event (self->srcpad, segment);
571 gst_pad_push_event (self->srcpad, tags);
576 * gst_aggregator_set_src_caps:
577 * @self: The #GstAggregator
578 * @caps: The #GstCaps to set on the src pad.
580 * Sets the caps to be used on the src pad.
583 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
585 GST_PAD_STREAM_LOCK (self->srcpad);
586 gst_caps_replace (&self->priv->srccaps, caps);
587 gst_aggregator_push_mandatory_events (self);
588 GST_PAD_STREAM_UNLOCK (self->srcpad);
592 * gst_aggregator_finish_buffer:
593 * @self: The #GstAggregator
594 * @buffer: (transfer full): the #GstBuffer to push.
596 * This method will push the provided output buffer downstream. If needed,
597 * mandatory events such as stream-start, caps, and segment events will be
598 * sent before pushing the buffer.
601 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
603 gst_aggregator_push_mandatory_events (self);
605 GST_OBJECT_LOCK (self);
606 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
607 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
608 GST_OBJECT_UNLOCK (self);
609 return gst_pad_push (self->srcpad, buffer);
611 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
612 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
613 GST_OBJECT_UNLOCK (self);
614 gst_buffer_unref (buffer);
620 gst_aggregator_push_eos (GstAggregator * self)
623 gst_aggregator_push_mandatory_events (self);
625 event = gst_event_new_eos ();
627 GST_OBJECT_LOCK (self);
628 self->priv->send_eos = FALSE;
629 gst_event_set_seqnum (event, self->priv->seqnum);
630 GST_OBJECT_UNLOCK (self);
632 gst_pad_push_event (self->srcpad, event);
636 gst_aggregator_get_next_time (GstAggregator * self)
638 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
640 if (klass->get_next_time)
641 return klass->get_next_time (self);
643 return GST_CLOCK_TIME_NONE;
647 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
649 GstClockTime latency;
657 latency = gst_aggregator_get_latency_unlocked (self);
659 if (gst_aggregator_check_pads_ready (self)) {
660 GST_DEBUG_OBJECT (self, "all pads have data");
666 /* Before waiting, check if we're actually still running */
667 if (!self->priv->running || !self->priv->send_eos) {
673 start = gst_aggregator_get_next_time (self);
675 /* If we're not live, or if we use the running time
676 * of the first buffer as start time, we wait until
677 * all pads have buffers.
678 * Otherwise (i.e. if we are live!), we wait on the clock
679 * and if a pad does not have a buffer in time we ignore
682 GST_OBJECT_LOCK (self);
683 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
684 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
685 !GST_CLOCK_TIME_IS_VALID (start) ||
686 (self->priv->first_buffer
687 && self->priv->start_time_selection ==
688 GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
689 /* We wake up here when something happened, and below
690 * then check if we're ready now. If we return FALSE,
691 * we will be directly called again.
693 GST_OBJECT_UNLOCK (self);
696 GstClockTime base_time, time;
698 GstClockReturn status;
699 GstClockTimeDiff jitter;
701 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
702 GST_TIME_ARGS (start));
704 base_time = GST_ELEMENT_CAST (self)->base_time;
705 clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
706 GST_OBJECT_UNLOCK (self);
708 time = base_time + start;
711 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
712 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
713 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
714 GST_TIME_ARGS (time),
715 GST_TIME_ARGS (base_time),
716 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
717 GST_TIME_ARGS (gst_clock_get_time (clock)));
719 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
720 gst_object_unref (clock);
724 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
727 if (self->priv->aggregate_id) {
728 gst_clock_id_unref (self->priv->aggregate_id);
729 self->priv->aggregate_id = NULL;
732 GST_DEBUG_OBJECT (self,
733 "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
734 status, GST_STIME_ARGS (jitter));
737 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
744 res = gst_aggregator_check_pads_ready (self);
751 check_events (GstAggregator * self, GstAggregatorPad * pad, gpointer user_data)
753 GstEvent *event = NULL;
754 GstAggregatorClass *klass = NULL;
755 gboolean *processed_event = user_data;
761 if (pad->priv->num_buffers == 0 && pad->priv->pending_eos) {
762 pad->priv->pending_eos = FALSE;
763 pad->priv->eos = TRUE;
765 if (pad->priv->clipped_buffer == NULL &&
766 GST_IS_EVENT (g_queue_peek_tail (&pad->priv->buffers))) {
767 event = gst_event_ref (g_queue_peek_tail (&pad->priv->buffers));
774 *processed_event = TRUE;
776 klass = GST_AGGREGATOR_GET_CLASS (self);
778 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
779 gst_event_ref (event);
780 ret = klass->sink_event (self, pad, event);
783 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS)
784 pad->priv->negotiated = ret;
785 if (g_queue_peek_tail (&pad->priv->buffers) == event)
786 gst_event_unref (g_queue_pop_tail (&pad->priv->buffers));
787 gst_event_unref (event);
788 PAD_BROADCAST_EVENT (pad);
791 } while (event != NULL);
797 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
798 GstFlowReturn flow_return, gboolean full)
803 if (flow_return == GST_FLOW_NOT_LINKED)
804 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
806 aggpad->priv->flow_return = flow_return;
808 item = g_queue_peek_head_link (&aggpad->priv->buffers);
810 GList *next = item->next;
812 /* In partial flush, we do like the pad, we get rid of non-sticky events
815 if (full || GST_IS_BUFFER (item->data) ||
816 GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
817 GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
818 !GST_EVENT_IS_STICKY (item->data)) {
819 gst_mini_object_unref (item->data);
820 g_queue_delete_link (&aggpad->priv->buffers, item);
824 aggpad->priv->num_buffers = 0;
825 gst_buffer_replace (&aggpad->priv->clipped_buffer, NULL);
827 PAD_BROADCAST_EVENT (aggpad);
832 gst_aggregator_default_update_src_caps (GstAggregator * agg, GstCaps * caps,
835 *ret = gst_caps_ref (caps);
841 gst_aggregator_default_fixate_src_caps (GstAggregator * agg, GstCaps * caps)
843 caps = gst_caps_fixate (caps);
849 gst_aggregator_default_negotiated_src_caps (GstAggregator * agg, GstCaps * caps)
855 /* takes ownership of the pool, allocator and query */
857 gst_aggregator_set_allocation (GstAggregator * self,
858 GstBufferPool * pool, GstAllocator * allocator,
859 GstAllocationParams * params, GstQuery * query)
861 GstAllocator *oldalloc;
862 GstBufferPool *oldpool;
865 GST_DEBUG ("storing allocation query");
867 GST_OBJECT_LOCK (self);
868 oldpool = self->priv->pool;
869 self->priv->pool = pool;
871 oldalloc = self->priv->allocator;
872 self->priv->allocator = allocator;
874 oldquery = self->priv->allocation_query;
875 self->priv->allocation_query = query;
878 self->priv->allocation_params = *params;
880 gst_allocation_params_init (&self->priv->allocation_params);
881 GST_OBJECT_UNLOCK (self);
884 GST_DEBUG_OBJECT (self, "deactivating old pool %p", oldpool);
885 gst_buffer_pool_set_active (oldpool, FALSE);
886 gst_object_unref (oldpool);
889 gst_object_unref (oldalloc);
892 gst_query_unref (oldquery);
899 gst_aggregator_decide_allocation (GstAggregator * self, GstQuery * query)
901 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
903 if (aggclass->decide_allocation)
904 if (!aggclass->decide_allocation (self, query))
911 gst_aggregator_do_allocation (GstAggregator * self, GstCaps * caps)
914 gboolean result = TRUE;
915 GstBufferPool *pool = NULL;
916 GstAllocator *allocator;
917 GstAllocationParams params;
919 /* find a pool for the negotiated caps now */
920 GST_DEBUG_OBJECT (self, "doing allocation query");
921 query = gst_query_new_allocation (caps, TRUE);
922 if (!gst_pad_peer_query (self->srcpad, query)) {
923 /* not a problem, just debug a little */
924 GST_DEBUG_OBJECT (self, "peer ALLOCATION query failed");
927 GST_DEBUG_OBJECT (self, "calling decide_allocation");
928 result = gst_aggregator_decide_allocation (self, query);
930 GST_DEBUG_OBJECT (self, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
934 goto no_decide_allocation;
936 /* we got configuration from our peer or the decide_allocation method,
938 if (gst_query_get_n_allocation_params (query) > 0) {
939 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
942 gst_allocation_params_init (¶ms);
945 if (gst_query_get_n_allocation_pools (query) > 0)
946 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
950 gst_aggregator_set_allocation (self, pool, allocator, ¶ms, query);
955 no_decide_allocation:
957 GST_WARNING_OBJECT (self, "Failed to decide allocation");
958 gst_query_unref (query);
965 /* WITH SRC_LOCK held */
967 gst_aggregator_update_src_caps (GstAggregator * self)
969 GstAggregatorClass *agg_klass = GST_AGGREGATOR_GET_CLASS (self);
970 GstCaps *downstream_caps, *template_caps, *caps = NULL;
971 GstFlowReturn ret = GST_FLOW_OK;
973 template_caps = gst_pad_get_pad_template_caps (self->srcpad);
974 downstream_caps = gst_pad_peer_query_caps (self->srcpad, template_caps);
976 if (gst_caps_is_empty (downstream_caps)) {
977 GST_INFO_OBJECT (self, "Downstream caps (%"
978 GST_PTR_FORMAT ") not compatible with pad template caps (%"
979 GST_PTR_FORMAT ")", downstream_caps, template_caps);
980 ret = GST_FLOW_NOT_NEGOTIATED;
984 g_assert (agg_klass->update_src_caps);
985 GST_DEBUG_OBJECT (self, "updating caps from %" GST_PTR_FORMAT,
987 ret = agg_klass->update_src_caps (self, downstream_caps, &caps);
988 if (ret < GST_FLOW_OK) {
989 GST_WARNING_OBJECT (self, "Subclass failed to update provided caps");
992 if ((caps == NULL || gst_caps_is_empty (caps)) && ret >= GST_FLOW_OK) {
993 ret = GST_FLOW_NOT_NEGOTIATED;
996 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
998 #ifdef GST_ENABLE_EXTRA_CHECKS
999 if (!gst_caps_is_subset (caps, template_caps)) {
1000 GstCaps *intersection;
1002 GST_ERROR_OBJECT (self,
1003 "update_src_caps returned caps %" GST_PTR_FORMAT
1004 " which are not a real subset of the template caps %"
1005 GST_PTR_FORMAT, caps, template_caps);
1006 g_warning ("%s: update_src_caps returned caps which are not a real "
1007 "subset of the filter caps", GST_ELEMENT_NAME (self));
1010 gst_caps_intersect_full (template_caps, caps, GST_CAPS_INTERSECT_FIRST);
1011 gst_caps_unref (caps);
1012 caps = intersection;
1016 if (gst_caps_is_any (caps)) {
1020 if (!gst_caps_is_fixed (caps)) {
1021 g_assert (agg_klass->fixate_src_caps);
1023 GST_DEBUG_OBJECT (self, "fixate caps from %" GST_PTR_FORMAT, caps);
1024 if (!(caps = agg_klass->fixate_src_caps (self, caps))) {
1025 GST_WARNING_OBJECT (self, "Subclass failed to fixate provided caps");
1026 ret = GST_FLOW_NOT_NEGOTIATED;
1029 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1032 if (agg_klass->negotiated_src_caps) {
1033 if (!agg_klass->negotiated_src_caps (self, caps)) {
1034 GST_WARNING_OBJECT (self, "Subclass failed to accept negotiated caps");
1035 ret = GST_FLOW_NOT_NEGOTIATED;
1040 gst_aggregator_set_src_caps (self, caps);
1042 if (!gst_aggregator_do_allocation (self, caps)) {
1043 GST_WARNING_OBJECT (self, "Allocation negotiation failed");
1044 ret = GST_FLOW_NOT_NEGOTIATED;
1048 gst_caps_unref (downstream_caps);
1049 gst_caps_unref (template_caps);
1052 gst_caps_unref (caps);
1058 gst_aggregator_aggregate_func (GstAggregator * self)
1060 GstAggregatorPrivate *priv = self->priv;
1061 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1062 gboolean timeout = FALSE;
1064 if (self->priv->running == FALSE) {
1065 GST_DEBUG_OBJECT (self, "Not running anymore");
1069 GST_LOG_OBJECT (self, "Checking aggregate");
1070 while (priv->send_eos && priv->running) {
1071 GstFlowReturn flow_return = GST_FLOW_OK;
1072 gboolean processed_event = FALSE;
1074 gst_aggregator_iterate_sinkpads (self, check_events, NULL);
1076 if (!gst_aggregator_wait_and_check (self, &timeout))
1079 gst_aggregator_iterate_sinkpads (self, check_events, &processed_event);
1080 if (processed_event)
1083 if (gst_pad_check_reconfigure (GST_AGGREGATOR_SRC_PAD (self))) {
1084 flow_return = gst_aggregator_update_src_caps (self);
1085 if (flow_return != GST_FLOW_OK)
1086 gst_pad_mark_reconfigure (GST_AGGREGATOR_SRC_PAD (self));
1089 if (timeout || flow_return >= GST_FLOW_OK) {
1090 GST_TRACE_OBJECT (self, "Actually aggregating!");
1091 flow_return = klass->aggregate (self, timeout);
1094 if (flow_return == GST_AGGREGATOR_FLOW_NEED_DATA)
1097 GST_OBJECT_LOCK (self);
1098 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
1099 /* We don't want to set the pads to flushing, but we want to
1100 * stop the thread, so just break here */
1101 GST_OBJECT_UNLOCK (self);
1104 GST_OBJECT_UNLOCK (self);
1106 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
1107 gst_aggregator_push_eos (self);
1110 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
1112 if (flow_return != GST_FLOW_OK) {
1115 GST_OBJECT_LOCK (self);
1116 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
1117 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1119 gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
1121 GST_OBJECT_UNLOCK (self);
1126 /* Pause the task here, the only ways to get here are:
1127 * 1) We're stopping, in which case the task is stopped anyway
1128 * 2) We got a flow error above, in which case it might take
1129 * some time to forward the flow return upstream and we
1130 * would otherwise call the task function over and over
1131 * again without doing anything
1133 gst_pad_pause_task (self->srcpad);
1137 gst_aggregator_start (GstAggregator * self)
1139 GstAggregatorClass *klass;
1142 self->priv->send_stream_start = TRUE;
1143 self->priv->send_segment = TRUE;
1144 self->priv->send_eos = TRUE;
1145 self->priv->srccaps = NULL;
1147 gst_aggregator_set_allocation (self, NULL, NULL, NULL, NULL);
1149 klass = GST_AGGREGATOR_GET_CLASS (self);
1152 result = klass->start (self);
1160 _check_pending_flush_stop (GstAggregatorPad * pad)
1165 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
1172 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
1174 gboolean res = TRUE;
1176 GST_INFO_OBJECT (self, "%s srcpad task",
1177 flush_start ? "Pausing" : "Stopping");
1180 self->priv->running = FALSE;
1181 SRC_BROADCAST (self);
1185 res = gst_pad_push_event (self->srcpad, flush_start);
1188 gst_pad_stop_task (self->srcpad);
1194 gst_aggregator_start_srcpad_task (GstAggregator * self)
1196 GST_INFO_OBJECT (self, "Starting srcpad task");
1198 self->priv->running = TRUE;
1199 gst_pad_start_task (GST_PAD (self->srcpad),
1200 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
1203 static GstFlowReturn
1204 gst_aggregator_flush (GstAggregator * self)
1206 GstFlowReturn ret = GST_FLOW_OK;
1207 GstAggregatorPrivate *priv = self->priv;
1208 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1210 GST_DEBUG_OBJECT (self, "Flushing everything");
1211 GST_OBJECT_LOCK (self);
1212 priv->send_segment = TRUE;
1213 priv->flush_seeking = FALSE;
1214 priv->tags_changed = FALSE;
1215 GST_OBJECT_UNLOCK (self);
1217 ret = klass->flush (self);
1223 /* Called with GstAggregator's object lock held */
1226 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
1229 GstAggregatorPad *tmppad;
1231 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
1232 tmppad = (GstAggregatorPad *) tmp->data;
1234 if (_check_pending_flush_stop (tmppad) == FALSE) {
1235 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
1236 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
1245 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
1248 GstAggregatorPrivate *priv = self->priv;
1249 GstAggregatorPadPrivate *padpriv = aggpad->priv;
1251 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
1253 PAD_FLUSH_LOCK (aggpad);
1255 if (padpriv->pending_flush_start) {
1256 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
1258 padpriv->pending_flush_start = FALSE;
1259 padpriv->pending_flush_stop = TRUE;
1261 PAD_UNLOCK (aggpad);
1263 GST_OBJECT_LOCK (self);
1264 if (priv->flush_seeking) {
1265 /* If flush_seeking we forward the first FLUSH_START */
1266 if (priv->pending_flush_start) {
1267 priv->pending_flush_start = FALSE;
1268 GST_OBJECT_UNLOCK (self);
1270 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
1271 gst_aggregator_stop_srcpad_task (self, event);
1273 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
1274 GST_PAD_STREAM_LOCK (self->srcpad);
1275 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
1278 GST_OBJECT_UNLOCK (self);
1279 gst_event_unref (event);
1282 GST_OBJECT_UNLOCK (self);
1283 gst_event_unref (event);
1285 PAD_FLUSH_UNLOCK (aggpad);
1288 /* Must be called with the the PAD_LOCK held */
1290 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1293 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->head_position) &&
1294 aggpad->priv->head_segment.format == GST_FORMAT_TIME)
1295 aggpad->priv->head_time =
1296 gst_segment_to_running_time (&aggpad->priv->head_segment,
1297 GST_FORMAT_TIME, aggpad->priv->head_position);
1299 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
1301 if (!GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_time))
1302 aggpad->priv->tail_time = aggpad->priv->head_time;
1304 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_position) &&
1305 aggpad->segment.format == GST_FORMAT_TIME)
1306 aggpad->priv->tail_time =
1307 gst_segment_to_running_time (&aggpad->segment,
1308 GST_FORMAT_TIME, aggpad->priv->tail_position);
1310 aggpad->priv->tail_time = aggpad->priv->head_time;
1313 if (aggpad->priv->head_time == GST_CLOCK_TIME_NONE ||
1314 aggpad->priv->tail_time == GST_CLOCK_TIME_NONE) {
1315 aggpad->priv->time_level = 0;
1319 if (aggpad->priv->tail_time > aggpad->priv->head_time)
1320 aggpad->priv->time_level = 0;
1322 aggpad->priv->time_level = aggpad->priv->head_time -
1323 aggpad->priv->tail_time;
1327 /* GstAggregator vmethods default implementations */
1329 gst_aggregator_default_sink_event (GstAggregator * self,
1330 GstAggregatorPad * aggpad, GstEvent * event)
1332 gboolean res = TRUE;
1333 GstPad *pad = GST_PAD (aggpad);
1334 GstAggregatorPrivate *priv = self->priv;
1336 switch (GST_EVENT_TYPE (event)) {
1337 case GST_EVENT_FLUSH_START:
1339 gst_aggregator_flush_start (self, aggpad, event);
1340 /* We forward only in one case: right after flush_seeking */
1344 case GST_EVENT_FLUSH_STOP:
1346 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
1348 gst_aggregator_pad_flush (aggpad, self);
1349 GST_OBJECT_LOCK (self);
1350 if (priv->flush_seeking) {
1351 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1352 if (gst_aggregator_all_flush_stop_received_locked (self)) {
1353 GST_OBJECT_UNLOCK (self);
1354 /* That means we received FLUSH_STOP/FLUSH_STOP on
1355 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1356 gst_aggregator_flush (self);
1357 gst_pad_push_event (self->srcpad, event);
1360 priv->send_eos = TRUE;
1361 SRC_BROADCAST (self);
1364 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1365 GST_PAD_STREAM_UNLOCK (self->srcpad);
1366 gst_aggregator_start_srcpad_task (self);
1368 GST_OBJECT_UNLOCK (self);
1371 GST_OBJECT_UNLOCK (self);
1374 /* We never forward the event */
1379 GST_DEBUG_OBJECT (aggpad, "EOS");
1381 /* We still have a buffer, and we don't want the subclass to have to
1382 * check for it. Mark pending_eos, eos will be set when steal_buffer is
1387 if (aggpad->priv->num_buffers == 0) {
1388 aggpad->priv->eos = TRUE;
1390 aggpad->priv->pending_eos = TRUE;
1392 PAD_UNLOCK (aggpad);
1394 SRC_BROADCAST (self);
1398 case GST_EVENT_SEGMENT:
1401 GST_OBJECT_LOCK (aggpad);
1402 gst_event_copy_segment (event, &aggpad->segment);
1403 update_time_level (aggpad, FALSE);
1404 GST_OBJECT_UNLOCK (aggpad);
1405 PAD_UNLOCK (aggpad);
1407 GST_OBJECT_LOCK (self);
1408 self->priv->seqnum = gst_event_get_seqnum (event);
1409 GST_OBJECT_UNLOCK (self);
1412 case GST_EVENT_STREAM_START:
1418 GstClockTime pts, endpts;
1419 GstClockTime duration;
1422 gst_event_parse_gap (event, &pts, &duration);
1423 gapbuf = gst_buffer_new ();
1425 if (GST_CLOCK_TIME_IS_VALID (duration))
1426 endpts = pts + duration;
1428 endpts = GST_CLOCK_TIME_NONE;
1430 GST_OBJECT_LOCK (aggpad);
1431 res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1433 GST_OBJECT_UNLOCK (aggpad);
1436 GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1440 if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1441 duration = endpts - pts;
1443 duration = GST_CLOCK_TIME_NONE;
1445 GST_BUFFER_PTS (gapbuf) = pts;
1446 GST_BUFFER_DURATION (gapbuf) = duration;
1447 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1448 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1450 if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1452 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1462 gst_event_parse_tag (event, &tags);
1464 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1465 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1466 gst_event_unref (event);
1478 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1479 return gst_pad_event_default (pad, GST_OBJECT (self), event);
1482 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1484 gst_event_unref (event);
1489 static inline gboolean
1490 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1491 gpointer unused_udata)
1493 gst_aggregator_pad_flush (pad, self);
1496 pad->priv->negotiated = FALSE;
1503 gst_aggregator_stop (GstAggregator * agg)
1505 GstAggregatorClass *klass;
1508 gst_aggregator_reset_flow_values (agg);
1510 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1512 klass = GST_AGGREGATOR_GET_CLASS (agg);
1515 result = klass->stop (agg);
1519 agg->priv->has_peer_latency = FALSE;
1520 agg->priv->peer_latency_live = FALSE;
1521 agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1523 if (agg->priv->tags)
1524 gst_tag_list_unref (agg->priv->tags);
1525 agg->priv->tags = NULL;
1527 gst_aggregator_set_allocation (agg, NULL, NULL, NULL, NULL);
1532 /* GstElement vmethods implementations */
1533 static GstStateChangeReturn
1534 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1536 GstStateChangeReturn ret;
1537 GstAggregator *self = GST_AGGREGATOR (element);
1539 switch (transition) {
1540 case GST_STATE_CHANGE_READY_TO_PAUSED:
1541 if (!gst_aggregator_start (self))
1549 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1550 transition)) == GST_STATE_CHANGE_FAILURE)
1554 switch (transition) {
1555 case GST_STATE_CHANGE_PAUSED_TO_READY:
1556 if (!gst_aggregator_stop (self)) {
1557 /* What to do in this case? Error out? */
1558 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1570 GST_ERROR_OBJECT (element, "parent failed state change");
1575 GST_ERROR_OBJECT (element, "Subclass failed to start");
1576 return GST_STATE_CHANGE_FAILURE;
1581 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1583 GstAggregator *self = GST_AGGREGATOR (element);
1584 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1586 GST_INFO_OBJECT (pad, "Removing pad");
1589 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1590 gst_element_remove_pad (element, pad);
1592 self->priv->has_peer_latency = FALSE;
1593 SRC_BROADCAST (self);
1597 static GstAggregatorPad *
1598 gst_aggregator_default_create_new_pad (GstAggregator * self,
1599 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1601 GstAggregatorPad *agg_pad;
1602 GstAggregatorPrivate *priv = self->priv;
1606 if (templ->direction != GST_PAD_SINK)
1609 GST_OBJECT_LOCK (self);
1610 if (req_name == NULL || strlen (req_name) < 6
1611 || !g_str_has_prefix (req_name, "sink_")) {
1612 /* no name given when requesting the pad, use next available int */
1613 serial = ++priv->max_padserial;
1615 /* parse serial number from requested padname */
1616 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1617 if (serial > priv->max_padserial)
1618 priv->max_padserial = serial;
1621 name = g_strdup_printf ("sink_%u", serial);
1622 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1623 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1626 GST_OBJECT_UNLOCK (self);
1633 GST_WARNING_OBJECT (self, "request new pad that is not a SINK pad\n");
1639 gst_aggregator_request_new_pad (GstElement * element,
1640 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1642 GstAggregator *self;
1643 GstAggregatorPad *agg_pad;
1644 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1645 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1647 self = GST_AGGREGATOR (element);
1649 agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1651 GST_ERROR_OBJECT (element, "Couldn't create new pad");
1655 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1658 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1660 /* add the pad to the element */
1661 gst_element_add_pad (element, GST_PAD (agg_pad));
1663 return GST_PAD (agg_pad);
1666 /* Must be called with SRC_LOCK held */
1669 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1671 gboolean query_ret, live;
1672 GstClockTime our_latency, min, max;
1674 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1677 GST_WARNING_OBJECT (self, "Latency query failed");
1681 gst_query_parse_latency (query, &live, &min, &max);
1683 our_latency = self->priv->latency;
1685 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1686 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1687 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1691 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1692 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1693 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1694 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1695 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1699 self->priv->peer_latency_live = live;
1700 self->priv->peer_latency_min = min;
1701 self->priv->peer_latency_max = max;
1702 self->priv->has_peer_latency = TRUE;
1706 min += self->priv->sub_latency_min;
1707 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1708 && GST_CLOCK_TIME_IS_VALID (max))
1709 max += self->priv->sub_latency_max + our_latency;
1711 max = GST_CLOCK_TIME_NONE;
1713 SRC_BROADCAST (self);
1715 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1716 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1718 gst_query_set_latency (query, live, min, max);
1724 * MUST be called with the src_lock held.
1726 * See gst_aggregator_get_latency() for doc
1729 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1731 GstClockTime latency;
1733 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1735 if (!self->priv->has_peer_latency) {
1736 GstQuery *query = gst_query_new_latency ();
1739 ret = gst_aggregator_query_latency_unlocked (self, query);
1740 gst_query_unref (query);
1742 return GST_CLOCK_TIME_NONE;
1745 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1746 return GST_CLOCK_TIME_NONE;
1748 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1749 latency = self->priv->peer_latency_min;
1752 latency += self->priv->latency;
1753 latency += self->priv->sub_latency_min;
1759 * gst_aggregator_get_latency:
1760 * @self: a #GstAggregator
1762 * Retrieves the latency values reported by @self in response to the latency
1763 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1764 * will not wait for the clock.
1766 * Typically only called by subclasses.
1768 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1771 gst_aggregator_get_latency (GstAggregator * self)
1776 ret = gst_aggregator_get_latency_unlocked (self);
1783 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1785 GstAggregator *self = GST_AGGREGATOR (element);
1787 GST_STATE_LOCK (element);
1788 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1789 GST_STATE (element) < GST_STATE_PAUSED) {
1793 GstSeekType start_type, stop_type;
1796 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1797 &start, &stop_type, &stop);
1799 GST_OBJECT_LOCK (self);
1800 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1801 stop_type, stop, NULL);
1802 self->priv->seqnum = gst_event_get_seqnum (event);
1803 self->priv->first_buffer = FALSE;
1804 GST_OBJECT_UNLOCK (self);
1806 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1808 GST_STATE_UNLOCK (element);
1811 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1816 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1818 gboolean res = TRUE;
1820 switch (GST_QUERY_TYPE (query)) {
1821 case GST_QUERY_SEEKING:
1825 /* don't pass it along as some (file)sink might claim it does
1826 * whereas with a collectpads in between that will not likely work */
1827 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1828 gst_query_set_seeking (query, format, FALSE, 0, -1);
1833 case GST_QUERY_LATENCY:
1835 res = gst_aggregator_query_latency_unlocked (self, query);
1839 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1846 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1848 EventData *evdata = user_data;
1849 gboolean ret = TRUE;
1850 GstPad *peer = gst_pad_get_peer (pad);
1851 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1854 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1855 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1858 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1859 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1860 gst_object_unref (peer);
1865 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1866 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1868 GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1870 if (gst_pad_query (peer, seeking)) {
1873 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1875 if (seekable == FALSE) {
1876 GST_INFO_OBJECT (pad,
1877 "Source not seekable, We failed but it does not matter!");
1882 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1885 gst_query_unref (seeking);
1888 if (evdata->flush) {
1890 aggpad->priv->pending_flush_start = FALSE;
1891 aggpad->priv->pending_flush_stop = FALSE;
1892 PAD_UNLOCK (aggpad);
1895 evdata->one_actually_seeked = TRUE;
1898 evdata->result &= ret;
1900 /* Always send to all pads */
1905 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1906 GstEvent * event, gboolean flush, gboolean only_to_active_pads)
1910 evdata.event = event;
1911 evdata.result = TRUE;
1912 evdata.flush = flush;
1913 evdata.one_actually_seeked = FALSE;
1914 evdata.only_to_active_pads = only_to_active_pads;
1916 /* We first need to set all pads as flushing in a first pass
1917 * as flush_start flush_stop is sometimes sent synchronously
1918 * while we send the seek event */
1922 GST_OBJECT_LOCK (self);
1923 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1924 GstAggregatorPad *pad = l->data;
1927 pad->priv->pending_flush_start = TRUE;
1928 pad->priv->pending_flush_stop = FALSE;
1931 GST_OBJECT_UNLOCK (self);
1934 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1936 gst_event_unref (event);
1942 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1947 GstSeekType start_type, stop_type;
1951 GstAggregatorPrivate *priv = self->priv;
1953 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1954 &start, &stop_type, &stop);
1956 GST_INFO_OBJECT (self, "starting SEEK");
1958 flush = flags & GST_SEEK_FLAG_FLUSH;
1960 GST_OBJECT_LOCK (self);
1962 priv->pending_flush_start = TRUE;
1963 priv->flush_seeking = TRUE;
1966 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1967 stop_type, stop, NULL);
1969 /* Seeking sets a position */
1970 self->priv->first_buffer = FALSE;
1971 GST_OBJECT_UNLOCK (self);
1973 /* forward the seek upstream */
1975 gst_aggregator_forward_event_to_all_sinkpads (self, event, flush, FALSE);
1978 if (!evdata.result || !evdata.one_actually_seeked) {
1979 GST_OBJECT_LOCK (self);
1980 priv->flush_seeking = FALSE;
1981 priv->pending_flush_start = FALSE;
1982 GST_OBJECT_UNLOCK (self);
1985 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1987 return evdata.result;
1991 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1994 gboolean res = TRUE;
1996 switch (GST_EVENT_TYPE (event)) {
1997 case GST_EVENT_SEEK:
1999 gst_event_ref (event);
2000 res = gst_aggregator_do_seek (self, event);
2001 gst_event_unref (event);
2005 case GST_EVENT_NAVIGATION:
2007 /* navigation is rather pointless. */
2009 gst_event_unref (event);
2018 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
2019 * they will receive a QOS event that has earliest_time=0 (because we can't
2020 * have negative timestamps), and consider their buffer as too late */
2022 gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE,
2023 GST_EVENT_TYPE (event) == GST_EVENT_QOS);
2024 res = evdata.result;
2031 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
2034 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2036 return klass->src_event (GST_AGGREGATOR (parent), event);
2040 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
2043 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2045 return klass->src_query (GST_AGGREGATOR (parent), query);
2049 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
2050 GstObject * parent, GstPadMode mode, gboolean active)
2052 GstAggregator *self = GST_AGGREGATOR (parent);
2053 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2055 if (klass->src_activate) {
2056 if (klass->src_activate (self, mode, active) == FALSE) {
2061 if (active == TRUE) {
2063 case GST_PAD_MODE_PUSH:
2065 GST_INFO_OBJECT (pad, "Activating pad!");
2066 gst_aggregator_start_srcpad_task (self);
2071 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
2078 GST_INFO_OBJECT (self, "Deactivating srcpad");
2079 gst_aggregator_stop_srcpad_task (self, FALSE);
2085 gst_aggregator_default_sink_query (GstAggregator * self,
2086 GstAggregatorPad * aggpad, GstQuery * query)
2088 GstPad *pad = GST_PAD (aggpad);
2090 if (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION) {
2091 GstQuery *decide_query = NULL;
2092 GstAggregatorClass *agg_class;
2095 GST_OBJECT_LOCK (self);
2097 if (G_UNLIKELY (!aggpad->priv->negotiated)) {
2098 GST_DEBUG_OBJECT (self,
2099 "not negotiated yet, can't answer ALLOCATION query");
2100 PAD_UNLOCK (aggpad);
2101 GST_OBJECT_UNLOCK (self);
2106 if ((decide_query = self->priv->allocation_query))
2107 gst_query_ref (decide_query);
2108 PAD_UNLOCK (aggpad);
2109 GST_OBJECT_UNLOCK (self);
2111 GST_DEBUG_OBJECT (self,
2112 "calling propose allocation with query %" GST_PTR_FORMAT, decide_query);
2114 agg_class = GST_AGGREGATOR_GET_CLASS (self);
2116 /* pass the query to the propose_allocation vmethod if any */
2117 if (agg_class->propose_allocation)
2118 ret = agg_class->propose_allocation (self, aggpad, decide_query, query);
2123 gst_query_unref (decide_query);
2125 GST_DEBUG_OBJECT (self, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
2129 return gst_pad_query_default (pad, GST_OBJECT (self), query);
2133 gst_aggregator_finalize (GObject * object)
2135 GstAggregator *self = (GstAggregator *) object;
2137 g_mutex_clear (&self->priv->src_lock);
2138 g_cond_clear (&self->priv->src_cond);
2140 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
2144 * gst_aggregator_set_latency_property:
2145 * @agg: a #GstAggregator
2146 * @latency: the new latency value (in nanoseconds).
2148 * Sets the new latency value to @latency. This value is used to limit the
2149 * amount of time a pad waits for data to appear before considering the pad
2153 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
2157 g_return_if_fail (GST_IS_AGGREGATOR (self));
2158 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
2161 changed = (self->priv->latency != latency);
2166 GST_OBJECT_LOCK (self);
2167 /* First lock all the pads */
2168 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2169 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2173 self->priv->latency = latency;
2175 SRC_BROADCAST (self);
2177 /* Now wake up the pads */
2178 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2179 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2180 PAD_BROADCAST_EVENT (aggpad);
2181 PAD_UNLOCK (aggpad);
2183 GST_OBJECT_UNLOCK (self);
2189 gst_element_post_message (GST_ELEMENT_CAST (self),
2190 gst_message_new_latency (GST_OBJECT_CAST (self)));
2194 * gst_aggregator_get_latency_property:
2195 * @agg: a #GstAggregator
2197 * Gets the latency value. See gst_aggregator_set_latency for
2200 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
2201 * before a pad is deemed unresponsive. A value of -1 means an
2205 gst_aggregator_get_latency_property (GstAggregator * agg)
2209 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
2211 GST_OBJECT_LOCK (agg);
2212 res = agg->priv->latency;
2213 GST_OBJECT_UNLOCK (agg);
2219 gst_aggregator_set_property (GObject * object, guint prop_id,
2220 const GValue * value, GParamSpec * pspec)
2222 GstAggregator *agg = GST_AGGREGATOR (object);
2226 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
2228 case PROP_START_TIME_SELECTION:
2229 agg->priv->start_time_selection = g_value_get_enum (value);
2231 case PROP_START_TIME:
2232 agg->priv->start_time = g_value_get_uint64 (value);
2235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2241 gst_aggregator_get_property (GObject * object, guint prop_id,
2242 GValue * value, GParamSpec * pspec)
2244 GstAggregator *agg = GST_AGGREGATOR (object);
2248 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
2250 case PROP_START_TIME_SELECTION:
2251 g_value_set_enum (value, agg->priv->start_time_selection);
2253 case PROP_START_TIME:
2254 g_value_set_uint64 (value, agg->priv->start_time);
2257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2262 /* GObject vmethods implementations */
2264 gst_aggregator_class_init (GstAggregatorClass * klass)
2266 GObjectClass *gobject_class = (GObjectClass *) klass;
2267 GstElementClass *gstelement_class = (GstElementClass *) klass;
2269 aggregator_parent_class = g_type_class_peek_parent (klass);
2270 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
2272 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
2273 GST_DEBUG_FG_MAGENTA, "GstAggregator");
2275 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
2277 klass->sink_event = gst_aggregator_default_sink_event;
2278 klass->sink_query = gst_aggregator_default_sink_query;
2280 klass->src_event = gst_aggregator_default_src_event;
2281 klass->src_query = gst_aggregator_default_src_query;
2283 klass->create_new_pad = gst_aggregator_default_create_new_pad;
2284 klass->update_src_caps = gst_aggregator_default_update_src_caps;
2285 klass->fixate_src_caps = gst_aggregator_default_fixate_src_caps;
2286 klass->negotiated_src_caps = gst_aggregator_default_negotiated_src_caps;
2288 gstelement_class->request_new_pad =
2289 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
2290 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
2291 gstelement_class->release_pad =
2292 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
2293 gstelement_class->change_state =
2294 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
2296 gobject_class->set_property = gst_aggregator_set_property;
2297 gobject_class->get_property = gst_aggregator_get_property;
2298 gobject_class->finalize = gst_aggregator_finalize;
2300 g_object_class_install_property (gobject_class, PROP_LATENCY,
2301 g_param_spec_int64 ("latency", "Buffer latency",
2302 "Additional latency in live mode to allow upstream "
2303 "to take longer to produce buffers for the current "
2304 "position (in nanoseconds)", 0,
2305 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
2306 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2308 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
2309 g_param_spec_enum ("start-time-selection", "Start Time Selection",
2310 "Decides which start time is output",
2311 gst_aggregator_start_time_selection_get_type (),
2312 DEFAULT_START_TIME_SELECTION,
2313 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2315 g_object_class_install_property (gobject_class, PROP_START_TIME,
2316 g_param_spec_uint64 ("start-time", "Start Time",
2317 "Start time to use if start-time-selection=set", 0,
2319 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2321 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
2325 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
2327 GstPadTemplate *pad_template;
2328 GstAggregatorPrivate *priv;
2330 g_return_if_fail (klass->aggregate != NULL);
2333 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2334 GstAggregatorPrivate);
2339 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2340 g_return_if_fail (pad_template != NULL);
2342 priv->max_padserial = -1;
2343 priv->tags_changed = FALSE;
2345 self->priv->peer_latency_live = FALSE;
2346 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2347 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2348 self->priv->has_peer_latency = FALSE;
2349 gst_aggregator_reset_flow_values (self);
2351 self->srcpad = gst_pad_new_from_template (pad_template, "src");
2353 gst_pad_set_event_function (self->srcpad,
2354 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2355 gst_pad_set_query_function (self->srcpad,
2356 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2357 gst_pad_set_activatemode_function (self->srcpad,
2358 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2360 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2362 self->priv->latency = DEFAULT_LATENCY;
2363 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2364 self->priv->start_time = DEFAULT_START_TIME;
2366 g_mutex_init (&self->priv->src_lock);
2367 g_cond_init (&self->priv->src_cond);
2370 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2371 * method to get to the padtemplates */
2373 gst_aggregator_get_type (void)
2375 static volatile gsize type = 0;
2377 if (g_once_init_enter (&type)) {
2379 static const GTypeInfo info = {
2380 sizeof (GstAggregatorClass),
2383 (GClassInitFunc) gst_aggregator_class_init,
2386 sizeof (GstAggregator),
2388 (GInstanceInitFunc) gst_aggregator_init,
2391 _type = g_type_register_static (GST_TYPE_ELEMENT,
2392 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2393 g_once_init_leave (&type, _type);
2398 /* Must be called with SRC lock and PAD lock held */
2400 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2402 /* Empty queue always has space */
2403 if (aggpad->priv->num_buffers == 0 && aggpad->priv->clipped_buffer == NULL)
2406 /* We also want at least two buffers, one is being processed and one is ready
2407 * for the next iteration when we operate in live mode. */
2408 if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2411 /* zero latency, if there is a buffer, it's full */
2412 if (self->priv->latency == 0)
2415 /* Allow no more buffers than the latency */
2416 return (aggpad->priv->time_level <= self->priv->latency);
2419 /* Must be called with the PAD_LOCK held */
2421 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2423 GstClockTime timestamp;
2425 if (GST_BUFFER_DTS_IS_VALID (buffer))
2426 timestamp = GST_BUFFER_DTS (buffer);
2428 timestamp = GST_BUFFER_PTS (buffer);
2430 if (timestamp == GST_CLOCK_TIME_NONE) {
2432 timestamp = aggpad->priv->head_position;
2434 timestamp = aggpad->priv->tail_position;
2438 if (GST_BUFFER_DURATION_IS_VALID (buffer))
2439 timestamp += GST_BUFFER_DURATION (buffer);
2442 aggpad->priv->head_position = timestamp;
2444 aggpad->priv->tail_position = timestamp;
2446 update_time_level (aggpad, head);
2449 static GstFlowReturn
2450 gst_aggregator_pad_chain_internal (GstAggregator * self,
2451 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2453 GstFlowReturn flow_return;
2454 GstClockTime buf_pts;
2456 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2458 PAD_FLUSH_LOCK (aggpad);
2461 flow_return = aggpad->priv->flow_return;
2462 if (flow_return != GST_FLOW_OK)
2465 if (aggpad->priv->pending_eos == TRUE)
2468 PAD_UNLOCK (aggpad);
2470 buf_pts = GST_BUFFER_PTS (buffer);
2474 GST_OBJECT_LOCK (self);
2477 if (aggpad->priv->first_buffer) {
2478 self->priv->has_peer_latency = FALSE;
2479 aggpad->priv->first_buffer = FALSE;
2482 if (gst_aggregator_pad_has_space (self, aggpad)
2483 && aggpad->priv->flow_return == GST_FLOW_OK) {
2485 g_queue_push_head (&aggpad->priv->buffers, buffer);
2487 g_queue_push_tail (&aggpad->priv->buffers, buffer);
2488 apply_buffer (aggpad, buffer, head);
2489 aggpad->priv->num_buffers++;
2491 SRC_BROADCAST (self);
2495 flow_return = aggpad->priv->flow_return;
2496 if (flow_return != GST_FLOW_OK) {
2497 GST_OBJECT_UNLOCK (self);
2501 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2502 GST_OBJECT_UNLOCK (self);
2504 PAD_WAIT_EVENT (aggpad);
2506 PAD_UNLOCK (aggpad);
2509 if (self->priv->first_buffer) {
2510 GstClockTime start_time;
2512 switch (self->priv->start_time_selection) {
2513 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2517 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2518 GST_OBJECT_LOCK (aggpad);
2519 if (aggpad->priv->head_segment.format == GST_FORMAT_TIME) {
2520 start_time = buf_pts;
2521 if (start_time != -1) {
2522 start_time = MAX (start_time, aggpad->priv->head_segment.start);
2524 gst_segment_to_running_time (&aggpad->priv->head_segment,
2525 GST_FORMAT_TIME, start_time);
2529 GST_WARNING_OBJECT (aggpad,
2530 "Ignoring request of selecting the first start time "
2531 "as the segment is a %s segment instead of a time segment",
2532 gst_format_get_name (aggpad->segment.format));
2534 GST_OBJECT_UNLOCK (aggpad);
2536 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2537 start_time = self->priv->start_time;
2538 if (start_time == -1)
2543 if (start_time != -1) {
2544 if (self->segment.position == -1)
2545 self->segment.position = start_time;
2547 self->segment.position = MIN (start_time, self->segment.position);
2549 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2550 GST_TIME_ARGS (start_time));
2554 PAD_UNLOCK (aggpad);
2555 GST_OBJECT_UNLOCK (self);
2558 PAD_FLUSH_UNLOCK (aggpad);
2560 GST_DEBUG_OBJECT (aggpad, "Done chaining");
2565 PAD_UNLOCK (aggpad);
2566 PAD_FLUSH_UNLOCK (aggpad);
2568 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2569 gst_flow_get_name (flow_return));
2571 gst_buffer_unref (buffer);
2576 PAD_UNLOCK (aggpad);
2577 PAD_FLUSH_UNLOCK (aggpad);
2579 gst_buffer_unref (buffer);
2580 GST_DEBUG_OBJECT (aggpad, "We are EOS already...");
2582 return GST_FLOW_EOS;
2585 static GstFlowReturn
2586 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2588 return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2589 GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2593 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2596 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2597 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2599 if (GST_QUERY_IS_SERIALIZED (query)) {
2602 while (!gst_aggregator_pad_queue_is_empty (aggpad)
2603 && aggpad->priv->flow_return == GST_FLOW_OK) {
2604 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2605 PAD_WAIT_EVENT (aggpad);
2608 if (aggpad->priv->flow_return != GST_FLOW_OK)
2611 PAD_UNLOCK (aggpad);
2614 return klass->sink_query (GST_AGGREGATOR (parent),
2615 GST_AGGREGATOR_PAD (pad), query);
2618 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2619 gst_flow_get_name (aggpad->priv->flow_return));
2620 PAD_UNLOCK (aggpad);
2624 static GstFlowReturn
2625 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2628 GstFlowReturn ret = GST_FLOW_OK;
2629 GstAggregator *self = GST_AGGREGATOR (parent);
2630 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2631 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2633 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
2634 /* && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE */ ) {
2638 if (aggpad->priv->flow_return != GST_FLOW_OK
2639 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2640 ret = aggpad->priv->flow_return;
2644 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2645 GST_OBJECT_LOCK (aggpad);
2646 gst_event_copy_segment (event, &aggpad->priv->head_segment);
2647 aggpad->priv->head_position = aggpad->priv->head_segment.position;
2648 update_time_level (aggpad, TRUE);
2649 GST_OBJECT_UNLOCK (aggpad);
2652 if (GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2653 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT,
2655 g_queue_push_head (&aggpad->priv->buffers, event);
2657 SRC_BROADCAST (self);
2659 PAD_UNLOCK (aggpad);
2664 if (!klass->sink_event (self, aggpad, event)) {
2665 /* Copied from GstPad to convert boolean to a GstFlowReturn in
2666 * the event handling func */
2667 ret = GST_FLOW_ERROR;
2674 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2675 gst_flow_get_name (aggpad->priv->flow_return));
2676 PAD_UNLOCK (aggpad);
2678 if (GST_EVENT_IS_STICKY (event))
2679 gst_pad_store_sticky_event (pad, event);
2680 gst_event_unref (event);
2686 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2687 GstObject * parent, GstPadMode mode, gboolean active)
2689 GstAggregator *self = GST_AGGREGATOR (parent);
2690 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2692 if (active == FALSE) {
2694 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2695 SRC_BROADCAST (self);
2699 aggpad->priv->flow_return = GST_FLOW_OK;
2700 PAD_BROADCAST_EVENT (aggpad);
2701 PAD_UNLOCK (aggpad);
2707 /***********************************
2708 * GstAggregatorPad implementation *
2709 ************************************/
2710 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2713 gst_aggregator_pad_constructed (GObject * object)
2715 GstPad *pad = GST_PAD (object);
2717 gst_pad_set_chain_function (pad,
2718 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2719 gst_pad_set_event_full_function_full (pad,
2720 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
2721 gst_pad_set_query_function (pad,
2722 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2723 gst_pad_set_activatemode_function (pad,
2724 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2728 gst_aggregator_pad_finalize (GObject * object)
2730 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2732 g_cond_clear (&pad->priv->event_cond);
2733 g_mutex_clear (&pad->priv->flush_lock);
2734 g_mutex_clear (&pad->priv->lock);
2736 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2740 gst_aggregator_pad_dispose (GObject * object)
2742 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2744 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2746 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2750 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2752 GObjectClass *gobject_class = (GObjectClass *) klass;
2754 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2756 gobject_class->constructed = gst_aggregator_pad_constructed;
2757 gobject_class->finalize = gst_aggregator_pad_finalize;
2758 gobject_class->dispose = gst_aggregator_pad_dispose;
2762 gst_aggregator_pad_init (GstAggregatorPad * pad)
2765 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2766 GstAggregatorPadPrivate);
2768 g_queue_init (&pad->priv->buffers);
2769 g_cond_init (&pad->priv->event_cond);
2771 g_mutex_init (&pad->priv->flush_lock);
2772 g_mutex_init (&pad->priv->lock);
2774 gst_aggregator_pad_reset_unlocked (pad);
2775 pad->priv->negotiated = FALSE;
2778 /* Must be called with the PAD_LOCK held */
2780 gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad)
2782 pad->priv->num_buffers--;
2783 GST_TRACE_OBJECT (pad, "Consuming buffer");
2784 if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
2785 pad->priv->pending_eos = FALSE;
2786 pad->priv->eos = TRUE;
2788 PAD_BROADCAST_EVENT (pad);
2791 /* Must be called with the PAD_LOCK held */
2793 gst_aggregator_pad_clip_buffer_unlocked (GstAggregatorPad * pad)
2795 GstAggregator *self = NULL;
2796 GstAggregatorClass *aggclass;
2797 GstBuffer *buffer = NULL;
2799 while (pad->priv->clipped_buffer == NULL &&
2800 GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->buffers))) {
2801 buffer = g_queue_pop_tail (&pad->priv->buffers);
2803 apply_buffer (pad, buffer, FALSE);
2805 /* We only take the parent here so that it's not taken if the buffer is
2806 * already clipped or if the queue is empty.
2809 self = GST_AGGREGATOR (gst_pad_get_parent_element (GST_PAD (pad)));
2811 gst_buffer_unref (buffer);
2815 aggclass = GST_AGGREGATOR_GET_CLASS (self);
2818 if (aggclass->clip) {
2819 GST_TRACE_OBJECT (pad, "Clipping: %" GST_PTR_FORMAT, buffer);
2821 buffer = aggclass->clip (self, pad, buffer);
2823 if (buffer == NULL) {
2824 gst_aggregator_pad_buffer_consumed (pad);
2825 GST_TRACE_OBJECT (pad, "Clipping consumed the buffer");
2829 pad->priv->clipped_buffer = buffer;
2833 gst_object_unref (self);
2837 * gst_aggregator_pad_steal_buffer:
2838 * @pad: the pad to get buffer from
2840 * Steal the ref to the buffer currently queued in @pad.
2842 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2843 * queued. You should unref the buffer after usage.
2846 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2852 gst_aggregator_pad_clip_buffer_unlocked (pad);
2854 buffer = pad->priv->clipped_buffer;
2855 pad->priv->clipped_buffer = NULL;
2858 gst_aggregator_pad_buffer_consumed (pad);
2859 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2868 * gst_aggregator_pad_drop_buffer:
2869 * @pad: the pad where to drop any pending buffer
2871 * Drop the buffer currently queued in @pad.
2873 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2876 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2880 buf = gst_aggregator_pad_steal_buffer (pad);
2885 gst_buffer_unref (buf);
2890 * gst_aggregator_pad_get_buffer:
2891 * @pad: the pad to get buffer from
2893 * Returns: (transfer full): A reference to the buffer in @pad or
2894 * NULL if no buffer was queued. You should unref the buffer after
2898 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2904 gst_aggregator_pad_clip_buffer_unlocked (pad);
2906 if (pad->priv->clipped_buffer) {
2907 buffer = gst_buffer_ref (pad->priv->clipped_buffer);
2917 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2922 is_eos = pad->priv->eos;
2929 * gst_aggregator_merge_tags:
2930 * @self: a #GstAggregator
2931 * @tags: a #GstTagList to merge
2932 * @mode: the #GstTagMergeMode to use
2934 * Adds tags to so-called pending tags, which will be processed
2935 * before pushing out data downstream.
2937 * Note that this is provided for convenience, and the subclass is
2938 * not required to use this and can still do tag handling on its own.
2943 gst_aggregator_merge_tags (GstAggregator * self,
2944 const GstTagList * tags, GstTagMergeMode mode)
2948 g_return_if_fail (GST_IS_AGGREGATOR (self));
2949 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2951 /* FIXME Check if we can use OBJECT lock here! */
2952 GST_OBJECT_LOCK (self);
2954 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2955 otags = self->priv->tags;
2956 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2958 gst_tag_list_unref (otags);
2959 self->priv->tags_changed = TRUE;
2960 GST_OBJECT_UNLOCK (self);
2964 * gst_aggregator_set_latency:
2965 * @self: a #GstAggregator
2966 * @min_latency: minimum latency
2967 * @max_latency: maximum latency
2969 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2970 * latency is. Will also post a LATENCY message on the bus so the pipeline
2971 * can reconfigure its global latency.
2974 gst_aggregator_set_latency (GstAggregator * self,
2975 GstClockTime min_latency, GstClockTime max_latency)
2977 gboolean changed = FALSE;
2979 g_return_if_fail (GST_IS_AGGREGATOR (self));
2980 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2981 g_return_if_fail (max_latency >= min_latency);
2984 if (self->priv->sub_latency_min != min_latency) {
2985 self->priv->sub_latency_min = min_latency;
2988 if (self->priv->sub_latency_max != max_latency) {
2989 self->priv->sub_latency_max = max_latency;
2994 SRC_BROADCAST (self);
2998 gst_element_post_message (GST_ELEMENT_CAST (self),
2999 gst_message_new_latency (GST_OBJECT_CAST (self)));
3004 * gst_aggregator_get_buffer_pool:
3005 * @self: a #GstAggregator
3007 * Returns: (transfer full): the instance of the #GstBufferPool used
3008 * by @trans; free it after use it
3011 gst_aggregator_get_buffer_pool (GstAggregator * self)
3013 GstBufferPool *pool;
3015 g_return_val_if_fail (GST_IS_AGGREGATOR (self), NULL);
3017 GST_OBJECT_LOCK (self);
3018 pool = self->priv->pool;
3020 gst_object_ref (pool);
3021 GST_OBJECT_UNLOCK (self);
3027 * gst_aggregator_get_allocator:
3028 * @self: a #GstAggregator
3029 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3031 * @params: (out) (allow-none) (transfer full): the
3032 * #GstAllocationParams of @allocator
3034 * Lets #GstAggregator sub-classes get the memory @allocator
3035 * acquired by the base class and its @params.
3037 * Unref the @allocator after use it.
3040 gst_aggregator_get_allocator (GstAggregator * self,
3041 GstAllocator ** allocator, GstAllocationParams * params)
3043 g_return_if_fail (GST_IS_AGGREGATOR (self));
3046 *allocator = self->priv->allocator ?
3047 gst_object_ref (self->priv->allocator) : NULL;
3050 *params = self->priv->allocation_params;