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, the 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
360 * Returns: %FALSE if there are no sinkpads or if @func returned %FALSE
363 gst_aggregator_iterate_sinkpads (GstAggregator * self,
364 GstAggregatorPadForeachFunc func, gpointer user_data)
366 gboolean result = FALSE;
368 gboolean done = FALSE;
369 GValue item = { 0, };
370 GList *seen_pads = NULL;
372 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
378 switch (gst_iterator_next (iter, &item)) {
379 case GST_ITERATOR_OK:
381 GstAggregatorPad *pad;
383 pad = g_value_get_object (&item);
385 /* if already pushed, skip. FIXME, find something faster to tag pads */
386 if (pad == NULL || g_list_find (seen_pads, pad)) {
387 g_value_reset (&item);
391 GST_LOG_OBJECT (pad, "calling function %s on pad",
392 GST_DEBUG_FUNCPTR_NAME (func));
394 result = func (self, pad, user_data);
398 seen_pads = g_list_prepend (seen_pads, pad);
400 g_value_reset (&item);
403 case GST_ITERATOR_RESYNC:
404 gst_iterator_resync (iter);
406 case GST_ITERATOR_ERROR:
407 GST_ERROR_OBJECT (self,
408 "Could not iterate over internally linked pads");
411 case GST_ITERATOR_DONE:
416 g_value_unset (&item);
417 gst_iterator_free (iter);
419 if (seen_pads == NULL) {
420 GST_DEBUG_OBJECT (self, "No pad seen");
424 g_list_free (seen_pads);
431 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
433 return (g_queue_peek_tail (&pad->priv->buffers) == NULL &&
434 pad->priv->clipped_buffer == NULL);
438 gst_aggregator_check_pads_ready (GstAggregator * self)
440 GstAggregatorPad *pad;
442 gboolean have_buffer = TRUE;
443 gboolean have_event = FALSE;
445 GST_LOG_OBJECT (self, "checking pads");
447 GST_OBJECT_LOCK (self);
449 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
450 if (sinkpads == NULL)
453 for (l = sinkpads; l != NULL; l = l->next) {
458 if (pad->priv->num_buffers == 0) {
459 if (!gst_aggregator_pad_queue_is_empty (pad))
461 if (!pad->priv->eos) {
464 /* If not live we need data on all pads, so leave the loop */
465 if (!self->priv->peer_latency_live) {
470 } else if (self->priv->peer_latency_live) {
471 /* In live mode, having a single pad with buffers is enough to
472 * generate a start time from it. In non-live mode all pads need
475 self->priv->first_buffer = FALSE;
481 if (!have_buffer && !have_event)
485 self->priv->first_buffer = FALSE;
487 GST_OBJECT_UNLOCK (self);
488 GST_LOG_OBJECT (self, "pads are ready");
493 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
494 GST_OBJECT_UNLOCK (self);
500 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet,"
501 " but waking up for serialized event");
503 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
504 GST_OBJECT_UNLOCK (self);
510 gst_aggregator_reset_flow_values (GstAggregator * self)
512 GST_OBJECT_LOCK (self);
513 self->priv->send_stream_start = TRUE;
514 self->priv->send_segment = TRUE;
515 gst_segment_init (&self->segment, GST_FORMAT_TIME);
516 self->priv->first_buffer = TRUE;
517 GST_OBJECT_UNLOCK (self);
521 gst_aggregator_push_mandatory_events (GstAggregator * self)
523 GstAggregatorPrivate *priv = self->priv;
524 GstEvent *segment = NULL;
525 GstEvent *tags = NULL;
527 if (self->priv->send_stream_start) {
530 GST_INFO_OBJECT (self, "pushing stream start");
531 /* stream-start (FIXME: create id based on input ids) */
532 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
533 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
534 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
536 self->priv->send_stream_start = FALSE;
539 if (self->priv->srccaps) {
541 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
542 self->priv->srccaps);
543 if (!gst_pad_push_event (self->srcpad,
544 gst_event_new_caps (self->priv->srccaps))) {
545 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
547 gst_caps_unref (self->priv->srccaps);
548 self->priv->srccaps = NULL;
551 GST_OBJECT_LOCK (self);
552 if (self->priv->send_segment && !self->priv->flush_seeking) {
553 segment = gst_event_new_segment (&self->segment);
555 if (!self->priv->seqnum)
556 self->priv->seqnum = gst_event_get_seqnum (segment);
558 gst_event_set_seqnum (segment, self->priv->seqnum);
559 self->priv->send_segment = FALSE;
561 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
564 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
565 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
566 priv->tags_changed = FALSE;
568 GST_OBJECT_UNLOCK (self);
571 gst_pad_push_event (self->srcpad, segment);
573 gst_pad_push_event (self->srcpad, tags);
578 * gst_aggregator_set_src_caps:
579 * @self: The #GstAggregator
580 * @caps: The #GstCaps to set on the src pad.
582 * Sets the caps to be used on the src pad.
585 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
587 GST_PAD_STREAM_LOCK (self->srcpad);
588 gst_caps_replace (&self->priv->srccaps, caps);
589 gst_aggregator_push_mandatory_events (self);
590 GST_PAD_STREAM_UNLOCK (self->srcpad);
594 * gst_aggregator_finish_buffer:
595 * @self: The #GstAggregator
596 * @buffer: (transfer full): the #GstBuffer to push.
598 * This method will push the provided output buffer downstream. If needed,
599 * mandatory events such as stream-start, caps, and segment events will be
600 * sent before pushing the buffer.
603 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
605 gst_aggregator_push_mandatory_events (self);
607 GST_OBJECT_LOCK (self);
608 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
609 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
610 GST_OBJECT_UNLOCK (self);
611 return gst_pad_push (self->srcpad, buffer);
613 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
614 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
615 GST_OBJECT_UNLOCK (self);
616 gst_buffer_unref (buffer);
622 gst_aggregator_push_eos (GstAggregator * self)
625 gst_aggregator_push_mandatory_events (self);
627 event = gst_event_new_eos ();
629 GST_OBJECT_LOCK (self);
630 self->priv->send_eos = FALSE;
631 gst_event_set_seqnum (event, self->priv->seqnum);
632 GST_OBJECT_UNLOCK (self);
634 gst_pad_push_event (self->srcpad, event);
638 gst_aggregator_get_next_time (GstAggregator * self)
640 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
642 if (klass->get_next_time)
643 return klass->get_next_time (self);
645 return GST_CLOCK_TIME_NONE;
649 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
651 GstClockTime latency;
659 latency = gst_aggregator_get_latency_unlocked (self);
661 if (gst_aggregator_check_pads_ready (self)) {
662 GST_DEBUG_OBJECT (self, "all pads have data");
668 /* Before waiting, check if we're actually still running */
669 if (!self->priv->running || !self->priv->send_eos) {
675 start = gst_aggregator_get_next_time (self);
677 /* If we're not live, or if we use the running time
678 * of the first buffer as start time, we wait until
679 * all pads have buffers.
680 * Otherwise (i.e. if we are live!), we wait on the clock
681 * and if a pad does not have a buffer in time we ignore
684 GST_OBJECT_LOCK (self);
685 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
686 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
687 !GST_CLOCK_TIME_IS_VALID (start) ||
688 (self->priv->first_buffer
689 && self->priv->start_time_selection ==
690 GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
691 /* We wake up here when something happened, and below
692 * then check if we're ready now. If we return FALSE,
693 * we will be directly called again.
695 GST_OBJECT_UNLOCK (self);
698 GstClockTime base_time, time;
700 GstClockReturn status;
701 GstClockTimeDiff jitter;
703 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
704 GST_TIME_ARGS (start));
706 base_time = GST_ELEMENT_CAST (self)->base_time;
707 clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
708 GST_OBJECT_UNLOCK (self);
710 time = base_time + start;
713 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
714 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
715 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
716 GST_TIME_ARGS (time),
717 GST_TIME_ARGS (base_time),
718 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
719 GST_TIME_ARGS (gst_clock_get_time (clock)));
721 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
722 gst_object_unref (clock);
726 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
729 if (self->priv->aggregate_id) {
730 gst_clock_id_unref (self->priv->aggregate_id);
731 self->priv->aggregate_id = NULL;
734 GST_DEBUG_OBJECT (self,
735 "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
736 status, GST_STIME_ARGS (jitter));
739 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
746 res = gst_aggregator_check_pads_ready (self);
753 check_events (GstAggregator * self, GstAggregatorPad * pad, gpointer user_data)
755 GstEvent *event = NULL;
756 GstQuery *query = NULL;
757 GstAggregatorClass *klass = NULL;
758 gboolean *processed_event = user_data;
764 if (pad->priv->num_buffers == 0 && pad->priv->pending_eos) {
765 pad->priv->pending_eos = FALSE;
766 pad->priv->eos = TRUE;
768 if (pad->priv->clipped_buffer == NULL &&
769 !GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->buffers))) {
770 if (GST_IS_EVENT (g_queue_peek_tail (&pad->priv->buffers)))
771 event = gst_event_ref (g_queue_peek_tail (&pad->priv->buffers));
772 if (GST_IS_QUERY (g_queue_peek_tail (&pad->priv->buffers)))
773 query = g_queue_peek_tail (&pad->priv->buffers);
776 if (event || query) {
780 *processed_event = TRUE;
782 klass = GST_AGGREGATOR_GET_CLASS (self);
785 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
786 gst_event_ref (event);
787 ret = klass->sink_event (self, pad, event);
790 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS)
791 pad->priv->negotiated = ret;
792 if (g_queue_peek_tail (&pad->priv->buffers) == event)
793 gst_event_unref (g_queue_pop_tail (&pad->priv->buffers));
794 gst_event_unref (event);
798 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, query);
799 ret = klass->sink_query (self, pad, query);
802 if (g_queue_peek_tail (&pad->priv->buffers) == query) {
805 s = gst_query_writable_structure (query);
806 gst_structure_set (s, "gst-aggregator-retval", G_TYPE_BOOLEAN, ret,
808 g_queue_pop_tail (&pad->priv->buffers);
812 PAD_BROADCAST_EVENT (pad);
815 } while (event != NULL);
821 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
822 GstFlowReturn flow_return, gboolean full)
827 if (flow_return == GST_FLOW_NOT_LINKED)
828 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
830 aggpad->priv->flow_return = flow_return;
832 item = g_queue_peek_head_link (&aggpad->priv->buffers);
834 GList *next = item->next;
836 /* In partial flush, we do like the pad, we get rid of non-sticky events
839 if (full || GST_IS_BUFFER (item->data) ||
840 GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
841 GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
842 !GST_EVENT_IS_STICKY (item->data)) {
843 if (!GST_IS_QUERY (item->data))
844 gst_mini_object_unref (item->data);
845 g_queue_delete_link (&aggpad->priv->buffers, item);
849 aggpad->priv->num_buffers = 0;
850 gst_buffer_replace (&aggpad->priv->clipped_buffer, NULL);
852 PAD_BROADCAST_EVENT (aggpad);
857 gst_aggregator_default_update_src_caps (GstAggregator * agg, GstCaps * caps,
860 *ret = gst_caps_ref (caps);
866 gst_aggregator_default_fixate_src_caps (GstAggregator * agg, GstCaps * caps)
868 caps = gst_caps_fixate (caps);
874 gst_aggregator_default_negotiated_src_caps (GstAggregator * agg, GstCaps * caps)
880 /* takes ownership of the pool, allocator and query */
882 gst_aggregator_set_allocation (GstAggregator * self,
883 GstBufferPool * pool, GstAllocator * allocator,
884 GstAllocationParams * params, GstQuery * query)
886 GstAllocator *oldalloc;
887 GstBufferPool *oldpool;
890 GST_DEBUG ("storing allocation query");
892 GST_OBJECT_LOCK (self);
893 oldpool = self->priv->pool;
894 self->priv->pool = pool;
896 oldalloc = self->priv->allocator;
897 self->priv->allocator = allocator;
899 oldquery = self->priv->allocation_query;
900 self->priv->allocation_query = query;
903 self->priv->allocation_params = *params;
905 gst_allocation_params_init (&self->priv->allocation_params);
906 GST_OBJECT_UNLOCK (self);
909 GST_DEBUG_OBJECT (self, "deactivating old pool %p", oldpool);
910 gst_buffer_pool_set_active (oldpool, FALSE);
911 gst_object_unref (oldpool);
914 gst_object_unref (oldalloc);
917 gst_query_unref (oldquery);
924 gst_aggregator_decide_allocation (GstAggregator * self, GstQuery * query)
926 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
928 if (aggclass->decide_allocation)
929 if (!aggclass->decide_allocation (self, query))
936 gst_aggregator_do_allocation (GstAggregator * self, GstCaps * caps)
939 gboolean result = TRUE;
940 GstBufferPool *pool = NULL;
941 GstAllocator *allocator;
942 GstAllocationParams params;
944 /* find a pool for the negotiated caps now */
945 GST_DEBUG_OBJECT (self, "doing allocation query");
946 query = gst_query_new_allocation (caps, TRUE);
947 if (!gst_pad_peer_query (self->srcpad, query)) {
948 /* not a problem, just debug a little */
949 GST_DEBUG_OBJECT (self, "peer ALLOCATION query failed");
952 GST_DEBUG_OBJECT (self, "calling decide_allocation");
953 result = gst_aggregator_decide_allocation (self, query);
955 GST_DEBUG_OBJECT (self, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
959 goto no_decide_allocation;
961 /* we got configuration from our peer or the decide_allocation method,
963 if (gst_query_get_n_allocation_params (query) > 0) {
964 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
967 gst_allocation_params_init (¶ms);
970 if (gst_query_get_n_allocation_pools (query) > 0)
971 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
975 gst_aggregator_set_allocation (self, pool, allocator, ¶ms, query);
980 no_decide_allocation:
982 GST_WARNING_OBJECT (self, "Failed to decide allocation");
983 gst_query_unref (query);
990 /* WITH SRC_LOCK held */
992 gst_aggregator_update_src_caps (GstAggregator * self)
994 GstAggregatorClass *agg_klass = GST_AGGREGATOR_GET_CLASS (self);
995 GstCaps *downstream_caps, *template_caps, *caps = NULL;
996 GstFlowReturn ret = GST_FLOW_OK;
998 template_caps = gst_pad_get_pad_template_caps (self->srcpad);
999 downstream_caps = gst_pad_peer_query_caps (self->srcpad, template_caps);
1001 if (gst_caps_is_empty (downstream_caps)) {
1002 GST_INFO_OBJECT (self, "Downstream caps (%"
1003 GST_PTR_FORMAT ") not compatible with pad template caps (%"
1004 GST_PTR_FORMAT ")", downstream_caps, template_caps);
1005 ret = GST_FLOW_NOT_NEGOTIATED;
1009 g_assert (agg_klass->update_src_caps);
1010 GST_DEBUG_OBJECT (self, "updating caps from %" GST_PTR_FORMAT,
1012 ret = agg_klass->update_src_caps (self, downstream_caps, &caps);
1013 if (ret < GST_FLOW_OK) {
1014 GST_WARNING_OBJECT (self, "Subclass failed to update provided caps");
1017 if ((caps == NULL || gst_caps_is_empty (caps)) && ret >= GST_FLOW_OK) {
1018 ret = GST_FLOW_NOT_NEGOTIATED;
1021 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1023 #ifdef GST_ENABLE_EXTRA_CHECKS
1024 if (!gst_caps_is_subset (caps, template_caps)) {
1025 GstCaps *intersection;
1027 GST_ERROR_OBJECT (self,
1028 "update_src_caps returned caps %" GST_PTR_FORMAT
1029 " which are not a real subset of the template caps %"
1030 GST_PTR_FORMAT, caps, template_caps);
1031 g_warning ("%s: update_src_caps returned caps which are not a real "
1032 "subset of the filter caps", GST_ELEMENT_NAME (self));
1035 gst_caps_intersect_full (template_caps, caps, GST_CAPS_INTERSECT_FIRST);
1036 gst_caps_unref (caps);
1037 caps = intersection;
1041 if (gst_caps_is_any (caps)) {
1045 if (!gst_caps_is_fixed (caps)) {
1046 g_assert (agg_klass->fixate_src_caps);
1048 GST_DEBUG_OBJECT (self, "fixate caps from %" GST_PTR_FORMAT, caps);
1049 if (!(caps = agg_klass->fixate_src_caps (self, caps))) {
1050 GST_WARNING_OBJECT (self, "Subclass failed to fixate provided caps");
1051 ret = GST_FLOW_NOT_NEGOTIATED;
1054 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1057 if (agg_klass->negotiated_src_caps) {
1058 if (!agg_klass->negotiated_src_caps (self, caps)) {
1059 GST_WARNING_OBJECT (self, "Subclass failed to accept negotiated caps");
1060 ret = GST_FLOW_NOT_NEGOTIATED;
1065 gst_aggregator_set_src_caps (self, caps);
1067 if (!gst_aggregator_do_allocation (self, caps)) {
1068 GST_WARNING_OBJECT (self, "Allocation negotiation failed");
1069 ret = GST_FLOW_NOT_NEGOTIATED;
1073 gst_caps_unref (downstream_caps);
1074 gst_caps_unref (template_caps);
1077 gst_caps_unref (caps);
1083 gst_aggregator_aggregate_func (GstAggregator * self)
1085 GstAggregatorPrivate *priv = self->priv;
1086 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1087 gboolean timeout = FALSE;
1089 if (self->priv->running == FALSE) {
1090 GST_DEBUG_OBJECT (self, "Not running anymore");
1094 GST_LOG_OBJECT (self, "Checking aggregate");
1095 while (priv->send_eos && priv->running) {
1096 GstFlowReturn flow_return = GST_FLOW_OK;
1097 gboolean processed_event = FALSE;
1099 gst_aggregator_iterate_sinkpads (self, check_events, NULL);
1101 if (!gst_aggregator_wait_and_check (self, &timeout))
1104 gst_aggregator_iterate_sinkpads (self, check_events, &processed_event);
1105 if (processed_event)
1108 if (gst_pad_check_reconfigure (GST_AGGREGATOR_SRC_PAD (self))) {
1109 flow_return = gst_aggregator_update_src_caps (self);
1110 if (flow_return != GST_FLOW_OK)
1111 gst_pad_mark_reconfigure (GST_AGGREGATOR_SRC_PAD (self));
1114 if (timeout || flow_return >= GST_FLOW_OK) {
1115 GST_TRACE_OBJECT (self, "Actually aggregating!");
1116 flow_return = klass->aggregate (self, timeout);
1119 if (flow_return == GST_AGGREGATOR_FLOW_NEED_DATA)
1122 GST_OBJECT_LOCK (self);
1123 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
1124 /* We don't want to set the pads to flushing, but we want to
1125 * stop the thread, so just break here */
1126 GST_OBJECT_UNLOCK (self);
1129 GST_OBJECT_UNLOCK (self);
1131 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
1132 gst_aggregator_push_eos (self);
1135 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
1137 if (flow_return != GST_FLOW_OK) {
1140 GST_OBJECT_LOCK (self);
1141 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
1142 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1144 gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
1146 GST_OBJECT_UNLOCK (self);
1151 /* Pause the task here, the only ways to get here are:
1152 * 1) We're stopping, in which case the task is stopped anyway
1153 * 2) We got a flow error above, in which case it might take
1154 * some time to forward the flow return upstream and we
1155 * would otherwise call the task function over and over
1156 * again without doing anything
1158 gst_pad_pause_task (self->srcpad);
1162 gst_aggregator_start (GstAggregator * self)
1164 GstAggregatorClass *klass;
1167 self->priv->send_stream_start = TRUE;
1168 self->priv->send_segment = TRUE;
1169 self->priv->send_eos = TRUE;
1170 self->priv->srccaps = NULL;
1172 gst_aggregator_set_allocation (self, NULL, NULL, NULL, NULL);
1174 klass = GST_AGGREGATOR_GET_CLASS (self);
1177 result = klass->start (self);
1185 _check_pending_flush_stop (GstAggregatorPad * pad)
1190 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
1197 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
1199 gboolean res = TRUE;
1201 GST_INFO_OBJECT (self, "%s srcpad task",
1202 flush_start ? "Pausing" : "Stopping");
1205 self->priv->running = FALSE;
1206 SRC_BROADCAST (self);
1210 res = gst_pad_push_event (self->srcpad, flush_start);
1213 gst_pad_stop_task (self->srcpad);
1219 gst_aggregator_start_srcpad_task (GstAggregator * self)
1221 GST_INFO_OBJECT (self, "Starting srcpad task");
1223 self->priv->running = TRUE;
1224 gst_pad_start_task (GST_PAD (self->srcpad),
1225 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
1228 static GstFlowReturn
1229 gst_aggregator_flush (GstAggregator * self)
1231 GstFlowReturn ret = GST_FLOW_OK;
1232 GstAggregatorPrivate *priv = self->priv;
1233 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1235 GST_DEBUG_OBJECT (self, "Flushing everything");
1236 GST_OBJECT_LOCK (self);
1237 priv->send_segment = TRUE;
1238 priv->flush_seeking = FALSE;
1239 priv->tags_changed = FALSE;
1240 GST_OBJECT_UNLOCK (self);
1242 ret = klass->flush (self);
1248 /* Called with GstAggregator's object lock held */
1251 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
1254 GstAggregatorPad *tmppad;
1256 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
1257 tmppad = (GstAggregatorPad *) tmp->data;
1259 if (_check_pending_flush_stop (tmppad) == FALSE) {
1260 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
1261 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
1270 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
1273 GstAggregatorPrivate *priv = self->priv;
1274 GstAggregatorPadPrivate *padpriv = aggpad->priv;
1276 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
1278 PAD_FLUSH_LOCK (aggpad);
1280 if (padpriv->pending_flush_start) {
1281 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
1283 padpriv->pending_flush_start = FALSE;
1284 padpriv->pending_flush_stop = TRUE;
1286 PAD_UNLOCK (aggpad);
1288 GST_OBJECT_LOCK (self);
1289 if (priv->flush_seeking) {
1290 /* If flush_seeking we forward the first FLUSH_START */
1291 if (priv->pending_flush_start) {
1292 priv->pending_flush_start = FALSE;
1293 GST_OBJECT_UNLOCK (self);
1295 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
1296 gst_aggregator_stop_srcpad_task (self, event);
1298 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
1299 GST_PAD_STREAM_LOCK (self->srcpad);
1300 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
1303 GST_OBJECT_UNLOCK (self);
1304 gst_event_unref (event);
1307 GST_OBJECT_UNLOCK (self);
1308 gst_event_unref (event);
1310 PAD_FLUSH_UNLOCK (aggpad);
1313 /* Must be called with the the PAD_LOCK held */
1315 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1318 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->head_position) &&
1319 aggpad->priv->head_segment.format == GST_FORMAT_TIME)
1320 aggpad->priv->head_time =
1321 gst_segment_to_running_time (&aggpad->priv->head_segment,
1322 GST_FORMAT_TIME, aggpad->priv->head_position);
1324 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
1326 if (!GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_time))
1327 aggpad->priv->tail_time = aggpad->priv->head_time;
1329 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_position) &&
1330 aggpad->segment.format == GST_FORMAT_TIME)
1331 aggpad->priv->tail_time =
1332 gst_segment_to_running_time (&aggpad->segment,
1333 GST_FORMAT_TIME, aggpad->priv->tail_position);
1335 aggpad->priv->tail_time = aggpad->priv->head_time;
1338 if (aggpad->priv->head_time == GST_CLOCK_TIME_NONE ||
1339 aggpad->priv->tail_time == GST_CLOCK_TIME_NONE) {
1340 aggpad->priv->time_level = 0;
1344 if (aggpad->priv->tail_time > aggpad->priv->head_time)
1345 aggpad->priv->time_level = 0;
1347 aggpad->priv->time_level = aggpad->priv->head_time -
1348 aggpad->priv->tail_time;
1352 /* GstAggregator vmethods default implementations */
1354 gst_aggregator_default_sink_event (GstAggregator * self,
1355 GstAggregatorPad * aggpad, GstEvent * event)
1357 gboolean res = TRUE;
1358 GstPad *pad = GST_PAD (aggpad);
1359 GstAggregatorPrivate *priv = self->priv;
1361 GST_DEBUG_OBJECT (aggpad, "Got event: %" GST_PTR_FORMAT, event);
1363 switch (GST_EVENT_TYPE (event)) {
1364 case GST_EVENT_FLUSH_START:
1366 gst_aggregator_flush_start (self, aggpad, event);
1367 /* We forward only in one case: right after flush_seeking */
1371 case GST_EVENT_FLUSH_STOP:
1373 gst_aggregator_pad_flush (aggpad, self);
1374 GST_OBJECT_LOCK (self);
1375 if (priv->flush_seeking) {
1376 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1377 if (gst_aggregator_all_flush_stop_received_locked (self)) {
1378 GST_OBJECT_UNLOCK (self);
1379 /* That means we received FLUSH_STOP/FLUSH_STOP on
1380 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1381 gst_aggregator_flush (self);
1382 gst_pad_push_event (self->srcpad, event);
1385 priv->send_eos = TRUE;
1386 SRC_BROADCAST (self);
1389 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1390 GST_PAD_STREAM_UNLOCK (self->srcpad);
1391 gst_aggregator_start_srcpad_task (self);
1393 GST_OBJECT_UNLOCK (self);
1396 GST_OBJECT_UNLOCK (self);
1399 /* We never forward the event */
1404 /* We still have a buffer, and we don't want the subclass to have to
1405 * check for it. Mark pending_eos, eos will be set when steal_buffer is
1410 if (aggpad->priv->num_buffers == 0) {
1411 aggpad->priv->eos = TRUE;
1413 aggpad->priv->pending_eos = TRUE;
1415 PAD_UNLOCK (aggpad);
1417 SRC_BROADCAST (self);
1421 case GST_EVENT_SEGMENT:
1424 GST_OBJECT_LOCK (aggpad);
1425 gst_event_copy_segment (event, &aggpad->segment);
1426 /* We've got a new segment, tail_position is now meaningless
1427 * and may interfere with the time_level calculation
1429 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
1430 update_time_level (aggpad, FALSE);
1431 GST_OBJECT_UNLOCK (aggpad);
1432 PAD_UNLOCK (aggpad);
1434 GST_OBJECT_LOCK (self);
1435 self->priv->seqnum = gst_event_get_seqnum (event);
1436 GST_OBJECT_UNLOCK (self);
1439 case GST_EVENT_STREAM_START:
1445 GstClockTime pts, endpts;
1446 GstClockTime duration;
1449 gst_event_parse_gap (event, &pts, &duration);
1450 gapbuf = gst_buffer_new ();
1452 if (GST_CLOCK_TIME_IS_VALID (duration))
1453 endpts = pts + duration;
1455 endpts = GST_CLOCK_TIME_NONE;
1457 GST_OBJECT_LOCK (aggpad);
1458 res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1460 GST_OBJECT_UNLOCK (aggpad);
1463 GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1467 if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1468 duration = endpts - pts;
1470 duration = GST_CLOCK_TIME_NONE;
1472 GST_BUFFER_PTS (gapbuf) = pts;
1473 GST_BUFFER_DURATION (gapbuf) = duration;
1474 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1475 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1477 /* Remove GAP event so we can replace it with the buffer */
1478 if (g_queue_peek_tail (&aggpad->priv->buffers) == event)
1479 gst_event_unref (g_queue_pop_tail (&aggpad->priv->buffers));
1481 if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1483 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1493 gst_event_parse_tag (event, &tags);
1495 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1496 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1497 gst_event_unref (event);
1509 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1510 return gst_pad_event_default (pad, GST_OBJECT (self), event);
1513 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1515 gst_event_unref (event);
1520 static inline gboolean
1521 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1522 gpointer unused_udata)
1524 gst_aggregator_pad_flush (pad, self);
1527 pad->priv->flow_return = GST_FLOW_FLUSHING;
1528 pad->priv->negotiated = FALSE;
1529 PAD_BROADCAST_EVENT (pad);
1536 gst_aggregator_stop (GstAggregator * agg)
1538 GstAggregatorClass *klass;
1541 gst_aggregator_reset_flow_values (agg);
1543 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1545 klass = GST_AGGREGATOR_GET_CLASS (agg);
1548 result = klass->stop (agg);
1552 agg->priv->has_peer_latency = FALSE;
1553 agg->priv->peer_latency_live = FALSE;
1554 agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1556 if (agg->priv->tags)
1557 gst_tag_list_unref (agg->priv->tags);
1558 agg->priv->tags = NULL;
1560 gst_aggregator_set_allocation (agg, NULL, NULL, NULL, NULL);
1565 /* GstElement vmethods implementations */
1566 static GstStateChangeReturn
1567 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1569 GstStateChangeReturn ret;
1570 GstAggregator *self = GST_AGGREGATOR (element);
1572 switch (transition) {
1573 case GST_STATE_CHANGE_READY_TO_PAUSED:
1574 if (!gst_aggregator_start (self))
1582 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1583 transition)) == GST_STATE_CHANGE_FAILURE)
1587 switch (transition) {
1588 case GST_STATE_CHANGE_PAUSED_TO_READY:
1589 if (!gst_aggregator_stop (self)) {
1590 /* What to do in this case? Error out? */
1591 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1603 GST_ERROR_OBJECT (element, "parent failed state change");
1608 GST_ERROR_OBJECT (element, "Subclass failed to start");
1609 return GST_STATE_CHANGE_FAILURE;
1614 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1616 GstAggregator *self = GST_AGGREGATOR (element);
1617 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1619 GST_INFO_OBJECT (pad, "Removing pad");
1622 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1623 gst_element_remove_pad (element, pad);
1625 self->priv->has_peer_latency = FALSE;
1626 SRC_BROADCAST (self);
1630 static GstAggregatorPad *
1631 gst_aggregator_default_create_new_pad (GstAggregator * self,
1632 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1634 GstAggregatorPad *agg_pad;
1635 GstAggregatorPrivate *priv = self->priv;
1639 if (templ->direction != GST_PAD_SINK)
1642 if (templ->presence != GST_PAD_REQUEST)
1645 GST_OBJECT_LOCK (self);
1646 if (req_name == NULL || strlen (req_name) < 6
1647 || !g_str_has_prefix (req_name, "sink_")) {
1648 /* no name given when requesting the pad, use next available int */
1649 serial = ++priv->max_padserial;
1651 /* parse serial number from requested padname */
1652 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1653 if (serial > priv->max_padserial)
1654 priv->max_padserial = serial;
1657 name = g_strdup_printf ("sink_%u", serial);
1658 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1659 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1662 GST_OBJECT_UNLOCK (self);
1669 GST_WARNING_OBJECT (self, "request new pad that is not a SINK pad");
1674 GST_WARNING_OBJECT (self, "request new pad that is not a REQUEST pad");
1680 gst_aggregator_request_new_pad (GstElement * element,
1681 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1683 GstAggregator *self;
1684 GstAggregatorPad *agg_pad;
1685 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1686 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1688 self = GST_AGGREGATOR (element);
1690 agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1692 GST_ERROR_OBJECT (element, "Couldn't create new pad");
1696 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1699 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1701 /* add the pad to the element */
1702 gst_element_add_pad (element, GST_PAD (agg_pad));
1704 return GST_PAD (agg_pad);
1707 /* Must be called with SRC_LOCK held */
1710 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1712 gboolean query_ret, live;
1713 GstClockTime our_latency, min, max;
1715 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1718 GST_WARNING_OBJECT (self, "Latency query failed");
1722 gst_query_parse_latency (query, &live, &min, &max);
1724 our_latency = self->priv->latency;
1726 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1727 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1728 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1732 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1733 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1734 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1735 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1736 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1740 self->priv->peer_latency_live = live;
1741 self->priv->peer_latency_min = min;
1742 self->priv->peer_latency_max = max;
1743 self->priv->has_peer_latency = TRUE;
1747 min += self->priv->sub_latency_min;
1748 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1749 && GST_CLOCK_TIME_IS_VALID (max))
1750 max += self->priv->sub_latency_max + our_latency;
1752 max = GST_CLOCK_TIME_NONE;
1754 SRC_BROADCAST (self);
1756 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1757 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1759 gst_query_set_latency (query, live, min, max);
1765 * MUST be called with the src_lock held.
1767 * See gst_aggregator_get_latency() for doc
1770 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1772 GstClockTime latency;
1774 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1776 if (!self->priv->has_peer_latency) {
1777 GstQuery *query = gst_query_new_latency ();
1780 ret = gst_aggregator_query_latency_unlocked (self, query);
1781 gst_query_unref (query);
1783 return GST_CLOCK_TIME_NONE;
1786 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1787 return GST_CLOCK_TIME_NONE;
1789 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1790 latency = self->priv->peer_latency_min;
1793 latency += self->priv->latency;
1794 latency += self->priv->sub_latency_min;
1800 * gst_aggregator_get_latency:
1801 * @self: a #GstAggregator
1803 * Retrieves the latency values reported by @self in response to the latency
1804 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1805 * will not wait for the clock.
1807 * Typically only called by subclasses.
1809 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1812 gst_aggregator_get_latency (GstAggregator * self)
1817 ret = gst_aggregator_get_latency_unlocked (self);
1824 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1826 GstAggregator *self = GST_AGGREGATOR (element);
1828 GST_STATE_LOCK (element);
1829 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1830 GST_STATE (element) < GST_STATE_PAUSED) {
1834 GstSeekType start_type, stop_type;
1837 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1838 &start, &stop_type, &stop);
1840 GST_OBJECT_LOCK (self);
1841 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1842 stop_type, stop, NULL);
1843 self->priv->seqnum = gst_event_get_seqnum (event);
1844 self->priv->first_buffer = FALSE;
1845 GST_OBJECT_UNLOCK (self);
1847 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1849 GST_STATE_UNLOCK (element);
1852 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1857 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1859 gboolean res = TRUE;
1861 switch (GST_QUERY_TYPE (query)) {
1862 case GST_QUERY_SEEKING:
1866 /* don't pass it along as some (file)sink might claim it does
1867 * whereas with a collectpads in between that will not likely work */
1868 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1869 gst_query_set_seeking (query, format, FALSE, 0, -1);
1874 case GST_QUERY_LATENCY:
1876 res = gst_aggregator_query_latency_unlocked (self, query);
1880 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1887 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1889 EventData *evdata = user_data;
1890 gboolean ret = TRUE;
1891 GstPad *peer = gst_pad_get_peer (pad);
1892 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1895 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1896 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1899 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1900 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1901 gst_object_unref (peer);
1906 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1907 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1909 GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1911 if (gst_pad_query (peer, seeking)) {
1914 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1916 if (seekable == FALSE) {
1917 GST_INFO_OBJECT (pad,
1918 "Source not seekable, We failed but it does not matter!");
1923 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1926 gst_query_unref (seeking);
1929 if (evdata->flush) {
1931 aggpad->priv->pending_flush_start = FALSE;
1932 aggpad->priv->pending_flush_stop = FALSE;
1933 PAD_UNLOCK (aggpad);
1936 evdata->one_actually_seeked = TRUE;
1939 evdata->result &= ret;
1941 /* Always send to all pads */
1946 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1947 GstEvent * event, gboolean flush, gboolean only_to_active_pads)
1951 evdata.event = event;
1952 evdata.result = TRUE;
1953 evdata.flush = flush;
1954 evdata.one_actually_seeked = FALSE;
1955 evdata.only_to_active_pads = only_to_active_pads;
1957 /* We first need to set all pads as flushing in a first pass
1958 * as flush_start flush_stop is sometimes sent synchronously
1959 * while we send the seek event */
1963 GST_OBJECT_LOCK (self);
1964 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1965 GstAggregatorPad *pad = l->data;
1968 pad->priv->pending_flush_start = TRUE;
1969 pad->priv->pending_flush_stop = FALSE;
1972 GST_OBJECT_UNLOCK (self);
1975 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1977 gst_event_unref (event);
1983 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1988 GstSeekType start_type, stop_type;
1992 GstAggregatorPrivate *priv = self->priv;
1994 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1995 &start, &stop_type, &stop);
1997 GST_INFO_OBJECT (self, "starting SEEK");
1999 flush = flags & GST_SEEK_FLAG_FLUSH;
2001 GST_OBJECT_LOCK (self);
2003 priv->pending_flush_start = TRUE;
2004 priv->flush_seeking = TRUE;
2007 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
2008 stop_type, stop, NULL);
2010 /* Seeking sets a position */
2011 self->priv->first_buffer = FALSE;
2012 GST_OBJECT_UNLOCK (self);
2014 /* forward the seek upstream */
2016 gst_aggregator_forward_event_to_all_sinkpads (self, event, flush, FALSE);
2019 if (!evdata.result || !evdata.one_actually_seeked) {
2020 GST_OBJECT_LOCK (self);
2021 priv->flush_seeking = FALSE;
2022 priv->pending_flush_start = FALSE;
2023 GST_OBJECT_UNLOCK (self);
2026 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
2028 return evdata.result;
2032 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
2035 gboolean res = TRUE;
2037 switch (GST_EVENT_TYPE (event)) {
2038 case GST_EVENT_SEEK:
2040 gst_event_ref (event);
2041 res = gst_aggregator_do_seek (self, event);
2042 gst_event_unref (event);
2046 case GST_EVENT_NAVIGATION:
2048 /* navigation is rather pointless. */
2050 gst_event_unref (event);
2059 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
2060 * they will receive a QOS event that has earliest_time=0 (because we can't
2061 * have negative timestamps), and consider their buffer as too late */
2063 gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE,
2064 GST_EVENT_TYPE (event) == GST_EVENT_QOS);
2065 res = evdata.result;
2072 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
2075 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2077 return klass->src_event (GST_AGGREGATOR (parent), event);
2081 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
2084 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2086 return klass->src_query (GST_AGGREGATOR (parent), query);
2090 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
2091 GstObject * parent, GstPadMode mode, gboolean active)
2093 GstAggregator *self = GST_AGGREGATOR (parent);
2094 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2096 if (klass->src_activate) {
2097 if (klass->src_activate (self, mode, active) == FALSE) {
2102 if (active == TRUE) {
2104 case GST_PAD_MODE_PUSH:
2106 GST_INFO_OBJECT (pad, "Activating pad!");
2107 gst_aggregator_start_srcpad_task (self);
2112 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
2119 GST_INFO_OBJECT (self, "Deactivating srcpad");
2120 gst_aggregator_stop_srcpad_task (self, FALSE);
2126 gst_aggregator_default_sink_query (GstAggregator * self,
2127 GstAggregatorPad * aggpad, GstQuery * query)
2129 GstPad *pad = GST_PAD (aggpad);
2131 if (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION) {
2132 GstQuery *decide_query = NULL;
2133 GstAggregatorClass *agg_class;
2136 GST_OBJECT_LOCK (self);
2138 if (G_UNLIKELY (!aggpad->priv->negotiated)) {
2139 GST_DEBUG_OBJECT (self,
2140 "not negotiated yet, can't answer ALLOCATION query");
2141 PAD_UNLOCK (aggpad);
2142 GST_OBJECT_UNLOCK (self);
2147 if ((decide_query = self->priv->allocation_query))
2148 gst_query_ref (decide_query);
2149 PAD_UNLOCK (aggpad);
2150 GST_OBJECT_UNLOCK (self);
2152 GST_DEBUG_OBJECT (self,
2153 "calling propose allocation with query %" GST_PTR_FORMAT, decide_query);
2155 agg_class = GST_AGGREGATOR_GET_CLASS (self);
2157 /* pass the query to the propose_allocation vmethod if any */
2158 if (agg_class->propose_allocation)
2159 ret = agg_class->propose_allocation (self, aggpad, decide_query, query);
2164 gst_query_unref (decide_query);
2166 GST_DEBUG_OBJECT (self, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
2170 return gst_pad_query_default (pad, GST_OBJECT (self), query);
2174 gst_aggregator_finalize (GObject * object)
2176 GstAggregator *self = (GstAggregator *) object;
2178 g_mutex_clear (&self->priv->src_lock);
2179 g_cond_clear (&self->priv->src_cond);
2181 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
2185 * gst_aggregator_set_latency_property:
2186 * @agg: a #GstAggregator
2187 * @latency: the new latency value (in nanoseconds).
2189 * Sets the new latency value to @latency. This value is used to limit the
2190 * amount of time a pad waits for data to appear before considering the pad
2194 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
2198 g_return_if_fail (GST_IS_AGGREGATOR (self));
2199 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
2202 changed = (self->priv->latency != latency);
2207 GST_OBJECT_LOCK (self);
2208 /* First lock all the pads */
2209 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2210 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2214 self->priv->latency = latency;
2216 SRC_BROADCAST (self);
2218 /* Now wake up the pads */
2219 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2220 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2221 PAD_BROADCAST_EVENT (aggpad);
2222 PAD_UNLOCK (aggpad);
2224 GST_OBJECT_UNLOCK (self);
2230 gst_element_post_message (GST_ELEMENT_CAST (self),
2231 gst_message_new_latency (GST_OBJECT_CAST (self)));
2235 * gst_aggregator_get_latency_property:
2236 * @agg: a #GstAggregator
2238 * Gets the latency value. See gst_aggregator_set_latency for
2241 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
2242 * before a pad is deemed unresponsive. A value of -1 means an
2246 gst_aggregator_get_latency_property (GstAggregator * agg)
2250 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
2252 GST_OBJECT_LOCK (agg);
2253 res = agg->priv->latency;
2254 GST_OBJECT_UNLOCK (agg);
2260 gst_aggregator_set_property (GObject * object, guint prop_id,
2261 const GValue * value, GParamSpec * pspec)
2263 GstAggregator *agg = GST_AGGREGATOR (object);
2267 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
2269 case PROP_START_TIME_SELECTION:
2270 agg->priv->start_time_selection = g_value_get_enum (value);
2272 case PROP_START_TIME:
2273 agg->priv->start_time = g_value_get_uint64 (value);
2276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2282 gst_aggregator_get_property (GObject * object, guint prop_id,
2283 GValue * value, GParamSpec * pspec)
2285 GstAggregator *agg = GST_AGGREGATOR (object);
2289 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
2291 case PROP_START_TIME_SELECTION:
2292 g_value_set_enum (value, agg->priv->start_time_selection);
2294 case PROP_START_TIME:
2295 g_value_set_uint64 (value, agg->priv->start_time);
2298 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2303 /* GObject vmethods implementations */
2305 gst_aggregator_class_init (GstAggregatorClass * klass)
2307 GObjectClass *gobject_class = (GObjectClass *) klass;
2308 GstElementClass *gstelement_class = (GstElementClass *) klass;
2310 aggregator_parent_class = g_type_class_peek_parent (klass);
2311 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
2313 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
2314 GST_DEBUG_FG_MAGENTA, "GstAggregator");
2316 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
2318 klass->sink_event = gst_aggregator_default_sink_event;
2319 klass->sink_query = gst_aggregator_default_sink_query;
2321 klass->src_event = gst_aggregator_default_src_event;
2322 klass->src_query = gst_aggregator_default_src_query;
2324 klass->create_new_pad = gst_aggregator_default_create_new_pad;
2325 klass->update_src_caps = gst_aggregator_default_update_src_caps;
2326 klass->fixate_src_caps = gst_aggregator_default_fixate_src_caps;
2327 klass->negotiated_src_caps = gst_aggregator_default_negotiated_src_caps;
2329 gstelement_class->request_new_pad =
2330 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
2331 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
2332 gstelement_class->release_pad =
2333 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
2334 gstelement_class->change_state =
2335 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
2337 gobject_class->set_property = gst_aggregator_set_property;
2338 gobject_class->get_property = gst_aggregator_get_property;
2339 gobject_class->finalize = gst_aggregator_finalize;
2341 g_object_class_install_property (gobject_class, PROP_LATENCY,
2342 g_param_spec_int64 ("latency", "Buffer latency",
2343 "Additional latency in live mode to allow upstream "
2344 "to take longer to produce buffers for the current "
2345 "position (in nanoseconds)", 0,
2346 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
2347 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2349 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
2350 g_param_spec_enum ("start-time-selection", "Start Time Selection",
2351 "Decides which start time is output",
2352 gst_aggregator_start_time_selection_get_type (),
2353 DEFAULT_START_TIME_SELECTION,
2354 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2356 g_object_class_install_property (gobject_class, PROP_START_TIME,
2357 g_param_spec_uint64 ("start-time", "Start Time",
2358 "Start time to use if start-time-selection=set", 0,
2360 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2362 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
2366 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
2368 GstPadTemplate *pad_template;
2369 GstAggregatorPrivate *priv;
2371 g_return_if_fail (klass->aggregate != NULL);
2374 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2375 GstAggregatorPrivate);
2380 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2381 g_return_if_fail (pad_template != NULL);
2383 priv->max_padserial = -1;
2384 priv->tags_changed = FALSE;
2386 self->priv->peer_latency_live = FALSE;
2387 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2388 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2389 self->priv->has_peer_latency = FALSE;
2390 gst_aggregator_reset_flow_values (self);
2392 self->srcpad = gst_pad_new_from_template (pad_template, "src");
2394 gst_pad_set_event_function (self->srcpad,
2395 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2396 gst_pad_set_query_function (self->srcpad,
2397 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2398 gst_pad_set_activatemode_function (self->srcpad,
2399 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2401 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2403 self->priv->latency = DEFAULT_LATENCY;
2404 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2405 self->priv->start_time = DEFAULT_START_TIME;
2407 g_mutex_init (&self->priv->src_lock);
2408 g_cond_init (&self->priv->src_cond);
2411 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2412 * method to get to the padtemplates */
2414 gst_aggregator_get_type (void)
2416 static volatile gsize type = 0;
2418 if (g_once_init_enter (&type)) {
2420 static const GTypeInfo info = {
2421 sizeof (GstAggregatorClass),
2424 (GClassInitFunc) gst_aggregator_class_init,
2427 sizeof (GstAggregator),
2429 (GInstanceInitFunc) gst_aggregator_init,
2432 _type = g_type_register_static (GST_TYPE_ELEMENT,
2433 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2434 g_once_init_leave (&type, _type);
2439 /* Must be called with SRC lock and PAD lock held */
2441 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2443 /* Empty queue always has space */
2444 if (aggpad->priv->num_buffers == 0 && aggpad->priv->clipped_buffer == NULL)
2447 /* We also want at least two buffers, one is being processed and one is ready
2448 * for the next iteration when we operate in live mode. */
2449 if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2452 /* zero latency, if there is a buffer, it's full */
2453 if (self->priv->latency == 0)
2456 /* Allow no more buffers than the latency */
2457 return (aggpad->priv->time_level <= self->priv->latency);
2460 /* Must be called with the PAD_LOCK held */
2462 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2464 GstClockTime timestamp;
2466 if (GST_BUFFER_DTS_IS_VALID (buffer))
2467 timestamp = GST_BUFFER_DTS (buffer);
2469 timestamp = GST_BUFFER_PTS (buffer);
2471 if (timestamp == GST_CLOCK_TIME_NONE) {
2473 timestamp = aggpad->priv->head_position;
2475 timestamp = aggpad->priv->tail_position;
2479 if (GST_BUFFER_DURATION_IS_VALID (buffer))
2480 timestamp += GST_BUFFER_DURATION (buffer);
2483 aggpad->priv->head_position = timestamp;
2485 aggpad->priv->tail_position = timestamp;
2487 update_time_level (aggpad, head);
2490 static GstFlowReturn
2491 gst_aggregator_pad_chain_internal (GstAggregator * self,
2492 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2494 GstFlowReturn flow_return;
2495 GstClockTime buf_pts;
2497 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2499 PAD_FLUSH_LOCK (aggpad);
2502 flow_return = aggpad->priv->flow_return;
2503 if (flow_return != GST_FLOW_OK)
2506 if (aggpad->priv->pending_eos == TRUE)
2509 PAD_UNLOCK (aggpad);
2511 buf_pts = GST_BUFFER_PTS (buffer);
2515 GST_OBJECT_LOCK (self);
2518 if (aggpad->priv->first_buffer) {
2519 self->priv->has_peer_latency = FALSE;
2520 aggpad->priv->first_buffer = FALSE;
2523 if (gst_aggregator_pad_has_space (self, aggpad)
2524 && aggpad->priv->flow_return == GST_FLOW_OK) {
2526 g_queue_push_head (&aggpad->priv->buffers, buffer);
2528 g_queue_push_tail (&aggpad->priv->buffers, buffer);
2529 apply_buffer (aggpad, buffer, head);
2530 aggpad->priv->num_buffers++;
2532 SRC_BROADCAST (self);
2536 flow_return = aggpad->priv->flow_return;
2537 if (flow_return != GST_FLOW_OK) {
2538 GST_OBJECT_UNLOCK (self);
2542 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2543 GST_OBJECT_UNLOCK (self);
2545 PAD_WAIT_EVENT (aggpad);
2547 PAD_UNLOCK (aggpad);
2550 if (self->priv->first_buffer) {
2551 GstClockTime start_time;
2553 switch (self->priv->start_time_selection) {
2554 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2558 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2559 GST_OBJECT_LOCK (aggpad);
2560 if (aggpad->priv->head_segment.format == GST_FORMAT_TIME) {
2561 start_time = buf_pts;
2562 if (start_time != -1) {
2563 start_time = MAX (start_time, aggpad->priv->head_segment.start);
2565 gst_segment_to_running_time (&aggpad->priv->head_segment,
2566 GST_FORMAT_TIME, start_time);
2570 GST_WARNING_OBJECT (aggpad,
2571 "Ignoring request of selecting the first start time "
2572 "as the segment is a %s segment instead of a time segment",
2573 gst_format_get_name (aggpad->segment.format));
2575 GST_OBJECT_UNLOCK (aggpad);
2577 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2578 start_time = self->priv->start_time;
2579 if (start_time == -1)
2584 if (start_time != -1) {
2585 if (self->segment.position == -1)
2586 self->segment.position = start_time;
2588 self->segment.position = MIN (start_time, self->segment.position);
2590 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2591 GST_TIME_ARGS (start_time));
2595 PAD_UNLOCK (aggpad);
2596 GST_OBJECT_UNLOCK (self);
2599 PAD_FLUSH_UNLOCK (aggpad);
2601 GST_DEBUG_OBJECT (aggpad, "Done chaining");
2606 PAD_UNLOCK (aggpad);
2607 PAD_FLUSH_UNLOCK (aggpad);
2609 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2610 gst_flow_get_name (flow_return));
2612 gst_buffer_unref (buffer);
2617 PAD_UNLOCK (aggpad);
2618 PAD_FLUSH_UNLOCK (aggpad);
2620 gst_buffer_unref (buffer);
2621 GST_DEBUG_OBJECT (aggpad, "We are EOS already...");
2623 return GST_FLOW_EOS;
2626 static GstFlowReturn
2627 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2629 return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2630 GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2634 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2637 GstAggregator *self = GST_AGGREGATOR (parent);
2638 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2639 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2641 if (GST_QUERY_IS_SERIALIZED (query)) {
2643 gboolean ret = FALSE;
2648 if (aggpad->priv->flow_return != GST_FLOW_OK) {
2653 g_queue_push_head (&aggpad->priv->buffers, query);
2654 SRC_BROADCAST (self);
2657 while (!gst_aggregator_pad_queue_is_empty (aggpad)
2658 && aggpad->priv->flow_return == GST_FLOW_OK) {
2659 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2660 PAD_WAIT_EVENT (aggpad);
2663 s = gst_query_writable_structure (query);
2664 if (gst_structure_get_boolean (s, "gst-aggregator-retval", &ret))
2665 gst_structure_remove_field (s, "gst-aggregator-retval");
2667 g_queue_remove (&aggpad->priv->buffers, query);
2669 if (aggpad->priv->flow_return != GST_FLOW_OK)
2672 PAD_UNLOCK (aggpad);
2677 return klass->sink_query (self, aggpad, query);
2680 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2681 gst_flow_get_name (aggpad->priv->flow_return));
2682 PAD_UNLOCK (aggpad);
2687 static GstFlowReturn
2688 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2691 GstFlowReturn ret = GST_FLOW_OK;
2692 GstAggregator *self = GST_AGGREGATOR (parent);
2693 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2694 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2696 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
2697 /* && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE */ ) {
2701 if (aggpad->priv->flow_return != GST_FLOW_OK
2702 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2703 ret = aggpad->priv->flow_return;
2707 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2708 GST_OBJECT_LOCK (aggpad);
2709 gst_event_copy_segment (event, &aggpad->priv->head_segment);
2710 aggpad->priv->head_position = aggpad->priv->head_segment.position;
2711 update_time_level (aggpad, TRUE);
2712 GST_OBJECT_UNLOCK (aggpad);
2715 if (GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2716 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT,
2718 g_queue_push_head (&aggpad->priv->buffers, event);
2720 SRC_BROADCAST (self);
2722 PAD_UNLOCK (aggpad);
2727 if (!klass->sink_event (self, aggpad, event)) {
2728 /* Copied from GstPad to convert boolean to a GstFlowReturn in
2729 * the event handling func */
2730 ret = GST_FLOW_ERROR;
2737 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2738 gst_flow_get_name (aggpad->priv->flow_return));
2739 PAD_UNLOCK (aggpad);
2741 if (GST_EVENT_IS_STICKY (event))
2742 gst_pad_store_sticky_event (pad, event);
2743 gst_event_unref (event);
2749 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2750 GstObject * parent, GstPadMode mode, gboolean active)
2752 GstAggregator *self = GST_AGGREGATOR (parent);
2753 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2755 if (active == FALSE) {
2757 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2758 SRC_BROADCAST (self);
2762 aggpad->priv->flow_return = GST_FLOW_OK;
2763 PAD_BROADCAST_EVENT (aggpad);
2764 PAD_UNLOCK (aggpad);
2770 /***********************************
2771 * GstAggregatorPad implementation *
2772 ************************************/
2773 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2776 gst_aggregator_pad_constructed (GObject * object)
2778 GstPad *pad = GST_PAD (object);
2780 gst_pad_set_chain_function (pad,
2781 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2782 gst_pad_set_event_full_function_full (pad,
2783 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
2784 gst_pad_set_query_function (pad,
2785 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2786 gst_pad_set_activatemode_function (pad,
2787 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2791 gst_aggregator_pad_finalize (GObject * object)
2793 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2795 g_cond_clear (&pad->priv->event_cond);
2796 g_mutex_clear (&pad->priv->flush_lock);
2797 g_mutex_clear (&pad->priv->lock);
2799 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2803 gst_aggregator_pad_dispose (GObject * object)
2805 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2807 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2809 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2813 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2815 GObjectClass *gobject_class = (GObjectClass *) klass;
2817 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2819 gobject_class->constructed = gst_aggregator_pad_constructed;
2820 gobject_class->finalize = gst_aggregator_pad_finalize;
2821 gobject_class->dispose = gst_aggregator_pad_dispose;
2825 gst_aggregator_pad_init (GstAggregatorPad * pad)
2828 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2829 GstAggregatorPadPrivate);
2831 g_queue_init (&pad->priv->buffers);
2832 g_cond_init (&pad->priv->event_cond);
2834 g_mutex_init (&pad->priv->flush_lock);
2835 g_mutex_init (&pad->priv->lock);
2837 gst_aggregator_pad_reset_unlocked (pad);
2838 pad->priv->negotiated = FALSE;
2841 /* Must be called with the PAD_LOCK held */
2843 gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad)
2845 pad->priv->num_buffers--;
2846 GST_TRACE_OBJECT (pad, "Consuming buffer");
2847 if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
2848 pad->priv->pending_eos = FALSE;
2849 pad->priv->eos = TRUE;
2851 PAD_BROADCAST_EVENT (pad);
2854 /* Must be called with the PAD_LOCK held */
2856 gst_aggregator_pad_clip_buffer_unlocked (GstAggregatorPad * pad)
2858 GstAggregator *self = NULL;
2859 GstAggregatorClass *aggclass = NULL;
2860 GstBuffer *buffer = NULL;
2862 while (pad->priv->clipped_buffer == NULL &&
2863 GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->buffers))) {
2864 buffer = g_queue_pop_tail (&pad->priv->buffers);
2866 apply_buffer (pad, buffer, FALSE);
2868 /* We only take the parent here so that it's not taken if the buffer is
2869 * already clipped or if the queue is empty.
2872 self = GST_AGGREGATOR (gst_pad_get_parent_element (GST_PAD (pad)));
2874 gst_buffer_unref (buffer);
2878 aggclass = GST_AGGREGATOR_GET_CLASS (self);
2881 if (aggclass->clip) {
2882 GST_TRACE_OBJECT (pad, "Clipping: %" GST_PTR_FORMAT, buffer);
2884 buffer = aggclass->clip (self, pad, buffer);
2886 if (buffer == NULL) {
2887 gst_aggregator_pad_buffer_consumed (pad);
2888 GST_TRACE_OBJECT (pad, "Clipping consumed the buffer");
2892 pad->priv->clipped_buffer = buffer;
2896 gst_object_unref (self);
2900 * gst_aggregator_pad_steal_buffer:
2901 * @pad: the pad to get buffer from
2903 * Steal the ref to the buffer currently queued in @pad.
2905 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2906 * queued. You should unref the buffer after usage.
2909 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2915 gst_aggregator_pad_clip_buffer_unlocked (pad);
2917 buffer = pad->priv->clipped_buffer;
2918 pad->priv->clipped_buffer = NULL;
2921 gst_aggregator_pad_buffer_consumed (pad);
2922 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2931 * gst_aggregator_pad_drop_buffer:
2932 * @pad: the pad where to drop any pending buffer
2934 * Drop the buffer currently queued in @pad.
2936 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2939 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2943 buf = gst_aggregator_pad_steal_buffer (pad);
2948 gst_buffer_unref (buf);
2953 * gst_aggregator_pad_get_buffer:
2954 * @pad: the pad to get buffer from
2956 * Returns: (transfer full): A reference to the buffer in @pad or
2957 * NULL if no buffer was queued. You should unref the buffer after
2961 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2967 gst_aggregator_pad_clip_buffer_unlocked (pad);
2969 if (pad->priv->clipped_buffer) {
2970 buffer = gst_buffer_ref (pad->priv->clipped_buffer);
2980 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2985 is_eos = pad->priv->eos;
2992 * gst_aggregator_merge_tags:
2993 * @self: a #GstAggregator
2994 * @tags: a #GstTagList to merge
2995 * @mode: the #GstTagMergeMode to use
2997 * Adds tags to so-called pending tags, which will be processed
2998 * before pushing out data downstream.
3000 * Note that this is provided for convenience, and the subclass is
3001 * not required to use this and can still do tag handling on its own.
3006 gst_aggregator_merge_tags (GstAggregator * self,
3007 const GstTagList * tags, GstTagMergeMode mode)
3011 g_return_if_fail (GST_IS_AGGREGATOR (self));
3012 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
3014 /* FIXME Check if we can use OBJECT lock here! */
3015 GST_OBJECT_LOCK (self);
3017 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
3018 otags = self->priv->tags;
3019 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
3021 gst_tag_list_unref (otags);
3022 self->priv->tags_changed = TRUE;
3023 GST_OBJECT_UNLOCK (self);
3027 * gst_aggregator_set_latency:
3028 * @self: a #GstAggregator
3029 * @min_latency: minimum latency
3030 * @max_latency: maximum latency
3032 * Lets #GstAggregator sub-classes tell the baseclass what their internal
3033 * latency is. Will also post a LATENCY message on the bus so the pipeline
3034 * can reconfigure its global latency.
3037 gst_aggregator_set_latency (GstAggregator * self,
3038 GstClockTime min_latency, GstClockTime max_latency)
3040 gboolean changed = FALSE;
3042 g_return_if_fail (GST_IS_AGGREGATOR (self));
3043 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
3044 g_return_if_fail (max_latency >= min_latency);
3047 if (self->priv->sub_latency_min != min_latency) {
3048 self->priv->sub_latency_min = min_latency;
3051 if (self->priv->sub_latency_max != max_latency) {
3052 self->priv->sub_latency_max = max_latency;
3057 SRC_BROADCAST (self);
3061 gst_element_post_message (GST_ELEMENT_CAST (self),
3062 gst_message_new_latency (GST_OBJECT_CAST (self)));
3067 * gst_aggregator_get_buffer_pool:
3068 * @self: a #GstAggregator
3070 * Returns: (transfer full): the instance of the #GstBufferPool used
3071 * by @trans; free it after use it
3074 gst_aggregator_get_buffer_pool (GstAggregator * self)
3076 GstBufferPool *pool;
3078 g_return_val_if_fail (GST_IS_AGGREGATOR (self), NULL);
3080 GST_OBJECT_LOCK (self);
3081 pool = self->priv->pool;
3083 gst_object_ref (pool);
3084 GST_OBJECT_UNLOCK (self);
3090 * gst_aggregator_get_allocator:
3091 * @self: a #GstAggregator
3092 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3094 * @params: (out) (allow-none) (transfer full): the
3095 * #GstAllocationParams of @allocator
3097 * Lets #GstAggregator sub-classes get the memory @allocator
3098 * acquired by the base class and its @params.
3100 * Unref the @allocator after use it.
3103 gst_aggregator_get_allocator (GstAggregator * self,
3104 GstAllocator ** allocator, GstAllocationParams * params)
3106 g_return_if_fail (GST_IS_AGGREGATOR (self));
3109 *allocator = self->priv->allocator ?
3110 gst_object_ref (self->priv->allocator) : NULL;
3113 *params = self->priv->allocation_params;