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 * * Installs a #GstPadChainFunction, a #GstPadEventFullFunction and a
36 * #GstPadQueryFunction to queue all serialized data packets per sink pad.
37 * Subclasses should not overwrite those, but instead implement
38 * #GstAggregatorClass.sink_event() and #GstAggregatorClass.sink_query() as
41 * * When data is queued on all pads, the aggregate vmethod is called.
43 * * One can peek at the data on any given GstAggregatorPad with the
44 * gst_aggregator_pad_get_buffer () method, and take ownership of it
45 * with the gst_aggregator_pad_steal_buffer () method. When a buffer
46 * has been taken with steal_buffer (), a new buffer can be queued
49 * * If the subclass wishes to push a buffer downstream in its aggregate
50 * implementation, it should do so through the
51 * gst_aggregator_finish_buffer () method. This method will take care
52 * of sending and ordering mandatory events such as stream start, caps
55 * * Same goes for EOS events, which should not be pushed directly by the
56 * subclass, it should instead return GST_FLOW_EOS in its aggregate
59 * * Note that the aggregator logic regarding gap event handling is to turn
60 * these into gap buffers with matching PTS and duration. It will also
61 * flag these buffers with GST_BUFFER_FLAG_GAP and GST_BUFFER_FLAG_DROPPABLE
62 * to ease their identification and subsequent processing.
70 #include <string.h> /* strlen */
72 #include "gstaggregator.h"
76 GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
77 GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
78 GST_AGGREGATOR_START_TIME_SELECTION_SET
79 } GstAggregatorStartTimeSelection;
82 gst_aggregator_start_time_selection_get_type (void)
84 static GType gtype = 0;
87 static const GEnumValue values[] = {
88 {GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
89 "Start at 0 running time (default)", "zero"},
90 {GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
91 "Start at first observed input running time", "first"},
92 {GST_AGGREGATOR_START_TIME_SELECTION_SET,
93 "Set start time with start-time property", "set"},
97 gtype = g_enum_register_static ("GstAggregatorStartTimeSelection", values);
102 /* Might become API */
103 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
104 const GstTagList * tags, GstTagMergeMode mode);
105 static void gst_aggregator_set_latency_property (GstAggregator * agg,
107 static gint64 gst_aggregator_get_latency_property (GstAggregator * agg);
110 /* Locking order, locks in this element must always be taken in this order
112 * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
113 * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
114 * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
115 * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
116 * standard element object lock -> GST_OBJECT_LOCK(agg)
117 * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
118 * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
119 * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
123 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
125 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
126 #define GST_CAT_DEFAULT aggregator_debug
128 /* GstAggregatorPad definitions */
129 #define PAD_LOCK(pad) G_STMT_START { \
130 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
132 g_mutex_lock(&pad->priv->lock); \
133 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
137 #define PAD_UNLOCK(pad) G_STMT_START { \
138 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
140 g_mutex_unlock(&pad->priv->lock); \
141 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
146 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
147 GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p", \
149 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
150 (&((GstAggregatorPad*)pad)->priv->lock)); \
151 GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
155 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
156 GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p", \
158 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
162 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
163 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
165 g_mutex_lock(&pad->priv->flush_lock); \
166 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
170 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
171 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
173 g_mutex_unlock(&pad->priv->flush_lock); \
174 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
178 #define SRC_LOCK(self) G_STMT_START { \
179 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
181 g_mutex_lock(&self->priv->src_lock); \
182 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
186 #define SRC_UNLOCK(self) G_STMT_START { \
187 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
189 g_mutex_unlock(&self->priv->src_lock); \
190 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
194 #define SRC_WAIT(self) G_STMT_START { \
195 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
197 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
198 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
202 #define SRC_BROADCAST(self) G_STMT_START { \
203 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
205 if (self->priv->aggregate_id) \
206 gst_clock_id_unschedule (self->priv->aggregate_id); \
207 g_cond_broadcast(&(self->priv->src_cond)); \
210 struct _GstAggregatorPadPrivate
212 /* Following fields are protected by the PAD_LOCK */
213 GstFlowReturn flow_return;
214 gboolean pending_flush_start;
215 gboolean pending_flush_stop;
216 gboolean pending_eos;
218 gboolean first_buffer;
220 GQueue data; /* buffers, events and queries */
221 GstBuffer *clipped_buffer;
223 GstClockTime head_position;
224 GstClockTime tail_position;
225 GstClockTime head_time; /* running time */
226 GstClockTime tail_time;
227 GstClockTime time_level; /* how much head is ahead of tail */
228 GstSegment head_segment; /* segment before the queue */
236 /* This lock prevents a flush start processing happening while
237 * the chain function is also happening.
242 /* Must be called with PAD_LOCK held */
244 gst_aggregator_pad_reset_unlocked (GstAggregatorPad * aggpad)
246 aggpad->priv->pending_eos = FALSE;
247 aggpad->priv->eos = FALSE;
248 aggpad->priv->flow_return = GST_FLOW_OK;
249 GST_OBJECT_LOCK (aggpad);
250 gst_segment_init (&aggpad->segment, GST_FORMAT_UNDEFINED);
251 gst_segment_init (&aggpad->priv->head_segment, GST_FORMAT_UNDEFINED);
252 GST_OBJECT_UNLOCK (aggpad);
253 aggpad->priv->head_position = GST_CLOCK_TIME_NONE;
254 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
255 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
256 aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
257 aggpad->priv->time_level = 0;
258 aggpad->priv->first_buffer = TRUE;
262 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
264 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
267 gst_aggregator_pad_reset_unlocked (aggpad);
271 return klass->flush (aggpad, agg);
276 /*************************************
277 * GstAggregator implementation *
278 *************************************/
279 static GstElementClass *aggregator_parent_class = NULL;
281 /* All members are protected by the object lock unless otherwise noted */
283 struct _GstAggregatorPrivate
287 /* Our state is >= PAUSED */
288 gboolean running; /* protected by src_lock */
291 gboolean send_stream_start; /* protected by srcpad stream lock */
292 gboolean send_segment;
293 gboolean flush_seeking;
294 gboolean pending_flush_start;
295 gboolean send_eos; /* protected by srcpad stream lock */
297 GstCaps *srccaps; /* protected by the srcpad stream lock */
300 gboolean tags_changed;
302 gboolean peer_latency_live; /* protected by src_lock */
303 GstClockTime peer_latency_min; /* protected by src_lock */
304 GstClockTime peer_latency_max; /* protected by src_lock */
305 gboolean has_peer_latency; /* protected by src_lock */
307 GstClockTime sub_latency_min; /* protected by src_lock */
308 GstClockTime sub_latency_max; /* protected by src_lock */
311 GstClockID aggregate_id; /* protected by src_lock */
315 gboolean first_buffer; /* protected by object lock */
316 GstAggregatorStartTimeSelection start_time_selection;
317 GstClockTime start_time;
319 /* protected by the object lock */
320 GstQuery *allocation_query;
321 GstAllocator *allocator;
323 GstAllocationParams allocation_params;
326 gint64 latency; /* protected by both src_lock and all pad locks */
329 /* Seek event forwarding helper */
335 gboolean only_to_active_pads;
339 gboolean one_actually_seeked;
342 #define DEFAULT_LATENCY 0
343 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
344 #define DEFAULT_START_TIME (-1)
350 PROP_START_TIME_SELECTION,
355 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
356 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
359 * gst_aggregator_iterate_sinkpads:
360 * @self: The #GstAggregator
361 * @func: (scope call): The function to call.
362 * @user_data: (closure): The data to pass to @func.
364 * Iterate the sinkpads of aggregator to call a function on them.
366 * This method guarantees that @func will be called only once for each
369 * Returns: %FALSE if there are no sinkpads or if @func returned %FALSE
372 gst_aggregator_iterate_sinkpads (GstAggregator * self,
373 GstAggregatorPadForeachFunc func, gpointer user_data)
375 gboolean result = FALSE;
377 gboolean done = FALSE;
378 GValue item = { 0, };
379 GList *seen_pads = NULL;
381 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
387 switch (gst_iterator_next (iter, &item)) {
388 case GST_ITERATOR_OK:
390 GstAggregatorPad *pad;
392 pad = g_value_get_object (&item);
394 /* if already pushed, skip. FIXME, find something faster to tag pads */
395 if (pad == NULL || g_list_find (seen_pads, pad)) {
396 g_value_reset (&item);
400 GST_LOG_OBJECT (pad, "calling function %s on pad",
401 GST_DEBUG_FUNCPTR_NAME (func));
403 result = func (self, pad, user_data);
407 seen_pads = g_list_prepend (seen_pads, pad);
409 g_value_reset (&item);
412 case GST_ITERATOR_RESYNC:
413 gst_iterator_resync (iter);
415 case GST_ITERATOR_ERROR:
416 GST_ERROR_OBJECT (self,
417 "Could not iterate over internally linked pads");
420 case GST_ITERATOR_DONE:
425 g_value_unset (&item);
426 gst_iterator_free (iter);
428 if (seen_pads == NULL) {
429 GST_DEBUG_OBJECT (self, "No pad seen");
433 g_list_free (seen_pads);
440 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
442 return (g_queue_peek_tail (&pad->priv->data) == NULL &&
443 pad->priv->clipped_buffer == NULL);
447 gst_aggregator_check_pads_ready (GstAggregator * self)
449 GstAggregatorPad *pad;
451 gboolean have_buffer = TRUE;
452 gboolean have_event = FALSE;
454 GST_LOG_OBJECT (self, "checking pads");
456 GST_OBJECT_LOCK (self);
458 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
459 if (sinkpads == NULL)
462 for (l = sinkpads; l != NULL; l = l->next) {
467 if (pad->priv->num_buffers == 0) {
468 if (!gst_aggregator_pad_queue_is_empty (pad))
470 if (!pad->priv->eos) {
473 /* If not live we need data on all pads, so leave the loop */
474 if (!self->priv->peer_latency_live) {
479 } else if (self->priv->peer_latency_live) {
480 /* In live mode, having a single pad with buffers is enough to
481 * generate a start time from it. In non-live mode all pads need
484 self->priv->first_buffer = FALSE;
490 if (!have_buffer && !have_event)
494 self->priv->first_buffer = FALSE;
496 GST_OBJECT_UNLOCK (self);
497 GST_LOG_OBJECT (self, "pads are ready");
502 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
503 GST_OBJECT_UNLOCK (self);
509 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet,"
510 " but waking up for serialized event");
512 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
513 GST_OBJECT_UNLOCK (self);
519 gst_aggregator_reset_flow_values (GstAggregator * self)
521 GST_OBJECT_LOCK (self);
522 self->priv->send_stream_start = TRUE;
523 self->priv->send_segment = TRUE;
524 gst_segment_init (&self->segment, GST_FORMAT_TIME);
525 self->priv->first_buffer = TRUE;
526 GST_OBJECT_UNLOCK (self);
530 gst_aggregator_push_mandatory_events (GstAggregator * self)
532 GstAggregatorPrivate *priv = self->priv;
533 GstEvent *segment = NULL;
534 GstEvent *tags = NULL;
536 if (self->priv->send_stream_start) {
539 GST_INFO_OBJECT (self, "pushing stream start");
540 /* stream-start (FIXME: create id based on input ids) */
541 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
542 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
543 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
545 self->priv->send_stream_start = FALSE;
548 if (self->priv->srccaps) {
550 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
551 self->priv->srccaps);
552 if (!gst_pad_push_event (self->srcpad,
553 gst_event_new_caps (self->priv->srccaps))) {
554 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
556 gst_caps_unref (self->priv->srccaps);
557 self->priv->srccaps = NULL;
560 GST_OBJECT_LOCK (self);
561 if (self->priv->send_segment && !self->priv->flush_seeking) {
562 segment = gst_event_new_segment (&self->segment);
564 if (!self->priv->seqnum)
565 self->priv->seqnum = gst_event_get_seqnum (segment);
567 gst_event_set_seqnum (segment, self->priv->seqnum);
568 self->priv->send_segment = FALSE;
570 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
573 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
574 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
575 priv->tags_changed = FALSE;
577 GST_OBJECT_UNLOCK (self);
580 gst_pad_push_event (self->srcpad, segment);
582 gst_pad_push_event (self->srcpad, tags);
587 * gst_aggregator_set_src_caps:
588 * @self: The #GstAggregator
589 * @caps: The #GstCaps to set on the src pad.
591 * Sets the caps to be used on the src pad.
594 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
596 GST_PAD_STREAM_LOCK (self->srcpad);
597 gst_caps_replace (&self->priv->srccaps, caps);
598 gst_aggregator_push_mandatory_events (self);
599 GST_PAD_STREAM_UNLOCK (self->srcpad);
603 * gst_aggregator_finish_buffer:
604 * @self: The #GstAggregator
605 * @buffer: (transfer full): the #GstBuffer to push.
607 * This method will push the provided output buffer downstream. If needed,
608 * mandatory events such as stream-start, caps, and segment events will be
609 * sent before pushing the buffer.
612 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
614 gst_aggregator_push_mandatory_events (self);
616 GST_OBJECT_LOCK (self);
617 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
618 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
619 GST_OBJECT_UNLOCK (self);
620 return gst_pad_push (self->srcpad, buffer);
622 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
623 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
624 GST_OBJECT_UNLOCK (self);
625 gst_buffer_unref (buffer);
631 gst_aggregator_push_eos (GstAggregator * self)
634 gst_aggregator_push_mandatory_events (self);
636 event = gst_event_new_eos ();
638 GST_OBJECT_LOCK (self);
639 self->priv->send_eos = FALSE;
640 gst_event_set_seqnum (event, self->priv->seqnum);
641 GST_OBJECT_UNLOCK (self);
643 gst_pad_push_event (self->srcpad, event);
647 gst_aggregator_get_next_time (GstAggregator * self)
649 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
651 if (klass->get_next_time)
652 return klass->get_next_time (self);
654 return GST_CLOCK_TIME_NONE;
658 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
660 GstClockTime latency;
668 latency = gst_aggregator_get_latency_unlocked (self);
670 if (gst_aggregator_check_pads_ready (self)) {
671 GST_DEBUG_OBJECT (self, "all pads have data");
677 /* Before waiting, check if we're actually still running */
678 if (!self->priv->running || !self->priv->send_eos) {
684 start = gst_aggregator_get_next_time (self);
686 /* If we're not live, or if we use the running time
687 * of the first buffer as start time, we wait until
688 * all pads have buffers.
689 * Otherwise (i.e. if we are live!), we wait on the clock
690 * and if a pad does not have a buffer in time we ignore
693 GST_OBJECT_LOCK (self);
694 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
695 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
696 !GST_CLOCK_TIME_IS_VALID (start) ||
697 (self->priv->first_buffer
698 && self->priv->start_time_selection ==
699 GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
700 /* We wake up here when something happened, and below
701 * then check if we're ready now. If we return FALSE,
702 * we will be directly called again.
704 GST_OBJECT_UNLOCK (self);
707 GstClockTime base_time, time;
709 GstClockReturn status;
710 GstClockTimeDiff jitter;
712 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
713 GST_TIME_ARGS (start));
715 base_time = GST_ELEMENT_CAST (self)->base_time;
716 clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
717 GST_OBJECT_UNLOCK (self);
719 time = base_time + start;
722 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
723 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
724 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
725 GST_TIME_ARGS (time),
726 GST_TIME_ARGS (base_time),
727 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
728 GST_TIME_ARGS (gst_clock_get_time (clock)));
730 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
731 gst_object_unref (clock);
735 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
738 if (self->priv->aggregate_id) {
739 gst_clock_id_unref (self->priv->aggregate_id);
740 self->priv->aggregate_id = NULL;
743 GST_DEBUG_OBJECT (self,
744 "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
745 status, GST_STIME_ARGS (jitter));
748 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
755 res = gst_aggregator_check_pads_ready (self);
762 gst_aggregator_do_events_and_queries (GstAggregator * self,
763 GstAggregatorPad * pad, gpointer user_data)
765 GstEvent *event = NULL;
766 GstQuery *query = NULL;
767 GstAggregatorClass *klass = NULL;
768 gboolean *processed_event = user_data;
775 if (pad->priv->num_buffers == 0 && pad->priv->pending_eos) {
776 pad->priv->pending_eos = FALSE;
777 pad->priv->eos = TRUE;
779 if (pad->priv->clipped_buffer == NULL &&
780 !GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->data))) {
781 if (GST_IS_EVENT (g_queue_peek_tail (&pad->priv->data)))
782 event = gst_event_ref (g_queue_peek_tail (&pad->priv->data));
783 if (GST_IS_QUERY (g_queue_peek_tail (&pad->priv->data)))
784 query = g_queue_peek_tail (&pad->priv->data);
787 if (event || query) {
791 *processed_event = TRUE;
793 klass = GST_AGGREGATOR_GET_CLASS (self);
796 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
797 gst_event_ref (event);
798 ret = klass->sink_event (self, pad, event);
801 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS)
802 pad->priv->negotiated = ret;
803 if (g_queue_peek_tail (&pad->priv->data) == event)
804 gst_event_unref (g_queue_pop_tail (&pad->priv->data));
805 gst_event_unref (event);
807 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, query);
808 ret = klass->sink_query (self, pad, query);
811 if (g_queue_peek_tail (&pad->priv->data) == query) {
814 s = gst_query_writable_structure (query);
815 gst_structure_set (s, "gst-aggregator-retval", G_TYPE_BOOLEAN, ret,
817 g_queue_pop_tail (&pad->priv->data);
821 PAD_BROADCAST_EVENT (pad);
824 } while (event || query);
830 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
831 GstFlowReturn flow_return, gboolean full)
836 if (flow_return == GST_FLOW_NOT_LINKED)
837 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
839 aggpad->priv->flow_return = flow_return;
841 item = g_queue_peek_head_link (&aggpad->priv->data);
843 GList *next = item->next;
845 /* In partial flush, we do like the pad, we get rid of non-sticky events
848 if (full || GST_IS_BUFFER (item->data) ||
849 GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
850 GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
851 !GST_EVENT_IS_STICKY (item->data)) {
852 if (!GST_IS_QUERY (item->data))
853 gst_mini_object_unref (item->data);
854 g_queue_delete_link (&aggpad->priv->data, item);
858 aggpad->priv->num_buffers = 0;
859 gst_buffer_replace (&aggpad->priv->clipped_buffer, NULL);
861 PAD_BROADCAST_EVENT (aggpad);
866 gst_aggregator_default_update_src_caps (GstAggregator * agg, GstCaps * caps,
869 *ret = gst_caps_ref (caps);
875 gst_aggregator_default_fixate_src_caps (GstAggregator * agg, GstCaps * caps)
877 caps = gst_caps_fixate (caps);
883 gst_aggregator_default_negotiated_src_caps (GstAggregator * agg, GstCaps * caps)
889 /* takes ownership of the pool, allocator and query */
891 gst_aggregator_set_allocation (GstAggregator * self,
892 GstBufferPool * pool, GstAllocator * allocator,
893 GstAllocationParams * params, GstQuery * query)
895 GstAllocator *oldalloc;
896 GstBufferPool *oldpool;
899 GST_DEBUG ("storing allocation query");
901 GST_OBJECT_LOCK (self);
902 oldpool = self->priv->pool;
903 self->priv->pool = pool;
905 oldalloc = self->priv->allocator;
906 self->priv->allocator = allocator;
908 oldquery = self->priv->allocation_query;
909 self->priv->allocation_query = query;
912 self->priv->allocation_params = *params;
914 gst_allocation_params_init (&self->priv->allocation_params);
915 GST_OBJECT_UNLOCK (self);
918 GST_DEBUG_OBJECT (self, "deactivating old pool %p", oldpool);
919 gst_buffer_pool_set_active (oldpool, FALSE);
920 gst_object_unref (oldpool);
923 gst_object_unref (oldalloc);
926 gst_query_unref (oldquery);
933 gst_aggregator_decide_allocation (GstAggregator * self, GstQuery * query)
935 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
937 if (aggclass->decide_allocation)
938 if (!aggclass->decide_allocation (self, query))
945 gst_aggregator_do_allocation (GstAggregator * self, GstCaps * caps)
948 gboolean result = TRUE;
949 GstBufferPool *pool = NULL;
950 GstAllocator *allocator;
951 GstAllocationParams params;
953 /* find a pool for the negotiated caps now */
954 GST_DEBUG_OBJECT (self, "doing allocation query");
955 query = gst_query_new_allocation (caps, TRUE);
956 if (!gst_pad_peer_query (self->srcpad, query)) {
957 /* not a problem, just debug a little */
958 GST_DEBUG_OBJECT (self, "peer ALLOCATION query failed");
961 GST_DEBUG_OBJECT (self, "calling decide_allocation");
962 result = gst_aggregator_decide_allocation (self, query);
964 GST_DEBUG_OBJECT (self, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
968 goto no_decide_allocation;
970 /* we got configuration from our peer or the decide_allocation method,
972 if (gst_query_get_n_allocation_params (query) > 0) {
973 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
976 gst_allocation_params_init (¶ms);
979 if (gst_query_get_n_allocation_pools (query) > 0)
980 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
984 gst_aggregator_set_allocation (self, pool, allocator, ¶ms, query);
989 no_decide_allocation:
991 GST_WARNING_OBJECT (self, "Failed to decide allocation");
992 gst_query_unref (query);
999 /* WITH SRC_LOCK held */
1000 static GstFlowReturn
1001 gst_aggregator_update_src_caps (GstAggregator * self)
1003 GstAggregatorClass *agg_klass = GST_AGGREGATOR_GET_CLASS (self);
1004 GstCaps *downstream_caps, *template_caps, *caps = NULL;
1005 GstFlowReturn ret = GST_FLOW_OK;
1007 template_caps = gst_pad_get_pad_template_caps (self->srcpad);
1008 downstream_caps = gst_pad_peer_query_caps (self->srcpad, template_caps);
1010 if (gst_caps_is_empty (downstream_caps)) {
1011 GST_INFO_OBJECT (self, "Downstream caps (%"
1012 GST_PTR_FORMAT ") not compatible with pad template caps (%"
1013 GST_PTR_FORMAT ")", downstream_caps, template_caps);
1014 ret = GST_FLOW_NOT_NEGOTIATED;
1018 g_assert (agg_klass->update_src_caps);
1019 GST_DEBUG_OBJECT (self, "updating caps from %" GST_PTR_FORMAT,
1021 ret = agg_klass->update_src_caps (self, downstream_caps, &caps);
1022 if (ret < GST_FLOW_OK) {
1023 GST_WARNING_OBJECT (self, "Subclass failed to update provided caps");
1026 if ((caps == NULL || gst_caps_is_empty (caps)) && ret >= GST_FLOW_OK) {
1027 ret = GST_FLOW_NOT_NEGOTIATED;
1030 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1032 #ifdef GST_ENABLE_EXTRA_CHECKS
1033 if (!gst_caps_is_subset (caps, template_caps)) {
1034 GstCaps *intersection;
1036 GST_ERROR_OBJECT (self,
1037 "update_src_caps returned caps %" GST_PTR_FORMAT
1038 " which are not a real subset of the template caps %"
1039 GST_PTR_FORMAT, caps, template_caps);
1040 g_warning ("%s: update_src_caps returned caps which are not a real "
1041 "subset of the filter caps", GST_ELEMENT_NAME (self));
1044 gst_caps_intersect_full (template_caps, caps, GST_CAPS_INTERSECT_FIRST);
1045 gst_caps_unref (caps);
1046 caps = intersection;
1050 if (gst_caps_is_any (caps)) {
1054 if (!gst_caps_is_fixed (caps)) {
1055 g_assert (agg_klass->fixate_src_caps);
1057 GST_DEBUG_OBJECT (self, "fixate caps from %" GST_PTR_FORMAT, caps);
1058 if (!(caps = agg_klass->fixate_src_caps (self, caps))) {
1059 GST_WARNING_OBJECT (self, "Subclass failed to fixate provided caps");
1060 ret = GST_FLOW_NOT_NEGOTIATED;
1063 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1066 if (agg_klass->negotiated_src_caps) {
1067 if (!agg_klass->negotiated_src_caps (self, caps)) {
1068 GST_WARNING_OBJECT (self, "Subclass failed to accept negotiated caps");
1069 ret = GST_FLOW_NOT_NEGOTIATED;
1074 gst_aggregator_set_src_caps (self, caps);
1076 if (!gst_aggregator_do_allocation (self, caps)) {
1077 GST_WARNING_OBJECT (self, "Allocation negotiation failed");
1078 ret = GST_FLOW_NOT_NEGOTIATED;
1082 gst_caps_unref (downstream_caps);
1083 gst_caps_unref (template_caps);
1086 gst_caps_unref (caps);
1092 gst_aggregator_aggregate_func (GstAggregator * self)
1094 GstAggregatorPrivate *priv = self->priv;
1095 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1096 gboolean timeout = FALSE;
1098 if (self->priv->running == FALSE) {
1099 GST_DEBUG_OBJECT (self, "Not running anymore");
1103 GST_LOG_OBJECT (self, "Checking aggregate");
1104 while (priv->send_eos && priv->running) {
1105 GstFlowReturn flow_return = GST_FLOW_OK;
1106 gboolean processed_event = FALSE;
1108 gst_aggregator_iterate_sinkpads (self, gst_aggregator_do_events_and_queries,
1111 if (!gst_aggregator_wait_and_check (self, &timeout))
1114 gst_aggregator_iterate_sinkpads (self, gst_aggregator_do_events_and_queries,
1116 if (processed_event)
1119 if (gst_pad_check_reconfigure (GST_AGGREGATOR_SRC_PAD (self))) {
1120 flow_return = gst_aggregator_update_src_caps (self);
1121 if (flow_return != GST_FLOW_OK)
1122 gst_pad_mark_reconfigure (GST_AGGREGATOR_SRC_PAD (self));
1125 if (timeout || flow_return >= GST_FLOW_OK) {
1126 GST_TRACE_OBJECT (self, "Actually aggregating!");
1127 flow_return = klass->aggregate (self, timeout);
1130 if (flow_return == GST_AGGREGATOR_FLOW_NEED_DATA)
1133 GST_OBJECT_LOCK (self);
1134 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
1135 /* We don't want to set the pads to flushing, but we want to
1136 * stop the thread, so just break here */
1137 GST_OBJECT_UNLOCK (self);
1140 GST_OBJECT_UNLOCK (self);
1142 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
1143 gst_aggregator_push_eos (self);
1146 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
1148 if (flow_return != GST_FLOW_OK) {
1151 GST_OBJECT_LOCK (self);
1152 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
1153 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1155 gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
1157 GST_OBJECT_UNLOCK (self);
1162 /* Pause the task here, the only ways to get here are:
1163 * 1) We're stopping, in which case the task is stopped anyway
1164 * 2) We got a flow error above, in which case it might take
1165 * some time to forward the flow return upstream and we
1166 * would otherwise call the task function over and over
1167 * again without doing anything
1169 gst_pad_pause_task (self->srcpad);
1173 gst_aggregator_start (GstAggregator * self)
1175 GstAggregatorClass *klass;
1178 self->priv->send_stream_start = TRUE;
1179 self->priv->send_segment = TRUE;
1180 self->priv->send_eos = TRUE;
1181 self->priv->srccaps = NULL;
1183 gst_aggregator_set_allocation (self, NULL, NULL, NULL, NULL);
1185 klass = GST_AGGREGATOR_GET_CLASS (self);
1188 result = klass->start (self);
1196 _check_pending_flush_stop (GstAggregatorPad * pad)
1201 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
1208 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
1210 gboolean res = TRUE;
1212 GST_INFO_OBJECT (self, "%s srcpad task",
1213 flush_start ? "Pausing" : "Stopping");
1216 self->priv->running = FALSE;
1217 SRC_BROADCAST (self);
1221 res = gst_pad_push_event (self->srcpad, flush_start);
1224 gst_pad_stop_task (self->srcpad);
1230 gst_aggregator_start_srcpad_task (GstAggregator * self)
1232 GST_INFO_OBJECT (self, "Starting srcpad task");
1234 self->priv->running = TRUE;
1235 gst_pad_start_task (GST_PAD (self->srcpad),
1236 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
1239 static GstFlowReturn
1240 gst_aggregator_flush (GstAggregator * self)
1242 GstFlowReturn ret = GST_FLOW_OK;
1243 GstAggregatorPrivate *priv = self->priv;
1244 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1246 GST_DEBUG_OBJECT (self, "Flushing everything");
1247 GST_OBJECT_LOCK (self);
1248 priv->send_segment = TRUE;
1249 priv->flush_seeking = FALSE;
1250 priv->tags_changed = FALSE;
1251 GST_OBJECT_UNLOCK (self);
1253 ret = klass->flush (self);
1259 /* Called with GstAggregator's object lock held */
1262 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
1265 GstAggregatorPad *tmppad;
1267 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
1268 tmppad = (GstAggregatorPad *) tmp->data;
1270 if (_check_pending_flush_stop (tmppad) == FALSE) {
1271 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
1272 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
1281 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
1284 GstAggregatorPrivate *priv = self->priv;
1285 GstAggregatorPadPrivate *padpriv = aggpad->priv;
1287 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
1289 PAD_FLUSH_LOCK (aggpad);
1291 if (padpriv->pending_flush_start) {
1292 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
1294 padpriv->pending_flush_start = FALSE;
1295 padpriv->pending_flush_stop = TRUE;
1297 PAD_UNLOCK (aggpad);
1299 GST_OBJECT_LOCK (self);
1300 if (priv->flush_seeking) {
1301 /* If flush_seeking we forward the first FLUSH_START */
1302 if (priv->pending_flush_start) {
1303 priv->pending_flush_start = FALSE;
1304 GST_OBJECT_UNLOCK (self);
1306 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
1307 gst_aggregator_stop_srcpad_task (self, event);
1309 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
1310 GST_PAD_STREAM_LOCK (self->srcpad);
1311 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
1314 GST_OBJECT_UNLOCK (self);
1315 gst_event_unref (event);
1318 GST_OBJECT_UNLOCK (self);
1319 gst_event_unref (event);
1321 PAD_FLUSH_UNLOCK (aggpad);
1324 /* Must be called with the the PAD_LOCK held */
1326 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1329 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->head_position) &&
1330 aggpad->priv->head_segment.format == GST_FORMAT_TIME)
1331 aggpad->priv->head_time =
1332 gst_segment_to_running_time (&aggpad->priv->head_segment,
1333 GST_FORMAT_TIME, aggpad->priv->head_position);
1335 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
1337 if (!GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_time))
1338 aggpad->priv->tail_time = aggpad->priv->head_time;
1340 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_position) &&
1341 aggpad->segment.format == GST_FORMAT_TIME)
1342 aggpad->priv->tail_time =
1343 gst_segment_to_running_time (&aggpad->segment,
1344 GST_FORMAT_TIME, aggpad->priv->tail_position);
1346 aggpad->priv->tail_time = aggpad->priv->head_time;
1349 if (aggpad->priv->head_time == GST_CLOCK_TIME_NONE ||
1350 aggpad->priv->tail_time == GST_CLOCK_TIME_NONE) {
1351 aggpad->priv->time_level = 0;
1355 if (aggpad->priv->tail_time > aggpad->priv->head_time)
1356 aggpad->priv->time_level = 0;
1358 aggpad->priv->time_level = aggpad->priv->head_time -
1359 aggpad->priv->tail_time;
1363 /* GstAggregator vmethods default implementations */
1365 gst_aggregator_default_sink_event (GstAggregator * self,
1366 GstAggregatorPad * aggpad, GstEvent * event)
1368 gboolean res = TRUE;
1369 GstPad *pad = GST_PAD (aggpad);
1370 GstAggregatorPrivate *priv = self->priv;
1372 GST_DEBUG_OBJECT (aggpad, "Got event: %" GST_PTR_FORMAT, event);
1374 switch (GST_EVENT_TYPE (event)) {
1375 case GST_EVENT_FLUSH_START:
1377 gst_aggregator_flush_start (self, aggpad, event);
1378 /* We forward only in one case: right after flush_seeking */
1382 case GST_EVENT_FLUSH_STOP:
1384 gst_aggregator_pad_flush (aggpad, self);
1385 GST_OBJECT_LOCK (self);
1386 if (priv->flush_seeking) {
1387 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1388 if (gst_aggregator_all_flush_stop_received_locked (self)) {
1389 GST_OBJECT_UNLOCK (self);
1390 /* That means we received FLUSH_STOP/FLUSH_STOP on
1391 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1392 gst_aggregator_flush (self);
1393 gst_pad_push_event (self->srcpad, event);
1396 priv->send_eos = TRUE;
1397 SRC_BROADCAST (self);
1400 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1401 GST_PAD_STREAM_UNLOCK (self->srcpad);
1402 gst_aggregator_start_srcpad_task (self);
1404 GST_OBJECT_UNLOCK (self);
1407 GST_OBJECT_UNLOCK (self);
1410 /* We never forward the event */
1415 /* We still have a buffer, and we don't want the subclass to have to
1416 * check for it. Mark pending_eos, eos will be set when steal_buffer is
1421 if (aggpad->priv->num_buffers == 0) {
1422 aggpad->priv->eos = TRUE;
1424 aggpad->priv->pending_eos = TRUE;
1426 PAD_UNLOCK (aggpad);
1428 SRC_BROADCAST (self);
1432 case GST_EVENT_SEGMENT:
1435 GST_OBJECT_LOCK (aggpad);
1436 gst_event_copy_segment (event, &aggpad->segment);
1437 /* We've got a new segment, tail_position is now meaningless
1438 * and may interfere with the time_level calculation
1440 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
1441 update_time_level (aggpad, FALSE);
1442 GST_OBJECT_UNLOCK (aggpad);
1443 PAD_UNLOCK (aggpad);
1445 GST_OBJECT_LOCK (self);
1446 self->priv->seqnum = gst_event_get_seqnum (event);
1447 GST_OBJECT_UNLOCK (self);
1450 case GST_EVENT_STREAM_START:
1456 GstClockTime pts, endpts;
1457 GstClockTime duration;
1460 gst_event_parse_gap (event, &pts, &duration);
1461 gapbuf = gst_buffer_new ();
1463 if (GST_CLOCK_TIME_IS_VALID (duration))
1464 endpts = pts + duration;
1466 endpts = GST_CLOCK_TIME_NONE;
1468 GST_OBJECT_LOCK (aggpad);
1469 res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1471 GST_OBJECT_UNLOCK (aggpad);
1474 GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1478 if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1479 duration = endpts - pts;
1481 duration = GST_CLOCK_TIME_NONE;
1483 GST_BUFFER_PTS (gapbuf) = pts;
1484 GST_BUFFER_DURATION (gapbuf) = duration;
1485 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1486 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1488 /* Remove GAP event so we can replace it with the buffer */
1489 if (g_queue_peek_tail (&aggpad->priv->data) == event)
1490 gst_event_unref (g_queue_pop_tail (&aggpad->priv->data));
1492 if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1494 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1504 gst_event_parse_tag (event, &tags);
1506 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1507 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1508 gst_event_unref (event);
1520 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1521 return gst_pad_event_default (pad, GST_OBJECT (self), event);
1524 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1526 gst_event_unref (event);
1531 static inline gboolean
1532 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1533 gpointer unused_udata)
1535 gst_aggregator_pad_flush (pad, self);
1538 pad->priv->flow_return = GST_FLOW_FLUSHING;
1539 pad->priv->negotiated = FALSE;
1540 PAD_BROADCAST_EVENT (pad);
1547 gst_aggregator_stop (GstAggregator * agg)
1549 GstAggregatorClass *klass;
1552 gst_aggregator_reset_flow_values (agg);
1554 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1556 klass = GST_AGGREGATOR_GET_CLASS (agg);
1559 result = klass->stop (agg);
1563 agg->priv->has_peer_latency = FALSE;
1564 agg->priv->peer_latency_live = FALSE;
1565 agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1567 if (agg->priv->tags)
1568 gst_tag_list_unref (agg->priv->tags);
1569 agg->priv->tags = NULL;
1571 gst_aggregator_set_allocation (agg, NULL, NULL, NULL, NULL);
1576 /* GstElement vmethods implementations */
1577 static GstStateChangeReturn
1578 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1580 GstStateChangeReturn ret;
1581 GstAggregator *self = GST_AGGREGATOR (element);
1583 switch (transition) {
1584 case GST_STATE_CHANGE_READY_TO_PAUSED:
1585 if (!gst_aggregator_start (self))
1593 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1594 transition)) == GST_STATE_CHANGE_FAILURE)
1598 switch (transition) {
1599 case GST_STATE_CHANGE_PAUSED_TO_READY:
1600 if (!gst_aggregator_stop (self)) {
1601 /* What to do in this case? Error out? */
1602 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1614 GST_ERROR_OBJECT (element, "parent failed state change");
1619 GST_ERROR_OBJECT (element, "Subclass failed to start");
1620 return GST_STATE_CHANGE_FAILURE;
1625 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1627 GstAggregator *self = GST_AGGREGATOR (element);
1628 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1630 GST_INFO_OBJECT (pad, "Removing pad");
1633 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1634 gst_element_remove_pad (element, pad);
1636 self->priv->has_peer_latency = FALSE;
1637 SRC_BROADCAST (self);
1641 static GstAggregatorPad *
1642 gst_aggregator_default_create_new_pad (GstAggregator * self,
1643 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1645 GstAggregatorPad *agg_pad;
1646 GstAggregatorPrivate *priv = self->priv;
1650 if (templ->direction != GST_PAD_SINK)
1653 if (templ->presence != GST_PAD_REQUEST)
1656 GST_OBJECT_LOCK (self);
1657 if (req_name == NULL || strlen (req_name) < 6
1658 || !g_str_has_prefix (req_name, "sink_")) {
1659 /* no name given when requesting the pad, use next available int */
1660 serial = ++priv->max_padserial;
1662 /* parse serial number from requested padname */
1663 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1664 if (serial > priv->max_padserial)
1665 priv->max_padserial = serial;
1668 name = g_strdup_printf ("sink_%u", serial);
1669 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1670 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1673 GST_OBJECT_UNLOCK (self);
1680 GST_WARNING_OBJECT (self, "request new pad that is not a SINK pad");
1685 GST_WARNING_OBJECT (self, "request new pad that is not a REQUEST pad");
1691 gst_aggregator_request_new_pad (GstElement * element,
1692 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1694 GstAggregator *self;
1695 GstAggregatorPad *agg_pad;
1696 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1697 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1699 self = GST_AGGREGATOR (element);
1701 agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1703 GST_ERROR_OBJECT (element, "Couldn't create new pad");
1707 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1710 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1712 /* add the pad to the element */
1713 gst_element_add_pad (element, GST_PAD (agg_pad));
1715 return GST_PAD (agg_pad);
1718 /* Must be called with SRC_LOCK held */
1721 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1723 gboolean query_ret, live;
1724 GstClockTime our_latency, min, max;
1726 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1729 GST_WARNING_OBJECT (self, "Latency query failed");
1733 gst_query_parse_latency (query, &live, &min, &max);
1735 our_latency = self->priv->latency;
1737 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1738 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1739 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1743 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1744 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1745 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1746 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1747 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1751 self->priv->peer_latency_live = live;
1752 self->priv->peer_latency_min = min;
1753 self->priv->peer_latency_max = max;
1754 self->priv->has_peer_latency = TRUE;
1758 min += self->priv->sub_latency_min;
1759 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1760 && GST_CLOCK_TIME_IS_VALID (max))
1761 max += self->priv->sub_latency_max + our_latency;
1763 max = GST_CLOCK_TIME_NONE;
1765 SRC_BROADCAST (self);
1767 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1768 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1770 gst_query_set_latency (query, live, min, max);
1776 * MUST be called with the src_lock held.
1778 * See gst_aggregator_get_latency() for doc
1781 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1783 GstClockTime latency;
1785 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1787 if (!self->priv->has_peer_latency) {
1788 GstQuery *query = gst_query_new_latency ();
1791 ret = gst_aggregator_query_latency_unlocked (self, query);
1792 gst_query_unref (query);
1794 return GST_CLOCK_TIME_NONE;
1797 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1798 return GST_CLOCK_TIME_NONE;
1800 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1801 latency = self->priv->peer_latency_min;
1804 latency += self->priv->latency;
1805 latency += self->priv->sub_latency_min;
1811 * gst_aggregator_get_latency:
1812 * @self: a #GstAggregator
1814 * Retrieves the latency values reported by @self in response to the latency
1815 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1816 * will not wait for the clock.
1818 * Typically only called by subclasses.
1820 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1823 gst_aggregator_get_latency (GstAggregator * self)
1828 ret = gst_aggregator_get_latency_unlocked (self);
1835 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1837 GstAggregator *self = GST_AGGREGATOR (element);
1839 GST_STATE_LOCK (element);
1840 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1841 GST_STATE (element) < GST_STATE_PAUSED) {
1845 GstSeekType start_type, stop_type;
1848 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1849 &start, &stop_type, &stop);
1851 GST_OBJECT_LOCK (self);
1852 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1853 stop_type, stop, NULL);
1854 self->priv->seqnum = gst_event_get_seqnum (event);
1855 self->priv->first_buffer = FALSE;
1856 GST_OBJECT_UNLOCK (self);
1858 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1860 GST_STATE_UNLOCK (element);
1863 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1868 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1870 gboolean res = TRUE;
1872 switch (GST_QUERY_TYPE (query)) {
1873 case GST_QUERY_SEEKING:
1877 /* don't pass it along as some (file)sink might claim it does
1878 * whereas with a collectpads in between that will not likely work */
1879 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1880 gst_query_set_seeking (query, format, FALSE, 0, -1);
1885 case GST_QUERY_LATENCY:
1887 res = gst_aggregator_query_latency_unlocked (self, query);
1891 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1898 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1900 EventData *evdata = user_data;
1901 gboolean ret = TRUE;
1902 GstPad *peer = gst_pad_get_peer (pad);
1903 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1906 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1907 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1910 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1911 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1912 gst_object_unref (peer);
1917 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1918 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1920 GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1922 if (gst_pad_query (peer, seeking)) {
1925 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1927 if (seekable == FALSE) {
1928 GST_INFO_OBJECT (pad,
1929 "Source not seekable, We failed but it does not matter!");
1934 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1937 gst_query_unref (seeking);
1940 if (evdata->flush) {
1942 aggpad->priv->pending_flush_start = FALSE;
1943 aggpad->priv->pending_flush_stop = FALSE;
1944 PAD_UNLOCK (aggpad);
1947 evdata->one_actually_seeked = TRUE;
1950 evdata->result &= ret;
1952 /* Always send to all pads */
1957 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1960 evdata->result = TRUE;
1961 evdata->one_actually_seeked = FALSE;
1963 /* We first need to set all pads as flushing in a first pass
1964 * as flush_start flush_stop is sometimes sent synchronously
1965 * while we send the seek event */
1966 if (evdata->flush) {
1969 GST_OBJECT_LOCK (self);
1970 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1971 GstAggregatorPad *pad = l->data;
1974 pad->priv->pending_flush_start = TRUE;
1975 pad->priv->pending_flush_stop = FALSE;
1978 GST_OBJECT_UNLOCK (self);
1981 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, evdata);
1983 gst_event_unref (evdata->event);
1987 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1992 GstSeekType start_type, stop_type;
1995 EventData evdata = { 0, };
1996 GstAggregatorPrivate *priv = self->priv;
1998 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1999 &start, &stop_type, &stop);
2001 GST_INFO_OBJECT (self, "starting SEEK");
2003 flush = flags & GST_SEEK_FLAG_FLUSH;
2005 GST_OBJECT_LOCK (self);
2007 priv->pending_flush_start = TRUE;
2008 priv->flush_seeking = TRUE;
2011 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
2012 stop_type, stop, NULL);
2014 /* Seeking sets a position */
2015 self->priv->first_buffer = FALSE;
2016 GST_OBJECT_UNLOCK (self);
2018 /* forward the seek upstream */
2019 evdata.event = event;
2020 evdata.flush = flush;
2021 evdata.only_to_active_pads = FALSE;
2022 gst_aggregator_forward_event_to_all_sinkpads (self, &evdata);
2025 if (!evdata.result || !evdata.one_actually_seeked) {
2026 GST_OBJECT_LOCK (self);
2027 priv->flush_seeking = FALSE;
2028 priv->pending_flush_start = FALSE;
2029 GST_OBJECT_UNLOCK (self);
2032 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
2034 return evdata.result;
2038 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
2040 EventData evdata = { 0, };
2042 switch (GST_EVENT_TYPE (event)) {
2043 case GST_EVENT_SEEK:
2044 /* _do_seek() unrefs the event. */
2045 return gst_aggregator_do_seek (self, event);
2046 case GST_EVENT_NAVIGATION:
2047 /* navigation is rather pointless. */
2048 gst_event_unref (event);
2054 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
2055 * they will receive a QOS event that has earliest_time=0 (because we can't
2056 * have negative timestamps), and consider their buffer as too late */
2057 evdata.event = event;
2058 evdata.flush = FALSE;
2059 evdata.only_to_active_pads = GST_EVENT_TYPE (event) == GST_EVENT_QOS;
2060 gst_aggregator_forward_event_to_all_sinkpads (self, &evdata);
2061 return evdata.result;
2065 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
2068 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2070 return klass->src_event (GST_AGGREGATOR (parent), event);
2074 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
2077 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2079 return klass->src_query (GST_AGGREGATOR (parent), query);
2083 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
2084 GstObject * parent, GstPadMode mode, gboolean active)
2086 GstAggregator *self = GST_AGGREGATOR (parent);
2087 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2089 if (klass->src_activate) {
2090 if (klass->src_activate (self, mode, active) == FALSE) {
2095 if (active == TRUE) {
2097 case GST_PAD_MODE_PUSH:
2099 GST_INFO_OBJECT (pad, "Activating pad!");
2100 gst_aggregator_start_srcpad_task (self);
2105 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
2112 GST_INFO_OBJECT (self, "Deactivating srcpad");
2113 gst_aggregator_stop_srcpad_task (self, FALSE);
2119 gst_aggregator_default_sink_query (GstAggregator * self,
2120 GstAggregatorPad * aggpad, GstQuery * query)
2122 GstPad *pad = GST_PAD (aggpad);
2124 if (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION) {
2125 GstQuery *decide_query = NULL;
2126 GstAggregatorClass *agg_class;
2129 GST_OBJECT_LOCK (self);
2131 if (G_UNLIKELY (!aggpad->priv->negotiated)) {
2132 GST_DEBUG_OBJECT (self,
2133 "not negotiated yet, can't answer ALLOCATION query");
2134 PAD_UNLOCK (aggpad);
2135 GST_OBJECT_UNLOCK (self);
2140 if ((decide_query = self->priv->allocation_query))
2141 gst_query_ref (decide_query);
2142 PAD_UNLOCK (aggpad);
2143 GST_OBJECT_UNLOCK (self);
2145 GST_DEBUG_OBJECT (self,
2146 "calling propose allocation with query %" GST_PTR_FORMAT, decide_query);
2148 agg_class = GST_AGGREGATOR_GET_CLASS (self);
2150 /* pass the query to the propose_allocation vmethod if any */
2151 if (agg_class->propose_allocation)
2152 ret = agg_class->propose_allocation (self, aggpad, decide_query, query);
2157 gst_query_unref (decide_query);
2159 GST_DEBUG_OBJECT (self, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
2163 return gst_pad_query_default (pad, GST_OBJECT (self), query);
2167 gst_aggregator_finalize (GObject * object)
2169 GstAggregator *self = (GstAggregator *) object;
2171 g_mutex_clear (&self->priv->src_lock);
2172 g_cond_clear (&self->priv->src_cond);
2174 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
2178 * gst_aggregator_set_latency_property:
2179 * @agg: a #GstAggregator
2180 * @latency: the new latency value (in nanoseconds).
2182 * Sets the new latency value to @latency. This value is used to limit the
2183 * amount of time a pad waits for data to appear before considering the pad
2187 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
2191 g_return_if_fail (GST_IS_AGGREGATOR (self));
2192 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
2195 changed = (self->priv->latency != latency);
2200 GST_OBJECT_LOCK (self);
2201 /* First lock all the pads */
2202 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2203 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2207 self->priv->latency = latency;
2209 SRC_BROADCAST (self);
2211 /* Now wake up the pads */
2212 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2213 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2214 PAD_BROADCAST_EVENT (aggpad);
2215 PAD_UNLOCK (aggpad);
2217 GST_OBJECT_UNLOCK (self);
2223 gst_element_post_message (GST_ELEMENT_CAST (self),
2224 gst_message_new_latency (GST_OBJECT_CAST (self)));
2228 * gst_aggregator_get_latency_property:
2229 * @agg: a #GstAggregator
2231 * Gets the latency value. See gst_aggregator_set_latency for
2234 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
2235 * before a pad is deemed unresponsive. A value of -1 means an
2239 gst_aggregator_get_latency_property (GstAggregator * agg)
2243 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
2245 GST_OBJECT_LOCK (agg);
2246 res = agg->priv->latency;
2247 GST_OBJECT_UNLOCK (agg);
2253 gst_aggregator_set_property (GObject * object, guint prop_id,
2254 const GValue * value, GParamSpec * pspec)
2256 GstAggregator *agg = GST_AGGREGATOR (object);
2260 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
2262 case PROP_START_TIME_SELECTION:
2263 agg->priv->start_time_selection = g_value_get_enum (value);
2265 case PROP_START_TIME:
2266 agg->priv->start_time = g_value_get_uint64 (value);
2269 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2275 gst_aggregator_get_property (GObject * object, guint prop_id,
2276 GValue * value, GParamSpec * pspec)
2278 GstAggregator *agg = GST_AGGREGATOR (object);
2282 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
2284 case PROP_START_TIME_SELECTION:
2285 g_value_set_enum (value, agg->priv->start_time_selection);
2287 case PROP_START_TIME:
2288 g_value_set_uint64 (value, agg->priv->start_time);
2291 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2296 /* GObject vmethods implementations */
2298 gst_aggregator_class_init (GstAggregatorClass * klass)
2300 GObjectClass *gobject_class = (GObjectClass *) klass;
2301 GstElementClass *gstelement_class = (GstElementClass *) klass;
2303 aggregator_parent_class = g_type_class_peek_parent (klass);
2304 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
2306 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
2307 GST_DEBUG_FG_MAGENTA, "GstAggregator");
2309 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
2311 klass->sink_event = gst_aggregator_default_sink_event;
2312 klass->sink_query = gst_aggregator_default_sink_query;
2314 klass->src_event = gst_aggregator_default_src_event;
2315 klass->src_query = gst_aggregator_default_src_query;
2317 klass->create_new_pad = gst_aggregator_default_create_new_pad;
2318 klass->update_src_caps = gst_aggregator_default_update_src_caps;
2319 klass->fixate_src_caps = gst_aggregator_default_fixate_src_caps;
2320 klass->negotiated_src_caps = gst_aggregator_default_negotiated_src_caps;
2322 gstelement_class->request_new_pad =
2323 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
2324 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
2325 gstelement_class->release_pad =
2326 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
2327 gstelement_class->change_state =
2328 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
2330 gobject_class->set_property = gst_aggregator_set_property;
2331 gobject_class->get_property = gst_aggregator_get_property;
2332 gobject_class->finalize = gst_aggregator_finalize;
2334 g_object_class_install_property (gobject_class, PROP_LATENCY,
2335 g_param_spec_int64 ("latency", "Buffer latency",
2336 "Additional latency in live mode to allow upstream "
2337 "to take longer to produce buffers for the current "
2338 "position (in nanoseconds)", 0,
2339 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
2340 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2342 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
2343 g_param_spec_enum ("start-time-selection", "Start Time Selection",
2344 "Decides which start time is output",
2345 gst_aggregator_start_time_selection_get_type (),
2346 DEFAULT_START_TIME_SELECTION,
2347 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2349 g_object_class_install_property (gobject_class, PROP_START_TIME,
2350 g_param_spec_uint64 ("start-time", "Start Time",
2351 "Start time to use if start-time-selection=set", 0,
2353 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2355 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
2356 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_do_events_and_queries);
2360 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
2362 GstPadTemplate *pad_template;
2363 GstAggregatorPrivate *priv;
2365 g_return_if_fail (klass->aggregate != NULL);
2368 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2369 GstAggregatorPrivate);
2374 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2375 g_return_if_fail (pad_template != NULL);
2377 priv->max_padserial = -1;
2378 priv->tags_changed = FALSE;
2380 self->priv->peer_latency_live = FALSE;
2381 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2382 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2383 self->priv->has_peer_latency = FALSE;
2384 gst_aggregator_reset_flow_values (self);
2386 self->srcpad = gst_pad_new_from_template (pad_template, "src");
2388 gst_pad_set_event_function (self->srcpad,
2389 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2390 gst_pad_set_query_function (self->srcpad,
2391 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2392 gst_pad_set_activatemode_function (self->srcpad,
2393 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2395 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2397 self->priv->latency = DEFAULT_LATENCY;
2398 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2399 self->priv->start_time = DEFAULT_START_TIME;
2401 g_mutex_init (&self->priv->src_lock);
2402 g_cond_init (&self->priv->src_cond);
2405 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2406 * method to get to the padtemplates */
2408 gst_aggregator_get_type (void)
2410 static volatile gsize type = 0;
2412 if (g_once_init_enter (&type)) {
2414 static const GTypeInfo info = {
2415 sizeof (GstAggregatorClass),
2418 (GClassInitFunc) gst_aggregator_class_init,
2421 sizeof (GstAggregator),
2423 (GInstanceInitFunc) gst_aggregator_init,
2426 _type = g_type_register_static (GST_TYPE_ELEMENT,
2427 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2428 g_once_init_leave (&type, _type);
2433 /* Must be called with SRC lock and PAD lock held */
2435 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2437 /* Empty queue always has space */
2438 if (aggpad->priv->num_buffers == 0 && aggpad->priv->clipped_buffer == NULL)
2441 /* We also want at least two buffers, one is being processed and one is ready
2442 * for the next iteration when we operate in live mode. */
2443 if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2446 /* zero latency, if there is a buffer, it's full */
2447 if (self->priv->latency == 0)
2450 /* Allow no more buffers than the latency */
2451 return (aggpad->priv->time_level <= self->priv->latency);
2454 /* Must be called with the PAD_LOCK held */
2456 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2458 GstClockTime timestamp;
2460 if (GST_BUFFER_DTS_IS_VALID (buffer))
2461 timestamp = GST_BUFFER_DTS (buffer);
2463 timestamp = GST_BUFFER_PTS (buffer);
2465 if (timestamp == GST_CLOCK_TIME_NONE) {
2467 timestamp = aggpad->priv->head_position;
2469 timestamp = aggpad->priv->tail_position;
2473 if (GST_BUFFER_DURATION_IS_VALID (buffer))
2474 timestamp += GST_BUFFER_DURATION (buffer);
2477 aggpad->priv->head_position = timestamp;
2479 aggpad->priv->tail_position = timestamp;
2481 update_time_level (aggpad, head);
2484 static GstFlowReturn
2485 gst_aggregator_pad_chain_internal (GstAggregator * self,
2486 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2488 GstFlowReturn flow_return;
2489 GstClockTime buf_pts;
2491 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2493 PAD_FLUSH_LOCK (aggpad);
2496 flow_return = aggpad->priv->flow_return;
2497 if (flow_return != GST_FLOW_OK)
2500 if (aggpad->priv->pending_eos == TRUE)
2503 PAD_UNLOCK (aggpad);
2505 buf_pts = GST_BUFFER_PTS (buffer);
2509 GST_OBJECT_LOCK (self);
2512 if (aggpad->priv->first_buffer) {
2513 self->priv->has_peer_latency = FALSE;
2514 aggpad->priv->first_buffer = FALSE;
2517 if (gst_aggregator_pad_has_space (self, aggpad)
2518 && aggpad->priv->flow_return == GST_FLOW_OK) {
2520 g_queue_push_head (&aggpad->priv->data, buffer);
2522 g_queue_push_tail (&aggpad->priv->data, buffer);
2523 apply_buffer (aggpad, buffer, head);
2524 aggpad->priv->num_buffers++;
2526 SRC_BROADCAST (self);
2530 flow_return = aggpad->priv->flow_return;
2531 if (flow_return != GST_FLOW_OK) {
2532 GST_OBJECT_UNLOCK (self);
2536 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2537 GST_OBJECT_UNLOCK (self);
2539 PAD_WAIT_EVENT (aggpad);
2541 PAD_UNLOCK (aggpad);
2544 if (self->priv->first_buffer) {
2545 GstClockTime start_time;
2547 switch (self->priv->start_time_selection) {
2548 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2552 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2553 GST_OBJECT_LOCK (aggpad);
2554 if (aggpad->priv->head_segment.format == GST_FORMAT_TIME) {
2555 start_time = buf_pts;
2556 if (start_time != -1) {
2557 start_time = MAX (start_time, aggpad->priv->head_segment.start);
2559 gst_segment_to_running_time (&aggpad->priv->head_segment,
2560 GST_FORMAT_TIME, start_time);
2564 GST_WARNING_OBJECT (aggpad,
2565 "Ignoring request of selecting the first start time "
2566 "as the segment is a %s segment instead of a time segment",
2567 gst_format_get_name (aggpad->segment.format));
2569 GST_OBJECT_UNLOCK (aggpad);
2571 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2572 start_time = self->priv->start_time;
2573 if (start_time == -1)
2578 if (start_time != -1) {
2579 if (self->segment.position == -1)
2580 self->segment.position = start_time;
2582 self->segment.position = MIN (start_time, self->segment.position);
2584 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2585 GST_TIME_ARGS (start_time));
2589 PAD_UNLOCK (aggpad);
2590 GST_OBJECT_UNLOCK (self);
2593 PAD_FLUSH_UNLOCK (aggpad);
2595 GST_DEBUG_OBJECT (aggpad, "Done chaining");
2600 PAD_UNLOCK (aggpad);
2601 PAD_FLUSH_UNLOCK (aggpad);
2603 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2604 gst_flow_get_name (flow_return));
2606 gst_buffer_unref (buffer);
2611 PAD_UNLOCK (aggpad);
2612 PAD_FLUSH_UNLOCK (aggpad);
2614 gst_buffer_unref (buffer);
2615 GST_DEBUG_OBJECT (aggpad, "We are EOS already...");
2617 return GST_FLOW_EOS;
2620 static GstFlowReturn
2621 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2623 return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2624 GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2628 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2631 GstAggregator *self = GST_AGGREGATOR (parent);
2632 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2633 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2635 if (GST_QUERY_IS_SERIALIZED (query)) {
2637 gboolean ret = FALSE;
2642 if (aggpad->priv->flow_return != GST_FLOW_OK) {
2647 g_queue_push_head (&aggpad->priv->data, query);
2648 SRC_BROADCAST (self);
2651 while (!gst_aggregator_pad_queue_is_empty (aggpad)
2652 && aggpad->priv->flow_return == GST_FLOW_OK) {
2653 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2654 PAD_WAIT_EVENT (aggpad);
2657 s = gst_query_writable_structure (query);
2658 if (gst_structure_get_boolean (s, "gst-aggregator-retval", &ret))
2659 gst_structure_remove_field (s, "gst-aggregator-retval");
2661 g_queue_remove (&aggpad->priv->data, query);
2663 if (aggpad->priv->flow_return != GST_FLOW_OK)
2666 PAD_UNLOCK (aggpad);
2671 return klass->sink_query (self, aggpad, query);
2674 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2675 gst_flow_get_name (aggpad->priv->flow_return));
2676 PAD_UNLOCK (aggpad);
2681 static GstFlowReturn
2682 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2685 GstFlowReturn ret = GST_FLOW_OK;
2686 GstAggregator *self = GST_AGGREGATOR (parent);
2687 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2688 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2690 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
2691 /* && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE */ ) {
2695 if (aggpad->priv->flow_return != GST_FLOW_OK
2696 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2697 ret = aggpad->priv->flow_return;
2701 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2702 GST_OBJECT_LOCK (aggpad);
2703 gst_event_copy_segment (event, &aggpad->priv->head_segment);
2704 aggpad->priv->head_position = aggpad->priv->head_segment.position;
2705 update_time_level (aggpad, TRUE);
2706 GST_OBJECT_UNLOCK (aggpad);
2709 if (GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2710 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT,
2712 g_queue_push_head (&aggpad->priv->data, event);
2714 SRC_BROADCAST (self);
2716 PAD_UNLOCK (aggpad);
2721 if (!klass->sink_event (self, aggpad, event)) {
2722 /* Copied from GstPad to convert boolean to a GstFlowReturn in
2723 * the event handling func */
2724 ret = GST_FLOW_ERROR;
2731 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2732 gst_flow_get_name (aggpad->priv->flow_return));
2733 PAD_UNLOCK (aggpad);
2735 if (GST_EVENT_IS_STICKY (event))
2736 gst_pad_store_sticky_event (pad, event);
2737 gst_event_unref (event);
2743 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2744 GstObject * parent, GstPadMode mode, gboolean active)
2746 GstAggregator *self = GST_AGGREGATOR (parent);
2747 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2749 if (active == FALSE) {
2751 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2752 SRC_BROADCAST (self);
2756 aggpad->priv->flow_return = GST_FLOW_OK;
2757 PAD_BROADCAST_EVENT (aggpad);
2758 PAD_UNLOCK (aggpad);
2764 /***********************************
2765 * GstAggregatorPad implementation *
2766 ************************************/
2767 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2770 gst_aggregator_pad_constructed (GObject * object)
2772 GstPad *pad = GST_PAD (object);
2774 gst_pad_set_chain_function (pad,
2775 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2776 gst_pad_set_event_full_function_full (pad,
2777 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
2778 gst_pad_set_query_function (pad,
2779 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2780 gst_pad_set_activatemode_function (pad,
2781 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2785 gst_aggregator_pad_finalize (GObject * object)
2787 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2789 g_cond_clear (&pad->priv->event_cond);
2790 g_mutex_clear (&pad->priv->flush_lock);
2791 g_mutex_clear (&pad->priv->lock);
2793 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2797 gst_aggregator_pad_dispose (GObject * object)
2799 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2801 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2803 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2807 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2809 GObjectClass *gobject_class = (GObjectClass *) klass;
2811 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2813 gobject_class->constructed = gst_aggregator_pad_constructed;
2814 gobject_class->finalize = gst_aggregator_pad_finalize;
2815 gobject_class->dispose = gst_aggregator_pad_dispose;
2819 gst_aggregator_pad_init (GstAggregatorPad * pad)
2822 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2823 GstAggregatorPadPrivate);
2825 g_queue_init (&pad->priv->data);
2826 g_cond_init (&pad->priv->event_cond);
2828 g_mutex_init (&pad->priv->flush_lock);
2829 g_mutex_init (&pad->priv->lock);
2831 gst_aggregator_pad_reset_unlocked (pad);
2832 pad->priv->negotiated = FALSE;
2835 /* Must be called with the PAD_LOCK held */
2837 gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad)
2839 pad->priv->num_buffers--;
2840 GST_TRACE_OBJECT (pad, "Consuming buffer");
2841 if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
2842 pad->priv->pending_eos = FALSE;
2843 pad->priv->eos = TRUE;
2845 PAD_BROADCAST_EVENT (pad);
2848 /* Must be called with the PAD_LOCK held */
2850 gst_aggregator_pad_clip_buffer_unlocked (GstAggregatorPad * pad)
2852 GstAggregator *self = NULL;
2853 GstAggregatorClass *aggclass = NULL;
2854 GstBuffer *buffer = NULL;
2856 while (pad->priv->clipped_buffer == NULL &&
2857 GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->data))) {
2858 buffer = g_queue_pop_tail (&pad->priv->data);
2860 apply_buffer (pad, buffer, FALSE);
2862 /* We only take the parent here so that it's not taken if the buffer is
2863 * already clipped or if the queue is empty.
2866 self = GST_AGGREGATOR (gst_pad_get_parent_element (GST_PAD (pad)));
2868 gst_buffer_unref (buffer);
2872 aggclass = GST_AGGREGATOR_GET_CLASS (self);
2875 if (aggclass->clip) {
2876 GST_TRACE_OBJECT (pad, "Clipping: %" GST_PTR_FORMAT, buffer);
2878 buffer = aggclass->clip (self, pad, buffer);
2880 if (buffer == NULL) {
2881 gst_aggregator_pad_buffer_consumed (pad);
2882 GST_TRACE_OBJECT (pad, "Clipping consumed the buffer");
2886 pad->priv->clipped_buffer = buffer;
2890 gst_object_unref (self);
2894 * gst_aggregator_pad_steal_buffer:
2895 * @pad: the pad to get buffer from
2897 * Steal the ref to the buffer currently queued in @pad.
2899 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2900 * queued. You should unref the buffer after usage.
2903 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2909 gst_aggregator_pad_clip_buffer_unlocked (pad);
2911 buffer = pad->priv->clipped_buffer;
2914 pad->priv->clipped_buffer = NULL;
2915 gst_aggregator_pad_buffer_consumed (pad);
2916 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2925 * gst_aggregator_pad_drop_buffer:
2926 * @pad: the pad where to drop any pending buffer
2928 * Drop the buffer currently queued in @pad.
2930 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2933 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2937 buf = gst_aggregator_pad_steal_buffer (pad);
2942 gst_buffer_unref (buf);
2947 * gst_aggregator_pad_get_buffer:
2948 * @pad: the pad to get buffer from
2950 * Returns: (transfer full): A reference to the buffer in @pad or
2951 * NULL if no buffer was queued. You should unref the buffer after
2955 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2961 gst_aggregator_pad_clip_buffer_unlocked (pad);
2963 if (pad->priv->clipped_buffer) {
2964 buffer = gst_buffer_ref (pad->priv->clipped_buffer);
2974 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2979 is_eos = pad->priv->eos;
2986 * gst_aggregator_merge_tags:
2987 * @self: a #GstAggregator
2988 * @tags: a #GstTagList to merge
2989 * @mode: the #GstTagMergeMode to use
2991 * Adds tags to so-called pending tags, which will be processed
2992 * before pushing out data downstream.
2994 * Note that this is provided for convenience, and the subclass is
2995 * not required to use this and can still do tag handling on its own.
3000 gst_aggregator_merge_tags (GstAggregator * self,
3001 const GstTagList * tags, GstTagMergeMode mode)
3005 g_return_if_fail (GST_IS_AGGREGATOR (self));
3006 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
3008 /* FIXME Check if we can use OBJECT lock here! */
3009 GST_OBJECT_LOCK (self);
3011 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
3012 otags = self->priv->tags;
3013 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
3015 gst_tag_list_unref (otags);
3016 self->priv->tags_changed = TRUE;
3017 GST_OBJECT_UNLOCK (self);
3021 * gst_aggregator_set_latency:
3022 * @self: a #GstAggregator
3023 * @min_latency: minimum latency
3024 * @max_latency: maximum latency
3026 * Lets #GstAggregator sub-classes tell the baseclass what their internal
3027 * latency is. Will also post a LATENCY message on the bus so the pipeline
3028 * can reconfigure its global latency.
3031 gst_aggregator_set_latency (GstAggregator * self,
3032 GstClockTime min_latency, GstClockTime max_latency)
3034 gboolean changed = FALSE;
3036 g_return_if_fail (GST_IS_AGGREGATOR (self));
3037 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
3038 g_return_if_fail (max_latency >= min_latency);
3041 if (self->priv->sub_latency_min != min_latency) {
3042 self->priv->sub_latency_min = min_latency;
3045 if (self->priv->sub_latency_max != max_latency) {
3046 self->priv->sub_latency_max = max_latency;
3051 SRC_BROADCAST (self);
3055 gst_element_post_message (GST_ELEMENT_CAST (self),
3056 gst_message_new_latency (GST_OBJECT_CAST (self)));
3061 * gst_aggregator_get_buffer_pool:
3062 * @self: a #GstAggregator
3064 * Returns: (transfer full): the instance of the #GstBufferPool used
3065 * by @trans; free it after use it
3068 gst_aggregator_get_buffer_pool (GstAggregator * self)
3070 GstBufferPool *pool;
3072 g_return_val_if_fail (GST_IS_AGGREGATOR (self), NULL);
3074 GST_OBJECT_LOCK (self);
3075 pool = self->priv->pool;
3077 gst_object_ref (pool);
3078 GST_OBJECT_UNLOCK (self);
3084 * gst_aggregator_get_allocator:
3085 * @self: a #GstAggregator
3086 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3088 * @params: (out) (allow-none) (transfer full): the
3089 * #GstAllocationParams of @allocator
3091 * Lets #GstAggregator sub-classes get the memory @allocator
3092 * acquired by the base class and its @params.
3094 * Unref the @allocator after use it.
3097 gst_aggregator_get_allocator (GstAggregator * self,
3098 GstAllocator ** allocator, GstAllocationParams * params)
3100 g_return_if_fail (GST_IS_AGGREGATOR (self));
3103 *allocator = self->priv->allocator ?
3104 gst_object_ref (self->priv->allocator) : NULL;
3107 *params = self->priv->allocation_params;