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. Implementers should at least implement
33 * the aggregate () vmethod.
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
64 #include <string.h> /* strlen */
66 #include "gstaggregator.h"
69 /* Might become API */
70 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
71 const GstTagList * tags, GstTagMergeMode mode);
72 static void gst_aggregator_set_latency_property (GstAggregator * agg,
74 static gint64 gst_aggregator_get_latency_property (GstAggregator * agg);
77 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
78 #define GST_CAT_DEFAULT aggregator_debug
80 /* GstAggregatorPad definitions */
81 #define PAD_LOCK_EVENT(pad) G_STMT_START { \
82 GST_LOG_OBJECT (pad, "Taking EVENT lock from thread %p", \
84 g_mutex_lock(&pad->priv->event_lock); \
85 GST_LOG_OBJECT (pad, "Took EVENT lock from thread %p", \
89 #define PAD_UNLOCK_EVENT(pad) G_STMT_START { \
90 GST_LOG_OBJECT (pad, "Releasing EVENT lock from thread %p", \
92 g_mutex_unlock(&pad->priv->event_lock); \
93 GST_LOG_OBJECT (pad, "Release EVENT lock from thread %p", \
98 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
99 GST_LOG_OBJECT (pad, "Waiting for EVENT on thread %p", \
101 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
102 &(pad->priv->event_lock)); \
103 GST_LOG_OBJECT (pad, "DONE Waiting for EVENT on thread %p", \
107 #define PAD_BROADCAST_EVENT(pad) { \
108 GST_LOG_OBJECT (pad, "Signaling EVENT from thread %p", \
110 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
113 #define GST_AGGREGATOR_SETCAPS_LOCK(self) G_STMT_START { \
114 GST_LOG_OBJECT (self, "Taking SETCAPS lock from thread %p", \
116 g_mutex_lock(&self->priv->setcaps_lock); \
117 GST_LOG_OBJECT (self, "Took SETCAPS lock from thread %p", \
121 #define GST_AGGREGATOR_SETCAPS_UNLOCK(self) G_STMT_START { \
122 GST_LOG_OBJECT (self, "Releasing SETCAPS lock from thread %p", \
124 g_mutex_unlock(&self->priv->setcaps_lock); \
125 GST_LOG_OBJECT (self, "Took SETCAPS lock from thread %p", \
129 #define PAD_STREAM_LOCK(pad) G_STMT_START { \
130 GST_LOG_OBJECT (pad, "Taking lock from thread %p", \
132 g_mutex_lock(&pad->priv->stream_lock); \
133 GST_LOG_OBJECT (pad, "Took lock from thread %p", \
137 #define PAD_STREAM_UNLOCK(pad) G_STMT_START { \
138 GST_LOG_OBJECT (pad, "Releasing lock from thread %p", \
140 g_mutex_unlock(&pad->priv->stream_lock); \
141 GST_LOG_OBJECT (pad, "Release lock from thread %p", \
145 #define SRC_STREAM_LOCK(self) G_STMT_START { \
146 GST_LOG_OBJECT (self, "Taking src STREAM lock from thread %p", \
148 g_mutex_lock(&self->priv->src_lock); \
149 GST_LOG_OBJECT (self, "Took src STREAM lock from thread %p", \
153 #define SRC_STREAM_UNLOCK(self) G_STMT_START { \
154 GST_LOG_OBJECT (self, "Releasing src STREAM lock from thread %p", \
156 g_mutex_unlock(&self->priv->src_lock); \
157 GST_LOG_OBJECT (self, "Release src STREAM lock from thread %p", \
161 #define SRC_STREAM_WAIT(self) G_STMT_START { \
162 GST_LOG_OBJECT (self, "Waiting for src STREAM on thread %p", \
164 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
165 GST_LOG_OBJECT (self, "DONE Waiting for src STREAM on thread %p", \
169 #define SRC_STREAM_BROADCAST_UNLOCKED(self) G_STMT_START { \
170 GST_LOG_OBJECT (self, "Signaling src STREAM from thread %p", \
172 if (self->priv->aggregate_id) \
173 gst_clock_id_unschedule (self->priv->aggregate_id); \
174 g_cond_broadcast(&(self->priv->src_cond)); \
177 #define SRC_STREAM_BROADCAST(self) G_STMT_START { \
178 SRC_STREAM_LOCK (self); \
179 SRC_STREAM_BROADCAST_UNLOCKED (self); \
180 SRC_STREAM_UNLOCK (self); \
183 struct _GstAggregatorPadPrivate
185 gboolean pending_flush_start;
186 gboolean pending_flush_stop;
187 gboolean pending_eos;
197 _aggpad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
199 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
202 aggpad->priv->flushing = FALSE;
205 return klass->flush (aggpad, agg);
210 /*************************************
211 * GstAggregator implementation *
212 *************************************/
213 static GstElementClass *aggregator_parent_class = NULL;
215 struct _GstAggregatorPrivate
219 /* Our state is >= PAUSED */
224 gboolean send_stream_start;
225 gboolean send_segment;
226 gboolean flush_seeking;
227 gboolean pending_flush_start;
229 GstFlowReturn flow_return;
234 gboolean tags_changed;
236 /* Lock to prevent two src setcaps from happening at the same time */
239 gboolean latency_live;
240 GstClockTime latency_min;
241 GstClockTime latency_max;
243 GstClockTime sub_latency_min;
244 GstClockTime sub_latency_max;
247 GstClockID aggregate_id;
258 gboolean one_actually_seeked;
261 #define DEFAULT_LATENCY 0
271 * gst_aggregator_iterate_sinkpads:
272 * @self: The #GstAggregator
273 * @func: The function to call.
274 * @user_data: The data to pass to @func.
276 * Iterate the sinkpads of aggregator to call a function on them.
278 * This method guarantees that @func will be called only once for each
282 gst_aggregator_iterate_sinkpads (GstAggregator * self,
283 GstAggregatorPadForeachFunc func, gpointer user_data)
285 gboolean result = FALSE;
287 gboolean done = FALSE;
288 GValue item = { 0, };
289 GList *seen_pads = NULL;
291 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
297 switch (gst_iterator_next (iter, &item)) {
298 case GST_ITERATOR_OK:
302 pad = g_value_get_object (&item);
304 /* if already pushed, skip. FIXME, find something faster to tag pads */
305 if (pad == NULL || g_list_find (seen_pads, pad)) {
306 g_value_reset (&item);
310 GST_LOG_OBJECT (self, "calling function on pad %s:%s",
311 GST_DEBUG_PAD_NAME (pad));
312 result = func (self, pad, user_data);
316 seen_pads = g_list_prepend (seen_pads, pad);
318 g_value_reset (&item);
321 case GST_ITERATOR_RESYNC:
322 gst_iterator_resync (iter);
324 case GST_ITERATOR_ERROR:
325 GST_ERROR_OBJECT (self,
326 "Could not iterate over internally linked pads");
329 case GST_ITERATOR_DONE:
334 g_value_unset (&item);
335 gst_iterator_free (iter);
337 if (seen_pads == NULL) {
338 GST_DEBUG_OBJECT (self, "No pad seen");
342 g_list_free (seen_pads);
348 static inline gboolean
349 _check_all_pads_with_data_or_eos (GstAggregator * self,
350 GstAggregatorPad * aggpad, gpointer user_data)
352 if (aggpad->buffer || aggpad->eos) {
356 GST_LOG_OBJECT (aggpad, "Not ready to be aggregated");
362 _reset_flow_values (GstAggregator * self)
364 self->priv->flow_return = GST_FLOW_FLUSHING;
365 self->priv->send_stream_start = TRUE;
366 self->priv->send_segment = TRUE;
367 gst_segment_init (&self->segment, GST_FORMAT_TIME);
371 _push_mandatory_events (GstAggregator * self)
373 GstAggregatorPrivate *priv = self->priv;
375 if (g_atomic_int_get (&self->priv->send_stream_start)) {
378 GST_INFO_OBJECT (self, "pushing stream start");
379 /* stream-start (FIXME: create id based on input ids) */
380 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
381 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
382 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
384 g_atomic_int_set (&self->priv->send_stream_start, FALSE);
387 if (self->priv->srccaps) {
389 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
390 self->priv->srccaps);
391 if (!gst_pad_push_event (self->srcpad,
392 gst_event_new_caps (self->priv->srccaps))) {
393 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
395 gst_caps_unref (self->priv->srccaps);
396 self->priv->srccaps = NULL;
399 if (g_atomic_int_get (&self->priv->send_segment)) {
400 if (!g_atomic_int_get (&self->priv->flush_seeking)) {
401 GstEvent *segev = gst_event_new_segment (&self->segment);
403 if (!self->priv->seqnum)
404 self->priv->seqnum = gst_event_get_seqnum (segev);
406 gst_event_set_seqnum (segev, self->priv->seqnum);
408 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segev);
409 gst_pad_push_event (self->srcpad, segev);
410 g_atomic_int_set (&self->priv->send_segment, FALSE);
414 if (priv->tags && priv->tags_changed) {
415 gst_pad_push_event (self->srcpad,
416 gst_event_new_tag (gst_tag_list_ref (priv->tags)));
417 priv->tags_changed = FALSE;
422 * gst_aggregator_set_src_caps:
423 * @self: The #GstAggregator
424 * @caps: The #GstCaps to set on the src pad.
426 * Sets the caps to be used on the src pad.
429 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
431 GST_AGGREGATOR_SETCAPS_LOCK (self);
432 gst_caps_replace (&self->priv->srccaps, caps);
433 _push_mandatory_events (self);
434 GST_AGGREGATOR_SETCAPS_UNLOCK (self);
438 * gst_aggregator_finish_buffer:
439 * @self: The #GstAggregator
440 * @buffer: the #GstBuffer to push.
442 * This method will take care of sending mandatory events before pushing
443 * the provided buffer.
446 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
448 _push_mandatory_events (self);
450 if (!g_atomic_int_get (&self->priv->flush_seeking) &&
451 gst_pad_is_active (self->srcpad)) {
452 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
453 return gst_pad_push (self->srcpad, buffer);
455 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
456 g_atomic_int_get (&self->priv->flush_seeking),
457 gst_pad_is_active (self->srcpad));
458 gst_buffer_unref (buffer);
464 _push_eos (GstAggregator * self)
467 _push_mandatory_events (self);
469 self->priv->send_eos = FALSE;
470 event = gst_event_new_eos ();
471 gst_event_set_seqnum (event, self->priv->seqnum);
472 gst_pad_push_event (self->srcpad, event);
476 gst_aggregator_get_next_time (GstAggregator * self)
478 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
480 if (klass->get_next_time)
481 return klass->get_next_time (self);
483 return GST_CLOCK_TIME_NONE;
486 /* called with the src STREAM lock */
488 _wait_and_check (GstAggregator * self, gboolean * timeout)
490 GstClockTime latency_max, latency_min;
496 SRC_STREAM_LOCK (self);
498 gst_aggregator_get_latency (self, &live, &latency_min, &latency_max);
500 if (gst_aggregator_iterate_sinkpads (self,
501 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos,
503 GST_DEBUG_OBJECT (self, "all pads have data");
504 SRC_STREAM_UNLOCK (self);
509 /* Before waiting, check if we're actually still running */
510 if (!self->priv->running || !self->priv->send_eos) {
511 SRC_STREAM_UNLOCK (self);
516 start = gst_aggregator_get_next_time (self);
518 if (!live || !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self))
519 || !GST_CLOCK_TIME_IS_VALID (start)) {
520 /* We wake up here when something happened, and below
521 * then check if we're ready now. If we return FALSE,
522 * we will be directly called again.
524 SRC_STREAM_WAIT (self);
526 GstClockTime base_time, time;
528 GstClockReturn status;
530 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
531 GST_TIME_ARGS (start));
533 GST_OBJECT_LOCK (self);
534 base_time = GST_ELEMENT_CAST (self)->base_time;
535 clock = GST_ELEMENT_CLOCK (self);
537 gst_object_ref (clock);
538 GST_OBJECT_UNLOCK (self);
540 time = base_time + start;
542 if (GST_CLOCK_TIME_IS_VALID (latency_min)) {
545 time += self->latency;
548 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
549 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
550 " latency max %" GST_TIME_FORMAT " latency min %" GST_TIME_FORMAT
551 " current %" GST_TIME_FORMAT ")", GST_TIME_ARGS (time),
552 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
553 GST_TIME_ARGS (start), GST_TIME_ARGS (latency_max),
554 GST_TIME_ARGS (latency_min),
555 GST_TIME_ARGS (gst_clock_get_time (clock)));
557 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
558 gst_object_unref (clock);
559 SRC_STREAM_UNLOCK (self);
561 status = gst_clock_id_wait (self->priv->aggregate_id, NULL);
563 SRC_STREAM_LOCK (self);
564 if (self->priv->aggregate_id) {
565 gst_clock_id_unref (self->priv->aggregate_id);
566 self->priv->aggregate_id = NULL;
569 GST_DEBUG_OBJECT (self, "clock returned %d", status);
572 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
573 SRC_STREAM_UNLOCK (self);
579 res = gst_aggregator_iterate_sinkpads (self,
580 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos, NULL);
581 SRC_STREAM_UNLOCK (self);
587 aggregate_func (GstAggregator * self)
589 GstAggregatorPrivate *priv = self->priv;
590 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
591 gboolean timeout = FALSE;
593 if (self->priv->running == FALSE) {
594 GST_DEBUG_OBJECT (self, "Not running anymore");
598 GST_LOG_OBJECT (self, "Checking aggregate");
599 while (priv->send_eos && priv->running) {
600 if (!_wait_and_check (self, &timeout))
603 GST_TRACE_OBJECT (self, "Actually aggregating!");
605 priv->flow_return = klass->aggregate (self, timeout);
607 if (priv->flow_return == GST_FLOW_EOS) {
611 if (priv->flow_return == GST_FLOW_FLUSHING &&
612 g_atomic_int_get (&priv->flush_seeking))
613 priv->flow_return = GST_FLOW_OK;
615 GST_LOG_OBJECT (self, "flow return is %s",
616 gst_flow_get_name (priv->flow_return));
618 if (priv->flow_return != GST_FLOW_OK)
624 _start (GstAggregator * self)
626 self->priv->running = TRUE;
627 self->priv->send_stream_start = TRUE;
628 self->priv->send_segment = TRUE;
629 self->priv->send_eos = TRUE;
630 self->priv->srccaps = NULL;
631 self->priv->flow_return = GST_FLOW_OK;
637 _check_pending_flush_stop (GstAggregatorPad * pad)
639 return (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
643 _stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
647 GST_INFO_OBJECT (self, "%s srcpad task",
648 flush_start ? "Pausing" : "Stopping");
650 self->priv->running = FALSE;
651 SRC_STREAM_BROADCAST (self);
654 res = gst_pad_push_event (self->srcpad, flush_start);
657 gst_pad_stop_task (self->srcpad);
658 SRC_STREAM_BROADCAST (self);
664 _start_srcpad_task (GstAggregator * self)
666 GST_INFO_OBJECT (self, "Starting srcpad task");
668 self->priv->running = TRUE;
669 gst_pad_start_task (GST_PAD (self->srcpad),
670 (GstTaskFunction) aggregate_func, self, NULL);
674 _flush (GstAggregator * self)
676 GstFlowReturn ret = GST_FLOW_OK;
677 GstAggregatorPrivate *priv = self->priv;
678 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
680 GST_DEBUG_OBJECT (self, "Flushing everything");
681 g_atomic_int_set (&priv->send_segment, TRUE);
682 g_atomic_int_set (&priv->flush_seeking, FALSE);
683 g_atomic_int_set (&priv->tags_changed, FALSE);
685 ret = klass->flush (self);
691 _all_flush_stop_received (GstAggregator * self)
694 GstAggregatorPad *tmppad;
696 GST_OBJECT_LOCK (self);
697 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
698 tmppad = (GstAggregatorPad *) tmp->data;
700 if (_check_pending_flush_stop (tmppad) == FALSE) {
701 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
702 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
703 GST_OBJECT_UNLOCK (self);
707 GST_OBJECT_UNLOCK (self);
713 _flush_start (GstAggregator * self, GstAggregatorPad * aggpad, GstEvent * event)
716 GstAggregatorPrivate *priv = self->priv;
717 GstAggregatorPadPrivate *padpriv = aggpad->priv;
719 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
720 /* Remove pad buffer and wake up the streaming thread */
721 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
722 gst_buffer_replace (&tmpbuf, NULL);
723 PAD_STREAM_LOCK (aggpad);
724 if (g_atomic_int_compare_and_exchange (&padpriv->pending_flush_start,
725 TRUE, FALSE) == TRUE) {
726 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
727 g_atomic_int_set (&padpriv->pending_flush_stop, TRUE);
730 if (g_atomic_int_get (&priv->flush_seeking)) {
731 /* If flush_seeking we forward the first FLUSH_START */
732 if (g_atomic_int_compare_and_exchange (&priv->pending_flush_start,
733 TRUE, FALSE) == TRUE) {
735 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
736 _stop_srcpad_task (self, event);
737 priv->flow_return = GST_FLOW_OK;
739 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
740 GST_PAD_STREAM_LOCK (self->srcpad);
741 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
744 gst_event_unref (event);
747 gst_event_unref (event);
749 PAD_STREAM_UNLOCK (aggpad);
751 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
752 gst_buffer_replace (&tmpbuf, NULL);
755 /* GstAggregator vmethods default implementations */
757 _sink_event (GstAggregator * self, GstAggregatorPad * aggpad, GstEvent * event)
760 GstPad *pad = GST_PAD (aggpad);
761 GstAggregatorPrivate *priv = self->priv;
763 switch (GST_EVENT_TYPE (event)) {
764 case GST_EVENT_FLUSH_START:
766 _flush_start (self, aggpad, event);
767 /* We forward only in one case: right after flush_seeking */
771 case GST_EVENT_FLUSH_STOP:
773 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
775 _aggpad_flush (aggpad, self);
776 if (g_atomic_int_get (&priv->flush_seeking)) {
777 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
779 if (g_atomic_int_get (&priv->flush_seeking)) {
780 if (_all_flush_stop_received (self)) {
781 /* That means we received FLUSH_STOP/FLUSH_STOP on
782 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
784 gst_pad_push_event (self->srcpad, event);
785 priv->send_eos = TRUE;
787 SRC_STREAM_BROADCAST (self);
789 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
790 GST_PAD_STREAM_UNLOCK (self->srcpad);
791 _start_srcpad_task (self);
796 /* We never forward the event */
801 GST_DEBUG_OBJECT (aggpad, "EOS");
803 /* We still have a buffer, and we don't want the subclass to have to
804 * check for it. Mark pending_eos, eos will be set when steal_buffer is
807 PAD_LOCK_EVENT (aggpad);
808 if (!aggpad->buffer) {
811 aggpad->priv->pending_eos = TRUE;
813 PAD_UNLOCK_EVENT (aggpad);
815 SRC_STREAM_BROADCAST (self);
818 case GST_EVENT_SEGMENT:
820 PAD_LOCK_EVENT (aggpad);
821 gst_event_copy_segment (event, &aggpad->segment);
822 self->priv->seqnum = gst_event_get_seqnum (event);
823 PAD_UNLOCK_EVENT (aggpad);
826 case GST_EVENT_STREAM_START:
834 gst_event_parse_tag (event, &tags);
836 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
837 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
838 gst_event_unref (event);
850 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
851 return gst_pad_event_default (pad, GST_OBJECT (self), event);
854 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
856 gst_event_unref (event);
862 _stop_pad (GstAggregator * self, GstAggregatorPad * pad, gpointer unused_udata)
864 _aggpad_flush (pad, self);
870 _stop (GstAggregator * agg)
872 _reset_flow_values (agg);
874 gst_aggregator_iterate_sinkpads (agg,
875 (GstAggregatorPadForeachFunc) _stop_pad, NULL);
878 gst_tag_list_unref (agg->priv->tags);
879 agg->priv->tags = NULL;
884 /* GstElement vmethods implementations */
885 static GstStateChangeReturn
886 _change_state (GstElement * element, GstStateChange transition)
888 GstStateChangeReturn ret;
889 GstAggregator *self = GST_AGGREGATOR (element);
890 GstAggregatorClass *agg_class = GST_AGGREGATOR_GET_CLASS (self);
893 switch (transition) {
894 case GST_STATE_CHANGE_READY_TO_PAUSED:
895 agg_class->start (self);
902 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
903 transition)) == GST_STATE_CHANGE_FAILURE)
907 switch (transition) {
908 case GST_STATE_CHANGE_PAUSED_TO_READY:
909 agg_class->stop (self);
919 GST_ERROR_OBJECT (element, "parent failed state change");
925 _release_pad (GstElement * element, GstPad * pad)
927 GstAggregator *self = GST_AGGREGATOR (element);
930 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
932 GST_INFO_OBJECT (pad, "Removing pad");
934 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
935 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
936 gst_buffer_replace (&tmpbuf, NULL);
937 gst_element_remove_pad (element, pad);
939 SRC_STREAM_BROADCAST (self);
943 _request_new_pad (GstElement * element,
944 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
947 GstAggregatorPad *agg_pad;
949 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
950 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
952 self = GST_AGGREGATOR (element);
954 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
958 GST_OBJECT_LOCK (element);
959 if (req_name == NULL || strlen (req_name) < 6
960 || !g_str_has_prefix (req_name, "sink_")) {
961 /* no name given when requesting the pad, use next available int */
964 /* parse serial number from requested padname */
965 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
966 if (serial >= priv->padcount)
967 priv->padcount = serial;
970 name = g_strdup_printf ("sink_%u", priv->padcount);
971 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
972 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
975 GST_OBJECT_UNLOCK (element);
981 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
984 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
986 /* add the pad to the element */
987 gst_element_add_pad (element, GST_PAD (agg_pad));
989 return GST_PAD (agg_pad);
994 GstClockTime min, max;
999 _latency_query (GstAggregator * self, GstPad * pad, gpointer user_data)
1001 LatencyData *data = user_data;
1002 GstClockTime min, max;
1006 query = gst_query_new_latency ();
1007 res = gst_pad_peer_query (pad, query);
1010 gst_query_parse_latency (query, &live, &min, &max);
1012 GST_LOG_OBJECT (self, "%s: got latency live:%s min:%" G_GINT64_FORMAT
1013 " max:%" G_GINT64_FORMAT, GST_PAD_NAME (pad),
1014 live ? "true" : "false", min, max);
1016 if (min != GST_CLOCK_TIME_NONE && min > data->min)
1019 if (max != GST_CLOCK_TIME_NONE &&
1020 ((data->max != GST_CLOCK_TIME_NONE && max < data->max) ||
1021 (data->max == GST_CLOCK_TIME_NONE)))
1027 gst_query_unref (query);
1033 * gst_aggregator_get_latency:
1034 * @self: a #GstAggregator
1035 * @live: (out) (allow-none): whether @self is live
1036 * @min_latency: (out) (allow-none): the configured minimum latency of @self
1037 * @max_latency: (out) (allow-none): the configured maximum latency of @self
1039 * Retreives the latency values reported by @self in response to the latency
1042 * Typically only called by subclasses.
1045 gst_aggregator_get_latency (GstAggregator * self, gboolean * live,
1046 GstClockTime * min_latency, GstClockTime * max_latency)
1048 GstClockTime min, max;
1050 g_return_if_fail (GST_IS_AGGREGATOR (self));
1052 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1053 min = self->priv->latency_min;
1054 max = self->priv->latency_max;
1056 min += self->priv->sub_latency_min;
1057 if (GST_CLOCK_TIME_IS_VALID (max)
1058 && GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max))
1059 max += self->priv->sub_latency_max;
1061 if (GST_CLOCK_TIME_IS_VALID (self->latency)) {
1062 min += self->latency;
1063 if (GST_CLOCK_TIME_IS_VALID (max))
1064 max += self->latency;
1068 *live = self->priv->latency_live;
1076 gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
1081 data.max = GST_CLOCK_TIME_NONE;
1084 /* query upstream's latency */
1085 SRC_STREAM_LOCK (self);
1086 gst_aggregator_iterate_sinkpads (self,
1087 (GstAggregatorPadForeachFunc) _latency_query, &data);
1088 SRC_STREAM_UNLOCK (self);
1090 if (data.live && GST_CLOCK_TIME_IS_VALID (self->latency) &&
1091 self->latency > data.max) {
1092 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1093 ("%s", "Latency too big"),
1094 ("The requested latency value is too big for the current pipeline. "
1095 "Limiting to %" G_GINT64_FORMAT, data.max));
1096 self->latency = data.max;
1099 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (data.min))) {
1100 GST_WARNING_OBJECT (self, "Invalid minimum latency, using 0");
1104 if (G_UNLIKELY (data.min > data.max)) {
1105 GST_WARNING_OBJECT (self, "Minimum latency is greater than maximum latency "
1106 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT "). "
1107 "Clamping it at the maximum latency", data.min, data.max);
1108 data.min = data.max;
1111 self->priv->latency_live = data.live;
1112 self->priv->latency_min = data.min;
1113 self->priv->latency_max = data.max;
1116 if (GST_CLOCK_TIME_IS_VALID (self->latency)) {
1117 if (GST_CLOCK_TIME_IS_VALID (data.min))
1118 data.min += self->latency;
1119 if (GST_CLOCK_TIME_IS_VALID (data.max))
1120 data.max += self->latency;
1123 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_min)
1124 && GST_CLOCK_TIME_IS_VALID (data.min))
1125 data.min += self->priv->sub_latency_min;
1126 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1127 && GST_CLOCK_TIME_IS_VALID (data.max))
1128 data.max += self->priv->sub_latency_max;
1130 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1131 " max:%" G_GINT64_FORMAT, data.live ? "true" : "false", data.min,
1134 gst_query_set_latency (query, data.live, data.min, data.max);
1140 _send_event (GstElement * element, GstEvent * event)
1142 GstAggregator *self = GST_AGGREGATOR (element);
1144 GST_STATE_LOCK (element);
1145 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1146 GST_STATE (element) < GST_STATE_PAUSED) {
1150 GstSeekType start_type, stop_type;
1153 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1154 &start, &stop_type, &stop);
1155 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1156 stop_type, stop, NULL);
1158 self->priv->seqnum = gst_event_get_seqnum (event);
1159 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1161 GST_STATE_UNLOCK (element);
1164 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1169 _src_query (GstAggregator * self, GstQuery * query)
1171 gboolean res = TRUE;
1173 switch (GST_QUERY_TYPE (query)) {
1174 case GST_QUERY_SEEKING:
1178 /* don't pass it along as some (file)sink might claim it does
1179 * whereas with a collectpads in between that will not likely work */
1180 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1181 gst_query_set_seeking (query, format, FALSE, 0, -1);
1186 case GST_QUERY_LATENCY:
1190 ret = gst_aggregator_query_latency (self, query);
1191 /* Wake up the src thread again, due to changed latencies
1192 * or changed live-ness we might have to adjust if we wait
1193 * on a deadline at all and how long.
1195 SRC_STREAM_BROADCAST (self);
1202 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1209 event_forward_func (GstPad * pad, EventData * evdata)
1211 gboolean ret = TRUE;
1212 GstPad *peer = gst_pad_get_peer (pad);
1213 GstAggregatorPadPrivate *padpriv = GST_AGGREGATOR_PAD (pad)->priv;
1216 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1217 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1218 gst_object_unref (peer);
1222 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1223 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1225 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1226 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1228 if (gst_pad_query (peer, seeking)) {
1231 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1233 if (seekable == FALSE) {
1234 GST_INFO_OBJECT (pad,
1235 "Source not seekable, We failed but it does not matter!");
1240 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1244 if (evdata->flush) {
1245 padpriv->pending_flush_start = FALSE;
1246 padpriv->pending_flush_stop = FALSE;
1249 evdata->one_actually_seeked = TRUE;
1252 evdata->result &= ret;
1254 /* Always send to all pads */
1259 _set_flush_pending (GstAggregator * self, GstAggregatorPad * pad,
1262 pad->priv->pending_flush_start = TRUE;
1263 pad->priv->pending_flush_stop = FALSE;
1269 _forward_event_to_all_sinkpads (GstAggregator * self, GstEvent * event,
1274 evdata.event = event;
1275 evdata.result = TRUE;
1276 evdata.flush = flush;
1277 evdata.one_actually_seeked = FALSE;
1279 /* We first need to set all pads as flushing in a first pass
1280 * as flush_start flush_stop is sometimes sent synchronously
1281 * while we send the seek event */
1283 gst_aggregator_iterate_sinkpads (self,
1284 (GstAggregatorPadForeachFunc) _set_flush_pending, NULL);
1285 gst_pad_forward (self->srcpad, (GstPadForwardFunction) event_forward_func,
1288 gst_event_unref (event);
1294 _do_seek (GstAggregator * self, GstEvent * event)
1299 GstSeekType start_type, stop_type;
1303 GstAggregatorPrivate *priv = self->priv;
1305 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1306 &start, &stop_type, &stop);
1308 GST_INFO_OBJECT (self, "starting SEEK");
1310 flush = flags & GST_SEEK_FLAG_FLUSH;
1313 g_atomic_int_set (&priv->pending_flush_start, TRUE);
1314 g_atomic_int_set (&priv->flush_seeking, TRUE);
1317 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1318 stop_type, stop, NULL);
1320 /* forward the seek upstream */
1321 evdata = _forward_event_to_all_sinkpads (self, event, flush);
1324 if (!evdata.result || !evdata.one_actually_seeked) {
1325 g_atomic_int_set (&priv->flush_seeking, FALSE);
1326 g_atomic_int_set (&priv->pending_flush_start, FALSE);
1329 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1331 return evdata.result;
1335 _src_event (GstAggregator * self, GstEvent * event)
1338 gboolean res = TRUE;
1340 switch (GST_EVENT_TYPE (event)) {
1341 case GST_EVENT_SEEK:
1343 gst_event_ref (event);
1344 res = _do_seek (self, event);
1345 gst_event_unref (event);
1349 case GST_EVENT_NAVIGATION:
1351 /* navigation is rather pointless. */
1353 gst_event_unref (event);
1362 evdata = _forward_event_to_all_sinkpads (self, event, FALSE);
1363 res = evdata.result;
1370 src_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
1372 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1374 return klass->src_event (GST_AGGREGATOR (parent), event);
1378 src_query_func (GstPad * pad, GstObject * parent, GstQuery * query)
1380 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1382 return klass->src_query (GST_AGGREGATOR (parent), query);
1386 src_activate_mode (GstPad * pad,
1387 GstObject * parent, GstPadMode mode, gboolean active)
1389 GstAggregator *self = GST_AGGREGATOR (parent);
1390 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1392 if (klass->src_activate) {
1393 if (klass->src_activate (self, mode, active) == FALSE) {
1398 if (active == TRUE) {
1400 case GST_PAD_MODE_PUSH:
1402 GST_INFO_OBJECT (pad, "Activating pad!");
1403 _start_srcpad_task (self);
1408 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1415 GST_INFO_OBJECT (self, "Deactivating srcpad");
1416 _stop_srcpad_task (self, FALSE);
1422 _sink_query (GstAggregator * self, GstAggregatorPad * aggpad, GstQuery * query)
1424 GstPad *pad = GST_PAD (aggpad);
1426 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1430 gst_aggregator_finalize (GObject * object)
1432 GstAggregator *self = (GstAggregator *) object;
1434 gst_object_unref (self->clock);
1435 g_mutex_clear (&self->priv->setcaps_lock);
1436 g_mutex_clear (&self->priv->src_lock);
1437 g_cond_clear (&self->priv->src_cond);
1439 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1443 gst_aggregator_dispose (GObject * object)
1445 G_OBJECT_CLASS (aggregator_parent_class)->dispose (object);
1449 * gst_aggregator_set_latency_property:
1450 * @agg: a #GstAggregator
1451 * @latency: the new latency value.
1453 * Sets the new latency value to @latency. This value is used to limit the
1454 * amount of time a pad waits for data to appear before considering the pad
1458 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1462 g_return_if_fail (GST_IS_AGGREGATOR (self));
1464 GST_OBJECT_LOCK (self);
1466 if (self->priv->latency_live && self->priv->latency_max != 0 &&
1467 GST_CLOCK_TIME_IS_VALID (latency) && latency > self->priv->latency_max) {
1468 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1469 ("%s", "Latency too big"),
1470 ("The requested latency value is too big for the latency in the "
1471 "current pipeline. Limiting to %" G_GINT64_FORMAT,
1472 self->priv->latency_max));
1473 latency = self->priv->latency_max;
1476 changed = self->latency != latency;
1477 self->latency = latency;
1478 GST_OBJECT_UNLOCK (self);
1481 gst_element_post_message (GST_ELEMENT_CAST (self),
1482 gst_message_new_latency (GST_OBJECT_CAST (self)));
1486 * gst_aggregator_get_latency_property:
1487 * @agg: a #GstAggregator
1489 * Gets the latency value. See gst_aggregator_set_latency for
1492 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1493 * before a pad is deemed unresponsive. A value of -1 means an
1497 gst_aggregator_get_latency_property (GstAggregator * agg)
1501 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1503 GST_OBJECT_LOCK (agg);
1505 GST_OBJECT_UNLOCK (agg);
1511 gst_aggregator_set_property (GObject * object, guint prop_id,
1512 const GValue * value, GParamSpec * pspec)
1514 GstAggregator *agg = GST_AGGREGATOR (object);
1518 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1521 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1527 gst_aggregator_get_property (GObject * object, guint prop_id,
1528 GValue * value, GParamSpec * pspec)
1530 GstAggregator *agg = GST_AGGREGATOR (object);
1534 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1537 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1542 /* GObject vmethods implementations */
1544 gst_aggregator_class_init (GstAggregatorClass * klass)
1546 GObjectClass *gobject_class = (GObjectClass *) klass;
1547 GstElementClass *gstelement_class = (GstElementClass *) klass;
1549 aggregator_parent_class = g_type_class_peek_parent (klass);
1550 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1552 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1553 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1555 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1556 klass->start = _start;
1557 klass->stop = _stop;
1559 klass->sink_event = _sink_event;
1560 klass->sink_query = _sink_query;
1562 klass->src_event = _src_event;
1563 klass->src_query = _src_query;
1565 gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR (_request_new_pad);
1566 gstelement_class->send_event = GST_DEBUG_FUNCPTR (_send_event);
1567 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (_release_pad);
1568 gstelement_class->change_state = GST_DEBUG_FUNCPTR (_change_state);
1570 gobject_class->set_property = gst_aggregator_set_property;
1571 gobject_class->get_property = gst_aggregator_get_property;
1572 gobject_class->finalize = gst_aggregator_finalize;
1573 gobject_class->dispose = gst_aggregator_dispose;
1575 g_object_class_install_property (gobject_class, PROP_LATENCY,
1576 g_param_spec_int64 ("latency", "Buffer latency",
1577 "Additional latency in live mode to allow upstream "
1578 "to take longer to produce buffers for the current "
1580 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1581 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1585 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1587 GstPadTemplate *pad_template;
1588 GstAggregatorPrivate *priv;
1590 g_return_if_fail (klass->aggregate != NULL);
1593 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1594 GstAggregatorPrivate);
1599 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1600 g_return_if_fail (pad_template != NULL);
1602 priv->padcount = -1;
1603 priv->tags_changed = FALSE;
1605 self->priv->latency_live = FALSE;
1606 self->priv->latency_min = self->priv->sub_latency_min = 0;
1607 self->priv->latency_max = self->priv->sub_latency_max = GST_CLOCK_TIME_NONE;
1608 _reset_flow_values (self);
1610 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1612 gst_pad_set_event_function (self->srcpad,
1613 GST_DEBUG_FUNCPTR ((GstPadEventFunction) src_event_func));
1614 gst_pad_set_query_function (self->srcpad,
1615 GST_DEBUG_FUNCPTR ((GstPadQueryFunction) src_query_func));
1616 gst_pad_set_activatemode_function (self->srcpad,
1617 GST_DEBUG_FUNCPTR ((GstPadActivateModeFunction) src_activate_mode));
1619 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1621 self->clock = gst_system_clock_obtain ();
1624 g_mutex_init (&self->priv->setcaps_lock);
1625 g_mutex_init (&self->priv->src_lock);
1626 g_cond_init (&self->priv->src_cond);
1629 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1630 * method to get to the padtemplates */
1632 gst_aggregator_get_type (void)
1634 static volatile gsize type = 0;
1636 if (g_once_init_enter (&type)) {
1638 static const GTypeInfo info = {
1639 sizeof (GstAggregatorClass),
1642 (GClassInitFunc) gst_aggregator_class_init,
1645 sizeof (GstAggregator),
1647 (GInstanceInitFunc) gst_aggregator_init,
1650 _type = g_type_register_static (GST_TYPE_ELEMENT,
1651 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1652 g_once_init_leave (&type, _type);
1657 static GstFlowReturn
1658 _chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1660 GstBuffer *actual_buf = buffer;
1661 GstAggregator *self = GST_AGGREGATOR (object);
1662 GstAggregatorPrivate *priv = self->priv;
1663 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1664 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1666 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1668 PAD_STREAM_LOCK (aggpad);
1670 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1673 if (g_atomic_int_get (&aggpad->priv->pending_eos) == TRUE)
1676 PAD_LOCK_EVENT (aggpad);
1678 while (aggpad->buffer && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1679 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1680 PAD_WAIT_EVENT (aggpad);
1682 PAD_UNLOCK_EVENT (aggpad);
1684 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1687 if (aggclass->clip) {
1688 aggclass->clip (self, aggpad, buffer, &actual_buf);
1691 PAD_LOCK_EVENT (aggpad);
1693 gst_buffer_unref (aggpad->buffer);
1694 aggpad->buffer = actual_buf;
1695 PAD_UNLOCK_EVENT (aggpad);
1696 PAD_STREAM_UNLOCK (aggpad);
1698 SRC_STREAM_LOCK (self);
1699 if (gst_aggregator_iterate_sinkpads (self,
1700 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos, NULL))
1701 SRC_STREAM_BROADCAST_UNLOCKED (self);
1702 SRC_STREAM_UNLOCK (self);
1704 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1706 return priv->flow_return;
1709 PAD_STREAM_UNLOCK (aggpad);
1711 gst_buffer_unref (buffer);
1712 GST_DEBUG_OBJECT (aggpad, "We are flushing");
1714 return GST_FLOW_FLUSHING;
1717 PAD_STREAM_UNLOCK (aggpad);
1719 gst_buffer_unref (buffer);
1720 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1722 return GST_FLOW_EOS;
1726 pad_query_func (GstPad * pad, GstObject * parent, GstQuery * query)
1728 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1729 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1731 if (GST_QUERY_IS_SERIALIZED (query)) {
1732 PAD_LOCK_EVENT (aggpad);
1734 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
1735 PAD_UNLOCK_EVENT (aggpad);
1739 while (aggpad->buffer
1740 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1741 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1742 PAD_WAIT_EVENT (aggpad);
1744 PAD_UNLOCK_EVENT (aggpad);
1746 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1750 return klass->sink_query (GST_AGGREGATOR (parent),
1751 GST_AGGREGATOR_PAD (pad), query);
1754 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping query");
1759 pad_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
1761 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1762 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1764 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1765 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1766 PAD_LOCK_EVENT (aggpad);
1768 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1769 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
1770 PAD_UNLOCK_EVENT (aggpad);
1774 while (aggpad->buffer
1775 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1776 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1777 PAD_WAIT_EVENT (aggpad);
1779 PAD_UNLOCK_EVENT (aggpad);
1781 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1782 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1786 return klass->sink_event (GST_AGGREGATOR (parent),
1787 GST_AGGREGATOR_PAD (pad), event);
1790 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping event");
1791 if (GST_EVENT_IS_STICKY (event))
1792 gst_pad_store_sticky_event (pad, event);
1793 gst_event_unref (event);
1798 pad_activate_mode_func (GstPad * pad,
1799 GstObject * parent, GstPadMode mode, gboolean active)
1801 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1803 if (active == FALSE) {
1804 PAD_LOCK_EVENT (aggpad);
1805 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1806 gst_buffer_replace (&aggpad->buffer, NULL);
1807 PAD_BROADCAST_EVENT (aggpad);
1808 PAD_UNLOCK_EVENT (aggpad);
1810 g_atomic_int_set (&aggpad->priv->flushing, FALSE);
1811 PAD_LOCK_EVENT (aggpad);
1812 PAD_BROADCAST_EVENT (aggpad);
1813 PAD_UNLOCK_EVENT (aggpad);
1819 /***********************************
1820 * GstAggregatorPad implementation *
1821 ************************************/
1822 static GstPadClass *aggregator_pad_parent_class = NULL;
1823 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1826 _pad_constructed (GObject * object)
1828 GstPad *pad = GST_PAD (object);
1830 gst_pad_set_chain_function (pad,
1831 GST_DEBUG_FUNCPTR ((GstPadChainFunction) _chain));
1832 gst_pad_set_event_function (pad,
1833 GST_DEBUG_FUNCPTR ((GstPadEventFunction) pad_event_func));
1834 gst_pad_set_query_function (pad,
1835 GST_DEBUG_FUNCPTR ((GstPadQueryFunction) pad_query_func));
1836 gst_pad_set_activatemode_function (pad,
1837 GST_DEBUG_FUNCPTR ((GstPadActivateModeFunction) pad_activate_mode_func));
1841 gst_aggregator_pad_finalize (GObject * object)
1843 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1845 g_mutex_clear (&pad->priv->event_lock);
1846 g_cond_clear (&pad->priv->event_cond);
1847 g_mutex_clear (&pad->priv->stream_lock);
1849 G_OBJECT_CLASS (aggregator_pad_parent_class)->finalize (object);
1853 gst_aggregator_pad_dispose (GObject * object)
1855 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1858 buf = gst_aggregator_pad_steal_buffer (pad);
1860 gst_buffer_unref (buf);
1862 G_OBJECT_CLASS (aggregator_pad_parent_class)->dispose (object);
1866 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
1868 GObjectClass *gobject_class = (GObjectClass *) klass;
1870 aggregator_pad_parent_class = g_type_class_peek_parent (klass);
1871 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
1873 gobject_class->constructed = GST_DEBUG_FUNCPTR (_pad_constructed);
1874 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_aggregator_pad_finalize);
1875 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_aggregator_pad_dispose);
1879 gst_aggregator_pad_init (GstAggregatorPad * pad)
1882 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
1883 GstAggregatorPadPrivate);
1886 g_mutex_init (&pad->priv->event_lock);
1887 g_cond_init (&pad->priv->event_cond);
1889 g_mutex_init (&pad->priv->stream_lock);
1893 * gst_aggregator_pad_steal_buffer:
1894 * @pad: the pad to get buffer from
1896 * Steal the ref to the buffer currently queued in @pad.
1898 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
1899 * queued. You should unref the buffer after usage.
1902 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
1904 GstBuffer *buffer = NULL;
1906 PAD_LOCK_EVENT (pad);
1908 GST_TRACE_OBJECT (pad, "Consuming buffer");
1909 buffer = pad->buffer;
1911 if (pad->priv->pending_eos) {
1912 pad->priv->pending_eos = FALSE;
1915 PAD_BROADCAST_EVENT (pad);
1916 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
1918 PAD_UNLOCK_EVENT (pad);
1924 * gst_aggregator_pad_get_buffer:
1925 * @pad: the pad to get buffer from
1927 * Returns: (transfer full): A reference to the buffer in @pad or
1928 * NULL if no buffer was queued. You should unref the buffer after
1932 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
1934 GstBuffer *buffer = NULL;
1936 PAD_LOCK_EVENT (pad);
1938 buffer = gst_buffer_ref (pad->buffer);
1939 PAD_UNLOCK_EVENT (pad);
1945 * gst_aggregator_merge_tags:
1946 * @self: a #GstAggregator
1947 * @tags: a #GstTagList to merge
1948 * @mode: the #GstTagMergeMode to use
1950 * Adds tags to so-called pending tags, which will be processed
1951 * before pushing out data downstream.
1953 * Note that this is provided for convenience, and the subclass is
1954 * not required to use this and can still do tag handling on its own.
1959 gst_aggregator_merge_tags (GstAggregator * self,
1960 const GstTagList * tags, GstTagMergeMode mode)
1964 g_return_if_fail (GST_IS_AGGREGATOR (self));
1965 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
1967 /* FIXME Check if we can use OBJECT lock here! */
1968 GST_OBJECT_LOCK (self);
1970 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
1971 otags = self->priv->tags;
1972 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
1974 gst_tag_list_unref (otags);
1975 self->priv->tags_changed = TRUE;
1976 GST_OBJECT_UNLOCK (self);
1980 * gst_aggregator_set_latency:
1981 * @self: a #GstAggregator
1982 * @min_latency: minimum latency
1983 * @max_latency: maximum latency
1985 * Lets #GstAggregator sub-classes tell the baseclass what their internal
1986 * latency is. Will also post a LATENCY message on the bus so the pipeline
1987 * can reconfigure its global latency.
1990 gst_aggregator_set_latency (GstAggregator * self,
1991 GstClockTime min_latency, GstClockTime max_latency)
1993 g_return_if_fail (GST_IS_AGGREGATOR (self));
1994 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
1995 g_return_if_fail (max_latency >= min_latency);
1997 GST_OBJECT_LOCK (self);
1998 self->priv->sub_latency_min = min_latency;
1999 self->priv->sub_latency_max = max_latency;
2000 GST_OBJECT_UNLOCK (self);
2002 gst_element_post_message (GST_ELEMENT_CAST (self),
2003 gst_message_new_latency (GST_OBJECT_CAST (self)));