1 /* GStreamer aggregator base class
2 * Copyright (C) 2014 Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
3 * Copyright (C) 2014 Thibault Saunier <tsaunier@gnome.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 * SECTION: gstaggregator
24 * @title: GstAggregator
25 * @short_description: manages a set of pads with the purpose of
26 * aggregating their buffers.
27 * @see_also: gstcollectpads for historical reasons.
29 * Manages a set of pads with the purpose of aggregating their buffers.
30 * Control is given to the subclass when all pads have data.
32 * * Base class for mixers and muxers. Subclasses should at least implement
33 * the #GstAggregatorClass.aggregate() virtual method.
35 * * When data is queued on all pads, tha aggregate vmethod is called.
37 * * One can peek at the data on any given GstAggregatorPad with the
38 * gst_aggregator_pad_get_buffer () method, and take ownership of it
39 * with the gst_aggregator_pad_steal_buffer () method. When a buffer
40 * has been taken with steal_buffer (), a new buffer can be queued
43 * * If the subclass wishes to push a buffer downstream in its aggregate
44 * implementation, it should do so through the
45 * gst_aggregator_finish_buffer () method. This method will take care
46 * of sending and ordering mandatory events such as stream start, caps
49 * * Same goes for EOS events, which should not be pushed directly by the
50 * subclass, it should instead return GST_FLOW_EOS in its aggregate
53 * * Note that the aggregator logic regarding gap event handling is to turn
54 * these into gap buffers with matching PTS and duration. It will also
55 * flag these buffers with GST_BUFFER_FLAG_GAP and GST_BUFFER_FLAG_DROPPABLE
56 * to ease their identification and subsequent processing.
64 #include <string.h> /* strlen */
66 #include "gstaggregator.h"
70 GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
71 GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
72 GST_AGGREGATOR_START_TIME_SELECTION_SET
73 } GstAggregatorStartTimeSelection;
76 gst_aggregator_start_time_selection_get_type (void)
78 static GType gtype = 0;
81 static const GEnumValue values[] = {
82 {GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
83 "Start at 0 running time (default)", "zero"},
84 {GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
85 "Start at first observed input running time", "first"},
86 {GST_AGGREGATOR_START_TIME_SELECTION_SET,
87 "Set start time with start-time property", "set"},
91 gtype = g_enum_register_static ("GstAggregatorStartTimeSelection", values);
96 /* Might become API */
97 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
98 const GstTagList * tags, GstTagMergeMode mode);
99 static void gst_aggregator_set_latency_property (GstAggregator * agg,
101 static gint64 gst_aggregator_get_latency_property (GstAggregator * agg);
104 /* Locking order, locks in this element must always be taken in this order
106 * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
107 * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
108 * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
109 * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
110 * standard element object lock -> GST_OBJECT_LOCK(agg)
111 * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
112 * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
113 * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
117 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
119 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
120 #define GST_CAT_DEFAULT aggregator_debug
122 /* GstAggregatorPad definitions */
123 #define PAD_LOCK(pad) G_STMT_START { \
124 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
126 g_mutex_lock(&pad->priv->lock); \
127 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
131 #define PAD_UNLOCK(pad) G_STMT_START { \
132 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
134 g_mutex_unlock(&pad->priv->lock); \
135 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
140 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
141 GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p", \
143 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
144 (&((GstAggregatorPad*)pad)->priv->lock)); \
145 GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
149 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
150 GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p", \
152 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
156 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
157 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
159 g_mutex_lock(&pad->priv->flush_lock); \
160 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
164 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
165 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
167 g_mutex_unlock(&pad->priv->flush_lock); \
168 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
172 #define SRC_LOCK(self) G_STMT_START { \
173 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
175 g_mutex_lock(&self->priv->src_lock); \
176 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
180 #define SRC_UNLOCK(self) G_STMT_START { \
181 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
183 g_mutex_unlock(&self->priv->src_lock); \
184 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
188 #define SRC_WAIT(self) G_STMT_START { \
189 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
191 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
192 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
196 #define SRC_BROADCAST(self) G_STMT_START { \
197 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
199 if (self->priv->aggregate_id) \
200 gst_clock_id_unschedule (self->priv->aggregate_id); \
201 g_cond_broadcast(&(self->priv->src_cond)); \
204 struct _GstAggregatorPadPrivate
206 /* Following fields are protected by the PAD_LOCK */
207 GstFlowReturn flow_return;
208 gboolean pending_flush_start;
209 gboolean pending_flush_stop;
210 gboolean pending_eos;
212 gboolean first_buffer;
215 GstBuffer *clipped_buffer;
217 GstClockTime head_position;
218 GstClockTime tail_position;
219 GstClockTime head_time;
220 GstClockTime tail_time;
221 GstClockTime time_level;
222 GstSegment head_segment; /* segment before the queue */
228 /* This lock prevents a flush start processing happening while
229 * the chain function is also happening.
234 /* Must be called with PAD_LOCK held */
236 gst_aggregator_pad_reset_unlocked (GstAggregatorPad * aggpad)
238 aggpad->priv->pending_eos = FALSE;
239 aggpad->priv->eos = FALSE;
240 aggpad->priv->flow_return = GST_FLOW_OK;
241 GST_OBJECT_LOCK (aggpad);
242 gst_segment_init (&aggpad->segment, GST_FORMAT_UNDEFINED);
243 gst_segment_init (&aggpad->priv->head_segment, GST_FORMAT_UNDEFINED);
244 GST_OBJECT_UNLOCK (aggpad);
245 aggpad->priv->head_position = GST_CLOCK_TIME_NONE;
246 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
247 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
248 aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
249 aggpad->priv->time_level = 0;
250 aggpad->priv->first_buffer = TRUE;
254 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
256 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
259 gst_aggregator_pad_reset_unlocked (aggpad);
263 return klass->flush (aggpad, agg);
268 /*************************************
269 * GstAggregator implementation *
270 *************************************/
271 static GstElementClass *aggregator_parent_class = NULL;
273 /* All members are protected by the object lock unless otherwise noted */
275 struct _GstAggregatorPrivate
279 /* Our state is >= PAUSED */
280 gboolean running; /* protected by src_lock */
283 gboolean send_stream_start; /* protected by srcpad stream lock */
284 gboolean send_segment;
285 gboolean flush_seeking;
286 gboolean pending_flush_start;
287 gboolean send_eos; /* protected by srcpad stream lock */
289 GstCaps *srccaps; /* protected by the srcpad stream lock */
292 gboolean tags_changed;
294 gboolean peer_latency_live; /* protected by src_lock */
295 GstClockTime peer_latency_min; /* protected by src_lock */
296 GstClockTime peer_latency_max; /* protected by src_lock */
297 gboolean has_peer_latency; /* protected by src_lock */
299 GstClockTime sub_latency_min; /* protected by src_lock */
300 GstClockTime sub_latency_max; /* protected by src_lock */
303 GstClockID aggregate_id; /* protected by src_lock */
307 gboolean first_buffer; /* protected by object lock */
308 GstAggregatorStartTimeSelection start_time_selection;
309 GstClockTime start_time;
311 /* protected by the object lock */
312 GstQuery *allocation_query;
313 GstAllocator *allocator;
315 GstAllocationParams allocation_params;
318 gint64 latency; /* protected by both src_lock and all pad locks */
326 gboolean only_to_active_pads;
328 gboolean one_actually_seeked;
331 #define DEFAULT_LATENCY 0
332 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
333 #define DEFAULT_START_TIME (-1)
339 PROP_START_TIME_SELECTION,
344 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
345 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
348 * gst_aggregator_iterate_sinkpads:
349 * @self: The #GstAggregator
350 * @func: (scope call): The function to call.
351 * @user_data: (closure): The data to pass to @func.
353 * Iterate the sinkpads of aggregator to call a function on them.
355 * This method guarantees that @func will be called only once for each
359 gst_aggregator_iterate_sinkpads (GstAggregator * self,
360 GstAggregatorPadForeachFunc func, gpointer user_data)
362 gboolean result = FALSE;
364 gboolean done = FALSE;
365 GValue item = { 0, };
366 GList *seen_pads = NULL;
368 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
374 switch (gst_iterator_next (iter, &item)) {
375 case GST_ITERATOR_OK:
377 GstAggregatorPad *pad;
379 pad = g_value_get_object (&item);
381 /* if already pushed, skip. FIXME, find something faster to tag pads */
382 if (pad == NULL || g_list_find (seen_pads, pad)) {
383 g_value_reset (&item);
387 GST_LOG_OBJECT (pad, "calling function %s on pad",
388 GST_DEBUG_FUNCPTR_NAME (func));
390 result = func (self, pad, user_data);
394 seen_pads = g_list_prepend (seen_pads, pad);
396 g_value_reset (&item);
399 case GST_ITERATOR_RESYNC:
400 gst_iterator_resync (iter);
402 case GST_ITERATOR_ERROR:
403 GST_ERROR_OBJECT (self,
404 "Could not iterate over internally linked pads");
407 case GST_ITERATOR_DONE:
412 g_value_unset (&item);
413 gst_iterator_free (iter);
415 if (seen_pads == NULL) {
416 GST_DEBUG_OBJECT (self, "No pad seen");
420 g_list_free (seen_pads);
427 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
429 return (g_queue_peek_tail (&pad->priv->buffers) == NULL &&
430 pad->priv->clipped_buffer == NULL);
434 gst_aggregator_check_pads_ready (GstAggregator * self)
436 GstAggregatorPad *pad;
438 gboolean have_buffer = TRUE;
439 gboolean have_event = FALSE;
441 GST_LOG_OBJECT (self, "checking pads");
443 GST_OBJECT_LOCK (self);
445 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
446 if (sinkpads == NULL)
449 for (l = sinkpads; l != NULL; l = l->next) {
454 if (pad->priv->num_buffers == 0) {
455 if (!gst_aggregator_pad_queue_is_empty (pad))
457 if (!pad->priv->eos) {
460 /* If not live we need data on all pads, so leave the loop */
461 if (!self->priv->peer_latency_live) {
466 } else if (self->priv->peer_latency_live) {
467 /* In live mode, having a single pad with buffers is enough to
468 * generate a start time from it. In non-live mode all pads need
471 self->priv->first_buffer = FALSE;
477 if (!have_buffer && !have_event)
481 self->priv->first_buffer = FALSE;
483 GST_OBJECT_UNLOCK (self);
484 GST_LOG_OBJECT (self, "pads are ready");
489 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
490 GST_OBJECT_UNLOCK (self);
496 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet,"
497 " but waking up for serialized event");
499 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
500 GST_OBJECT_UNLOCK (self);
506 gst_aggregator_reset_flow_values (GstAggregator * self)
508 GST_OBJECT_LOCK (self);
509 self->priv->send_stream_start = TRUE;
510 self->priv->send_segment = TRUE;
511 gst_segment_init (&self->segment, GST_FORMAT_TIME);
512 self->priv->first_buffer = TRUE;
513 GST_OBJECT_UNLOCK (self);
517 gst_aggregator_push_mandatory_events (GstAggregator * self)
519 GstAggregatorPrivate *priv = self->priv;
520 GstEvent *segment = NULL;
521 GstEvent *tags = NULL;
523 if (self->priv->send_stream_start) {
526 GST_INFO_OBJECT (self, "pushing stream start");
527 /* stream-start (FIXME: create id based on input ids) */
528 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
529 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
530 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
532 self->priv->send_stream_start = FALSE;
535 if (self->priv->srccaps) {
537 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
538 self->priv->srccaps);
539 if (!gst_pad_push_event (self->srcpad,
540 gst_event_new_caps (self->priv->srccaps))) {
541 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
543 gst_caps_unref (self->priv->srccaps);
544 self->priv->srccaps = NULL;
547 GST_OBJECT_LOCK (self);
548 if (self->priv->send_segment && !self->priv->flush_seeking) {
549 segment = gst_event_new_segment (&self->segment);
551 if (!self->priv->seqnum)
552 self->priv->seqnum = gst_event_get_seqnum (segment);
554 gst_event_set_seqnum (segment, self->priv->seqnum);
555 self->priv->send_segment = FALSE;
557 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
560 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
561 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
562 priv->tags_changed = FALSE;
564 GST_OBJECT_UNLOCK (self);
567 gst_pad_push_event (self->srcpad, segment);
569 gst_pad_push_event (self->srcpad, tags);
574 * gst_aggregator_set_src_caps:
575 * @self: The #GstAggregator
576 * @caps: The #GstCaps to set on the src pad.
578 * Sets the caps to be used on the src pad.
581 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
583 GST_PAD_STREAM_LOCK (self->srcpad);
584 gst_caps_replace (&self->priv->srccaps, caps);
585 gst_aggregator_push_mandatory_events (self);
586 GST_PAD_STREAM_UNLOCK (self->srcpad);
590 * gst_aggregator_finish_buffer:
591 * @self: The #GstAggregator
592 * @buffer: (transfer full): the #GstBuffer to push.
594 * This method will push the provided output buffer downstream. If needed,
595 * mandatory events such as stream-start, caps, and segment events will be
596 * sent before pushing the buffer.
599 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
601 gst_aggregator_push_mandatory_events (self);
603 GST_OBJECT_LOCK (self);
604 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
605 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
606 GST_OBJECT_UNLOCK (self);
607 return gst_pad_push (self->srcpad, buffer);
609 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
610 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
611 GST_OBJECT_UNLOCK (self);
612 gst_buffer_unref (buffer);
618 gst_aggregator_push_eos (GstAggregator * self)
621 gst_aggregator_push_mandatory_events (self);
623 event = gst_event_new_eos ();
625 GST_OBJECT_LOCK (self);
626 self->priv->send_eos = FALSE;
627 gst_event_set_seqnum (event, self->priv->seqnum);
628 GST_OBJECT_UNLOCK (self);
630 gst_pad_push_event (self->srcpad, event);
634 gst_aggregator_get_next_time (GstAggregator * self)
636 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
638 if (klass->get_next_time)
639 return klass->get_next_time (self);
641 return GST_CLOCK_TIME_NONE;
645 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
647 GstClockTime latency;
655 latency = gst_aggregator_get_latency_unlocked (self);
657 if (gst_aggregator_check_pads_ready (self)) {
658 GST_DEBUG_OBJECT (self, "all pads have data");
664 /* Before waiting, check if we're actually still running */
665 if (!self->priv->running || !self->priv->send_eos) {
671 start = gst_aggregator_get_next_time (self);
673 /* If we're not live, or if we use the running time
674 * of the first buffer as start time, we wait until
675 * all pads have buffers.
676 * Otherwise (i.e. if we are live!), we wait on the clock
677 * and if a pad does not have a buffer in time we ignore
680 GST_OBJECT_LOCK (self);
681 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
682 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
683 !GST_CLOCK_TIME_IS_VALID (start) ||
684 (self->priv->first_buffer
685 && self->priv->start_time_selection ==
686 GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
687 /* We wake up here when something happened, and below
688 * then check if we're ready now. If we return FALSE,
689 * we will be directly called again.
691 GST_OBJECT_UNLOCK (self);
694 GstClockTime base_time, time;
696 GstClockReturn status;
697 GstClockTimeDiff jitter;
699 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
700 GST_TIME_ARGS (start));
702 base_time = GST_ELEMENT_CAST (self)->base_time;
703 clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
704 GST_OBJECT_UNLOCK (self);
706 time = base_time + start;
709 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
710 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
711 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
712 GST_TIME_ARGS (time),
713 GST_TIME_ARGS (base_time),
714 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
715 GST_TIME_ARGS (gst_clock_get_time (clock)));
717 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
718 gst_object_unref (clock);
722 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
725 if (self->priv->aggregate_id) {
726 gst_clock_id_unref (self->priv->aggregate_id);
727 self->priv->aggregate_id = NULL;
730 GST_DEBUG_OBJECT (self,
731 "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
732 status, GST_STIME_ARGS (jitter));
735 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
742 res = gst_aggregator_check_pads_ready (self);
749 check_events (GstAggregator * self, GstAggregatorPad * pad, gpointer user_data)
751 GstEvent *event = NULL;
752 GstAggregatorClass *klass = NULL;
753 gboolean *processed_event = user_data;
759 if (pad->priv->num_buffers == 0 && pad->priv->pending_eos) {
760 pad->priv->pending_eos = FALSE;
761 pad->priv->eos = TRUE;
763 if (pad->priv->clipped_buffer == NULL &&
764 GST_IS_EVENT (g_queue_peek_tail (&pad->priv->buffers))) {
765 event = g_queue_pop_tail (&pad->priv->buffers);
766 PAD_BROADCAST_EVENT (pad);
771 *processed_event = TRUE;
773 klass = GST_AGGREGATOR_GET_CLASS (self);
775 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
776 klass->sink_event (self, pad, event);
778 } while (event != NULL);
784 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
785 GstFlowReturn flow_return, gboolean full)
790 if (flow_return == GST_FLOW_NOT_LINKED)
791 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
793 aggpad->priv->flow_return = flow_return;
795 item = g_queue_peek_head_link (&aggpad->priv->buffers);
797 GList *next = item->next;
799 /* In partial flush, we do like the pad, we get rid of non-sticky events
802 if (full || GST_IS_BUFFER (item->data) ||
803 GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
804 GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
805 !GST_EVENT_IS_STICKY (item->data)) {
806 gst_mini_object_unref (item->data);
807 g_queue_delete_link (&aggpad->priv->buffers, item);
811 aggpad->priv->num_buffers = 0;
812 gst_buffer_replace (&aggpad->priv->clipped_buffer, NULL);
814 PAD_BROADCAST_EVENT (aggpad);
819 gst_aggregator_default_update_src_caps (GstAggregator * agg, GstCaps * caps,
822 *ret = gst_caps_ref (caps);
828 gst_aggregator_default_fixate_src_caps (GstAggregator * agg, GstCaps * caps)
830 caps = gst_caps_fixate (caps);
836 gst_aggregator_default_negotiated_src_caps (GstAggregator * agg, GstCaps * caps)
842 /* takes ownership of the pool, allocator and query */
844 gst_aggregator_set_allocation (GstAggregator * self,
845 GstBufferPool * pool, GstAllocator * allocator,
846 GstAllocationParams * params, GstQuery * query)
848 GstAllocator *oldalloc;
849 GstBufferPool *oldpool;
852 GST_DEBUG ("storing allocation query");
854 GST_OBJECT_LOCK (self);
855 oldpool = self->priv->pool;
856 self->priv->pool = pool;
858 oldalloc = self->priv->allocator;
859 self->priv->allocator = allocator;
861 oldquery = self->priv->allocation_query;
862 self->priv->allocation_query = query;
865 self->priv->allocation_params = *params;
867 gst_allocation_params_init (&self->priv->allocation_params);
868 GST_OBJECT_UNLOCK (self);
871 GST_DEBUG_OBJECT (self, "deactivating old pool %p", oldpool);
872 gst_buffer_pool_set_active (oldpool, FALSE);
873 gst_object_unref (oldpool);
876 gst_object_unref (oldalloc);
879 gst_query_unref (oldquery);
886 gst_aggregator_decide_allocation (GstAggregator * self, GstQuery * query)
888 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
890 if (aggclass->decide_allocation)
891 if (!aggclass->decide_allocation (self, query))
898 gst_aggregator_do_allocation (GstAggregator * self, GstCaps * caps)
901 gboolean result = TRUE;
902 GstBufferPool *pool = NULL;
903 GstAllocator *allocator;
904 GstAllocationParams params;
906 /* find a pool for the negotiated caps now */
907 GST_DEBUG_OBJECT (self, "doing allocation query");
908 query = gst_query_new_allocation (caps, TRUE);
909 if (!gst_pad_peer_query (self->srcpad, query)) {
910 /* not a problem, just debug a little */
911 GST_DEBUG_OBJECT (self, "peer ALLOCATION query failed");
914 GST_DEBUG_OBJECT (self, "calling decide_allocation");
915 result = gst_aggregator_decide_allocation (self, query);
917 GST_DEBUG_OBJECT (self, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
921 goto no_decide_allocation;
923 /* we got configuration from our peer or the decide_allocation method,
925 if (gst_query_get_n_allocation_params (query) > 0) {
926 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
929 gst_allocation_params_init (¶ms);
932 if (gst_query_get_n_allocation_pools (query) > 0)
933 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
937 gst_aggregator_set_allocation (self, pool, allocator, ¶ms, query);
942 no_decide_allocation:
944 GST_WARNING_OBJECT (self, "Failed to decide allocation");
945 gst_query_unref (query);
952 /* WITH SRC_LOCK held */
954 gst_aggregator_update_src_caps (GstAggregator * self)
956 GstAggregatorClass *agg_klass = GST_AGGREGATOR_GET_CLASS (self);
957 GstCaps *downstream_caps, *template_caps, *caps = NULL;
958 GstFlowReturn ret = GST_FLOW_OK;
960 template_caps = gst_pad_get_pad_template_caps (self->srcpad);
961 downstream_caps = gst_pad_peer_query_caps (self->srcpad, template_caps);
963 if (gst_caps_is_empty (downstream_caps)) {
964 GST_INFO_OBJECT (self, "Downstream caps (%"
965 GST_PTR_FORMAT ") not compatible with pad template caps (%"
966 GST_PTR_FORMAT ")", downstream_caps, template_caps);
967 ret = GST_FLOW_NOT_NEGOTIATED;
971 g_assert (agg_klass->update_src_caps);
972 GST_DEBUG_OBJECT (self, "updating caps from %" GST_PTR_FORMAT,
974 ret = agg_klass->update_src_caps (self, downstream_caps, &caps);
975 if (ret < GST_FLOW_OK) {
976 GST_WARNING_OBJECT (self, "Subclass failed to update provided caps");
979 if ((caps == NULL || gst_caps_is_empty (caps)) && ret >= GST_FLOW_OK) {
980 ret = GST_FLOW_NOT_NEGOTIATED;
983 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
985 #ifdef GST_ENABLE_EXTRA_CHECKS
986 if (!gst_caps_is_subset (caps, template_caps)) {
987 GstCaps *intersection;
989 GST_ERROR_OBJECT (self,
990 "update_src_caps returned caps %" GST_PTR_FORMAT
991 " which are not a real subset of the template caps %"
992 GST_PTR_FORMAT, caps, template_caps);
993 g_warning ("%s: update_src_caps returned caps which are not a real "
994 "subset of the filter caps", GST_ELEMENT_NAME (self));
997 gst_caps_intersect_full (template_caps, caps, GST_CAPS_INTERSECT_FIRST);
998 gst_caps_unref (caps);
1003 if (gst_caps_is_any (caps)) {
1007 if (!gst_caps_is_fixed (caps)) {
1008 g_assert (agg_klass->fixate_src_caps);
1010 GST_DEBUG_OBJECT (self, "fixate caps from %" GST_PTR_FORMAT, caps);
1011 if (!(caps = agg_klass->fixate_src_caps (self, caps))) {
1012 GST_WARNING_OBJECT (self, "Subclass failed to fixate provided caps");
1013 ret = GST_FLOW_NOT_NEGOTIATED;
1016 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1019 if (agg_klass->negotiated_src_caps) {
1020 if (!agg_klass->negotiated_src_caps (self, caps)) {
1021 GST_WARNING_OBJECT (self, "Subclass failed to accept negotiated caps");
1022 ret = GST_FLOW_NOT_NEGOTIATED;
1027 gst_aggregator_set_src_caps (self, caps);
1029 if (!gst_aggregator_do_allocation (self, caps)) {
1030 GST_WARNING_OBJECT (self, "Allocation negotiation failed");
1031 ret = GST_FLOW_NOT_NEGOTIATED;
1035 gst_caps_unref (downstream_caps);
1036 gst_caps_unref (template_caps);
1039 gst_caps_unref (caps);
1045 gst_aggregator_aggregate_func (GstAggregator * self)
1047 GstAggregatorPrivate *priv = self->priv;
1048 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1049 gboolean timeout = FALSE;
1051 if (self->priv->running == FALSE) {
1052 GST_DEBUG_OBJECT (self, "Not running anymore");
1056 GST_LOG_OBJECT (self, "Checking aggregate");
1057 while (priv->send_eos && priv->running) {
1058 GstFlowReturn flow_return = GST_FLOW_OK;
1059 gboolean processed_event = FALSE;
1061 gst_aggregator_iterate_sinkpads (self, check_events, NULL);
1063 if (!gst_aggregator_wait_and_check (self, &timeout))
1066 gst_aggregator_iterate_sinkpads (self, check_events, &processed_event);
1067 if (processed_event)
1070 if (gst_pad_check_reconfigure (GST_AGGREGATOR_SRC_PAD (self))) {
1071 flow_return = gst_aggregator_update_src_caps (self);
1072 if (flow_return != GST_FLOW_OK)
1073 gst_pad_mark_reconfigure (GST_AGGREGATOR_SRC_PAD (self));
1076 if (timeout || flow_return >= GST_FLOW_OK) {
1077 GST_TRACE_OBJECT (self, "Actually aggregating!");
1078 flow_return = klass->aggregate (self, timeout);
1081 if (flow_return == GST_AGGREGATOR_FLOW_NEED_DATA)
1084 GST_OBJECT_LOCK (self);
1085 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
1086 /* We don't want to set the pads to flushing, but we want to
1087 * stop the thread, so just break here */
1088 GST_OBJECT_UNLOCK (self);
1091 GST_OBJECT_UNLOCK (self);
1093 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
1094 gst_aggregator_push_eos (self);
1097 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
1099 if (flow_return != GST_FLOW_OK) {
1102 GST_OBJECT_LOCK (self);
1103 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
1104 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1106 gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
1108 GST_OBJECT_UNLOCK (self);
1113 /* Pause the task here, the only ways to get here are:
1114 * 1) We're stopping, in which case the task is stopped anyway
1115 * 2) We got a flow error above, in which case it might take
1116 * some time to forward the flow return upstream and we
1117 * would otherwise call the task function over and over
1118 * again without doing anything
1120 gst_pad_pause_task (self->srcpad);
1124 gst_aggregator_start (GstAggregator * self)
1126 GstAggregatorClass *klass;
1129 self->priv->send_stream_start = TRUE;
1130 self->priv->send_segment = TRUE;
1131 self->priv->send_eos = TRUE;
1132 self->priv->srccaps = NULL;
1134 gst_aggregator_set_allocation (self, NULL, NULL, NULL, NULL);
1136 klass = GST_AGGREGATOR_GET_CLASS (self);
1139 result = klass->start (self);
1147 _check_pending_flush_stop (GstAggregatorPad * pad)
1152 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
1159 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
1161 gboolean res = TRUE;
1163 GST_INFO_OBJECT (self, "%s srcpad task",
1164 flush_start ? "Pausing" : "Stopping");
1167 self->priv->running = FALSE;
1168 SRC_BROADCAST (self);
1172 res = gst_pad_push_event (self->srcpad, flush_start);
1175 gst_pad_stop_task (self->srcpad);
1181 gst_aggregator_start_srcpad_task (GstAggregator * self)
1183 GST_INFO_OBJECT (self, "Starting srcpad task");
1185 self->priv->running = TRUE;
1186 gst_pad_start_task (GST_PAD (self->srcpad),
1187 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
1190 static GstFlowReturn
1191 gst_aggregator_flush (GstAggregator * self)
1193 GstFlowReturn ret = GST_FLOW_OK;
1194 GstAggregatorPrivate *priv = self->priv;
1195 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1197 GST_DEBUG_OBJECT (self, "Flushing everything");
1198 GST_OBJECT_LOCK (self);
1199 priv->send_segment = TRUE;
1200 priv->flush_seeking = FALSE;
1201 priv->tags_changed = FALSE;
1202 GST_OBJECT_UNLOCK (self);
1204 ret = klass->flush (self);
1210 /* Called with GstAggregator's object lock held */
1213 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
1216 GstAggregatorPad *tmppad;
1218 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
1219 tmppad = (GstAggregatorPad *) tmp->data;
1221 if (_check_pending_flush_stop (tmppad) == FALSE) {
1222 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
1223 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
1232 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
1235 GstAggregatorPrivate *priv = self->priv;
1236 GstAggregatorPadPrivate *padpriv = aggpad->priv;
1238 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
1240 PAD_FLUSH_LOCK (aggpad);
1242 if (padpriv->pending_flush_start) {
1243 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
1245 padpriv->pending_flush_start = FALSE;
1246 padpriv->pending_flush_stop = TRUE;
1248 PAD_UNLOCK (aggpad);
1250 GST_OBJECT_LOCK (self);
1251 if (priv->flush_seeking) {
1252 /* If flush_seeking we forward the first FLUSH_START */
1253 if (priv->pending_flush_start) {
1254 priv->pending_flush_start = FALSE;
1255 GST_OBJECT_UNLOCK (self);
1257 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
1258 gst_aggregator_stop_srcpad_task (self, event);
1260 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
1261 GST_PAD_STREAM_LOCK (self->srcpad);
1262 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
1265 GST_OBJECT_UNLOCK (self);
1266 gst_event_unref (event);
1269 GST_OBJECT_UNLOCK (self);
1270 gst_event_unref (event);
1272 PAD_FLUSH_UNLOCK (aggpad);
1275 /* Must be called with the the PAD_LOCK held */
1277 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1280 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->head_position) &&
1281 aggpad->priv->head_segment.format == GST_FORMAT_TIME)
1282 aggpad->priv->head_time =
1283 gst_segment_to_running_time (&aggpad->priv->head_segment,
1284 GST_FORMAT_TIME, aggpad->priv->head_position);
1286 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
1288 if (!GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_time))
1289 aggpad->priv->tail_time = aggpad->priv->head_time;
1291 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_position) &&
1292 aggpad->segment.format == GST_FORMAT_TIME)
1293 aggpad->priv->tail_time =
1294 gst_segment_to_running_time (&aggpad->segment,
1295 GST_FORMAT_TIME, aggpad->priv->tail_position);
1297 aggpad->priv->tail_time = aggpad->priv->head_time;
1300 if (aggpad->priv->head_time == GST_CLOCK_TIME_NONE ||
1301 aggpad->priv->tail_time == GST_CLOCK_TIME_NONE) {
1302 aggpad->priv->time_level = 0;
1306 if (aggpad->priv->tail_time > aggpad->priv->head_time)
1307 aggpad->priv->time_level = 0;
1309 aggpad->priv->time_level = aggpad->priv->head_time -
1310 aggpad->priv->tail_time;
1314 /* GstAggregator vmethods default implementations */
1316 gst_aggregator_default_sink_event (GstAggregator * self,
1317 GstAggregatorPad * aggpad, GstEvent * event)
1319 gboolean res = TRUE;
1320 GstPad *pad = GST_PAD (aggpad);
1321 GstAggregatorPrivate *priv = self->priv;
1323 switch (GST_EVENT_TYPE (event)) {
1324 case GST_EVENT_FLUSH_START:
1326 gst_aggregator_flush_start (self, aggpad, event);
1327 /* We forward only in one case: right after flush_seeking */
1331 case GST_EVENT_FLUSH_STOP:
1333 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
1335 gst_aggregator_pad_flush (aggpad, self);
1336 GST_OBJECT_LOCK (self);
1337 if (priv->flush_seeking) {
1338 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1339 if (gst_aggregator_all_flush_stop_received_locked (self)) {
1340 GST_OBJECT_UNLOCK (self);
1341 /* That means we received FLUSH_STOP/FLUSH_STOP on
1342 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1343 gst_aggregator_flush (self);
1344 gst_pad_push_event (self->srcpad, event);
1347 priv->send_eos = TRUE;
1348 SRC_BROADCAST (self);
1351 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1352 GST_PAD_STREAM_UNLOCK (self->srcpad);
1353 gst_aggregator_start_srcpad_task (self);
1355 GST_OBJECT_UNLOCK (self);
1358 GST_OBJECT_UNLOCK (self);
1361 /* We never forward the event */
1366 GST_DEBUG_OBJECT (aggpad, "EOS");
1368 /* We still have a buffer, and we don't want the subclass to have to
1369 * check for it. Mark pending_eos, eos will be set when steal_buffer is
1374 if (aggpad->priv->num_buffers == 0) {
1375 aggpad->priv->eos = TRUE;
1377 aggpad->priv->pending_eos = TRUE;
1379 PAD_UNLOCK (aggpad);
1381 SRC_BROADCAST (self);
1385 case GST_EVENT_SEGMENT:
1388 GST_OBJECT_LOCK (aggpad);
1389 gst_event_copy_segment (event, &aggpad->segment);
1390 update_time_level (aggpad, FALSE);
1391 GST_OBJECT_UNLOCK (aggpad);
1392 PAD_UNLOCK (aggpad);
1394 GST_OBJECT_LOCK (self);
1395 self->priv->seqnum = gst_event_get_seqnum (event);
1396 GST_OBJECT_UNLOCK (self);
1399 case GST_EVENT_STREAM_START:
1405 GstClockTime pts, endpts;
1406 GstClockTime duration;
1409 gst_event_parse_gap (event, &pts, &duration);
1410 gapbuf = gst_buffer_new ();
1412 if (GST_CLOCK_TIME_IS_VALID (duration))
1413 endpts = pts + duration;
1415 endpts = GST_CLOCK_TIME_NONE;
1417 GST_OBJECT_LOCK (aggpad);
1418 res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1420 GST_OBJECT_UNLOCK (aggpad);
1423 GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1427 if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1428 duration = endpts - pts;
1430 duration = GST_CLOCK_TIME_NONE;
1432 GST_BUFFER_PTS (gapbuf) = pts;
1433 GST_BUFFER_DURATION (gapbuf) = duration;
1434 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1435 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1437 if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1439 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1449 gst_event_parse_tag (event, &tags);
1451 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1452 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1453 gst_event_unref (event);
1465 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1466 return gst_pad_event_default (pad, GST_OBJECT (self), event);
1469 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1471 gst_event_unref (event);
1476 static inline gboolean
1477 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1478 gpointer unused_udata)
1480 gst_aggregator_pad_flush (pad, self);
1486 gst_aggregator_stop (GstAggregator * agg)
1488 GstAggregatorClass *klass;
1491 gst_aggregator_reset_flow_values (agg);
1493 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1495 klass = GST_AGGREGATOR_GET_CLASS (agg);
1498 result = klass->stop (agg);
1502 agg->priv->has_peer_latency = FALSE;
1503 agg->priv->peer_latency_live = FALSE;
1504 agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1506 if (agg->priv->tags)
1507 gst_tag_list_unref (agg->priv->tags);
1508 agg->priv->tags = NULL;
1510 gst_aggregator_set_allocation (agg, NULL, NULL, NULL, NULL);
1515 /* GstElement vmethods implementations */
1516 static GstStateChangeReturn
1517 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1519 GstStateChangeReturn ret;
1520 GstAggregator *self = GST_AGGREGATOR (element);
1522 switch (transition) {
1523 case GST_STATE_CHANGE_READY_TO_PAUSED:
1524 if (!gst_aggregator_start (self))
1532 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1533 transition)) == GST_STATE_CHANGE_FAILURE)
1537 switch (transition) {
1538 case GST_STATE_CHANGE_PAUSED_TO_READY:
1539 if (!gst_aggregator_stop (self)) {
1540 /* What to do in this case? Error out? */
1541 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1553 GST_ERROR_OBJECT (element, "parent failed state change");
1558 GST_ERROR_OBJECT (element, "Subclass failed to start");
1559 return GST_STATE_CHANGE_FAILURE;
1564 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1566 GstAggregator *self = GST_AGGREGATOR (element);
1567 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1569 GST_INFO_OBJECT (pad, "Removing pad");
1572 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1573 gst_element_remove_pad (element, pad);
1575 self->priv->has_peer_latency = FALSE;
1576 SRC_BROADCAST (self);
1580 static GstAggregatorPad *
1581 gst_aggregator_default_create_new_pad (GstAggregator * self,
1582 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1584 GstAggregatorPad *agg_pad;
1585 GstAggregatorPrivate *priv = self->priv;
1589 if (templ->direction != GST_PAD_SINK ||
1590 g_strcmp0 (templ->name_template, "sink_%u") != 0)
1593 GST_OBJECT_LOCK (self);
1594 if (req_name == NULL || strlen (req_name) < 6
1595 || !g_str_has_prefix (req_name, "sink_")) {
1596 /* no name given when requesting the pad, use next available int */
1597 serial = ++priv->max_padserial;
1599 /* parse serial number from requested padname */
1600 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1601 if (serial > priv->max_padserial)
1602 priv->max_padserial = serial;
1605 name = g_strdup_printf ("sink_%u", serial);
1606 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1607 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1610 GST_OBJECT_UNLOCK (self);
1617 GST_WARNING_OBJECT (self, "request new pad that is not a SINK pad\n");
1623 gst_aggregator_request_new_pad (GstElement * element,
1624 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1626 GstAggregator *self;
1627 GstAggregatorPad *agg_pad;
1628 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1629 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1631 self = GST_AGGREGATOR (element);
1633 agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1635 GST_ERROR_OBJECT (element, "Couldn't create new pad");
1639 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1642 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1644 /* add the pad to the element */
1645 gst_element_add_pad (element, GST_PAD (agg_pad));
1647 return GST_PAD (agg_pad);
1650 /* Must be called with SRC_LOCK held */
1653 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1655 gboolean query_ret, live;
1656 GstClockTime our_latency, min, max;
1658 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1661 GST_WARNING_OBJECT (self, "Latency query failed");
1665 gst_query_parse_latency (query, &live, &min, &max);
1667 our_latency = self->priv->latency;
1669 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1670 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1671 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1675 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1676 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1677 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1678 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1679 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1683 self->priv->peer_latency_live = live;
1684 self->priv->peer_latency_min = min;
1685 self->priv->peer_latency_max = max;
1686 self->priv->has_peer_latency = TRUE;
1690 min += self->priv->sub_latency_min;
1691 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1692 && GST_CLOCK_TIME_IS_VALID (max))
1693 max += self->priv->sub_latency_max + our_latency;
1695 max = GST_CLOCK_TIME_NONE;
1697 SRC_BROADCAST (self);
1699 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1700 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1702 gst_query_set_latency (query, live, min, max);
1708 * MUST be called with the src_lock held.
1710 * See gst_aggregator_get_latency() for doc
1713 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1715 GstClockTime latency;
1717 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1719 if (!self->priv->has_peer_latency) {
1720 GstQuery *query = gst_query_new_latency ();
1723 ret = gst_aggregator_query_latency_unlocked (self, query);
1724 gst_query_unref (query);
1726 return GST_CLOCK_TIME_NONE;
1729 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1730 return GST_CLOCK_TIME_NONE;
1732 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1733 latency = self->priv->peer_latency_min;
1736 latency += self->priv->latency;
1737 latency += self->priv->sub_latency_min;
1743 * gst_aggregator_get_latency:
1744 * @self: a #GstAggregator
1746 * Retrieves the latency values reported by @self in response to the latency
1747 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1748 * will not wait for the clock.
1750 * Typically only called by subclasses.
1752 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1755 gst_aggregator_get_latency (GstAggregator * self)
1760 ret = gst_aggregator_get_latency_unlocked (self);
1767 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1769 GstAggregator *self = GST_AGGREGATOR (element);
1771 GST_STATE_LOCK (element);
1772 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1773 GST_STATE (element) < GST_STATE_PAUSED) {
1777 GstSeekType start_type, stop_type;
1780 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1781 &start, &stop_type, &stop);
1783 GST_OBJECT_LOCK (self);
1784 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1785 stop_type, stop, NULL);
1786 self->priv->seqnum = gst_event_get_seqnum (event);
1787 self->priv->first_buffer = FALSE;
1788 GST_OBJECT_UNLOCK (self);
1790 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1792 GST_STATE_UNLOCK (element);
1795 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1800 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1802 gboolean res = TRUE;
1804 switch (GST_QUERY_TYPE (query)) {
1805 case GST_QUERY_SEEKING:
1809 /* don't pass it along as some (file)sink might claim it does
1810 * whereas with a collectpads in between that will not likely work */
1811 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1812 gst_query_set_seeking (query, format, FALSE, 0, -1);
1817 case GST_QUERY_LATENCY:
1819 res = gst_aggregator_query_latency_unlocked (self, query);
1823 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1830 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1832 EventData *evdata = user_data;
1833 gboolean ret = TRUE;
1834 GstPad *peer = gst_pad_get_peer (pad);
1835 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1838 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1839 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1842 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1843 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1844 gst_object_unref (peer);
1849 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1850 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1852 GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1854 if (gst_pad_query (peer, seeking)) {
1857 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1859 if (seekable == FALSE) {
1860 GST_INFO_OBJECT (pad,
1861 "Source not seekable, We failed but it does not matter!");
1866 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1869 gst_query_unref (seeking);
1872 if (evdata->flush) {
1874 aggpad->priv->pending_flush_start = FALSE;
1875 aggpad->priv->pending_flush_stop = FALSE;
1876 PAD_UNLOCK (aggpad);
1879 evdata->one_actually_seeked = TRUE;
1882 evdata->result &= ret;
1884 /* Always send to all pads */
1889 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1890 GstEvent * event, gboolean flush, gboolean only_to_active_pads)
1894 evdata.event = event;
1895 evdata.result = TRUE;
1896 evdata.flush = flush;
1897 evdata.one_actually_seeked = FALSE;
1898 evdata.only_to_active_pads = only_to_active_pads;
1900 /* We first need to set all pads as flushing in a first pass
1901 * as flush_start flush_stop is sometimes sent synchronously
1902 * while we send the seek event */
1906 GST_OBJECT_LOCK (self);
1907 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1908 GstAggregatorPad *pad = l->data;
1911 pad->priv->pending_flush_start = TRUE;
1912 pad->priv->pending_flush_stop = FALSE;
1915 GST_OBJECT_UNLOCK (self);
1918 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1920 gst_event_unref (event);
1926 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1931 GstSeekType start_type, stop_type;
1935 GstAggregatorPrivate *priv = self->priv;
1937 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1938 &start, &stop_type, &stop);
1940 GST_INFO_OBJECT (self, "starting SEEK");
1942 flush = flags & GST_SEEK_FLAG_FLUSH;
1944 GST_OBJECT_LOCK (self);
1946 priv->pending_flush_start = TRUE;
1947 priv->flush_seeking = TRUE;
1950 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1951 stop_type, stop, NULL);
1953 /* Seeking sets a position */
1954 self->priv->first_buffer = FALSE;
1955 GST_OBJECT_UNLOCK (self);
1957 /* forward the seek upstream */
1959 gst_aggregator_forward_event_to_all_sinkpads (self, event, flush, FALSE);
1962 if (!evdata.result || !evdata.one_actually_seeked) {
1963 GST_OBJECT_LOCK (self);
1964 priv->flush_seeking = FALSE;
1965 priv->pending_flush_start = FALSE;
1966 GST_OBJECT_UNLOCK (self);
1969 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1971 return evdata.result;
1975 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1978 gboolean res = TRUE;
1980 switch (GST_EVENT_TYPE (event)) {
1981 case GST_EVENT_SEEK:
1983 gst_event_ref (event);
1984 res = gst_aggregator_do_seek (self, event);
1985 gst_event_unref (event);
1989 case GST_EVENT_NAVIGATION:
1991 /* navigation is rather pointless. */
1993 gst_event_unref (event);
2002 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
2003 * they will receive a QOS event that has earliest_time=0 (because we can't
2004 * have negative timestamps), and consider their buffer as too late */
2006 gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE,
2007 GST_EVENT_TYPE (event) == GST_EVENT_QOS);
2008 res = evdata.result;
2015 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
2018 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2020 return klass->src_event (GST_AGGREGATOR (parent), event);
2024 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
2027 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2029 return klass->src_query (GST_AGGREGATOR (parent), query);
2033 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
2034 GstObject * parent, GstPadMode mode, gboolean active)
2036 GstAggregator *self = GST_AGGREGATOR (parent);
2037 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2039 if (klass->src_activate) {
2040 if (klass->src_activate (self, mode, active) == FALSE) {
2045 if (active == TRUE) {
2047 case GST_PAD_MODE_PUSH:
2049 GST_INFO_OBJECT (pad, "Activating pad!");
2050 gst_aggregator_start_srcpad_task (self);
2055 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
2062 GST_INFO_OBJECT (self, "Deactivating srcpad");
2063 gst_aggregator_stop_srcpad_task (self, FALSE);
2069 gst_aggregator_default_sink_query (GstAggregator * self,
2070 GstAggregatorPad * aggpad, GstQuery * query)
2072 GstPad *pad = GST_PAD (aggpad);
2074 return gst_pad_query_default (pad, GST_OBJECT (self), query);
2078 gst_aggregator_finalize (GObject * object)
2080 GstAggregator *self = (GstAggregator *) object;
2082 g_mutex_clear (&self->priv->src_lock);
2083 g_cond_clear (&self->priv->src_cond);
2085 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
2089 * gst_aggregator_set_latency_property:
2090 * @agg: a #GstAggregator
2091 * @latency: the new latency value (in nanoseconds).
2093 * Sets the new latency value to @latency. This value is used to limit the
2094 * amount of time a pad waits for data to appear before considering the pad
2098 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
2102 g_return_if_fail (GST_IS_AGGREGATOR (self));
2103 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
2106 changed = (self->priv->latency != latency);
2111 GST_OBJECT_LOCK (self);
2112 /* First lock all the pads */
2113 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2114 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2118 self->priv->latency = latency;
2120 SRC_BROADCAST (self);
2122 /* Now wake up the pads */
2123 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2124 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2125 PAD_BROADCAST_EVENT (aggpad);
2126 PAD_UNLOCK (aggpad);
2128 GST_OBJECT_UNLOCK (self);
2134 gst_element_post_message (GST_ELEMENT_CAST (self),
2135 gst_message_new_latency (GST_OBJECT_CAST (self)));
2139 * gst_aggregator_get_latency_property:
2140 * @agg: a #GstAggregator
2142 * Gets the latency value. See gst_aggregator_set_latency for
2145 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
2146 * before a pad is deemed unresponsive. A value of -1 means an
2150 gst_aggregator_get_latency_property (GstAggregator * agg)
2154 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
2156 GST_OBJECT_LOCK (agg);
2157 res = agg->priv->latency;
2158 GST_OBJECT_UNLOCK (agg);
2164 gst_aggregator_set_property (GObject * object, guint prop_id,
2165 const GValue * value, GParamSpec * pspec)
2167 GstAggregator *agg = GST_AGGREGATOR (object);
2171 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
2173 case PROP_START_TIME_SELECTION:
2174 agg->priv->start_time_selection = g_value_get_enum (value);
2176 case PROP_START_TIME:
2177 agg->priv->start_time = g_value_get_uint64 (value);
2180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2186 gst_aggregator_get_property (GObject * object, guint prop_id,
2187 GValue * value, GParamSpec * pspec)
2189 GstAggregator *agg = GST_AGGREGATOR (object);
2193 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
2195 case PROP_START_TIME_SELECTION:
2196 g_value_set_enum (value, agg->priv->start_time_selection);
2198 case PROP_START_TIME:
2199 g_value_set_uint64 (value, agg->priv->start_time);
2202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2207 /* GObject vmethods implementations */
2209 gst_aggregator_class_init (GstAggregatorClass * klass)
2211 GObjectClass *gobject_class = (GObjectClass *) klass;
2212 GstElementClass *gstelement_class = (GstElementClass *) klass;
2214 aggregator_parent_class = g_type_class_peek_parent (klass);
2215 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
2217 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
2218 GST_DEBUG_FG_MAGENTA, "GstAggregator");
2220 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
2222 klass->sink_event = gst_aggregator_default_sink_event;
2223 klass->sink_query = gst_aggregator_default_sink_query;
2225 klass->src_event = gst_aggregator_default_src_event;
2226 klass->src_query = gst_aggregator_default_src_query;
2228 klass->create_new_pad = gst_aggregator_default_create_new_pad;
2229 klass->update_src_caps = gst_aggregator_default_update_src_caps;
2230 klass->fixate_src_caps = gst_aggregator_default_fixate_src_caps;
2231 klass->negotiated_src_caps = gst_aggregator_default_negotiated_src_caps;
2233 gstelement_class->request_new_pad =
2234 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
2235 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
2236 gstelement_class->release_pad =
2237 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
2238 gstelement_class->change_state =
2239 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
2241 gobject_class->set_property = gst_aggregator_set_property;
2242 gobject_class->get_property = gst_aggregator_get_property;
2243 gobject_class->finalize = gst_aggregator_finalize;
2245 g_object_class_install_property (gobject_class, PROP_LATENCY,
2246 g_param_spec_int64 ("latency", "Buffer latency",
2247 "Additional latency in live mode to allow upstream "
2248 "to take longer to produce buffers for the current "
2249 "position (in nanoseconds)", 0,
2250 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
2251 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2253 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
2254 g_param_spec_enum ("start-time-selection", "Start Time Selection",
2255 "Decides which start time is output",
2256 gst_aggregator_start_time_selection_get_type (),
2257 DEFAULT_START_TIME_SELECTION,
2258 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2260 g_object_class_install_property (gobject_class, PROP_START_TIME,
2261 g_param_spec_uint64 ("start-time", "Start Time",
2262 "Start time to use if start-time-selection=set", 0,
2264 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2266 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
2270 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
2272 GstPadTemplate *pad_template;
2273 GstAggregatorPrivate *priv;
2275 g_return_if_fail (klass->aggregate != NULL);
2278 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2279 GstAggregatorPrivate);
2284 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2285 g_return_if_fail (pad_template != NULL);
2287 priv->max_padserial = -1;
2288 priv->tags_changed = FALSE;
2290 self->priv->peer_latency_live = FALSE;
2291 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2292 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2293 self->priv->has_peer_latency = FALSE;
2294 gst_aggregator_reset_flow_values (self);
2296 self->srcpad = gst_pad_new_from_template (pad_template, "src");
2298 gst_pad_set_event_function (self->srcpad,
2299 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2300 gst_pad_set_query_function (self->srcpad,
2301 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2302 gst_pad_set_activatemode_function (self->srcpad,
2303 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2305 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2307 self->priv->latency = DEFAULT_LATENCY;
2308 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2309 self->priv->start_time = DEFAULT_START_TIME;
2311 g_mutex_init (&self->priv->src_lock);
2312 g_cond_init (&self->priv->src_cond);
2315 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2316 * method to get to the padtemplates */
2318 gst_aggregator_get_type (void)
2320 static volatile gsize type = 0;
2322 if (g_once_init_enter (&type)) {
2324 static const GTypeInfo info = {
2325 sizeof (GstAggregatorClass),
2328 (GClassInitFunc) gst_aggregator_class_init,
2331 sizeof (GstAggregator),
2333 (GInstanceInitFunc) gst_aggregator_init,
2336 _type = g_type_register_static (GST_TYPE_ELEMENT,
2337 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2338 g_once_init_leave (&type, _type);
2343 /* Must be called with SRC lock and PAD lock held */
2345 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2347 /* Empty queue always has space */
2348 if (aggpad->priv->num_buffers == 0 && aggpad->priv->clipped_buffer == NULL)
2351 /* We also want at least two buffers, one is being processed and one is ready
2352 * for the next iteration when we operate in live mode. */
2353 if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2356 /* zero latency, if there is a buffer, it's full */
2357 if (self->priv->latency == 0)
2360 /* Allow no more buffers than the latency */
2361 return (aggpad->priv->time_level <= self->priv->latency);
2364 /* Must be called with the PAD_LOCK held */
2366 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2368 GstClockTime timestamp;
2370 if (GST_BUFFER_DTS_IS_VALID (buffer))
2371 timestamp = GST_BUFFER_DTS (buffer);
2373 timestamp = GST_BUFFER_PTS (buffer);
2375 if (timestamp == GST_CLOCK_TIME_NONE) {
2377 timestamp = aggpad->priv->head_position;
2379 timestamp = aggpad->priv->tail_position;
2383 if (GST_BUFFER_DURATION_IS_VALID (buffer))
2384 timestamp += GST_BUFFER_DURATION (buffer);
2387 aggpad->priv->head_position = timestamp;
2389 aggpad->priv->tail_position = timestamp;
2391 update_time_level (aggpad, head);
2394 static GstFlowReturn
2395 gst_aggregator_pad_chain_internal (GstAggregator * self,
2396 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2398 GstFlowReturn flow_return;
2399 GstClockTime buf_pts;
2401 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2403 PAD_FLUSH_LOCK (aggpad);
2406 flow_return = aggpad->priv->flow_return;
2407 if (flow_return != GST_FLOW_OK)
2410 if (aggpad->priv->pending_eos == TRUE)
2413 PAD_UNLOCK (aggpad);
2415 buf_pts = GST_BUFFER_PTS (buffer);
2419 GST_OBJECT_LOCK (self);
2422 if (aggpad->priv->first_buffer) {
2423 self->priv->has_peer_latency = FALSE;
2424 aggpad->priv->first_buffer = FALSE;
2427 if (gst_aggregator_pad_has_space (self, aggpad)
2428 && aggpad->priv->flow_return == GST_FLOW_OK) {
2430 g_queue_push_head (&aggpad->priv->buffers, buffer);
2432 g_queue_push_tail (&aggpad->priv->buffers, buffer);
2433 apply_buffer (aggpad, buffer, head);
2434 aggpad->priv->num_buffers++;
2436 SRC_BROADCAST (self);
2440 flow_return = aggpad->priv->flow_return;
2441 if (flow_return != GST_FLOW_OK) {
2442 GST_OBJECT_UNLOCK (self);
2446 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2447 GST_OBJECT_UNLOCK (self);
2449 PAD_WAIT_EVENT (aggpad);
2451 PAD_UNLOCK (aggpad);
2454 if (self->priv->first_buffer) {
2455 GstClockTime start_time;
2457 switch (self->priv->start_time_selection) {
2458 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2462 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2463 GST_OBJECT_LOCK (aggpad);
2464 if (aggpad->priv->head_segment.format == GST_FORMAT_TIME) {
2465 start_time = buf_pts;
2466 if (start_time != -1) {
2467 start_time = MAX (start_time, aggpad->priv->head_segment.start);
2469 gst_segment_to_running_time (&aggpad->priv->head_segment,
2470 GST_FORMAT_TIME, start_time);
2474 GST_WARNING_OBJECT (aggpad,
2475 "Ignoring request of selecting the first start time "
2476 "as the segment is a %s segment instead of a time segment",
2477 gst_format_get_name (aggpad->segment.format));
2479 GST_OBJECT_UNLOCK (aggpad);
2481 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2482 start_time = self->priv->start_time;
2483 if (start_time == -1)
2488 if (start_time != -1) {
2489 if (self->segment.position == -1)
2490 self->segment.position = start_time;
2492 self->segment.position = MIN (start_time, self->segment.position);
2494 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2495 GST_TIME_ARGS (start_time));
2499 PAD_UNLOCK (aggpad);
2500 GST_OBJECT_UNLOCK (self);
2503 PAD_FLUSH_UNLOCK (aggpad);
2505 GST_DEBUG_OBJECT (aggpad, "Done chaining");
2510 PAD_UNLOCK (aggpad);
2511 PAD_FLUSH_UNLOCK (aggpad);
2513 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2514 gst_flow_get_name (flow_return));
2516 gst_buffer_unref (buffer);
2521 PAD_UNLOCK (aggpad);
2522 PAD_FLUSH_UNLOCK (aggpad);
2524 gst_buffer_unref (buffer);
2525 GST_DEBUG_OBJECT (aggpad, "We are EOS already...");
2527 return GST_FLOW_EOS;
2530 static GstFlowReturn
2531 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2533 return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2534 GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2538 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2541 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2542 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2544 if (GST_QUERY_IS_SERIALIZED (query)) {
2547 while (!gst_aggregator_pad_queue_is_empty (aggpad)
2548 && aggpad->priv->flow_return == GST_FLOW_OK) {
2549 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2550 PAD_WAIT_EVENT (aggpad);
2553 if (aggpad->priv->flow_return != GST_FLOW_OK)
2556 PAD_UNLOCK (aggpad);
2559 return klass->sink_query (GST_AGGREGATOR (parent),
2560 GST_AGGREGATOR_PAD (pad), query);
2563 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2564 gst_flow_get_name (aggpad->priv->flow_return));
2565 PAD_UNLOCK (aggpad);
2569 static GstFlowReturn
2570 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2573 GstFlowReturn ret = GST_FLOW_OK;
2574 GstAggregator *self = GST_AGGREGATOR (parent);
2575 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2576 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2578 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
2579 /* && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE */ ) {
2583 if (aggpad->priv->flow_return != GST_FLOW_OK
2584 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2585 ret = aggpad->priv->flow_return;
2589 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2590 GST_OBJECT_LOCK (aggpad);
2591 gst_event_copy_segment (event, &aggpad->priv->head_segment);
2592 aggpad->priv->head_position = aggpad->priv->head_segment.position;
2593 update_time_level (aggpad, TRUE);
2594 GST_OBJECT_UNLOCK (aggpad);
2597 if (GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2598 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT,
2600 g_queue_push_head (&aggpad->priv->buffers, event);
2602 SRC_BROADCAST (self);
2604 PAD_UNLOCK (aggpad);
2609 gboolean is_caps = (GST_EVENT_TYPE (event) == GST_EVENT_CAPS);
2611 if (!klass->sink_event (self, aggpad, event)) {
2612 /* Copied from GstPad to convert boolean to a GstFlowReturn in
2613 * the event handling func */
2614 ret = is_caps ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_ERROR;
2621 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2622 gst_flow_get_name (aggpad->priv->flow_return));
2623 PAD_UNLOCK (aggpad);
2625 if (GST_EVENT_IS_STICKY (event))
2626 gst_pad_store_sticky_event (pad, event);
2627 gst_event_unref (event);
2633 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2634 GstObject * parent, GstPadMode mode, gboolean active)
2636 GstAggregator *self = GST_AGGREGATOR (parent);
2637 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2639 if (active == FALSE) {
2641 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2642 SRC_BROADCAST (self);
2646 aggpad->priv->flow_return = GST_FLOW_OK;
2647 PAD_BROADCAST_EVENT (aggpad);
2648 PAD_UNLOCK (aggpad);
2654 /***********************************
2655 * GstAggregatorPad implementation *
2656 ************************************/
2657 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2660 gst_aggregator_pad_constructed (GObject * object)
2662 GstPad *pad = GST_PAD (object);
2664 gst_pad_set_chain_function (pad,
2665 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2666 gst_pad_set_event_full_function_full (pad,
2667 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
2668 gst_pad_set_query_function (pad,
2669 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2670 gst_pad_set_activatemode_function (pad,
2671 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2675 gst_aggregator_pad_finalize (GObject * object)
2677 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2679 g_cond_clear (&pad->priv->event_cond);
2680 g_mutex_clear (&pad->priv->flush_lock);
2681 g_mutex_clear (&pad->priv->lock);
2683 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2687 gst_aggregator_pad_dispose (GObject * object)
2689 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2691 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2693 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2697 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2699 GObjectClass *gobject_class = (GObjectClass *) klass;
2701 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2703 gobject_class->constructed = gst_aggregator_pad_constructed;
2704 gobject_class->finalize = gst_aggregator_pad_finalize;
2705 gobject_class->dispose = gst_aggregator_pad_dispose;
2709 gst_aggregator_pad_init (GstAggregatorPad * pad)
2712 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2713 GstAggregatorPadPrivate);
2715 g_queue_init (&pad->priv->buffers);
2716 g_cond_init (&pad->priv->event_cond);
2718 g_mutex_init (&pad->priv->flush_lock);
2719 g_mutex_init (&pad->priv->lock);
2721 gst_aggregator_pad_reset_unlocked (pad);
2724 /* Must be called with the PAD_LOCK held */
2726 gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad)
2728 pad->priv->num_buffers--;
2729 GST_TRACE_OBJECT (pad, "Consuming buffer");
2730 if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
2731 pad->priv->pending_eos = FALSE;
2732 pad->priv->eos = TRUE;
2734 PAD_BROADCAST_EVENT (pad);
2737 /* Must be called with the PAD_LOCK held */
2739 gst_aggregator_pad_clip_buffer_unlocked (GstAggregatorPad * pad)
2741 GstAggregator *self = NULL;
2742 GstAggregatorClass *aggclass;
2743 GstBuffer *buffer = NULL;
2745 while (pad->priv->clipped_buffer == NULL &&
2746 GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->buffers))) {
2747 buffer = g_queue_pop_tail (&pad->priv->buffers);
2749 apply_buffer (pad, buffer, FALSE);
2751 /* We only take the parent here so that it's not taken if the buffer is
2752 * already clipped or if the queue is empty.
2755 self = GST_AGGREGATOR (gst_pad_get_parent_element (GST_PAD (pad)));
2757 gst_buffer_unref (buffer);
2761 aggclass = GST_AGGREGATOR_GET_CLASS (self);
2764 if (aggclass->clip) {
2765 GST_TRACE_OBJECT (pad, "Clipping: %" GST_PTR_FORMAT, buffer);
2767 buffer = aggclass->clip (self, pad, buffer);
2769 if (buffer == NULL) {
2770 gst_aggregator_pad_buffer_consumed (pad);
2771 GST_TRACE_OBJECT (pad, "Clipping consumed the buffer");
2775 pad->priv->clipped_buffer = buffer;
2779 gst_object_unref (self);
2783 * gst_aggregator_pad_steal_buffer:
2784 * @pad: the pad to get buffer from
2786 * Steal the ref to the buffer currently queued in @pad.
2788 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2789 * queued. You should unref the buffer after usage.
2792 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2798 gst_aggregator_pad_clip_buffer_unlocked (pad);
2800 buffer = pad->priv->clipped_buffer;
2801 pad->priv->clipped_buffer = NULL;
2804 gst_aggregator_pad_buffer_consumed (pad);
2805 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2814 * gst_aggregator_pad_drop_buffer:
2815 * @pad: the pad where to drop any pending buffer
2817 * Drop the buffer currently queued in @pad.
2819 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2822 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2826 buf = gst_aggregator_pad_steal_buffer (pad);
2831 gst_buffer_unref (buf);
2836 * gst_aggregator_pad_get_buffer:
2837 * @pad: the pad to get buffer from
2839 * Returns: (transfer full): A reference to the buffer in @pad or
2840 * NULL if no buffer was queued. You should unref the buffer after
2844 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2850 gst_aggregator_pad_clip_buffer_unlocked (pad);
2852 if (pad->priv->clipped_buffer) {
2853 buffer = gst_buffer_ref (pad->priv->clipped_buffer);
2863 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2868 is_eos = pad->priv->eos;
2875 * gst_aggregator_merge_tags:
2876 * @self: a #GstAggregator
2877 * @tags: a #GstTagList to merge
2878 * @mode: the #GstTagMergeMode to use
2880 * Adds tags to so-called pending tags, which will be processed
2881 * before pushing out data downstream.
2883 * Note that this is provided for convenience, and the subclass is
2884 * not required to use this and can still do tag handling on its own.
2889 gst_aggregator_merge_tags (GstAggregator * self,
2890 const GstTagList * tags, GstTagMergeMode mode)
2894 g_return_if_fail (GST_IS_AGGREGATOR (self));
2895 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2897 /* FIXME Check if we can use OBJECT lock here! */
2898 GST_OBJECT_LOCK (self);
2900 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2901 otags = self->priv->tags;
2902 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2904 gst_tag_list_unref (otags);
2905 self->priv->tags_changed = TRUE;
2906 GST_OBJECT_UNLOCK (self);
2910 * gst_aggregator_set_latency:
2911 * @self: a #GstAggregator
2912 * @min_latency: minimum latency
2913 * @max_latency: maximum latency
2915 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2916 * latency is. Will also post a LATENCY message on the bus so the pipeline
2917 * can reconfigure its global latency.
2920 gst_aggregator_set_latency (GstAggregator * self,
2921 GstClockTime min_latency, GstClockTime max_latency)
2923 gboolean changed = FALSE;
2925 g_return_if_fail (GST_IS_AGGREGATOR (self));
2926 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2927 g_return_if_fail (max_latency >= min_latency);
2930 if (self->priv->sub_latency_min != min_latency) {
2931 self->priv->sub_latency_min = min_latency;
2934 if (self->priv->sub_latency_max != max_latency) {
2935 self->priv->sub_latency_max = max_latency;
2940 SRC_BROADCAST (self);
2944 gst_element_post_message (GST_ELEMENT_CAST (self),
2945 gst_message_new_latency (GST_OBJECT_CAST (self)));
2950 * gst_aggregator_get_buffer_pool:
2951 * @self: a #GstAggregator
2953 * Returns: (transfer full): the instance of the #GstBufferPool used
2954 * by @trans; free it after use it
2957 gst_aggregator_get_buffer_pool (GstAggregator * self)
2959 GstBufferPool *pool;
2961 g_return_val_if_fail (GST_IS_AGGREGATOR (self), NULL);
2963 GST_OBJECT_LOCK (self);
2964 pool = self->priv->pool;
2966 gst_object_ref (pool);
2967 GST_OBJECT_UNLOCK (self);
2973 * gst_aggregator_get_allocator:
2974 * @self: a #GstAggregator
2975 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
2977 * @params: (out) (allow-none) (transfer full): the
2978 * #GstAllocationParams of @allocator
2980 * Lets #GstAggregator sub-classes get the memory @allocator
2981 * acquired by the base class and its @params.
2983 * Unref the @allocator after use it.
2986 gst_aggregator_get_allocator (GstAggregator * self,
2987 GstAllocator ** allocator, GstAllocationParams * params)
2989 g_return_if_fail (GST_IS_AGGREGATOR (self));
2992 *allocator = self->priv->allocator ?
2993 gst_object_ref (self->priv->allocator) : NULL;
2996 *params = self->priv->allocation_params;