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_peek_buffer () method, and remove it from the pad
45 * with the gst_aggregator_pad_pop_buffer () method. When a buffer
46 * has been taken with pop_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 * * Subclasses must use (a subclass of) #GstAggregatorPad for both their
65 * sink and source pads.
66 * See gst_element_class_add_static_pad_template_with_gtype().
68 * This class used to live in gst-plugins-bad and was moved to core.
74 * SECTION: gstaggregatorpad
75 * @title: GstAggregatorPad
76 * @short_description: #GstPad subclass for pads managed by #GstAggregator
77 * @see_also: gstcollectpads for historical reasons.
79 * Pads managed by a #GstAggregor subclass.
81 * This class used to live in gst-plugins-bad and was moved to core.
90 #include <string.h> /* strlen */
92 #include "gstaggregator.h"
96 GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
97 GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
98 GST_AGGREGATOR_START_TIME_SELECTION_SET
99 } GstAggregatorStartTimeSelection;
102 gst_aggregator_start_time_selection_get_type (void)
104 static GType gtype = 0;
107 static const GEnumValue values[] = {
108 {GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
109 "Start at 0 running time (default)", "zero"},
110 {GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
111 "Start at first observed input running time", "first"},
112 {GST_AGGREGATOR_START_TIME_SELECTION_SET,
113 "Set start time with start-time property", "set"},
117 gtype = g_enum_register_static ("GstAggregatorStartTimeSelection", values);
122 /* Might become API */
124 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
125 const GstTagList * tags, GstTagMergeMode mode);
127 static void gst_aggregator_set_latency_property (GstAggregator * agg,
128 GstClockTime latency);
129 static GstClockTime gst_aggregator_get_latency_property (GstAggregator * agg);
131 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
133 static void gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad,
136 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
137 #define GST_CAT_DEFAULT aggregator_debug
139 /* Locking order, locks in this element must always be taken in this order
141 * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
142 * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
143 * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
144 * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
145 * standard element object lock -> GST_OBJECT_LOCK(agg)
146 * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
147 * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
148 * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
151 /* GstAggregatorPad definitions */
152 #define PAD_LOCK(pad) G_STMT_START { \
153 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
155 g_mutex_lock(&pad->priv->lock); \
156 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
160 #define PAD_UNLOCK(pad) G_STMT_START { \
161 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
163 g_mutex_unlock(&pad->priv->lock); \
164 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
169 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
170 GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p", \
172 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
173 (&((GstAggregatorPad*)pad)->priv->lock)); \
174 GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
178 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
179 GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p", \
181 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
185 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
186 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
188 g_mutex_lock(&pad->priv->flush_lock); \
189 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
193 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
194 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
196 g_mutex_unlock(&pad->priv->flush_lock); \
197 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
201 #define SRC_LOCK(self) G_STMT_START { \
202 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
204 g_mutex_lock(&self->priv->src_lock); \
205 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
209 #define SRC_UNLOCK(self) G_STMT_START { \
210 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
212 g_mutex_unlock(&self->priv->src_lock); \
213 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
217 #define SRC_WAIT(self) G_STMT_START { \
218 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
220 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
221 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
225 #define SRC_BROADCAST(self) G_STMT_START { \
226 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
228 if (self->priv->aggregate_id) \
229 gst_clock_id_unschedule (self->priv->aggregate_id); \
230 g_cond_broadcast(&(self->priv->src_cond)); \
233 struct _GstAggregatorPadPrivate
235 /* Following fields are protected by the PAD_LOCK */
236 GstFlowReturn flow_return;
237 gboolean pending_flush_start;
238 gboolean pending_flush_stop;
240 gboolean first_buffer;
242 GQueue data; /* buffers, events and queries */
243 GstBuffer *clipped_buffer;
246 /* used to track fill state of queues, only used with live-src and when
247 * latency property is set to > 0 */
248 GstClockTime head_position;
249 GstClockTime tail_position;
250 GstClockTime head_time; /* running time */
251 GstClockTime tail_time;
252 GstClockTime time_level; /* how much head is ahead of tail */
253 GstSegment head_segment; /* segment before the queue */
261 /* This lock prevents a flush start processing happening while
262 * the chain function is also happening.
267 gboolean emit_signals;
270 /* Must be called with PAD_LOCK held */
272 gst_aggregator_pad_reset_unlocked (GstAggregatorPad * aggpad)
274 aggpad->priv->eos = FALSE;
275 aggpad->priv->flow_return = GST_FLOW_OK;
276 GST_OBJECT_LOCK (aggpad);
277 gst_segment_init (&aggpad->segment, GST_FORMAT_UNDEFINED);
278 gst_segment_init (&aggpad->priv->head_segment, GST_FORMAT_UNDEFINED);
279 GST_OBJECT_UNLOCK (aggpad);
280 aggpad->priv->head_position = GST_CLOCK_TIME_NONE;
281 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
282 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
283 aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
284 aggpad->priv->time_level = 0;
285 aggpad->priv->first_buffer = TRUE;
289 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
291 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
294 gst_aggregator_pad_reset_unlocked (aggpad);
298 return (klass->flush (aggpad, agg) == GST_FLOW_OK);
303 /*************************************
304 * GstAggregator implementation *
305 *************************************/
306 static GstElementClass *aggregator_parent_class = NULL;
307 static gint aggregator_private_offset = 0;
309 /* All members are protected by the object lock unless otherwise noted */
311 struct _GstAggregatorPrivate
315 /* Our state is >= PAUSED */
316 gboolean running; /* protected by src_lock */
318 /* seqnum from seek or segment,
319 * to be applied to synthetic segment/eos events */
321 gboolean send_stream_start; /* protected by srcpad stream lock */
322 gboolean send_segment;
323 gboolean flush_seeking;
324 gboolean pending_flush_start;
325 gboolean send_eos; /* protected by srcpad stream lock */
327 GstCaps *srccaps; /* protected by the srcpad stream lock */
330 gboolean tags_changed;
332 gboolean peer_latency_live; /* protected by src_lock */
333 GstClockTime peer_latency_min; /* protected by src_lock */
334 GstClockTime peer_latency_max; /* protected by src_lock */
335 gboolean has_peer_latency; /* protected by src_lock */
337 GstClockTime sub_latency_min; /* protected by src_lock */
338 GstClockTime sub_latency_max; /* protected by src_lock */
340 GstClockTime upstream_latency_min; /* protected by src_lock */
343 GstClockID aggregate_id; /* protected by src_lock */
347 gboolean first_buffer; /* protected by object lock */
348 GstAggregatorStartTimeSelection start_time_selection;
349 GstClockTime start_time;
351 /* protected by the object lock */
352 GstQuery *allocation_query;
353 GstAllocator *allocator;
355 GstAllocationParams allocation_params;
358 gint64 latency; /* protected by both src_lock and all pad locks */
361 /* Seek event forwarding helper */
367 gboolean only_to_active_pads;
371 gboolean one_actually_seeked;
374 #define DEFAULT_LATENCY 0
375 #define DEFAULT_MIN_UPSTREAM_LATENCY 0
376 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
377 #define DEFAULT_START_TIME (-1)
383 PROP_MIN_UPSTREAM_LATENCY,
384 PROP_START_TIME_SELECTION,
389 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
390 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
393 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
395 return (g_queue_peek_tail (&pad->priv->data) == NULL &&
396 pad->priv->clipped_buffer == NULL);
400 gst_aggregator_check_pads_ready (GstAggregator * self)
402 GstAggregatorPad *pad = NULL;
404 gboolean have_buffer = TRUE;
405 gboolean have_event_or_query = FALSE;
407 GST_LOG_OBJECT (self, "checking pads");
409 GST_OBJECT_LOCK (self);
411 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
412 if (sinkpads == NULL)
415 for (l = sinkpads; l != NULL; l = l->next) {
420 if (pad->priv->num_buffers == 0) {
421 if (!gst_aggregator_pad_queue_is_empty (pad))
422 have_event_or_query = TRUE;
423 if (!pad->priv->eos) {
426 /* If not live we need data on all pads, so leave the loop */
427 if (!self->priv->peer_latency_live) {
432 } else if (self->priv->peer_latency_live) {
433 /* In live mode, having a single pad with buffers is enough to
434 * generate a start time from it. In non-live mode all pads need
437 self->priv->first_buffer = FALSE;
443 if (!have_buffer && !have_event_or_query)
447 self->priv->first_buffer = FALSE;
449 GST_OBJECT_UNLOCK (self);
450 GST_LOG_OBJECT (self, "pads are ready");
455 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
456 GST_OBJECT_UNLOCK (self);
461 if (have_event_or_query)
462 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet,"
463 " but waking up for serialized event");
465 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
466 GST_OBJECT_UNLOCK (self);
467 return have_event_or_query;
472 gst_aggregator_reset_flow_values (GstAggregator * self)
474 GST_OBJECT_LOCK (self);
475 self->priv->send_stream_start = TRUE;
476 self->priv->send_segment = TRUE;
477 gst_segment_init (&GST_AGGREGATOR_PAD (self->srcpad)->segment,
479 self->priv->first_buffer = TRUE;
480 GST_OBJECT_UNLOCK (self);
484 gst_aggregator_push_mandatory_events (GstAggregator * self)
486 GstAggregatorPrivate *priv = self->priv;
487 GstEvent *segment = NULL;
488 GstEvent *tags = NULL;
490 if (self->priv->send_stream_start) {
493 GST_INFO_OBJECT (self, "pushing stream start");
494 /* stream-start (FIXME: create id based on input ids) */
495 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
496 if (!gst_pad_push_event (GST_PAD (self->srcpad),
497 gst_event_new_stream_start (s_id))) {
498 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
500 self->priv->send_stream_start = FALSE;
503 if (self->priv->srccaps) {
505 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
506 self->priv->srccaps);
507 if (!gst_pad_push_event (GST_PAD (self->srcpad),
508 gst_event_new_caps (self->priv->srccaps))) {
509 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
511 gst_caps_unref (self->priv->srccaps);
512 self->priv->srccaps = NULL;
515 GST_OBJECT_LOCK (self);
516 if (self->priv->send_segment && !self->priv->flush_seeking) {
518 gst_event_new_segment (&GST_AGGREGATOR_PAD (self->srcpad)->segment);
520 if (!self->priv->seqnum)
521 /* This code-path is in preparation to be able to run without a source
522 * connected. Then we won't have a seq-num from a segment event. */
523 self->priv->seqnum = gst_event_get_seqnum (segment);
525 gst_event_set_seqnum (segment, self->priv->seqnum);
526 self->priv->send_segment = FALSE;
528 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
531 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
532 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
533 priv->tags_changed = FALSE;
535 GST_OBJECT_UNLOCK (self);
538 gst_pad_push_event (self->srcpad, segment);
540 gst_pad_push_event (self->srcpad, tags);
545 * gst_aggregator_set_src_caps:
546 * @self: The #GstAggregator
547 * @caps: The #GstCaps to set on the src pad.
549 * Sets the caps to be used on the src pad.
552 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
554 GST_PAD_STREAM_LOCK (self->srcpad);
555 gst_caps_replace (&self->priv->srccaps, caps);
556 gst_aggregator_push_mandatory_events (self);
557 GST_PAD_STREAM_UNLOCK (self->srcpad);
561 gst_aggregator_default_finish_buffer (GstAggregator * self, GstBuffer * buffer)
563 gst_aggregator_push_mandatory_events (self);
565 GST_OBJECT_LOCK (self);
566 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
567 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
568 GST_OBJECT_UNLOCK (self);
569 return gst_pad_push (self->srcpad, buffer);
571 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
572 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
573 GST_OBJECT_UNLOCK (self);
574 gst_buffer_unref (buffer);
580 * gst_aggregator_finish_buffer:
581 * @aggregator: The #GstAggregator
582 * @buffer: (transfer full): the #GstBuffer to push.
584 * This method will push the provided output buffer downstream. If needed,
585 * mandatory events such as stream-start, caps, and segment events will be
586 * sent before pushing the buffer.
589 gst_aggregator_finish_buffer (GstAggregator * aggregator, GstBuffer * buffer)
591 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (aggregator);
593 g_assert (klass->finish_buffer != NULL);
595 return klass->finish_buffer (aggregator, buffer);
599 gst_aggregator_push_eos (GstAggregator * self)
602 gst_aggregator_push_mandatory_events (self);
604 event = gst_event_new_eos ();
606 GST_OBJECT_LOCK (self);
607 self->priv->send_eos = FALSE;
608 gst_event_set_seqnum (event, self->priv->seqnum);
609 GST_OBJECT_UNLOCK (self);
611 gst_pad_push_event (self->srcpad, event);
615 gst_aggregator_get_next_time (GstAggregator * self)
617 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
619 if (klass->get_next_time)
620 return klass->get_next_time (self);
622 return GST_CLOCK_TIME_NONE;
626 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
628 GstClockTime latency;
636 latency = gst_aggregator_get_latency_unlocked (self);
638 if (gst_aggregator_check_pads_ready (self)) {
639 GST_DEBUG_OBJECT (self, "all pads have data");
645 /* Before waiting, check if we're actually still running */
646 if (!self->priv->running || !self->priv->send_eos) {
652 start = gst_aggregator_get_next_time (self);
654 /* If we're not live, or if we use the running time
655 * of the first buffer as start time, we wait until
656 * all pads have buffers.
657 * Otherwise (i.e. if we are live!), we wait on the clock
658 * and if a pad does not have a buffer in time we ignore
661 GST_OBJECT_LOCK (self);
662 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
663 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
664 !GST_CLOCK_TIME_IS_VALID (start) ||
665 (self->priv->first_buffer
666 && self->priv->start_time_selection ==
667 GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
668 /* We wake up here when something happened, and below
669 * then check if we're ready now. If we return FALSE,
670 * we will be directly called again.
672 GST_OBJECT_UNLOCK (self);
675 GstClockTime base_time, time;
677 GstClockReturn status;
678 GstClockTimeDiff jitter;
680 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
681 GST_TIME_ARGS (start));
683 base_time = GST_ELEMENT_CAST (self)->base_time;
684 clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
685 GST_OBJECT_UNLOCK (self);
687 time = base_time + start;
690 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
691 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
692 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
693 GST_TIME_ARGS (time),
694 GST_TIME_ARGS (base_time),
695 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
696 GST_TIME_ARGS (gst_clock_get_time (clock)));
698 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
699 gst_object_unref (clock);
703 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
706 if (self->priv->aggregate_id) {
707 gst_clock_id_unref (self->priv->aggregate_id);
708 self->priv->aggregate_id = NULL;
711 GST_DEBUG_OBJECT (self,
712 "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
713 status, GST_STIME_ARGS (jitter));
716 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
723 res = gst_aggregator_check_pads_ready (self);
731 gboolean processed_event;
732 GstFlowReturn flow_ret;
733 } DoHandleEventsAndQueriesData;
736 gst_aggregator_do_events_and_queries (GstElement * self, GstPad * epad,
739 GstAggregatorPad *pad = GST_AGGREGATOR_PAD_CAST (epad);
740 GstAggregator *aggregator = GST_AGGREGATOR_CAST (self);
741 GstEvent *event = NULL;
742 GstQuery *query = NULL;
743 GstAggregatorClass *klass = NULL;
744 DoHandleEventsAndQueriesData *data = user_data;
751 if (pad->priv->clipped_buffer == NULL &&
752 !GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->data))) {
753 if (GST_IS_EVENT (g_queue_peek_tail (&pad->priv->data)))
754 event = gst_event_ref (g_queue_peek_tail (&pad->priv->data));
755 if (GST_IS_QUERY (g_queue_peek_tail (&pad->priv->data)))
756 query = g_queue_peek_tail (&pad->priv->data);
759 if (event || query) {
762 data->processed_event = TRUE;
764 klass = GST_AGGREGATOR_GET_CLASS (self);
767 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
768 gst_event_ref (event);
769 ret = klass->sink_event (aggregator, pad, event);
772 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
773 pad->priv->negotiated = ret;
775 pad->priv->flow_return = data->flow_ret = GST_FLOW_NOT_NEGOTIATED;
777 if (g_queue_peek_tail (&pad->priv->data) == event)
778 gst_event_unref (g_queue_pop_tail (&pad->priv->data));
779 gst_event_unref (event);
781 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, query);
782 ret = klass->sink_query (aggregator, pad, query);
785 if (g_queue_peek_tail (&pad->priv->data) == query) {
788 s = gst_query_writable_structure (query);
789 gst_structure_set (s, "gst-aggregator-retval", G_TYPE_BOOLEAN, ret,
791 g_queue_pop_tail (&pad->priv->data);
795 PAD_BROADCAST_EVENT (pad);
798 } while (event || query);
804 gst_aggregator_pad_skip_buffers (GstElement * self, GstPad * epad,
808 GstAggregatorPad *aggpad = (GstAggregatorPad *) epad;
809 GstAggregator *agg = (GstAggregator *) self;
810 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
812 if (!klass->skip_buffer)
817 item = g_queue_peek_head_link (&aggpad->priv->data);
819 GList *next = item->next;
821 if (GST_IS_BUFFER (item->data)
822 && klass->skip_buffer (aggpad, agg, item->data)) {
823 GST_LOG_OBJECT (aggpad, "Skipping %" GST_PTR_FORMAT, item->data);
824 gst_aggregator_pad_buffer_consumed (aggpad, GST_BUFFER (item->data));
825 gst_buffer_unref (item->data);
826 g_queue_delete_link (&aggpad->priv->data, item);
840 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
841 GstFlowReturn flow_return, gboolean full)
846 if (flow_return == GST_FLOW_NOT_LINKED)
847 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
849 aggpad->priv->flow_return = flow_return;
851 item = g_queue_peek_head_link (&aggpad->priv->data);
853 GList *next = item->next;
855 /* In partial flush, we do like the pad, we get rid of non-sticky events
858 if (full || GST_IS_BUFFER (item->data) ||
859 GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
860 GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
861 !GST_EVENT_IS_STICKY (item->data)) {
862 if (!GST_IS_QUERY (item->data))
863 gst_mini_object_unref (item->data);
864 g_queue_delete_link (&aggpad->priv->data, item);
868 aggpad->priv->num_buffers = 0;
869 gst_buffer_replace (&aggpad->priv->clipped_buffer, NULL);
871 PAD_BROADCAST_EVENT (aggpad);
876 gst_aggregator_default_update_src_caps (GstAggregator * agg, GstCaps * caps,
879 *ret = gst_caps_ref (caps);
885 gst_aggregator_default_fixate_src_caps (GstAggregator * agg, GstCaps * caps)
887 caps = gst_caps_fixate (caps);
893 gst_aggregator_default_negotiated_src_caps (GstAggregator * agg, GstCaps * caps)
899 /* takes ownership of the pool, allocator and query */
901 gst_aggregator_set_allocation (GstAggregator * self,
902 GstBufferPool * pool, GstAllocator * allocator,
903 GstAllocationParams * params, GstQuery * query)
905 GstAllocator *oldalloc;
906 GstBufferPool *oldpool;
909 GST_DEBUG ("storing allocation query");
911 GST_OBJECT_LOCK (self);
912 oldpool = self->priv->pool;
913 self->priv->pool = pool;
915 oldalloc = self->priv->allocator;
916 self->priv->allocator = allocator;
918 oldquery = self->priv->allocation_query;
919 self->priv->allocation_query = query;
922 self->priv->allocation_params = *params;
924 gst_allocation_params_init (&self->priv->allocation_params);
925 GST_OBJECT_UNLOCK (self);
928 GST_DEBUG_OBJECT (self, "deactivating old pool %p", oldpool);
929 gst_buffer_pool_set_active (oldpool, FALSE);
930 gst_object_unref (oldpool);
933 gst_object_unref (oldalloc);
936 gst_query_unref (oldquery);
943 gst_aggregator_decide_allocation (GstAggregator * self, GstQuery * query)
945 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
947 if (aggclass->decide_allocation)
948 if (!aggclass->decide_allocation (self, query))
955 gst_aggregator_do_allocation (GstAggregator * self, GstCaps * caps)
958 gboolean result = TRUE;
959 GstBufferPool *pool = NULL;
960 GstAllocator *allocator;
961 GstAllocationParams params;
963 /* find a pool for the negotiated caps now */
964 GST_DEBUG_OBJECT (self, "doing allocation query");
965 query = gst_query_new_allocation (caps, TRUE);
966 if (!gst_pad_peer_query (self->srcpad, query)) {
967 /* not a problem, just debug a little */
968 GST_DEBUG_OBJECT (self, "peer ALLOCATION query failed");
971 GST_DEBUG_OBJECT (self, "calling decide_allocation");
972 result = gst_aggregator_decide_allocation (self, query);
974 GST_DEBUG_OBJECT (self, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
978 goto no_decide_allocation;
980 /* we got configuration from our peer or the decide_allocation method,
982 if (gst_query_get_n_allocation_params (query) > 0) {
983 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
986 gst_allocation_params_init (¶ms);
989 if (gst_query_get_n_allocation_pools (query) > 0)
990 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
994 gst_aggregator_set_allocation (self, pool, allocator, ¶ms, query);
999 no_decide_allocation:
1001 GST_WARNING_OBJECT (self, "Failed to decide allocation");
1002 gst_query_unref (query);
1009 /* WITH SRC_LOCK held */
1010 static GstFlowReturn
1011 gst_aggregator_update_src_caps (GstAggregator * self)
1013 GstAggregatorClass *agg_klass = GST_AGGREGATOR_GET_CLASS (self);
1014 GstCaps *downstream_caps, *template_caps, *caps = NULL;
1015 GstFlowReturn ret = GST_FLOW_OK;
1017 template_caps = gst_pad_get_pad_template_caps (self->srcpad);
1018 downstream_caps = gst_pad_peer_query_caps (self->srcpad, template_caps);
1020 if (gst_caps_is_empty (downstream_caps)) {
1021 GST_INFO_OBJECT (self, "Downstream caps (%"
1022 GST_PTR_FORMAT ") not compatible with pad template caps (%"
1023 GST_PTR_FORMAT ")", downstream_caps, template_caps);
1024 ret = GST_FLOW_NOT_NEGOTIATED;
1028 g_assert (agg_klass->update_src_caps);
1029 GST_DEBUG_OBJECT (self, "updating caps from %" GST_PTR_FORMAT,
1031 ret = agg_klass->update_src_caps (self, downstream_caps, &caps);
1032 if (ret < GST_FLOW_OK) {
1033 GST_WARNING_OBJECT (self, "Subclass failed to update provided caps");
1036 if ((caps == NULL || gst_caps_is_empty (caps)) && ret >= GST_FLOW_OK) {
1037 ret = GST_FLOW_NOT_NEGOTIATED;
1040 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1042 #ifdef GST_ENABLE_EXTRA_CHECKS
1043 if (!gst_caps_is_subset (caps, template_caps)) {
1044 GstCaps *intersection;
1046 GST_ERROR_OBJECT (self,
1047 "update_src_caps returned caps %" GST_PTR_FORMAT
1048 " which are not a real subset of the template caps %"
1049 GST_PTR_FORMAT, caps, template_caps);
1050 g_warning ("%s: update_src_caps returned caps which are not a real "
1051 "subset of the filter caps", GST_ELEMENT_NAME (self));
1054 gst_caps_intersect_full (template_caps, caps, GST_CAPS_INTERSECT_FIRST);
1055 gst_caps_unref (caps);
1056 caps = intersection;
1060 if (gst_caps_is_any (caps)) {
1064 if (!gst_caps_is_fixed (caps)) {
1065 g_assert (agg_klass->fixate_src_caps);
1067 GST_DEBUG_OBJECT (self, "fixate caps from %" GST_PTR_FORMAT, caps);
1068 if (!(caps = agg_klass->fixate_src_caps (self, caps))) {
1069 GST_WARNING_OBJECT (self, "Subclass failed to fixate provided caps");
1070 ret = GST_FLOW_NOT_NEGOTIATED;
1073 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1076 if (agg_klass->negotiated_src_caps) {
1077 if (!agg_klass->negotiated_src_caps (self, caps)) {
1078 GST_WARNING_OBJECT (self, "Subclass failed to accept negotiated caps");
1079 ret = GST_FLOW_NOT_NEGOTIATED;
1084 gst_aggregator_set_src_caps (self, caps);
1086 if (!gst_aggregator_do_allocation (self, caps)) {
1087 GST_WARNING_OBJECT (self, "Allocation negotiation failed");
1088 ret = GST_FLOW_NOT_NEGOTIATED;
1092 gst_caps_unref (downstream_caps);
1093 gst_caps_unref (template_caps);
1096 gst_caps_unref (caps);
1102 gst_aggregator_aggregate_func (GstAggregator * self)
1104 GstAggregatorPrivate *priv = self->priv;
1105 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1106 gboolean timeout = FALSE;
1108 if (self->priv->running == FALSE) {
1109 GST_DEBUG_OBJECT (self, "Not running anymore");
1113 GST_LOG_OBJECT (self, "Checking aggregate");
1114 while (priv->send_eos && priv->running) {
1115 GstFlowReturn flow_return = GST_FLOW_OK;
1116 DoHandleEventsAndQueriesData events_query_data = { FALSE, GST_FLOW_OK };
1118 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (self),
1119 gst_aggregator_do_events_and_queries, &events_query_data);
1121 if ((flow_return = events_query_data.flow_ret) != GST_FLOW_OK)
1124 if (self->priv->peer_latency_live)
1125 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (self),
1126 gst_aggregator_pad_skip_buffers, NULL);
1128 /* Ensure we have buffers ready (either in clipped_buffer or at the head of
1130 if (!gst_aggregator_wait_and_check (self, &timeout))
1133 events_query_data.processed_event = FALSE;
1134 events_query_data.flow_ret = GST_FLOW_OK;
1135 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (self),
1136 gst_aggregator_do_events_and_queries, &events_query_data);
1138 if ((flow_return = events_query_data.flow_ret) != GST_FLOW_OK)
1141 if (events_query_data.processed_event)
1144 if (gst_pad_check_reconfigure (GST_AGGREGATOR_SRC_PAD (self))) {
1145 flow_return = gst_aggregator_update_src_caps (self);
1146 if (flow_return != GST_FLOW_OK)
1147 gst_pad_mark_reconfigure (GST_AGGREGATOR_SRC_PAD (self));
1150 if (timeout || flow_return >= GST_FLOW_OK) {
1151 GST_TRACE_OBJECT (self, "Actually aggregating!");
1152 flow_return = klass->aggregate (self, timeout);
1155 if (flow_return == GST_AGGREGATOR_FLOW_NEED_DATA)
1158 GST_OBJECT_LOCK (self);
1159 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
1160 /* We don't want to set the pads to flushing, but we want to
1161 * stop the thread, so just break here */
1162 GST_OBJECT_UNLOCK (self);
1165 GST_OBJECT_UNLOCK (self);
1167 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
1168 gst_aggregator_push_eos (self);
1172 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
1174 if (flow_return != GST_FLOW_OK) {
1177 GST_OBJECT_LOCK (self);
1178 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
1179 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1181 gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
1183 GST_OBJECT_UNLOCK (self);
1188 /* Pause the task here, the only ways to get here are:
1189 * 1) We're stopping, in which case the task is stopped anyway
1190 * 2) We got a flow error above, in which case it might take
1191 * some time to forward the flow return upstream and we
1192 * would otherwise call the task function over and over
1193 * again without doing anything
1195 gst_pad_pause_task (self->srcpad);
1199 gst_aggregator_start (GstAggregator * self)
1201 GstAggregatorClass *klass;
1204 self->priv->send_stream_start = TRUE;
1205 self->priv->send_segment = TRUE;
1206 self->priv->send_eos = TRUE;
1207 self->priv->srccaps = NULL;
1209 gst_aggregator_set_allocation (self, NULL, NULL, NULL, NULL);
1211 klass = GST_AGGREGATOR_GET_CLASS (self);
1214 result = klass->start (self);
1222 _check_pending_flush_stop (GstAggregatorPad * pad)
1227 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
1234 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
1236 gboolean res = TRUE;
1238 GST_INFO_OBJECT (self, "%s srcpad task",
1239 flush_start ? "Pausing" : "Stopping");
1242 self->priv->running = FALSE;
1243 SRC_BROADCAST (self);
1247 res = gst_pad_push_event (self->srcpad, flush_start);
1250 gst_pad_stop_task (self->srcpad);
1256 gst_aggregator_start_srcpad_task (GstAggregator * self)
1258 GST_INFO_OBJECT (self, "Starting srcpad task");
1260 self->priv->running = TRUE;
1261 gst_pad_start_task (GST_PAD (self->srcpad),
1262 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
1265 static GstFlowReturn
1266 gst_aggregator_flush (GstAggregator * self)
1268 GstFlowReturn ret = GST_FLOW_OK;
1269 GstAggregatorPrivate *priv = self->priv;
1270 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1272 GST_DEBUG_OBJECT (self, "Flushing everything");
1273 GST_OBJECT_LOCK (self);
1274 priv->send_segment = TRUE;
1275 priv->flush_seeking = FALSE;
1276 priv->tags_changed = FALSE;
1277 GST_OBJECT_UNLOCK (self);
1279 ret = klass->flush (self);
1285 /* Called with GstAggregator's object lock held */
1288 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
1291 GstAggregatorPad *tmppad;
1293 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
1294 tmppad = (GstAggregatorPad *) tmp->data;
1296 if (_check_pending_flush_stop (tmppad) == FALSE) {
1297 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
1298 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
1307 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
1310 GstAggregatorPrivate *priv = self->priv;
1311 GstAggregatorPadPrivate *padpriv = aggpad->priv;
1313 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
1315 PAD_FLUSH_LOCK (aggpad);
1317 if (padpriv->pending_flush_start) {
1318 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
1320 padpriv->pending_flush_start = FALSE;
1321 padpriv->pending_flush_stop = TRUE;
1323 PAD_UNLOCK (aggpad);
1325 GST_OBJECT_LOCK (self);
1326 if (priv->flush_seeking) {
1327 /* If flush_seeking we forward the first FLUSH_START */
1328 if (priv->pending_flush_start) {
1329 priv->pending_flush_start = FALSE;
1330 GST_OBJECT_UNLOCK (self);
1332 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
1333 gst_aggregator_stop_srcpad_task (self, event);
1335 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
1336 GST_PAD_STREAM_LOCK (self->srcpad);
1337 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
1340 GST_OBJECT_UNLOCK (self);
1341 gst_event_unref (event);
1344 GST_OBJECT_UNLOCK (self);
1345 gst_event_unref (event);
1347 PAD_FLUSH_UNLOCK (aggpad);
1350 /* Must be called with the the PAD_LOCK held */
1352 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1354 GstAggregatorPadPrivate *priv = aggpad->priv;
1357 if (GST_CLOCK_TIME_IS_VALID (priv->head_position) &&
1358 priv->head_segment.format == GST_FORMAT_TIME)
1359 priv->head_time = gst_segment_to_running_time (&priv->head_segment,
1360 GST_FORMAT_TIME, priv->head_position);
1362 priv->head_time = GST_CLOCK_TIME_NONE;
1364 if (!GST_CLOCK_TIME_IS_VALID (priv->tail_time))
1365 priv->tail_time = priv->head_time;
1367 if (GST_CLOCK_TIME_IS_VALID (priv->tail_position) &&
1368 aggpad->segment.format == GST_FORMAT_TIME)
1369 priv->tail_time = gst_segment_to_running_time (&aggpad->segment,
1370 GST_FORMAT_TIME, priv->tail_position);
1372 priv->tail_time = priv->head_time;
1375 if (priv->head_time == GST_CLOCK_TIME_NONE ||
1376 priv->tail_time == GST_CLOCK_TIME_NONE) {
1377 priv->time_level = 0;
1381 if (priv->tail_time > priv->head_time)
1382 priv->time_level = 0;
1384 priv->time_level = priv->head_time - priv->tail_time;
1388 /* GstAggregator vmethods default implementations */
1390 gst_aggregator_default_sink_event (GstAggregator * self,
1391 GstAggregatorPad * aggpad, GstEvent * event)
1393 gboolean res = TRUE;
1394 GstPad *pad = GST_PAD (aggpad);
1395 GstAggregatorPrivate *priv = self->priv;
1397 GST_DEBUG_OBJECT (aggpad, "Got event: %" GST_PTR_FORMAT, event);
1399 switch (GST_EVENT_TYPE (event)) {
1400 case GST_EVENT_FLUSH_START:
1402 gst_aggregator_flush_start (self, aggpad, event);
1403 /* We forward only in one case: right after flush_seeking */
1407 case GST_EVENT_FLUSH_STOP:
1409 gst_aggregator_pad_flush (aggpad, self);
1410 GST_OBJECT_LOCK (self);
1411 if (priv->flush_seeking) {
1412 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1413 if (gst_aggregator_all_flush_stop_received_locked (self)) {
1414 GST_OBJECT_UNLOCK (self);
1415 /* That means we received FLUSH_STOP/FLUSH_STOP on
1416 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1417 gst_aggregator_flush (self);
1418 gst_pad_push_event (self->srcpad, event);
1421 priv->send_eos = TRUE;
1422 SRC_BROADCAST (self);
1425 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1426 GST_PAD_STREAM_UNLOCK (self->srcpad);
1427 gst_aggregator_start_srcpad_task (self);
1429 GST_OBJECT_UNLOCK (self);
1432 GST_OBJECT_UNLOCK (self);
1435 /* We never forward the event */
1442 g_assert (aggpad->priv->num_buffers == 0);
1443 aggpad->priv->eos = TRUE;
1444 PAD_UNLOCK (aggpad);
1445 SRC_BROADCAST (self);
1449 case GST_EVENT_SEGMENT:
1452 GST_OBJECT_LOCK (aggpad);
1453 gst_event_copy_segment (event, &aggpad->segment);
1454 /* We've got a new segment, tail_position is now meaningless
1455 * and may interfere with the time_level calculation
1457 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
1458 update_time_level (aggpad, FALSE);
1459 GST_OBJECT_UNLOCK (aggpad);
1460 PAD_UNLOCK (aggpad);
1462 GST_OBJECT_LOCK (self);
1463 self->priv->seqnum = gst_event_get_seqnum (event);
1464 GST_OBJECT_UNLOCK (self);
1467 case GST_EVENT_STREAM_START:
1473 GstClockTime pts, endpts;
1474 GstClockTime duration;
1477 gst_event_parse_gap (event, &pts, &duration);
1479 if (GST_CLOCK_TIME_IS_VALID (duration))
1480 endpts = pts + duration;
1482 endpts = GST_CLOCK_TIME_NONE;
1484 GST_OBJECT_LOCK (aggpad);
1485 res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1487 GST_OBJECT_UNLOCK (aggpad);
1490 GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1494 if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1495 duration = endpts - pts;
1497 duration = GST_CLOCK_TIME_NONE;
1499 gapbuf = gst_buffer_new ();
1500 GST_BUFFER_PTS (gapbuf) = pts;
1501 GST_BUFFER_DURATION (gapbuf) = duration;
1502 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1503 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1505 /* Remove GAP event so we can replace it with the buffer */
1507 if (g_queue_peek_tail (&aggpad->priv->data) == event)
1508 gst_event_unref (g_queue_pop_tail (&aggpad->priv->data));
1509 PAD_UNLOCK (aggpad);
1511 if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1513 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1527 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1528 return gst_pad_event_default (pad, GST_OBJECT (self), event);
1531 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1533 gst_event_unref (event);
1539 gst_aggregator_stop_pad (GstElement * self, GstPad * epad, gpointer user_data)
1541 GstAggregatorPad *pad = GST_AGGREGATOR_PAD_CAST (epad);
1542 GstAggregator *agg = GST_AGGREGATOR_CAST (self);
1544 gst_aggregator_pad_flush (pad, agg);
1547 pad->priv->flow_return = GST_FLOW_FLUSHING;
1548 pad->priv->negotiated = FALSE;
1549 PAD_BROADCAST_EVENT (pad);
1556 gst_aggregator_stop (GstAggregator * agg)
1558 GstAggregatorClass *klass;
1561 gst_aggregator_reset_flow_values (agg);
1563 /* Application needs to make sure no pads are added while it shuts us down */
1564 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (agg),
1565 gst_aggregator_stop_pad, NULL);
1567 klass = GST_AGGREGATOR_GET_CLASS (agg);
1570 result = klass->stop (agg);
1574 agg->priv->has_peer_latency = FALSE;
1575 agg->priv->peer_latency_live = FALSE;
1576 agg->priv->peer_latency_min = agg->priv->peer_latency_max = 0;
1578 if (agg->priv->tags)
1579 gst_tag_list_unref (agg->priv->tags);
1580 agg->priv->tags = NULL;
1582 gst_aggregator_set_allocation (agg, NULL, NULL, NULL, NULL);
1587 /* GstElement vmethods implementations */
1588 static GstStateChangeReturn
1589 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1591 GstStateChangeReturn ret;
1592 GstAggregator *self = GST_AGGREGATOR (element);
1594 switch (transition) {
1595 case GST_STATE_CHANGE_READY_TO_PAUSED:
1596 if (!gst_aggregator_start (self))
1604 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1605 transition)) == GST_STATE_CHANGE_FAILURE)
1609 switch (transition) {
1610 case GST_STATE_CHANGE_PAUSED_TO_READY:
1611 if (!gst_aggregator_stop (self)) {
1612 /* What to do in this case? Error out? */
1613 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1625 GST_ERROR_OBJECT (element, "parent failed state change");
1630 GST_ERROR_OBJECT (element, "Subclass failed to start");
1631 return GST_STATE_CHANGE_FAILURE;
1636 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1638 GstAggregator *self = GST_AGGREGATOR (element);
1639 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1641 GST_INFO_OBJECT (pad, "Removing pad");
1644 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1645 gst_element_remove_pad (element, pad);
1647 self->priv->has_peer_latency = FALSE;
1648 SRC_BROADCAST (self);
1652 static GstAggregatorPad *
1653 gst_aggregator_default_create_new_pad (GstAggregator * self,
1654 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1656 GstAggregatorPad *agg_pad;
1657 GstAggregatorPrivate *priv = self->priv;
1661 GST_PAD_TEMPLATE_GTYPE (templ) ==
1662 G_TYPE_NONE ? GST_TYPE_AGGREGATOR_PAD : GST_PAD_TEMPLATE_GTYPE (templ);
1664 if (templ->direction != GST_PAD_SINK)
1667 if (templ->presence != GST_PAD_REQUEST)
1670 GST_OBJECT_LOCK (self);
1671 if (req_name == NULL || strlen (req_name) < 6
1672 || !g_str_has_prefix (req_name, "sink_")
1673 || strrchr (req_name, '%') != NULL) {
1674 /* no name given when requesting the pad, use next available int */
1675 serial = ++priv->max_padserial;
1677 gchar *endptr = NULL;
1679 /* parse serial number from requested padname */
1680 serial = g_ascii_strtoull (&req_name[5], &endptr, 10);
1681 if (endptr != NULL && *endptr == '\0') {
1682 if (serial > priv->max_padserial) {
1683 priv->max_padserial = serial;
1686 serial = ++priv->max_padserial;
1690 name = g_strdup_printf ("sink_%u", serial);
1691 g_assert (g_type_is_a (pad_type, GST_TYPE_AGGREGATOR_PAD));
1692 agg_pad = g_object_new (pad_type,
1693 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1696 GST_OBJECT_UNLOCK (self);
1703 GST_WARNING_OBJECT (self, "request new pad that is not a SINK pad");
1708 GST_WARNING_OBJECT (self, "request new pad that is not a REQUEST pad");
1714 gst_aggregator_request_new_pad (GstElement * element,
1715 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1717 GstAggregator *self;
1718 GstAggregatorPad *agg_pad;
1719 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1720 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1722 self = GST_AGGREGATOR (element);
1724 agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1726 GST_ERROR_OBJECT (element, "Couldn't create new pad");
1730 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1733 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1735 /* add the pad to the element */
1736 gst_element_add_pad (element, GST_PAD (agg_pad));
1738 return GST_PAD (agg_pad);
1741 /* Must be called with SRC_LOCK held */
1744 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1746 gboolean query_ret, live;
1747 GstClockTime our_latency, min, max;
1749 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1752 GST_WARNING_OBJECT (self, "Latency query failed");
1756 gst_query_parse_latency (query, &live, &min, &max);
1758 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1759 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1760 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1764 if (self->priv->upstream_latency_min > min) {
1765 GstClockTimeDiff diff =
1766 GST_CLOCK_DIFF (min, self->priv->upstream_latency_min);
1769 if (GST_CLOCK_TIME_IS_VALID (max)) {
1774 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1775 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1776 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1777 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1778 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1782 our_latency = self->priv->latency;
1784 self->priv->peer_latency_live = live;
1785 self->priv->peer_latency_min = min;
1786 self->priv->peer_latency_max = max;
1787 self->priv->has_peer_latency = TRUE;
1791 min += self->priv->sub_latency_min;
1792 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1793 && GST_CLOCK_TIME_IS_VALID (max))
1794 max += self->priv->sub_latency_max + our_latency;
1796 max = GST_CLOCK_TIME_NONE;
1798 SRC_BROADCAST (self);
1800 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1801 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1803 gst_query_set_latency (query, live, min, max);
1809 * MUST be called with the src_lock held.
1811 * See gst_aggregator_get_latency() for doc
1814 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1816 GstClockTime latency;
1818 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1820 if (!self->priv->has_peer_latency) {
1821 GstQuery *query = gst_query_new_latency ();
1824 ret = gst_aggregator_query_latency_unlocked (self, query);
1825 gst_query_unref (query);
1827 return GST_CLOCK_TIME_NONE;
1830 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1831 return GST_CLOCK_TIME_NONE;
1833 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1834 latency = self->priv->peer_latency_min;
1837 latency += self->priv->latency;
1838 latency += self->priv->sub_latency_min;
1844 * gst_aggregator_get_latency:
1845 * @self: a #GstAggregator
1847 * Retrieves the latency values reported by @self in response to the latency
1848 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1849 * will not wait for the clock.
1851 * Typically only called by subclasses.
1853 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1856 gst_aggregator_get_latency (GstAggregator * self)
1861 ret = gst_aggregator_get_latency_unlocked (self);
1868 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1870 GstAggregator *self = GST_AGGREGATOR (element);
1872 GST_STATE_LOCK (element);
1873 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1874 GST_STATE (element) < GST_STATE_PAUSED) {
1878 GstSeekType start_type, stop_type;
1881 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1882 &start, &stop_type, &stop);
1884 GST_OBJECT_LOCK (self);
1885 gst_segment_do_seek (&GST_AGGREGATOR_PAD (self->srcpad)->segment, rate, fmt,
1886 flags, start_type, start, stop_type, stop, NULL);
1887 self->priv->seqnum = gst_event_get_seqnum (event);
1888 self->priv->first_buffer = FALSE;
1889 GST_OBJECT_UNLOCK (self);
1891 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1893 GST_STATE_UNLOCK (element);
1896 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1901 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1903 gboolean res = TRUE;
1905 switch (GST_QUERY_TYPE (query)) {
1906 case GST_QUERY_SEEKING:
1910 /* don't pass it along as some (file)sink might claim it does
1911 * whereas with a collectpads in between that will not likely work */
1912 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1913 gst_query_set_seeking (query, format, FALSE, 0, -1);
1918 case GST_QUERY_LATENCY:
1920 res = gst_aggregator_query_latency_unlocked (self, query);
1924 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1931 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1933 EventData *evdata = user_data;
1934 gboolean ret = TRUE;
1935 GstPad *peer = gst_pad_get_peer (pad);
1936 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1939 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1940 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1943 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1944 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1949 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1950 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1952 GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1954 if (gst_pad_query (peer, seeking)) {
1957 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1959 if (seekable == FALSE) {
1960 GST_INFO_OBJECT (pad,
1961 "Source not seekable, We failed but it does not matter!");
1966 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1969 gst_query_unref (seeking);
1972 if (evdata->flush) {
1974 aggpad->priv->pending_flush_start = FALSE;
1975 aggpad->priv->pending_flush_stop = FALSE;
1976 PAD_UNLOCK (aggpad);
1979 evdata->one_actually_seeked = TRUE;
1982 evdata->result &= ret;
1985 gst_object_unref (peer);
1987 /* Always send to all pads */
1992 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1995 evdata->result = TRUE;
1996 evdata->one_actually_seeked = FALSE;
1998 /* We first need to set all pads as flushing in a first pass
1999 * as flush_start flush_stop is sometimes sent synchronously
2000 * while we send the seek event */
2001 if (evdata->flush) {
2004 GST_OBJECT_LOCK (self);
2005 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
2006 GstAggregatorPad *pad = l->data;
2009 pad->priv->pending_flush_start = TRUE;
2010 pad->priv->pending_flush_stop = FALSE;
2013 GST_OBJECT_UNLOCK (self);
2016 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, evdata);
2018 gst_event_unref (evdata->event);
2022 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
2027 GstSeekType start_type, stop_type;
2030 EventData evdata = { 0, };
2031 GstAggregatorPrivate *priv = self->priv;
2033 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
2034 &start, &stop_type, &stop);
2036 GST_INFO_OBJECT (self, "starting SEEK");
2038 flush = flags & GST_SEEK_FLAG_FLUSH;
2040 GST_OBJECT_LOCK (self);
2042 priv->pending_flush_start = TRUE;
2043 priv->flush_seeking = TRUE;
2046 gst_segment_do_seek (&GST_AGGREGATOR_PAD (self->srcpad)->segment, rate, fmt,
2047 flags, start_type, start, stop_type, stop, NULL);
2049 /* Seeking sets a position */
2050 self->priv->first_buffer = FALSE;
2051 GST_OBJECT_UNLOCK (self);
2053 /* forward the seek upstream */
2054 evdata.event = event;
2055 evdata.flush = flush;
2056 evdata.only_to_active_pads = FALSE;
2057 gst_aggregator_forward_event_to_all_sinkpads (self, &evdata);
2060 if (!evdata.result || !evdata.one_actually_seeked) {
2061 GST_OBJECT_LOCK (self);
2062 priv->flush_seeking = FALSE;
2063 priv->pending_flush_start = FALSE;
2064 GST_OBJECT_UNLOCK (self);
2067 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
2069 return evdata.result;
2073 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
2075 EventData evdata = { 0, };
2077 switch (GST_EVENT_TYPE (event)) {
2078 case GST_EVENT_SEEK:
2079 /* _do_seek() unrefs the event. */
2080 return gst_aggregator_do_seek (self, event);
2081 case GST_EVENT_NAVIGATION:
2082 /* navigation is rather pointless. */
2083 gst_event_unref (event);
2089 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
2090 * they will receive a QOS event that has earliest_time=0 (because we can't
2091 * have negative timestamps), and consider their buffer as too late */
2092 evdata.event = event;
2093 evdata.flush = FALSE;
2094 evdata.only_to_active_pads = GST_EVENT_TYPE (event) == GST_EVENT_QOS;
2095 gst_aggregator_forward_event_to_all_sinkpads (self, &evdata);
2096 return evdata.result;
2100 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
2103 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2105 return klass->src_event (GST_AGGREGATOR (parent), event);
2109 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
2112 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2114 return klass->src_query (GST_AGGREGATOR (parent), query);
2118 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
2119 GstObject * parent, GstPadMode mode, gboolean active)
2121 GstAggregator *self = GST_AGGREGATOR (parent);
2122 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2124 if (klass->src_activate) {
2125 if (klass->src_activate (self, mode, active) == FALSE) {
2130 if (active == TRUE) {
2132 case GST_PAD_MODE_PUSH:
2134 GST_INFO_OBJECT (pad, "Activating pad!");
2135 gst_aggregator_start_srcpad_task (self);
2140 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
2147 GST_INFO_OBJECT (self, "Deactivating srcpad");
2148 gst_aggregator_stop_srcpad_task (self, FALSE);
2154 gst_aggregator_default_sink_query (GstAggregator * self,
2155 GstAggregatorPad * aggpad, GstQuery * query)
2157 GstPad *pad = GST_PAD (aggpad);
2159 if (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION) {
2160 GstQuery *decide_query = NULL;
2161 GstAggregatorClass *agg_class;
2164 GST_OBJECT_LOCK (self);
2166 if (G_UNLIKELY (!aggpad->priv->negotiated)) {
2167 GST_DEBUG_OBJECT (self,
2168 "not negotiated yet, can't answer ALLOCATION query");
2169 PAD_UNLOCK (aggpad);
2170 GST_OBJECT_UNLOCK (self);
2175 if ((decide_query = self->priv->allocation_query))
2176 gst_query_ref (decide_query);
2177 PAD_UNLOCK (aggpad);
2178 GST_OBJECT_UNLOCK (self);
2180 GST_DEBUG_OBJECT (self,
2181 "calling propose allocation with query %" GST_PTR_FORMAT, decide_query);
2183 agg_class = GST_AGGREGATOR_GET_CLASS (self);
2185 /* pass the query to the propose_allocation vmethod if any */
2186 if (agg_class->propose_allocation)
2187 ret = agg_class->propose_allocation (self, aggpad, decide_query, query);
2192 gst_query_unref (decide_query);
2194 GST_DEBUG_OBJECT (self, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
2198 return gst_pad_query_default (pad, GST_OBJECT (self), query);
2202 gst_aggregator_finalize (GObject * object)
2204 GstAggregator *self = (GstAggregator *) object;
2206 g_mutex_clear (&self->priv->src_lock);
2207 g_cond_clear (&self->priv->src_cond);
2209 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
2213 * gst_aggregator_set_latency_property:
2214 * @agg: a #GstAggregator
2215 * @latency: the new latency value (in nanoseconds).
2217 * Sets the new latency value to @latency. This value is used to limit the
2218 * amount of time a pad waits for data to appear before considering the pad
2222 gst_aggregator_set_latency_property (GstAggregator * self, GstClockTime latency)
2226 g_return_if_fail (GST_IS_AGGREGATOR (self));
2227 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
2230 changed = (self->priv->latency != latency);
2235 GST_OBJECT_LOCK (self);
2236 /* First lock all the pads */
2237 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2238 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2242 self->priv->latency = latency;
2244 SRC_BROADCAST (self);
2246 /* Now wake up the pads */
2247 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2248 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2249 PAD_BROADCAST_EVENT (aggpad);
2250 PAD_UNLOCK (aggpad);
2252 GST_OBJECT_UNLOCK (self);
2258 gst_element_post_message (GST_ELEMENT_CAST (self),
2259 gst_message_new_latency (GST_OBJECT_CAST (self)));
2263 * gst_aggregator_get_latency_property:
2264 * @agg: a #GstAggregator
2266 * Gets the latency value. See gst_aggregator_set_latency for
2269 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
2270 * before a pad is deemed unresponsive. A value of -1 means an
2274 gst_aggregator_get_latency_property (GstAggregator * agg)
2278 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), GST_CLOCK_TIME_NONE);
2280 GST_OBJECT_LOCK (agg);
2281 res = agg->priv->latency;
2282 GST_OBJECT_UNLOCK (agg);
2288 gst_aggregator_set_property (GObject * object, guint prop_id,
2289 const GValue * value, GParamSpec * pspec)
2291 GstAggregator *agg = GST_AGGREGATOR (object);
2295 gst_aggregator_set_latency_property (agg, g_value_get_uint64 (value));
2297 case PROP_MIN_UPSTREAM_LATENCY:
2299 agg->priv->upstream_latency_min = g_value_get_uint64 (value);
2302 case PROP_START_TIME_SELECTION:
2303 agg->priv->start_time_selection = g_value_get_enum (value);
2305 case PROP_START_TIME:
2306 agg->priv->start_time = g_value_get_uint64 (value);
2309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2315 gst_aggregator_get_property (GObject * object, guint prop_id,
2316 GValue * value, GParamSpec * pspec)
2318 GstAggregator *agg = GST_AGGREGATOR (object);
2322 g_value_set_uint64 (value, gst_aggregator_get_latency_property (agg));
2324 case PROP_MIN_UPSTREAM_LATENCY:
2326 g_value_set_uint64 (value, agg->priv->upstream_latency_min);
2329 case PROP_START_TIME_SELECTION:
2330 g_value_set_enum (value, agg->priv->start_time_selection);
2332 case PROP_START_TIME:
2333 g_value_set_uint64 (value, agg->priv->start_time);
2336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2341 /* GObject vmethods implementations */
2343 gst_aggregator_class_init (GstAggregatorClass * klass)
2345 GObjectClass *gobject_class = (GObjectClass *) klass;
2346 GstElementClass *gstelement_class = (GstElementClass *) klass;
2348 aggregator_parent_class = g_type_class_peek_parent (klass);
2350 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
2351 GST_DEBUG_FG_MAGENTA, "GstAggregator");
2353 if (aggregator_private_offset != 0)
2354 g_type_class_adjust_private_offset (klass, &aggregator_private_offset);
2356 klass->finish_buffer = gst_aggregator_default_finish_buffer;
2358 klass->sink_event = gst_aggregator_default_sink_event;
2359 klass->sink_query = gst_aggregator_default_sink_query;
2361 klass->src_event = gst_aggregator_default_src_event;
2362 klass->src_query = gst_aggregator_default_src_query;
2364 klass->create_new_pad = gst_aggregator_default_create_new_pad;
2365 klass->update_src_caps = gst_aggregator_default_update_src_caps;
2366 klass->fixate_src_caps = gst_aggregator_default_fixate_src_caps;
2367 klass->negotiated_src_caps = gst_aggregator_default_negotiated_src_caps;
2369 gstelement_class->request_new_pad =
2370 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
2371 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
2372 gstelement_class->release_pad =
2373 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
2374 gstelement_class->change_state =
2375 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
2377 gobject_class->set_property = gst_aggregator_set_property;
2378 gobject_class->get_property = gst_aggregator_get_property;
2379 gobject_class->finalize = gst_aggregator_finalize;
2381 g_object_class_install_property (gobject_class, PROP_LATENCY,
2382 g_param_spec_uint64 ("latency", "Buffer latency",
2383 "Additional latency in live mode to allow upstream "
2384 "to take longer to produce buffers for the current "
2385 "position (in nanoseconds)", 0, G_MAXUINT64,
2386 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2389 * GstAggregator:min-upstream-latency:
2391 * Force minimum upstream latency (in nanoseconds). When sources with a
2392 * higher latency are expected to be plugged in dynamically after the
2393 * aggregator has started playing, this allows overriding the minimum
2394 * latency reported by the initial source(s). This is only taken into
2395 * account when larger than the actually reported minimum latency.
2399 g_object_class_install_property (gobject_class, PROP_MIN_UPSTREAM_LATENCY,
2400 g_param_spec_uint64 ("min-upstream-latency", "Buffer latency",
2401 "When sources with a higher latency are expected to be plugged "
2402 "in dynamically after the aggregator has started playing, "
2403 "this allows overriding the minimum latency reported by the "
2404 "initial source(s). This is only taken into account when larger "
2405 "than the actually reported minimum latency. (nanoseconds)",
2407 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2409 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
2410 g_param_spec_enum ("start-time-selection", "Start Time Selection",
2411 "Decides which start time is output",
2412 gst_aggregator_start_time_selection_get_type (),
2413 DEFAULT_START_TIME_SELECTION,
2414 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2416 g_object_class_install_property (gobject_class, PROP_START_TIME,
2417 g_param_spec_uint64 ("start-time", "Start Time",
2418 "Start time to use if start-time-selection=set", 0,
2420 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2423 static inline gpointer
2424 gst_aggregator_get_instance_private (GstAggregator * self)
2426 return (G_STRUCT_MEMBER_P (self, aggregator_private_offset));
2430 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
2432 GstPadTemplate *pad_template;
2433 GstAggregatorPrivate *priv;
2436 g_return_if_fail (klass->aggregate != NULL);
2438 self->priv = gst_aggregator_get_instance_private (self);
2443 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2444 g_return_if_fail (pad_template != NULL);
2446 priv->max_padserial = -1;
2447 priv->tags_changed = FALSE;
2449 self->priv->peer_latency_live = FALSE;
2450 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2451 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2452 self->priv->has_peer_latency = FALSE;
2455 GST_PAD_TEMPLATE_GTYPE (pad_template) ==
2456 G_TYPE_NONE ? GST_TYPE_AGGREGATOR_PAD :
2457 GST_PAD_TEMPLATE_GTYPE (pad_template);
2458 g_assert (g_type_is_a (pad_type, GST_TYPE_AGGREGATOR_PAD));
2460 g_object_new (pad_type, "name", "src", "direction", GST_PAD_SRC,
2461 "template", pad_template, NULL);
2463 gst_aggregator_reset_flow_values (self);
2465 gst_pad_set_event_function (self->srcpad,
2466 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2467 gst_pad_set_query_function (self->srcpad,
2468 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2469 gst_pad_set_activatemode_function (self->srcpad,
2470 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2472 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2474 self->priv->upstream_latency_min = DEFAULT_MIN_UPSTREAM_LATENCY;
2475 self->priv->latency = DEFAULT_LATENCY;
2476 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2477 self->priv->start_time = DEFAULT_START_TIME;
2479 g_mutex_init (&self->priv->src_lock);
2480 g_cond_init (&self->priv->src_cond);
2483 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2484 * method to get to the padtemplates */
2486 gst_aggregator_get_type (void)
2488 static volatile gsize type = 0;
2490 if (g_once_init_enter (&type)) {
2492 static const GTypeInfo info = {
2493 sizeof (GstAggregatorClass),
2496 (GClassInitFunc) gst_aggregator_class_init,
2499 sizeof (GstAggregator),
2501 (GInstanceInitFunc) gst_aggregator_init,
2504 _type = g_type_register_static (GST_TYPE_ELEMENT,
2505 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2507 aggregator_private_offset =
2508 g_type_add_instance_private (_type, sizeof (GstAggregatorPrivate));
2510 g_once_init_leave (&type, _type);
2515 /* Must be called with SRC lock and PAD lock held */
2517 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2519 /* Empty queue always has space */
2520 if (aggpad->priv->num_buffers == 0 && aggpad->priv->clipped_buffer == NULL)
2523 /* We also want at least two buffers, one is being processed and one is ready
2524 * for the next iteration when we operate in live mode. */
2525 if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2528 /* zero latency, if there is a buffer, it's full */
2529 if (self->priv->latency == 0)
2532 /* Allow no more buffers than the latency */
2533 return (aggpad->priv->time_level <= self->priv->latency);
2536 /* Must be called with the PAD_LOCK held */
2538 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2540 GstClockTime timestamp;
2542 if (GST_BUFFER_DTS_IS_VALID (buffer))
2543 timestamp = GST_BUFFER_DTS (buffer);
2545 timestamp = GST_BUFFER_PTS (buffer);
2547 if (timestamp == GST_CLOCK_TIME_NONE) {
2549 timestamp = aggpad->priv->head_position;
2551 timestamp = aggpad->priv->tail_position;
2555 if (GST_BUFFER_DURATION_IS_VALID (buffer))
2556 timestamp += GST_BUFFER_DURATION (buffer);
2559 aggpad->priv->head_position = timestamp;
2561 aggpad->priv->tail_position = timestamp;
2563 update_time_level (aggpad, head);
2567 * Can be called either from the sinkpad's chain function or from the srcpad's
2568 * thread in the case of a buffer synthetized from a GAP event.
2569 * Because of this second case, FLUSH_LOCK can't be used here.
2572 static GstFlowReturn
2573 gst_aggregator_pad_chain_internal (GstAggregator * self,
2574 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2576 GstFlowReturn flow_return;
2577 GstClockTime buf_pts;
2580 flow_return = aggpad->priv->flow_return;
2581 if (flow_return != GST_FLOW_OK)
2584 PAD_UNLOCK (aggpad);
2586 buf_pts = GST_BUFFER_PTS (buffer);
2590 GST_OBJECT_LOCK (self);
2593 if (aggpad->priv->first_buffer) {
2594 self->priv->has_peer_latency = FALSE;
2595 aggpad->priv->first_buffer = FALSE;
2598 if ((gst_aggregator_pad_has_space (self, aggpad) || !head)
2599 && aggpad->priv->flow_return == GST_FLOW_OK) {
2601 g_queue_push_head (&aggpad->priv->data, buffer);
2603 g_queue_push_tail (&aggpad->priv->data, buffer);
2604 apply_buffer (aggpad, buffer, head);
2605 aggpad->priv->num_buffers++;
2607 SRC_BROADCAST (self);
2611 flow_return = aggpad->priv->flow_return;
2612 if (flow_return != GST_FLOW_OK) {
2613 GST_OBJECT_UNLOCK (self);
2617 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2618 GST_OBJECT_UNLOCK (self);
2620 PAD_WAIT_EVENT (aggpad);
2622 PAD_UNLOCK (aggpad);
2625 if (self->priv->first_buffer) {
2626 GstClockTime start_time;
2627 GstAggregatorPad *srcpad = GST_AGGREGATOR_PAD (self->srcpad);
2629 switch (self->priv->start_time_selection) {
2630 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2634 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2635 GST_OBJECT_LOCK (aggpad);
2636 if (aggpad->priv->head_segment.format == GST_FORMAT_TIME) {
2637 start_time = buf_pts;
2638 if (start_time != -1) {
2639 start_time = MAX (start_time, aggpad->priv->head_segment.start);
2641 gst_segment_to_running_time (&aggpad->priv->head_segment,
2642 GST_FORMAT_TIME, start_time);
2646 GST_WARNING_OBJECT (aggpad,
2647 "Ignoring request of selecting the first start time "
2648 "as the segment is a %s segment instead of a time segment",
2649 gst_format_get_name (aggpad->segment.format));
2651 GST_OBJECT_UNLOCK (aggpad);
2653 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2654 start_time = self->priv->start_time;
2655 if (start_time == -1)
2660 if (start_time != -1) {
2661 if (srcpad->segment.position == -1)
2662 srcpad->segment.position = start_time;
2664 srcpad->segment.position = MIN (start_time, srcpad->segment.position);
2666 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2667 GST_TIME_ARGS (start_time));
2671 PAD_UNLOCK (aggpad);
2672 GST_OBJECT_UNLOCK (self);
2675 GST_DEBUG_OBJECT (aggpad, "Done chaining");
2680 PAD_UNLOCK (aggpad);
2682 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2683 gst_flow_get_name (flow_return));
2685 gst_buffer_unref (buffer);
2690 static GstFlowReturn
2691 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2694 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2696 PAD_FLUSH_LOCK (aggpad);
2698 ret = gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2699 aggpad, buffer, TRUE);
2701 PAD_FLUSH_UNLOCK (aggpad);
2707 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2710 GstAggregator *self = GST_AGGREGATOR (parent);
2711 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2713 if (GST_QUERY_IS_SERIALIZED (query)) {
2715 gboolean ret = FALSE;
2720 if (aggpad->priv->flow_return != GST_FLOW_OK) {
2725 g_queue_push_head (&aggpad->priv->data, query);
2726 SRC_BROADCAST (self);
2729 while (!gst_aggregator_pad_queue_is_empty (aggpad)
2730 && aggpad->priv->flow_return == GST_FLOW_OK) {
2731 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2732 PAD_WAIT_EVENT (aggpad);
2735 s = gst_query_writable_structure (query);
2736 if (gst_structure_get_boolean (s, "gst-aggregator-retval", &ret))
2737 gst_structure_remove_field (s, "gst-aggregator-retval");
2739 g_queue_remove (&aggpad->priv->data, query);
2741 if (aggpad->priv->flow_return != GST_FLOW_OK)
2744 PAD_UNLOCK (aggpad);
2748 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2750 return klass->sink_query (self, aggpad, query);
2754 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2755 gst_flow_get_name (aggpad->priv->flow_return));
2756 PAD_UNLOCK (aggpad);
2761 /* Queue serialized events and let the others go through directly.
2762 * The queued events with be handled from the src-pad task in
2763 * gst_aggregator_do_events_and_queries().
2765 static GstFlowReturn
2766 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2769 GstFlowReturn ret = GST_FLOW_OK;
2770 GstAggregator *self = GST_AGGREGATOR (parent);
2771 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2773 if (GST_EVENT_IS_SERIALIZED (event)
2774 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2778 if (aggpad->priv->flow_return != GST_FLOW_OK)
2781 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2782 GST_OBJECT_LOCK (aggpad);
2783 gst_event_copy_segment (event, &aggpad->priv->head_segment);
2784 aggpad->priv->head_position = aggpad->priv->head_segment.position;
2785 update_time_level (aggpad, TRUE);
2786 GST_OBJECT_UNLOCK (aggpad);
2789 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT, event);
2790 g_queue_push_head (&aggpad->priv->data, event);
2791 SRC_BROADCAST (self);
2792 PAD_UNLOCK (aggpad);
2795 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2797 if (!klass->sink_event (self, aggpad, event)) {
2798 /* Copied from GstPad to convert boolean to a GstFlowReturn in
2799 * the event handling func */
2800 ret = GST_FLOW_ERROR;
2807 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2808 gst_flow_get_name (aggpad->priv->flow_return));
2809 PAD_UNLOCK (aggpad);
2811 if (GST_EVENT_IS_STICKY (event))
2812 gst_pad_store_sticky_event (pad, event);
2813 gst_event_unref (event);
2815 return aggpad->priv->flow_return;
2819 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2820 GstObject * parent, GstPadMode mode, gboolean active)
2822 GstAggregator *self = GST_AGGREGATOR (parent);
2823 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2825 if (active == FALSE) {
2827 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2828 SRC_BROADCAST (self);
2832 aggpad->priv->flow_return = GST_FLOW_OK;
2833 PAD_BROADCAST_EVENT (aggpad);
2834 PAD_UNLOCK (aggpad);
2840 /***********************************
2841 * GstAggregatorPad implementation *
2842 ************************************/
2843 G_DEFINE_TYPE_WITH_PRIVATE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2845 #define DEFAULT_PAD_EMIT_SIGNALS FALSE
2850 PAD_PROP_EMIT_SIGNALS,
2855 PAD_SIGNAL_BUFFER_CONSUMED,
2859 static guint gst_aggregator_pad_signals[PAD_LAST_SIGNAL] = { 0 };
2862 gst_aggregator_pad_constructed (GObject * object)
2864 GstPad *pad = GST_PAD (object);
2866 if (GST_PAD_IS_SINK (pad)) {
2867 gst_pad_set_chain_function (pad,
2868 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2869 gst_pad_set_event_full_function_full (pad,
2870 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
2871 gst_pad_set_query_function (pad,
2872 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2873 gst_pad_set_activatemode_function (pad,
2874 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2879 gst_aggregator_pad_finalize (GObject * object)
2881 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2883 g_cond_clear (&pad->priv->event_cond);
2884 g_mutex_clear (&pad->priv->flush_lock);
2885 g_mutex_clear (&pad->priv->lock);
2887 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2891 gst_aggregator_pad_dispose (GObject * object)
2893 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2895 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2897 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2901 gst_aggregator_pad_set_property (GObject * object, guint prop_id,
2902 const GValue * value, GParamSpec * pspec)
2904 GstAggregatorPad *pad = GST_AGGREGATOR_PAD (object);
2907 case PAD_PROP_EMIT_SIGNALS:
2908 pad->priv->emit_signals = g_value_get_boolean (value);
2911 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2917 gst_aggregator_pad_get_property (GObject * object, guint prop_id,
2918 GValue * value, GParamSpec * pspec)
2920 GstAggregatorPad *pad = GST_AGGREGATOR_PAD (object);
2923 case PAD_PROP_EMIT_SIGNALS:
2924 g_value_set_boolean (value, pad->priv->emit_signals);
2927 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2933 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2935 GObjectClass *gobject_class = (GObjectClass *) klass;
2937 gobject_class->constructed = gst_aggregator_pad_constructed;
2938 gobject_class->finalize = gst_aggregator_pad_finalize;
2939 gobject_class->dispose = gst_aggregator_pad_dispose;
2940 gobject_class->set_property = gst_aggregator_pad_set_property;
2941 gobject_class->get_property = gst_aggregator_pad_get_property;
2944 * GstAggregatorPad:buffer-consumed:
2946 * Signals that a buffer was consumed. As aggregator pads store buffers
2947 * in an internal queue, there is no direct match between input and output
2948 * buffers at any given time. This signal can be useful to forward metas
2949 * such as #GstVideoTimeCodeMeta or #GstVideoCaptionMeta at the right time.
2953 gst_aggregator_pad_signals[PAD_SIGNAL_BUFFER_CONSUMED] =
2954 g_signal_new ("buffer-consumed", G_TYPE_FROM_CLASS (klass),
2955 G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_generic,
2956 G_TYPE_NONE, 1, GST_TYPE_BUFFER);
2959 * GstAggregatorPad:emit-signals:
2961 * Enables the emission of signals such as #GstAggregatorPad::buffer-consumed
2965 g_object_class_install_property (gobject_class, PAD_PROP_EMIT_SIGNALS,
2966 g_param_spec_boolean ("emit-signals", "Emit signals",
2967 "Send signals to signal data consumption", DEFAULT_PAD_EMIT_SIGNALS,
2968 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2972 gst_aggregator_pad_init (GstAggregatorPad * pad)
2974 pad->priv = gst_aggregator_pad_get_instance_private (pad);
2976 g_queue_init (&pad->priv->data);
2977 g_cond_init (&pad->priv->event_cond);
2979 g_mutex_init (&pad->priv->flush_lock);
2980 g_mutex_init (&pad->priv->lock);
2982 gst_aggregator_pad_reset_unlocked (pad);
2983 pad->priv->negotiated = FALSE;
2984 pad->priv->emit_signals = DEFAULT_PAD_EMIT_SIGNALS;
2987 /* Must be called with the PAD_LOCK held */
2989 gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad, GstBuffer * buffer)
2991 pad->priv->num_buffers--;
2992 GST_TRACE_OBJECT (pad, "Consuming buffer %" GST_PTR_FORMAT, buffer);
2993 if (buffer && pad->priv->emit_signals) {
2994 g_signal_emit (pad, gst_aggregator_pad_signals[PAD_SIGNAL_BUFFER_CONSUMED],
2997 PAD_BROADCAST_EVENT (pad);
3000 /* Must be called with the PAD_LOCK held */
3002 gst_aggregator_pad_clip_buffer_unlocked (GstAggregatorPad * pad)
3004 GstAggregator *self = NULL;
3005 GstAggregatorClass *aggclass = NULL;
3006 GstBuffer *buffer = NULL;
3008 while (pad->priv->clipped_buffer == NULL &&
3009 GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->data))) {
3010 buffer = g_queue_pop_tail (&pad->priv->data);
3012 apply_buffer (pad, buffer, FALSE);
3014 /* We only take the parent here so that it's not taken if the buffer is
3015 * already clipped or if the queue is empty.
3018 self = GST_AGGREGATOR (gst_pad_get_parent_element (GST_PAD (pad)));
3020 gst_buffer_unref (buffer);
3024 aggclass = GST_AGGREGATOR_GET_CLASS (self);
3027 if (aggclass->clip) {
3028 GST_TRACE_OBJECT (pad, "Clipping: %" GST_PTR_FORMAT, buffer);
3030 buffer = aggclass->clip (self, pad, buffer);
3032 if (buffer == NULL) {
3033 gst_aggregator_pad_buffer_consumed (pad, buffer);
3034 GST_TRACE_OBJECT (pad, "Clipping consumed the buffer");
3038 pad->priv->clipped_buffer = buffer;
3042 gst_object_unref (self);
3046 * gst_aggregator_pad_pop_buffer:
3047 * @pad: the pad to get buffer from
3049 * Steal the ref to the buffer currently queued in @pad.
3051 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
3052 * queued. You should unref the buffer after usage.
3055 gst_aggregator_pad_pop_buffer (GstAggregatorPad * pad)
3061 if (pad->priv->flow_return != GST_FLOW_OK) {
3066 gst_aggregator_pad_clip_buffer_unlocked (pad);
3068 buffer = pad->priv->clipped_buffer;
3071 pad->priv->clipped_buffer = NULL;
3072 gst_aggregator_pad_buffer_consumed (pad, buffer);
3073 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
3082 * gst_aggregator_pad_drop_buffer:
3083 * @pad: the pad where to drop any pending buffer
3085 * Drop the buffer currently queued in @pad.
3087 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
3090 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
3094 buf = gst_aggregator_pad_pop_buffer (pad);
3099 gst_buffer_unref (buf);
3104 * gst_aggregator_pad_peek_buffer:
3105 * @pad: the pad to get buffer from
3107 * Returns: (transfer full): A reference to the buffer in @pad or
3108 * NULL if no buffer was queued. You should unref the buffer after
3112 gst_aggregator_pad_peek_buffer (GstAggregatorPad * pad)
3118 if (pad->priv->flow_return != GST_FLOW_OK) {
3123 gst_aggregator_pad_clip_buffer_unlocked (pad);
3125 if (pad->priv->clipped_buffer) {
3126 buffer = gst_buffer_ref (pad->priv->clipped_buffer);
3136 * gst_aggregator_pad_has_buffer:
3137 * @pad: the pad to check the buffer on
3139 * This checks if a pad has a buffer available that will be returned by
3140 * a call to gst_aggregator_pad_peek_buffer() or
3141 * gst_aggregator_pad_pop_buffer().
3143 * Returns: %TRUE if the pad has a buffer available as the next thing.
3148 gst_aggregator_pad_has_buffer (GstAggregatorPad * pad)
3150 gboolean has_buffer;
3153 gst_aggregator_pad_clip_buffer_unlocked (pad);
3154 has_buffer = (pad->priv->clipped_buffer != NULL);
3161 * gst_aggregator_pad_is_eos:
3162 * @pad: an aggregator pad
3164 * Returns: %TRUE if the pad is EOS, otherwise %FALSE.
3167 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
3172 is_eos = pad->priv->eos;
3180 * gst_aggregator_merge_tags:
3181 * @self: a #GstAggregator
3182 * @tags: a #GstTagList to merge
3183 * @mode: the #GstTagMergeMode to use
3185 * Adds tags to so-called pending tags, which will be processed
3186 * before pushing out data downstream.
3188 * Note that this is provided for convenience, and the subclass is
3189 * not required to use this and can still do tag handling on its own.
3194 gst_aggregator_merge_tags (GstAggregator * self,
3195 const GstTagList * tags, GstTagMergeMode mode)
3199 g_return_if_fail (GST_IS_AGGREGATOR (self));
3200 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
3202 /* FIXME Check if we can use OBJECT lock here! */
3203 GST_OBJECT_LOCK (self);
3205 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
3206 otags = self->priv->tags;
3207 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
3209 gst_tag_list_unref (otags);
3210 self->priv->tags_changed = TRUE;
3211 GST_OBJECT_UNLOCK (self);
3216 * gst_aggregator_set_latency:
3217 * @self: a #GstAggregator
3218 * @min_latency: minimum latency
3219 * @max_latency: maximum latency
3221 * Lets #GstAggregator sub-classes tell the baseclass what their internal
3222 * latency is. Will also post a LATENCY message on the bus so the pipeline
3223 * can reconfigure its global latency.
3226 gst_aggregator_set_latency (GstAggregator * self,
3227 GstClockTime min_latency, GstClockTime max_latency)
3229 gboolean changed = FALSE;
3231 g_return_if_fail (GST_IS_AGGREGATOR (self));
3232 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
3233 g_return_if_fail (max_latency >= min_latency);
3236 if (self->priv->sub_latency_min != min_latency) {
3237 self->priv->sub_latency_min = min_latency;
3240 if (self->priv->sub_latency_max != max_latency) {
3241 self->priv->sub_latency_max = max_latency;
3246 SRC_BROADCAST (self);
3250 gst_element_post_message (GST_ELEMENT_CAST (self),
3251 gst_message_new_latency (GST_OBJECT_CAST (self)));
3256 * gst_aggregator_get_buffer_pool:
3257 * @self: a #GstAggregator
3259 * Returns: (transfer full): the instance of the #GstBufferPool used
3260 * by @trans; free it after use it
3263 gst_aggregator_get_buffer_pool (GstAggregator * self)
3265 GstBufferPool *pool;
3267 g_return_val_if_fail (GST_IS_AGGREGATOR (self), NULL);
3269 GST_OBJECT_LOCK (self);
3270 pool = self->priv->pool;
3272 gst_object_ref (pool);
3273 GST_OBJECT_UNLOCK (self);
3279 * gst_aggregator_get_allocator:
3280 * @self: a #GstAggregator
3281 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3283 * @params: (out) (allow-none) (transfer full): the
3284 * #GstAllocationParams of @allocator
3286 * Lets #GstAggregator sub-classes get the memory @allocator
3287 * acquired by the base class and its @params.
3289 * Unref the @allocator after use it.
3292 gst_aggregator_get_allocator (GstAggregator * self,
3293 GstAllocator ** allocator, GstAllocationParams * params)
3295 g_return_if_fail (GST_IS_AGGREGATOR (self));
3298 *allocator = self->priv->allocator ?
3299 gst_object_ref (self->priv->allocator) : NULL;
3302 *params = self->priv->allocation_params;
3306 * gst_aggregator_simple_get_next_time:
3307 * @self: A #GstAggregator
3309 * This is a simple #GstAggregator::get_next_time implementation that
3310 * just looks at the #GstSegment on the srcpad of the aggregator and bases
3311 * the next time on the running time there.
3313 * This is the desired behaviour in most cases where you have a live source
3314 * and you have a dead line based aggregator subclass.
3316 * Returns: The running time based on the position
3321 gst_aggregator_simple_get_next_time (GstAggregator * self)
3323 GstClockTime next_time;
3324 GstAggregatorPad *srcpad = GST_AGGREGATOR_PAD (self->srcpad);
3325 GstSegment *segment = &srcpad->segment;
3327 GST_OBJECT_LOCK (self);
3328 if (segment->position == -1 || segment->position < segment->start)
3329 next_time = segment->start;
3331 next_time = segment->position;
3333 if (segment->stop != -1 && next_time > segment->stop)
3334 next_time = segment->stop;
3336 next_time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, next_time);
3337 GST_OBJECT_UNLOCK (self);