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 SRC_STREAM_LOCK (self); \
171 GST_LOG_OBJECT (self, "Signaling src STREAM from thread %p", \
173 if (self->priv->aggregate_id) \
174 gst_clock_id_unschedule (self->priv->aggregate_id); \
175 g_cond_broadcast(&(self->priv->src_cond)); \
176 SRC_STREAM_UNLOCK (self); \
179 struct _GstAggregatorPadPrivate
181 gboolean pending_flush_start;
182 gboolean pending_flush_stop;
183 gboolean pending_eos;
193 _aggpad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
195 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
198 aggpad->priv->flushing = FALSE;
201 return klass->flush (aggpad, agg);
206 /*************************************
207 * GstAggregator implementation *
208 *************************************/
209 static GstElementClass *aggregator_parent_class = NULL;
211 struct _GstAggregatorPrivate
215 /* Our state is >= PAUSED */
220 gboolean send_stream_start;
221 gboolean send_segment;
222 gboolean flush_seeking;
223 gboolean pending_flush_start;
225 GstFlowReturn flow_return;
230 gboolean tags_changed;
232 /* Lock to prevent two src setcaps from happening at the same time */
235 gboolean latency_live;
236 GstClockTime latency_min;
237 GstClockTime latency_max;
239 GstClockTime sub_latency_min;
240 GstClockTime sub_latency_max;
243 GstClockID aggregate_id;
254 gboolean one_actually_seeked;
257 #define DEFAULT_LATENCY -1
267 * gst_aggregator_iterate_sinkpads:
268 * @self: The #GstAggregator
269 * @func: The function to call.
270 * @user_data: The data to pass to @func.
272 * Iterate the sinkpads of aggregator to call a function on them.
274 * This method guarantees that @func will be called only once for each
278 gst_aggregator_iterate_sinkpads (GstAggregator * self,
279 GstAggregatorPadForeachFunc func, gpointer user_data)
281 gboolean result = FALSE;
283 gboolean done = FALSE;
284 GValue item = { 0, };
285 GList *seen_pads = NULL;
287 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
293 switch (gst_iterator_next (iter, &item)) {
294 case GST_ITERATOR_OK:
298 pad = g_value_get_object (&item);
300 /* if already pushed, skip. FIXME, find something faster to tag pads */
301 if (pad == NULL || g_list_find (seen_pads, pad)) {
302 g_value_reset (&item);
306 GST_LOG_OBJECT (self, "calling function on pad %s:%s",
307 GST_DEBUG_PAD_NAME (pad));
308 result = func (self, pad, user_data);
312 seen_pads = g_list_prepend (seen_pads, pad);
314 g_value_reset (&item);
317 case GST_ITERATOR_RESYNC:
318 gst_iterator_resync (iter);
320 case GST_ITERATOR_ERROR:
321 GST_ERROR_OBJECT (self,
322 "Could not iterate over internally linked pads");
325 case GST_ITERATOR_DONE:
330 g_value_unset (&item);
331 gst_iterator_free (iter);
333 if (seen_pads == NULL) {
334 GST_DEBUG_OBJECT (self, "No pad seen");
338 g_list_free (seen_pads);
344 static inline gboolean
345 _check_all_pads_with_data_or_eos (GstAggregator * self,
346 GstAggregatorPad * aggpad, gpointer user_data)
348 if (aggpad->buffer || aggpad->eos) {
352 GST_LOG_OBJECT (aggpad, "Not ready to be aggregated");
358 _reset_flow_values (GstAggregator * self)
360 self->priv->flow_return = GST_FLOW_FLUSHING;
361 self->priv->send_stream_start = TRUE;
362 self->priv->send_segment = TRUE;
363 gst_segment_init (&self->segment, GST_FORMAT_TIME);
367 _push_mandatory_events (GstAggregator * self)
369 GstAggregatorPrivate *priv = self->priv;
371 if (g_atomic_int_get (&self->priv->send_stream_start)) {
374 GST_INFO_OBJECT (self, "pushing stream start");
375 /* stream-start (FIXME: create id based on input ids) */
376 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
377 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
378 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
380 g_atomic_int_set (&self->priv->send_stream_start, FALSE);
383 if (self->priv->srccaps) {
385 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
386 self->priv->srccaps);
387 if (!gst_pad_push_event (self->srcpad,
388 gst_event_new_caps (self->priv->srccaps))) {
389 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
391 gst_caps_unref (self->priv->srccaps);
392 self->priv->srccaps = NULL;
395 if (g_atomic_int_get (&self->priv->send_segment)) {
396 if (!g_atomic_int_get (&self->priv->flush_seeking)) {
397 GstEvent *segev = gst_event_new_segment (&self->segment);
399 if (!self->priv->seqnum)
400 self->priv->seqnum = gst_event_get_seqnum (segev);
402 gst_event_set_seqnum (segev, self->priv->seqnum);
404 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segev);
405 gst_pad_push_event (self->srcpad, segev);
406 g_atomic_int_set (&self->priv->send_segment, FALSE);
410 if (priv->tags && priv->tags_changed) {
411 gst_pad_push_event (self->srcpad,
412 gst_event_new_tag (gst_tag_list_ref (priv->tags)));
413 priv->tags_changed = FALSE;
418 * gst_aggregator_set_src_caps:
419 * @self: The #GstAggregator
420 * @caps: The #GstCaps to set on the src pad.
422 * Sets the caps to be used on the src pad.
425 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
427 GST_AGGREGATOR_SETCAPS_LOCK (self);
428 gst_caps_replace (&self->priv->srccaps, caps);
429 _push_mandatory_events (self);
430 GST_AGGREGATOR_SETCAPS_UNLOCK (self);
434 * gst_aggregator_finish_buffer:
435 * @self: The #GstAggregator
436 * @buffer: the #GstBuffer to push.
438 * This method will take care of sending mandatory events before pushing
439 * the provided buffer.
442 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
444 _push_mandatory_events (self);
446 if (!g_atomic_int_get (&self->priv->flush_seeking) &&
447 gst_pad_is_active (self->srcpad)) {
448 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
449 return gst_pad_push (self->srcpad, buffer);
451 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
452 g_atomic_int_get (&self->priv->flush_seeking),
453 gst_pad_is_active (self->srcpad));
454 gst_buffer_unref (buffer);
460 _push_eos (GstAggregator * self)
463 _push_mandatory_events (self);
465 self->priv->send_eos = FALSE;
466 event = gst_event_new_eos ();
467 gst_event_set_seqnum (event, self->priv->seqnum);
468 gst_pad_push_event (self->srcpad, event);
472 gst_aggregator_get_next_time (GstAggregator * self)
474 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
476 if (klass->get_next_time)
477 return klass->get_next_time (self);
479 return GST_CLOCK_TIME_NONE;
482 /* called with the src STREAM lock */
484 _wait_and_check (GstAggregator * self, gboolean * timeout)
486 GstClockTime latency_max, latency_min;
492 gst_aggregator_get_latency (self, &live, &latency_min, &latency_max);
494 if (gst_aggregator_iterate_sinkpads (self,
495 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos,
497 GST_DEBUG_OBJECT (self, "all pads have data");
501 SRC_STREAM_LOCK (self);
503 /* Before waiting, check if we're actually still running */
504 if (!self->priv->running || !self->priv->send_eos) {
505 SRC_STREAM_UNLOCK (self);
510 start = gst_aggregator_get_next_time (self);
512 if (!live || !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self))
513 || !GST_CLOCK_TIME_IS_VALID (start)) {
514 /* We wake up here when something happened, and below
515 * then check if we're ready now. If we return FALSE,
516 * we will be directly called again.
518 SRC_STREAM_WAIT (self);
520 GstClockTime base_time, time;
522 GstClockReturn status;
524 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
525 GST_TIME_ARGS (start));
527 GST_OBJECT_LOCK (self);
528 base_time = GST_ELEMENT_CAST (self)->base_time;
529 clock = GST_ELEMENT_CLOCK (self);
531 gst_object_ref (clock);
532 GST_OBJECT_UNLOCK (self);
534 time = base_time + start;
536 if (GST_CLOCK_TIME_IS_VALID (latency_max)) {
538 } else if (GST_CLOCK_TIME_IS_VALID (latency_min)) {
541 time += self->latency;
544 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
545 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
546 " latency max %" GST_TIME_FORMAT " latency min %" GST_TIME_FORMAT
547 " current %" GST_TIME_FORMAT ")", GST_TIME_ARGS (time),
548 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
549 GST_TIME_ARGS (start), GST_TIME_ARGS (latency_max),
550 GST_TIME_ARGS (latency_min),
551 GST_TIME_ARGS (gst_clock_get_time (clock)));
553 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
554 gst_object_unref (clock);
555 SRC_STREAM_UNLOCK (self);
557 status = gst_clock_id_wait (self->priv->aggregate_id, NULL);
559 SRC_STREAM_LOCK (self);
560 if (self->priv->aggregate_id) {
561 gst_clock_id_unref (self->priv->aggregate_id);
562 self->priv->aggregate_id = NULL;
565 GST_DEBUG_OBJECT (self, "clock returned %d", status);
568 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
569 SRC_STREAM_UNLOCK (self);
574 SRC_STREAM_UNLOCK (self);
576 return gst_aggregator_iterate_sinkpads (self,
577 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos, NULL);
581 aggregate_func (GstAggregator * self)
583 GstAggregatorPrivate *priv = self->priv;
584 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
585 gboolean timeout = FALSE;
587 if (self->priv->running == FALSE) {
588 GST_DEBUG_OBJECT (self, "Not running anymore");
592 GST_LOG_OBJECT (self, "Checking aggregate");
593 while (priv->send_eos && priv->running) {
594 if (!_wait_and_check (self, &timeout))
597 GST_TRACE_OBJECT (self, "Actually aggregating!");
599 priv->flow_return = klass->aggregate (self, timeout);
601 if (priv->flow_return == GST_FLOW_EOS) {
605 if (priv->flow_return == GST_FLOW_FLUSHING &&
606 g_atomic_int_get (&priv->flush_seeking))
607 priv->flow_return = GST_FLOW_OK;
609 GST_LOG_OBJECT (self, "flow return is %s",
610 gst_flow_get_name (priv->flow_return));
612 if (priv->flow_return != GST_FLOW_OK)
618 _start (GstAggregator * self)
620 self->priv->running = TRUE;
621 self->priv->send_stream_start = TRUE;
622 self->priv->send_segment = TRUE;
623 self->priv->send_eos = TRUE;
624 self->priv->srccaps = NULL;
625 self->priv->flow_return = GST_FLOW_OK;
631 _check_pending_flush_stop (GstAggregatorPad * pad)
633 return (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
637 _stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
641 GST_INFO_OBJECT (self, "%s srcpad task",
642 flush_start ? "Pausing" : "Stopping");
644 self->priv->running = FALSE;
645 SRC_STREAM_BROADCAST (self);
648 res = gst_pad_push_event (self->srcpad, flush_start);
651 gst_pad_stop_task (self->srcpad);
652 SRC_STREAM_BROADCAST (self);
658 _start_srcpad_task (GstAggregator * self)
660 GST_INFO_OBJECT (self, "Starting srcpad task");
662 self->priv->running = TRUE;
663 gst_pad_start_task (GST_PAD (self->srcpad),
664 (GstTaskFunction) aggregate_func, self, NULL);
668 _flush (GstAggregator * self)
670 GstFlowReturn ret = GST_FLOW_OK;
671 GstAggregatorPrivate *priv = self->priv;
672 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
674 GST_DEBUG_OBJECT (self, "Flushing everything");
675 g_atomic_int_set (&priv->send_segment, TRUE);
676 g_atomic_int_set (&priv->flush_seeking, FALSE);
677 g_atomic_int_set (&priv->tags_changed, FALSE);
679 ret = klass->flush (self);
685 _all_flush_stop_received (GstAggregator * self)
688 GstAggregatorPad *tmppad;
690 GST_OBJECT_LOCK (self);
691 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
692 tmppad = (GstAggregatorPad *) tmp->data;
694 if (_check_pending_flush_stop (tmppad) == FALSE) {
695 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
696 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
697 GST_OBJECT_UNLOCK (self);
701 GST_OBJECT_UNLOCK (self);
707 _flush_start (GstAggregator * self, GstAggregatorPad * aggpad, GstEvent * event)
710 GstAggregatorPrivate *priv = self->priv;
711 GstAggregatorPadPrivate *padpriv = aggpad->priv;
713 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
714 /* Remove pad buffer and wake up the streaming thread */
715 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
716 gst_buffer_replace (&tmpbuf, NULL);
717 PAD_STREAM_LOCK (aggpad);
718 if (g_atomic_int_compare_and_exchange (&padpriv->pending_flush_start,
719 TRUE, FALSE) == TRUE) {
720 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
721 g_atomic_int_set (&padpriv->pending_flush_stop, TRUE);
724 if (g_atomic_int_get (&priv->flush_seeking)) {
725 /* If flush_seeking we forward the first FLUSH_START */
726 if (g_atomic_int_compare_and_exchange (&priv->pending_flush_start,
727 TRUE, FALSE) == TRUE) {
729 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
730 _stop_srcpad_task (self, event);
731 priv->flow_return = GST_FLOW_OK;
733 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
734 GST_PAD_STREAM_LOCK (self->srcpad);
735 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
739 gst_event_unref (event);
741 PAD_STREAM_UNLOCK (aggpad);
743 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
744 gst_buffer_replace (&tmpbuf, NULL);
747 /* GstAggregator vmethods default implementations */
749 _sink_event (GstAggregator * self, GstAggregatorPad * aggpad, GstEvent * event)
752 GstPad *pad = GST_PAD (aggpad);
753 GstAggregatorPrivate *priv = self->priv;
755 switch (GST_EVENT_TYPE (event)) {
756 case GST_EVENT_FLUSH_START:
758 _flush_start (self, aggpad, event);
759 /* We forward only in one case: right after flush_seeking */
763 case GST_EVENT_FLUSH_STOP:
765 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
767 _aggpad_flush (aggpad, self);
768 if (g_atomic_int_get (&priv->flush_seeking)) {
769 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
771 if (g_atomic_int_get (&priv->flush_seeking)) {
772 if (_all_flush_stop_received (self)) {
773 /* That means we received FLUSH_STOP/FLUSH_STOP on
774 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
776 gst_pad_push_event (self->srcpad, event);
777 priv->send_eos = TRUE;
779 SRC_STREAM_BROADCAST (self);
781 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
782 GST_PAD_STREAM_UNLOCK (self->srcpad);
783 _start_srcpad_task (self);
788 /* We never forward the event */
793 GST_DEBUG_OBJECT (aggpad, "EOS");
795 /* We still have a buffer, and we don't want the subclass to have to
796 * check for it. Mark pending_eos, eos will be set when steal_buffer is
799 PAD_LOCK_EVENT (aggpad);
800 if (!aggpad->buffer) {
803 aggpad->priv->pending_eos = TRUE;
805 PAD_UNLOCK_EVENT (aggpad);
807 SRC_STREAM_BROADCAST (self);
810 case GST_EVENT_SEGMENT:
812 PAD_LOCK_EVENT (aggpad);
813 gst_event_copy_segment (event, &aggpad->segment);
814 self->priv->seqnum = gst_event_get_seqnum (event);
815 PAD_UNLOCK_EVENT (aggpad);
818 case GST_EVENT_STREAM_START:
826 gst_event_parse_tag (event, &tags);
828 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
829 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
830 gst_event_unref (event);
842 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
843 return gst_pad_event_default (pad, GST_OBJECT (self), event);
846 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
848 gst_event_unref (event);
854 _stop_pad (GstAggregator * self, GstAggregatorPad * pad, gpointer unused_udata)
856 _aggpad_flush (pad, self);
862 _stop (GstAggregator * agg)
864 _reset_flow_values (agg);
866 gst_aggregator_iterate_sinkpads (agg,
867 (GstAggregatorPadForeachFunc) _stop_pad, NULL);
870 gst_tag_list_unref (agg->priv->tags);
871 agg->priv->tags = NULL;
876 /* GstElement vmethods implementations */
877 static GstStateChangeReturn
878 _change_state (GstElement * element, GstStateChange transition)
880 GstStateChangeReturn ret;
881 GstAggregator *self = GST_AGGREGATOR (element);
882 GstAggregatorClass *agg_class = GST_AGGREGATOR_GET_CLASS (self);
885 switch (transition) {
886 case GST_STATE_CHANGE_READY_TO_PAUSED:
887 agg_class->start (self);
894 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
895 transition)) == GST_STATE_CHANGE_FAILURE)
899 switch (transition) {
900 case GST_STATE_CHANGE_PAUSED_TO_READY:
901 agg_class->stop (self);
911 GST_ERROR_OBJECT (element, "parent failed state change");
917 _release_pad (GstElement * element, GstPad * pad)
919 GstAggregator *self = GST_AGGREGATOR (element);
922 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
924 GST_INFO_OBJECT (pad, "Removing pad");
926 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
927 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
928 gst_buffer_replace (&tmpbuf, NULL);
929 gst_element_remove_pad (element, pad);
931 SRC_STREAM_BROADCAST (self);
935 _request_new_pad (GstElement * element,
936 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
939 GstAggregatorPad *agg_pad;
941 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
942 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
944 self = GST_AGGREGATOR (element);
946 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
950 GST_OBJECT_LOCK (element);
951 if (req_name == NULL || strlen (req_name) < 6
952 || !g_str_has_prefix (req_name, "sink_")) {
953 /* no name given when requesting the pad, use next available int */
956 /* parse serial number from requested padname */
957 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
958 if (serial >= priv->padcount)
959 priv->padcount = serial;
962 name = g_strdup_printf ("sink_%u", priv->padcount);
963 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
964 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
967 GST_OBJECT_UNLOCK (element);
973 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
976 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
978 /* add the pad to the element */
979 gst_element_add_pad (element, GST_PAD (agg_pad));
981 return GST_PAD (agg_pad);
986 GstClockTime min, max;
991 _latency_query (GstAggregator * self, GstPad * pad, gpointer user_data)
993 LatencyData *data = user_data;
994 GstClockTime min, max;
998 query = gst_query_new_latency ();
999 res = gst_pad_peer_query (pad, query);
1002 gst_query_parse_latency (query, &live, &min, &max);
1004 GST_LOG_OBJECT (self, "got latency live:%s min:%" G_GINT64_FORMAT
1005 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1007 if (min > data->min)
1010 if (max != GST_CLOCK_TIME_NONE &&
1011 ((data->max != GST_CLOCK_TIME_NONE && max < data->max) ||
1012 (data->max == GST_CLOCK_TIME_NONE)))
1018 gst_query_unref (query);
1024 * gst_aggregator_get_latency:
1025 * @self: a #GstAggregator
1026 * @live: (out) (allow-none): whether @self is live
1027 * @min_latency: (out) (allow-none): the configured minimum latency of @self
1028 * @max_latency: (out) (allow-none): the configured maximum latency of @self
1030 * Retreives the latency values reported by @self in response to the latency
1033 * Typically only called by subclasses.
1036 gst_aggregator_get_latency (GstAggregator * self, gboolean * live,
1037 GstClockTime * min_latency, GstClockTime * max_latency)
1039 GstClockTime min, max;
1041 g_return_if_fail (GST_IS_AGGREGATOR (self));
1043 min = self->priv->latency_min;
1044 max = self->priv->latency_max;
1046 min += self->priv->sub_latency_min;
1047 if (GST_CLOCK_TIME_IS_VALID (max)
1048 && GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max))
1049 max += self->priv->sub_latency_max;
1051 if (GST_CLOCK_TIME_IS_VALID (self->latency)) {
1052 min += self->latency;
1053 if (GST_CLOCK_TIME_IS_VALID (max))
1054 max += self->latency;
1058 *live = self->priv->latency_live;
1066 gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
1071 data.max = GST_CLOCK_TIME_NONE;
1074 /* query upstream's latency */
1075 gst_aggregator_iterate_sinkpads (self,
1076 (GstAggregatorPadForeachFunc) _latency_query, &data);
1078 if (data.live && GST_CLOCK_TIME_IS_VALID (self->latency) &&
1079 self->latency > data.max) {
1080 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1081 ("%s", "Latency too big"),
1082 ("The requested latency value is too big for the current pipeline. "
1083 "Limiting to %" G_GINT64_FORMAT, data.max));
1084 self->latency = data.max;
1087 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (data.min))) {
1088 GST_WARNING_OBJECT (self, "Invalid minimum latency, using 0");
1092 self->priv->latency_live = data.live;
1093 self->priv->latency_min = data.min;
1094 self->priv->latency_max = data.max;
1097 if (GST_CLOCK_TIME_IS_VALID (self->latency)) {
1098 if (GST_CLOCK_TIME_IS_VALID (data.min))
1099 data.min += self->latency;
1100 if (GST_CLOCK_TIME_IS_VALID (data.max))
1101 data.max += self->latency;
1104 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1105 " max:%" G_GINT64_FORMAT, data.live ? "true" : "false", data.min,
1108 gst_query_set_latency (query, data.live, data.min, data.max);
1114 _send_event (GstElement * element, GstEvent * event)
1116 GstAggregator *self = GST_AGGREGATOR (element);
1118 GST_STATE_LOCK (element);
1119 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1120 GST_STATE (element) < GST_STATE_PAUSED) {
1124 GstSeekType start_type, stop_type;
1127 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1128 &start, &stop_type, &stop);
1129 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1130 stop_type, stop, NULL);
1132 self->priv->seqnum = gst_event_get_seqnum (event);
1133 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1135 GST_STATE_UNLOCK (element);
1138 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1143 _src_query (GstAggregator * self, GstQuery * query)
1145 gboolean res = TRUE;
1147 switch (GST_QUERY_TYPE (query)) {
1148 case GST_QUERY_SEEKING:
1152 /* don't pass it along as some (file)sink might claim it does
1153 * whereas with a collectpads in between that will not likely work */
1154 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1155 gst_query_set_seeking (query, format, FALSE, 0, -1);
1160 case GST_QUERY_LATENCY:
1162 return gst_aggregator_query_latency (self, query);
1168 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1175 event_forward_func (GstPad * pad, EventData * evdata)
1177 gboolean ret = TRUE;
1178 GstPad *peer = gst_pad_get_peer (pad);
1179 GstAggregatorPadPrivate *padpriv = GST_AGGREGATOR_PAD (pad)->priv;
1182 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1183 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1184 gst_object_unref (peer);
1188 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1189 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1191 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1192 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1194 if (gst_pad_query (peer, seeking)) {
1197 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1199 if (seekable == FALSE) {
1200 GST_INFO_OBJECT (pad,
1201 "Source not seekable, We failed but it does not matter!");
1206 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1210 if (evdata->flush) {
1211 padpriv->pending_flush_start = FALSE;
1212 padpriv->pending_flush_stop = FALSE;
1215 evdata->one_actually_seeked = TRUE;
1218 evdata->result &= ret;
1220 /* Always send to all pads */
1225 _set_flush_pending (GstAggregator * self, GstAggregatorPad * pad,
1228 pad->priv->pending_flush_start = TRUE;
1229 pad->priv->pending_flush_stop = FALSE;
1235 _forward_event_to_all_sinkpads (GstAggregator * self, GstEvent * event,
1240 evdata.event = event;
1241 evdata.result = TRUE;
1242 evdata.flush = flush;
1243 evdata.one_actually_seeked = FALSE;
1245 /* We first need to set all pads as flushing in a first pass
1246 * as flush_start flush_stop is sometimes sent synchronously
1247 * while we send the seek event */
1249 gst_aggregator_iterate_sinkpads (self,
1250 (GstAggregatorPadForeachFunc) _set_flush_pending, NULL);
1251 gst_pad_forward (self->srcpad, (GstPadForwardFunction) event_forward_func,
1254 gst_event_unref (event);
1260 _do_seek (GstAggregator * self, GstEvent * event)
1265 GstSeekType start_type, stop_type;
1269 GstAggregatorPrivate *priv = self->priv;
1271 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1272 &start, &stop_type, &stop);
1274 GST_INFO_OBJECT (self, "starting SEEK");
1276 flush = flags & GST_SEEK_FLAG_FLUSH;
1279 g_atomic_int_set (&priv->pending_flush_start, TRUE);
1280 g_atomic_int_set (&priv->flush_seeking, TRUE);
1283 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1284 stop_type, stop, NULL);
1286 /* forward the seek upstream */
1287 evdata = _forward_event_to_all_sinkpads (self, event, flush);
1290 if (!evdata.result || !evdata.one_actually_seeked) {
1291 g_atomic_int_set (&priv->flush_seeking, FALSE);
1292 g_atomic_int_set (&priv->pending_flush_start, FALSE);
1295 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1297 return evdata.result;
1301 _src_event (GstAggregator * self, GstEvent * event)
1304 gboolean res = TRUE;
1306 switch (GST_EVENT_TYPE (event)) {
1307 case GST_EVENT_SEEK:
1309 gst_event_ref (event);
1310 res = _do_seek (self, event);
1311 gst_event_unref (event);
1315 case GST_EVENT_NAVIGATION:
1317 /* navigation is rather pointless. */
1319 gst_event_unref (event);
1328 evdata = _forward_event_to_all_sinkpads (self, event, FALSE);
1329 res = evdata.result;
1336 src_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
1338 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1340 return klass->src_event (GST_AGGREGATOR (parent), event);
1344 src_query_func (GstPad * pad, GstObject * parent, GstQuery * query)
1346 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1348 return klass->src_query (GST_AGGREGATOR (parent), query);
1352 src_activate_mode (GstPad * pad,
1353 GstObject * parent, GstPadMode mode, gboolean active)
1355 GstAggregator *self = GST_AGGREGATOR (parent);
1356 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1358 if (klass->src_activate) {
1359 if (klass->src_activate (self, mode, active) == FALSE) {
1364 if (active == TRUE) {
1366 case GST_PAD_MODE_PUSH:
1368 GST_INFO_OBJECT (pad, "Activating pad!");
1369 _start_srcpad_task (self);
1374 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1381 GST_INFO_OBJECT (self, "Deactivating srcpad");
1382 _stop_srcpad_task (self, FALSE);
1388 _sink_query (GstAggregator * self, GstAggregatorPad * aggpad, GstQuery * query)
1390 GstPad *pad = GST_PAD (aggpad);
1392 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1396 gst_aggregator_finalize (GObject * object)
1398 GstAggregator *self = (GstAggregator *) object;
1400 gst_object_unref (self->clock);
1401 g_mutex_clear (&self->priv->setcaps_lock);
1402 g_mutex_clear (&self->priv->src_lock);
1403 g_cond_clear (&self->priv->src_cond);
1405 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1409 gst_aggregator_dispose (GObject * object)
1411 G_OBJECT_CLASS (aggregator_parent_class)->dispose (object);
1415 * gst_aggregator_set_latency_property:
1416 * @agg: a #GstAggregator
1417 * @latency: the new latency value.
1419 * Sets the new latency value to @latency. This value is used to limit the
1420 * amount of time a pad waits for data to appear before considering the pad
1424 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1426 g_return_if_fail (GST_IS_AGGREGATOR (self));
1428 GST_OBJECT_LOCK (self);
1430 if (self->priv->latency_live && self->priv->latency_max != 0 &&
1431 GST_CLOCK_TIME_IS_VALID (latency) && latency > self->priv->latency_max) {
1432 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1433 ("%s", "Latency too big"),
1434 ("The requested latency value is too big for the latency in the "
1435 "current pipeline. Limiting to %" G_GINT64_FORMAT,
1436 self->priv->latency_max));
1437 latency = self->priv->latency_max;
1440 self->latency = latency;
1441 GST_OBJECT_UNLOCK (self);
1445 * gst_aggregator_get_latency_property:
1446 * @agg: a #GstAggregator
1448 * Gets the latency value. See gst_aggregator_set_latency for
1451 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1452 * before a pad is deemed unresponsive. A value of -1 means an
1456 gst_aggregator_get_latency_property (GstAggregator * agg)
1460 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1462 GST_OBJECT_LOCK (agg);
1464 GST_OBJECT_UNLOCK (agg);
1470 gst_aggregator_set_property (GObject * object, guint prop_id,
1471 const GValue * value, GParamSpec * pspec)
1473 GstAggregator *agg = GST_AGGREGATOR (object);
1477 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1486 gst_aggregator_get_property (GObject * object, guint prop_id,
1487 GValue * value, GParamSpec * pspec)
1489 GstAggregator *agg = GST_AGGREGATOR (object);
1493 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1496 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1501 /* GObject vmethods implementations */
1503 gst_aggregator_class_init (GstAggregatorClass * klass)
1505 GObjectClass *gobject_class = (GObjectClass *) klass;
1506 GstElementClass *gstelement_class = (GstElementClass *) klass;
1508 aggregator_parent_class = g_type_class_peek_parent (klass);
1509 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1511 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1512 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1514 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1515 klass->start = _start;
1516 klass->stop = _stop;
1518 klass->sink_event = _sink_event;
1519 klass->sink_query = _sink_query;
1521 klass->src_event = _src_event;
1522 klass->src_query = _src_query;
1524 gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR (_request_new_pad);
1525 gstelement_class->send_event = GST_DEBUG_FUNCPTR (_send_event);
1526 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (_release_pad);
1527 gstelement_class->change_state = GST_DEBUG_FUNCPTR (_change_state);
1529 gobject_class->set_property = gst_aggregator_set_property;
1530 gobject_class->get_property = gst_aggregator_get_property;
1531 gobject_class->finalize = gst_aggregator_finalize;
1532 gobject_class->dispose = gst_aggregator_dispose;
1534 g_object_class_install_property (gobject_class, PROP_LATENCY,
1535 g_param_spec_int64 ("latency", "Buffer latency",
1536 "Number of nanoseconds to wait for a buffer to arrive on a sink pad"
1537 "before the pad is deemed unresponsive (-1 unlimited)", -1,
1538 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1539 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1543 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1545 GstPadTemplate *pad_template;
1546 GstAggregatorPrivate *priv;
1548 g_return_if_fail (klass->aggregate != NULL);
1551 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1552 GstAggregatorPrivate);
1557 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1558 g_return_if_fail (pad_template != NULL);
1560 priv->padcount = -1;
1561 priv->tags_changed = FALSE;
1563 self->priv->latency_live = FALSE;
1564 self->priv->latency_min = self->priv->sub_latency_min = 0;
1565 self->priv->latency_max = self->priv->sub_latency_max = GST_CLOCK_TIME_NONE;
1566 _reset_flow_values (self);
1568 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1570 gst_pad_set_event_function (self->srcpad,
1571 GST_DEBUG_FUNCPTR ((GstPadEventFunction) src_event_func));
1572 gst_pad_set_query_function (self->srcpad,
1573 GST_DEBUG_FUNCPTR ((GstPadQueryFunction) src_query_func));
1574 gst_pad_set_activatemode_function (self->srcpad,
1575 GST_DEBUG_FUNCPTR ((GstPadActivateModeFunction) src_activate_mode));
1577 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1579 self->clock = gst_system_clock_obtain ();
1582 g_mutex_init (&self->priv->setcaps_lock);
1583 g_mutex_init (&self->priv->src_lock);
1584 g_cond_init (&self->priv->src_cond);
1587 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1588 * method to get to the padtemplates */
1590 gst_aggregator_get_type (void)
1592 static volatile gsize type = 0;
1594 if (g_once_init_enter (&type)) {
1596 static const GTypeInfo info = {
1597 sizeof (GstAggregatorClass),
1600 (GClassInitFunc) gst_aggregator_class_init,
1603 sizeof (GstAggregator),
1605 (GInstanceInitFunc) gst_aggregator_init,
1608 _type = g_type_register_static (GST_TYPE_ELEMENT,
1609 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1610 g_once_init_leave (&type, _type);
1615 static GstFlowReturn
1616 _chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1618 GstBuffer *actual_buf = buffer;
1619 GstAggregator *self = GST_AGGREGATOR (object);
1620 GstAggregatorPrivate *priv = self->priv;
1621 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1622 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1624 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1626 PAD_STREAM_LOCK (aggpad);
1628 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1631 if (g_atomic_int_get (&aggpad->priv->pending_eos) == TRUE)
1634 PAD_LOCK_EVENT (aggpad);
1636 while (aggpad->buffer && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1637 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1638 PAD_WAIT_EVENT (aggpad);
1640 PAD_UNLOCK_EVENT (aggpad);
1642 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1645 if (aggclass->clip) {
1646 aggclass->clip (self, aggpad, buffer, &actual_buf);
1649 PAD_LOCK_EVENT (aggpad);
1651 gst_buffer_unref (aggpad->buffer);
1652 aggpad->buffer = actual_buf;
1653 PAD_UNLOCK_EVENT (aggpad);
1654 PAD_STREAM_UNLOCK (aggpad);
1656 if (gst_aggregator_iterate_sinkpads (self,
1657 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos, NULL))
1658 SRC_STREAM_BROADCAST (self);
1660 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1662 return priv->flow_return;
1665 PAD_STREAM_UNLOCK (aggpad);
1667 gst_buffer_unref (buffer);
1668 GST_DEBUG_OBJECT (aggpad, "We are flushing");
1670 return GST_FLOW_FLUSHING;
1673 PAD_STREAM_UNLOCK (aggpad);
1675 gst_buffer_unref (buffer);
1676 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1678 return GST_FLOW_EOS;
1682 pad_query_func (GstPad * pad, GstObject * parent, GstQuery * query)
1684 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1685 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1687 if (GST_QUERY_IS_SERIALIZED (query)) {
1688 PAD_LOCK_EVENT (aggpad);
1690 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
1691 PAD_UNLOCK_EVENT (aggpad);
1695 while (aggpad->buffer
1696 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1697 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1698 PAD_WAIT_EVENT (aggpad);
1700 PAD_UNLOCK_EVENT (aggpad);
1702 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1706 return klass->sink_query (GST_AGGREGATOR (parent),
1707 GST_AGGREGATOR_PAD (pad), query);
1710 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping query");
1715 pad_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
1717 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1718 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1720 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1721 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1722 PAD_LOCK_EVENT (aggpad);
1724 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1725 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
1726 PAD_UNLOCK_EVENT (aggpad);
1730 while (aggpad->buffer
1731 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1732 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1733 PAD_WAIT_EVENT (aggpad);
1735 PAD_UNLOCK_EVENT (aggpad);
1737 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1738 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1742 return klass->sink_event (GST_AGGREGATOR (parent),
1743 GST_AGGREGATOR_PAD (pad), event);
1746 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping event");
1747 if (GST_EVENT_IS_STICKY (event))
1748 gst_pad_store_sticky_event (pad, event);
1749 gst_event_unref (event);
1754 pad_activate_mode_func (GstPad * pad,
1755 GstObject * parent, GstPadMode mode, gboolean active)
1757 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1759 if (active == FALSE) {
1760 PAD_LOCK_EVENT (aggpad);
1761 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1762 gst_buffer_replace (&aggpad->buffer, NULL);
1763 PAD_BROADCAST_EVENT (aggpad);
1764 PAD_UNLOCK_EVENT (aggpad);
1766 g_atomic_int_set (&aggpad->priv->flushing, FALSE);
1767 PAD_LOCK_EVENT (aggpad);
1768 PAD_BROADCAST_EVENT (aggpad);
1769 PAD_UNLOCK_EVENT (aggpad);
1775 /***********************************
1776 * GstAggregatorPad implementation *
1777 ************************************/
1778 static GstPadClass *aggregator_pad_parent_class = NULL;
1779 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1782 _pad_constructed (GObject * object)
1784 GstPad *pad = GST_PAD (object);
1786 gst_pad_set_chain_function (pad,
1787 GST_DEBUG_FUNCPTR ((GstPadChainFunction) _chain));
1788 gst_pad_set_event_function (pad,
1789 GST_DEBUG_FUNCPTR ((GstPadEventFunction) pad_event_func));
1790 gst_pad_set_query_function (pad,
1791 GST_DEBUG_FUNCPTR ((GstPadQueryFunction) pad_query_func));
1792 gst_pad_set_activatemode_function (pad,
1793 GST_DEBUG_FUNCPTR ((GstPadActivateModeFunction) pad_activate_mode_func));
1797 gst_aggregator_pad_finalize (GObject * object)
1799 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1801 g_mutex_clear (&pad->priv->event_lock);
1802 g_cond_clear (&pad->priv->event_cond);
1803 g_mutex_clear (&pad->priv->stream_lock);
1805 G_OBJECT_CLASS (aggregator_pad_parent_class)->finalize (object);
1809 gst_aggregator_pad_dispose (GObject * object)
1811 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1814 buf = gst_aggregator_pad_steal_buffer (pad);
1816 gst_buffer_unref (buf);
1818 G_OBJECT_CLASS (aggregator_pad_parent_class)->dispose (object);
1822 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
1824 GObjectClass *gobject_class = (GObjectClass *) klass;
1826 aggregator_pad_parent_class = g_type_class_peek_parent (klass);
1827 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
1829 gobject_class->constructed = GST_DEBUG_FUNCPTR (_pad_constructed);
1830 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_aggregator_pad_finalize);
1831 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_aggregator_pad_dispose);
1835 gst_aggregator_pad_init (GstAggregatorPad * pad)
1838 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
1839 GstAggregatorPadPrivate);
1842 g_mutex_init (&pad->priv->event_lock);
1843 g_cond_init (&pad->priv->event_cond);
1845 g_mutex_init (&pad->priv->stream_lock);
1849 * gst_aggregator_pad_steal_buffer:
1850 * @pad: the pad to get buffer from
1852 * Steal the ref to the buffer currently queued in @pad.
1854 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
1855 * queued. You should unref the buffer after usage.
1858 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
1860 GstBuffer *buffer = NULL;
1862 PAD_LOCK_EVENT (pad);
1864 GST_TRACE_OBJECT (pad, "Consuming buffer");
1865 buffer = pad->buffer;
1867 if (pad->priv->pending_eos) {
1868 pad->priv->pending_eos = FALSE;
1871 PAD_BROADCAST_EVENT (pad);
1872 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
1874 PAD_UNLOCK_EVENT (pad);
1880 * gst_aggregator_pad_get_buffer:
1881 * @pad: the pad to get buffer from
1883 * Returns: (transfer full): A reference to the buffer in @pad or
1884 * NULL if no buffer was queued. You should unref the buffer after
1888 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
1890 GstBuffer *buffer = NULL;
1892 PAD_LOCK_EVENT (pad);
1894 buffer = gst_buffer_ref (pad->buffer);
1895 PAD_UNLOCK_EVENT (pad);
1901 * gst_aggregator_merge_tags:
1902 * @self: a #GstAggregator
1903 * @tags: a #GstTagList to merge
1904 * @mode: the #GstTagMergeMode to use
1906 * Adds tags to so-called pending tags, which will be processed
1907 * before pushing out data downstream.
1909 * Note that this is provided for convenience, and the subclass is
1910 * not required to use this and can still do tag handling on its own.
1915 gst_aggregator_merge_tags (GstAggregator * self,
1916 const GstTagList * tags, GstTagMergeMode mode)
1920 g_return_if_fail (GST_IS_AGGREGATOR (self));
1921 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
1923 /* FIXME Check if we can use OBJECT lock here! */
1924 GST_OBJECT_LOCK (self);
1926 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
1927 otags = self->priv->tags;
1928 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
1930 gst_tag_list_unref (otags);
1931 self->priv->tags_changed = TRUE;
1932 GST_OBJECT_UNLOCK (self);
1936 * gst_aggregator_set_latency:
1937 * @self: a #GstAggregator
1938 * @min_latency: minimum latency
1939 * @max_latency: maximum latency
1941 * Lets #GstAggregator sub-classes tell the baseclass what their internal
1942 * latency is. Will also post a LATENCY message on the bus so the pipeline
1943 * can reconfigure its global latency.
1946 gst_aggregator_set_latency (GstAggregator * self,
1947 GstClockTime min_latency, GstClockTime max_latency)
1949 g_return_if_fail (GST_IS_AGGREGATOR (self));
1950 g_return_if_fail (max_latency >= min_latency);
1952 GST_OBJECT_LOCK (self);
1953 self->priv->sub_latency_min = min_latency;
1954 self->priv->sub_latency_max = max_latency;
1955 GST_OBJECT_UNLOCK (self);
1957 gst_element_post_message (GST_ELEMENT_CAST (self),
1958 gst_message_new_latency (GST_OBJECT_CAST (self)));