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: Base class for mixers and muxers, manages a set of input
26 * pads and aggregates their streams
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.
64 * This class used to live in gst-plugins-bad and was moved to core.
70 * SECTION: gstaggregatorpad
71 * @title: GstAggregatorPad
72 * @short_description: #GstPad subclass for pads managed by #GstAggregator
73 * @see_also: gstcollectpads for historical reasons.
75 * Pads managed by a #GstAggregor subclass.
77 * This class used to live in gst-plugins-bad and was moved to core.
86 #include <string.h> /* strlen */
88 #include "gstaggregator.h"
92 GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
93 GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
94 GST_AGGREGATOR_START_TIME_SELECTION_SET
95 } GstAggregatorStartTimeSelection;
98 gst_aggregator_start_time_selection_get_type (void)
100 static GType gtype = 0;
103 static const GEnumValue values[] = {
104 {GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
105 "Start at 0 running time (default)", "zero"},
106 {GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
107 "Start at first observed input running time", "first"},
108 {GST_AGGREGATOR_START_TIME_SELECTION_SET,
109 "Set start time with start-time property", "set"},
113 gtype = g_enum_register_static ("GstAggregatorStartTimeSelection", values);
118 /* Might become API */
119 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
120 const GstTagList * tags, GstTagMergeMode mode);
121 static void gst_aggregator_set_latency_property (GstAggregator * agg,
122 GstClockTime latency);
123 static GstClockTime gst_aggregator_get_latency_property (GstAggregator * agg);
125 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
127 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
128 #define GST_CAT_DEFAULT aggregator_debug
130 /* Locking order, locks in this element must always be taken in this order
132 * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
133 * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
134 * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
135 * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
136 * standard element object lock -> GST_OBJECT_LOCK(agg)
137 * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
138 * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
139 * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
142 /* GstAggregatorPad definitions */
143 #define PAD_LOCK(pad) G_STMT_START { \
144 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
146 g_mutex_lock(&pad->priv->lock); \
147 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
151 #define PAD_UNLOCK(pad) G_STMT_START { \
152 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
154 g_mutex_unlock(&pad->priv->lock); \
155 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
160 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
161 GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p", \
163 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
164 (&((GstAggregatorPad*)pad)->priv->lock)); \
165 GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
169 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
170 GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p", \
172 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
176 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
177 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
179 g_mutex_lock(&pad->priv->flush_lock); \
180 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
184 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
185 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
187 g_mutex_unlock(&pad->priv->flush_lock); \
188 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
192 #define SRC_LOCK(self) G_STMT_START { \
193 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
195 g_mutex_lock(&self->priv->src_lock); \
196 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
200 #define SRC_UNLOCK(self) G_STMT_START { \
201 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
203 g_mutex_unlock(&self->priv->src_lock); \
204 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
208 #define SRC_WAIT(self) G_STMT_START { \
209 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
211 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
212 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
216 #define SRC_BROADCAST(self) G_STMT_START { \
217 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
219 if (self->priv->aggregate_id) \
220 gst_clock_id_unschedule (self->priv->aggregate_id); \
221 g_cond_broadcast(&(self->priv->src_cond)); \
224 struct _GstAggregatorPadPrivate
226 /* Following fields are protected by the PAD_LOCK */
227 GstFlowReturn flow_return;
228 gboolean pending_flush_start;
229 gboolean pending_flush_stop;
231 gboolean first_buffer;
233 GQueue data; /* buffers, events and queries */
234 GstBuffer *clipped_buffer;
237 /* used to track fill state of queues, only used with live-src and when
238 * latency property is set to > 0 */
239 GstClockTime head_position;
240 GstClockTime tail_position;
241 GstClockTime head_time; /* running time */
242 GstClockTime tail_time;
243 GstClockTime time_level; /* how much head is ahead of tail */
244 GstSegment head_segment; /* segment before the queue */
252 /* This lock prevents a flush start processing happening while
253 * the chain function is also happening.
258 /* Must be called with PAD_LOCK held */
260 gst_aggregator_pad_reset_unlocked (GstAggregatorPad * aggpad)
262 aggpad->priv->eos = FALSE;
263 aggpad->priv->flow_return = GST_FLOW_OK;
264 GST_OBJECT_LOCK (aggpad);
265 gst_segment_init (&aggpad->segment, GST_FORMAT_UNDEFINED);
266 gst_segment_init (&aggpad->priv->head_segment, GST_FORMAT_UNDEFINED);
267 GST_OBJECT_UNLOCK (aggpad);
268 aggpad->priv->head_position = GST_CLOCK_TIME_NONE;
269 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
270 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
271 aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
272 aggpad->priv->time_level = 0;
273 aggpad->priv->first_buffer = TRUE;
277 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
279 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
282 gst_aggregator_pad_reset_unlocked (aggpad);
286 return klass->flush (aggpad, agg);
291 /*************************************
292 * GstAggregator implementation *
293 *************************************/
294 static GstElementClass *aggregator_parent_class = NULL;
296 /* All members are protected by the object lock unless otherwise noted */
298 struct _GstAggregatorPrivate
302 /* Our state is >= PAUSED */
303 gboolean running; /* protected by src_lock */
305 /* seqnum from seek or segment,
306 * to be applied to synthetic segment/eos events */
308 gboolean send_stream_start; /* protected by srcpad stream lock */
309 gboolean send_segment;
310 gboolean flush_seeking;
311 gboolean pending_flush_start;
312 gboolean send_eos; /* protected by srcpad stream lock */
314 GstCaps *srccaps; /* protected by the srcpad stream lock */
317 gboolean tags_changed;
319 gboolean peer_latency_live; /* protected by src_lock */
320 GstClockTime peer_latency_min; /* protected by src_lock */
321 GstClockTime peer_latency_max; /* protected by src_lock */
322 gboolean has_peer_latency; /* protected by src_lock */
324 GstClockTime sub_latency_min; /* protected by src_lock */
325 GstClockTime sub_latency_max; /* protected by src_lock */
328 GstClockID aggregate_id; /* protected by src_lock */
332 gboolean first_buffer; /* protected by object lock */
333 GstAggregatorStartTimeSelection start_time_selection;
334 GstClockTime start_time;
336 /* protected by the object lock */
337 GstQuery *allocation_query;
338 GstAllocator *allocator;
340 GstAllocationParams allocation_params;
343 gint64 latency; /* protected by both src_lock and all pad locks */
346 /* Seek event forwarding helper */
352 gboolean only_to_active_pads;
356 gboolean one_actually_seeked;
359 #define DEFAULT_LATENCY 0
360 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
361 #define DEFAULT_START_TIME (-1)
367 PROP_START_TIME_SELECTION,
372 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
373 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
376 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
378 return (g_queue_peek_tail (&pad->priv->data) == NULL &&
379 pad->priv->clipped_buffer == NULL);
383 gst_aggregator_check_pads_ready (GstAggregator * self)
385 GstAggregatorPad *pad;
387 gboolean have_buffer = TRUE;
388 gboolean have_event_or_query = FALSE;
390 GST_LOG_OBJECT (self, "checking pads");
392 GST_OBJECT_LOCK (self);
394 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
395 if (sinkpads == NULL)
398 for (l = sinkpads; l != NULL; l = l->next) {
403 if (pad->priv->num_buffers == 0) {
404 if (!gst_aggregator_pad_queue_is_empty (pad))
405 have_event_or_query = TRUE;
406 if (!pad->priv->eos) {
409 /* If not live we need data on all pads, so leave the loop */
410 if (!self->priv->peer_latency_live) {
415 } else if (self->priv->peer_latency_live) {
416 /* In live mode, having a single pad with buffers is enough to
417 * generate a start time from it. In non-live mode all pads need
420 self->priv->first_buffer = FALSE;
426 if (!have_buffer && !have_event_or_query)
430 self->priv->first_buffer = FALSE;
432 GST_OBJECT_UNLOCK (self);
433 GST_LOG_OBJECT (self, "pads are ready");
438 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
439 GST_OBJECT_UNLOCK (self);
444 if (have_event_or_query)
445 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet,"
446 " but waking up for serialized event");
448 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
449 GST_OBJECT_UNLOCK (self);
450 return have_event_or_query;
455 gst_aggregator_reset_flow_values (GstAggregator * self)
457 GST_OBJECT_LOCK (self);
458 self->priv->send_stream_start = TRUE;
459 self->priv->send_segment = TRUE;
460 gst_segment_init (&self->segment, GST_FORMAT_TIME);
461 self->priv->first_buffer = TRUE;
462 GST_OBJECT_UNLOCK (self);
466 gst_aggregator_push_mandatory_events (GstAggregator * self)
468 GstAggregatorPrivate *priv = self->priv;
469 GstEvent *segment = NULL;
470 GstEvent *tags = NULL;
472 if (self->priv->send_stream_start) {
475 GST_INFO_OBJECT (self, "pushing stream start");
476 /* stream-start (FIXME: create id based on input ids) */
477 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
478 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
479 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
481 self->priv->send_stream_start = FALSE;
484 if (self->priv->srccaps) {
486 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
487 self->priv->srccaps);
488 if (!gst_pad_push_event (self->srcpad,
489 gst_event_new_caps (self->priv->srccaps))) {
490 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
492 gst_caps_unref (self->priv->srccaps);
493 self->priv->srccaps = NULL;
496 GST_OBJECT_LOCK (self);
497 if (self->priv->send_segment && !self->priv->flush_seeking) {
498 segment = gst_event_new_segment (&self->segment);
500 if (!self->priv->seqnum)
501 /* This code-path is in preparation to be able to run without a source
502 * connected. Then we won't have a seq-num from a segment event. */
503 self->priv->seqnum = gst_event_get_seqnum (segment);
505 gst_event_set_seqnum (segment, self->priv->seqnum);
506 self->priv->send_segment = FALSE;
508 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
511 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
512 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
513 priv->tags_changed = FALSE;
515 GST_OBJECT_UNLOCK (self);
518 gst_pad_push_event (self->srcpad, segment);
520 gst_pad_push_event (self->srcpad, tags);
525 * gst_aggregator_set_src_caps:
526 * @self: The #GstAggregator
527 * @caps: The #GstCaps to set on the src pad.
529 * Sets the caps to be used on the src pad.
532 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
534 GST_PAD_STREAM_LOCK (self->srcpad);
535 gst_caps_replace (&self->priv->srccaps, caps);
536 gst_aggregator_push_mandatory_events (self);
537 GST_PAD_STREAM_UNLOCK (self->srcpad);
541 * gst_aggregator_finish_buffer:
542 * @self: The #GstAggregator
543 * @buffer: (transfer full): the #GstBuffer to push.
545 * This method will push the provided output buffer downstream. If needed,
546 * mandatory events such as stream-start, caps, and segment events will be
547 * sent before pushing the buffer.
550 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
552 gst_aggregator_push_mandatory_events (self);
554 GST_OBJECT_LOCK (self);
555 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
556 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
557 GST_OBJECT_UNLOCK (self);
558 return gst_pad_push (self->srcpad, buffer);
560 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
561 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
562 GST_OBJECT_UNLOCK (self);
563 gst_buffer_unref (buffer);
569 gst_aggregator_push_eos (GstAggregator * self)
572 gst_aggregator_push_mandatory_events (self);
574 event = gst_event_new_eos ();
576 GST_OBJECT_LOCK (self);
577 self->priv->send_eos = FALSE;
578 gst_event_set_seqnum (event, self->priv->seqnum);
579 GST_OBJECT_UNLOCK (self);
581 gst_pad_push_event (self->srcpad, event);
585 gst_aggregator_get_next_time (GstAggregator * self)
587 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
589 if (klass->get_next_time)
590 return klass->get_next_time (self);
592 return GST_CLOCK_TIME_NONE;
596 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
598 GstClockTime latency;
606 latency = gst_aggregator_get_latency_unlocked (self);
608 if (gst_aggregator_check_pads_ready (self)) {
609 GST_DEBUG_OBJECT (self, "all pads have data");
615 /* Before waiting, check if we're actually still running */
616 if (!self->priv->running || !self->priv->send_eos) {
622 start = gst_aggregator_get_next_time (self);
624 /* If we're not live, or if we use the running time
625 * of the first buffer as start time, we wait until
626 * all pads have buffers.
627 * Otherwise (i.e. if we are live!), we wait on the clock
628 * and if a pad does not have a buffer in time we ignore
631 GST_OBJECT_LOCK (self);
632 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
633 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
634 !GST_CLOCK_TIME_IS_VALID (start) ||
635 (self->priv->first_buffer
636 && self->priv->start_time_selection ==
637 GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
638 /* We wake up here when something happened, and below
639 * then check if we're ready now. If we return FALSE,
640 * we will be directly called again.
642 GST_OBJECT_UNLOCK (self);
645 GstClockTime base_time, time;
647 GstClockReturn status;
648 GstClockTimeDiff jitter;
650 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
651 GST_TIME_ARGS (start));
653 base_time = GST_ELEMENT_CAST (self)->base_time;
654 clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
655 GST_OBJECT_UNLOCK (self);
657 time = base_time + start;
660 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
661 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
662 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
663 GST_TIME_ARGS (time),
664 GST_TIME_ARGS (base_time),
665 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
666 GST_TIME_ARGS (gst_clock_get_time (clock)));
668 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
669 gst_object_unref (clock);
673 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
676 if (self->priv->aggregate_id) {
677 gst_clock_id_unref (self->priv->aggregate_id);
678 self->priv->aggregate_id = NULL;
681 GST_DEBUG_OBJECT (self,
682 "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
683 status, GST_STIME_ARGS (jitter));
686 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
693 res = gst_aggregator_check_pads_ready (self);
700 gst_aggregator_do_events_and_queries (GstElement * self, GstPad * epad,
703 GstAggregatorPad *pad = GST_AGGREGATOR_PAD_CAST (epad);
704 GstAggregator *aggregator = GST_AGGREGATOR_CAST (self);
705 GstEvent *event = NULL;
706 GstQuery *query = NULL;
707 GstAggregatorClass *klass = NULL;
708 gboolean *processed_event = user_data;
715 if (pad->priv->clipped_buffer == NULL &&
716 !GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->data))) {
717 if (GST_IS_EVENT (g_queue_peek_tail (&pad->priv->data)))
718 event = gst_event_ref (g_queue_peek_tail (&pad->priv->data));
719 if (GST_IS_QUERY (g_queue_peek_tail (&pad->priv->data)))
720 query = g_queue_peek_tail (&pad->priv->data);
723 if (event || query) {
727 *processed_event = TRUE;
729 klass = GST_AGGREGATOR_GET_CLASS (self);
732 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
733 gst_event_ref (event);
734 ret = klass->sink_event (aggregator, pad, event);
737 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS)
738 pad->priv->negotiated = ret;
739 if (g_queue_peek_tail (&pad->priv->data) == event)
740 gst_event_unref (g_queue_pop_tail (&pad->priv->data));
741 gst_event_unref (event);
743 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, query);
744 ret = klass->sink_query (aggregator, pad, query);
747 if (g_queue_peek_tail (&pad->priv->data) == query) {
750 s = gst_query_writable_structure (query);
751 gst_structure_set (s, "gst-aggregator-retval", G_TYPE_BOOLEAN, ret,
753 g_queue_pop_tail (&pad->priv->data);
757 PAD_BROADCAST_EVENT (pad);
760 } while (event || query);
766 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
767 GstFlowReturn flow_return, gboolean full)
772 if (flow_return == GST_FLOW_NOT_LINKED)
773 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
775 aggpad->priv->flow_return = flow_return;
777 item = g_queue_peek_head_link (&aggpad->priv->data);
779 GList *next = item->next;
781 /* In partial flush, we do like the pad, we get rid of non-sticky events
784 if (full || GST_IS_BUFFER (item->data) ||
785 GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
786 GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
787 !GST_EVENT_IS_STICKY (item->data)) {
788 if (!GST_IS_QUERY (item->data))
789 gst_mini_object_unref (item->data);
790 g_queue_delete_link (&aggpad->priv->data, item);
794 aggpad->priv->num_buffers = 0;
795 gst_buffer_replace (&aggpad->priv->clipped_buffer, NULL);
797 PAD_BROADCAST_EVENT (aggpad);
802 gst_aggregator_default_update_src_caps (GstAggregator * agg, GstCaps * caps,
805 *ret = gst_caps_ref (caps);
811 gst_aggregator_default_fixate_src_caps (GstAggregator * agg, GstCaps * caps)
813 caps = gst_caps_fixate (caps);
819 gst_aggregator_default_negotiated_src_caps (GstAggregator * agg, GstCaps * caps)
825 /* takes ownership of the pool, allocator and query */
827 gst_aggregator_set_allocation (GstAggregator * self,
828 GstBufferPool * pool, GstAllocator * allocator,
829 GstAllocationParams * params, GstQuery * query)
831 GstAllocator *oldalloc;
832 GstBufferPool *oldpool;
835 GST_DEBUG ("storing allocation query");
837 GST_OBJECT_LOCK (self);
838 oldpool = self->priv->pool;
839 self->priv->pool = pool;
841 oldalloc = self->priv->allocator;
842 self->priv->allocator = allocator;
844 oldquery = self->priv->allocation_query;
845 self->priv->allocation_query = query;
848 self->priv->allocation_params = *params;
850 gst_allocation_params_init (&self->priv->allocation_params);
851 GST_OBJECT_UNLOCK (self);
854 GST_DEBUG_OBJECT (self, "deactivating old pool %p", oldpool);
855 gst_buffer_pool_set_active (oldpool, FALSE);
856 gst_object_unref (oldpool);
859 gst_object_unref (oldalloc);
862 gst_query_unref (oldquery);
869 gst_aggregator_decide_allocation (GstAggregator * self, GstQuery * query)
871 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
873 if (aggclass->decide_allocation)
874 if (!aggclass->decide_allocation (self, query))
881 gst_aggregator_do_allocation (GstAggregator * self, GstCaps * caps)
884 gboolean result = TRUE;
885 GstBufferPool *pool = NULL;
886 GstAllocator *allocator;
887 GstAllocationParams params;
889 /* find a pool for the negotiated caps now */
890 GST_DEBUG_OBJECT (self, "doing allocation query");
891 query = gst_query_new_allocation (caps, TRUE);
892 if (!gst_pad_peer_query (self->srcpad, query)) {
893 /* not a problem, just debug a little */
894 GST_DEBUG_OBJECT (self, "peer ALLOCATION query failed");
897 GST_DEBUG_OBJECT (self, "calling decide_allocation");
898 result = gst_aggregator_decide_allocation (self, query);
900 GST_DEBUG_OBJECT (self, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
904 goto no_decide_allocation;
906 /* we got configuration from our peer or the decide_allocation method,
908 if (gst_query_get_n_allocation_params (query) > 0) {
909 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
912 gst_allocation_params_init (¶ms);
915 if (gst_query_get_n_allocation_pools (query) > 0)
916 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
920 gst_aggregator_set_allocation (self, pool, allocator, ¶ms, query);
925 no_decide_allocation:
927 GST_WARNING_OBJECT (self, "Failed to decide allocation");
928 gst_query_unref (query);
935 /* WITH SRC_LOCK held */
937 gst_aggregator_update_src_caps (GstAggregator * self)
939 GstAggregatorClass *agg_klass = GST_AGGREGATOR_GET_CLASS (self);
940 GstCaps *downstream_caps, *template_caps, *caps = NULL;
941 GstFlowReturn ret = GST_FLOW_OK;
943 template_caps = gst_pad_get_pad_template_caps (self->srcpad);
944 downstream_caps = gst_pad_peer_query_caps (self->srcpad, template_caps);
946 if (gst_caps_is_empty (downstream_caps)) {
947 GST_INFO_OBJECT (self, "Downstream caps (%"
948 GST_PTR_FORMAT ") not compatible with pad template caps (%"
949 GST_PTR_FORMAT ")", downstream_caps, template_caps);
950 ret = GST_FLOW_NOT_NEGOTIATED;
954 g_assert (agg_klass->update_src_caps);
955 GST_DEBUG_OBJECT (self, "updating caps from %" GST_PTR_FORMAT,
957 ret = agg_klass->update_src_caps (self, downstream_caps, &caps);
958 if (ret < GST_FLOW_OK) {
959 GST_WARNING_OBJECT (self, "Subclass failed to update provided caps");
962 if ((caps == NULL || gst_caps_is_empty (caps)) && ret >= GST_FLOW_OK) {
963 ret = GST_FLOW_NOT_NEGOTIATED;
966 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
968 #ifdef GST_ENABLE_EXTRA_CHECKS
969 if (!gst_caps_is_subset (caps, template_caps)) {
970 GstCaps *intersection;
972 GST_ERROR_OBJECT (self,
973 "update_src_caps returned caps %" GST_PTR_FORMAT
974 " which are not a real subset of the template caps %"
975 GST_PTR_FORMAT, caps, template_caps);
976 g_warning ("%s: update_src_caps returned caps which are not a real "
977 "subset of the filter caps", GST_ELEMENT_NAME (self));
980 gst_caps_intersect_full (template_caps, caps, GST_CAPS_INTERSECT_FIRST);
981 gst_caps_unref (caps);
986 if (gst_caps_is_any (caps)) {
990 if (!gst_caps_is_fixed (caps)) {
991 g_assert (agg_klass->fixate_src_caps);
993 GST_DEBUG_OBJECT (self, "fixate caps from %" GST_PTR_FORMAT, caps);
994 if (!(caps = agg_klass->fixate_src_caps (self, caps))) {
995 GST_WARNING_OBJECT (self, "Subclass failed to fixate provided caps");
996 ret = GST_FLOW_NOT_NEGOTIATED;
999 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1002 if (agg_klass->negotiated_src_caps) {
1003 if (!agg_klass->negotiated_src_caps (self, caps)) {
1004 GST_WARNING_OBJECT (self, "Subclass failed to accept negotiated caps");
1005 ret = GST_FLOW_NOT_NEGOTIATED;
1010 gst_aggregator_set_src_caps (self, caps);
1012 if (!gst_aggregator_do_allocation (self, caps)) {
1013 GST_WARNING_OBJECT (self, "Allocation negotiation failed");
1014 ret = GST_FLOW_NOT_NEGOTIATED;
1018 gst_caps_unref (downstream_caps);
1019 gst_caps_unref (template_caps);
1022 gst_caps_unref (caps);
1028 gst_aggregator_aggregate_func (GstAggregator * self)
1030 GstAggregatorPrivate *priv = self->priv;
1031 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1032 gboolean timeout = FALSE;
1034 if (self->priv->running == FALSE) {
1035 GST_DEBUG_OBJECT (self, "Not running anymore");
1039 GST_LOG_OBJECT (self, "Checking aggregate");
1040 while (priv->send_eos && priv->running) {
1041 GstFlowReturn flow_return = GST_FLOW_OK;
1042 gboolean processed_event = FALSE;
1044 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (self),
1045 gst_aggregator_do_events_and_queries, NULL);
1047 /* Ensure we have buffers ready (either in clipped_buffer or at the head of
1049 if (!gst_aggregator_wait_and_check (self, &timeout))
1052 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (self),
1053 gst_aggregator_do_events_and_queries, &processed_event);
1055 if (processed_event)
1058 if (gst_pad_check_reconfigure (GST_AGGREGATOR_SRC_PAD (self))) {
1059 flow_return = gst_aggregator_update_src_caps (self);
1060 if (flow_return != GST_FLOW_OK)
1061 gst_pad_mark_reconfigure (GST_AGGREGATOR_SRC_PAD (self));
1064 if (timeout || flow_return >= GST_FLOW_OK) {
1065 GST_TRACE_OBJECT (self, "Actually aggregating!");
1066 flow_return = klass->aggregate (self, timeout);
1069 if (flow_return == GST_AGGREGATOR_FLOW_NEED_DATA)
1072 GST_OBJECT_LOCK (self);
1073 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
1074 /* We don't want to set the pads to flushing, but we want to
1075 * stop the thread, so just break here */
1076 GST_OBJECT_UNLOCK (self);
1079 GST_OBJECT_UNLOCK (self);
1081 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
1082 gst_aggregator_push_eos (self);
1085 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
1087 if (flow_return != GST_FLOW_OK) {
1090 GST_OBJECT_LOCK (self);
1091 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
1092 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1094 gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
1096 GST_OBJECT_UNLOCK (self);
1101 /* Pause the task here, the only ways to get here are:
1102 * 1) We're stopping, in which case the task is stopped anyway
1103 * 2) We got a flow error above, in which case it might take
1104 * some time to forward the flow return upstream and we
1105 * would otherwise call the task function over and over
1106 * again without doing anything
1108 gst_pad_pause_task (self->srcpad);
1112 gst_aggregator_start (GstAggregator * self)
1114 GstAggregatorClass *klass;
1117 self->priv->send_stream_start = TRUE;
1118 self->priv->send_segment = TRUE;
1119 self->priv->send_eos = TRUE;
1120 self->priv->srccaps = NULL;
1122 gst_aggregator_set_allocation (self, NULL, NULL, NULL, NULL);
1124 klass = GST_AGGREGATOR_GET_CLASS (self);
1127 result = klass->start (self);
1135 _check_pending_flush_stop (GstAggregatorPad * pad)
1140 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
1147 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
1149 gboolean res = TRUE;
1151 GST_INFO_OBJECT (self, "%s srcpad task",
1152 flush_start ? "Pausing" : "Stopping");
1155 self->priv->running = FALSE;
1156 SRC_BROADCAST (self);
1160 res = gst_pad_push_event (self->srcpad, flush_start);
1163 gst_pad_stop_task (self->srcpad);
1169 gst_aggregator_start_srcpad_task (GstAggregator * self)
1171 GST_INFO_OBJECT (self, "Starting srcpad task");
1173 self->priv->running = TRUE;
1174 gst_pad_start_task (GST_PAD (self->srcpad),
1175 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
1178 static GstFlowReturn
1179 gst_aggregator_flush (GstAggregator * self)
1181 GstFlowReturn ret = GST_FLOW_OK;
1182 GstAggregatorPrivate *priv = self->priv;
1183 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1185 GST_DEBUG_OBJECT (self, "Flushing everything");
1186 GST_OBJECT_LOCK (self);
1187 priv->send_segment = TRUE;
1188 priv->flush_seeking = FALSE;
1189 priv->tags_changed = FALSE;
1190 GST_OBJECT_UNLOCK (self);
1192 ret = klass->flush (self);
1198 /* Called with GstAggregator's object lock held */
1201 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
1204 GstAggregatorPad *tmppad;
1206 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
1207 tmppad = (GstAggregatorPad *) tmp->data;
1209 if (_check_pending_flush_stop (tmppad) == FALSE) {
1210 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
1211 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
1220 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
1223 GstAggregatorPrivate *priv = self->priv;
1224 GstAggregatorPadPrivate *padpriv = aggpad->priv;
1226 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
1228 PAD_FLUSH_LOCK (aggpad);
1230 if (padpriv->pending_flush_start) {
1231 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
1233 padpriv->pending_flush_start = FALSE;
1234 padpriv->pending_flush_stop = TRUE;
1236 PAD_UNLOCK (aggpad);
1238 GST_OBJECT_LOCK (self);
1239 if (priv->flush_seeking) {
1240 /* If flush_seeking we forward the first FLUSH_START */
1241 if (priv->pending_flush_start) {
1242 priv->pending_flush_start = FALSE;
1243 GST_OBJECT_UNLOCK (self);
1245 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
1246 gst_aggregator_stop_srcpad_task (self, event);
1248 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
1249 GST_PAD_STREAM_LOCK (self->srcpad);
1250 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
1253 GST_OBJECT_UNLOCK (self);
1254 gst_event_unref (event);
1257 GST_OBJECT_UNLOCK (self);
1258 gst_event_unref (event);
1260 PAD_FLUSH_UNLOCK (aggpad);
1263 /* Must be called with the the PAD_LOCK held */
1265 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1267 GstAggregatorPadPrivate *priv = aggpad->priv;
1270 if (GST_CLOCK_TIME_IS_VALID (priv->head_position) &&
1271 priv->head_segment.format == GST_FORMAT_TIME)
1272 priv->head_time = gst_segment_to_running_time (&priv->head_segment,
1273 GST_FORMAT_TIME, priv->head_position);
1275 priv->head_time = GST_CLOCK_TIME_NONE;
1277 if (!GST_CLOCK_TIME_IS_VALID (priv->tail_time))
1278 priv->tail_time = priv->head_time;
1280 if (GST_CLOCK_TIME_IS_VALID (priv->tail_position) &&
1281 aggpad->segment.format == GST_FORMAT_TIME)
1282 priv->tail_time = gst_segment_to_running_time (&aggpad->segment,
1283 GST_FORMAT_TIME, priv->tail_position);
1285 priv->tail_time = priv->head_time;
1288 if (priv->head_time == GST_CLOCK_TIME_NONE ||
1289 priv->tail_time == GST_CLOCK_TIME_NONE) {
1290 priv->time_level = 0;
1294 if (priv->tail_time > priv->head_time)
1295 priv->time_level = 0;
1297 priv->time_level = priv->head_time - priv->tail_time;
1301 /* GstAggregator vmethods default implementations */
1303 gst_aggregator_default_sink_event (GstAggregator * self,
1304 GstAggregatorPad * aggpad, GstEvent * event)
1306 gboolean res = TRUE;
1307 GstPad *pad = GST_PAD (aggpad);
1308 GstAggregatorPrivate *priv = self->priv;
1310 GST_DEBUG_OBJECT (aggpad, "Got event: %" GST_PTR_FORMAT, event);
1312 switch (GST_EVENT_TYPE (event)) {
1313 case GST_EVENT_FLUSH_START:
1315 gst_aggregator_flush_start (self, aggpad, event);
1316 /* We forward only in one case: right after flush_seeking */
1320 case GST_EVENT_FLUSH_STOP:
1322 gst_aggregator_pad_flush (aggpad, self);
1323 GST_OBJECT_LOCK (self);
1324 if (priv->flush_seeking) {
1325 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1326 if (gst_aggregator_all_flush_stop_received_locked (self)) {
1327 GST_OBJECT_UNLOCK (self);
1328 /* That means we received FLUSH_STOP/FLUSH_STOP on
1329 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1330 gst_aggregator_flush (self);
1331 gst_pad_push_event (self->srcpad, event);
1334 priv->send_eos = TRUE;
1335 SRC_BROADCAST (self);
1338 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1339 GST_PAD_STREAM_UNLOCK (self->srcpad);
1340 gst_aggregator_start_srcpad_task (self);
1342 GST_OBJECT_UNLOCK (self);
1345 GST_OBJECT_UNLOCK (self);
1348 /* We never forward the event */
1355 g_assert (aggpad->priv->num_buffers == 0);
1356 aggpad->priv->eos = TRUE;
1357 PAD_UNLOCK (aggpad);
1358 SRC_BROADCAST (self);
1362 case GST_EVENT_SEGMENT:
1365 GST_OBJECT_LOCK (aggpad);
1366 gst_event_copy_segment (event, &aggpad->segment);
1367 /* We've got a new segment, tail_position is now meaningless
1368 * and may interfere with the time_level calculation
1370 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
1371 update_time_level (aggpad, FALSE);
1372 GST_OBJECT_UNLOCK (aggpad);
1373 PAD_UNLOCK (aggpad);
1375 GST_OBJECT_LOCK (self);
1376 self->priv->seqnum = gst_event_get_seqnum (event);
1377 GST_OBJECT_UNLOCK (self);
1380 case GST_EVENT_STREAM_START:
1386 GstClockTime pts, endpts;
1387 GstClockTime duration;
1390 gst_event_parse_gap (event, &pts, &duration);
1391 gapbuf = gst_buffer_new ();
1393 if (GST_CLOCK_TIME_IS_VALID (duration))
1394 endpts = pts + duration;
1396 endpts = GST_CLOCK_TIME_NONE;
1398 GST_OBJECT_LOCK (aggpad);
1399 res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1401 GST_OBJECT_UNLOCK (aggpad);
1404 GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1408 if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1409 duration = endpts - pts;
1411 duration = GST_CLOCK_TIME_NONE;
1413 GST_BUFFER_PTS (gapbuf) = pts;
1414 GST_BUFFER_DURATION (gapbuf) = duration;
1415 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1416 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1418 /* Remove GAP event so we can replace it with the buffer */
1419 if (g_queue_peek_tail (&aggpad->priv->data) == event)
1420 gst_event_unref (g_queue_pop_tail (&aggpad->priv->data));
1422 if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1424 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1434 gst_event_parse_tag (event, &tags);
1436 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1437 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1438 gst_event_unref (event);
1450 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1451 return gst_pad_event_default (pad, GST_OBJECT (self), event);
1454 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1456 gst_event_unref (event);
1462 gst_aggregator_stop_pad (GstElement * self, GstPad * epad, gpointer user_data)
1464 GstAggregatorPad *pad = GST_AGGREGATOR_PAD_CAST (epad);
1465 GstAggregator *agg = GST_AGGREGATOR_CAST (self);
1467 gst_aggregator_pad_flush (pad, agg);
1470 pad->priv->flow_return = GST_FLOW_FLUSHING;
1471 pad->priv->negotiated = FALSE;
1472 PAD_BROADCAST_EVENT (pad);
1479 gst_aggregator_stop (GstAggregator * agg)
1481 GstAggregatorClass *klass;
1484 gst_aggregator_reset_flow_values (agg);
1486 /* Application needs to make sure no pads are added while it shuts us down */
1487 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (agg),
1488 gst_aggregator_stop_pad, NULL);
1490 klass = GST_AGGREGATOR_GET_CLASS (agg);
1493 result = klass->stop (agg);
1497 agg->priv->has_peer_latency = FALSE;
1498 agg->priv->peer_latency_live = FALSE;
1499 agg->priv->peer_latency_min = agg->priv->peer_latency_max = 0;
1501 if (agg->priv->tags)
1502 gst_tag_list_unref (agg->priv->tags);
1503 agg->priv->tags = NULL;
1505 gst_aggregator_set_allocation (agg, NULL, NULL, NULL, NULL);
1510 /* GstElement vmethods implementations */
1511 static GstStateChangeReturn
1512 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1514 GstStateChangeReturn ret;
1515 GstAggregator *self = GST_AGGREGATOR (element);
1517 switch (transition) {
1518 case GST_STATE_CHANGE_READY_TO_PAUSED:
1519 if (!gst_aggregator_start (self))
1527 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1528 transition)) == GST_STATE_CHANGE_FAILURE)
1532 switch (transition) {
1533 case GST_STATE_CHANGE_PAUSED_TO_READY:
1534 if (!gst_aggregator_stop (self)) {
1535 /* What to do in this case? Error out? */
1536 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1548 GST_ERROR_OBJECT (element, "parent failed state change");
1553 GST_ERROR_OBJECT (element, "Subclass failed to start");
1554 return GST_STATE_CHANGE_FAILURE;
1559 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1561 GstAggregator *self = GST_AGGREGATOR (element);
1562 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1564 GST_INFO_OBJECT (pad, "Removing pad");
1567 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1568 gst_element_remove_pad (element, pad);
1570 self->priv->has_peer_latency = FALSE;
1571 SRC_BROADCAST (self);
1575 static GstAggregatorPad *
1576 gst_aggregator_default_create_new_pad (GstAggregator * self,
1577 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1579 GstAggregatorPad *agg_pad;
1580 GstAggregatorPrivate *priv = self->priv;
1584 GST_PAD_TEMPLATE_GTYPE (templ) ==
1585 G_TYPE_NONE ? GST_TYPE_AGGREGATOR_PAD : GST_PAD_TEMPLATE_GTYPE (templ);
1587 if (templ->direction != GST_PAD_SINK)
1590 if (templ->presence != GST_PAD_REQUEST)
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 (pad_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");
1622 GST_WARNING_OBJECT (self, "request new pad that is not a REQUEST pad");
1628 gst_aggregator_request_new_pad (GstElement * element,
1629 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1631 GstAggregator *self;
1632 GstAggregatorPad *agg_pad;
1633 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1634 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1636 self = GST_AGGREGATOR (element);
1638 agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1640 GST_ERROR_OBJECT (element, "Couldn't create new pad");
1644 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1647 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1649 /* add the pad to the element */
1650 gst_element_add_pad (element, GST_PAD (agg_pad));
1652 return GST_PAD (agg_pad);
1655 /* Must be called with SRC_LOCK held */
1658 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1660 gboolean query_ret, live;
1661 GstClockTime our_latency, min, max;
1663 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1666 GST_WARNING_OBJECT (self, "Latency query failed");
1670 gst_query_parse_latency (query, &live, &min, &max);
1672 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1673 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1674 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1678 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1679 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1680 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1681 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1682 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1686 our_latency = self->priv->latency;
1688 self->priv->peer_latency_live = live;
1689 self->priv->peer_latency_min = min;
1690 self->priv->peer_latency_max = max;
1691 self->priv->has_peer_latency = TRUE;
1695 min += self->priv->sub_latency_min;
1696 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1697 && GST_CLOCK_TIME_IS_VALID (max))
1698 max += self->priv->sub_latency_max + our_latency;
1700 max = GST_CLOCK_TIME_NONE;
1702 SRC_BROADCAST (self);
1704 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1705 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1707 gst_query_set_latency (query, live, min, max);
1713 * MUST be called with the src_lock held.
1715 * See gst_aggregator_get_latency() for doc
1718 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1720 GstClockTime latency;
1722 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1724 if (!self->priv->has_peer_latency) {
1725 GstQuery *query = gst_query_new_latency ();
1728 ret = gst_aggregator_query_latency_unlocked (self, query);
1729 gst_query_unref (query);
1731 return GST_CLOCK_TIME_NONE;
1734 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1735 return GST_CLOCK_TIME_NONE;
1737 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1738 latency = self->priv->peer_latency_min;
1741 latency += self->priv->latency;
1742 latency += self->priv->sub_latency_min;
1748 * gst_aggregator_get_latency:
1749 * @self: a #GstAggregator
1751 * Retrieves the latency values reported by @self in response to the latency
1752 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1753 * will not wait for the clock.
1755 * Typically only called by subclasses.
1757 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1760 gst_aggregator_get_latency (GstAggregator * self)
1765 ret = gst_aggregator_get_latency_unlocked (self);
1772 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1774 GstAggregator *self = GST_AGGREGATOR (element);
1776 GST_STATE_LOCK (element);
1777 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1778 GST_STATE (element) < GST_STATE_PAUSED) {
1782 GstSeekType start_type, stop_type;
1785 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1786 &start, &stop_type, &stop);
1788 GST_OBJECT_LOCK (self);
1789 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1790 stop_type, stop, NULL);
1791 self->priv->seqnum = gst_event_get_seqnum (event);
1792 self->priv->first_buffer = FALSE;
1793 GST_OBJECT_UNLOCK (self);
1795 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1797 GST_STATE_UNLOCK (element);
1800 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1805 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1807 gboolean res = TRUE;
1809 switch (GST_QUERY_TYPE (query)) {
1810 case GST_QUERY_SEEKING:
1814 /* don't pass it along as some (file)sink might claim it does
1815 * whereas with a collectpads in between that will not likely work */
1816 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1817 gst_query_set_seeking (query, format, FALSE, 0, -1);
1822 case GST_QUERY_LATENCY:
1824 res = gst_aggregator_query_latency_unlocked (self, query);
1828 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1835 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1837 EventData *evdata = user_data;
1838 gboolean ret = TRUE;
1839 GstPad *peer = gst_pad_get_peer (pad);
1840 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1843 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1844 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1847 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1848 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1849 gst_object_unref (peer);
1854 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1855 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1857 GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1859 if (gst_pad_query (peer, seeking)) {
1862 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1864 if (seekable == FALSE) {
1865 GST_INFO_OBJECT (pad,
1866 "Source not seekable, We failed but it does not matter!");
1871 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1874 gst_query_unref (seeking);
1877 if (evdata->flush) {
1879 aggpad->priv->pending_flush_start = FALSE;
1880 aggpad->priv->pending_flush_stop = FALSE;
1881 PAD_UNLOCK (aggpad);
1884 evdata->one_actually_seeked = TRUE;
1887 evdata->result &= ret;
1889 /* Always send to all pads */
1894 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1897 evdata->result = TRUE;
1898 evdata->one_actually_seeked = FALSE;
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 */
1903 if (evdata->flush) {
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 (evdata->event);
1924 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1929 GstSeekType start_type, stop_type;
1932 EventData evdata = { 0, };
1933 GstAggregatorPrivate *priv = self->priv;
1935 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1936 &start, &stop_type, &stop);
1938 GST_INFO_OBJECT (self, "starting SEEK");
1940 flush = flags & GST_SEEK_FLAG_FLUSH;
1942 GST_OBJECT_LOCK (self);
1944 priv->pending_flush_start = TRUE;
1945 priv->flush_seeking = TRUE;
1948 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1949 stop_type, stop, NULL);
1951 /* Seeking sets a position */
1952 self->priv->first_buffer = FALSE;
1953 GST_OBJECT_UNLOCK (self);
1955 /* forward the seek upstream */
1956 evdata.event = event;
1957 evdata.flush = flush;
1958 evdata.only_to_active_pads = FALSE;
1959 gst_aggregator_forward_event_to_all_sinkpads (self, &evdata);
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)
1977 EventData evdata = { 0, };
1979 switch (GST_EVENT_TYPE (event)) {
1980 case GST_EVENT_SEEK:
1981 /* _do_seek() unrefs the event. */
1982 return gst_aggregator_do_seek (self, event);
1983 case GST_EVENT_NAVIGATION:
1984 /* navigation is rather pointless. */
1985 gst_event_unref (event);
1991 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
1992 * they will receive a QOS event that has earliest_time=0 (because we can't
1993 * have negative timestamps), and consider their buffer as too late */
1994 evdata.event = event;
1995 evdata.flush = FALSE;
1996 evdata.only_to_active_pads = GST_EVENT_TYPE (event) == GST_EVENT_QOS;
1997 gst_aggregator_forward_event_to_all_sinkpads (self, &evdata);
1998 return evdata.result;
2002 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
2005 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2007 return klass->src_event (GST_AGGREGATOR (parent), event);
2011 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
2014 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2016 return klass->src_query (GST_AGGREGATOR (parent), query);
2020 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
2021 GstObject * parent, GstPadMode mode, gboolean active)
2023 GstAggregator *self = GST_AGGREGATOR (parent);
2024 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2026 if (klass->src_activate) {
2027 if (klass->src_activate (self, mode, active) == FALSE) {
2032 if (active == TRUE) {
2034 case GST_PAD_MODE_PUSH:
2036 GST_INFO_OBJECT (pad, "Activating pad!");
2037 gst_aggregator_start_srcpad_task (self);
2042 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
2049 GST_INFO_OBJECT (self, "Deactivating srcpad");
2050 gst_aggregator_stop_srcpad_task (self, FALSE);
2056 gst_aggregator_default_sink_query (GstAggregator * self,
2057 GstAggregatorPad * aggpad, GstQuery * query)
2059 GstPad *pad = GST_PAD (aggpad);
2061 if (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION) {
2062 GstQuery *decide_query = NULL;
2063 GstAggregatorClass *agg_class;
2066 GST_OBJECT_LOCK (self);
2068 if (G_UNLIKELY (!aggpad->priv->negotiated)) {
2069 GST_DEBUG_OBJECT (self,
2070 "not negotiated yet, can't answer ALLOCATION query");
2071 PAD_UNLOCK (aggpad);
2072 GST_OBJECT_UNLOCK (self);
2077 if ((decide_query = self->priv->allocation_query))
2078 gst_query_ref (decide_query);
2079 PAD_UNLOCK (aggpad);
2080 GST_OBJECT_UNLOCK (self);
2082 GST_DEBUG_OBJECT (self,
2083 "calling propose allocation with query %" GST_PTR_FORMAT, decide_query);
2085 agg_class = GST_AGGREGATOR_GET_CLASS (self);
2087 /* pass the query to the propose_allocation vmethod if any */
2088 if (agg_class->propose_allocation)
2089 ret = agg_class->propose_allocation (self, aggpad, decide_query, query);
2094 gst_query_unref (decide_query);
2096 GST_DEBUG_OBJECT (self, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
2100 return gst_pad_query_default (pad, GST_OBJECT (self), query);
2104 gst_aggregator_finalize (GObject * object)
2106 GstAggregator *self = (GstAggregator *) object;
2108 g_mutex_clear (&self->priv->src_lock);
2109 g_cond_clear (&self->priv->src_cond);
2111 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
2115 * gst_aggregator_set_latency_property:
2116 * @agg: a #GstAggregator
2117 * @latency: the new latency value (in nanoseconds).
2119 * Sets the new latency value to @latency. This value is used to limit the
2120 * amount of time a pad waits for data to appear before considering the pad
2124 gst_aggregator_set_latency_property (GstAggregator * self, GstClockTime latency)
2128 g_return_if_fail (GST_IS_AGGREGATOR (self));
2129 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
2132 changed = (self->priv->latency != latency);
2137 GST_OBJECT_LOCK (self);
2138 /* First lock all the pads */
2139 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2140 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2144 self->priv->latency = latency;
2146 SRC_BROADCAST (self);
2148 /* Now wake up the pads */
2149 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2150 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2151 PAD_BROADCAST_EVENT (aggpad);
2152 PAD_UNLOCK (aggpad);
2154 GST_OBJECT_UNLOCK (self);
2160 gst_element_post_message (GST_ELEMENT_CAST (self),
2161 gst_message_new_latency (GST_OBJECT_CAST (self)));
2165 * gst_aggregator_get_latency_property:
2166 * @agg: a #GstAggregator
2168 * Gets the latency value. See gst_aggregator_set_latency for
2171 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
2172 * before a pad is deemed unresponsive. A value of -1 means an
2176 gst_aggregator_get_latency_property (GstAggregator * agg)
2180 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), GST_CLOCK_TIME_NONE);
2182 GST_OBJECT_LOCK (agg);
2183 res = agg->priv->latency;
2184 GST_OBJECT_UNLOCK (agg);
2190 gst_aggregator_set_property (GObject * object, guint prop_id,
2191 const GValue * value, GParamSpec * pspec)
2193 GstAggregator *agg = GST_AGGREGATOR (object);
2197 gst_aggregator_set_latency_property (agg, g_value_get_uint64 (value));
2199 case PROP_START_TIME_SELECTION:
2200 agg->priv->start_time_selection = g_value_get_enum (value);
2202 case PROP_START_TIME:
2203 agg->priv->start_time = g_value_get_uint64 (value);
2206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2212 gst_aggregator_get_property (GObject * object, guint prop_id,
2213 GValue * value, GParamSpec * pspec)
2215 GstAggregator *agg = GST_AGGREGATOR (object);
2219 g_value_set_uint64 (value, gst_aggregator_get_latency_property (agg));
2221 case PROP_START_TIME_SELECTION:
2222 g_value_set_enum (value, agg->priv->start_time_selection);
2224 case PROP_START_TIME:
2225 g_value_set_uint64 (value, agg->priv->start_time);
2228 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2233 /* GObject vmethods implementations */
2235 gst_aggregator_class_init (GstAggregatorClass * klass)
2237 GObjectClass *gobject_class = (GObjectClass *) klass;
2238 GstElementClass *gstelement_class = (GstElementClass *) klass;
2240 aggregator_parent_class = g_type_class_peek_parent (klass);
2241 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
2243 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
2244 GST_DEBUG_FG_MAGENTA, "GstAggregator");
2246 klass->sink_event = gst_aggregator_default_sink_event;
2247 klass->sink_query = gst_aggregator_default_sink_query;
2249 klass->src_event = gst_aggregator_default_src_event;
2250 klass->src_query = gst_aggregator_default_src_query;
2252 klass->create_new_pad = gst_aggregator_default_create_new_pad;
2253 klass->update_src_caps = gst_aggregator_default_update_src_caps;
2254 klass->fixate_src_caps = gst_aggregator_default_fixate_src_caps;
2255 klass->negotiated_src_caps = gst_aggregator_default_negotiated_src_caps;
2257 gstelement_class->request_new_pad =
2258 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
2259 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
2260 gstelement_class->release_pad =
2261 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
2262 gstelement_class->change_state =
2263 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
2265 gobject_class->set_property = gst_aggregator_set_property;
2266 gobject_class->get_property = gst_aggregator_get_property;
2267 gobject_class->finalize = gst_aggregator_finalize;
2269 g_object_class_install_property (gobject_class, PROP_LATENCY,
2270 g_param_spec_uint64 ("latency", "Buffer latency",
2271 "Additional latency in live mode to allow upstream "
2272 "to take longer to produce buffers for the current "
2273 "position (in nanoseconds)", 0, G_MAXUINT64,
2274 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2276 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
2277 g_param_spec_enum ("start-time-selection", "Start Time Selection",
2278 "Decides which start time is output",
2279 gst_aggregator_start_time_selection_get_type (),
2280 DEFAULT_START_TIME_SELECTION,
2281 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2283 g_object_class_install_property (gobject_class, PROP_START_TIME,
2284 g_param_spec_uint64 ("start-time", "Start Time",
2285 "Start time to use if start-time-selection=set", 0,
2287 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2289 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_do_events_and_queries);
2293 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
2295 GstPadTemplate *pad_template;
2296 GstAggregatorPrivate *priv;
2298 g_return_if_fail (klass->aggregate != NULL);
2301 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2302 GstAggregatorPrivate);
2307 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2308 g_return_if_fail (pad_template != NULL);
2310 priv->max_padserial = -1;
2311 priv->tags_changed = FALSE;
2313 self->priv->peer_latency_live = FALSE;
2314 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2315 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2316 self->priv->has_peer_latency = FALSE;
2317 gst_aggregator_reset_flow_values (self);
2319 self->srcpad = gst_pad_new_from_template (pad_template, "src");
2321 gst_pad_set_event_function (self->srcpad,
2322 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2323 gst_pad_set_query_function (self->srcpad,
2324 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2325 gst_pad_set_activatemode_function (self->srcpad,
2326 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2328 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2330 self->priv->latency = DEFAULT_LATENCY;
2331 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2332 self->priv->start_time = DEFAULT_START_TIME;
2334 g_mutex_init (&self->priv->src_lock);
2335 g_cond_init (&self->priv->src_cond);
2338 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2339 * method to get to the padtemplates */
2341 gst_aggregator_get_type (void)
2343 static volatile gsize type = 0;
2345 if (g_once_init_enter (&type)) {
2347 static const GTypeInfo info = {
2348 sizeof (GstAggregatorClass),
2351 (GClassInitFunc) gst_aggregator_class_init,
2354 sizeof (GstAggregator),
2356 (GInstanceInitFunc) gst_aggregator_init,
2359 _type = g_type_register_static (GST_TYPE_ELEMENT,
2360 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2361 g_once_init_leave (&type, _type);
2366 /* Must be called with SRC lock and PAD lock held */
2368 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2370 /* Empty queue always has space */
2371 if (aggpad->priv->num_buffers == 0 && aggpad->priv->clipped_buffer == NULL)
2374 /* We also want at least two buffers, one is being processed and one is ready
2375 * for the next iteration when we operate in live mode. */
2376 if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2379 /* zero latency, if there is a buffer, it's full */
2380 if (self->priv->latency == 0)
2383 /* Allow no more buffers than the latency */
2384 return (aggpad->priv->time_level <= self->priv->latency);
2387 /* Must be called with the PAD_LOCK held */
2389 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2391 GstClockTime timestamp;
2393 if (GST_BUFFER_DTS_IS_VALID (buffer))
2394 timestamp = GST_BUFFER_DTS (buffer);
2396 timestamp = GST_BUFFER_PTS (buffer);
2398 if (timestamp == GST_CLOCK_TIME_NONE) {
2400 timestamp = aggpad->priv->head_position;
2402 timestamp = aggpad->priv->tail_position;
2406 if (GST_BUFFER_DURATION_IS_VALID (buffer))
2407 timestamp += GST_BUFFER_DURATION (buffer);
2410 aggpad->priv->head_position = timestamp;
2412 aggpad->priv->tail_position = timestamp;
2414 update_time_level (aggpad, head);
2418 * Can be called either from the sinkpad's chain function or from the srcpad's
2419 * thread in the case of a buffer synthetized from a GAP event.
2420 * Because of this second case, FLUSH_LOCK can't be used here.
2423 static GstFlowReturn
2424 gst_aggregator_pad_chain_internal (GstAggregator * self,
2425 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2427 GstFlowReturn flow_return;
2428 GstClockTime buf_pts;
2430 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2433 flow_return = aggpad->priv->flow_return;
2434 if (flow_return != GST_FLOW_OK)
2437 PAD_UNLOCK (aggpad);
2439 buf_pts = GST_BUFFER_PTS (buffer);
2443 GST_OBJECT_LOCK (self);
2446 if (aggpad->priv->first_buffer) {
2447 self->priv->has_peer_latency = FALSE;
2448 aggpad->priv->first_buffer = FALSE;
2451 if ((gst_aggregator_pad_has_space (self, aggpad) || !head)
2452 && aggpad->priv->flow_return == GST_FLOW_OK) {
2454 g_queue_push_head (&aggpad->priv->data, buffer);
2456 g_queue_push_tail (&aggpad->priv->data, buffer);
2457 apply_buffer (aggpad, buffer, head);
2458 aggpad->priv->num_buffers++;
2460 SRC_BROADCAST (self);
2464 flow_return = aggpad->priv->flow_return;
2465 if (flow_return != GST_FLOW_OK) {
2466 GST_OBJECT_UNLOCK (self);
2470 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2471 GST_OBJECT_UNLOCK (self);
2473 PAD_WAIT_EVENT (aggpad);
2475 PAD_UNLOCK (aggpad);
2478 if (self->priv->first_buffer) {
2479 GstClockTime start_time;
2481 switch (self->priv->start_time_selection) {
2482 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2486 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2487 GST_OBJECT_LOCK (aggpad);
2488 if (aggpad->priv->head_segment.format == GST_FORMAT_TIME) {
2489 start_time = buf_pts;
2490 if (start_time != -1) {
2491 start_time = MAX (start_time, aggpad->priv->head_segment.start);
2493 gst_segment_to_running_time (&aggpad->priv->head_segment,
2494 GST_FORMAT_TIME, start_time);
2498 GST_WARNING_OBJECT (aggpad,
2499 "Ignoring request of selecting the first start time "
2500 "as the segment is a %s segment instead of a time segment",
2501 gst_format_get_name (aggpad->segment.format));
2503 GST_OBJECT_UNLOCK (aggpad);
2505 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2506 start_time = self->priv->start_time;
2507 if (start_time == -1)
2512 if (start_time != -1) {
2513 if (self->segment.position == -1)
2514 self->segment.position = start_time;
2516 self->segment.position = MIN (start_time, self->segment.position);
2518 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2519 GST_TIME_ARGS (start_time));
2523 PAD_UNLOCK (aggpad);
2524 GST_OBJECT_UNLOCK (self);
2527 GST_DEBUG_OBJECT (aggpad, "Done chaining");
2532 PAD_UNLOCK (aggpad);
2534 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2535 gst_flow_get_name (flow_return));
2537 gst_buffer_unref (buffer);
2542 static GstFlowReturn
2543 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2546 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2548 PAD_FLUSH_LOCK (aggpad);
2550 ret = gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2551 aggpad, buffer, TRUE);
2553 PAD_FLUSH_UNLOCK (aggpad);
2559 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2562 GstAggregator *self = GST_AGGREGATOR (parent);
2563 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2565 if (GST_QUERY_IS_SERIALIZED (query)) {
2567 gboolean ret = FALSE;
2572 if (aggpad->priv->flow_return != GST_FLOW_OK) {
2577 g_queue_push_head (&aggpad->priv->data, query);
2578 SRC_BROADCAST (self);
2581 while (!gst_aggregator_pad_queue_is_empty (aggpad)
2582 && aggpad->priv->flow_return == GST_FLOW_OK) {
2583 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2584 PAD_WAIT_EVENT (aggpad);
2587 s = gst_query_writable_structure (query);
2588 if (gst_structure_get_boolean (s, "gst-aggregator-retval", &ret))
2589 gst_structure_remove_field (s, "gst-aggregator-retval");
2591 g_queue_remove (&aggpad->priv->data, query);
2593 if (aggpad->priv->flow_return != GST_FLOW_OK)
2596 PAD_UNLOCK (aggpad);
2600 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2602 return klass->sink_query (self, aggpad, query);
2606 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2607 gst_flow_get_name (aggpad->priv->flow_return));
2608 PAD_UNLOCK (aggpad);
2613 /* Queue serialized events and let the others go through directly.
2614 * The queued events with be handled from the src-pad task in
2615 * gst_aggregator_do_events_and_queries().
2617 static GstFlowReturn
2618 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2621 GstFlowReturn ret = GST_FLOW_OK;
2622 GstAggregator *self = GST_AGGREGATOR (parent);
2623 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2625 if (GST_EVENT_IS_SERIALIZED (event)
2626 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2630 if (aggpad->priv->flow_return != GST_FLOW_OK)
2633 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2634 GST_OBJECT_LOCK (aggpad);
2635 gst_event_copy_segment (event, &aggpad->priv->head_segment);
2636 aggpad->priv->head_position = aggpad->priv->head_segment.position;
2637 update_time_level (aggpad, TRUE);
2638 GST_OBJECT_UNLOCK (aggpad);
2641 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT, event);
2642 g_queue_push_head (&aggpad->priv->data, event);
2643 SRC_BROADCAST (self);
2644 PAD_UNLOCK (aggpad);
2647 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2649 if (!klass->sink_event (self, aggpad, event)) {
2650 /* Copied from GstPad to convert boolean to a GstFlowReturn in
2651 * the event handling func */
2652 ret = GST_FLOW_ERROR;
2659 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2660 gst_flow_get_name (aggpad->priv->flow_return));
2661 PAD_UNLOCK (aggpad);
2663 if (GST_EVENT_IS_STICKY (event))
2664 gst_pad_store_sticky_event (pad, event);
2665 gst_event_unref (event);
2667 return aggpad->priv->flow_return;
2671 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2672 GstObject * parent, GstPadMode mode, gboolean active)
2674 GstAggregator *self = GST_AGGREGATOR (parent);
2675 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2677 if (active == FALSE) {
2679 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2680 SRC_BROADCAST (self);
2684 aggpad->priv->flow_return = GST_FLOW_OK;
2685 PAD_BROADCAST_EVENT (aggpad);
2686 PAD_UNLOCK (aggpad);
2692 /***********************************
2693 * GstAggregatorPad implementation *
2694 ************************************/
2695 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2698 gst_aggregator_pad_constructed (GObject * object)
2700 GstPad *pad = GST_PAD (object);
2702 gst_pad_set_chain_function (pad,
2703 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2704 gst_pad_set_event_full_function_full (pad,
2705 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
2706 gst_pad_set_query_function (pad,
2707 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2708 gst_pad_set_activatemode_function (pad,
2709 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2713 gst_aggregator_pad_finalize (GObject * object)
2715 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2717 g_cond_clear (&pad->priv->event_cond);
2718 g_mutex_clear (&pad->priv->flush_lock);
2719 g_mutex_clear (&pad->priv->lock);
2721 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2725 gst_aggregator_pad_dispose (GObject * object)
2727 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2729 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2731 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2735 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2737 GObjectClass *gobject_class = (GObjectClass *) klass;
2739 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2741 gobject_class->constructed = gst_aggregator_pad_constructed;
2742 gobject_class->finalize = gst_aggregator_pad_finalize;
2743 gobject_class->dispose = gst_aggregator_pad_dispose;
2747 gst_aggregator_pad_init (GstAggregatorPad * pad)
2750 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2751 GstAggregatorPadPrivate);
2753 g_queue_init (&pad->priv->data);
2754 g_cond_init (&pad->priv->event_cond);
2756 g_mutex_init (&pad->priv->flush_lock);
2757 g_mutex_init (&pad->priv->lock);
2759 gst_aggregator_pad_reset_unlocked (pad);
2760 pad->priv->negotiated = FALSE;
2763 /* Must be called with the PAD_LOCK held */
2765 gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad)
2767 pad->priv->num_buffers--;
2768 GST_TRACE_OBJECT (pad, "Consuming buffer");
2769 PAD_BROADCAST_EVENT (pad);
2772 /* Must be called with the PAD_LOCK held */
2774 gst_aggregator_pad_clip_buffer_unlocked (GstAggregatorPad * pad)
2776 GstAggregator *self = NULL;
2777 GstAggregatorClass *aggclass = NULL;
2778 GstBuffer *buffer = NULL;
2780 while (pad->priv->clipped_buffer == NULL &&
2781 GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->data))) {
2782 buffer = g_queue_pop_tail (&pad->priv->data);
2784 apply_buffer (pad, buffer, FALSE);
2786 /* We only take the parent here so that it's not taken if the buffer is
2787 * already clipped or if the queue is empty.
2790 self = GST_AGGREGATOR (gst_pad_get_parent_element (GST_PAD (pad)));
2792 gst_buffer_unref (buffer);
2796 aggclass = GST_AGGREGATOR_GET_CLASS (self);
2799 if (aggclass->clip) {
2800 GST_TRACE_OBJECT (pad, "Clipping: %" GST_PTR_FORMAT, buffer);
2802 buffer = aggclass->clip (self, pad, buffer);
2804 if (buffer == NULL) {
2805 gst_aggregator_pad_buffer_consumed (pad);
2806 GST_TRACE_OBJECT (pad, "Clipping consumed the buffer");
2810 pad->priv->clipped_buffer = buffer;
2814 gst_object_unref (self);
2818 * gst_aggregator_pad_steal_buffer:
2819 * @pad: the pad to get buffer from
2821 * Steal the ref to the buffer currently queued in @pad.
2823 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2824 * queued. You should unref the buffer after usage.
2827 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2833 gst_aggregator_pad_clip_buffer_unlocked (pad);
2835 buffer = pad->priv->clipped_buffer;
2838 pad->priv->clipped_buffer = NULL;
2839 gst_aggregator_pad_buffer_consumed (pad);
2840 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2849 * gst_aggregator_pad_drop_buffer:
2850 * @pad: the pad where to drop any pending buffer
2852 * Drop the buffer currently queued in @pad.
2854 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2857 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2861 buf = gst_aggregator_pad_steal_buffer (pad);
2866 gst_buffer_unref (buf);
2871 * gst_aggregator_pad_get_buffer:
2872 * @pad: the pad to get buffer from
2874 * Returns: (transfer full): A reference to the buffer in @pad or
2875 * NULL if no buffer was queued. You should unref the buffer after
2879 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2885 gst_aggregator_pad_clip_buffer_unlocked (pad);
2887 if (pad->priv->clipped_buffer) {
2888 buffer = gst_buffer_ref (pad->priv->clipped_buffer);
2898 * gst_aggregator_pad_is_eos:
2899 * @pad: an aggregator pad
2901 * Returns: %TRUE if the pad is EOS, otherwise %FALSE.
2904 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2909 is_eos = pad->priv->eos;
2916 * gst_aggregator_merge_tags:
2917 * @self: a #GstAggregator
2918 * @tags: a #GstTagList to merge
2919 * @mode: the #GstTagMergeMode to use
2921 * Adds tags to so-called pending tags, which will be processed
2922 * before pushing out data downstream.
2924 * Note that this is provided for convenience, and the subclass is
2925 * not required to use this and can still do tag handling on its own.
2930 gst_aggregator_merge_tags (GstAggregator * self,
2931 const GstTagList * tags, GstTagMergeMode mode)
2935 g_return_if_fail (GST_IS_AGGREGATOR (self));
2936 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2938 /* FIXME Check if we can use OBJECT lock here! */
2939 GST_OBJECT_LOCK (self);
2941 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2942 otags = self->priv->tags;
2943 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2945 gst_tag_list_unref (otags);
2946 self->priv->tags_changed = TRUE;
2947 GST_OBJECT_UNLOCK (self);
2951 * gst_aggregator_set_latency:
2952 * @self: a #GstAggregator
2953 * @min_latency: minimum latency
2954 * @max_latency: maximum latency
2956 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2957 * latency is. Will also post a LATENCY message on the bus so the pipeline
2958 * can reconfigure its global latency.
2961 gst_aggregator_set_latency (GstAggregator * self,
2962 GstClockTime min_latency, GstClockTime max_latency)
2964 gboolean changed = FALSE;
2966 g_return_if_fail (GST_IS_AGGREGATOR (self));
2967 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2968 g_return_if_fail (max_latency >= min_latency);
2971 if (self->priv->sub_latency_min != min_latency) {
2972 self->priv->sub_latency_min = min_latency;
2975 if (self->priv->sub_latency_max != max_latency) {
2976 self->priv->sub_latency_max = max_latency;
2981 SRC_BROADCAST (self);
2985 gst_element_post_message (GST_ELEMENT_CAST (self),
2986 gst_message_new_latency (GST_OBJECT_CAST (self)));
2991 * gst_aggregator_get_buffer_pool:
2992 * @self: a #GstAggregator
2994 * Returns: (transfer full): the instance of the #GstBufferPool used
2995 * by @trans; free it after use it
2998 gst_aggregator_get_buffer_pool (GstAggregator * self)
3000 GstBufferPool *pool;
3002 g_return_val_if_fail (GST_IS_AGGREGATOR (self), NULL);
3004 GST_OBJECT_LOCK (self);
3005 pool = self->priv->pool;
3007 gst_object_ref (pool);
3008 GST_OBJECT_UNLOCK (self);
3014 * gst_aggregator_get_allocator:
3015 * @self: a #GstAggregator
3016 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3018 * @params: (out) (allow-none) (transfer full): the
3019 * #GstAllocationParams of @allocator
3021 * Lets #GstAggregator sub-classes get the memory @allocator
3022 * acquired by the base class and its @params.
3024 * Unref the @allocator after use it.
3027 gst_aggregator_get_allocator (GstAggregator * self,
3028 GstAllocator ** allocator, GstAllocationParams * params)
3030 g_return_if_fail (GST_IS_AGGREGATOR (self));
3033 *allocator = self->priv->allocator ?
3034 gst_object_ref (self->priv->allocator) : NULL;
3037 *params = self->priv->allocation_params;