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_max)) {
544 } else if (GST_CLOCK_TIME_IS_VALID (latency_min)) {
547 time += self->latency;
550 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
551 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
552 " latency max %" GST_TIME_FORMAT " latency min %" GST_TIME_FORMAT
553 " current %" GST_TIME_FORMAT ")", GST_TIME_ARGS (time),
554 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
555 GST_TIME_ARGS (start), GST_TIME_ARGS (latency_max),
556 GST_TIME_ARGS (latency_min),
557 GST_TIME_ARGS (gst_clock_get_time (clock)));
559 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
560 gst_object_unref (clock);
561 SRC_STREAM_UNLOCK (self);
563 status = gst_clock_id_wait (self->priv->aggregate_id, NULL);
565 SRC_STREAM_LOCK (self);
566 if (self->priv->aggregate_id) {
567 gst_clock_id_unref (self->priv->aggregate_id);
568 self->priv->aggregate_id = NULL;
571 GST_DEBUG_OBJECT (self, "clock returned %d", status);
574 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
575 SRC_STREAM_UNLOCK (self);
581 res = gst_aggregator_iterate_sinkpads (self,
582 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos, NULL);
583 SRC_STREAM_UNLOCK (self);
589 aggregate_func (GstAggregator * self)
591 GstAggregatorPrivate *priv = self->priv;
592 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
593 gboolean timeout = FALSE;
595 if (self->priv->running == FALSE) {
596 GST_DEBUG_OBJECT (self, "Not running anymore");
600 GST_LOG_OBJECT (self, "Checking aggregate");
601 while (priv->send_eos && priv->running) {
602 if (!_wait_and_check (self, &timeout))
605 GST_TRACE_OBJECT (self, "Actually aggregating!");
607 priv->flow_return = klass->aggregate (self, timeout);
609 if (priv->flow_return == GST_FLOW_EOS) {
613 if (priv->flow_return == GST_FLOW_FLUSHING &&
614 g_atomic_int_get (&priv->flush_seeking))
615 priv->flow_return = GST_FLOW_OK;
617 GST_LOG_OBJECT (self, "flow return is %s",
618 gst_flow_get_name (priv->flow_return));
620 if (priv->flow_return != GST_FLOW_OK)
626 _start (GstAggregator * self)
628 self->priv->running = TRUE;
629 self->priv->send_stream_start = TRUE;
630 self->priv->send_segment = TRUE;
631 self->priv->send_eos = TRUE;
632 self->priv->srccaps = NULL;
633 self->priv->flow_return = GST_FLOW_OK;
639 _check_pending_flush_stop (GstAggregatorPad * pad)
641 return (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
645 _stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
649 GST_INFO_OBJECT (self, "%s srcpad task",
650 flush_start ? "Pausing" : "Stopping");
652 self->priv->running = FALSE;
653 SRC_STREAM_BROADCAST (self);
656 res = gst_pad_push_event (self->srcpad, flush_start);
659 gst_pad_stop_task (self->srcpad);
660 SRC_STREAM_BROADCAST (self);
666 _start_srcpad_task (GstAggregator * self)
668 GST_INFO_OBJECT (self, "Starting srcpad task");
670 self->priv->running = TRUE;
671 gst_pad_start_task (GST_PAD (self->srcpad),
672 (GstTaskFunction) aggregate_func, self, NULL);
676 _flush (GstAggregator * self)
678 GstFlowReturn ret = GST_FLOW_OK;
679 GstAggregatorPrivate *priv = self->priv;
680 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
682 GST_DEBUG_OBJECT (self, "Flushing everything");
683 g_atomic_int_set (&priv->send_segment, TRUE);
684 g_atomic_int_set (&priv->flush_seeking, FALSE);
685 g_atomic_int_set (&priv->tags_changed, FALSE);
687 ret = klass->flush (self);
693 _all_flush_stop_received (GstAggregator * self)
696 GstAggregatorPad *tmppad;
698 GST_OBJECT_LOCK (self);
699 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
700 tmppad = (GstAggregatorPad *) tmp->data;
702 if (_check_pending_flush_stop (tmppad) == FALSE) {
703 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
704 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
705 GST_OBJECT_UNLOCK (self);
709 GST_OBJECT_UNLOCK (self);
715 _flush_start (GstAggregator * self, GstAggregatorPad * aggpad, GstEvent * event)
718 GstAggregatorPrivate *priv = self->priv;
719 GstAggregatorPadPrivate *padpriv = aggpad->priv;
721 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
722 /* Remove pad buffer and wake up the streaming thread */
723 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
724 gst_buffer_replace (&tmpbuf, NULL);
725 PAD_STREAM_LOCK (aggpad);
726 if (g_atomic_int_compare_and_exchange (&padpriv->pending_flush_start,
727 TRUE, FALSE) == TRUE) {
728 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
729 g_atomic_int_set (&padpriv->pending_flush_stop, TRUE);
732 if (g_atomic_int_get (&priv->flush_seeking)) {
733 /* If flush_seeking we forward the first FLUSH_START */
734 if (g_atomic_int_compare_and_exchange (&priv->pending_flush_start,
735 TRUE, FALSE) == TRUE) {
737 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
738 _stop_srcpad_task (self, event);
739 priv->flow_return = GST_FLOW_OK;
741 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
742 GST_PAD_STREAM_LOCK (self->srcpad);
743 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
746 gst_event_unref (event);
749 gst_event_unref (event);
751 PAD_STREAM_UNLOCK (aggpad);
753 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
754 gst_buffer_replace (&tmpbuf, NULL);
757 /* GstAggregator vmethods default implementations */
759 _sink_event (GstAggregator * self, GstAggregatorPad * aggpad, GstEvent * event)
762 GstPad *pad = GST_PAD (aggpad);
763 GstAggregatorPrivate *priv = self->priv;
765 switch (GST_EVENT_TYPE (event)) {
766 case GST_EVENT_FLUSH_START:
768 _flush_start (self, aggpad, event);
769 /* We forward only in one case: right after flush_seeking */
773 case GST_EVENT_FLUSH_STOP:
775 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
777 _aggpad_flush (aggpad, self);
778 if (g_atomic_int_get (&priv->flush_seeking)) {
779 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
781 if (g_atomic_int_get (&priv->flush_seeking)) {
782 if (_all_flush_stop_received (self)) {
783 /* That means we received FLUSH_STOP/FLUSH_STOP on
784 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
786 gst_pad_push_event (self->srcpad, event);
787 priv->send_eos = TRUE;
789 SRC_STREAM_BROADCAST (self);
791 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
792 GST_PAD_STREAM_UNLOCK (self->srcpad);
793 _start_srcpad_task (self);
798 /* We never forward the event */
803 GST_DEBUG_OBJECT (aggpad, "EOS");
805 /* We still have a buffer, and we don't want the subclass to have to
806 * check for it. Mark pending_eos, eos will be set when steal_buffer is
809 PAD_LOCK_EVENT (aggpad);
810 if (!aggpad->buffer) {
813 aggpad->priv->pending_eos = TRUE;
815 PAD_UNLOCK_EVENT (aggpad);
817 SRC_STREAM_BROADCAST (self);
820 case GST_EVENT_SEGMENT:
822 PAD_LOCK_EVENT (aggpad);
823 gst_event_copy_segment (event, &aggpad->segment);
824 self->priv->seqnum = gst_event_get_seqnum (event);
825 PAD_UNLOCK_EVENT (aggpad);
828 case GST_EVENT_STREAM_START:
836 gst_event_parse_tag (event, &tags);
838 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
839 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
840 gst_event_unref (event);
852 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
853 return gst_pad_event_default (pad, GST_OBJECT (self), event);
856 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
858 gst_event_unref (event);
864 _stop_pad (GstAggregator * self, GstAggregatorPad * pad, gpointer unused_udata)
866 _aggpad_flush (pad, self);
872 _stop (GstAggregator * agg)
874 _reset_flow_values (agg);
876 gst_aggregator_iterate_sinkpads (agg,
877 (GstAggregatorPadForeachFunc) _stop_pad, NULL);
880 gst_tag_list_unref (agg->priv->tags);
881 agg->priv->tags = NULL;
886 /* GstElement vmethods implementations */
887 static GstStateChangeReturn
888 _change_state (GstElement * element, GstStateChange transition)
890 GstStateChangeReturn ret;
891 GstAggregator *self = GST_AGGREGATOR (element);
892 GstAggregatorClass *agg_class = GST_AGGREGATOR_GET_CLASS (self);
895 switch (transition) {
896 case GST_STATE_CHANGE_READY_TO_PAUSED:
897 agg_class->start (self);
904 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
905 transition)) == GST_STATE_CHANGE_FAILURE)
909 switch (transition) {
910 case GST_STATE_CHANGE_PAUSED_TO_READY:
911 agg_class->stop (self);
921 GST_ERROR_OBJECT (element, "parent failed state change");
927 _release_pad (GstElement * element, GstPad * pad)
929 GstAggregator *self = GST_AGGREGATOR (element);
932 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
934 GST_INFO_OBJECT (pad, "Removing pad");
936 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
937 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
938 gst_buffer_replace (&tmpbuf, NULL);
939 gst_element_remove_pad (element, pad);
941 SRC_STREAM_BROADCAST (self);
945 _request_new_pad (GstElement * element,
946 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
949 GstAggregatorPad *agg_pad;
951 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
952 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
954 self = GST_AGGREGATOR (element);
956 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
960 GST_OBJECT_LOCK (element);
961 if (req_name == NULL || strlen (req_name) < 6
962 || !g_str_has_prefix (req_name, "sink_")) {
963 /* no name given when requesting the pad, use next available int */
966 /* parse serial number from requested padname */
967 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
968 if (serial >= priv->padcount)
969 priv->padcount = serial;
972 name = g_strdup_printf ("sink_%u", priv->padcount);
973 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
974 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
977 GST_OBJECT_UNLOCK (element);
983 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
986 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
988 /* add the pad to the element */
989 gst_element_add_pad (element, GST_PAD (agg_pad));
991 return GST_PAD (agg_pad);
996 GstClockTime min, max;
1001 _latency_query (GstAggregator * self, GstPad * pad, gpointer user_data)
1003 LatencyData *data = user_data;
1004 GstClockTime min, max;
1008 query = gst_query_new_latency ();
1009 res = gst_pad_peer_query (pad, query);
1012 gst_query_parse_latency (query, &live, &min, &max);
1014 GST_LOG_OBJECT (self, "%s: got latency live:%s min:%" G_GINT64_FORMAT
1015 " max:%" G_GINT64_FORMAT, GST_PAD_NAME (pad),
1016 live ? "true" : "false", min, max);
1018 if (min > data->min)
1021 if (max != GST_CLOCK_TIME_NONE &&
1022 ((data->max != GST_CLOCK_TIME_NONE && max < data->max) ||
1023 (data->max == GST_CLOCK_TIME_NONE)))
1029 gst_query_unref (query);
1035 * gst_aggregator_get_latency:
1036 * @self: a #GstAggregator
1037 * @live: (out) (allow-none): whether @self is live
1038 * @min_latency: (out) (allow-none): the configured minimum latency of @self
1039 * @max_latency: (out) (allow-none): the configured maximum latency of @self
1041 * Retreives the latency values reported by @self in response to the latency
1044 * Typically only called by subclasses.
1047 gst_aggregator_get_latency (GstAggregator * self, gboolean * live,
1048 GstClockTime * min_latency, GstClockTime * max_latency)
1050 GstClockTime min, max;
1052 g_return_if_fail (GST_IS_AGGREGATOR (self));
1054 min = self->priv->latency_min;
1055 max = self->priv->latency_max;
1057 min += self->priv->sub_latency_min;
1058 if (GST_CLOCK_TIME_IS_VALID (max)
1059 && GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max))
1060 max += self->priv->sub_latency_max;
1062 if (GST_CLOCK_TIME_IS_VALID (self->latency)) {
1063 min += self->latency;
1064 if (GST_CLOCK_TIME_IS_VALID (max))
1065 max += self->latency;
1069 *live = self->priv->latency_live;
1077 gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
1082 data.max = GST_CLOCK_TIME_NONE;
1085 /* query upstream's latency */
1086 SRC_STREAM_LOCK (self);
1087 gst_aggregator_iterate_sinkpads (self,
1088 (GstAggregatorPadForeachFunc) _latency_query, &data);
1089 SRC_STREAM_UNLOCK (self);
1091 if (data.live && GST_CLOCK_TIME_IS_VALID (self->latency) &&
1092 self->latency > data.max) {
1093 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1094 ("%s", "Latency too big"),
1095 ("The requested latency value is too big for the current pipeline. "
1096 "Limiting to %" G_GINT64_FORMAT, data.max));
1097 self->latency = data.max;
1100 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (data.min))) {
1101 GST_WARNING_OBJECT (self, "Invalid minimum latency, using 0");
1105 self->priv->latency_live = data.live;
1106 self->priv->latency_min = data.min;
1107 self->priv->latency_max = data.max;
1110 if (GST_CLOCK_TIME_IS_VALID (self->latency)) {
1111 if (GST_CLOCK_TIME_IS_VALID (data.min))
1112 data.min += self->latency;
1113 if (GST_CLOCK_TIME_IS_VALID (data.max))
1114 data.max += self->latency;
1117 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_min)
1118 && GST_CLOCK_TIME_IS_VALID (data.min))
1119 data.min += self->priv->sub_latency_min;
1120 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1121 && GST_CLOCK_TIME_IS_VALID (data.max))
1122 data.max += self->priv->sub_latency_max;
1124 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1125 " max:%" G_GINT64_FORMAT, data.live ? "true" : "false", data.min,
1128 gst_query_set_latency (query, data.live, data.min, data.max);
1134 _send_event (GstElement * element, GstEvent * event)
1136 GstAggregator *self = GST_AGGREGATOR (element);
1138 GST_STATE_LOCK (element);
1139 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1140 GST_STATE (element) < GST_STATE_PAUSED) {
1144 GstSeekType start_type, stop_type;
1147 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1148 &start, &stop_type, &stop);
1149 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1150 stop_type, stop, NULL);
1152 self->priv->seqnum = gst_event_get_seqnum (event);
1153 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1155 GST_STATE_UNLOCK (element);
1158 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1163 _src_query (GstAggregator * self, GstQuery * query)
1165 gboolean res = TRUE;
1167 switch (GST_QUERY_TYPE (query)) {
1168 case GST_QUERY_SEEKING:
1172 /* don't pass it along as some (file)sink might claim it does
1173 * whereas with a collectpads in between that will not likely work */
1174 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1175 gst_query_set_seeking (query, format, FALSE, 0, -1);
1180 case GST_QUERY_LATENCY:
1184 ret = gst_aggregator_query_latency (self, query);
1185 /* Wake up the src thread again, due to changed latencies
1186 * or changed live-ness we might have to adjust if we wait
1187 * on a deadline at all and how long.
1189 SRC_STREAM_BROADCAST (self);
1196 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1203 event_forward_func (GstPad * pad, EventData * evdata)
1205 gboolean ret = TRUE;
1206 GstPad *peer = gst_pad_get_peer (pad);
1207 GstAggregatorPadPrivate *padpriv = GST_AGGREGATOR_PAD (pad)->priv;
1210 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1211 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1212 gst_object_unref (peer);
1216 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1217 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1219 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1220 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1222 if (gst_pad_query (peer, seeking)) {
1225 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1227 if (seekable == FALSE) {
1228 GST_INFO_OBJECT (pad,
1229 "Source not seekable, We failed but it does not matter!");
1234 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1238 if (evdata->flush) {
1239 padpriv->pending_flush_start = FALSE;
1240 padpriv->pending_flush_stop = FALSE;
1243 evdata->one_actually_seeked = TRUE;
1246 evdata->result &= ret;
1248 /* Always send to all pads */
1253 _set_flush_pending (GstAggregator * self, GstAggregatorPad * pad,
1256 pad->priv->pending_flush_start = TRUE;
1257 pad->priv->pending_flush_stop = FALSE;
1263 _forward_event_to_all_sinkpads (GstAggregator * self, GstEvent * event,
1268 evdata.event = event;
1269 evdata.result = TRUE;
1270 evdata.flush = flush;
1271 evdata.one_actually_seeked = FALSE;
1273 /* We first need to set all pads as flushing in a first pass
1274 * as flush_start flush_stop is sometimes sent synchronously
1275 * while we send the seek event */
1277 gst_aggregator_iterate_sinkpads (self,
1278 (GstAggregatorPadForeachFunc) _set_flush_pending, NULL);
1279 gst_pad_forward (self->srcpad, (GstPadForwardFunction) event_forward_func,
1282 gst_event_unref (event);
1288 _do_seek (GstAggregator * self, GstEvent * event)
1293 GstSeekType start_type, stop_type;
1297 GstAggregatorPrivate *priv = self->priv;
1299 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1300 &start, &stop_type, &stop);
1302 GST_INFO_OBJECT (self, "starting SEEK");
1304 flush = flags & GST_SEEK_FLAG_FLUSH;
1307 g_atomic_int_set (&priv->pending_flush_start, TRUE);
1308 g_atomic_int_set (&priv->flush_seeking, TRUE);
1311 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1312 stop_type, stop, NULL);
1314 /* forward the seek upstream */
1315 evdata = _forward_event_to_all_sinkpads (self, event, flush);
1318 if (!evdata.result || !evdata.one_actually_seeked) {
1319 g_atomic_int_set (&priv->flush_seeking, FALSE);
1320 g_atomic_int_set (&priv->pending_flush_start, FALSE);
1323 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1325 return evdata.result;
1329 _src_event (GstAggregator * self, GstEvent * event)
1332 gboolean res = TRUE;
1334 switch (GST_EVENT_TYPE (event)) {
1335 case GST_EVENT_SEEK:
1337 gst_event_ref (event);
1338 res = _do_seek (self, event);
1339 gst_event_unref (event);
1343 case GST_EVENT_NAVIGATION:
1345 /* navigation is rather pointless. */
1347 gst_event_unref (event);
1356 evdata = _forward_event_to_all_sinkpads (self, event, FALSE);
1357 res = evdata.result;
1364 src_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
1366 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1368 return klass->src_event (GST_AGGREGATOR (parent), event);
1372 src_query_func (GstPad * pad, GstObject * parent, GstQuery * query)
1374 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1376 return klass->src_query (GST_AGGREGATOR (parent), query);
1380 src_activate_mode (GstPad * pad,
1381 GstObject * parent, GstPadMode mode, gboolean active)
1383 GstAggregator *self = GST_AGGREGATOR (parent);
1384 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1386 if (klass->src_activate) {
1387 if (klass->src_activate (self, mode, active) == FALSE) {
1392 if (active == TRUE) {
1394 case GST_PAD_MODE_PUSH:
1396 GST_INFO_OBJECT (pad, "Activating pad!");
1397 _start_srcpad_task (self);
1402 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1409 GST_INFO_OBJECT (self, "Deactivating srcpad");
1410 _stop_srcpad_task (self, FALSE);
1416 _sink_query (GstAggregator * self, GstAggregatorPad * aggpad, GstQuery * query)
1418 GstPad *pad = GST_PAD (aggpad);
1420 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1424 gst_aggregator_finalize (GObject * object)
1426 GstAggregator *self = (GstAggregator *) object;
1428 gst_object_unref (self->clock);
1429 g_mutex_clear (&self->priv->setcaps_lock);
1430 g_mutex_clear (&self->priv->src_lock);
1431 g_cond_clear (&self->priv->src_cond);
1433 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1437 gst_aggregator_dispose (GObject * object)
1439 G_OBJECT_CLASS (aggregator_parent_class)->dispose (object);
1443 * gst_aggregator_set_latency_property:
1444 * @agg: a #GstAggregator
1445 * @latency: the new latency value.
1447 * Sets the new latency value to @latency. This value is used to limit the
1448 * amount of time a pad waits for data to appear before considering the pad
1452 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1456 g_return_if_fail (GST_IS_AGGREGATOR (self));
1458 GST_OBJECT_LOCK (self);
1460 if (self->priv->latency_live && self->priv->latency_max != 0 &&
1461 GST_CLOCK_TIME_IS_VALID (latency) && latency > self->priv->latency_max) {
1462 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1463 ("%s", "Latency too big"),
1464 ("The requested latency value is too big for the latency in the "
1465 "current pipeline. Limiting to %" G_GINT64_FORMAT,
1466 self->priv->latency_max));
1467 latency = self->priv->latency_max;
1470 changed = self->latency != latency;
1471 self->latency = latency;
1472 GST_OBJECT_UNLOCK (self);
1475 gst_element_post_message (GST_ELEMENT_CAST (self),
1476 gst_message_new_latency (GST_OBJECT_CAST (self)));
1480 * gst_aggregator_get_latency_property:
1481 * @agg: a #GstAggregator
1483 * Gets the latency value. See gst_aggregator_set_latency for
1486 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1487 * before a pad is deemed unresponsive. A value of -1 means an
1491 gst_aggregator_get_latency_property (GstAggregator * agg)
1495 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1497 GST_OBJECT_LOCK (agg);
1499 GST_OBJECT_UNLOCK (agg);
1505 gst_aggregator_set_property (GObject * object, guint prop_id,
1506 const GValue * value, GParamSpec * pspec)
1508 GstAggregator *agg = GST_AGGREGATOR (object);
1512 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1515 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1521 gst_aggregator_get_property (GObject * object, guint prop_id,
1522 GValue * value, GParamSpec * pspec)
1524 GstAggregator *agg = GST_AGGREGATOR (object);
1528 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1531 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1536 /* GObject vmethods implementations */
1538 gst_aggregator_class_init (GstAggregatorClass * klass)
1540 GObjectClass *gobject_class = (GObjectClass *) klass;
1541 GstElementClass *gstelement_class = (GstElementClass *) klass;
1543 aggregator_parent_class = g_type_class_peek_parent (klass);
1544 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1546 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1547 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1549 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1550 klass->start = _start;
1551 klass->stop = _stop;
1553 klass->sink_event = _sink_event;
1554 klass->sink_query = _sink_query;
1556 klass->src_event = _src_event;
1557 klass->src_query = _src_query;
1559 gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR (_request_new_pad);
1560 gstelement_class->send_event = GST_DEBUG_FUNCPTR (_send_event);
1561 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (_release_pad);
1562 gstelement_class->change_state = GST_DEBUG_FUNCPTR (_change_state);
1564 gobject_class->set_property = gst_aggregator_set_property;
1565 gobject_class->get_property = gst_aggregator_get_property;
1566 gobject_class->finalize = gst_aggregator_finalize;
1567 gobject_class->dispose = gst_aggregator_dispose;
1569 g_object_class_install_property (gobject_class, PROP_LATENCY,
1570 g_param_spec_int64 ("latency", "Buffer latency",
1571 "Additional latency in live mode to allow upstream "
1572 "to take longer to produce buffers for the current "
1574 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1575 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1579 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1581 GstPadTemplate *pad_template;
1582 GstAggregatorPrivate *priv;
1584 g_return_if_fail (klass->aggregate != NULL);
1587 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1588 GstAggregatorPrivate);
1593 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1594 g_return_if_fail (pad_template != NULL);
1596 priv->padcount = -1;
1597 priv->tags_changed = FALSE;
1599 self->priv->latency_live = FALSE;
1600 self->priv->latency_min = self->priv->sub_latency_min = 0;
1601 self->priv->latency_max = self->priv->sub_latency_max = GST_CLOCK_TIME_NONE;
1602 _reset_flow_values (self);
1604 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1606 gst_pad_set_event_function (self->srcpad,
1607 GST_DEBUG_FUNCPTR ((GstPadEventFunction) src_event_func));
1608 gst_pad_set_query_function (self->srcpad,
1609 GST_DEBUG_FUNCPTR ((GstPadQueryFunction) src_query_func));
1610 gst_pad_set_activatemode_function (self->srcpad,
1611 GST_DEBUG_FUNCPTR ((GstPadActivateModeFunction) src_activate_mode));
1613 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1615 self->clock = gst_system_clock_obtain ();
1618 g_mutex_init (&self->priv->setcaps_lock);
1619 g_mutex_init (&self->priv->src_lock);
1620 g_cond_init (&self->priv->src_cond);
1623 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1624 * method to get to the padtemplates */
1626 gst_aggregator_get_type (void)
1628 static volatile gsize type = 0;
1630 if (g_once_init_enter (&type)) {
1632 static const GTypeInfo info = {
1633 sizeof (GstAggregatorClass),
1636 (GClassInitFunc) gst_aggregator_class_init,
1639 sizeof (GstAggregator),
1641 (GInstanceInitFunc) gst_aggregator_init,
1644 _type = g_type_register_static (GST_TYPE_ELEMENT,
1645 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1646 g_once_init_leave (&type, _type);
1651 static GstFlowReturn
1652 _chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1654 GstBuffer *actual_buf = buffer;
1655 GstAggregator *self = GST_AGGREGATOR (object);
1656 GstAggregatorPrivate *priv = self->priv;
1657 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1658 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1660 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1662 PAD_STREAM_LOCK (aggpad);
1664 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1667 if (g_atomic_int_get (&aggpad->priv->pending_eos) == TRUE)
1670 PAD_LOCK_EVENT (aggpad);
1672 while (aggpad->buffer && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1673 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1674 PAD_WAIT_EVENT (aggpad);
1676 PAD_UNLOCK_EVENT (aggpad);
1678 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1681 if (aggclass->clip) {
1682 aggclass->clip (self, aggpad, buffer, &actual_buf);
1685 PAD_LOCK_EVENT (aggpad);
1687 gst_buffer_unref (aggpad->buffer);
1688 aggpad->buffer = actual_buf;
1689 PAD_UNLOCK_EVENT (aggpad);
1690 PAD_STREAM_UNLOCK (aggpad);
1692 SRC_STREAM_LOCK (self);
1693 if (gst_aggregator_iterate_sinkpads (self,
1694 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos, NULL))
1695 SRC_STREAM_BROADCAST_UNLOCKED (self);
1696 SRC_STREAM_UNLOCK (self);
1698 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1700 return priv->flow_return;
1703 PAD_STREAM_UNLOCK (aggpad);
1705 gst_buffer_unref (buffer);
1706 GST_DEBUG_OBJECT (aggpad, "We are flushing");
1708 return GST_FLOW_FLUSHING;
1711 PAD_STREAM_UNLOCK (aggpad);
1713 gst_buffer_unref (buffer);
1714 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1716 return GST_FLOW_EOS;
1720 pad_query_func (GstPad * pad, GstObject * parent, GstQuery * query)
1722 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1723 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1725 if (GST_QUERY_IS_SERIALIZED (query)) {
1726 PAD_LOCK_EVENT (aggpad);
1728 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
1729 PAD_UNLOCK_EVENT (aggpad);
1733 while (aggpad->buffer
1734 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1735 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1736 PAD_WAIT_EVENT (aggpad);
1738 PAD_UNLOCK_EVENT (aggpad);
1740 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1744 return klass->sink_query (GST_AGGREGATOR (parent),
1745 GST_AGGREGATOR_PAD (pad), query);
1748 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping query");
1753 pad_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
1755 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1756 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1758 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1759 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1760 PAD_LOCK_EVENT (aggpad);
1762 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1763 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
1764 PAD_UNLOCK_EVENT (aggpad);
1768 while (aggpad->buffer
1769 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1770 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1771 PAD_WAIT_EVENT (aggpad);
1773 PAD_UNLOCK_EVENT (aggpad);
1775 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1776 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1780 return klass->sink_event (GST_AGGREGATOR (parent),
1781 GST_AGGREGATOR_PAD (pad), event);
1784 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping event");
1785 if (GST_EVENT_IS_STICKY (event))
1786 gst_pad_store_sticky_event (pad, event);
1787 gst_event_unref (event);
1792 pad_activate_mode_func (GstPad * pad,
1793 GstObject * parent, GstPadMode mode, gboolean active)
1795 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1797 if (active == FALSE) {
1798 PAD_LOCK_EVENT (aggpad);
1799 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1800 gst_buffer_replace (&aggpad->buffer, NULL);
1801 PAD_BROADCAST_EVENT (aggpad);
1802 PAD_UNLOCK_EVENT (aggpad);
1804 g_atomic_int_set (&aggpad->priv->flushing, FALSE);
1805 PAD_LOCK_EVENT (aggpad);
1806 PAD_BROADCAST_EVENT (aggpad);
1807 PAD_UNLOCK_EVENT (aggpad);
1813 /***********************************
1814 * GstAggregatorPad implementation *
1815 ************************************/
1816 static GstPadClass *aggregator_pad_parent_class = NULL;
1817 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1820 _pad_constructed (GObject * object)
1822 GstPad *pad = GST_PAD (object);
1824 gst_pad_set_chain_function (pad,
1825 GST_DEBUG_FUNCPTR ((GstPadChainFunction) _chain));
1826 gst_pad_set_event_function (pad,
1827 GST_DEBUG_FUNCPTR ((GstPadEventFunction) pad_event_func));
1828 gst_pad_set_query_function (pad,
1829 GST_DEBUG_FUNCPTR ((GstPadQueryFunction) pad_query_func));
1830 gst_pad_set_activatemode_function (pad,
1831 GST_DEBUG_FUNCPTR ((GstPadActivateModeFunction) pad_activate_mode_func));
1835 gst_aggregator_pad_finalize (GObject * object)
1837 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1839 g_mutex_clear (&pad->priv->event_lock);
1840 g_cond_clear (&pad->priv->event_cond);
1841 g_mutex_clear (&pad->priv->stream_lock);
1843 G_OBJECT_CLASS (aggregator_pad_parent_class)->finalize (object);
1847 gst_aggregator_pad_dispose (GObject * object)
1849 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1852 buf = gst_aggregator_pad_steal_buffer (pad);
1854 gst_buffer_unref (buf);
1856 G_OBJECT_CLASS (aggregator_pad_parent_class)->dispose (object);
1860 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
1862 GObjectClass *gobject_class = (GObjectClass *) klass;
1864 aggregator_pad_parent_class = g_type_class_peek_parent (klass);
1865 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
1867 gobject_class->constructed = GST_DEBUG_FUNCPTR (_pad_constructed);
1868 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_aggregator_pad_finalize);
1869 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_aggregator_pad_dispose);
1873 gst_aggregator_pad_init (GstAggregatorPad * pad)
1876 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
1877 GstAggregatorPadPrivate);
1880 g_mutex_init (&pad->priv->event_lock);
1881 g_cond_init (&pad->priv->event_cond);
1883 g_mutex_init (&pad->priv->stream_lock);
1887 * gst_aggregator_pad_steal_buffer:
1888 * @pad: the pad to get buffer from
1890 * Steal the ref to the buffer currently queued in @pad.
1892 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
1893 * queued. You should unref the buffer after usage.
1896 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
1898 GstBuffer *buffer = NULL;
1900 PAD_LOCK_EVENT (pad);
1902 GST_TRACE_OBJECT (pad, "Consuming buffer");
1903 buffer = pad->buffer;
1905 if (pad->priv->pending_eos) {
1906 pad->priv->pending_eos = FALSE;
1909 PAD_BROADCAST_EVENT (pad);
1910 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
1912 PAD_UNLOCK_EVENT (pad);
1918 * gst_aggregator_pad_get_buffer:
1919 * @pad: the pad to get buffer from
1921 * Returns: (transfer full): A reference to the buffer in @pad or
1922 * NULL if no buffer was queued. You should unref the buffer after
1926 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
1928 GstBuffer *buffer = NULL;
1930 PAD_LOCK_EVENT (pad);
1932 buffer = gst_buffer_ref (pad->buffer);
1933 PAD_UNLOCK_EVENT (pad);
1939 * gst_aggregator_merge_tags:
1940 * @self: a #GstAggregator
1941 * @tags: a #GstTagList to merge
1942 * @mode: the #GstTagMergeMode to use
1944 * Adds tags to so-called pending tags, which will be processed
1945 * before pushing out data downstream.
1947 * Note that this is provided for convenience, and the subclass is
1948 * not required to use this and can still do tag handling on its own.
1953 gst_aggregator_merge_tags (GstAggregator * self,
1954 const GstTagList * tags, GstTagMergeMode mode)
1958 g_return_if_fail (GST_IS_AGGREGATOR (self));
1959 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
1961 /* FIXME Check if we can use OBJECT lock here! */
1962 GST_OBJECT_LOCK (self);
1964 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
1965 otags = self->priv->tags;
1966 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
1968 gst_tag_list_unref (otags);
1969 self->priv->tags_changed = TRUE;
1970 GST_OBJECT_UNLOCK (self);
1974 * gst_aggregator_set_latency:
1975 * @self: a #GstAggregator
1976 * @min_latency: minimum latency
1977 * @max_latency: maximum latency
1979 * Lets #GstAggregator sub-classes tell the baseclass what their internal
1980 * latency is. Will also post a LATENCY message on the bus so the pipeline
1981 * can reconfigure its global latency.
1984 gst_aggregator_set_latency (GstAggregator * self,
1985 GstClockTime min_latency, GstClockTime max_latency)
1987 g_return_if_fail (GST_IS_AGGREGATOR (self));
1988 g_return_if_fail (max_latency >= min_latency);
1990 GST_OBJECT_LOCK (self);
1991 self->priv->sub_latency_min = min_latency;
1992 self->priv->sub_latency_max = max_latency;
1993 GST_OBJECT_UNLOCK (self);
1995 gst_element_post_message (GST_ELEMENT_CAST (self),
1996 gst_message_new_latency (GST_OBJECT_CAST (self)));