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 * @short_description: manages a set of pads with the purpose of
25 * aggregating their buffers.
26 * @see_also: gstcollectpads for historical reasons.
28 * Manages a set of pads with the purpose of aggregating their buffers.
29 * 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.
36 * When data is queued on all pads, tha aggregate vmethod is called.
39 * One can peek at the data on any given GstAggregatorPad with the
40 * gst_aggregator_pad_get_buffer () method, and take ownership of it
41 * with the gst_aggregator_pad_steal_buffer () method. When a buffer
42 * has been taken with steal_buffer (), a new buffer can be queued
46 * If the subclass wishes to push a buffer downstream in its aggregate
47 * implementation, it should do so through the
48 * gst_aggregator_finish_buffer () method. This method will take care
49 * of sending and ordering mandatory events such as stream start, caps
53 * Same goes for EOS events, which should not be pushed directly by the
54 * subclass, it should instead return GST_FLOW_EOS in its aggregate
58 * Note that the aggregator logic regarding gap event handling is to turn
59 * these into gap buffers with matching PTS and duration. It will also
60 * flag these buffers with GST_BUFFER_FLAG_GAP and GST_BUFFER_FLAG_DROPPABLE
61 * to ease their identification and subsequent processing.
70 #include <string.h> /* strlen */
72 #include "gstaggregator.h"
76 GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
77 GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
78 GST_AGGREGATOR_START_TIME_SELECTION_SET
79 } GstAggregatorStartTimeSelection;
82 gst_aggregator_start_time_selection_get_type (void)
84 static GType gtype = 0;
87 static const GEnumValue values[] = {
88 {GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
89 "Start at 0 running time (default)", "zero"},
90 {GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
91 "Start at first observed input running time", "first"},
92 {GST_AGGREGATOR_START_TIME_SELECTION_SET,
93 "Set start time with start-time property", "set"},
97 gtype = g_enum_register_static ("GstAggregatorStartTimeSelection", values);
102 /* Might become API */
103 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
104 const GstTagList * tags, GstTagMergeMode mode);
105 static void gst_aggregator_set_latency_property (GstAggregator * agg,
107 static gint64 gst_aggregator_get_latency_property (GstAggregator * agg);
110 /* Locking order, locks in this element must always be taken in this order
112 * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
113 * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
114 * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
115 * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
116 * standard element object lock -> GST_OBJECT_LOCK(agg)
117 * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
118 * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
119 * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
123 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
125 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
126 #define GST_CAT_DEFAULT aggregator_debug
128 /* GstAggregatorPad definitions */
129 #define PAD_LOCK(pad) G_STMT_START { \
130 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
132 g_mutex_lock(&pad->priv->lock); \
133 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
137 #define PAD_UNLOCK(pad) G_STMT_START { \
138 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
140 g_mutex_unlock(&pad->priv->lock); \
141 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
146 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
147 GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p", \
149 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
150 (&((GstAggregatorPad*)pad)->priv->lock)); \
151 GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
155 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
156 GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p", \
158 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
162 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
163 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
165 g_mutex_lock(&pad->priv->flush_lock); \
166 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
170 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
171 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
173 g_mutex_unlock(&pad->priv->flush_lock); \
174 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
178 #define SRC_LOCK(self) G_STMT_START { \
179 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
181 g_mutex_lock(&self->priv->src_lock); \
182 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
186 #define SRC_UNLOCK(self) G_STMT_START { \
187 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
189 g_mutex_unlock(&self->priv->src_lock); \
190 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
194 #define SRC_WAIT(self) G_STMT_START { \
195 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
197 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
198 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
202 #define SRC_BROADCAST(self) G_STMT_START { \
203 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
205 if (self->priv->aggregate_id) \
206 gst_clock_id_unschedule (self->priv->aggregate_id); \
207 g_cond_broadcast(&(self->priv->src_cond)); \
210 struct _GstAggregatorPadPrivate
212 /* Following fields are protected by the PAD_LOCK */
213 GstFlowReturn flow_return;
214 gboolean pending_flush_start;
215 gboolean pending_flush_stop;
216 gboolean pending_eos;
218 gboolean first_buffer;
222 GstClockTime head_position;
223 GstClockTime tail_position;
224 GstClockTime head_time;
225 GstClockTime tail_time;
226 GstClockTime time_level;
232 /* This lock prevents a flush start processing happening while
233 * the chain function is also happening.
239 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
241 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
244 aggpad->priv->pending_eos = FALSE;
245 aggpad->priv->eos = FALSE;
246 aggpad->priv->flow_return = GST_FLOW_OK;
247 GST_OBJECT_LOCK (aggpad);
248 gst_segment_init (&aggpad->segment, GST_FORMAT_UNDEFINED);
249 gst_segment_init (&aggpad->clip_segment, GST_FORMAT_UNDEFINED);
250 GST_OBJECT_UNLOCK (aggpad);
251 aggpad->priv->head_position = GST_CLOCK_TIME_NONE;
252 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
253 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
254 aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
255 aggpad->priv->time_level = 0;
259 return klass->flush (aggpad, agg);
264 /*************************************
265 * GstAggregator implementation *
266 *************************************/
267 static GstElementClass *aggregator_parent_class = NULL;
269 /* All members are protected by the object lock unless otherwise noted */
271 struct _GstAggregatorPrivate
275 /* Our state is >= PAUSED */
276 gboolean running; /* protected by src_lock */
279 gboolean send_stream_start; /* protected by srcpad stream lock */
280 gboolean send_segment;
281 gboolean flush_seeking;
282 gboolean pending_flush_start;
283 gboolean send_eos; /* protected by srcpad stream lock */
285 GstCaps *srccaps; /* protected by the srcpad stream lock */
288 gboolean tags_changed;
290 gboolean peer_latency_live; /* protected by src_lock */
291 GstClockTime peer_latency_min; /* protected by src_lock */
292 GstClockTime peer_latency_max; /* protected by src_lock */
293 gboolean has_peer_latency;
295 GstClockTime sub_latency_min; /* protected by src_lock */
296 GstClockTime sub_latency_max; /* protected by src_lock */
299 GstClockID aggregate_id; /* protected by src_lock */
303 gboolean first_buffer;
304 GstAggregatorStartTimeSelection start_time_selection;
305 GstClockTime start_time;
308 gint64 latency; /* protected by both src_lock and all pad locks */
316 gboolean only_to_active_pads;
318 gboolean one_actually_seeked;
321 #define DEFAULT_LATENCY 0
322 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
323 #define DEFAULT_START_TIME (-1)
329 PROP_START_TIME_SELECTION,
334 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
335 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
338 * gst_aggregator_iterate_sinkpads:
339 * @self: The #GstAggregator
340 * @func: (scope call): The function to call.
341 * @user_data: (closure): The data to pass to @func.
343 * Iterate the sinkpads of aggregator to call a function on them.
345 * This method guarantees that @func will be called only once for each
349 gst_aggregator_iterate_sinkpads (GstAggregator * self,
350 GstAggregatorPadForeachFunc func, gpointer user_data)
352 gboolean result = FALSE;
354 gboolean done = FALSE;
355 GValue item = { 0, };
356 GList *seen_pads = NULL;
358 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
364 switch (gst_iterator_next (iter, &item)) {
365 case GST_ITERATOR_OK:
367 GstAggregatorPad *pad;
369 pad = g_value_get_object (&item);
371 /* if already pushed, skip. FIXME, find something faster to tag pads */
372 if (pad == NULL || g_list_find (seen_pads, pad)) {
373 g_value_reset (&item);
377 GST_LOG_OBJECT (pad, "calling function %s on pad",
378 GST_DEBUG_FUNCPTR_NAME (func));
380 result = func (self, pad, user_data);
384 seen_pads = g_list_prepend (seen_pads, pad);
386 g_value_reset (&item);
389 case GST_ITERATOR_RESYNC:
390 gst_iterator_resync (iter);
392 case GST_ITERATOR_ERROR:
393 GST_ERROR_OBJECT (self,
394 "Could not iterate over internally linked pads");
397 case GST_ITERATOR_DONE:
402 g_value_unset (&item);
403 gst_iterator_free (iter);
405 if (seen_pads == NULL) {
406 GST_DEBUG_OBJECT (self, "No pad seen");
410 g_list_free (seen_pads);
417 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
419 return (g_queue_peek_tail (&pad->priv->buffers) == NULL);
423 gst_aggregator_check_pads_ready (GstAggregator * self)
425 GstAggregatorPad *pad;
428 GST_LOG_OBJECT (self, "checking pads");
430 GST_OBJECT_LOCK (self);
432 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
433 if (sinkpads == NULL)
436 for (l = sinkpads; l != NULL; l = l->next) {
441 /* In live mode, having a single pad with buffers is enough to
442 * generate a start time from it. In non-live mode all pads need
445 if (self->priv->peer_latency_live &&
446 !gst_aggregator_pad_queue_is_empty (pad))
447 self->priv->first_buffer = FALSE;
449 if (gst_aggregator_pad_queue_is_empty (pad) && !pad->priv->eos) {
457 self->priv->first_buffer = FALSE;
459 GST_OBJECT_UNLOCK (self);
460 GST_LOG_OBJECT (self, "pads are ready");
465 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
466 GST_OBJECT_UNLOCK (self);
471 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
472 GST_OBJECT_UNLOCK (self);
478 gst_aggregator_reset_flow_values (GstAggregator * self)
480 GST_OBJECT_LOCK (self);
481 self->priv->send_stream_start = TRUE;
482 self->priv->send_segment = TRUE;
483 gst_segment_init (&self->segment, GST_FORMAT_TIME);
484 self->priv->first_buffer = TRUE;
485 GST_OBJECT_UNLOCK (self);
489 gst_aggregator_push_mandatory_events (GstAggregator * self)
491 GstAggregatorPrivate *priv = self->priv;
492 GstEvent *segment = NULL;
493 GstEvent *tags = NULL;
495 if (self->priv->send_stream_start) {
498 GST_INFO_OBJECT (self, "pushing stream start");
499 /* stream-start (FIXME: create id based on input ids) */
500 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
501 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
502 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
504 self->priv->send_stream_start = FALSE;
507 if (self->priv->srccaps) {
509 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
510 self->priv->srccaps);
511 if (!gst_pad_push_event (self->srcpad,
512 gst_event_new_caps (self->priv->srccaps))) {
513 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
515 gst_caps_unref (self->priv->srccaps);
516 self->priv->srccaps = NULL;
519 GST_OBJECT_LOCK (self);
520 if (self->priv->send_segment && !self->priv->flush_seeking) {
521 segment = gst_event_new_segment (&self->segment);
523 if (!self->priv->seqnum)
524 self->priv->seqnum = gst_event_get_seqnum (segment);
526 gst_event_set_seqnum (segment, self->priv->seqnum);
527 self->priv->send_segment = FALSE;
529 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
532 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
533 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
534 priv->tags_changed = FALSE;
536 GST_OBJECT_UNLOCK (self);
539 gst_pad_push_event (self->srcpad, segment);
541 gst_pad_push_event (self->srcpad, tags);
546 * gst_aggregator_set_src_caps:
547 * @self: The #GstAggregator
548 * @caps: The #GstCaps to set on the src pad.
550 * Sets the caps to be used on the src pad.
553 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
555 GST_PAD_STREAM_LOCK (self->srcpad);
556 gst_caps_replace (&self->priv->srccaps, caps);
557 gst_aggregator_push_mandatory_events (self);
558 GST_PAD_STREAM_UNLOCK (self->srcpad);
562 * gst_aggregator_finish_buffer:
563 * @self: The #GstAggregator
564 * @buffer: (transfer full): the #GstBuffer to push.
566 * This method will push the provided output buffer downstream. If needed,
567 * mandatory events such as stream-start, caps, and segment events will be
568 * sent before pushing the buffer.
571 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
573 gst_aggregator_push_mandatory_events (self);
575 GST_OBJECT_LOCK (self);
576 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
577 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
578 GST_OBJECT_UNLOCK (self);
579 return gst_pad_push (self->srcpad, buffer);
581 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
582 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
583 GST_OBJECT_UNLOCK (self);
584 gst_buffer_unref (buffer);
590 gst_aggregator_push_eos (GstAggregator * self)
593 gst_aggregator_push_mandatory_events (self);
595 event = gst_event_new_eos ();
597 GST_OBJECT_LOCK (self);
598 self->priv->send_eos = FALSE;
599 gst_event_set_seqnum (event, self->priv->seqnum);
600 GST_OBJECT_UNLOCK (self);
602 gst_pad_push_event (self->srcpad, event);
606 gst_aggregator_get_next_time (GstAggregator * self)
608 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
610 if (klass->get_next_time)
611 return klass->get_next_time (self);
613 return GST_CLOCK_TIME_NONE;
617 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
619 GstClockTime latency;
627 latency = gst_aggregator_get_latency_unlocked (self);
629 if (gst_aggregator_check_pads_ready (self)) {
630 GST_DEBUG_OBJECT (self, "all pads have data");
636 /* Before waiting, check if we're actually still running */
637 if (!self->priv->running || !self->priv->send_eos) {
643 start = gst_aggregator_get_next_time (self);
645 /* If we're not live, or if we use the running time
646 * of the first buffer as start time, we wait until
647 * all pads have buffers.
648 * Otherwise (i.e. if we are live!), we wait on the clock
649 * and if a pad does not have a buffer in time we ignore
652 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
653 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
654 !GST_CLOCK_TIME_IS_VALID (start) ||
655 (self->priv->first_buffer
656 && self->priv->start_time_selection ==
657 GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
658 /* We wake up here when something happened, and below
659 * then check if we're ready now. If we return FALSE,
660 * we will be directly called again.
664 GstClockTime base_time, time;
666 GstClockReturn status;
667 GstClockTimeDiff jitter;
669 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
670 GST_TIME_ARGS (start));
672 GST_OBJECT_LOCK (self);
673 base_time = GST_ELEMENT_CAST (self)->base_time;
674 clock = GST_ELEMENT_CLOCK (self);
676 gst_object_ref (clock);
677 GST_OBJECT_UNLOCK (self);
679 time = base_time + start;
682 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
683 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
684 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
685 GST_TIME_ARGS (time),
686 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
687 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
688 GST_TIME_ARGS (gst_clock_get_time (clock)));
690 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
691 gst_object_unref (clock);
695 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
698 if (self->priv->aggregate_id) {
699 gst_clock_id_unref (self->priv->aggregate_id);
700 self->priv->aggregate_id = NULL;
703 GST_DEBUG_OBJECT (self,
704 "clock returned %d (jitter: %s%" GST_TIME_FORMAT ")",
705 status, (jitter < 0 ? "-" : " "),
706 GST_TIME_ARGS ((jitter < 0 ? -jitter : jitter)));
709 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
716 res = gst_aggregator_check_pads_ready (self);
723 check_events (GstAggregator * self, GstAggregatorPad * pad, gpointer user_data)
725 GstEvent *event = NULL;
726 GstAggregatorClass *klass = NULL;
727 gboolean *processed_event = user_data;
733 if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
734 pad->priv->pending_eos = FALSE;
735 pad->priv->eos = TRUE;
737 if (GST_IS_EVENT (g_queue_peek_tail (&pad->priv->buffers))) {
738 event = g_queue_pop_tail (&pad->priv->buffers);
739 PAD_BROADCAST_EVENT (pad);
744 *processed_event = TRUE;
746 klass = GST_AGGREGATOR_GET_CLASS (self);
748 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
749 klass->sink_event (self, pad, event);
751 } while (event != NULL);
757 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
758 GstFlowReturn flow_return, gboolean full)
763 if (flow_return == GST_FLOW_NOT_LINKED)
764 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
766 aggpad->priv->flow_return = flow_return;
768 item = g_queue_peek_head_link (&aggpad->priv->buffers);
770 GList *next = item->next;
772 /* In partial flush, we do like the pad, we get rid of non-sticky events
775 if (full || GST_IS_BUFFER (item->data) ||
776 GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
777 GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
778 !GST_EVENT_IS_STICKY (item->data)) {
779 gst_mini_object_unref (item->data);
780 g_queue_delete_link (&aggpad->priv->buffers, item);
784 aggpad->priv->num_buffers = 0;
786 PAD_BROADCAST_EVENT (aggpad);
791 gst_aggregator_aggregate_func (GstAggregator * self)
793 GstAggregatorPrivate *priv = self->priv;
794 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
795 gboolean timeout = FALSE;
797 if (self->priv->running == FALSE) {
798 GST_DEBUG_OBJECT (self, "Not running anymore");
802 GST_LOG_OBJECT (self, "Checking aggregate");
803 while (priv->send_eos && priv->running) {
804 GstFlowReturn flow_return;
805 gboolean processed_event = FALSE;
807 gst_aggregator_iterate_sinkpads (self, check_events, NULL);
809 if (!gst_aggregator_wait_and_check (self, &timeout))
812 gst_aggregator_iterate_sinkpads (self, check_events, &processed_event);
816 GST_TRACE_OBJECT (self, "Actually aggregating!");
817 flow_return = klass->aggregate (self, timeout);
819 GST_OBJECT_LOCK (self);
820 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
821 /* We don't want to set the pads to flushing, but we want to
822 * stop the thread, so just break here */
823 GST_OBJECT_UNLOCK (self);
826 GST_OBJECT_UNLOCK (self);
828 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
829 gst_aggregator_push_eos (self);
832 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
834 if (flow_return != GST_FLOW_OK) {
837 GST_OBJECT_LOCK (self);
838 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
839 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
841 gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
843 GST_OBJECT_UNLOCK (self);
848 /* Pause the task here, the only ways to get here are:
849 * 1) We're stopping, in which case the task is stopped anyway
850 * 2) We got a flow error above, in which case it might take
851 * some time to forward the flow return upstream and we
852 * would otherwise call the task function over and over
853 * again without doing anything
855 gst_pad_pause_task (self->srcpad);
859 gst_aggregator_start (GstAggregator * self)
861 GstAggregatorClass *klass;
864 self->priv->running = TRUE;
865 self->priv->send_stream_start = TRUE;
866 self->priv->send_segment = TRUE;
867 self->priv->send_eos = TRUE;
868 self->priv->srccaps = NULL;
870 klass = GST_AGGREGATOR_GET_CLASS (self);
873 result = klass->start (self);
881 _check_pending_flush_stop (GstAggregatorPad * pad)
886 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
893 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
897 GST_INFO_OBJECT (self, "%s srcpad task",
898 flush_start ? "Pausing" : "Stopping");
901 self->priv->running = FALSE;
902 SRC_BROADCAST (self);
906 res = gst_pad_push_event (self->srcpad, flush_start);
909 gst_pad_stop_task (self->srcpad);
915 gst_aggregator_start_srcpad_task (GstAggregator * self)
917 GST_INFO_OBJECT (self, "Starting srcpad task");
919 self->priv->running = TRUE;
920 gst_pad_start_task (GST_PAD (self->srcpad),
921 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
925 gst_aggregator_flush (GstAggregator * self)
927 GstFlowReturn ret = GST_FLOW_OK;
928 GstAggregatorPrivate *priv = self->priv;
929 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
931 GST_DEBUG_OBJECT (self, "Flushing everything");
932 GST_OBJECT_LOCK (self);
933 priv->send_segment = TRUE;
934 priv->flush_seeking = FALSE;
935 priv->tags_changed = FALSE;
936 GST_OBJECT_UNLOCK (self);
938 ret = klass->flush (self);
944 /* Called with GstAggregator's object lock held */
947 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
950 GstAggregatorPad *tmppad;
952 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
953 tmppad = (GstAggregatorPad *) tmp->data;
955 if (_check_pending_flush_stop (tmppad) == FALSE) {
956 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
957 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
966 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
969 GstAggregatorPrivate *priv = self->priv;
970 GstAggregatorPadPrivate *padpriv = aggpad->priv;
972 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
974 PAD_FLUSH_LOCK (aggpad);
976 if (padpriv->pending_flush_start) {
977 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
979 padpriv->pending_flush_start = FALSE;
980 padpriv->pending_flush_stop = TRUE;
984 GST_OBJECT_LOCK (self);
985 if (priv->flush_seeking) {
986 /* If flush_seeking we forward the first FLUSH_START */
987 if (priv->pending_flush_start) {
988 priv->pending_flush_start = FALSE;
989 GST_OBJECT_UNLOCK (self);
991 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
992 gst_aggregator_stop_srcpad_task (self, event);
994 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
995 GST_PAD_STREAM_LOCK (self->srcpad);
996 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
999 GST_OBJECT_UNLOCK (self);
1000 gst_event_unref (event);
1003 GST_OBJECT_UNLOCK (self);
1004 gst_event_unref (event);
1006 PAD_FLUSH_UNLOCK (aggpad);
1009 /* Must be called with the the PAD_LOCK held */
1011 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1014 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->head_position) &&
1015 aggpad->clip_segment.format == GST_FORMAT_TIME)
1016 aggpad->priv->head_time =
1017 gst_segment_to_running_time (&aggpad->clip_segment,
1018 GST_FORMAT_TIME, aggpad->priv->head_position);
1020 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
1022 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_position) &&
1023 aggpad->segment.format == GST_FORMAT_TIME)
1024 aggpad->priv->tail_time =
1025 gst_segment_to_running_time (&aggpad->segment,
1026 GST_FORMAT_TIME, aggpad->priv->tail_position);
1028 aggpad->priv->tail_time = aggpad->priv->head_time;
1031 if (aggpad->priv->head_time == GST_CLOCK_TIME_NONE ||
1032 aggpad->priv->tail_time == GST_CLOCK_TIME_NONE) {
1033 aggpad->priv->time_level = 0;
1037 if (aggpad->priv->tail_time > aggpad->priv->head_time)
1038 aggpad->priv->time_level = 0;
1040 aggpad->priv->time_level = aggpad->priv->head_time -
1041 aggpad->priv->tail_time;
1045 /* GstAggregator vmethods default implementations */
1047 gst_aggregator_default_sink_event (GstAggregator * self,
1048 GstAggregatorPad * aggpad, GstEvent * event)
1050 gboolean res = TRUE;
1051 GstPad *pad = GST_PAD (aggpad);
1052 GstAggregatorPrivate *priv = self->priv;
1054 switch (GST_EVENT_TYPE (event)) {
1055 case GST_EVENT_FLUSH_START:
1057 gst_aggregator_flush_start (self, aggpad, event);
1058 /* We forward only in one case: right after flush_seeking */
1062 case GST_EVENT_FLUSH_STOP:
1064 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
1066 gst_aggregator_pad_flush (aggpad, self);
1067 GST_OBJECT_LOCK (self);
1068 if (priv->flush_seeking) {
1069 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1070 if (gst_aggregator_all_flush_stop_received_locked (self)) {
1071 GST_OBJECT_UNLOCK (self);
1072 /* That means we received FLUSH_STOP/FLUSH_STOP on
1073 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1074 gst_aggregator_flush (self);
1075 gst_pad_push_event (self->srcpad, event);
1078 priv->send_eos = TRUE;
1079 SRC_BROADCAST (self);
1082 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1083 GST_PAD_STREAM_UNLOCK (self->srcpad);
1084 gst_aggregator_start_srcpad_task (self);
1086 GST_OBJECT_UNLOCK (self);
1089 GST_OBJECT_UNLOCK (self);
1092 aggpad->priv->first_buffer = TRUE;
1094 /* We never forward the event */
1099 GST_DEBUG_OBJECT (aggpad, "EOS");
1101 /* We still have a buffer, and we don't want the subclass to have to
1102 * check for it. Mark pending_eos, eos will be set when steal_buffer is
1107 if (gst_aggregator_pad_queue_is_empty (aggpad)) {
1108 aggpad->priv->eos = TRUE;
1110 aggpad->priv->pending_eos = TRUE;
1112 PAD_UNLOCK (aggpad);
1114 SRC_BROADCAST (self);
1118 case GST_EVENT_SEGMENT:
1121 GST_OBJECT_LOCK (aggpad);
1122 gst_event_copy_segment (event, &aggpad->segment);
1123 update_time_level (aggpad, FALSE);
1124 GST_OBJECT_UNLOCK (aggpad);
1125 PAD_UNLOCK (aggpad);
1127 GST_OBJECT_LOCK (self);
1128 self->priv->seqnum = gst_event_get_seqnum (event);
1129 GST_OBJECT_UNLOCK (self);
1132 case GST_EVENT_STREAM_START:
1138 GstClockTime pts, endpts;
1139 GstClockTime duration;
1142 gst_event_parse_gap (event, &pts, &duration);
1143 gapbuf = gst_buffer_new ();
1145 if (GST_CLOCK_TIME_IS_VALID (duration))
1146 endpts = pts + duration;
1148 endpts = GST_CLOCK_TIME_NONE;
1150 GST_OBJECT_LOCK (aggpad);
1151 res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1153 GST_OBJECT_UNLOCK (aggpad);
1156 GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1160 if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1161 duration = endpts - pts;
1163 duration = GST_CLOCK_TIME_NONE;
1165 GST_BUFFER_PTS (gapbuf) = pts;
1166 GST_BUFFER_DURATION (gapbuf) = duration;
1167 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1168 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1170 if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1172 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1182 gst_event_parse_tag (event, &tags);
1184 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1185 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1186 gst_event_unref (event);
1198 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1199 return gst_pad_event_default (pad, GST_OBJECT (self), event);
1202 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1204 gst_event_unref (event);
1209 static inline gboolean
1210 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1211 gpointer unused_udata)
1213 gst_aggregator_pad_flush (pad, self);
1219 gst_aggregator_stop (GstAggregator * agg)
1221 GstAggregatorClass *klass;
1224 gst_aggregator_reset_flow_values (agg);
1226 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1228 klass = GST_AGGREGATOR_GET_CLASS (agg);
1231 result = klass->stop (agg);
1235 agg->priv->has_peer_latency = FALSE;
1236 agg->priv->peer_latency_live = FALSE;
1237 agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1239 if (agg->priv->tags)
1240 gst_tag_list_unref (agg->priv->tags);
1241 agg->priv->tags = NULL;
1246 /* GstElement vmethods implementations */
1247 static GstStateChangeReturn
1248 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1250 GstStateChangeReturn ret;
1251 GstAggregator *self = GST_AGGREGATOR (element);
1253 switch (transition) {
1254 case GST_STATE_CHANGE_READY_TO_PAUSED:
1255 if (!gst_aggregator_start (self))
1263 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1264 transition)) == GST_STATE_CHANGE_FAILURE)
1268 switch (transition) {
1269 case GST_STATE_CHANGE_PAUSED_TO_READY:
1270 if (!gst_aggregator_stop (self)) {
1271 /* What to do in this case? Error out? */
1272 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1284 GST_ERROR_OBJECT (element, "parent failed state change");
1289 GST_ERROR_OBJECT (element, "Subclass failed to start");
1290 return GST_STATE_CHANGE_FAILURE;
1295 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1297 GstAggregator *self = GST_AGGREGATOR (element);
1298 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1300 GST_INFO_OBJECT (pad, "Removing pad");
1303 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1304 gst_element_remove_pad (element, pad);
1306 self->priv->has_peer_latency = FALSE;
1307 SRC_BROADCAST (self);
1312 gst_aggregator_request_new_pad (GstElement * element,
1313 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1315 GstAggregator *self;
1316 GstAggregatorPad *agg_pad;
1318 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1319 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1321 self = GST_AGGREGATOR (element);
1323 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1327 GST_OBJECT_LOCK (element);
1328 if (req_name == NULL || strlen (req_name) < 6
1329 || !g_str_has_prefix (req_name, "sink_")) {
1330 /* no name given when requesting the pad, use next available int */
1333 /* parse serial number from requested padname */
1334 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1335 if (serial >= priv->padcount)
1336 priv->padcount = serial;
1339 name = g_strdup_printf ("sink_%u", priv->padcount);
1340 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1341 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1344 GST_OBJECT_UNLOCK (element);
1350 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1351 self->priv->has_peer_latency = FALSE;
1354 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1356 /* add the pad to the element */
1357 gst_element_add_pad (element, GST_PAD (agg_pad));
1359 return GST_PAD (agg_pad);
1362 /* Must be called with SRC_LOCK held */
1365 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1367 gboolean query_ret, live;
1368 GstClockTime our_latency, min, max;
1370 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1373 GST_WARNING_OBJECT (self, "Latency query failed");
1377 gst_query_parse_latency (query, &live, &min, &max);
1379 our_latency = self->priv->latency;
1381 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1382 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1383 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1387 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1388 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1389 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1390 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1391 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1395 self->priv->peer_latency_live = live;
1396 self->priv->peer_latency_min = min;
1397 self->priv->peer_latency_max = max;
1398 self->priv->has_peer_latency = TRUE;
1402 min += self->priv->sub_latency_min;
1403 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1404 && GST_CLOCK_TIME_IS_VALID (max))
1405 max += self->priv->sub_latency_max + our_latency;
1407 max = GST_CLOCK_TIME_NONE;
1409 if (live && min > max) {
1410 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1411 ("%s", "Latency too big"),
1412 ("The requested latency value is too big for the current pipeline. "
1413 "Limiting to %" G_GINT64_FORMAT, max));
1415 /* FIXME: This could in theory become negative, but in
1416 * that case all is lost anyway */
1417 self->priv->latency -= min - max;
1418 /* FIXME: shouldn't we g_object_notify() the change here? */
1421 SRC_BROADCAST (self);
1423 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1424 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1426 gst_query_set_latency (query, live, min, max);
1432 * MUST be called with the src_lock held.
1434 * See gst_aggregator_get_latency() for doc
1437 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1439 GstClockTime latency;
1441 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1443 if (!self->priv->has_peer_latency) {
1444 GstQuery *query = gst_query_new_latency ();
1447 ret = gst_aggregator_query_latency_unlocked (self, query);
1448 gst_query_unref (query);
1450 return GST_CLOCK_TIME_NONE;
1453 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1454 return GST_CLOCK_TIME_NONE;
1456 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1457 latency = self->priv->peer_latency_min;
1460 latency += self->priv->latency;
1461 latency += self->priv->sub_latency_min;
1467 * gst_aggregator_get_latency:
1468 * @self: a #GstAggregator
1470 * Retrieves the latency values reported by @self in response to the latency
1471 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1472 * will not wait for the clock.
1474 * Typically only called by subclasses.
1476 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1479 gst_aggregator_get_latency (GstAggregator * self)
1484 ret = gst_aggregator_get_latency_unlocked (self);
1491 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1493 GstAggregator *self = GST_AGGREGATOR (element);
1495 GST_STATE_LOCK (element);
1496 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1497 GST_STATE (element) < GST_STATE_PAUSED) {
1501 GstSeekType start_type, stop_type;
1504 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1505 &start, &stop_type, &stop);
1507 GST_OBJECT_LOCK (self);
1508 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1509 stop_type, stop, NULL);
1510 self->priv->seqnum = gst_event_get_seqnum (event);
1511 self->priv->first_buffer = FALSE;
1512 GST_OBJECT_UNLOCK (self);
1514 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1516 GST_STATE_UNLOCK (element);
1519 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1524 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1526 gboolean res = TRUE;
1528 switch (GST_QUERY_TYPE (query)) {
1529 case GST_QUERY_SEEKING:
1533 /* don't pass it along as some (file)sink might claim it does
1534 * whereas with a collectpads in between that will not likely work */
1535 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1536 gst_query_set_seeking (query, format, FALSE, 0, -1);
1541 case GST_QUERY_LATENCY:
1543 res = gst_aggregator_query_latency_unlocked (self, query);
1547 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1554 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1556 EventData *evdata = user_data;
1557 gboolean ret = TRUE;
1558 GstPad *peer = gst_pad_get_peer (pad);
1559 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1562 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1563 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1566 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1567 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1568 gst_object_unref (peer);
1573 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1574 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1576 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1577 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1579 if (gst_pad_query (peer, seeking)) {
1582 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1584 if (seekable == FALSE) {
1585 GST_INFO_OBJECT (pad,
1586 "Source not seekable, We failed but it does not matter!");
1591 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1594 gst_query_unref (seeking);
1597 if (evdata->flush) {
1599 aggpad->priv->pending_flush_start = FALSE;
1600 aggpad->priv->pending_flush_stop = FALSE;
1601 PAD_UNLOCK (aggpad);
1604 evdata->one_actually_seeked = TRUE;
1607 evdata->result &= ret;
1609 /* Always send to all pads */
1614 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1615 GstEvent * event, gboolean flush, gboolean only_to_active_pads)
1619 evdata.event = event;
1620 evdata.result = TRUE;
1621 evdata.flush = flush;
1622 evdata.one_actually_seeked = FALSE;
1623 evdata.only_to_active_pads = only_to_active_pads;
1625 /* We first need to set all pads as flushing in a first pass
1626 * as flush_start flush_stop is sometimes sent synchronously
1627 * while we send the seek event */
1631 GST_OBJECT_LOCK (self);
1632 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1633 GstAggregatorPad *pad = l->data;
1636 pad->priv->pending_flush_start = TRUE;
1637 pad->priv->pending_flush_stop = FALSE;
1640 GST_OBJECT_UNLOCK (self);
1643 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1645 gst_event_unref (event);
1651 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1656 GstSeekType start_type, stop_type;
1660 GstAggregatorPrivate *priv = self->priv;
1662 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1663 &start, &stop_type, &stop);
1665 GST_INFO_OBJECT (self, "starting SEEK");
1667 flush = flags & GST_SEEK_FLAG_FLUSH;
1669 GST_OBJECT_LOCK (self);
1671 priv->pending_flush_start = TRUE;
1672 priv->flush_seeking = TRUE;
1675 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1676 stop_type, stop, NULL);
1678 /* Seeking sets a position */
1679 self->priv->first_buffer = FALSE;
1680 GST_OBJECT_UNLOCK (self);
1682 /* forward the seek upstream */
1684 gst_aggregator_forward_event_to_all_sinkpads (self, event, flush, FALSE);
1687 if (!evdata.result || !evdata.one_actually_seeked) {
1688 GST_OBJECT_LOCK (self);
1689 priv->flush_seeking = FALSE;
1690 priv->pending_flush_start = FALSE;
1691 GST_OBJECT_UNLOCK (self);
1694 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1696 return evdata.result;
1700 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1703 gboolean res = TRUE;
1705 switch (GST_EVENT_TYPE (event)) {
1706 case GST_EVENT_SEEK:
1708 gst_event_ref (event);
1709 res = gst_aggregator_do_seek (self, event);
1710 gst_event_unref (event);
1714 case GST_EVENT_NAVIGATION:
1716 /* navigation is rather pointless. */
1718 gst_event_unref (event);
1727 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
1728 * they will receive a QOS event that has earliest_time=0 (because we can't
1729 * have negative timestamps), and consider their buffer as too late */
1731 gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE,
1732 GST_EVENT_TYPE (event) == GST_EVENT_QOS);
1733 res = evdata.result;
1740 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1743 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1745 return klass->src_event (GST_AGGREGATOR (parent), event);
1749 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1752 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1754 return klass->src_query (GST_AGGREGATOR (parent), query);
1758 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1759 GstObject * parent, GstPadMode mode, gboolean active)
1761 GstAggregator *self = GST_AGGREGATOR (parent);
1762 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1764 if (klass->src_activate) {
1765 if (klass->src_activate (self, mode, active) == FALSE) {
1770 if (active == TRUE) {
1772 case GST_PAD_MODE_PUSH:
1774 GST_INFO_OBJECT (pad, "Activating pad!");
1775 gst_aggregator_start_srcpad_task (self);
1780 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1787 GST_INFO_OBJECT (self, "Deactivating srcpad");
1788 gst_aggregator_stop_srcpad_task (self, FALSE);
1794 gst_aggregator_default_sink_query (GstAggregator * self,
1795 GstAggregatorPad * aggpad, GstQuery * query)
1797 GstPad *pad = GST_PAD (aggpad);
1799 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1803 gst_aggregator_finalize (GObject * object)
1805 GstAggregator *self = (GstAggregator *) object;
1807 g_mutex_clear (&self->priv->src_lock);
1808 g_cond_clear (&self->priv->src_cond);
1810 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1814 * gst_aggregator_set_latency_property:
1815 * @agg: a #GstAggregator
1816 * @latency: the new latency value (in nanoseconds).
1818 * Sets the new latency value to @latency. This value is used to limit the
1819 * amount of time a pad waits for data to appear before considering the pad
1823 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1827 g_return_if_fail (GST_IS_AGGREGATOR (self));
1828 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1831 changed = (self->priv->latency != latency);
1836 GST_OBJECT_LOCK (self);
1837 /* First lock all the pads */
1838 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1839 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1843 self->priv->latency = latency;
1845 SRC_BROADCAST (self);
1847 /* Now wake up the pads */
1848 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1849 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1850 PAD_BROADCAST_EVENT (aggpad);
1851 PAD_UNLOCK (aggpad);
1853 GST_OBJECT_UNLOCK (self);
1859 gst_element_post_message (GST_ELEMENT_CAST (self),
1860 gst_message_new_latency (GST_OBJECT_CAST (self)));
1864 * gst_aggregator_get_latency_property:
1865 * @agg: a #GstAggregator
1867 * Gets the latency value. See gst_aggregator_set_latency for
1870 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1871 * before a pad is deemed unresponsive. A value of -1 means an
1875 gst_aggregator_get_latency_property (GstAggregator * agg)
1879 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1881 GST_OBJECT_LOCK (agg);
1882 res = agg->priv->latency;
1883 GST_OBJECT_UNLOCK (agg);
1889 gst_aggregator_set_property (GObject * object, guint prop_id,
1890 const GValue * value, GParamSpec * pspec)
1892 GstAggregator *agg = GST_AGGREGATOR (object);
1896 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1898 case PROP_START_TIME_SELECTION:
1899 agg->priv->start_time_selection = g_value_get_enum (value);
1901 case PROP_START_TIME:
1902 agg->priv->start_time = g_value_get_uint64 (value);
1905 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1911 gst_aggregator_get_property (GObject * object, guint prop_id,
1912 GValue * value, GParamSpec * pspec)
1914 GstAggregator *agg = GST_AGGREGATOR (object);
1918 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1920 case PROP_START_TIME_SELECTION:
1921 g_value_set_enum (value, agg->priv->start_time_selection);
1923 case PROP_START_TIME:
1924 g_value_set_uint64 (value, agg->priv->start_time);
1927 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1932 /* GObject vmethods implementations */
1934 gst_aggregator_class_init (GstAggregatorClass * klass)
1936 GObjectClass *gobject_class = (GObjectClass *) klass;
1937 GstElementClass *gstelement_class = (GstElementClass *) klass;
1939 aggregator_parent_class = g_type_class_peek_parent (klass);
1940 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1942 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1943 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1945 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1947 klass->sink_event = gst_aggregator_default_sink_event;
1948 klass->sink_query = gst_aggregator_default_sink_query;
1950 klass->src_event = gst_aggregator_default_src_event;
1951 klass->src_query = gst_aggregator_default_src_query;
1953 gstelement_class->request_new_pad =
1954 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1955 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1956 gstelement_class->release_pad =
1957 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1958 gstelement_class->change_state =
1959 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1961 gobject_class->set_property = gst_aggregator_set_property;
1962 gobject_class->get_property = gst_aggregator_get_property;
1963 gobject_class->finalize = gst_aggregator_finalize;
1965 g_object_class_install_property (gobject_class, PROP_LATENCY,
1966 g_param_spec_int64 ("latency", "Buffer latency",
1967 "Additional latency in live mode to allow upstream "
1968 "to take longer to produce buffers for the current "
1969 "position (in nanoseconds)", 0,
1970 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1971 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1973 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
1974 g_param_spec_enum ("start-time-selection", "Start Time Selection",
1975 "Decides which start time is output",
1976 gst_aggregator_start_time_selection_get_type (),
1977 DEFAULT_START_TIME_SELECTION,
1978 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1980 g_object_class_install_property (gobject_class, PROP_START_TIME,
1981 g_param_spec_uint64 ("start-time", "Start Time",
1982 "Start time to use if start-time-selection=set", 0,
1984 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1986 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1990 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1992 GstPadTemplate *pad_template;
1993 GstAggregatorPrivate *priv;
1995 g_return_if_fail (klass->aggregate != NULL);
1998 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1999 GstAggregatorPrivate);
2004 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2005 g_return_if_fail (pad_template != NULL);
2007 priv->padcount = -1;
2008 priv->tags_changed = FALSE;
2010 self->priv->peer_latency_live = FALSE;
2011 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2012 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2013 self->priv->has_peer_latency = FALSE;
2014 gst_aggregator_reset_flow_values (self);
2016 self->srcpad = gst_pad_new_from_template (pad_template, "src");
2018 gst_pad_set_event_function (self->srcpad,
2019 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2020 gst_pad_set_query_function (self->srcpad,
2021 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2022 gst_pad_set_activatemode_function (self->srcpad,
2023 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2025 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2027 self->priv->latency = DEFAULT_LATENCY;
2028 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2029 self->priv->start_time = DEFAULT_START_TIME;
2031 g_mutex_init (&self->priv->src_lock);
2032 g_cond_init (&self->priv->src_cond);
2035 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2036 * method to get to the padtemplates */
2038 gst_aggregator_get_type (void)
2040 static volatile gsize type = 0;
2042 if (g_once_init_enter (&type)) {
2044 static const GTypeInfo info = {
2045 sizeof (GstAggregatorClass),
2048 (GClassInitFunc) gst_aggregator_class_init,
2051 sizeof (GstAggregator),
2053 (GInstanceInitFunc) gst_aggregator_init,
2056 _type = g_type_register_static (GST_TYPE_ELEMENT,
2057 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2058 g_once_init_leave (&type, _type);
2063 /* Must be called with SRC lock and PAD lock held */
2065 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2067 /* Empty queue always has space */
2068 if (g_queue_get_length (&aggpad->priv->buffers) == 0)
2071 /* We also want at least two buffers, one is being processed and one is ready
2072 * for the next iteration when we operate in live mode. */
2073 if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2076 /* zero latency, if there is a buffer, it's full */
2077 if (self->priv->latency == 0)
2080 /* Allow no more buffers than the latency */
2081 return (aggpad->priv->time_level <= self->priv->latency);
2084 /* Must be called with the PAD_LOCK held */
2086 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2088 GstClockTime timestamp;
2090 if (GST_BUFFER_DTS_IS_VALID (buffer))
2091 timestamp = GST_BUFFER_DTS (buffer);
2093 timestamp = GST_BUFFER_PTS (buffer);
2095 if (timestamp == GST_CLOCK_TIME_NONE) {
2097 timestamp = aggpad->priv->head_position;
2099 timestamp = aggpad->priv->tail_position;
2103 if (GST_BUFFER_DURATION_IS_VALID (buffer))
2104 timestamp += GST_BUFFER_DURATION (buffer);
2107 aggpad->priv->head_position = timestamp;
2109 aggpad->priv->tail_position = timestamp;
2111 update_time_level (aggpad, head);
2114 static GstFlowReturn
2115 gst_aggregator_pad_chain_internal (GstAggregator * self,
2116 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2118 GstBuffer *actual_buf = buffer;
2119 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
2120 GstFlowReturn flow_return;
2121 GstClockTime buf_pts;
2123 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2125 PAD_FLUSH_LOCK (aggpad);
2128 flow_return = aggpad->priv->flow_return;
2129 if (flow_return != GST_FLOW_OK)
2132 if (aggpad->priv->pending_eos == TRUE)
2135 flow_return = aggpad->priv->flow_return;
2136 if (flow_return != GST_FLOW_OK)
2139 PAD_UNLOCK (aggpad);
2141 if (aggclass->clip && head) {
2142 aggclass->clip (self, aggpad, buffer, &actual_buf);
2145 if (actual_buf == NULL) {
2146 GST_LOG_OBJECT (actual_buf, "Buffer dropped by clip function");
2150 buf_pts = GST_BUFFER_PTS (actual_buf);
2152 aggpad->priv->first_buffer = FALSE;
2157 if (gst_aggregator_pad_has_space (self, aggpad)
2158 && aggpad->priv->flow_return == GST_FLOW_OK) {
2160 g_queue_push_head (&aggpad->priv->buffers, actual_buf);
2162 g_queue_push_tail (&aggpad->priv->buffers, actual_buf);
2163 apply_buffer (aggpad, actual_buf, head);
2164 aggpad->priv->num_buffers++;
2165 actual_buf = buffer = NULL;
2166 SRC_BROADCAST (self);
2170 flow_return = aggpad->priv->flow_return;
2171 if (flow_return != GST_FLOW_OK) {
2175 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2177 PAD_WAIT_EVENT (aggpad);
2179 PAD_UNLOCK (aggpad);
2182 if (self->priv->first_buffer) {
2183 GstClockTime start_time;
2185 switch (self->priv->start_time_selection) {
2186 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2190 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2191 if (aggpad->segment.format == GST_FORMAT_TIME) {
2192 start_time = buf_pts;
2193 if (start_time != -1) {
2194 start_time = MAX (start_time, aggpad->segment.start);
2196 gst_segment_to_running_time (&aggpad->segment, GST_FORMAT_TIME,
2201 GST_WARNING_OBJECT (aggpad,
2202 "Ignoring request of selecting the first start time "
2203 "as the segment is a %s segment instead of a time segment",
2204 gst_format_get_name (aggpad->segment.format));
2207 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2208 start_time = self->priv->start_time;
2209 if (start_time == -1)
2214 if (start_time != -1) {
2215 if (self->segment.position == -1)
2216 self->segment.position = start_time;
2218 self->segment.position = MIN (start_time, self->segment.position);
2220 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2221 GST_TIME_ARGS (start_time));
2225 PAD_UNLOCK (aggpad);
2230 PAD_FLUSH_UNLOCK (aggpad);
2232 GST_DEBUG_OBJECT (aggpad, "Done chaining");
2237 PAD_UNLOCK (aggpad);
2238 PAD_FLUSH_UNLOCK (aggpad);
2240 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2241 gst_flow_get_name (flow_return));
2243 gst_buffer_unref (buffer);
2248 PAD_UNLOCK (aggpad);
2249 PAD_FLUSH_UNLOCK (aggpad);
2251 gst_buffer_unref (buffer);
2252 GST_DEBUG_OBJECT (aggpad, "We are EOS already...");
2254 return GST_FLOW_EOS;
2257 static GstFlowReturn
2258 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2260 return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2261 GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2265 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2268 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2269 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2271 if (GST_QUERY_IS_SERIALIZED (query)) {
2274 while (!gst_aggregator_pad_queue_is_empty (aggpad)
2275 && aggpad->priv->flow_return == GST_FLOW_OK) {
2276 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2277 PAD_WAIT_EVENT (aggpad);
2280 if (aggpad->priv->flow_return != GST_FLOW_OK)
2283 PAD_UNLOCK (aggpad);
2286 return klass->sink_query (GST_AGGREGATOR (parent),
2287 GST_AGGREGATOR_PAD (pad), query);
2290 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2291 gst_flow_get_name (aggpad->priv->flow_return));
2292 PAD_UNLOCK (aggpad);
2297 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2300 GstAggregator *self = GST_AGGREGATOR (parent);
2301 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2302 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2304 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
2305 /* && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE */ ) {
2309 if (aggpad->priv->flow_return != GST_FLOW_OK
2310 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
2313 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2314 GST_OBJECT_LOCK (aggpad);
2315 gst_event_copy_segment (event, &aggpad->clip_segment);
2316 aggpad->priv->head_position = aggpad->clip_segment.position;
2317 update_time_level (aggpad, TRUE);
2318 GST_OBJECT_UNLOCK (aggpad);
2321 if (!gst_aggregator_pad_queue_is_empty (aggpad) &&
2322 GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2323 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT,
2325 g_queue_push_head (&aggpad->priv->buffers, event);
2327 SRC_BROADCAST (self);
2329 PAD_UNLOCK (aggpad);
2334 return klass->sink_event (self, aggpad, event);
2339 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2340 gst_flow_get_name (aggpad->priv->flow_return));
2341 PAD_UNLOCK (aggpad);
2343 if (GST_EVENT_IS_STICKY (event))
2344 gst_pad_store_sticky_event (pad, event);
2345 gst_event_unref (event);
2350 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2351 GstObject * parent, GstPadMode mode, gboolean active)
2353 GstAggregator *self = GST_AGGREGATOR (parent);
2354 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2356 if (active == FALSE) {
2358 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2359 SRC_BROADCAST (self);
2363 aggpad->priv->flow_return = GST_FLOW_OK;
2364 PAD_BROADCAST_EVENT (aggpad);
2365 PAD_UNLOCK (aggpad);
2371 /***********************************
2372 * GstAggregatorPad implementation *
2373 ************************************/
2374 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2377 gst_aggregator_pad_constructed (GObject * object)
2379 GstPad *pad = GST_PAD (object);
2381 gst_pad_set_chain_function (pad,
2382 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2383 gst_pad_set_event_function (pad,
2384 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
2385 gst_pad_set_query_function (pad,
2386 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2387 gst_pad_set_activatemode_function (pad,
2388 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2392 gst_aggregator_pad_finalize (GObject * object)
2394 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2396 g_cond_clear (&pad->priv->event_cond);
2397 g_mutex_clear (&pad->priv->flush_lock);
2398 g_mutex_clear (&pad->priv->lock);
2400 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2404 gst_aggregator_pad_dispose (GObject * object)
2406 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2408 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2410 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2414 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2416 GObjectClass *gobject_class = (GObjectClass *) klass;
2418 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2420 gobject_class->constructed = gst_aggregator_pad_constructed;
2421 gobject_class->finalize = gst_aggregator_pad_finalize;
2422 gobject_class->dispose = gst_aggregator_pad_dispose;
2426 gst_aggregator_pad_init (GstAggregatorPad * pad)
2429 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2430 GstAggregatorPadPrivate);
2432 g_queue_init (&pad->priv->buffers);
2433 g_cond_init (&pad->priv->event_cond);
2435 g_mutex_init (&pad->priv->flush_lock);
2436 g_mutex_init (&pad->priv->lock);
2438 pad->priv->first_buffer = TRUE;
2442 * gst_aggregator_pad_steal_buffer:
2443 * @pad: the pad to get buffer from
2445 * Steal the ref to the buffer currently queued in @pad.
2447 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2448 * queued. You should unref the buffer after usage.
2451 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2453 GstBuffer *buffer = NULL;
2456 if (GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->buffers)))
2457 buffer = g_queue_pop_tail (&pad->priv->buffers);
2460 apply_buffer (pad, buffer, FALSE);
2461 pad->priv->num_buffers--;
2462 GST_TRACE_OBJECT (pad, "Consuming buffer");
2463 if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
2464 pad->priv->pending_eos = FALSE;
2465 pad->priv->eos = TRUE;
2467 PAD_BROADCAST_EVENT (pad);
2468 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2476 * gst_aggregator_pad_drop_buffer:
2477 * @pad: the pad where to drop any pending buffer
2479 * Drop the buffer currently queued in @pad.
2481 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2484 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2488 buf = gst_aggregator_pad_steal_buffer (pad);
2493 gst_buffer_unref (buf);
2498 * gst_aggregator_pad_get_buffer:
2499 * @pad: the pad to get buffer from
2501 * Returns: (transfer full): A reference to the buffer in @pad or
2502 * NULL if no buffer was queued. You should unref the buffer after
2506 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2508 GstBuffer *buffer = NULL;
2511 buffer = g_queue_peek_tail (&pad->priv->buffers);
2512 /* The tail should always be a buffer, because if it is an event,
2513 * it will be consumed immeditaly in gst_aggregator_steal_buffer */
2515 if (GST_IS_BUFFER (buffer))
2516 gst_buffer_ref (buffer);
2525 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2530 is_eos = pad->priv->eos;
2537 * gst_aggregator_merge_tags:
2538 * @self: a #GstAggregator
2539 * @tags: a #GstTagList to merge
2540 * @mode: the #GstTagMergeMode to use
2542 * Adds tags to so-called pending tags, which will be processed
2543 * before pushing out data downstream.
2545 * Note that this is provided for convenience, and the subclass is
2546 * not required to use this and can still do tag handling on its own.
2551 gst_aggregator_merge_tags (GstAggregator * self,
2552 const GstTagList * tags, GstTagMergeMode mode)
2556 g_return_if_fail (GST_IS_AGGREGATOR (self));
2557 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2559 /* FIXME Check if we can use OBJECT lock here! */
2560 GST_OBJECT_LOCK (self);
2562 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2563 otags = self->priv->tags;
2564 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2566 gst_tag_list_unref (otags);
2567 self->priv->tags_changed = TRUE;
2568 GST_OBJECT_UNLOCK (self);
2572 * gst_aggregator_set_latency:
2573 * @self: a #GstAggregator
2574 * @min_latency: minimum latency
2575 * @max_latency: maximum latency
2577 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2578 * latency is. Will also post a LATENCY message on the bus so the pipeline
2579 * can reconfigure its global latency.
2582 gst_aggregator_set_latency (GstAggregator * self,
2583 GstClockTime min_latency, GstClockTime max_latency)
2585 gboolean changed = FALSE;
2587 g_return_if_fail (GST_IS_AGGREGATOR (self));
2588 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2589 g_return_if_fail (max_latency >= min_latency);
2592 if (self->priv->sub_latency_min != min_latency) {
2593 self->priv->sub_latency_min = min_latency;
2596 if (self->priv->sub_latency_max != max_latency) {
2597 self->priv->sub_latency_max = max_latency;
2602 SRC_BROADCAST (self);
2606 gst_element_post_message (GST_ELEMENT_CAST (self),
2607 gst_message_new_latency (GST_OBJECT_CAST (self)));