1 /* GStreamer aggregator base class
2 * Copyright (C) 2014 Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
3 * Copyright (C) 2014 Thibault Saunier <tsaunier@gnome.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 * SECTION: gstaggregator
24 * @title: GstAggregator
25 * @short_description: manages a set of pads with the purpose of
26 * aggregating their buffers.
27 * @see_also: gstcollectpads for historical reasons.
29 * Manages a set of pads with the purpose of aggregating their buffers.
30 * Control is given to the subclass when all pads have data.
32 * * Base class for mixers and muxers. Subclasses should at least implement
33 * the #GstAggregatorClass.aggregate() virtual method.
35 * * Installs a #GstPadChainFunction, a #GstPadEventFullFunction and a
36 * #GstPadQueryFunction to queue all serialized data packets per sink pad.
37 * Subclasses should not overwrite those, but instead implement
38 * #GstAggregatorClass.sink_event() and #GstAggregatorClass.sink_query() as
41 * * When data is queued on all pads, the aggregate vmethod is called.
43 * * One can peek at the data on any given GstAggregatorPad with the
44 * gst_aggregator_pad_get_buffer () method, and take ownership of it
45 * with the gst_aggregator_pad_steal_buffer () method. When a buffer
46 * has been taken with steal_buffer (), a new buffer can be queued
49 * * If the subclass wishes to push a buffer downstream in its aggregate
50 * implementation, it should do so through the
51 * gst_aggregator_finish_buffer () method. This method will take care
52 * of sending and ordering mandatory events such as stream start, caps
55 * * Same goes for EOS events, which should not be pushed directly by the
56 * subclass, it should instead return GST_FLOW_EOS in its aggregate
59 * * Note that the aggregator logic regarding gap event handling is to turn
60 * these into gap buffers with matching PTS and duration. It will also
61 * flag these buffers with GST_BUFFER_FLAG_GAP and GST_BUFFER_FLAG_DROPPABLE
62 * to ease their identification and subsequent processing.
70 #include <string.h> /* strlen */
72 #include "gstaggregator.h"
76 GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
77 GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
78 GST_AGGREGATOR_START_TIME_SELECTION_SET
79 } GstAggregatorStartTimeSelection;
82 gst_aggregator_start_time_selection_get_type (void)
84 static GType gtype = 0;
87 static const GEnumValue values[] = {
88 {GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
89 "Start at 0 running time (default)", "zero"},
90 {GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
91 "Start at first observed input running time", "first"},
92 {GST_AGGREGATOR_START_TIME_SELECTION_SET,
93 "Set start time with start-time property", "set"},
97 gtype = g_enum_register_static ("GstAggregatorStartTimeSelection", values);
102 /* Might become API */
103 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
104 const GstTagList * tags, GstTagMergeMode mode);
105 static void gst_aggregator_set_latency_property (GstAggregator * agg,
107 static gint64 gst_aggregator_get_latency_property (GstAggregator * agg);
109 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
111 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
112 #define GST_CAT_DEFAULT aggregator_debug
114 /* Locking order, locks in this element must always be taken in this order
116 * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
117 * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
118 * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
119 * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
120 * standard element object lock -> GST_OBJECT_LOCK(agg)
121 * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
122 * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
123 * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
126 /* GstAggregatorPad definitions */
127 #define PAD_LOCK(pad) G_STMT_START { \
128 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
130 g_mutex_lock(&pad->priv->lock); \
131 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
135 #define PAD_UNLOCK(pad) G_STMT_START { \
136 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
138 g_mutex_unlock(&pad->priv->lock); \
139 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
144 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
145 GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p", \
147 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
148 (&((GstAggregatorPad*)pad)->priv->lock)); \
149 GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
153 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
154 GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p", \
156 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
160 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
161 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
163 g_mutex_lock(&pad->priv->flush_lock); \
164 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
168 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
169 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
171 g_mutex_unlock(&pad->priv->flush_lock); \
172 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
176 #define SRC_LOCK(self) G_STMT_START { \
177 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
179 g_mutex_lock(&self->priv->src_lock); \
180 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
184 #define SRC_UNLOCK(self) G_STMT_START { \
185 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
187 g_mutex_unlock(&self->priv->src_lock); \
188 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
192 #define SRC_WAIT(self) G_STMT_START { \
193 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
195 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
196 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
200 #define SRC_BROADCAST(self) G_STMT_START { \
201 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
203 if (self->priv->aggregate_id) \
204 gst_clock_id_unschedule (self->priv->aggregate_id); \
205 g_cond_broadcast(&(self->priv->src_cond)); \
208 struct _GstAggregatorPadPrivate
210 /* Following fields are protected by the PAD_LOCK */
211 GstFlowReturn flow_return;
212 gboolean pending_flush_start;
213 gboolean pending_flush_stop;
215 gboolean first_buffer;
217 GQueue data; /* buffers, events and queries */
218 GstBuffer *clipped_buffer;
221 /* used to track fill state of queues, only used with live-src and when
222 * latency property is set to > 0 */
223 GstClockTime head_position;
224 GstClockTime tail_position;
225 GstClockTime head_time; /* running time */
226 GstClockTime tail_time;
227 GstClockTime time_level; /* how much head is ahead of tail */
228 GstSegment head_segment; /* segment before the queue */
236 /* This lock prevents a flush start processing happening while
237 * the chain function is also happening.
242 /* Must be called with PAD_LOCK held */
244 gst_aggregator_pad_reset_unlocked (GstAggregatorPad * aggpad)
246 aggpad->priv->eos = FALSE;
247 aggpad->priv->flow_return = GST_FLOW_OK;
248 GST_OBJECT_LOCK (aggpad);
249 gst_segment_init (&aggpad->segment, GST_FORMAT_UNDEFINED);
250 gst_segment_init (&aggpad->priv->head_segment, GST_FORMAT_UNDEFINED);
251 GST_OBJECT_UNLOCK (aggpad);
252 aggpad->priv->head_position = GST_CLOCK_TIME_NONE;
253 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
254 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
255 aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
256 aggpad->priv->time_level = 0;
257 aggpad->priv->first_buffer = TRUE;
261 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
263 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
266 gst_aggregator_pad_reset_unlocked (aggpad);
270 return klass->flush (aggpad, agg);
275 /*************************************
276 * GstAggregator implementation *
277 *************************************/
278 static GstElementClass *aggregator_parent_class = NULL;
280 /* All members are protected by the object lock unless otherwise noted */
282 struct _GstAggregatorPrivate
286 /* Our state is >= PAUSED */
287 gboolean running; /* protected by src_lock */
290 gboolean send_stream_start; /* protected by srcpad stream lock */
291 gboolean send_segment;
292 gboolean flush_seeking;
293 gboolean pending_flush_start;
294 gboolean send_eos; /* protected by srcpad stream lock */
296 GstCaps *srccaps; /* protected by the srcpad stream lock */
299 gboolean tags_changed;
301 gboolean peer_latency_live; /* protected by src_lock */
302 GstClockTime peer_latency_min; /* protected by src_lock */
303 GstClockTime peer_latency_max; /* protected by src_lock */
304 gboolean has_peer_latency; /* protected by src_lock */
306 GstClockTime sub_latency_min; /* protected by src_lock */
307 GstClockTime sub_latency_max; /* protected by src_lock */
310 GstClockID aggregate_id; /* protected by src_lock */
314 gboolean first_buffer; /* protected by object lock */
315 GstAggregatorStartTimeSelection start_time_selection;
316 GstClockTime start_time;
318 /* protected by the object lock */
319 GstQuery *allocation_query;
320 GstAllocator *allocator;
322 GstAllocationParams allocation_params;
325 gint64 latency; /* protected by both src_lock and all pad locks */
328 /* Seek event forwarding helper */
334 gboolean only_to_active_pads;
338 gboolean one_actually_seeked;
341 #define DEFAULT_LATENCY 0
342 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
343 #define DEFAULT_START_TIME (-1)
349 PROP_START_TIME_SELECTION,
354 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
355 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
358 * gst_aggregator_iterate_sinkpads:
359 * @self: The #GstAggregator
360 * @func: (scope call): The function to call.
361 * @user_data: (closure): The data to pass to @func.
363 * Iterate the sinkpads of aggregator to call a function on them.
365 * This method guarantees that @func will be called only once for each
368 * Returns: %FALSE if there are no sinkpads or if @func returned %FALSE
371 gst_aggregator_iterate_sinkpads (GstAggregator * self,
372 GstAggregatorPadForeachFunc func, gpointer user_data)
374 gboolean result = FALSE;
376 gboolean done = FALSE;
377 GValue item = { 0, };
378 GList *seen_pads = NULL;
380 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
386 switch (gst_iterator_next (iter, &item)) {
387 case GST_ITERATOR_OK:
389 GstAggregatorPad *pad;
391 pad = g_value_get_object (&item);
393 /* if already pushed, skip. FIXME, find something faster to tag pads */
394 if (pad == NULL || g_list_find (seen_pads, pad)) {
395 g_value_reset (&item);
399 GST_LOG_OBJECT (pad, "calling function %s on pad",
400 GST_DEBUG_FUNCPTR_NAME (func));
402 result = func (self, pad, user_data);
406 seen_pads = g_list_prepend (seen_pads, pad);
408 g_value_reset (&item);
411 case GST_ITERATOR_RESYNC:
412 gst_iterator_resync (iter);
414 case GST_ITERATOR_ERROR:
415 GST_ERROR_OBJECT (self,
416 "Could not iterate over internally linked pads");
419 case GST_ITERATOR_DONE:
424 g_value_unset (&item);
425 gst_iterator_free (iter);
427 if (seen_pads == NULL) {
428 GST_DEBUG_OBJECT (self, "No pad seen");
432 g_list_free (seen_pads);
439 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
441 return (g_queue_peek_tail (&pad->priv->data) == NULL &&
442 pad->priv->clipped_buffer == NULL);
446 gst_aggregator_check_pads_ready (GstAggregator * self)
448 GstAggregatorPad *pad;
450 gboolean have_buffer = TRUE;
451 gboolean have_event_or_query = FALSE;
453 GST_LOG_OBJECT (self, "checking pads");
455 GST_OBJECT_LOCK (self);
457 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
458 if (sinkpads == NULL)
461 for (l = sinkpads; l != NULL; l = l->next) {
466 if (pad->priv->num_buffers == 0) {
467 if (!gst_aggregator_pad_queue_is_empty (pad))
468 have_event_or_query = TRUE;
469 if (!pad->priv->eos) {
472 /* If not live we need data on all pads, so leave the loop */
473 if (!self->priv->peer_latency_live) {
478 } else if (self->priv->peer_latency_live) {
479 /* In live mode, having a single pad with buffers is enough to
480 * generate a start time from it. In non-live mode all pads need
483 self->priv->first_buffer = FALSE;
489 if (!have_buffer && !have_event_or_query)
493 self->priv->first_buffer = FALSE;
495 GST_OBJECT_UNLOCK (self);
496 GST_LOG_OBJECT (self, "pads are ready");
501 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
502 GST_OBJECT_UNLOCK (self);
507 if (have_event_or_query)
508 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet,"
509 " but waking up for serialized event");
511 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
512 GST_OBJECT_UNLOCK (self);
513 return have_event_or_query;
518 gst_aggregator_reset_flow_values (GstAggregator * self)
520 GST_OBJECT_LOCK (self);
521 self->priv->send_stream_start = TRUE;
522 self->priv->send_segment = TRUE;
523 gst_segment_init (&self->segment, GST_FORMAT_TIME);
524 self->priv->first_buffer = TRUE;
525 GST_OBJECT_UNLOCK (self);
529 gst_aggregator_push_mandatory_events (GstAggregator * self)
531 GstAggregatorPrivate *priv = self->priv;
532 GstEvent *segment = NULL;
533 GstEvent *tags = NULL;
535 if (self->priv->send_stream_start) {
538 GST_INFO_OBJECT (self, "pushing stream start");
539 /* stream-start (FIXME: create id based on input ids) */
540 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
541 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
542 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
544 self->priv->send_stream_start = FALSE;
547 if (self->priv->srccaps) {
549 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
550 self->priv->srccaps);
551 if (!gst_pad_push_event (self->srcpad,
552 gst_event_new_caps (self->priv->srccaps))) {
553 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
555 gst_caps_unref (self->priv->srccaps);
556 self->priv->srccaps = NULL;
559 GST_OBJECT_LOCK (self);
560 if (self->priv->send_segment && !self->priv->flush_seeking) {
561 segment = gst_event_new_segment (&self->segment);
563 if (!self->priv->seqnum)
564 self->priv->seqnum = gst_event_get_seqnum (segment);
566 gst_event_set_seqnum (segment, self->priv->seqnum);
567 self->priv->send_segment = FALSE;
569 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
572 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
573 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
574 priv->tags_changed = FALSE;
576 GST_OBJECT_UNLOCK (self);
579 gst_pad_push_event (self->srcpad, segment);
581 gst_pad_push_event (self->srcpad, tags);
586 * gst_aggregator_set_src_caps:
587 * @self: The #GstAggregator
588 * @caps: The #GstCaps to set on the src pad.
590 * Sets the caps to be used on the src pad.
593 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
595 GST_PAD_STREAM_LOCK (self->srcpad);
596 gst_caps_replace (&self->priv->srccaps, caps);
597 gst_aggregator_push_mandatory_events (self);
598 GST_PAD_STREAM_UNLOCK (self->srcpad);
602 * gst_aggregator_finish_buffer:
603 * @self: The #GstAggregator
604 * @buffer: (transfer full): the #GstBuffer to push.
606 * This method will push the provided output buffer downstream. If needed,
607 * mandatory events such as stream-start, caps, and segment events will be
608 * sent before pushing the buffer.
611 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
613 gst_aggregator_push_mandatory_events (self);
615 GST_OBJECT_LOCK (self);
616 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
617 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
618 GST_OBJECT_UNLOCK (self);
619 return gst_pad_push (self->srcpad, buffer);
621 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
622 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
623 GST_OBJECT_UNLOCK (self);
624 gst_buffer_unref (buffer);
630 gst_aggregator_push_eos (GstAggregator * self)
633 gst_aggregator_push_mandatory_events (self);
635 event = gst_event_new_eos ();
637 GST_OBJECT_LOCK (self);
638 self->priv->send_eos = FALSE;
639 gst_event_set_seqnum (event, self->priv->seqnum);
640 GST_OBJECT_UNLOCK (self);
642 gst_pad_push_event (self->srcpad, event);
646 gst_aggregator_get_next_time (GstAggregator * self)
648 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
650 if (klass->get_next_time)
651 return klass->get_next_time (self);
653 return GST_CLOCK_TIME_NONE;
657 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
659 GstClockTime latency;
667 latency = gst_aggregator_get_latency_unlocked (self);
669 if (gst_aggregator_check_pads_ready (self)) {
670 GST_DEBUG_OBJECT (self, "all pads have data");
676 /* Before waiting, check if we're actually still running */
677 if (!self->priv->running || !self->priv->send_eos) {
683 start = gst_aggregator_get_next_time (self);
685 /* If we're not live, or if we use the running time
686 * of the first buffer as start time, we wait until
687 * all pads have buffers.
688 * Otherwise (i.e. if we are live!), we wait on the clock
689 * and if a pad does not have a buffer in time we ignore
692 GST_OBJECT_LOCK (self);
693 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
694 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
695 !GST_CLOCK_TIME_IS_VALID (start) ||
696 (self->priv->first_buffer
697 && self->priv->start_time_selection ==
698 GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
699 /* We wake up here when something happened, and below
700 * then check if we're ready now. If we return FALSE,
701 * we will be directly called again.
703 GST_OBJECT_UNLOCK (self);
706 GstClockTime base_time, time;
708 GstClockReturn status;
709 GstClockTimeDiff jitter;
711 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
712 GST_TIME_ARGS (start));
714 base_time = GST_ELEMENT_CAST (self)->base_time;
715 clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
716 GST_OBJECT_UNLOCK (self);
718 time = base_time + start;
721 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
722 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
723 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
724 GST_TIME_ARGS (time),
725 GST_TIME_ARGS (base_time),
726 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
727 GST_TIME_ARGS (gst_clock_get_time (clock)));
729 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
730 gst_object_unref (clock);
734 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
737 if (self->priv->aggregate_id) {
738 gst_clock_id_unref (self->priv->aggregate_id);
739 self->priv->aggregate_id = NULL;
742 GST_DEBUG_OBJECT (self,
743 "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
744 status, GST_STIME_ARGS (jitter));
747 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
754 res = gst_aggregator_check_pads_ready (self);
761 gst_aggregator_do_events_and_queries (GstAggregator * self,
762 GstAggregatorPad * pad, gpointer user_data)
764 GstEvent *event = NULL;
765 GstQuery *query = NULL;
766 GstAggregatorClass *klass = NULL;
767 gboolean *processed_event = user_data;
774 if (pad->priv->clipped_buffer == NULL &&
775 !GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->data))) {
776 if (GST_IS_EVENT (g_queue_peek_tail (&pad->priv->data)))
777 event = gst_event_ref (g_queue_peek_tail (&pad->priv->data));
778 if (GST_IS_QUERY (g_queue_peek_tail (&pad->priv->data)))
779 query = g_queue_peek_tail (&pad->priv->data);
782 if (event || query) {
786 *processed_event = TRUE;
788 klass = GST_AGGREGATOR_GET_CLASS (self);
791 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
792 gst_event_ref (event);
793 ret = klass->sink_event (self, pad, event);
796 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS)
797 pad->priv->negotiated = ret;
798 if (g_queue_peek_tail (&pad->priv->data) == event)
799 gst_event_unref (g_queue_pop_tail (&pad->priv->data));
800 gst_event_unref (event);
802 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, query);
803 ret = klass->sink_query (self, pad, query);
806 if (g_queue_peek_tail (&pad->priv->data) == query) {
809 s = gst_query_writable_structure (query);
810 gst_structure_set (s, "gst-aggregator-retval", G_TYPE_BOOLEAN, ret,
812 g_queue_pop_tail (&pad->priv->data);
816 PAD_BROADCAST_EVENT (pad);
819 } while (event || query);
825 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
826 GstFlowReturn flow_return, gboolean full)
831 if (flow_return == GST_FLOW_NOT_LINKED)
832 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
834 aggpad->priv->flow_return = flow_return;
836 item = g_queue_peek_head_link (&aggpad->priv->data);
838 GList *next = item->next;
840 /* In partial flush, we do like the pad, we get rid of non-sticky events
843 if (full || GST_IS_BUFFER (item->data) ||
844 GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
845 GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
846 !GST_EVENT_IS_STICKY (item->data)) {
847 if (!GST_IS_QUERY (item->data))
848 gst_mini_object_unref (item->data);
849 g_queue_delete_link (&aggpad->priv->data, item);
853 aggpad->priv->num_buffers = 0;
854 gst_buffer_replace (&aggpad->priv->clipped_buffer, NULL);
856 PAD_BROADCAST_EVENT (aggpad);
861 gst_aggregator_default_update_src_caps (GstAggregator * agg, GstCaps * caps,
864 *ret = gst_caps_ref (caps);
870 gst_aggregator_default_fixate_src_caps (GstAggregator * agg, GstCaps * caps)
872 caps = gst_caps_fixate (caps);
878 gst_aggregator_default_negotiated_src_caps (GstAggregator * agg, GstCaps * caps)
884 /* takes ownership of the pool, allocator and query */
886 gst_aggregator_set_allocation (GstAggregator * self,
887 GstBufferPool * pool, GstAllocator * allocator,
888 GstAllocationParams * params, GstQuery * query)
890 GstAllocator *oldalloc;
891 GstBufferPool *oldpool;
894 GST_DEBUG ("storing allocation query");
896 GST_OBJECT_LOCK (self);
897 oldpool = self->priv->pool;
898 self->priv->pool = pool;
900 oldalloc = self->priv->allocator;
901 self->priv->allocator = allocator;
903 oldquery = self->priv->allocation_query;
904 self->priv->allocation_query = query;
907 self->priv->allocation_params = *params;
909 gst_allocation_params_init (&self->priv->allocation_params);
910 GST_OBJECT_UNLOCK (self);
913 GST_DEBUG_OBJECT (self, "deactivating old pool %p", oldpool);
914 gst_buffer_pool_set_active (oldpool, FALSE);
915 gst_object_unref (oldpool);
918 gst_object_unref (oldalloc);
921 gst_query_unref (oldquery);
928 gst_aggregator_decide_allocation (GstAggregator * self, GstQuery * query)
930 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
932 if (aggclass->decide_allocation)
933 if (!aggclass->decide_allocation (self, query))
940 gst_aggregator_do_allocation (GstAggregator * self, GstCaps * caps)
943 gboolean result = TRUE;
944 GstBufferPool *pool = NULL;
945 GstAllocator *allocator;
946 GstAllocationParams params;
948 /* find a pool for the negotiated caps now */
949 GST_DEBUG_OBJECT (self, "doing allocation query");
950 query = gst_query_new_allocation (caps, TRUE);
951 if (!gst_pad_peer_query (self->srcpad, query)) {
952 /* not a problem, just debug a little */
953 GST_DEBUG_OBJECT (self, "peer ALLOCATION query failed");
956 GST_DEBUG_OBJECT (self, "calling decide_allocation");
957 result = gst_aggregator_decide_allocation (self, query);
959 GST_DEBUG_OBJECT (self, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
963 goto no_decide_allocation;
965 /* we got configuration from our peer or the decide_allocation method,
967 if (gst_query_get_n_allocation_params (query) > 0) {
968 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
971 gst_allocation_params_init (¶ms);
974 if (gst_query_get_n_allocation_pools (query) > 0)
975 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
979 gst_aggregator_set_allocation (self, pool, allocator, ¶ms, query);
984 no_decide_allocation:
986 GST_WARNING_OBJECT (self, "Failed to decide allocation");
987 gst_query_unref (query);
994 /* WITH SRC_LOCK held */
996 gst_aggregator_update_src_caps (GstAggregator * self)
998 GstAggregatorClass *agg_klass = GST_AGGREGATOR_GET_CLASS (self);
999 GstCaps *downstream_caps, *template_caps, *caps = NULL;
1000 GstFlowReturn ret = GST_FLOW_OK;
1002 template_caps = gst_pad_get_pad_template_caps (self->srcpad);
1003 downstream_caps = gst_pad_peer_query_caps (self->srcpad, template_caps);
1005 if (gst_caps_is_empty (downstream_caps)) {
1006 GST_INFO_OBJECT (self, "Downstream caps (%"
1007 GST_PTR_FORMAT ") not compatible with pad template caps (%"
1008 GST_PTR_FORMAT ")", downstream_caps, template_caps);
1009 ret = GST_FLOW_NOT_NEGOTIATED;
1013 g_assert (agg_klass->update_src_caps);
1014 GST_DEBUG_OBJECT (self, "updating caps from %" GST_PTR_FORMAT,
1016 ret = agg_klass->update_src_caps (self, downstream_caps, &caps);
1017 if (ret < GST_FLOW_OK) {
1018 GST_WARNING_OBJECT (self, "Subclass failed to update provided caps");
1021 if ((caps == NULL || gst_caps_is_empty (caps)) && ret >= GST_FLOW_OK) {
1022 ret = GST_FLOW_NOT_NEGOTIATED;
1025 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1027 #ifdef GST_ENABLE_EXTRA_CHECKS
1028 if (!gst_caps_is_subset (caps, template_caps)) {
1029 GstCaps *intersection;
1031 GST_ERROR_OBJECT (self,
1032 "update_src_caps returned caps %" GST_PTR_FORMAT
1033 " which are not a real subset of the template caps %"
1034 GST_PTR_FORMAT, caps, template_caps);
1035 g_warning ("%s: update_src_caps returned caps which are not a real "
1036 "subset of the filter caps", GST_ELEMENT_NAME (self));
1039 gst_caps_intersect_full (template_caps, caps, GST_CAPS_INTERSECT_FIRST);
1040 gst_caps_unref (caps);
1041 caps = intersection;
1045 if (gst_caps_is_any (caps)) {
1049 if (!gst_caps_is_fixed (caps)) {
1050 g_assert (agg_klass->fixate_src_caps);
1052 GST_DEBUG_OBJECT (self, "fixate caps from %" GST_PTR_FORMAT, caps);
1053 if (!(caps = agg_klass->fixate_src_caps (self, caps))) {
1054 GST_WARNING_OBJECT (self, "Subclass failed to fixate provided caps");
1055 ret = GST_FLOW_NOT_NEGOTIATED;
1058 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1061 if (agg_klass->negotiated_src_caps) {
1062 if (!agg_klass->negotiated_src_caps (self, caps)) {
1063 GST_WARNING_OBJECT (self, "Subclass failed to accept negotiated caps");
1064 ret = GST_FLOW_NOT_NEGOTIATED;
1069 gst_aggregator_set_src_caps (self, caps);
1071 if (!gst_aggregator_do_allocation (self, caps)) {
1072 GST_WARNING_OBJECT (self, "Allocation negotiation failed");
1073 ret = GST_FLOW_NOT_NEGOTIATED;
1077 gst_caps_unref (downstream_caps);
1078 gst_caps_unref (template_caps);
1081 gst_caps_unref (caps);
1087 gst_aggregator_aggregate_func (GstAggregator * self)
1089 GstAggregatorPrivate *priv = self->priv;
1090 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1091 gboolean timeout = FALSE;
1093 if (self->priv->running == FALSE) {
1094 GST_DEBUG_OBJECT (self, "Not running anymore");
1098 GST_LOG_OBJECT (self, "Checking aggregate");
1099 while (priv->send_eos && priv->running) {
1100 GstFlowReturn flow_return = GST_FLOW_OK;
1101 gboolean processed_event = FALSE;
1103 gst_aggregator_iterate_sinkpads (self, gst_aggregator_do_events_and_queries,
1106 if (!gst_aggregator_wait_and_check (self, &timeout))
1109 gst_aggregator_iterate_sinkpads (self, gst_aggregator_do_events_and_queries,
1111 if (processed_event)
1114 if (gst_pad_check_reconfigure (GST_AGGREGATOR_SRC_PAD (self))) {
1115 flow_return = gst_aggregator_update_src_caps (self);
1116 if (flow_return != GST_FLOW_OK)
1117 gst_pad_mark_reconfigure (GST_AGGREGATOR_SRC_PAD (self));
1120 if (timeout || flow_return >= GST_FLOW_OK) {
1121 GST_TRACE_OBJECT (self, "Actually aggregating!");
1122 flow_return = klass->aggregate (self, timeout);
1125 if (flow_return == GST_AGGREGATOR_FLOW_NEED_DATA)
1128 GST_OBJECT_LOCK (self);
1129 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
1130 /* We don't want to set the pads to flushing, but we want to
1131 * stop the thread, so just break here */
1132 GST_OBJECT_UNLOCK (self);
1135 GST_OBJECT_UNLOCK (self);
1137 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
1138 gst_aggregator_push_eos (self);
1141 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
1143 if (flow_return != GST_FLOW_OK) {
1146 GST_OBJECT_LOCK (self);
1147 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
1148 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1150 gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
1152 GST_OBJECT_UNLOCK (self);
1157 /* Pause the task here, the only ways to get here are:
1158 * 1) We're stopping, in which case the task is stopped anyway
1159 * 2) We got a flow error above, in which case it might take
1160 * some time to forward the flow return upstream and we
1161 * would otherwise call the task function over and over
1162 * again without doing anything
1164 gst_pad_pause_task (self->srcpad);
1168 gst_aggregator_start (GstAggregator * self)
1170 GstAggregatorClass *klass;
1173 self->priv->send_stream_start = TRUE;
1174 self->priv->send_segment = TRUE;
1175 self->priv->send_eos = TRUE;
1176 self->priv->srccaps = NULL;
1178 gst_aggregator_set_allocation (self, NULL, NULL, NULL, NULL);
1180 klass = GST_AGGREGATOR_GET_CLASS (self);
1183 result = klass->start (self);
1191 _check_pending_flush_stop (GstAggregatorPad * pad)
1196 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
1203 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
1205 gboolean res = TRUE;
1207 GST_INFO_OBJECT (self, "%s srcpad task",
1208 flush_start ? "Pausing" : "Stopping");
1211 self->priv->running = FALSE;
1212 SRC_BROADCAST (self);
1216 res = gst_pad_push_event (self->srcpad, flush_start);
1219 gst_pad_stop_task (self->srcpad);
1225 gst_aggregator_start_srcpad_task (GstAggregator * self)
1227 GST_INFO_OBJECT (self, "Starting srcpad task");
1229 self->priv->running = TRUE;
1230 gst_pad_start_task (GST_PAD (self->srcpad),
1231 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
1234 static GstFlowReturn
1235 gst_aggregator_flush (GstAggregator * self)
1237 GstFlowReturn ret = GST_FLOW_OK;
1238 GstAggregatorPrivate *priv = self->priv;
1239 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1241 GST_DEBUG_OBJECT (self, "Flushing everything");
1242 GST_OBJECT_LOCK (self);
1243 priv->send_segment = TRUE;
1244 priv->flush_seeking = FALSE;
1245 priv->tags_changed = FALSE;
1246 GST_OBJECT_UNLOCK (self);
1248 ret = klass->flush (self);
1254 /* Called with GstAggregator's object lock held */
1257 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
1260 GstAggregatorPad *tmppad;
1262 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
1263 tmppad = (GstAggregatorPad *) tmp->data;
1265 if (_check_pending_flush_stop (tmppad) == FALSE) {
1266 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
1267 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
1276 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
1279 GstAggregatorPrivate *priv = self->priv;
1280 GstAggregatorPadPrivate *padpriv = aggpad->priv;
1282 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
1284 PAD_FLUSH_LOCK (aggpad);
1286 if (padpriv->pending_flush_start) {
1287 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
1289 padpriv->pending_flush_start = FALSE;
1290 padpriv->pending_flush_stop = TRUE;
1292 PAD_UNLOCK (aggpad);
1294 GST_OBJECT_LOCK (self);
1295 if (priv->flush_seeking) {
1296 /* If flush_seeking we forward the first FLUSH_START */
1297 if (priv->pending_flush_start) {
1298 priv->pending_flush_start = FALSE;
1299 GST_OBJECT_UNLOCK (self);
1301 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
1302 gst_aggregator_stop_srcpad_task (self, event);
1304 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
1305 GST_PAD_STREAM_LOCK (self->srcpad);
1306 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
1309 GST_OBJECT_UNLOCK (self);
1310 gst_event_unref (event);
1313 GST_OBJECT_UNLOCK (self);
1314 gst_event_unref (event);
1316 PAD_FLUSH_UNLOCK (aggpad);
1319 /* Must be called with the the PAD_LOCK held */
1321 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1323 GstAggregatorPadPrivate *priv = aggpad->priv;
1326 if (GST_CLOCK_TIME_IS_VALID (priv->head_position) &&
1327 priv->head_segment.format == GST_FORMAT_TIME)
1328 priv->head_time = gst_segment_to_running_time (&priv->head_segment,
1329 GST_FORMAT_TIME, priv->head_position);
1331 priv->head_time = GST_CLOCK_TIME_NONE;
1333 if (!GST_CLOCK_TIME_IS_VALID (priv->tail_time))
1334 priv->tail_time = priv->head_time;
1336 if (GST_CLOCK_TIME_IS_VALID (priv->tail_position) &&
1337 aggpad->segment.format == GST_FORMAT_TIME)
1338 priv->tail_time = gst_segment_to_running_time (&aggpad->segment,
1339 GST_FORMAT_TIME, priv->tail_position);
1341 priv->tail_time = priv->head_time;
1344 if (priv->head_time == GST_CLOCK_TIME_NONE ||
1345 priv->tail_time == GST_CLOCK_TIME_NONE) {
1346 priv->time_level = 0;
1350 if (priv->tail_time > priv->head_time)
1351 priv->time_level = 0;
1353 priv->time_level = priv->head_time - priv->tail_time;
1357 /* GstAggregator vmethods default implementations */
1359 gst_aggregator_default_sink_event (GstAggregator * self,
1360 GstAggregatorPad * aggpad, GstEvent * event)
1362 gboolean res = TRUE;
1363 GstPad *pad = GST_PAD (aggpad);
1364 GstAggregatorPrivate *priv = self->priv;
1366 GST_DEBUG_OBJECT (aggpad, "Got event: %" GST_PTR_FORMAT, event);
1368 switch (GST_EVENT_TYPE (event)) {
1369 case GST_EVENT_FLUSH_START:
1371 gst_aggregator_flush_start (self, aggpad, event);
1372 /* We forward only in one case: right after flush_seeking */
1376 case GST_EVENT_FLUSH_STOP:
1378 gst_aggregator_pad_flush (aggpad, self);
1379 GST_OBJECT_LOCK (self);
1380 if (priv->flush_seeking) {
1381 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1382 if (gst_aggregator_all_flush_stop_received_locked (self)) {
1383 GST_OBJECT_UNLOCK (self);
1384 /* That means we received FLUSH_STOP/FLUSH_STOP on
1385 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1386 gst_aggregator_flush (self);
1387 gst_pad_push_event (self->srcpad, event);
1390 priv->send_eos = TRUE;
1391 SRC_BROADCAST (self);
1394 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1395 GST_PAD_STREAM_UNLOCK (self->srcpad);
1396 gst_aggregator_start_srcpad_task (self);
1398 GST_OBJECT_UNLOCK (self);
1401 GST_OBJECT_UNLOCK (self);
1404 /* We never forward the event */
1411 g_assert (aggpad->priv->num_buffers == 0);
1412 aggpad->priv->eos = TRUE;
1413 PAD_UNLOCK (aggpad);
1414 SRC_BROADCAST (self);
1418 case GST_EVENT_SEGMENT:
1421 GST_OBJECT_LOCK (aggpad);
1422 gst_event_copy_segment (event, &aggpad->segment);
1423 /* We've got a new segment, tail_position is now meaningless
1424 * and may interfere with the time_level calculation
1426 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
1427 update_time_level (aggpad, FALSE);
1428 GST_OBJECT_UNLOCK (aggpad);
1429 PAD_UNLOCK (aggpad);
1431 GST_OBJECT_LOCK (self);
1432 self->priv->seqnum = gst_event_get_seqnum (event);
1433 GST_OBJECT_UNLOCK (self);
1436 case GST_EVENT_STREAM_START:
1442 GstClockTime pts, endpts;
1443 GstClockTime duration;
1446 gst_event_parse_gap (event, &pts, &duration);
1447 gapbuf = gst_buffer_new ();
1449 if (GST_CLOCK_TIME_IS_VALID (duration))
1450 endpts = pts + duration;
1452 endpts = GST_CLOCK_TIME_NONE;
1454 GST_OBJECT_LOCK (aggpad);
1455 res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1457 GST_OBJECT_UNLOCK (aggpad);
1460 GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1464 if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1465 duration = endpts - pts;
1467 duration = GST_CLOCK_TIME_NONE;
1469 GST_BUFFER_PTS (gapbuf) = pts;
1470 GST_BUFFER_DURATION (gapbuf) = duration;
1471 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1472 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1474 /* Remove GAP event so we can replace it with the buffer */
1475 if (g_queue_peek_tail (&aggpad->priv->data) == event)
1476 gst_event_unref (g_queue_pop_tail (&aggpad->priv->data));
1478 if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1480 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1490 gst_event_parse_tag (event, &tags);
1492 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1493 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1494 gst_event_unref (event);
1506 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1507 return gst_pad_event_default (pad, GST_OBJECT (self), event);
1510 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1512 gst_event_unref (event);
1517 static inline gboolean
1518 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1519 gpointer unused_udata)
1521 gst_aggregator_pad_flush (pad, self);
1524 pad->priv->flow_return = GST_FLOW_FLUSHING;
1525 pad->priv->negotiated = FALSE;
1526 PAD_BROADCAST_EVENT (pad);
1533 gst_aggregator_stop (GstAggregator * agg)
1535 GstAggregatorClass *klass;
1538 gst_aggregator_reset_flow_values (agg);
1540 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1542 klass = GST_AGGREGATOR_GET_CLASS (agg);
1545 result = klass->stop (agg);
1549 agg->priv->has_peer_latency = FALSE;
1550 agg->priv->peer_latency_live = FALSE;
1551 agg->priv->peer_latency_min = agg->priv->peer_latency_max = 0;
1553 if (agg->priv->tags)
1554 gst_tag_list_unref (agg->priv->tags);
1555 agg->priv->tags = NULL;
1557 gst_aggregator_set_allocation (agg, NULL, NULL, NULL, NULL);
1562 /* GstElement vmethods implementations */
1563 static GstStateChangeReturn
1564 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1566 GstStateChangeReturn ret;
1567 GstAggregator *self = GST_AGGREGATOR (element);
1569 switch (transition) {
1570 case GST_STATE_CHANGE_READY_TO_PAUSED:
1571 if (!gst_aggregator_start (self))
1579 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1580 transition)) == GST_STATE_CHANGE_FAILURE)
1584 switch (transition) {
1585 case GST_STATE_CHANGE_PAUSED_TO_READY:
1586 if (!gst_aggregator_stop (self)) {
1587 /* What to do in this case? Error out? */
1588 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1600 GST_ERROR_OBJECT (element, "parent failed state change");
1605 GST_ERROR_OBJECT (element, "Subclass failed to start");
1606 return GST_STATE_CHANGE_FAILURE;
1611 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1613 GstAggregator *self = GST_AGGREGATOR (element);
1614 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1616 GST_INFO_OBJECT (pad, "Removing pad");
1619 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1620 gst_element_remove_pad (element, pad);
1622 self->priv->has_peer_latency = FALSE;
1623 SRC_BROADCAST (self);
1627 static GstAggregatorPad *
1628 gst_aggregator_default_create_new_pad (GstAggregator * self,
1629 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1631 GstAggregatorPad *agg_pad;
1632 GstAggregatorPrivate *priv = self->priv;
1636 if (templ->direction != GST_PAD_SINK)
1639 if (templ->presence != GST_PAD_REQUEST)
1642 GST_OBJECT_LOCK (self);
1643 if (req_name == NULL || strlen (req_name) < 6
1644 || !g_str_has_prefix (req_name, "sink_")) {
1645 /* no name given when requesting the pad, use next available int */
1646 serial = ++priv->max_padserial;
1648 /* parse serial number from requested padname */
1649 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1650 if (serial > priv->max_padserial)
1651 priv->max_padserial = serial;
1654 name = g_strdup_printf ("sink_%u", serial);
1655 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1656 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1659 GST_OBJECT_UNLOCK (self);
1666 GST_WARNING_OBJECT (self, "request new pad that is not a SINK pad");
1671 GST_WARNING_OBJECT (self, "request new pad that is not a REQUEST pad");
1677 gst_aggregator_request_new_pad (GstElement * element,
1678 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1680 GstAggregator *self;
1681 GstAggregatorPad *agg_pad;
1682 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1683 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1685 self = GST_AGGREGATOR (element);
1687 agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1689 GST_ERROR_OBJECT (element, "Couldn't create new pad");
1693 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1696 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1698 /* add the pad to the element */
1699 gst_element_add_pad (element, GST_PAD (agg_pad));
1701 return GST_PAD (agg_pad);
1704 /* Must be called with SRC_LOCK held */
1707 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1709 gboolean query_ret, live;
1710 GstClockTime our_latency, min, max;
1712 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1715 GST_WARNING_OBJECT (self, "Latency query failed");
1719 gst_query_parse_latency (query, &live, &min, &max);
1721 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1722 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1723 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1727 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1728 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1729 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1730 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1731 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1735 our_latency = self->priv->latency;
1737 self->priv->peer_latency_live = live;
1738 self->priv->peer_latency_min = min;
1739 self->priv->peer_latency_max = max;
1740 self->priv->has_peer_latency = TRUE;
1744 min += self->priv->sub_latency_min;
1745 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1746 && GST_CLOCK_TIME_IS_VALID (max))
1747 max += self->priv->sub_latency_max + our_latency;
1749 max = GST_CLOCK_TIME_NONE;
1751 SRC_BROADCAST (self);
1753 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1754 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1756 gst_query_set_latency (query, live, min, max);
1762 * MUST be called with the src_lock held.
1764 * See gst_aggregator_get_latency() for doc
1767 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1769 GstClockTime latency;
1771 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1773 if (!self->priv->has_peer_latency) {
1774 GstQuery *query = gst_query_new_latency ();
1777 ret = gst_aggregator_query_latency_unlocked (self, query);
1778 gst_query_unref (query);
1780 return GST_CLOCK_TIME_NONE;
1783 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1784 return GST_CLOCK_TIME_NONE;
1786 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1787 latency = self->priv->peer_latency_min;
1790 latency += self->priv->latency;
1791 latency += self->priv->sub_latency_min;
1797 * gst_aggregator_get_latency:
1798 * @self: a #GstAggregator
1800 * Retrieves the latency values reported by @self in response to the latency
1801 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1802 * will not wait for the clock.
1804 * Typically only called by subclasses.
1806 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1809 gst_aggregator_get_latency (GstAggregator * self)
1814 ret = gst_aggregator_get_latency_unlocked (self);
1821 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1823 GstAggregator *self = GST_AGGREGATOR (element);
1825 GST_STATE_LOCK (element);
1826 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1827 GST_STATE (element) < GST_STATE_PAUSED) {
1831 GstSeekType start_type, stop_type;
1834 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1835 &start, &stop_type, &stop);
1837 GST_OBJECT_LOCK (self);
1838 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1839 stop_type, stop, NULL);
1840 self->priv->seqnum = gst_event_get_seqnum (event);
1841 self->priv->first_buffer = FALSE;
1842 GST_OBJECT_UNLOCK (self);
1844 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1846 GST_STATE_UNLOCK (element);
1849 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1854 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1856 gboolean res = TRUE;
1858 switch (GST_QUERY_TYPE (query)) {
1859 case GST_QUERY_SEEKING:
1863 /* don't pass it along as some (file)sink might claim it does
1864 * whereas with a collectpads in between that will not likely work */
1865 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1866 gst_query_set_seeking (query, format, FALSE, 0, -1);
1871 case GST_QUERY_LATENCY:
1873 res = gst_aggregator_query_latency_unlocked (self, query);
1877 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1884 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1886 EventData *evdata = user_data;
1887 gboolean ret = TRUE;
1888 GstPad *peer = gst_pad_get_peer (pad);
1889 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1892 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1893 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1896 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1897 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1898 gst_object_unref (peer);
1903 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1904 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1906 GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1908 if (gst_pad_query (peer, seeking)) {
1911 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1913 if (seekable == FALSE) {
1914 GST_INFO_OBJECT (pad,
1915 "Source not seekable, We failed but it does not matter!");
1920 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1923 gst_query_unref (seeking);
1926 if (evdata->flush) {
1928 aggpad->priv->pending_flush_start = FALSE;
1929 aggpad->priv->pending_flush_stop = FALSE;
1930 PAD_UNLOCK (aggpad);
1933 evdata->one_actually_seeked = TRUE;
1936 evdata->result &= ret;
1938 /* Always send to all pads */
1943 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1946 evdata->result = TRUE;
1947 evdata->one_actually_seeked = FALSE;
1949 /* We first need to set all pads as flushing in a first pass
1950 * as flush_start flush_stop is sometimes sent synchronously
1951 * while we send the seek event */
1952 if (evdata->flush) {
1955 GST_OBJECT_LOCK (self);
1956 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1957 GstAggregatorPad *pad = l->data;
1960 pad->priv->pending_flush_start = TRUE;
1961 pad->priv->pending_flush_stop = FALSE;
1964 GST_OBJECT_UNLOCK (self);
1967 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, evdata);
1969 gst_event_unref (evdata->event);
1973 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1978 GstSeekType start_type, stop_type;
1981 EventData evdata = { 0, };
1982 GstAggregatorPrivate *priv = self->priv;
1984 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1985 &start, &stop_type, &stop);
1987 GST_INFO_OBJECT (self, "starting SEEK");
1989 flush = flags & GST_SEEK_FLAG_FLUSH;
1991 GST_OBJECT_LOCK (self);
1993 priv->pending_flush_start = TRUE;
1994 priv->flush_seeking = TRUE;
1997 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1998 stop_type, stop, NULL);
2000 /* Seeking sets a position */
2001 self->priv->first_buffer = FALSE;
2002 GST_OBJECT_UNLOCK (self);
2004 /* forward the seek upstream */
2005 evdata.event = event;
2006 evdata.flush = flush;
2007 evdata.only_to_active_pads = FALSE;
2008 gst_aggregator_forward_event_to_all_sinkpads (self, &evdata);
2011 if (!evdata.result || !evdata.one_actually_seeked) {
2012 GST_OBJECT_LOCK (self);
2013 priv->flush_seeking = FALSE;
2014 priv->pending_flush_start = FALSE;
2015 GST_OBJECT_UNLOCK (self);
2018 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
2020 return evdata.result;
2024 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
2026 EventData evdata = { 0, };
2028 switch (GST_EVENT_TYPE (event)) {
2029 case GST_EVENT_SEEK:
2030 /* _do_seek() unrefs the event. */
2031 return gst_aggregator_do_seek (self, event);
2032 case GST_EVENT_NAVIGATION:
2033 /* navigation is rather pointless. */
2034 gst_event_unref (event);
2040 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
2041 * they will receive a QOS event that has earliest_time=0 (because we can't
2042 * have negative timestamps), and consider their buffer as too late */
2043 evdata.event = event;
2044 evdata.flush = FALSE;
2045 evdata.only_to_active_pads = GST_EVENT_TYPE (event) == GST_EVENT_QOS;
2046 gst_aggregator_forward_event_to_all_sinkpads (self, &evdata);
2047 return evdata.result;
2051 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
2054 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2056 return klass->src_event (GST_AGGREGATOR (parent), event);
2060 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
2063 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2065 return klass->src_query (GST_AGGREGATOR (parent), query);
2069 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
2070 GstObject * parent, GstPadMode mode, gboolean active)
2072 GstAggregator *self = GST_AGGREGATOR (parent);
2073 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2075 if (klass->src_activate) {
2076 if (klass->src_activate (self, mode, active) == FALSE) {
2081 if (active == TRUE) {
2083 case GST_PAD_MODE_PUSH:
2085 GST_INFO_OBJECT (pad, "Activating pad!");
2086 gst_aggregator_start_srcpad_task (self);
2091 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
2098 GST_INFO_OBJECT (self, "Deactivating srcpad");
2099 gst_aggregator_stop_srcpad_task (self, FALSE);
2105 gst_aggregator_default_sink_query (GstAggregator * self,
2106 GstAggregatorPad * aggpad, GstQuery * query)
2108 GstPad *pad = GST_PAD (aggpad);
2110 if (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION) {
2111 GstQuery *decide_query = NULL;
2112 GstAggregatorClass *agg_class;
2115 GST_OBJECT_LOCK (self);
2117 if (G_UNLIKELY (!aggpad->priv->negotiated)) {
2118 GST_DEBUG_OBJECT (self,
2119 "not negotiated yet, can't answer ALLOCATION query");
2120 PAD_UNLOCK (aggpad);
2121 GST_OBJECT_UNLOCK (self);
2126 if ((decide_query = self->priv->allocation_query))
2127 gst_query_ref (decide_query);
2128 PAD_UNLOCK (aggpad);
2129 GST_OBJECT_UNLOCK (self);
2131 GST_DEBUG_OBJECT (self,
2132 "calling propose allocation with query %" GST_PTR_FORMAT, decide_query);
2134 agg_class = GST_AGGREGATOR_GET_CLASS (self);
2136 /* pass the query to the propose_allocation vmethod if any */
2137 if (agg_class->propose_allocation)
2138 ret = agg_class->propose_allocation (self, aggpad, decide_query, query);
2143 gst_query_unref (decide_query);
2145 GST_DEBUG_OBJECT (self, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
2149 return gst_pad_query_default (pad, GST_OBJECT (self), query);
2153 gst_aggregator_finalize (GObject * object)
2155 GstAggregator *self = (GstAggregator *) object;
2157 g_mutex_clear (&self->priv->src_lock);
2158 g_cond_clear (&self->priv->src_cond);
2160 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
2164 * gst_aggregator_set_latency_property:
2165 * @agg: a #GstAggregator
2166 * @latency: the new latency value (in nanoseconds).
2168 * Sets the new latency value to @latency. This value is used to limit the
2169 * amount of time a pad waits for data to appear before considering the pad
2173 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
2177 g_return_if_fail (GST_IS_AGGREGATOR (self));
2178 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
2181 changed = (self->priv->latency != latency);
2186 GST_OBJECT_LOCK (self);
2187 /* First lock all the pads */
2188 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2189 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2193 self->priv->latency = latency;
2195 SRC_BROADCAST (self);
2197 /* Now wake up the pads */
2198 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2199 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2200 PAD_BROADCAST_EVENT (aggpad);
2201 PAD_UNLOCK (aggpad);
2203 GST_OBJECT_UNLOCK (self);
2209 gst_element_post_message (GST_ELEMENT_CAST (self),
2210 gst_message_new_latency (GST_OBJECT_CAST (self)));
2214 * gst_aggregator_get_latency_property:
2215 * @agg: a #GstAggregator
2217 * Gets the latency value. See gst_aggregator_set_latency for
2220 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
2221 * before a pad is deemed unresponsive. A value of -1 means an
2225 gst_aggregator_get_latency_property (GstAggregator * agg)
2229 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
2231 GST_OBJECT_LOCK (agg);
2232 res = agg->priv->latency;
2233 GST_OBJECT_UNLOCK (agg);
2239 gst_aggregator_set_property (GObject * object, guint prop_id,
2240 const GValue * value, GParamSpec * pspec)
2242 GstAggregator *agg = GST_AGGREGATOR (object);
2246 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
2248 case PROP_START_TIME_SELECTION:
2249 agg->priv->start_time_selection = g_value_get_enum (value);
2251 case PROP_START_TIME:
2252 agg->priv->start_time = g_value_get_uint64 (value);
2255 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2261 gst_aggregator_get_property (GObject * object, guint prop_id,
2262 GValue * value, GParamSpec * pspec)
2264 GstAggregator *agg = GST_AGGREGATOR (object);
2268 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
2270 case PROP_START_TIME_SELECTION:
2271 g_value_set_enum (value, agg->priv->start_time_selection);
2273 case PROP_START_TIME:
2274 g_value_set_uint64 (value, agg->priv->start_time);
2277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2282 /* GObject vmethods implementations */
2284 gst_aggregator_class_init (GstAggregatorClass * klass)
2286 GObjectClass *gobject_class = (GObjectClass *) klass;
2287 GstElementClass *gstelement_class = (GstElementClass *) klass;
2289 aggregator_parent_class = g_type_class_peek_parent (klass);
2290 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
2292 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
2293 GST_DEBUG_FG_MAGENTA, "GstAggregator");
2295 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
2297 klass->sink_event = gst_aggregator_default_sink_event;
2298 klass->sink_query = gst_aggregator_default_sink_query;
2300 klass->src_event = gst_aggregator_default_src_event;
2301 klass->src_query = gst_aggregator_default_src_query;
2303 klass->create_new_pad = gst_aggregator_default_create_new_pad;
2304 klass->update_src_caps = gst_aggregator_default_update_src_caps;
2305 klass->fixate_src_caps = gst_aggregator_default_fixate_src_caps;
2306 klass->negotiated_src_caps = gst_aggregator_default_negotiated_src_caps;
2308 gstelement_class->request_new_pad =
2309 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
2310 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
2311 gstelement_class->release_pad =
2312 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
2313 gstelement_class->change_state =
2314 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
2316 gobject_class->set_property = gst_aggregator_set_property;
2317 gobject_class->get_property = gst_aggregator_get_property;
2318 gobject_class->finalize = gst_aggregator_finalize;
2320 g_object_class_install_property (gobject_class, PROP_LATENCY,
2321 g_param_spec_int64 ("latency", "Buffer latency",
2322 "Additional latency in live mode to allow upstream "
2323 "to take longer to produce buffers for the current "
2324 "position (in nanoseconds)", 0,
2325 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
2326 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2328 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
2329 g_param_spec_enum ("start-time-selection", "Start Time Selection",
2330 "Decides which start time is output",
2331 gst_aggregator_start_time_selection_get_type (),
2332 DEFAULT_START_TIME_SELECTION,
2333 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2335 g_object_class_install_property (gobject_class, PROP_START_TIME,
2336 g_param_spec_uint64 ("start-time", "Start Time",
2337 "Start time to use if start-time-selection=set", 0,
2339 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2341 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
2342 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_do_events_and_queries);
2346 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
2348 GstPadTemplate *pad_template;
2349 GstAggregatorPrivate *priv;
2351 g_return_if_fail (klass->aggregate != NULL);
2354 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2355 GstAggregatorPrivate);
2360 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2361 g_return_if_fail (pad_template != NULL);
2363 priv->max_padserial = -1;
2364 priv->tags_changed = FALSE;
2366 self->priv->peer_latency_live = FALSE;
2367 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2368 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2369 self->priv->has_peer_latency = FALSE;
2370 gst_aggregator_reset_flow_values (self);
2372 self->srcpad = gst_pad_new_from_template (pad_template, "src");
2374 gst_pad_set_event_function (self->srcpad,
2375 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2376 gst_pad_set_query_function (self->srcpad,
2377 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2378 gst_pad_set_activatemode_function (self->srcpad,
2379 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2381 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2383 self->priv->latency = DEFAULT_LATENCY;
2384 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2385 self->priv->start_time = DEFAULT_START_TIME;
2387 g_mutex_init (&self->priv->src_lock);
2388 g_cond_init (&self->priv->src_cond);
2391 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2392 * method to get to the padtemplates */
2394 gst_aggregator_get_type (void)
2396 static volatile gsize type = 0;
2398 if (g_once_init_enter (&type)) {
2400 static const GTypeInfo info = {
2401 sizeof (GstAggregatorClass),
2404 (GClassInitFunc) gst_aggregator_class_init,
2407 sizeof (GstAggregator),
2409 (GInstanceInitFunc) gst_aggregator_init,
2412 _type = g_type_register_static (GST_TYPE_ELEMENT,
2413 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2414 g_once_init_leave (&type, _type);
2419 /* Must be called with SRC lock and PAD lock held */
2421 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2423 /* Empty queue always has space */
2424 if (aggpad->priv->num_buffers == 0 && aggpad->priv->clipped_buffer == NULL)
2427 /* We also want at least two buffers, one is being processed and one is ready
2428 * for the next iteration when we operate in live mode. */
2429 if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2432 /* zero latency, if there is a buffer, it's full */
2433 if (self->priv->latency == 0)
2436 /* Allow no more buffers than the latency */
2437 return (aggpad->priv->time_level <= self->priv->latency);
2440 /* Must be called with the PAD_LOCK held */
2442 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2444 GstClockTime timestamp;
2446 if (GST_BUFFER_DTS_IS_VALID (buffer))
2447 timestamp = GST_BUFFER_DTS (buffer);
2449 timestamp = GST_BUFFER_PTS (buffer);
2451 if (timestamp == GST_CLOCK_TIME_NONE) {
2453 timestamp = aggpad->priv->head_position;
2455 timestamp = aggpad->priv->tail_position;
2459 if (GST_BUFFER_DURATION_IS_VALID (buffer))
2460 timestamp += GST_BUFFER_DURATION (buffer);
2463 aggpad->priv->head_position = timestamp;
2465 aggpad->priv->tail_position = timestamp;
2467 update_time_level (aggpad, head);
2470 static GstFlowReturn
2471 gst_aggregator_pad_chain_internal (GstAggregator * self,
2472 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2474 GstFlowReturn flow_return;
2475 GstClockTime buf_pts;
2477 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2479 PAD_FLUSH_LOCK (aggpad);
2482 flow_return = aggpad->priv->flow_return;
2483 if (flow_return != GST_FLOW_OK)
2486 PAD_UNLOCK (aggpad);
2488 buf_pts = GST_BUFFER_PTS (buffer);
2492 GST_OBJECT_LOCK (self);
2495 if (aggpad->priv->first_buffer) {
2496 self->priv->has_peer_latency = FALSE;
2497 aggpad->priv->first_buffer = FALSE;
2500 if ((gst_aggregator_pad_has_space (self, aggpad) || !head)
2501 && aggpad->priv->flow_return == GST_FLOW_OK) {
2503 g_queue_push_head (&aggpad->priv->data, buffer);
2505 g_queue_push_tail (&aggpad->priv->data, buffer);
2506 apply_buffer (aggpad, buffer, head);
2507 aggpad->priv->num_buffers++;
2509 SRC_BROADCAST (self);
2513 flow_return = aggpad->priv->flow_return;
2514 if (flow_return != GST_FLOW_OK) {
2515 GST_OBJECT_UNLOCK (self);
2519 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2520 GST_OBJECT_UNLOCK (self);
2522 PAD_WAIT_EVENT (aggpad);
2524 PAD_UNLOCK (aggpad);
2527 if (self->priv->first_buffer) {
2528 GstClockTime start_time;
2530 switch (self->priv->start_time_selection) {
2531 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2535 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2536 GST_OBJECT_LOCK (aggpad);
2537 if (aggpad->priv->head_segment.format == GST_FORMAT_TIME) {
2538 start_time = buf_pts;
2539 if (start_time != -1) {
2540 start_time = MAX (start_time, aggpad->priv->head_segment.start);
2542 gst_segment_to_running_time (&aggpad->priv->head_segment,
2543 GST_FORMAT_TIME, start_time);
2547 GST_WARNING_OBJECT (aggpad,
2548 "Ignoring request of selecting the first start time "
2549 "as the segment is a %s segment instead of a time segment",
2550 gst_format_get_name (aggpad->segment.format));
2552 GST_OBJECT_UNLOCK (aggpad);
2554 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2555 start_time = self->priv->start_time;
2556 if (start_time == -1)
2561 if (start_time != -1) {
2562 if (self->segment.position == -1)
2563 self->segment.position = start_time;
2565 self->segment.position = MIN (start_time, self->segment.position);
2567 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2568 GST_TIME_ARGS (start_time));
2572 PAD_UNLOCK (aggpad);
2573 GST_OBJECT_UNLOCK (self);
2576 PAD_FLUSH_UNLOCK (aggpad);
2578 GST_DEBUG_OBJECT (aggpad, "Done chaining");
2583 PAD_UNLOCK (aggpad);
2584 PAD_FLUSH_UNLOCK (aggpad);
2586 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2587 gst_flow_get_name (flow_return));
2589 gst_buffer_unref (buffer);
2594 static GstFlowReturn
2595 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2597 return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2598 GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2602 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2605 GstAggregator *self = GST_AGGREGATOR (parent);
2606 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2608 if (GST_QUERY_IS_SERIALIZED (query)) {
2610 gboolean ret = FALSE;
2615 if (aggpad->priv->flow_return != GST_FLOW_OK) {
2620 g_queue_push_head (&aggpad->priv->data, query);
2621 SRC_BROADCAST (self);
2624 while (!gst_aggregator_pad_queue_is_empty (aggpad)
2625 && aggpad->priv->flow_return == GST_FLOW_OK) {
2626 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2627 PAD_WAIT_EVENT (aggpad);
2630 s = gst_query_writable_structure (query);
2631 if (gst_structure_get_boolean (s, "gst-aggregator-retval", &ret))
2632 gst_structure_remove_field (s, "gst-aggregator-retval");
2634 g_queue_remove (&aggpad->priv->data, query);
2636 if (aggpad->priv->flow_return != GST_FLOW_OK)
2639 PAD_UNLOCK (aggpad);
2643 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2645 return klass->sink_query (self, aggpad, query);
2649 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2650 gst_flow_get_name (aggpad->priv->flow_return));
2651 PAD_UNLOCK (aggpad);
2656 /* Queue serialized events and let the others go though directly.
2657 * The queued events with be handled from the src-pad task in
2658 * gst_aggregator_do_events_and_queries().
2660 static GstFlowReturn
2661 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2664 GstFlowReturn ret = GST_FLOW_OK;
2665 GstAggregator *self = GST_AGGREGATOR (parent);
2666 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2668 if (GST_EVENT_IS_SERIALIZED (event)
2669 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2673 if (aggpad->priv->flow_return != GST_FLOW_OK)
2676 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2677 GST_OBJECT_LOCK (aggpad);
2678 gst_event_copy_segment (event, &aggpad->priv->head_segment);
2679 aggpad->priv->head_position = aggpad->priv->head_segment.position;
2680 update_time_level (aggpad, TRUE);
2681 GST_OBJECT_UNLOCK (aggpad);
2684 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT, event);
2685 g_queue_push_head (&aggpad->priv->data, event);
2686 SRC_BROADCAST (self);
2687 PAD_UNLOCK (aggpad);
2690 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2692 if (!klass->sink_event (self, aggpad, event)) {
2693 /* Copied from GstPad to convert boolean to a GstFlowReturn in
2694 * the event handling func */
2695 ret = GST_FLOW_ERROR;
2702 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2703 gst_flow_get_name (aggpad->priv->flow_return));
2704 PAD_UNLOCK (aggpad);
2706 if (GST_EVENT_IS_STICKY (event))
2707 gst_pad_store_sticky_event (pad, event);
2708 gst_event_unref (event);
2710 return aggpad->priv->flow_return;
2714 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2715 GstObject * parent, GstPadMode mode, gboolean active)
2717 GstAggregator *self = GST_AGGREGATOR (parent);
2718 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2720 if (active == FALSE) {
2722 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2723 SRC_BROADCAST (self);
2727 aggpad->priv->flow_return = GST_FLOW_OK;
2728 PAD_BROADCAST_EVENT (aggpad);
2729 PAD_UNLOCK (aggpad);
2735 /***********************************
2736 * GstAggregatorPad implementation *
2737 ************************************/
2738 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2741 gst_aggregator_pad_constructed (GObject * object)
2743 GstPad *pad = GST_PAD (object);
2745 gst_pad_set_chain_function (pad,
2746 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2747 gst_pad_set_event_full_function_full (pad,
2748 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
2749 gst_pad_set_query_function (pad,
2750 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2751 gst_pad_set_activatemode_function (pad,
2752 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2756 gst_aggregator_pad_finalize (GObject * object)
2758 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2760 g_cond_clear (&pad->priv->event_cond);
2761 g_mutex_clear (&pad->priv->flush_lock);
2762 g_mutex_clear (&pad->priv->lock);
2764 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2768 gst_aggregator_pad_dispose (GObject * object)
2770 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2772 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2774 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2778 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2780 GObjectClass *gobject_class = (GObjectClass *) klass;
2782 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2784 gobject_class->constructed = gst_aggregator_pad_constructed;
2785 gobject_class->finalize = gst_aggregator_pad_finalize;
2786 gobject_class->dispose = gst_aggregator_pad_dispose;
2790 gst_aggregator_pad_init (GstAggregatorPad * pad)
2793 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2794 GstAggregatorPadPrivate);
2796 g_queue_init (&pad->priv->data);
2797 g_cond_init (&pad->priv->event_cond);
2799 g_mutex_init (&pad->priv->flush_lock);
2800 g_mutex_init (&pad->priv->lock);
2802 gst_aggregator_pad_reset_unlocked (pad);
2803 pad->priv->negotiated = FALSE;
2806 /* Must be called with the PAD_LOCK held */
2808 gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad)
2810 pad->priv->num_buffers--;
2811 GST_TRACE_OBJECT (pad, "Consuming buffer");
2812 PAD_BROADCAST_EVENT (pad);
2815 /* Must be called with the PAD_LOCK held */
2817 gst_aggregator_pad_clip_buffer_unlocked (GstAggregatorPad * pad)
2819 GstAggregator *self = NULL;
2820 GstAggregatorClass *aggclass = NULL;
2821 GstBuffer *buffer = NULL;
2823 while (pad->priv->clipped_buffer == NULL &&
2824 GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->data))) {
2825 buffer = g_queue_pop_tail (&pad->priv->data);
2827 apply_buffer (pad, buffer, FALSE);
2829 /* We only take the parent here so that it's not taken if the buffer is
2830 * already clipped or if the queue is empty.
2833 self = GST_AGGREGATOR (gst_pad_get_parent_element (GST_PAD (pad)));
2835 gst_buffer_unref (buffer);
2839 aggclass = GST_AGGREGATOR_GET_CLASS (self);
2842 if (aggclass->clip) {
2843 GST_TRACE_OBJECT (pad, "Clipping: %" GST_PTR_FORMAT, buffer);
2845 buffer = aggclass->clip (self, pad, buffer);
2847 if (buffer == NULL) {
2848 gst_aggregator_pad_buffer_consumed (pad);
2849 GST_TRACE_OBJECT (pad, "Clipping consumed the buffer");
2853 pad->priv->clipped_buffer = buffer;
2857 gst_object_unref (self);
2861 * gst_aggregator_pad_steal_buffer:
2862 * @pad: the pad to get buffer from
2864 * Steal the ref to the buffer currently queued in @pad.
2866 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2867 * queued. You should unref the buffer after usage.
2870 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2876 gst_aggregator_pad_clip_buffer_unlocked (pad);
2878 buffer = pad->priv->clipped_buffer;
2881 pad->priv->clipped_buffer = NULL;
2882 gst_aggregator_pad_buffer_consumed (pad);
2883 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2892 * gst_aggregator_pad_drop_buffer:
2893 * @pad: the pad where to drop any pending buffer
2895 * Drop the buffer currently queued in @pad.
2897 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2900 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2904 buf = gst_aggregator_pad_steal_buffer (pad);
2909 gst_buffer_unref (buf);
2914 * gst_aggregator_pad_get_buffer:
2915 * @pad: the pad to get buffer from
2917 * Returns: (transfer full): A reference to the buffer in @pad or
2918 * NULL if no buffer was queued. You should unref the buffer after
2922 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2928 gst_aggregator_pad_clip_buffer_unlocked (pad);
2930 if (pad->priv->clipped_buffer) {
2931 buffer = gst_buffer_ref (pad->priv->clipped_buffer);
2941 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2946 is_eos = pad->priv->eos;
2953 * gst_aggregator_merge_tags:
2954 * @self: a #GstAggregator
2955 * @tags: a #GstTagList to merge
2956 * @mode: the #GstTagMergeMode to use
2958 * Adds tags to so-called pending tags, which will be processed
2959 * before pushing out data downstream.
2961 * Note that this is provided for convenience, and the subclass is
2962 * not required to use this and can still do tag handling on its own.
2967 gst_aggregator_merge_tags (GstAggregator * self,
2968 const GstTagList * tags, GstTagMergeMode mode)
2972 g_return_if_fail (GST_IS_AGGREGATOR (self));
2973 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2975 /* FIXME Check if we can use OBJECT lock here! */
2976 GST_OBJECT_LOCK (self);
2978 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2979 otags = self->priv->tags;
2980 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2982 gst_tag_list_unref (otags);
2983 self->priv->tags_changed = TRUE;
2984 GST_OBJECT_UNLOCK (self);
2988 * gst_aggregator_set_latency:
2989 * @self: a #GstAggregator
2990 * @min_latency: minimum latency
2991 * @max_latency: maximum latency
2993 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2994 * latency is. Will also post a LATENCY message on the bus so the pipeline
2995 * can reconfigure its global latency.
2998 gst_aggregator_set_latency (GstAggregator * self,
2999 GstClockTime min_latency, GstClockTime max_latency)
3001 gboolean changed = FALSE;
3003 g_return_if_fail (GST_IS_AGGREGATOR (self));
3004 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
3005 g_return_if_fail (max_latency >= min_latency);
3008 if (self->priv->sub_latency_min != min_latency) {
3009 self->priv->sub_latency_min = min_latency;
3012 if (self->priv->sub_latency_max != max_latency) {
3013 self->priv->sub_latency_max = max_latency;
3018 SRC_BROADCAST (self);
3022 gst_element_post_message (GST_ELEMENT_CAST (self),
3023 gst_message_new_latency (GST_OBJECT_CAST (self)));
3028 * gst_aggregator_get_buffer_pool:
3029 * @self: a #GstAggregator
3031 * Returns: (transfer full): the instance of the #GstBufferPool used
3032 * by @trans; free it after use it
3035 gst_aggregator_get_buffer_pool (GstAggregator * self)
3037 GstBufferPool *pool;
3039 g_return_val_if_fail (GST_IS_AGGREGATOR (self), NULL);
3041 GST_OBJECT_LOCK (self);
3042 pool = self->priv->pool;
3044 gst_object_ref (pool);
3045 GST_OBJECT_UNLOCK (self);
3051 * gst_aggregator_get_allocator:
3052 * @self: a #GstAggregator
3053 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3055 * @params: (out) (allow-none) (transfer full): the
3056 * #GstAllocationParams of @allocator
3058 * Lets #GstAggregator sub-classes get the memory @allocator
3059 * acquired by the base class and its @params.
3061 * Unref the @allocator after use it.
3064 gst_aggregator_get_allocator (GstAggregator * self,
3065 GstAllocator ** allocator, GstAllocationParams * params)
3067 g_return_if_fail (GST_IS_AGGREGATOR (self));
3070 *allocator = self->priv->allocator ?
3071 gst_object_ref (self->priv->allocator) : NULL;
3074 *params = self->priv->allocation_params;