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) G_STMT_START { \
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) G_STMT_START { \
176 SRC_STREAM_LOCK (self); \
177 GST_LOG_OBJECT (self, "kicking src STREAM from thread %p", \
179 if (self->priv->aggregate_id) \
180 gst_clock_id_unschedule (self->priv->aggregate_id); \
181 self->priv->n_kicks++; \
182 SRC_STREAM_BROADCAST (self); \
183 SRC_STREAM_UNLOCK (self); \
186 struct _GstAggregatorPadPrivate
188 gboolean pending_flush_start;
189 gboolean pending_flush_stop;
190 gboolean pending_eos;
200 _aggpad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
202 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
205 aggpad->priv->flushing = FALSE;
208 return klass->flush (aggpad, agg);
213 /*************************************
214 * GstAggregator implementation *
215 *************************************/
216 static GstElementClass *aggregator_parent_class = NULL;
218 struct _GstAggregatorPrivate
222 /* Our state is >= PAUSED */
227 gboolean send_stream_start;
228 gboolean send_segment;
229 gboolean flush_seeking;
230 gboolean pending_flush_start;
232 GstFlowReturn flow_return;
237 gboolean tags_changed;
239 /* Lock to prevent two src setcaps from happening at the same time */
242 gboolean latency_live;
243 GstClockTime latency_min;
244 GstClockTime latency_max;
247 GstClockID aggregate_id;
259 gboolean one_actually_seeked;
262 #define DEFAULT_LATENCY -1
272 * gst_aggregator_iterate_sinkpads:
273 * @self: The #GstAggregator
274 * @func: The function to call.
275 * @user_data: The data to pass to @func.
277 * Iterate the sinkpads of aggregator to call a function on them.
279 * This method guarantees that @func will be called only once for each
283 gst_aggregator_iterate_sinkpads (GstAggregator * self,
284 GstAggregatorPadForeachFunc func, gpointer user_data)
286 gboolean result = FALSE;
288 gboolean done = FALSE;
289 GValue item = { 0, };
290 GList *seen_pads = NULL;
292 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
298 switch (gst_iterator_next (iter, &item)) {
299 case GST_ITERATOR_OK:
303 pad = g_value_get_object (&item);
305 /* if already pushed, skip. FIXME, find something faster to tag pads */
306 if (pad == NULL || g_list_find (seen_pads, pad)) {
307 g_value_reset (&item);
311 GST_LOG_OBJECT (self, "calling function on pad %s:%s",
312 GST_DEBUG_PAD_NAME (pad));
313 result = func (self, pad, user_data);
317 seen_pads = g_list_prepend (seen_pads, pad);
319 g_value_reset (&item);
322 case GST_ITERATOR_RESYNC:
323 gst_iterator_resync (iter);
325 case GST_ITERATOR_ERROR:
326 GST_ERROR_OBJECT (self,
327 "Could not iterate over internally linked pads");
330 case GST_ITERATOR_DONE:
335 g_value_unset (&item);
336 gst_iterator_free (iter);
338 if (seen_pads == NULL) {
339 GST_DEBUG_OBJECT (self, "No pad seen");
343 g_list_free (seen_pads);
349 static inline gboolean
350 _check_all_pads_with_data_or_eos (GstAggregator * self,
351 GstAggregatorPad * aggpad, gpointer user_data)
353 if (aggpad->buffer || aggpad->eos) {
357 GST_LOG_OBJECT (aggpad, "Not ready to be aggregated");
363 _reset_flow_values (GstAggregator * self)
365 self->priv->flow_return = GST_FLOW_FLUSHING;
366 self->priv->send_stream_start = TRUE;
367 self->priv->send_segment = TRUE;
368 gst_segment_init (&self->segment, GST_FORMAT_TIME);
372 _push_mandatory_events (GstAggregator * self)
374 GstAggregatorPrivate *priv = self->priv;
376 if (g_atomic_int_get (&self->priv->send_stream_start)) {
379 GST_INFO_OBJECT (self, "pushing stream start");
380 /* stream-start (FIXME: create id based on input ids) */
381 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
382 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
383 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
385 g_atomic_int_set (&self->priv->send_stream_start, FALSE);
388 if (self->priv->srccaps) {
390 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
391 self->priv->srccaps);
392 if (!gst_pad_push_event (self->srcpad,
393 gst_event_new_caps (self->priv->srccaps))) {
394 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
396 gst_caps_unref (self->priv->srccaps);
397 self->priv->srccaps = NULL;
400 if (g_atomic_int_get (&self->priv->send_segment)) {
401 if (!g_atomic_int_get (&self->priv->flush_seeking)) {
402 GstEvent *segev = gst_event_new_segment (&self->segment);
404 if (!self->priv->seqnum)
405 self->priv->seqnum = gst_event_get_seqnum (segev);
407 gst_event_set_seqnum (segev, self->priv->seqnum);
409 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segev);
410 gst_pad_push_event (self->srcpad, segev);
411 g_atomic_int_set (&self->priv->send_segment, FALSE);
415 if (priv->tags && priv->tags_changed) {
416 gst_pad_push_event (self->srcpad,
417 gst_event_new_tag (gst_tag_list_ref (priv->tags)));
418 priv->tags_changed = FALSE;
423 * gst_aggregator_set_src_caps:
424 * @self: The #GstAggregator
425 * @caps: The #GstCaps to set on the src pad.
427 * Sets the caps to be used on the src pad.
430 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
432 GST_AGGREGATOR_SETCAPS_LOCK (self);
433 gst_caps_replace (&self->priv->srccaps, caps);
434 _push_mandatory_events (self);
435 GST_AGGREGATOR_SETCAPS_UNLOCK (self);
439 * gst_aggregator_finish_buffer:
440 * @self: The #GstAggregator
441 * @buffer: the #GstBuffer to push.
443 * This method will take care of sending mandatory events before pushing
444 * the provided buffer.
447 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
449 _push_mandatory_events (self);
451 if (!g_atomic_int_get (&self->priv->flush_seeking) &&
452 gst_pad_is_active (self->srcpad)) {
453 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
454 return gst_pad_push (self->srcpad, buffer);
456 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
457 g_atomic_int_get (&self->priv->flush_seeking),
458 gst_pad_is_active (self->srcpad));
459 gst_buffer_unref (buffer);
465 _push_eos (GstAggregator * self)
468 _push_mandatory_events (self);
470 self->priv->send_eos = FALSE;
471 event = gst_event_new_eos ();
472 gst_event_set_seqnum (event, self->priv->seqnum);
473 gst_pad_push_event (self->srcpad, event);
477 gst_aggregator_get_next_time (GstAggregator * self)
479 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
481 if (klass->get_next_time)
482 return klass->get_next_time (self);
484 return GST_CLOCK_TIME_NONE;
487 /* called with the src STREAM lock */
489 _wait_and_check (GstAggregator * self, gboolean * timeout)
491 GstClockTime latency_max, latency_min;
497 gst_aggregator_get_latency (self, &live, &latency_min, &latency_max);
499 if (gst_aggregator_iterate_sinkpads (self,
500 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos,
502 GST_DEBUG_OBJECT (self, "all pads have data");
506 SRC_STREAM_LOCK (self);
507 start = gst_aggregator_get_next_time (self);
509 if (!live || !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self))
510 || !GST_CLOCK_TIME_IS_VALID (start)) {
511 while (self->priv->n_kicks <= 0)
512 SRC_STREAM_WAIT (self);
513 self->priv->n_kicks--;
515 GstClockTime base_time, time;
517 GstClockReturn status;
519 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
520 GST_TIME_ARGS (start));
522 GST_OBJECT_LOCK (self);
523 base_time = GST_ELEMENT_CAST (self)->base_time;
524 clock = GST_ELEMENT_CLOCK (self);
526 gst_object_ref (clock);
527 GST_OBJECT_UNLOCK (self);
529 time = base_time + start;
531 if (GST_CLOCK_TIME_IS_VALID (latency_max)) {
533 } else if (GST_CLOCK_TIME_IS_VALID (latency_min)) {
536 time += self->latency;
539 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
540 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
541 " latency max %" GST_TIME_FORMAT " latency min %" GST_TIME_FORMAT
542 " current %" GST_TIME_FORMAT ")", GST_TIME_ARGS (time),
543 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
544 GST_TIME_ARGS (start), GST_TIME_ARGS (latency_max),
545 GST_TIME_ARGS (latency_min),
546 GST_TIME_ARGS (gst_clock_get_time (clock)));
548 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
549 gst_object_unref (clock);
550 SRC_STREAM_UNLOCK (self);
552 status = gst_clock_id_wait (self->priv->aggregate_id, NULL);
554 SRC_STREAM_LOCK (self);
555 if (self->priv->aggregate_id) {
556 gst_clock_id_unref (self->priv->aggregate_id);
557 self->priv->aggregate_id = NULL;
559 self->priv->n_kicks--;
561 GST_DEBUG_OBJECT (self, "clock returned %d", status);
564 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
565 SRC_STREAM_UNLOCK (self);
570 SRC_STREAM_UNLOCK (self);
572 return gst_aggregator_iterate_sinkpads (self,
573 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos, NULL);
577 aggregate_func (GstAggregator * self)
579 GstAggregatorPrivate *priv = self->priv;
580 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
581 gboolean timeout = FALSE;
583 if (self->priv->running == FALSE) {
584 GST_DEBUG_OBJECT (self, "Not running anymore");
588 GST_LOG_OBJECT (self, "Checking aggregate");
589 while (priv->send_eos && priv->running) {
590 if (!_wait_and_check (self, &timeout))
593 GST_TRACE_OBJECT (self, "Actually aggregating!");
595 priv->flow_return = klass->aggregate (self, timeout);
597 if (priv->flow_return == GST_FLOW_EOS) {
601 if (priv->flow_return == GST_FLOW_FLUSHING &&
602 g_atomic_int_get (&priv->flush_seeking))
603 priv->flow_return = GST_FLOW_OK;
605 GST_LOG_OBJECT (self, "flow return is %s",
606 gst_flow_get_name (priv->flow_return));
608 if (priv->flow_return != GST_FLOW_OK)
614 _start (GstAggregator * self)
616 self->priv->running = TRUE;
617 self->priv->send_stream_start = TRUE;
618 self->priv->send_segment = TRUE;
619 self->priv->send_eos = TRUE;
620 self->priv->srccaps = NULL;
621 self->priv->flow_return = GST_FLOW_OK;
627 _check_pending_flush_stop (GstAggregatorPad * pad)
629 return (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
633 _stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
637 GST_INFO_OBJECT (self, "%s srcpad task",
638 flush_start ? "Pausing" : "Stopping");
640 self->priv->running = FALSE;
641 KICK_SRC_THREAD (self);
644 res = gst_pad_push_event (self->srcpad, flush_start);
647 gst_pad_stop_task (self->srcpad);
648 KICK_SRC_THREAD (self);
654 _start_srcpad_task (GstAggregator * self)
656 GST_INFO_OBJECT (self, "Starting srcpad task");
658 self->priv->running = TRUE;
659 self->priv->n_kicks = 0;
660 gst_pad_start_task (GST_PAD (self->srcpad),
661 (GstTaskFunction) aggregate_func, self, NULL);
665 _flush (GstAggregator * self)
667 GstFlowReturn ret = GST_FLOW_OK;
668 GstAggregatorPrivate *priv = self->priv;
669 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
671 GST_DEBUG_OBJECT (self, "Flushing everything");
672 g_atomic_int_set (&priv->send_segment, TRUE);
673 g_atomic_int_set (&priv->flush_seeking, FALSE);
674 g_atomic_int_set (&priv->tags_changed, FALSE);
676 ret = klass->flush (self);
682 _all_flush_stop_received (GstAggregator * self)
685 GstAggregatorPad *tmppad;
687 GST_OBJECT_LOCK (self);
688 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
689 tmppad = (GstAggregatorPad *) tmp->data;
691 if (_check_pending_flush_stop (tmppad) == FALSE) {
692 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
693 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
694 GST_OBJECT_UNLOCK (self);
698 GST_OBJECT_UNLOCK (self);
704 _flush_start (GstAggregator * self, GstAggregatorPad * aggpad, GstEvent * event)
707 GstAggregatorPrivate *priv = self->priv;
708 GstAggregatorPadPrivate *padpriv = aggpad->priv;
710 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
711 /* Remove pad buffer and wake up the streaming thread */
712 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
713 gst_buffer_replace (&tmpbuf, NULL);
714 PAD_STREAM_LOCK (aggpad);
715 if (g_atomic_int_compare_and_exchange (&padpriv->pending_flush_start,
716 TRUE, FALSE) == TRUE) {
717 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
718 g_atomic_int_set (&padpriv->pending_flush_stop, TRUE);
721 if (g_atomic_int_get (&priv->flush_seeking)) {
722 /* If flush_seeking we forward the first FLUSH_START */
723 if (g_atomic_int_compare_and_exchange (&priv->pending_flush_start,
724 TRUE, FALSE) == TRUE) {
726 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
727 _stop_srcpad_task (self, event);
728 priv->flow_return = GST_FLOW_OK;
730 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
731 GST_PAD_STREAM_LOCK (self->srcpad);
732 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
736 gst_event_unref (event);
738 PAD_STREAM_UNLOCK (aggpad);
740 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
741 gst_buffer_replace (&tmpbuf, NULL);
744 /* GstAggregator vmethods default implementations */
746 _sink_event (GstAggregator * self, GstAggregatorPad * aggpad, GstEvent * event)
749 GstPad *pad = GST_PAD (aggpad);
750 GstAggregatorPrivate *priv = self->priv;
752 switch (GST_EVENT_TYPE (event)) {
753 case GST_EVENT_FLUSH_START:
755 _flush_start (self, aggpad, event);
756 /* We forward only in one case: right after flush_seeking */
760 case GST_EVENT_FLUSH_STOP:
762 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
764 _aggpad_flush (aggpad, self);
765 if (g_atomic_int_get (&priv->flush_seeking)) {
766 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
768 if (g_atomic_int_get (&priv->flush_seeking)) {
769 if (_all_flush_stop_received (self)) {
770 /* That means we received FLUSH_STOP/FLUSH_STOP on
771 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
773 gst_pad_push_event (self->srcpad, event);
774 priv->send_eos = TRUE;
776 KICK_SRC_THREAD (self);
778 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
779 GST_PAD_STREAM_UNLOCK (self->srcpad);
780 _start_srcpad_task (self);
785 /* We never forward the event */
790 GST_DEBUG_OBJECT (aggpad, "EOS");
792 /* We still have a buffer, and we don't want the subclass to have to
793 * check for it. Mark pending_eos, eos will be set when steal_buffer is
796 PAD_LOCK_EVENT (aggpad);
797 if (!aggpad->buffer) {
800 aggpad->priv->pending_eos = TRUE;
802 PAD_UNLOCK_EVENT (aggpad);
804 KICK_SRC_THREAD (self);
807 case GST_EVENT_SEGMENT:
809 PAD_LOCK_EVENT (aggpad);
810 gst_event_copy_segment (event, &aggpad->segment);
811 self->priv->seqnum = gst_event_get_seqnum (event);
812 PAD_UNLOCK_EVENT (aggpad);
815 case GST_EVENT_STREAM_START:
823 gst_event_parse_tag (event, &tags);
825 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
826 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
827 gst_event_unref (event);
839 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
840 return gst_pad_event_default (pad, GST_OBJECT (self), event);
843 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
845 gst_event_unref (event);
851 _stop_pad (GstAggregator * self, GstAggregatorPad * pad, gpointer unused_udata)
853 _aggpad_flush (pad, self);
859 _stop (GstAggregator * agg)
861 _reset_flow_values (agg);
863 gst_aggregator_iterate_sinkpads (agg,
864 (GstAggregatorPadForeachFunc) _stop_pad, NULL);
867 gst_tag_list_unref (agg->priv->tags);
868 agg->priv->tags = NULL;
873 /* GstElement vmethods implementations */
874 static GstStateChangeReturn
875 _change_state (GstElement * element, GstStateChange transition)
877 GstStateChangeReturn ret;
878 GstAggregator *self = GST_AGGREGATOR (element);
879 GstAggregatorClass *agg_class = GST_AGGREGATOR_GET_CLASS (self);
882 switch (transition) {
883 case GST_STATE_CHANGE_READY_TO_PAUSED:
884 agg_class->start (self);
891 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
892 transition)) == GST_STATE_CHANGE_FAILURE)
896 switch (transition) {
897 case GST_STATE_CHANGE_PAUSED_TO_READY:
898 agg_class->stop (self);
908 GST_ERROR_OBJECT (element, "parent failed state change");
914 _release_pad (GstElement * element, GstPad * pad)
916 GstAggregator *self = GST_AGGREGATOR (element);
919 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
921 GST_INFO_OBJECT (pad, "Removing pad");
923 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
924 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
925 gst_buffer_replace (&tmpbuf, NULL);
926 gst_element_remove_pad (element, pad);
928 KICK_SRC_THREAD (self);
932 _request_new_pad (GstElement * element,
933 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
936 GstAggregatorPad *agg_pad;
938 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
939 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
941 self = GST_AGGREGATOR (element);
943 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
947 GST_OBJECT_LOCK (element);
948 if (req_name == NULL || strlen (req_name) < 6
949 || !g_str_has_prefix (req_name, "sink_")) {
950 /* no name given when requesting the pad, use next available int */
953 /* parse serial number from requested padname */
954 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
955 if (serial >= priv->padcount)
956 priv->padcount = serial;
959 name = g_strdup_printf ("sink_%u", priv->padcount);
960 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
961 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
964 GST_OBJECT_UNLOCK (element);
970 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
973 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
975 /* add the pad to the element */
976 gst_element_add_pad (element, GST_PAD (agg_pad));
978 return GST_PAD (agg_pad);
983 GstClockTime min, max;
988 _latency_query (GstAggregator * self, GstPad * pad, gpointer user_data)
990 LatencyData *data = user_data;
991 GstClockTime min, max;
995 query = gst_query_new_latency ();
996 res = gst_pad_peer_query (pad, query);
999 gst_query_parse_latency (query, &live, &min, &max);
1001 GST_LOG_OBJECT (self, "got latency live:%s min:%" G_GINT64_FORMAT
1002 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1004 if (min > data->min)
1007 if (max != GST_CLOCK_TIME_NONE &&
1008 ((data->max != GST_CLOCK_TIME_NONE && max < data->max) ||
1009 (data->max == GST_CLOCK_TIME_NONE)))
1015 gst_query_unref (query);
1021 * gst_aggregator_get_latency:
1022 * @self: a #GstAggregator
1023 * @live: (out) (allow-none): whether @self is live
1024 * @min_latency: (out) (allow-none): the configured minimum latency of @self
1025 * @max_latency: (out) (allow-none): the configured maximum latency of @self
1027 * Retreives the latency values reported by @self in response to the latency
1030 * Typically only called by subclasses.
1033 gst_aggregator_get_latency (GstAggregator * self, gboolean * live,
1034 GstClockTime * min_latency, GstClockTime * max_latency)
1036 GstClockTime min, max;
1038 g_return_if_fail (GST_IS_AGGREGATOR (self));
1040 min = self->priv->latency_min;
1041 max = self->priv->latency_max;
1043 if (GST_CLOCK_TIME_IS_VALID (self->latency)) {
1044 min += self->latency;
1045 if (GST_CLOCK_TIME_IS_VALID (max))
1046 max += self->latency;
1050 *live = self->priv->latency_live;
1058 gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
1063 data.max = GST_CLOCK_TIME_NONE;
1066 /* query upstream's latency */
1067 gst_aggregator_iterate_sinkpads (self,
1068 (GstAggregatorPadForeachFunc) _latency_query, &data);
1070 if (data.live && GST_CLOCK_TIME_IS_VALID (self->latency) &&
1071 self->latency > data.max) {
1072 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1073 ("%s", "Latency too big"),
1074 ("The requested latency value is too big for the current pipeline. "
1075 "Limiting to %" G_GINT64_FORMAT, data.max));
1076 self->latency = data.max;
1079 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (data.min))) {
1080 GST_WARNING_OBJECT (self, "Invalid minimum latency, using 0");
1084 self->priv->latency_live = data.live;
1085 self->priv->latency_min = data.min;
1086 self->priv->latency_max = data.max;
1089 if (GST_CLOCK_TIME_IS_VALID (self->latency)) {
1090 if (GST_CLOCK_TIME_IS_VALID (data.min))
1091 data.min += self->latency;
1092 if (GST_CLOCK_TIME_IS_VALID (data.max))
1093 data.max += self->latency;
1096 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1097 " max:%" G_GINT64_FORMAT, data.live ? "true" : "false", data.min,
1100 gst_query_set_latency (query, data.live, data.min, data.max);
1106 _send_event (GstElement * element, GstEvent * event)
1108 GstAggregator *self = GST_AGGREGATOR (element);
1110 GST_STATE_LOCK (element);
1111 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1112 GST_STATE (element) < GST_STATE_PAUSED) {
1116 GstSeekType start_type, stop_type;
1119 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1120 &start, &stop_type, &stop);
1121 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1122 stop_type, stop, NULL);
1124 self->priv->seqnum = gst_event_get_seqnum (event);
1125 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1127 GST_STATE_UNLOCK (element);
1130 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1135 _src_query (GstAggregator * self, GstQuery * query)
1137 gboolean res = TRUE;
1139 switch (GST_QUERY_TYPE (query)) {
1140 case GST_QUERY_SEEKING:
1144 /* don't pass it along as some (file)sink might claim it does
1145 * whereas with a collectpads in between that will not likely work */
1146 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1147 gst_query_set_seeking (query, format, FALSE, 0, -1);
1152 case GST_QUERY_LATENCY:
1154 return gst_aggregator_query_latency (self, query);
1160 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1167 event_forward_func (GstPad * pad, EventData * evdata)
1169 gboolean ret = TRUE;
1170 GstPad *peer = gst_pad_get_peer (pad);
1171 GstAggregatorPadPrivate *padpriv = GST_AGGREGATOR_PAD (pad)->priv;
1174 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1175 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1176 gst_object_unref (peer);
1180 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1181 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1183 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1184 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1186 if (gst_pad_query (peer, seeking)) {
1189 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1191 if (seekable == FALSE) {
1192 GST_INFO_OBJECT (pad,
1193 "Source not seekable, We failed but it does not matter!");
1198 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1202 if (evdata->flush) {
1203 padpriv->pending_flush_start = FALSE;
1204 padpriv->pending_flush_stop = FALSE;
1207 evdata->one_actually_seeked = TRUE;
1210 evdata->result &= ret;
1212 /* Always send to all pads */
1217 _set_flush_pending (GstAggregator * self, GstAggregatorPad * pad,
1220 pad->priv->pending_flush_start = TRUE;
1221 pad->priv->pending_flush_stop = FALSE;
1227 _forward_event_to_all_sinkpads (GstAggregator * self, GstEvent * event,
1232 evdata.event = event;
1233 evdata.result = TRUE;
1234 evdata.flush = flush;
1235 evdata.one_actually_seeked = FALSE;
1237 /* We first need to set all pads as flushing in a first pass
1238 * as flush_start flush_stop is sometimes sent synchronously
1239 * while we send the seek event */
1241 gst_aggregator_iterate_sinkpads (self,
1242 (GstAggregatorPadForeachFunc) _set_flush_pending, NULL);
1243 gst_pad_forward (self->srcpad, (GstPadForwardFunction) event_forward_func,
1246 gst_event_unref (event);
1252 _do_seek (GstAggregator * self, GstEvent * event)
1257 GstSeekType start_type, stop_type;
1261 GstAggregatorPrivate *priv = self->priv;
1263 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1264 &start, &stop_type, &stop);
1266 GST_INFO_OBJECT (self, "starting SEEK");
1268 flush = flags & GST_SEEK_FLAG_FLUSH;
1271 g_atomic_int_set (&priv->pending_flush_start, TRUE);
1272 g_atomic_int_set (&priv->flush_seeking, TRUE);
1275 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1276 stop_type, stop, NULL);
1278 /* forward the seek upstream */
1279 evdata = _forward_event_to_all_sinkpads (self, event, flush);
1282 if (!evdata.result || !evdata.one_actually_seeked) {
1283 g_atomic_int_set (&priv->flush_seeking, FALSE);
1284 g_atomic_int_set (&priv->pending_flush_start, FALSE);
1287 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1289 return evdata.result;
1293 _src_event (GstAggregator * self, GstEvent * event)
1296 gboolean res = TRUE;
1298 switch (GST_EVENT_TYPE (event)) {
1299 case GST_EVENT_SEEK:
1301 gst_event_ref (event);
1302 res = _do_seek (self, event);
1303 gst_event_unref (event);
1307 case GST_EVENT_NAVIGATION:
1309 /* navigation is rather pointless. */
1311 gst_event_unref (event);
1320 evdata = _forward_event_to_all_sinkpads (self, event, FALSE);
1321 res = evdata.result;
1328 src_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
1330 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1332 return klass->src_event (GST_AGGREGATOR (parent), event);
1336 src_query_func (GstPad * pad, GstObject * parent, GstQuery * query)
1338 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1340 return klass->src_query (GST_AGGREGATOR (parent), query);
1344 src_activate_mode (GstPad * pad,
1345 GstObject * parent, GstPadMode mode, gboolean active)
1347 GstAggregator *self = GST_AGGREGATOR (parent);
1348 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1350 if (klass->src_activate) {
1351 if (klass->src_activate (self, mode, active) == FALSE) {
1356 if (active == TRUE) {
1358 case GST_PAD_MODE_PUSH:
1360 GST_INFO_OBJECT (pad, "Activating pad!");
1361 _start_srcpad_task (self);
1366 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1373 GST_INFO_OBJECT (self, "Deactivating srcpad");
1374 _stop_srcpad_task (self, FALSE);
1380 _sink_query (GstAggregator * self, GstAggregatorPad * aggpad, GstQuery * query)
1382 GstPad *pad = GST_PAD (aggpad);
1384 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1388 gst_aggregator_finalize (GObject * object)
1390 GstAggregator *self = (GstAggregator *) object;
1392 gst_object_unref (self->clock);
1393 g_mutex_clear (&self->priv->setcaps_lock);
1394 g_mutex_clear (&self->priv->src_lock);
1395 g_cond_clear (&self->priv->src_cond);
1397 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1401 gst_aggregator_dispose (GObject * object)
1403 G_OBJECT_CLASS (aggregator_parent_class)->dispose (object);
1407 * gst_aggregator_set_latency_property:
1408 * @agg: a #GstAggregator
1409 * @latency: the new latency value.
1411 * Sets the new latency value to @latency. This value is used to limit the
1412 * amount of time a pad waits for data to appear before considering the pad
1416 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1418 g_return_if_fail (GST_IS_AGGREGATOR (self));
1420 GST_OBJECT_LOCK (self);
1422 if (self->priv->latency_live && self->priv->latency_max != 0 &&
1423 GST_CLOCK_TIME_IS_VALID (latency) && latency > self->priv->latency_max) {
1424 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1425 ("%s", "Latency too big"),
1426 ("The requested latency value is too big for the latency in the "
1427 "current pipeline. Limiting to %" G_GINT64_FORMAT,
1428 self->priv->latency_max));
1429 latency = self->priv->latency_max;
1432 self->latency = latency;
1433 GST_OBJECT_UNLOCK (self);
1437 * gst_aggregator_get_latency_property:
1438 * @agg: a #GstAggregator
1440 * Gets the latency value. See gst_aggregator_set_latency for
1443 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1444 * before a pad is deemed unresponsive. A value of -1 means an
1448 gst_aggregator_get_latency_property (GstAggregator * agg)
1452 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1454 GST_OBJECT_LOCK (agg);
1456 GST_OBJECT_UNLOCK (agg);
1462 gst_aggregator_set_property (GObject * object, guint prop_id,
1463 const GValue * value, GParamSpec * pspec)
1465 GstAggregator *agg = GST_AGGREGATOR (object);
1469 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1472 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1478 gst_aggregator_get_property (GObject * object, guint prop_id,
1479 GValue * value, GParamSpec * pspec)
1481 GstAggregator *agg = GST_AGGREGATOR (object);
1485 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1488 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1493 /* GObject vmethods implementations */
1495 gst_aggregator_class_init (GstAggregatorClass * klass)
1497 GObjectClass *gobject_class = (GObjectClass *) klass;
1498 GstElementClass *gstelement_class = (GstElementClass *) klass;
1500 aggregator_parent_class = g_type_class_peek_parent (klass);
1501 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1503 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1504 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1506 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1507 klass->start = _start;
1508 klass->stop = _stop;
1510 klass->sink_event = _sink_event;
1511 klass->sink_query = _sink_query;
1513 klass->src_event = _src_event;
1514 klass->src_query = _src_query;
1516 gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR (_request_new_pad);
1517 gstelement_class->send_event = GST_DEBUG_FUNCPTR (_send_event);
1518 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (_release_pad);
1519 gstelement_class->change_state = GST_DEBUG_FUNCPTR (_change_state);
1521 gobject_class->set_property = gst_aggregator_set_property;
1522 gobject_class->get_property = gst_aggregator_get_property;
1523 gobject_class->finalize = gst_aggregator_finalize;
1524 gobject_class->dispose = gst_aggregator_dispose;
1526 g_object_class_install_property (gobject_class, PROP_LATENCY,
1527 g_param_spec_int64 ("latency", "Buffer latency",
1528 "Number of nanoseconds to wait for a buffer to arrive on a sink pad"
1529 "before the pad is deemed unresponsive (-1 unlimited)", -1,
1530 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1531 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1535 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1537 GstPadTemplate *pad_template;
1538 GstAggregatorPrivate *priv;
1540 g_return_if_fail (klass->aggregate != NULL);
1543 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1544 GstAggregatorPrivate);
1549 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1550 g_return_if_fail (pad_template != NULL);
1552 priv->padcount = -1;
1553 priv->tags_changed = FALSE;
1555 self->priv->latency_live = FALSE;
1556 self->priv->latency_min = 0;
1557 self->priv->latency_max = GST_CLOCK_TIME_NONE;
1558 _reset_flow_values (self);
1560 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1562 gst_pad_set_event_function (self->srcpad,
1563 GST_DEBUG_FUNCPTR ((GstPadEventFunction) src_event_func));
1564 gst_pad_set_query_function (self->srcpad,
1565 GST_DEBUG_FUNCPTR ((GstPadQueryFunction) src_query_func));
1566 gst_pad_set_activatemode_function (self->srcpad,
1567 GST_DEBUG_FUNCPTR ((GstPadActivateModeFunction) src_activate_mode));
1569 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1571 self->clock = gst_system_clock_obtain ();
1574 g_mutex_init (&self->priv->setcaps_lock);
1575 g_mutex_init (&self->priv->src_lock);
1576 g_cond_init (&self->priv->src_cond);
1579 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1580 * method to get to the padtemplates */
1582 gst_aggregator_get_type (void)
1584 static volatile gsize type = 0;
1586 if (g_once_init_enter (&type)) {
1588 static const GTypeInfo info = {
1589 sizeof (GstAggregatorClass),
1592 (GClassInitFunc) gst_aggregator_class_init,
1595 sizeof (GstAggregator),
1597 (GInstanceInitFunc) gst_aggregator_init,
1600 _type = g_type_register_static (GST_TYPE_ELEMENT,
1601 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1602 g_once_init_leave (&type, _type);
1607 static GstFlowReturn
1608 _chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1610 GstBuffer *actual_buf = buffer;
1611 GstAggregator *self = GST_AGGREGATOR (object);
1612 GstAggregatorPrivate *priv = self->priv;
1613 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1614 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1616 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1618 PAD_STREAM_LOCK (aggpad);
1620 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1623 if (g_atomic_int_get (&aggpad->priv->pending_eos) == TRUE)
1626 PAD_LOCK_EVENT (aggpad);
1628 while (aggpad->buffer && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1629 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1630 PAD_WAIT_EVENT (aggpad);
1632 PAD_UNLOCK_EVENT (aggpad);
1634 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1637 if (aggclass->clip) {
1638 aggclass->clip (self, aggpad, buffer, &actual_buf);
1641 PAD_LOCK_EVENT (aggpad);
1643 gst_buffer_unref (aggpad->buffer);
1644 aggpad->buffer = actual_buf;
1645 PAD_UNLOCK_EVENT (aggpad);
1646 PAD_STREAM_UNLOCK (aggpad);
1648 if (gst_aggregator_iterate_sinkpads (self,
1649 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos, NULL))
1650 KICK_SRC_THREAD (self);
1652 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1654 return priv->flow_return;
1657 PAD_STREAM_UNLOCK (aggpad);
1659 gst_buffer_unref (buffer);
1660 GST_DEBUG_OBJECT (aggpad, "We are flushing");
1662 return GST_FLOW_FLUSHING;
1665 PAD_STREAM_UNLOCK (aggpad);
1667 gst_buffer_unref (buffer);
1668 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1670 return GST_FLOW_EOS;
1674 pad_query_func (GstPad * pad, GstObject * parent, GstQuery * query)
1676 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1677 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1679 if (GST_QUERY_IS_SERIALIZED (query)) {
1680 PAD_LOCK_EVENT (aggpad);
1682 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
1683 PAD_UNLOCK_EVENT (aggpad);
1687 while (aggpad->buffer
1688 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1689 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1690 PAD_WAIT_EVENT (aggpad);
1692 PAD_UNLOCK_EVENT (aggpad);
1694 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1698 return klass->sink_query (GST_AGGREGATOR (parent),
1699 GST_AGGREGATOR_PAD (pad), query);
1702 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping query");
1707 pad_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
1709 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1710 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1712 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1713 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1714 PAD_LOCK_EVENT (aggpad);
1716 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1717 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
1718 PAD_UNLOCK_EVENT (aggpad);
1722 while (aggpad->buffer
1723 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1724 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1725 PAD_WAIT_EVENT (aggpad);
1727 PAD_UNLOCK_EVENT (aggpad);
1729 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1730 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1734 return klass->sink_event (GST_AGGREGATOR (parent),
1735 GST_AGGREGATOR_PAD (pad), event);
1738 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping event");
1739 if (GST_EVENT_IS_STICKY (event))
1740 gst_pad_store_sticky_event (pad, event);
1741 gst_event_unref (event);
1746 pad_activate_mode_func (GstPad * pad,
1747 GstObject * parent, GstPadMode mode, gboolean active)
1749 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1751 if (active == FALSE) {
1752 PAD_LOCK_EVENT (aggpad);
1753 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1754 gst_buffer_replace (&aggpad->buffer, NULL);
1755 PAD_BROADCAST_EVENT (aggpad);
1756 PAD_UNLOCK_EVENT (aggpad);
1758 g_atomic_int_set (&aggpad->priv->flushing, FALSE);
1759 PAD_LOCK_EVENT (aggpad);
1760 PAD_BROADCAST_EVENT (aggpad);
1761 PAD_UNLOCK_EVENT (aggpad);
1767 /***********************************
1768 * GstAggregatorPad implementation *
1769 ************************************/
1770 static GstPadClass *aggregator_pad_parent_class = NULL;
1771 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1774 _pad_constructed (GObject * object)
1776 GstPad *pad = GST_PAD (object);
1778 gst_pad_set_chain_function (pad,
1779 GST_DEBUG_FUNCPTR ((GstPadChainFunction) _chain));
1780 gst_pad_set_event_function (pad,
1781 GST_DEBUG_FUNCPTR ((GstPadEventFunction) pad_event_func));
1782 gst_pad_set_query_function (pad,
1783 GST_DEBUG_FUNCPTR ((GstPadQueryFunction) pad_query_func));
1784 gst_pad_set_activatemode_function (pad,
1785 GST_DEBUG_FUNCPTR ((GstPadActivateModeFunction) pad_activate_mode_func));
1789 gst_aggregator_pad_finalize (GObject * object)
1791 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1793 g_mutex_clear (&pad->priv->event_lock);
1794 g_cond_clear (&pad->priv->event_cond);
1795 g_mutex_clear (&pad->priv->stream_lock);
1797 G_OBJECT_CLASS (aggregator_pad_parent_class)->finalize (object);
1801 gst_aggregator_pad_dispose (GObject * object)
1803 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1806 buf = gst_aggregator_pad_steal_buffer (pad);
1808 gst_buffer_unref (buf);
1810 G_OBJECT_CLASS (aggregator_pad_parent_class)->dispose (object);
1814 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
1816 GObjectClass *gobject_class = (GObjectClass *) klass;
1818 aggregator_pad_parent_class = g_type_class_peek_parent (klass);
1819 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
1821 gobject_class->constructed = GST_DEBUG_FUNCPTR (_pad_constructed);
1822 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_aggregator_pad_finalize);
1823 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_aggregator_pad_dispose);
1827 gst_aggregator_pad_init (GstAggregatorPad * pad)
1830 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
1831 GstAggregatorPadPrivate);
1834 g_mutex_init (&pad->priv->event_lock);
1835 g_cond_init (&pad->priv->event_cond);
1837 g_mutex_init (&pad->priv->stream_lock);
1841 * gst_aggregator_pad_steal_buffer:
1842 * @pad: the pad to get buffer from
1844 * Steal the ref to the buffer currently queued in @pad.
1846 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
1847 * queued. You should unref the buffer after usage.
1850 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
1852 GstBuffer *buffer = NULL;
1854 PAD_LOCK_EVENT (pad);
1856 GST_TRACE_OBJECT (pad, "Consuming buffer");
1857 buffer = pad->buffer;
1859 if (pad->priv->pending_eos) {
1860 pad->priv->pending_eos = FALSE;
1863 PAD_BROADCAST_EVENT (pad);
1864 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
1866 PAD_UNLOCK_EVENT (pad);
1872 * gst_aggregator_pad_get_buffer:
1873 * @pad: the pad to get buffer from
1875 * Returns: (transfer full): A reference to the buffer in @pad or
1876 * NULL if no buffer was queued. You should unref the buffer after
1880 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
1882 GstBuffer *buffer = NULL;
1884 PAD_LOCK_EVENT (pad);
1886 buffer = gst_buffer_ref (pad->buffer);
1887 PAD_UNLOCK_EVENT (pad);
1893 * gst_aggregator_merge_tags:
1894 * @self: a #GstAggregator
1895 * @tags: a #GstTagList to merge
1896 * @mode: the #GstTagMergeMode to use
1898 * Adds tags to so-called pending tags, which will be processed
1899 * before pushing out data downstream.
1901 * Note that this is provided for convenience, and the subclass is
1902 * not required to use this and can still do tag handling on its own.
1907 gst_aggregator_merge_tags (GstAggregator * self,
1908 const GstTagList * tags, GstTagMergeMode mode)
1912 g_return_if_fail (GST_IS_AGGREGATOR (self));
1913 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
1915 /* FIXME Check if we can use OBJECT lock here! */
1916 GST_OBJECT_LOCK (self);
1918 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
1919 otags = self->priv->tags;
1920 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
1922 gst_tag_list_unref (otags);
1923 self->priv->tags_changed = TRUE;
1924 GST_OBJECT_UNLOCK (self);