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(self) { \
170 GST_LOG_OBJECT (self, "Signaling src STREAM from thread %p", \
172 g_cond_broadcast(&(self->priv->src_cond)); \
175 #define KICK_SRC_THREAD(self) \
177 SRC_STREAM_LOCK (self); \
178 GST_LOG_OBJECT (self, "kicking src STREAM from thread %p", \
180 if (self->priv->aggregate_id) \
181 gst_clock_id_unschedule (self->priv->aggregate_id); \
182 self->priv->n_kicks++; \
183 SRC_STREAM_BROADCAST (self); \
184 SRC_STREAM_UNLOCK (self); \
187 struct _GstAggregatorPadPrivate
189 gboolean pending_flush_start;
190 gboolean pending_flush_stop;
191 gboolean pending_eos;
201 _aggpad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
203 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
206 aggpad->priv->flushing = FALSE;
209 return klass->flush (aggpad, agg);
214 /*************************************
215 * GstAggregator implementation *
216 *************************************/
217 static GstElementClass *aggregator_parent_class = NULL;
219 struct _GstAggregatorPrivate
223 /* Our state is >= PAUSED */
228 gboolean send_stream_start;
229 gboolean send_segment;
230 gboolean flush_seeking;
231 gboolean pending_flush_start;
233 GstFlowReturn flow_return;
238 gboolean tags_changed;
240 /* Lock to prevent two src setcaps from happening at the same time */
243 gboolean latency_live;
244 GstClockTime latency_min;
245 GstClockTime latency_max;
248 GstClockID aggregate_id;
260 gboolean one_actually_seeked;
263 #define DEFAULT_LATENCY -1
273 * gst_aggregator_iterate_sinkpads:
274 * @self: The #GstAggregator
275 * @func: The function to call.
276 * @user_data: The data to pass to @func.
278 * Iterate the sinkpads of aggregator to call a function on them.
280 * This method guarantees that @func will be called only once for each
284 gst_aggregator_iterate_sinkpads (GstAggregator * self,
285 GstAggregatorPadForeachFunc func, gpointer user_data)
287 gboolean result = FALSE;
289 gboolean done = FALSE;
290 GValue item = { 0, };
291 GList *seen_pads = NULL;
293 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
299 switch (gst_iterator_next (iter, &item)) {
300 case GST_ITERATOR_OK:
304 pad = g_value_get_object (&item);
306 /* if already pushed, skip. FIXME, find something faster to tag pads */
307 if (pad == NULL || g_list_find (seen_pads, pad)) {
308 g_value_reset (&item);
312 GST_LOG_OBJECT (self, "calling function on pad %s:%s",
313 GST_DEBUG_PAD_NAME (pad));
314 result = func (self, pad, user_data);
318 seen_pads = g_list_prepend (seen_pads, pad);
320 g_value_reset (&item);
323 case GST_ITERATOR_RESYNC:
324 gst_iterator_resync (iter);
326 case GST_ITERATOR_ERROR:
327 GST_ERROR_OBJECT (self,
328 "Could not iterate over internally linked pads");
331 case GST_ITERATOR_DONE:
336 g_value_unset (&item);
337 gst_iterator_free (iter);
339 if (seen_pads == NULL) {
340 GST_DEBUG_OBJECT (self, "No pad seen");
344 g_list_free (seen_pads);
350 static inline gboolean
351 _check_all_pads_with_data_or_eos (GstAggregator * self,
352 GstAggregatorPad * aggpad, gpointer user_data)
354 if (aggpad->buffer || aggpad->eos) {
358 GST_LOG_OBJECT (aggpad, "Not ready to be aggregated");
364 _reset_flow_values (GstAggregator * self)
366 self->priv->flow_return = GST_FLOW_FLUSHING;
367 self->priv->send_stream_start = TRUE;
368 self->priv->send_segment = TRUE;
369 gst_segment_init (&self->segment, GST_FORMAT_TIME);
373 _push_mandatory_events (GstAggregator * self)
375 GstAggregatorPrivate *priv = self->priv;
377 if (g_atomic_int_get (&self->priv->send_stream_start)) {
380 GST_INFO_OBJECT (self, "pushing stream start");
381 /* stream-start (FIXME: create id based on input ids) */
382 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
383 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
384 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
386 g_atomic_int_set (&self->priv->send_stream_start, FALSE);
389 if (self->priv->srccaps) {
391 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
392 self->priv->srccaps);
393 if (!gst_pad_push_event (self->srcpad,
394 gst_event_new_caps (self->priv->srccaps))) {
395 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
397 gst_caps_unref (self->priv->srccaps);
398 self->priv->srccaps = NULL;
401 if (g_atomic_int_get (&self->priv->send_segment)) {
402 if (!g_atomic_int_get (&self->priv->flush_seeking)) {
403 GstEvent *segev = gst_event_new_segment (&self->segment);
405 if (!self->priv->seqnum)
406 self->priv->seqnum = gst_event_get_seqnum (segev);
408 gst_event_set_seqnum (segev, self->priv->seqnum);
410 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segev);
411 gst_pad_push_event (self->srcpad, segev);
412 g_atomic_int_set (&self->priv->send_segment, FALSE);
416 if (priv->tags && priv->tags_changed) {
417 gst_pad_push_event (self->srcpad,
418 gst_event_new_tag (gst_tag_list_ref (priv->tags)));
419 priv->tags_changed = FALSE;
424 * gst_aggregator_set_src_caps:
425 * @self: The #GstAggregator
426 * @caps: The #GstCaps to set on the src pad.
428 * Sets the caps to be used on the src pad.
431 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
433 GST_AGGREGATOR_SETCAPS_LOCK (self);
434 gst_caps_replace (&self->priv->srccaps, caps);
435 _push_mandatory_events (self);
436 GST_AGGREGATOR_SETCAPS_UNLOCK (self);
440 * gst_aggregator_finish_buffer:
441 * @self: The #GstAggregator
442 * @buffer: the #GstBuffer to push.
444 * This method will take care of sending mandatory events before pushing
445 * the provided buffer.
448 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
450 _push_mandatory_events (self);
452 if (!g_atomic_int_get (&self->priv->flush_seeking) &&
453 gst_pad_is_active (self->srcpad)) {
454 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
455 return gst_pad_push (self->srcpad, buffer);
457 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
458 g_atomic_int_get (&self->priv->flush_seeking),
459 gst_pad_is_active (self->srcpad));
460 gst_buffer_unref (buffer);
466 _push_eos (GstAggregator * self)
469 _push_mandatory_events (self);
471 self->priv->send_eos = FALSE;
472 event = gst_event_new_eos ();
473 gst_event_set_seqnum (event, self->priv->seqnum);
474 gst_pad_push_event (self->srcpad, event);
478 gst_aggregator_get_next_time (GstAggregator * self)
480 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
482 if (klass->get_next_time)
483 return klass->get_next_time (self);
485 return GST_CLOCK_TIME_NONE;
488 /* called with the src STREAM lock */
490 _wait_and_check (GstAggregator * self)
492 GstClockTime latency_max, latency_min;
496 gst_aggregator_get_latency (self, &live, &latency_min, &latency_max);
498 if (gst_aggregator_iterate_sinkpads (self,
499 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos,
501 GST_DEBUG_OBJECT (self, "all pads have data");
505 SRC_STREAM_LOCK (self);
506 start = gst_aggregator_get_next_time (self);
508 if (!live || !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self))
509 || !GST_CLOCK_TIME_IS_VALID (start)) {
510 while (self->priv->n_kicks <= 0)
511 SRC_STREAM_WAIT (self);
512 self->priv->n_kicks--;
515 GstClockReturn status;
517 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
518 GST_TIME_ARGS (start));
520 time = GST_ELEMENT_CAST (self)->base_time + start;
522 if (GST_CLOCK_TIME_IS_VALID (latency_max)) {
524 } else if (GST_CLOCK_TIME_IS_VALID (latency_min)) {
527 time += self->latency;
530 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
531 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
532 " latency max %" GST_TIME_FORMAT " latency min %" GST_TIME_FORMAT
533 " current %" GST_TIME_FORMAT ")", GST_TIME_ARGS (time),
534 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
535 GST_TIME_ARGS (start), GST_TIME_ARGS (latency_max),
536 GST_TIME_ARGS (latency_min),
537 GST_TIME_ARGS (gst_clock_get_time (GST_ELEMENT_CLOCK (self))));
539 self->priv->aggregate_id =
540 gst_clock_new_single_shot_id (GST_ELEMENT_CLOCK (self), time);
541 SRC_STREAM_UNLOCK (self);
543 status = gst_clock_id_wait (self->priv->aggregate_id, NULL);
545 SRC_STREAM_LOCK (self);
546 if (self->priv->aggregate_id) {
547 gst_clock_id_unref (self->priv->aggregate_id);
548 self->priv->aggregate_id = NULL;
550 self->priv->n_kicks--;
552 GST_DEBUG_OBJECT (self, "clock returned %d", status);
555 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
556 SRC_STREAM_UNLOCK (self);
560 SRC_STREAM_UNLOCK (self);
562 return gst_aggregator_iterate_sinkpads (self,
563 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos, NULL);
567 aggregate_func (GstAggregator * self)
569 GstAggregatorPrivate *priv = self->priv;
570 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
572 if (self->priv->running == FALSE) {
573 GST_DEBUG_OBJECT (self, "Not running anymore");
577 GST_LOG_OBJECT (self, "Checking aggregate");
578 while (priv->send_eos && priv->running) {
579 if (!_wait_and_check (self))
582 GST_TRACE_OBJECT (self, "Actually aggregating!");
584 priv->flow_return = klass->aggregate (self);
586 if (priv->flow_return == GST_FLOW_EOS) {
590 if (priv->flow_return == GST_FLOW_FLUSHING &&
591 g_atomic_int_get (&priv->flush_seeking))
592 priv->flow_return = GST_FLOW_OK;
594 GST_LOG_OBJECT (self, "flow return is %s",
595 gst_flow_get_name (priv->flow_return));
597 if (priv->flow_return != GST_FLOW_OK)
603 _start (GstAggregator * self)
605 self->priv->running = TRUE;
606 self->priv->send_stream_start = TRUE;
607 self->priv->send_segment = TRUE;
608 self->priv->send_eos = TRUE;
609 self->priv->srccaps = NULL;
610 self->priv->flow_return = GST_FLOW_OK;
616 _check_pending_flush_stop (GstAggregatorPad * pad)
618 return (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
622 _stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
626 GST_INFO_OBJECT (self, "%s srcpad task",
627 flush_start ? "Pausing" : "Stopping");
629 self->priv->running = FALSE;
630 KICK_SRC_THREAD (self);
633 res = gst_pad_push_event (self->srcpad, flush_start);
636 gst_pad_stop_task (self->srcpad);
637 KICK_SRC_THREAD (self);
643 _start_srcpad_task (GstAggregator * self)
645 GST_INFO_OBJECT (self, "Starting srcpad task");
647 self->priv->running = TRUE;
648 self->priv->n_kicks = 0;
649 gst_pad_start_task (GST_PAD (self->srcpad),
650 (GstTaskFunction) aggregate_func, self, NULL);
654 _flush (GstAggregator * self)
656 GstFlowReturn ret = GST_FLOW_OK;
657 GstAggregatorPrivate *priv = self->priv;
658 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
660 GST_DEBUG_OBJECT (self, "Flushing everything");
661 g_atomic_int_set (&priv->send_segment, TRUE);
662 g_atomic_int_set (&priv->flush_seeking, FALSE);
663 g_atomic_int_set (&priv->tags_changed, FALSE);
665 ret = klass->flush (self);
671 _all_flush_stop_received (GstAggregator * self)
674 GstAggregatorPad *tmppad;
676 GST_OBJECT_LOCK (self);
677 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
678 tmppad = (GstAggregatorPad *) tmp->data;
680 if (_check_pending_flush_stop (tmppad) == FALSE) {
681 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
682 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
683 GST_OBJECT_UNLOCK (self);
687 GST_OBJECT_UNLOCK (self);
693 _flush_start (GstAggregator * self, GstAggregatorPad * aggpad, GstEvent * event)
696 GstAggregatorPrivate *priv = self->priv;
697 GstAggregatorPadPrivate *padpriv = aggpad->priv;
699 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
700 /* Remove pad buffer and wake up the streaming thread */
701 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
702 gst_buffer_replace (&tmpbuf, NULL);
703 PAD_STREAM_LOCK (aggpad);
704 if (g_atomic_int_compare_and_exchange (&padpriv->pending_flush_start,
705 TRUE, FALSE) == TRUE) {
706 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
707 g_atomic_int_set (&padpriv->pending_flush_stop, TRUE);
710 if (g_atomic_int_get (&priv->flush_seeking)) {
711 /* If flush_seeking we forward the first FLUSH_START */
712 if (g_atomic_int_compare_and_exchange (&priv->pending_flush_start,
713 TRUE, FALSE) == TRUE) {
715 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
716 _stop_srcpad_task (self, event);
717 priv->flow_return = GST_FLOW_OK;
719 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
720 GST_PAD_STREAM_LOCK (self->srcpad);
721 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
725 gst_event_unref (event);
727 PAD_STREAM_UNLOCK (aggpad);
729 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
730 gst_buffer_replace (&tmpbuf, NULL);
733 /* GstAggregator vmethods default implementations */
735 _sink_event (GstAggregator * self, GstAggregatorPad * aggpad, GstEvent * event)
738 GstPad *pad = GST_PAD (aggpad);
739 GstAggregatorPrivate *priv = self->priv;
741 switch (GST_EVENT_TYPE (event)) {
742 case GST_EVENT_FLUSH_START:
744 _flush_start (self, aggpad, event);
745 /* We forward only in one case: right after flush_seeking */
749 case GST_EVENT_FLUSH_STOP:
751 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
753 _aggpad_flush (aggpad, self);
754 if (g_atomic_int_get (&priv->flush_seeking)) {
755 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
757 if (g_atomic_int_get (&priv->flush_seeking)) {
758 if (_all_flush_stop_received (self)) {
759 /* That means we received FLUSH_STOP/FLUSH_STOP on
760 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
762 gst_pad_push_event (self->srcpad, event);
763 priv->send_eos = TRUE;
765 KICK_SRC_THREAD (self);
767 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
768 GST_PAD_STREAM_UNLOCK (self->srcpad);
769 _start_srcpad_task (self);
774 /* We never forward the event */
779 GST_DEBUG_OBJECT (aggpad, "EOS");
781 /* We still have a buffer, and we don't want the subclass to have to
782 * check for it. Mark pending_eos, eos will be set when steal_buffer is
785 PAD_LOCK_EVENT (aggpad);
786 if (!aggpad->buffer) {
789 aggpad->priv->pending_eos = TRUE;
791 PAD_UNLOCK_EVENT (aggpad);
793 KICK_SRC_THREAD (self);
796 case GST_EVENT_SEGMENT:
798 PAD_LOCK_EVENT (aggpad);
799 gst_event_copy_segment (event, &aggpad->segment);
800 self->priv->seqnum = gst_event_get_seqnum (event);
801 PAD_UNLOCK_EVENT (aggpad);
804 case GST_EVENT_STREAM_START:
812 gst_event_parse_tag (event, &tags);
814 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
815 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
816 gst_event_unref (event);
828 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
829 return gst_pad_event_default (pad, GST_OBJECT (self), event);
832 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
834 gst_event_unref (event);
840 _stop_pad (GstAggregator * self, GstAggregatorPad * pad, gpointer unused_udata)
842 _aggpad_flush (pad, self);
848 _stop (GstAggregator * agg)
850 _reset_flow_values (agg);
852 gst_aggregator_iterate_sinkpads (agg,
853 (GstAggregatorPadForeachFunc) _stop_pad, NULL);
856 gst_tag_list_unref (agg->priv->tags);
857 agg->priv->tags = NULL;
862 /* GstElement vmethods implementations */
863 static GstStateChangeReturn
864 _change_state (GstElement * element, GstStateChange transition)
866 GstStateChangeReturn ret;
867 GstAggregator *self = GST_AGGREGATOR (element);
868 GstAggregatorClass *agg_class = GST_AGGREGATOR_GET_CLASS (self);
871 switch (transition) {
872 case GST_STATE_CHANGE_READY_TO_PAUSED:
873 agg_class->start (self);
880 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
881 transition)) == GST_STATE_CHANGE_FAILURE)
885 switch (transition) {
886 case GST_STATE_CHANGE_PAUSED_TO_READY:
887 agg_class->stop (self);
897 GST_ERROR_OBJECT (element, "parent failed state change");
903 _release_pad (GstElement * element, GstPad * pad)
905 GstAggregator *self = GST_AGGREGATOR (element);
908 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
910 GST_INFO_OBJECT (pad, "Removing pad");
912 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
913 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
914 gst_buffer_replace (&tmpbuf, NULL);
915 gst_element_remove_pad (element, pad);
917 KICK_SRC_THREAD (self);
921 _request_new_pad (GstElement * element,
922 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
925 GstAggregatorPad *agg_pad;
927 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
928 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
930 self = GST_AGGREGATOR (element);
932 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
936 GST_OBJECT_LOCK (element);
937 if (req_name == NULL || strlen (req_name) < 6
938 || !g_str_has_prefix (req_name, "sink_")) {
939 /* no name given when requesting the pad, use next available int */
942 /* parse serial number from requested padname */
943 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
944 if (serial >= priv->padcount)
945 priv->padcount = serial;
948 name = g_strdup_printf ("sink_%u", priv->padcount);
949 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
950 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
953 GST_OBJECT_UNLOCK (element);
959 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
962 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
964 /* add the pad to the element */
965 gst_element_add_pad (element, GST_PAD (agg_pad));
967 return GST_PAD (agg_pad);
972 GstClockTime min, max;
977 _latency_query (GstAggregator * self, GstPad * pad, gpointer user_data)
979 LatencyData *data = user_data;
980 GstClockTime min, max;
984 query = gst_query_new_latency ();
985 res = gst_pad_peer_query (pad, query);
988 gst_query_parse_latency (query, &live, &min, &max);
990 GST_LOG_OBJECT (self, "got latency live:%s min:%" G_GINT64_FORMAT
991 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
996 if (max != GST_CLOCK_TIME_NONE &&
997 ((data->max != GST_CLOCK_TIME_NONE && max < data->max) ||
998 (data->max == GST_CLOCK_TIME_NONE)))
1004 gst_query_unref (query);
1010 * gst_aggregator_get_latency:
1011 * @self: a #GstAggregator
1012 * @live: (out) (allow-none): whether @self is live
1013 * @min_latency: (out) (allow-none): the configured minimum latency of @self
1014 * @max_latency: (out) (allow-none): the configured maximum latency of @self
1016 * Retreives the latency values reported by @self in response to the latency
1019 * Typically only called by subclasses.
1022 gst_aggregator_get_latency (GstAggregator * self, gboolean * live,
1023 GstClockTime * min_latency, GstClockTime * max_latency)
1025 GstClockTime min, max;
1027 g_return_if_fail (GST_IS_AGGREGATOR (self));
1029 min = self->priv->latency_min;
1030 max = self->priv->latency_max;
1032 if (GST_CLOCK_TIME_IS_VALID (self->latency)) {
1033 if (GST_CLOCK_TIME_IS_VALID (min))
1034 min += self->latency;
1035 if (GST_CLOCK_TIME_IS_VALID (max))
1036 max += self->latency;
1040 *live = self->priv->latency_live;
1048 gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
1053 data.max = GST_CLOCK_TIME_NONE;
1056 /* query upstream's latency */
1057 gst_aggregator_iterate_sinkpads (self,
1058 (GstAggregatorPadForeachFunc) _latency_query, &data);
1060 if (data.live && GST_CLOCK_TIME_IS_VALID (self->latency) &&
1061 self->latency > data.max) {
1062 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1063 ("%s", "Latency too big"),
1064 ("The requested latency value is too big for the current pipeline. "
1065 "Limiting to %" G_GINT64_FORMAT, data.max));
1066 self->latency = data.max;
1069 self->priv->latency_live = data.live;
1070 self->priv->latency_min = data.min;
1071 self->priv->latency_max = data.max;
1074 if (GST_CLOCK_TIME_IS_VALID (self->latency)) {
1075 if (GST_CLOCK_TIME_IS_VALID (data.min))
1076 data.min += self->latency;
1077 if (GST_CLOCK_TIME_IS_VALID (data.max))
1078 data.max += self->latency;
1081 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1082 " max:%" G_GINT64_FORMAT, data.live ? "true" : "false", data.min,
1085 gst_query_set_latency (query, data.live, data.min, data.max);
1091 _send_event (GstElement * element, GstEvent * event)
1093 GstAggregator *self = GST_AGGREGATOR (element);
1095 GST_STATE_LOCK (element);
1096 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1097 GST_STATE (element) < GST_STATE_PAUSED) {
1101 GstSeekType start_type, stop_type;
1104 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1105 &start, &stop_type, &stop);
1106 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1107 stop_type, stop, NULL);
1109 self->priv->seqnum = gst_event_get_seqnum (event);
1110 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1112 GST_STATE_UNLOCK (element);
1115 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1120 _src_query (GstAggregator * self, GstQuery * query)
1122 gboolean res = TRUE;
1124 switch (GST_QUERY_TYPE (query)) {
1125 case GST_QUERY_SEEKING:
1129 /* don't pass it along as some (file)sink might claim it does
1130 * whereas with a collectpads in between that will not likely work */
1131 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1132 gst_query_set_seeking (query, format, FALSE, 0, -1);
1137 case GST_QUERY_LATENCY:
1139 return gst_aggregator_query_latency (self, query);
1145 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1152 event_forward_func (GstPad * pad, EventData * evdata)
1154 gboolean ret = TRUE;
1155 GstPad *peer = gst_pad_get_peer (pad);
1156 GstAggregatorPadPrivate *padpriv = GST_AGGREGATOR_PAD (pad)->priv;
1159 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1160 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1161 gst_object_unref (peer);
1165 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1166 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1168 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1169 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1171 if (gst_pad_query (peer, seeking)) {
1174 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1176 if (seekable == FALSE) {
1177 GST_INFO_OBJECT (pad,
1178 "Source not seekable, We failed but it does not matter!");
1183 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1187 if (evdata->flush) {
1188 padpriv->pending_flush_start = FALSE;
1189 padpriv->pending_flush_stop = FALSE;
1192 evdata->one_actually_seeked = TRUE;
1195 evdata->result &= ret;
1197 /* Always send to all pads */
1202 _set_flush_pending (GstAggregator * self, GstAggregatorPad * pad,
1205 pad->priv->pending_flush_start = TRUE;
1206 pad->priv->pending_flush_stop = FALSE;
1212 _forward_event_to_all_sinkpads (GstAggregator * self, GstEvent * event,
1217 evdata.event = event;
1218 evdata.result = TRUE;
1219 evdata.flush = flush;
1220 evdata.one_actually_seeked = FALSE;
1222 /* We first need to set all pads as flushing in a first pass
1223 * as flush_start flush_stop is sometimes sent synchronously
1224 * while we send the seek event */
1226 gst_aggregator_iterate_sinkpads (self,
1227 (GstAggregatorPadForeachFunc) _set_flush_pending, NULL);
1228 gst_pad_forward (self->srcpad, (GstPadForwardFunction) event_forward_func,
1231 gst_event_unref (event);
1237 _do_seek (GstAggregator * self, GstEvent * event)
1242 GstSeekType start_type, stop_type;
1246 GstAggregatorPrivate *priv = self->priv;
1248 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1249 &start, &stop_type, &stop);
1251 GST_INFO_OBJECT (self, "starting SEEK");
1253 flush = flags & GST_SEEK_FLAG_FLUSH;
1256 g_atomic_int_set (&priv->pending_flush_start, TRUE);
1257 g_atomic_int_set (&priv->flush_seeking, TRUE);
1260 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1261 stop_type, stop, NULL);
1263 /* forward the seek upstream */
1264 evdata = _forward_event_to_all_sinkpads (self, event, flush);
1267 if (!evdata.result || !evdata.one_actually_seeked) {
1268 g_atomic_int_set (&priv->flush_seeking, FALSE);
1269 g_atomic_int_set (&priv->pending_flush_start, FALSE);
1272 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1274 return evdata.result;
1278 _src_event (GstAggregator * self, GstEvent * event)
1281 gboolean res = TRUE;
1283 switch (GST_EVENT_TYPE (event)) {
1284 case GST_EVENT_SEEK:
1286 gst_event_ref (event);
1287 res = _do_seek (self, event);
1288 gst_event_unref (event);
1292 case GST_EVENT_NAVIGATION:
1294 /* navigation is rather pointless. */
1296 gst_event_unref (event);
1305 evdata = _forward_event_to_all_sinkpads (self, event, FALSE);
1306 res = evdata.result;
1313 src_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
1315 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1317 return klass->src_event (GST_AGGREGATOR (parent), event);
1321 src_query_func (GstPad * pad, GstObject * parent, GstQuery * query)
1323 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1325 return klass->src_query (GST_AGGREGATOR (parent), query);
1329 src_activate_mode (GstPad * pad,
1330 GstObject * parent, GstPadMode mode, gboolean active)
1332 GstAggregator *self = GST_AGGREGATOR (parent);
1333 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1335 if (klass->src_activate) {
1336 if (klass->src_activate (self, mode, active) == FALSE) {
1341 if (active == TRUE) {
1343 case GST_PAD_MODE_PUSH:
1345 GST_INFO_OBJECT (pad, "Activating pad!");
1346 _start_srcpad_task (self);
1351 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1358 GST_INFO_OBJECT (self, "Deactivating srcpad");
1359 _stop_srcpad_task (self, FALSE);
1365 _sink_query (GstAggregator * self, GstAggregatorPad * aggpad, GstQuery * query)
1367 GstPad *pad = GST_PAD (aggpad);
1369 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1373 gst_aggregator_finalize (GObject * object)
1375 GstAggregator *self = (GstAggregator *) object;
1377 gst_object_unref (self->clock);
1378 g_mutex_clear (&self->priv->setcaps_lock);
1379 g_mutex_clear (&self->priv->src_lock);
1380 g_cond_clear (&self->priv->src_cond);
1382 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1386 gst_aggregator_dispose (GObject * object)
1388 G_OBJECT_CLASS (aggregator_parent_class)->dispose (object);
1392 * gst_aggregator_set_latency_property:
1393 * @agg: a #GstAggregator
1394 * @latency: the new latency value.
1396 * Sets the new latency value to @latency. This value is used to limit the
1397 * amount of time a pad waits for data to appear before considering the pad
1401 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1403 g_return_if_fail (GST_IS_AGGREGATOR (self));
1405 GST_OBJECT_LOCK (self);
1407 if (self->priv->latency_live && self->priv->latency_max != 0 &&
1408 GST_CLOCK_TIME_IS_VALID (latency) && latency > self->priv->latency_max) {
1409 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1410 ("%s", "Latency too big"),
1411 ("The requested latency value is too big for the latency in the "
1412 "current pipeline. Limiting to %" G_GINT64_FORMAT,
1413 self->priv->latency_max));
1414 latency = self->priv->latency_max;
1417 self->latency = latency;
1418 GST_OBJECT_UNLOCK (self);
1422 * gst_aggregator_get_latency_property:
1423 * @agg: a #GstAggregator
1425 * Gets the latency value. See gst_aggregator_set_latency for
1428 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1429 * before a pad is deemed unresponsive. A value of -1 means an
1433 gst_aggregator_get_latency_property (GstAggregator * agg)
1437 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1439 GST_OBJECT_LOCK (agg);
1441 GST_OBJECT_UNLOCK (agg);
1447 gst_aggregator_set_property (GObject * object, guint prop_id,
1448 const GValue * value, GParamSpec * pspec)
1450 GstAggregator *agg = GST_AGGREGATOR (object);
1454 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1457 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1463 gst_aggregator_get_property (GObject * object, guint prop_id,
1464 GValue * value, GParamSpec * pspec)
1466 GstAggregator *agg = GST_AGGREGATOR (object);
1470 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1473 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1478 /* GObject vmethods implementations */
1480 gst_aggregator_class_init (GstAggregatorClass * klass)
1482 GObjectClass *gobject_class = (GObjectClass *) klass;
1483 GstElementClass *gstelement_class = (GstElementClass *) klass;
1485 aggregator_parent_class = g_type_class_peek_parent (klass);
1486 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1488 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1489 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1491 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1492 klass->start = _start;
1493 klass->stop = _stop;
1495 klass->sink_event = _sink_event;
1496 klass->sink_query = _sink_query;
1498 klass->src_event = _src_event;
1499 klass->src_query = _src_query;
1501 gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR (_request_new_pad);
1502 gstelement_class->send_event = GST_DEBUG_FUNCPTR (_send_event);
1503 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (_release_pad);
1504 gstelement_class->change_state = GST_DEBUG_FUNCPTR (_change_state);
1506 gobject_class->set_property = gst_aggregator_set_property;
1507 gobject_class->get_property = gst_aggregator_get_property;
1508 gobject_class->finalize = gst_aggregator_finalize;
1509 gobject_class->dispose = gst_aggregator_dispose;
1511 g_object_class_install_property (gobject_class, PROP_LATENCY,
1512 g_param_spec_int64 ("latency", "Buffer latency",
1513 "Number of nanoseconds to wait for a buffer to arrive on a sink pad"
1514 "before the pad is deemed unresponsive (-1 unlimited)", -1,
1515 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1516 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1520 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1522 GstPadTemplate *pad_template;
1523 GstAggregatorPrivate *priv;
1525 g_return_if_fail (klass->aggregate != NULL);
1528 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1529 GstAggregatorPrivate);
1534 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1535 g_return_if_fail (pad_template != NULL);
1537 priv->padcount = -1;
1538 priv->tags_changed = FALSE;
1540 self->priv->latency_live = FALSE;
1541 self->priv->latency_min = 0;
1542 self->priv->latency_max = GST_CLOCK_TIME_NONE;
1543 _reset_flow_values (self);
1545 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1547 gst_pad_set_event_function (self->srcpad,
1548 GST_DEBUG_FUNCPTR ((GstPadEventFunction) src_event_func));
1549 gst_pad_set_query_function (self->srcpad,
1550 GST_DEBUG_FUNCPTR ((GstPadQueryFunction) src_query_func));
1551 gst_pad_set_activatemode_function (self->srcpad,
1552 GST_DEBUG_FUNCPTR ((GstPadActivateModeFunction) src_activate_mode));
1554 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1556 self->clock = gst_system_clock_obtain ();
1559 g_mutex_init (&self->priv->setcaps_lock);
1560 g_mutex_init (&self->priv->src_lock);
1561 g_cond_init (&self->priv->src_cond);
1564 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1565 * method to get to the padtemplates */
1567 gst_aggregator_get_type (void)
1569 static volatile gsize type = 0;
1571 if (g_once_init_enter (&type)) {
1573 static const GTypeInfo info = {
1574 sizeof (GstAggregatorClass),
1577 (GClassInitFunc) gst_aggregator_class_init,
1580 sizeof (GstAggregator),
1582 (GInstanceInitFunc) gst_aggregator_init,
1585 _type = g_type_register_static (GST_TYPE_ELEMENT,
1586 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1587 g_once_init_leave (&type, _type);
1592 static GstFlowReturn
1593 _chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1595 GstBuffer *actual_buf = buffer;
1596 GstAggregator *self = GST_AGGREGATOR (object);
1597 GstAggregatorPrivate *priv = self->priv;
1598 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1599 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1601 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1603 PAD_STREAM_LOCK (aggpad);
1605 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1608 if (g_atomic_int_get (&aggpad->priv->pending_eos) == TRUE)
1611 PAD_LOCK_EVENT (aggpad);
1613 while (aggpad->buffer && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1614 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1615 PAD_WAIT_EVENT (aggpad);
1617 PAD_UNLOCK_EVENT (aggpad);
1619 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1622 if (aggclass->clip) {
1623 aggclass->clip (self, aggpad, buffer, &actual_buf);
1626 PAD_LOCK_EVENT (aggpad);
1628 gst_buffer_unref (aggpad->buffer);
1629 aggpad->buffer = actual_buf;
1630 PAD_UNLOCK_EVENT (aggpad);
1631 PAD_STREAM_UNLOCK (aggpad);
1633 if (gst_aggregator_iterate_sinkpads (self,
1634 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos, NULL))
1635 KICK_SRC_THREAD (self);
1637 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1639 return priv->flow_return;
1642 PAD_STREAM_UNLOCK (aggpad);
1644 gst_buffer_unref (buffer);
1645 GST_DEBUG_OBJECT (aggpad, "We are flushing");
1647 return GST_FLOW_FLUSHING;
1650 PAD_STREAM_UNLOCK (aggpad);
1652 gst_buffer_unref (buffer);
1653 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1655 return GST_FLOW_EOS;
1659 pad_query_func (GstPad * pad, GstObject * parent, GstQuery * query)
1661 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1662 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1664 if (GST_QUERY_IS_SERIALIZED (query)) {
1665 PAD_LOCK_EVENT (aggpad);
1667 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
1668 PAD_UNLOCK_EVENT (aggpad);
1672 while (aggpad->buffer
1673 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1674 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1675 PAD_WAIT_EVENT (aggpad);
1677 PAD_UNLOCK_EVENT (aggpad);
1679 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1683 return klass->sink_query (GST_AGGREGATOR (parent),
1684 GST_AGGREGATOR_PAD (pad), query);
1687 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping query");
1692 pad_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
1694 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1695 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1697 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1698 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1699 PAD_LOCK_EVENT (aggpad);
1701 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1702 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
1703 PAD_UNLOCK_EVENT (aggpad);
1707 while (aggpad->buffer
1708 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1709 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1710 PAD_WAIT_EVENT (aggpad);
1712 PAD_UNLOCK_EVENT (aggpad);
1714 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1715 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1719 return klass->sink_event (GST_AGGREGATOR (parent),
1720 GST_AGGREGATOR_PAD (pad), event);
1723 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping event");
1724 if (GST_EVENT_IS_STICKY (event))
1725 gst_pad_store_sticky_event (pad, event);
1726 gst_event_unref (event);
1731 pad_activate_mode_func (GstPad * pad,
1732 GstObject * parent, GstPadMode mode, gboolean active)
1734 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1736 if (active == FALSE) {
1737 PAD_LOCK_EVENT (aggpad);
1738 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1739 gst_buffer_replace (&aggpad->buffer, NULL);
1740 PAD_BROADCAST_EVENT (aggpad);
1741 PAD_UNLOCK_EVENT (aggpad);
1743 g_atomic_int_set (&aggpad->priv->flushing, FALSE);
1744 PAD_LOCK_EVENT (aggpad);
1745 PAD_BROADCAST_EVENT (aggpad);
1746 PAD_UNLOCK_EVENT (aggpad);
1752 /***********************************
1753 * GstAggregatorPad implementation *
1754 ************************************/
1755 static GstPadClass *aggregator_pad_parent_class = NULL;
1756 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1759 _pad_constructed (GObject * object)
1761 GstPad *pad = GST_PAD (object);
1763 gst_pad_set_chain_function (pad,
1764 GST_DEBUG_FUNCPTR ((GstPadChainFunction) _chain));
1765 gst_pad_set_event_function (pad,
1766 GST_DEBUG_FUNCPTR ((GstPadEventFunction) pad_event_func));
1767 gst_pad_set_query_function (pad,
1768 GST_DEBUG_FUNCPTR ((GstPadQueryFunction) pad_query_func));
1769 gst_pad_set_activatemode_function (pad,
1770 GST_DEBUG_FUNCPTR ((GstPadActivateModeFunction) pad_activate_mode_func));
1774 gst_aggregator_pad_finalize (GObject * object)
1776 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1778 g_mutex_clear (&pad->priv->event_lock);
1779 g_cond_clear (&pad->priv->event_cond);
1780 g_mutex_clear (&pad->priv->stream_lock);
1782 G_OBJECT_CLASS (aggregator_pad_parent_class)->finalize (object);
1786 gst_aggregator_pad_dispose (GObject * object)
1788 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1791 buf = gst_aggregator_pad_steal_buffer (pad);
1793 gst_buffer_unref (buf);
1795 G_OBJECT_CLASS (aggregator_pad_parent_class)->dispose (object);
1799 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
1801 GObjectClass *gobject_class = (GObjectClass *) klass;
1803 aggregator_pad_parent_class = g_type_class_peek_parent (klass);
1804 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
1806 gobject_class->constructed = GST_DEBUG_FUNCPTR (_pad_constructed);
1807 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_aggregator_pad_finalize);
1808 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_aggregator_pad_dispose);
1812 gst_aggregator_pad_init (GstAggregatorPad * pad)
1815 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
1816 GstAggregatorPadPrivate);
1819 g_mutex_init (&pad->priv->event_lock);
1820 g_cond_init (&pad->priv->event_cond);
1822 g_mutex_init (&pad->priv->stream_lock);
1826 * gst_aggregator_pad_steal_buffer:
1827 * @pad: the pad to get buffer from
1829 * Steal the ref to the buffer currently queued in @pad.
1831 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
1832 * queued. You should unref the buffer after usage.
1835 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
1837 GstBuffer *buffer = NULL;
1839 PAD_LOCK_EVENT (pad);
1841 GST_TRACE_OBJECT (pad, "Consuming buffer");
1842 buffer = pad->buffer;
1844 if (pad->priv->pending_eos) {
1845 pad->priv->pending_eos = FALSE;
1848 PAD_BROADCAST_EVENT (pad);
1849 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
1851 PAD_UNLOCK_EVENT (pad);
1857 * gst_aggregator_pad_get_buffer:
1858 * @pad: the pad to get buffer from
1860 * Returns: (transfer full): A reference to the buffer in @pad or
1861 * NULL if no buffer was queued. You should unref the buffer after
1865 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
1867 GstBuffer *buffer = NULL;
1869 PAD_LOCK_EVENT (pad);
1871 buffer = gst_buffer_ref (pad->buffer);
1872 PAD_UNLOCK_EVENT (pad);
1878 * gst_aggregator_merge_tags:
1879 * @self: a #GstAggregator
1880 * @tags: a #GstTagList to merge
1881 * @mode: the #GstTagMergeMode to use
1883 * Adds tags to so-called pending tags, which will be processed
1884 * before pushing out data downstream.
1886 * Note that this is provided for convenience, and the subclass is
1887 * not required to use this and can still do tag handling on its own.
1892 gst_aggregator_merge_tags (GstAggregator * self,
1893 const GstTagList * tags, GstTagMergeMode mode)
1897 g_return_if_fail (GST_IS_AGGREGATOR (self));
1898 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
1900 /* FIXME Check if we can use OBJECT lock here! */
1901 GST_OBJECT_LOCK (self);
1903 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
1904 otags = self->priv->tags;
1905 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
1907 gst_tag_list_unref (otags);
1908 self->priv->tags_changed = TRUE;
1909 GST_OBJECT_UNLOCK (self);