1 /* GStreamer aggregator base class
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. Subclasses should at least implement
33 * the #GstAggregatorClass.aggregate() virtual method.
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 /* Locking order, locks in this element must always be taken in this order
79 * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
80 * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
81 * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
82 * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
83 * standard element object lock -> GST_OBJECT_LOCK(agg)
84 * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
85 * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
86 * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
90 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
91 #define GST_CAT_DEFAULT aggregator_debug
93 /* GstAggregatorPad definitions */
94 #define PAD_LOCK(pad) G_STMT_START { \
95 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
97 g_mutex_lock(&pad->priv->lock); \
98 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
102 #define PAD_UNLOCK(pad) G_STMT_START { \
103 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
105 g_mutex_unlock(&pad->priv->lock); \
106 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
111 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
112 GST_LOG_OBJECT (pad, "Waiting for EVENT on thread %p", \
114 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
115 (&((GstAggregatorPad*)pad)->priv->lock)); \
116 GST_LOG_OBJECT (pad, "DONE Waiting for EVENT on thread %p", \
120 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
121 GST_LOG_OBJECT (pad, "Signaling EVENT from thread %p", \
123 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
127 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
128 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
130 g_mutex_lock(&pad->priv->flush_lock); \
131 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
135 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
136 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
138 g_mutex_unlock(&pad->priv->flush_lock); \
139 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
143 #define SRC_LOCK(self) G_STMT_START { \
144 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
146 g_mutex_lock(&self->priv->src_lock); \
147 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
151 #define SRC_UNLOCK(self) G_STMT_START { \
152 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
154 g_mutex_unlock(&self->priv->src_lock); \
155 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
159 #define SRC_WAIT(self) G_STMT_START { \
160 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
162 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
163 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
167 #define SRC_BROADCAST(self) G_STMT_START { \
168 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
170 if (self->priv->aggregate_id) \
171 gst_clock_id_unschedule (self->priv->aggregate_id); \
172 g_cond_broadcast(&(self->priv->src_cond)); \
175 struct _GstAggregatorPadPrivate
177 /* To always be used atomically */
180 /* Following fields are protected by the PAD_LOCK */
181 gboolean pending_flush_start;
182 gboolean pending_flush_stop;
183 gboolean pending_eos;
190 /* This lock prevents a flush start processing happening while
191 * the chain function is also happening.
197 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
199 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
202 aggpad->priv->eos = FALSE;
203 aggpad->priv->flushing = FALSE;
207 return klass->flush (aggpad, agg);
212 /*************************************
213 * GstAggregator implementation *
214 *************************************/
215 static GstElementClass *aggregator_parent_class = NULL;
217 /* All members are protected by the object lock unless otherwise noted */
219 struct _GstAggregatorPrivate
223 /* Our state is >= PAUSED */
224 gboolean running; /* protected by src_lock */
227 gboolean send_stream_start; /* protected by srcpad stream lock */
228 gboolean send_segment;
229 gboolean flush_seeking;
230 gboolean pending_flush_start;
231 gboolean send_eos; /* protected by srcpad stream lock */
232 GstFlowReturn flow_return;
234 GstCaps *srccaps; /* protected by the srcpad stream lock */
237 gboolean tags_changed;
239 gboolean latency_live; /* protected by src_lock */
240 GstClockTime latency_min; /* protected by src_lock */
241 GstClockTime latency_max; /* protected by src_lock */
243 GstClockTime sub_latency_min; /* protected by src_lock */
244 GstClockTime sub_latency_max; /* protected by src_lock */
247 GstClockID aggregate_id; /* protected by src_lock */
261 gboolean one_actually_seeked;
264 #define DEFAULT_LATENCY 0
274 * gst_aggregator_iterate_sinkpads:
275 * @self: The #GstAggregator
276 * @func: (scope call): The function to call.
277 * @user_data: (closure): The data to pass to @func.
279 * Iterate the sinkpads of aggregator to call a function on them.
281 * This method guarantees that @func will be called only once for each
285 gst_aggregator_iterate_sinkpads (GstAggregator * self,
286 GstAggregatorPadForeachFunc func, gpointer user_data)
288 gboolean result = FALSE;
290 gboolean done = FALSE;
291 GValue item = { 0, };
292 GList *seen_pads = NULL;
294 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
300 switch (gst_iterator_next (iter, &item)) {
301 case GST_ITERATOR_OK:
303 GstAggregatorPad *pad;
305 pad = g_value_get_object (&item);
307 /* if already pushed, skip. FIXME, find something faster to tag pads */
308 if (pad == NULL || g_list_find (seen_pads, pad)) {
309 g_value_reset (&item);
313 GST_LOG_OBJECT (pad, "calling function %s on pad",
314 GST_DEBUG_FUNCPTR_NAME (func));
316 result = func (self, pad, user_data);
320 seen_pads = g_list_prepend (seen_pads, pad);
322 g_value_reset (&item);
325 case GST_ITERATOR_RESYNC:
326 gst_iterator_resync (iter);
328 case GST_ITERATOR_ERROR:
329 GST_ERROR_OBJECT (self,
330 "Could not iterate over internally linked pads");
333 case GST_ITERATOR_DONE:
338 g_value_unset (&item);
339 gst_iterator_free (iter);
341 if (seen_pads == NULL) {
342 GST_DEBUG_OBJECT (self, "No pad seen");
346 g_list_free (seen_pads);
353 gst_aggregator_check_pads_ready (GstAggregator * self)
355 GstAggregatorPad *pad;
358 GST_LOG_OBJECT (self, "checking pads");
360 GST_OBJECT_LOCK (self);
362 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
363 if (sinkpads == NULL)
366 for (l = sinkpads; l != NULL; l = l->next) {
370 if (pad->priv->buffer == NULL && !pad->priv->eos) {
378 GST_OBJECT_UNLOCK (self);
379 GST_LOG_OBJECT (self, "pads are ready");
384 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
385 GST_OBJECT_UNLOCK (self);
390 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
391 GST_OBJECT_UNLOCK (self);
397 gst_aggregator_reset_flow_values (GstAggregator * self)
399 GST_OBJECT_LOCK (self);
400 self->priv->flow_return = GST_FLOW_FLUSHING;
401 self->priv->send_stream_start = TRUE;
402 self->priv->send_segment = TRUE;
403 gst_segment_init (&self->segment, GST_FORMAT_TIME);
404 GST_OBJECT_UNLOCK (self);
408 gst_aggregator_push_mandatory_events (GstAggregator * self)
410 GstAggregatorPrivate *priv = self->priv;
411 GstEvent *segment = NULL;
412 GstEvent *tags = NULL;
414 if (self->priv->send_stream_start) {
417 GST_INFO_OBJECT (self, "pushing stream start");
418 /* stream-start (FIXME: create id based on input ids) */
419 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
420 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
421 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
423 self->priv->send_stream_start = FALSE;
426 if (self->priv->srccaps) {
428 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
429 self->priv->srccaps);
430 if (!gst_pad_push_event (self->srcpad,
431 gst_event_new_caps (self->priv->srccaps))) {
432 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
434 gst_caps_unref (self->priv->srccaps);
435 self->priv->srccaps = NULL;
438 GST_OBJECT_LOCK (self);
439 if (self->priv->send_segment && !self->priv->flush_seeking) {
440 segment = gst_event_new_segment (&self->segment);
442 if (!self->priv->seqnum)
443 self->priv->seqnum = gst_event_get_seqnum (segment);
445 gst_event_set_seqnum (segment, self->priv->seqnum);
446 self->priv->send_segment = FALSE;
448 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
451 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
452 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
453 priv->tags_changed = FALSE;
455 GST_OBJECT_UNLOCK (self);
458 gst_pad_push_event (self->srcpad, segment);
460 gst_pad_push_event (self->srcpad, tags);
465 * gst_aggregator_set_src_caps:
466 * @self: The #GstAggregator
467 * @caps: The #GstCaps to set on the src pad.
469 * Sets the caps to be used on the src pad.
472 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
474 GST_PAD_STREAM_LOCK (self->srcpad);
475 gst_caps_replace (&self->priv->srccaps, caps);
476 gst_aggregator_push_mandatory_events (self);
477 GST_PAD_STREAM_UNLOCK (self->srcpad);
481 * gst_aggregator_finish_buffer:
482 * @self: The #GstAggregator
483 * @buffer: (transfer full): the #GstBuffer to push.
485 * This method will push the provided output buffer downstream. If needed,
486 * mandatory events such as stream-start, caps, and segment events will be
487 * sent before pushing the buffer.
490 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
492 gst_aggregator_push_mandatory_events (self);
494 GST_OBJECT_LOCK (self);
495 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
496 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
497 GST_OBJECT_UNLOCK (self);
498 return gst_pad_push (self->srcpad, buffer);
500 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
501 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
502 GST_OBJECT_UNLOCK (self);
503 gst_buffer_unref (buffer);
509 gst_aggregator_push_eos (GstAggregator * self)
512 gst_aggregator_push_mandatory_events (self);
514 event = gst_event_new_eos ();
516 GST_OBJECT_LOCK (self);
517 self->priv->send_eos = FALSE;
518 gst_event_set_seqnum (event, self->priv->seqnum);
519 GST_OBJECT_UNLOCK (self);
521 gst_pad_push_event (self->srcpad, event);
525 gst_aggregator_get_next_time (GstAggregator * self)
527 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
529 if (klass->get_next_time)
530 return klass->get_next_time (self);
532 return GST_CLOCK_TIME_NONE;
536 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
538 GstClockTime latency_max, latency_min;
546 gst_aggregator_get_latency_unlocked (self, &live, &latency_min, &latency_max);
548 if (gst_aggregator_check_pads_ready (self)) {
549 GST_DEBUG_OBJECT (self, "all pads have data");
555 /* Before waiting, check if we're actually still running */
556 if (!self->priv->running || !self->priv->send_eos) {
562 start = gst_aggregator_get_next_time (self);
564 if (!live || !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self))
565 || !GST_CLOCK_TIME_IS_VALID (start)) {
566 /* We wake up here when something happened, and below
567 * then check if we're ready now. If we return FALSE,
568 * we will be directly called again.
572 GstClockTime base_time, time;
574 GstClockReturn status;
575 GstClockTimeDiff jitter;
577 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
578 GST_TIME_ARGS (start));
580 GST_OBJECT_LOCK (self);
581 base_time = GST_ELEMENT_CAST (self)->base_time;
582 clock = GST_ELEMENT_CLOCK (self);
584 gst_object_ref (clock);
585 GST_OBJECT_UNLOCK (self);
587 time = base_time + start;
590 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
591 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
592 " latency max %" GST_TIME_FORMAT " latency min %" GST_TIME_FORMAT
593 " current %" GST_TIME_FORMAT ")", GST_TIME_ARGS (time),
594 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
595 GST_TIME_ARGS (start), GST_TIME_ARGS (latency_max),
596 GST_TIME_ARGS (latency_min),
597 GST_TIME_ARGS (gst_clock_get_time (clock)));
600 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
601 gst_object_unref (clock);
605 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
608 if (self->priv->aggregate_id) {
609 gst_clock_id_unref (self->priv->aggregate_id);
610 self->priv->aggregate_id = NULL;
613 GST_DEBUG_OBJECT (self,
614 "clock returned %d (jitter: %s%" GST_TIME_FORMAT ")",
615 status, (jitter < 0 ? "-" : " "),
616 GST_TIME_ARGS ((jitter < 0 ? -jitter : jitter)));
619 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
626 res = gst_aggregator_check_pads_ready (self);
633 gst_aggregator_aggregate_func (GstAggregator * self)
635 GstAggregatorPrivate *priv = self->priv;
636 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
637 gboolean timeout = FALSE;
639 if (self->priv->running == FALSE) {
640 GST_DEBUG_OBJECT (self, "Not running anymore");
644 GST_LOG_OBJECT (self, "Checking aggregate");
645 while (priv->send_eos && priv->running) {
646 GstFlowReturn flow_return;
648 if (!gst_aggregator_wait_and_check (self, &timeout))
651 GST_TRACE_OBJECT (self, "Actually aggregating!");
653 flow_return = klass->aggregate (self, timeout);
655 GST_OBJECT_LOCK (self);
656 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking)
657 priv->flow_return = GST_FLOW_OK;
659 priv->flow_return = flow_return;
660 GST_OBJECT_UNLOCK (self);
662 if (flow_return == GST_FLOW_EOS) {
663 gst_aggregator_push_eos (self);
666 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
668 if (flow_return != GST_FLOW_OK)
672 /* Pause the task here, the only ways to get here are:
673 * 1) We're stopping, in which case the task is stopped anyway
674 * 2) We got a flow error above, in which case it might take
675 * some time to forward the flow return upstream and we
676 * would otherwise call the task function over and over
677 * again without doing anything
679 gst_pad_pause_task (self->srcpad);
683 gst_aggregator_start (GstAggregator * self)
685 GstAggregatorClass *klass;
688 self->priv->running = TRUE;
689 self->priv->send_stream_start = TRUE;
690 self->priv->send_segment = TRUE;
691 self->priv->send_eos = TRUE;
692 self->priv->srccaps = NULL;
693 self->priv->flow_return = GST_FLOW_OK;
695 klass = GST_AGGREGATOR_GET_CLASS (self);
698 result = klass->start (self);
706 _check_pending_flush_stop (GstAggregatorPad * pad)
711 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
718 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
722 GST_INFO_OBJECT (self, "%s srcpad task",
723 flush_start ? "Pausing" : "Stopping");
726 self->priv->running = FALSE;
727 SRC_BROADCAST (self);
731 res = gst_pad_push_event (self->srcpad, flush_start);
734 gst_pad_stop_task (self->srcpad);
740 gst_aggregator_start_srcpad_task (GstAggregator * self)
742 GST_INFO_OBJECT (self, "Starting srcpad task");
744 self->priv->running = TRUE;
745 gst_pad_start_task (GST_PAD (self->srcpad),
746 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
750 gst_aggregator_flush (GstAggregator * self)
752 GstFlowReturn ret = GST_FLOW_OK;
753 GstAggregatorPrivate *priv = self->priv;
754 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
756 GST_DEBUG_OBJECT (self, "Flushing everything");
757 GST_OBJECT_LOCK (self);
758 priv->send_segment = TRUE;
759 priv->flush_seeking = FALSE;
760 priv->tags_changed = FALSE;
761 GST_OBJECT_UNLOCK (self);
763 ret = klass->flush (self);
769 /* Called with GstAggregator's object lock held */
772 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
775 GstAggregatorPad *tmppad;
777 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
778 tmppad = (GstAggregatorPad *) tmp->data;
780 if (_check_pending_flush_stop (tmppad) == FALSE) {
781 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
782 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
791 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
794 GstAggregatorPrivate *priv = self->priv;
795 GstAggregatorPadPrivate *padpriv = aggpad->priv;
797 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
799 /* Remove pad buffer and wake up the streaming thread */
800 gst_aggregator_pad_drop_buffer (aggpad);
802 PAD_FLUSH_LOCK (aggpad);
804 if (padpriv->pending_flush_start) {
805 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
807 padpriv->pending_flush_start = FALSE;
808 padpriv->pending_flush_stop = TRUE;
812 GST_OBJECT_LOCK (self);
813 if (priv->flush_seeking) {
814 /* If flush_seeking we forward the first FLUSH_START */
815 if (priv->pending_flush_start) {
816 priv->pending_flush_start = FALSE;
817 GST_OBJECT_UNLOCK (self);
819 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
820 gst_aggregator_stop_srcpad_task (self, event);
821 priv->flow_return = GST_FLOW_OK;
823 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
824 GST_PAD_STREAM_LOCK (self->srcpad);
825 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
828 GST_OBJECT_UNLOCK (self);
829 gst_event_unref (event);
832 GST_OBJECT_UNLOCK (self);
833 gst_event_unref (event);
835 PAD_FLUSH_UNLOCK (aggpad);
837 gst_aggregator_pad_drop_buffer (aggpad);
840 /* GstAggregator vmethods default implementations */
842 gst_aggregator_default_sink_event (GstAggregator * self,
843 GstAggregatorPad * aggpad, GstEvent * event)
846 GstPad *pad = GST_PAD (aggpad);
847 GstAggregatorPrivate *priv = self->priv;
849 switch (GST_EVENT_TYPE (event)) {
850 case GST_EVENT_FLUSH_START:
852 gst_aggregator_flush_start (self, aggpad, event);
853 /* We forward only in one case: right after flush_seeking */
857 case GST_EVENT_FLUSH_STOP:
859 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
861 gst_aggregator_pad_flush (aggpad, self);
862 GST_OBJECT_LOCK (self);
863 if (priv->flush_seeking) {
864 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
865 if (gst_aggregator_all_flush_stop_received_locked (self)) {
866 GST_OBJECT_UNLOCK (self);
867 /* That means we received FLUSH_STOP/FLUSH_STOP on
868 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
869 gst_aggregator_flush (self);
870 gst_pad_push_event (self->srcpad, event);
873 priv->send_eos = TRUE;
874 SRC_BROADCAST (self);
877 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
878 GST_PAD_STREAM_UNLOCK (self->srcpad);
879 gst_aggregator_start_srcpad_task (self);
881 GST_OBJECT_UNLOCK (self);
884 GST_OBJECT_UNLOCK (self);
887 /* We never forward the event */
892 GST_DEBUG_OBJECT (aggpad, "EOS");
894 /* We still have a buffer, and we don't want the subclass to have to
895 * check for it. Mark pending_eos, eos will be set when steal_buffer is
900 if (!aggpad->priv->buffer) {
901 aggpad->priv->eos = TRUE;
903 aggpad->priv->pending_eos = TRUE;
907 SRC_BROADCAST (self);
911 case GST_EVENT_SEGMENT:
913 GST_OBJECT_LOCK (aggpad);
914 gst_event_copy_segment (event, &aggpad->segment);
915 GST_OBJECT_UNLOCK (aggpad);
917 GST_OBJECT_LOCK (self);
918 self->priv->seqnum = gst_event_get_seqnum (event);
919 GST_OBJECT_UNLOCK (self);
922 case GST_EVENT_STREAM_START:
928 /* FIXME: need API to handle GAP events properly */
929 GST_FIXME_OBJECT (self, "implement support for GAP events");
930 /* don't forward GAP events downstream */
937 gst_event_parse_tag (event, &tags);
939 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
940 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
941 gst_event_unref (event);
953 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
954 return gst_pad_event_default (pad, GST_OBJECT (self), event);
957 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
959 gst_event_unref (event);
964 static inline gboolean
965 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
966 gpointer unused_udata)
968 gst_aggregator_pad_flush (pad, self);
974 gst_aggregator_stop (GstAggregator * agg)
976 GstAggregatorClass *klass;
979 gst_aggregator_reset_flow_values (agg);
981 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
983 klass = GST_AGGREGATOR_GET_CLASS (agg);
986 result = klass->stop (agg);
991 gst_tag_list_unref (agg->priv->tags);
992 agg->priv->tags = NULL;
997 /* GstElement vmethods implementations */
998 static GstStateChangeReturn
999 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1001 GstStateChangeReturn ret;
1002 GstAggregator *self = GST_AGGREGATOR (element);
1004 switch (transition) {
1005 case GST_STATE_CHANGE_READY_TO_PAUSED:
1006 if (!gst_aggregator_start (self))
1014 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1015 transition)) == GST_STATE_CHANGE_FAILURE)
1019 switch (transition) {
1020 case GST_STATE_CHANGE_PAUSED_TO_READY:
1021 if (!gst_aggregator_stop (self)) {
1022 /* What to do in this case? Error out? */
1023 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1035 GST_ERROR_OBJECT (element, "parent failed state change");
1040 GST_ERROR_OBJECT (element, "Subclass failed to start");
1041 return GST_STATE_CHANGE_FAILURE;
1046 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1048 GstAggregator *self = GST_AGGREGATOR (element);
1050 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1052 GST_INFO_OBJECT (pad, "Removing pad");
1055 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1056 gst_aggregator_pad_drop_buffer (aggpad);
1057 gst_element_remove_pad (element, pad);
1059 SRC_BROADCAST (self);
1064 gst_aggregator_request_new_pad (GstElement * element,
1065 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1067 GstAggregator *self;
1068 GstAggregatorPad *agg_pad;
1070 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1071 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1073 self = GST_AGGREGATOR (element);
1075 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1079 GST_OBJECT_LOCK (element);
1080 if (req_name == NULL || strlen (req_name) < 6
1081 || !g_str_has_prefix (req_name, "sink_")) {
1082 /* no name given when requesting the pad, use next available int */
1085 /* parse serial number from requested padname */
1086 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1087 if (serial >= priv->padcount)
1088 priv->padcount = serial;
1091 name = g_strdup_printf ("sink_%u", priv->padcount);
1092 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1093 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1096 GST_OBJECT_UNLOCK (element);
1102 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1105 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1107 /* add the pad to the element */
1108 gst_element_add_pad (element, GST_PAD (agg_pad));
1110 return GST_PAD (agg_pad);
1114 gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
1116 gboolean query_ret, live;
1117 GstClockTime our_latency, min, max;
1119 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1122 GST_WARNING_OBJECT (self, "Latency query failed");
1126 gst_query_parse_latency (query, &live, &min, &max);
1129 our_latency = self->priv->latency;
1131 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1132 GST_WARNING_OBJECT (self, "Invalid minimum latency, using 0");
1136 if (G_UNLIKELY (min > max)) {
1137 GST_WARNING_OBJECT (self, "Minimum latency is greater than maximum latency "
1138 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT "). "
1139 "Clamping it at the maximum latency", min, max);
1143 self->priv->latency_live = live;
1144 self->priv->latency_min = min;
1145 self->priv->latency_max = max;
1149 min += self->priv->sub_latency_min;
1150 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1151 && GST_CLOCK_TIME_IS_VALID (max))
1152 max += self->priv->sub_latency_max;
1154 max = GST_CLOCK_TIME_NONE;
1156 if (live && min > max) {
1157 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1158 ("%s", "Latency too big"),
1159 ("The requested latency value is too big for the current pipeline. "
1160 "Limiting to %" G_GINT64_FORMAT, max));
1162 /* FIXME: This could in theory become negative, but in
1163 * that case all is lost anyway */
1164 self->priv->latency -= min - max;
1165 /* FIXME: shouldn't we g_object_notify() the change here? */
1168 SRC_BROADCAST (self);
1171 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1172 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1174 gst_query_set_latency (query, live, min, max);
1180 * gst_aggregator_get_latency_unlocked:
1181 * @self: a #GstAggregator
1182 * @live: (out) (allow-none): whether @self is live
1183 * @min_latency: (out) (allow-none): the configured minimum latency of @self
1184 * @max_latency: (out) (allow-none): the configured maximum latency of @self
1186 * Retreives the latency values reported by @self in response to the latency
1189 * Typically only called by subclasses.
1191 * MUST be called with the src_lock held.
1194 gst_aggregator_get_latency_unlocked (GstAggregator * self, gboolean * live,
1195 GstClockTime * min_latency, GstClockTime * max_latency)
1197 GstClockTime min, max;
1199 g_return_if_fail (GST_IS_AGGREGATOR (self));
1201 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1202 min = self->priv->latency_min;
1203 max = self->priv->latency_max;
1206 min += self->priv->latency;
1207 min += self->priv->sub_latency_min;
1208 if (GST_CLOCK_TIME_IS_VALID (max)
1209 && GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max))
1210 max += self->priv->sub_latency_max;
1212 max = GST_CLOCK_TIME_NONE;
1215 *live = self->priv->latency_live;
1223 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1225 GstAggregator *self = GST_AGGREGATOR (element);
1227 GST_STATE_LOCK (element);
1228 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1229 GST_STATE (element) < GST_STATE_PAUSED) {
1233 GstSeekType start_type, stop_type;
1236 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1237 &start, &stop_type, &stop);
1239 GST_OBJECT_LOCK (self);
1240 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1241 stop_type, stop, NULL);
1242 self->priv->seqnum = gst_event_get_seqnum (event);
1243 GST_OBJECT_UNLOCK (self);
1245 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1247 GST_STATE_UNLOCK (element);
1250 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1255 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1257 gboolean res = TRUE;
1259 switch (GST_QUERY_TYPE (query)) {
1260 case GST_QUERY_SEEKING:
1264 /* don't pass it along as some (file)sink might claim it does
1265 * whereas with a collectpads in between that will not likely work */
1266 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1267 gst_query_set_seeking (query, format, FALSE, 0, -1);
1272 case GST_QUERY_LATENCY:
1273 return gst_aggregator_query_latency (self, query);
1278 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1285 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1287 EventData *evdata = user_data;
1288 gboolean ret = TRUE;
1289 GstPad *peer = gst_pad_get_peer (pad);
1290 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1293 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1294 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1295 gst_object_unref (peer);
1299 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1300 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1302 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1303 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1305 if (gst_pad_query (peer, seeking)) {
1308 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1310 if (seekable == FALSE) {
1311 GST_INFO_OBJECT (pad,
1312 "Source not seekable, We failed but it does not matter!");
1317 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1320 gst_query_unref (seeking);
1323 if (evdata->flush) {
1325 aggpad->priv->pending_flush_start = FALSE;
1326 aggpad->priv->pending_flush_stop = FALSE;
1327 PAD_UNLOCK (aggpad);
1330 evdata->one_actually_seeked = TRUE;
1333 evdata->result &= ret;
1335 /* Always send to all pads */
1340 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1341 GstEvent * event, gboolean flush)
1345 evdata.event = event;
1346 evdata.result = TRUE;
1347 evdata.flush = flush;
1348 evdata.one_actually_seeked = FALSE;
1350 /* We first need to set all pads as flushing in a first pass
1351 * as flush_start flush_stop is sometimes sent synchronously
1352 * while we send the seek event */
1356 GST_OBJECT_LOCK (self);
1357 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1358 GstAggregatorPad *pad = l->data;
1361 pad->priv->pending_flush_start = TRUE;
1362 pad->priv->pending_flush_stop = FALSE;
1365 GST_OBJECT_UNLOCK (self);
1368 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1370 gst_event_unref (event);
1376 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1381 GstSeekType start_type, stop_type;
1385 GstAggregatorPrivate *priv = self->priv;
1387 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1388 &start, &stop_type, &stop);
1390 GST_INFO_OBJECT (self, "starting SEEK");
1392 flush = flags & GST_SEEK_FLAG_FLUSH;
1394 GST_OBJECT_LOCK (self);
1396 priv->pending_flush_start = TRUE;
1397 priv->flush_seeking = TRUE;
1400 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1401 stop_type, stop, NULL);
1402 GST_OBJECT_UNLOCK (self);
1404 /* forward the seek upstream */
1405 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, flush);
1408 if (!evdata.result || !evdata.one_actually_seeked) {
1409 GST_OBJECT_LOCK (self);
1410 priv->flush_seeking = FALSE;
1411 priv->pending_flush_start = FALSE;
1412 GST_OBJECT_UNLOCK (self);
1415 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1417 return evdata.result;
1421 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1424 gboolean res = TRUE;
1426 switch (GST_EVENT_TYPE (event)) {
1427 case GST_EVENT_SEEK:
1429 gst_event_ref (event);
1430 res = gst_aggregator_do_seek (self, event);
1431 gst_event_unref (event);
1435 case GST_EVENT_NAVIGATION:
1437 /* navigation is rather pointless. */
1439 gst_event_unref (event);
1448 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE);
1449 res = evdata.result;
1456 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1459 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1461 return klass->src_event (GST_AGGREGATOR (parent), event);
1465 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1468 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1470 return klass->src_query (GST_AGGREGATOR (parent), query);
1474 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1475 GstObject * parent, GstPadMode mode, gboolean active)
1477 GstAggregator *self = GST_AGGREGATOR (parent);
1478 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1480 if (klass->src_activate) {
1481 if (klass->src_activate (self, mode, active) == FALSE) {
1486 if (active == TRUE) {
1488 case GST_PAD_MODE_PUSH:
1490 GST_INFO_OBJECT (pad, "Activating pad!");
1491 gst_aggregator_start_srcpad_task (self);
1496 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1503 GST_INFO_OBJECT (self, "Deactivating srcpad");
1504 gst_aggregator_stop_srcpad_task (self, FALSE);
1510 gst_aggregator_default_sink_query (GstAggregator * self,
1511 GstAggregatorPad * aggpad, GstQuery * query)
1513 GstPad *pad = GST_PAD (aggpad);
1515 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1519 gst_aggregator_finalize (GObject * object)
1521 GstAggregator *self = (GstAggregator *) object;
1523 g_mutex_clear (&self->priv->src_lock);
1524 g_cond_clear (&self->priv->src_cond);
1526 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1530 * gst_aggregator_set_latency_property:
1531 * @agg: a #GstAggregator
1532 * @latency: the new latency value.
1534 * Sets the new latency value to @latency. This value is used to limit the
1535 * amount of time a pad waits for data to appear before considering the pad
1539 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1542 GstClockTime min, max;
1544 g_return_if_fail (GST_IS_AGGREGATOR (self));
1545 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1548 if (self->priv->latency_live) {
1549 min = self->priv->latency_min;
1550 max = self->priv->latency_max;
1553 min += self->priv->sub_latency_min;
1554 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1555 && GST_CLOCK_TIME_IS_VALID (max))
1556 max += self->priv->sub_latency_max;
1558 max = GST_CLOCK_TIME_NONE;
1560 if (GST_CLOCK_TIME_IS_VALID (max) && min > max) {
1561 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1562 ("%s", "Latency too big"),
1563 ("The requested latency value is too big for the latency in the "
1564 "current pipeline. Limiting to %" G_GINT64_FORMAT, max));
1565 /* FIXME: This could in theory become negative, but in
1566 * that case all is lost anyway */
1567 latency -= min - max;
1568 /* FIXME: shouldn't we g_object_notify() the change here? */
1572 changed = (self->priv->latency != latency);
1573 self->priv->latency = latency;
1576 SRC_BROADCAST (self);
1580 gst_element_post_message (GST_ELEMENT_CAST (self),
1581 gst_message_new_latency (GST_OBJECT_CAST (self)));
1585 * gst_aggregator_get_latency_property:
1586 * @agg: a #GstAggregator
1588 * Gets the latency value. See gst_aggregator_set_latency for
1591 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1592 * before a pad is deemed unresponsive. A value of -1 means an
1596 gst_aggregator_get_latency_property (GstAggregator * agg)
1600 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1602 GST_OBJECT_LOCK (agg);
1603 res = agg->priv->latency;
1604 GST_OBJECT_UNLOCK (agg);
1610 gst_aggregator_set_property (GObject * object, guint prop_id,
1611 const GValue * value, GParamSpec * pspec)
1613 GstAggregator *agg = GST_AGGREGATOR (object);
1617 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1620 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1626 gst_aggregator_get_property (GObject * object, guint prop_id,
1627 GValue * value, GParamSpec * pspec)
1629 GstAggregator *agg = GST_AGGREGATOR (object);
1633 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1636 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1641 /* GObject vmethods implementations */
1643 gst_aggregator_class_init (GstAggregatorClass * klass)
1645 GObjectClass *gobject_class = (GObjectClass *) klass;
1646 GstElementClass *gstelement_class = (GstElementClass *) klass;
1648 aggregator_parent_class = g_type_class_peek_parent (klass);
1649 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1651 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1652 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1654 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1656 klass->sink_event = gst_aggregator_default_sink_event;
1657 klass->sink_query = gst_aggregator_default_sink_query;
1659 klass->src_event = gst_aggregator_default_src_event;
1660 klass->src_query = gst_aggregator_default_src_query;
1662 gstelement_class->request_new_pad =
1663 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1664 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1665 gstelement_class->release_pad =
1666 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1667 gstelement_class->change_state =
1668 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1670 gobject_class->set_property = gst_aggregator_set_property;
1671 gobject_class->get_property = gst_aggregator_get_property;
1672 gobject_class->finalize = gst_aggregator_finalize;
1674 g_object_class_install_property (gobject_class, PROP_LATENCY,
1675 g_param_spec_int64 ("latency", "Buffer latency",
1676 "Additional latency in live mode to allow upstream "
1677 "to take longer to produce buffers for the current "
1679 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1680 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1682 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1686 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1688 GstPadTemplate *pad_template;
1689 GstAggregatorPrivate *priv;
1691 g_return_if_fail (klass->aggregate != NULL);
1694 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1695 GstAggregatorPrivate);
1700 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1701 g_return_if_fail (pad_template != NULL);
1703 priv->padcount = -1;
1704 priv->tags_changed = FALSE;
1706 self->priv->latency_live = FALSE;
1707 self->priv->latency_min = self->priv->sub_latency_min = 0;
1708 self->priv->latency_max = self->priv->sub_latency_max = 0;
1709 gst_aggregator_reset_flow_values (self);
1711 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1713 gst_pad_set_event_function (self->srcpad,
1714 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
1715 gst_pad_set_query_function (self->srcpad,
1716 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
1717 gst_pad_set_activatemode_function (self->srcpad,
1718 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
1720 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1722 self->priv->latency = DEFAULT_LATENCY;
1724 g_mutex_init (&self->priv->src_lock);
1725 g_cond_init (&self->priv->src_cond);
1728 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1729 * method to get to the padtemplates */
1731 gst_aggregator_get_type (void)
1733 static volatile gsize type = 0;
1735 if (g_once_init_enter (&type)) {
1737 static const GTypeInfo info = {
1738 sizeof (GstAggregatorClass),
1741 (GClassInitFunc) gst_aggregator_class_init,
1744 sizeof (GstAggregator),
1746 (GInstanceInitFunc) gst_aggregator_init,
1749 _type = g_type_register_static (GST_TYPE_ELEMENT,
1750 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1751 g_once_init_leave (&type, _type);
1756 static GstFlowReturn
1757 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1759 GstBuffer *actual_buf = buffer;
1760 GstAggregator *self = GST_AGGREGATOR (object);
1761 GstAggregatorPrivate *priv = self->priv;
1762 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1763 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1764 GstFlowReturn flow_return;
1766 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1768 PAD_FLUSH_LOCK (aggpad);
1770 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1774 if (aggpad->priv->pending_eos == TRUE)
1777 while (aggpad->priv->buffer
1778 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1779 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1780 PAD_WAIT_EVENT (aggpad);
1782 PAD_UNLOCK (aggpad);
1784 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1787 if (aggclass->clip) {
1788 aggclass->clip (self, aggpad, buffer, &actual_buf);
1793 if (aggpad->priv->buffer)
1794 gst_buffer_unref (aggpad->priv->buffer);
1795 aggpad->priv->buffer = actual_buf;
1796 PAD_UNLOCK (aggpad);
1797 PAD_FLUSH_UNLOCK (aggpad);
1799 SRC_BROADCAST (self);
1802 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1804 GST_OBJECT_LOCK (self);
1805 flow_return = priv->flow_return;
1806 GST_OBJECT_UNLOCK (self);
1811 PAD_FLUSH_UNLOCK (aggpad);
1813 gst_buffer_unref (buffer);
1814 GST_DEBUG_OBJECT (aggpad, "We are flushing");
1816 return GST_FLOW_FLUSHING;
1819 PAD_UNLOCK (aggpad);
1820 PAD_FLUSH_UNLOCK (aggpad);
1822 gst_buffer_unref (buffer);
1823 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1825 return GST_FLOW_EOS;
1829 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
1832 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1833 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1835 if (GST_QUERY_IS_SERIALIZED (query)) {
1838 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
1839 PAD_UNLOCK (aggpad);
1843 while (aggpad->priv->buffer
1844 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1845 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1846 PAD_WAIT_EVENT (aggpad);
1848 PAD_UNLOCK (aggpad);
1850 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1854 return klass->sink_query (GST_AGGREGATOR (parent),
1855 GST_AGGREGATOR_PAD (pad), query);
1858 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping query");
1863 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
1866 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1867 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1869 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1870 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1873 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1874 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
1875 PAD_UNLOCK (aggpad);
1879 while (aggpad->priv->buffer
1880 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1881 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1882 PAD_WAIT_EVENT (aggpad);
1884 PAD_UNLOCK (aggpad);
1886 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1887 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1891 return klass->sink_event (GST_AGGREGATOR (parent),
1892 GST_AGGREGATOR_PAD (pad), event);
1895 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping event");
1896 if (GST_EVENT_IS_STICKY (event))
1897 gst_pad_store_sticky_event (pad, event);
1898 gst_event_unref (event);
1903 gst_aggregator_pad_activate_mode_func (GstPad * pad,
1904 GstObject * parent, GstPadMode mode, gboolean active)
1906 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1908 if (active == FALSE) {
1910 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1911 gst_buffer_replace (&aggpad->priv->buffer, NULL);
1912 PAD_BROADCAST_EVENT (aggpad);
1913 PAD_UNLOCK (aggpad);
1916 g_atomic_int_set (&aggpad->priv->flushing, FALSE);
1917 PAD_BROADCAST_EVENT (aggpad);
1918 PAD_UNLOCK (aggpad);
1924 /***********************************
1925 * GstAggregatorPad implementation *
1926 ************************************/
1927 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1930 gst_aggregator_pad_constructed (GObject * object)
1932 GstPad *pad = GST_PAD (object);
1934 gst_pad_set_chain_function (pad,
1935 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
1936 gst_pad_set_event_function (pad,
1937 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
1938 gst_pad_set_query_function (pad,
1939 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
1940 gst_pad_set_activatemode_function (pad,
1941 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
1945 gst_aggregator_pad_finalize (GObject * object)
1947 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1949 g_cond_clear (&pad->priv->event_cond);
1950 g_mutex_clear (&pad->priv->flush_lock);
1951 g_mutex_clear (&pad->priv->lock);
1953 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
1957 gst_aggregator_pad_dispose (GObject * object)
1959 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1961 gst_aggregator_pad_drop_buffer (pad);
1963 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
1967 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
1969 GObjectClass *gobject_class = (GObjectClass *) klass;
1971 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
1973 gobject_class->constructed = gst_aggregator_pad_constructed;
1974 gobject_class->finalize = gst_aggregator_pad_finalize;
1975 gobject_class->dispose = gst_aggregator_pad_dispose;
1979 gst_aggregator_pad_init (GstAggregatorPad * pad)
1982 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
1983 GstAggregatorPadPrivate);
1985 pad->priv->buffer = NULL;
1986 g_cond_init (&pad->priv->event_cond);
1988 g_mutex_init (&pad->priv->flush_lock);
1989 g_mutex_init (&pad->priv->lock);
1993 * gst_aggregator_pad_steal_buffer:
1994 * @pad: the pad to get buffer from
1996 * Steal the ref to the buffer currently queued in @pad.
1998 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
1999 * queued. You should unref the buffer after usage.
2002 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2004 GstBuffer *buffer = NULL;
2007 if (pad->priv->buffer) {
2008 GST_TRACE_OBJECT (pad, "Consuming buffer");
2009 buffer = pad->priv->buffer;
2010 pad->priv->buffer = NULL;
2011 if (pad->priv->pending_eos) {
2012 pad->priv->pending_eos = FALSE;
2013 pad->priv->eos = TRUE;
2015 PAD_BROADCAST_EVENT (pad);
2016 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2024 * gst_aggregator_pad_drop_buffer:
2025 * @pad: the pad where to drop any pending buffer
2027 * Drop the buffer currently queued in @pad.
2029 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2032 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2036 buf = gst_aggregator_pad_steal_buffer (pad);
2041 gst_buffer_unref (buf);
2046 * gst_aggregator_pad_get_buffer:
2047 * @pad: the pad to get buffer from
2049 * Returns: (transfer full): A reference to the buffer in @pad or
2050 * NULL if no buffer was queued. You should unref the buffer after
2054 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2056 GstBuffer *buffer = NULL;
2059 if (pad->priv->buffer)
2060 buffer = gst_buffer_ref (pad->priv->buffer);
2067 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2072 is_eos = pad->priv->eos;
2079 * gst_aggregator_merge_tags:
2080 * @self: a #GstAggregator
2081 * @tags: a #GstTagList to merge
2082 * @mode: the #GstTagMergeMode to use
2084 * Adds tags to so-called pending tags, which will be processed
2085 * before pushing out data downstream.
2087 * Note that this is provided for convenience, and the subclass is
2088 * not required to use this and can still do tag handling on its own.
2093 gst_aggregator_merge_tags (GstAggregator * self,
2094 const GstTagList * tags, GstTagMergeMode mode)
2098 g_return_if_fail (GST_IS_AGGREGATOR (self));
2099 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2101 /* FIXME Check if we can use OBJECT lock here! */
2102 GST_OBJECT_LOCK (self);
2104 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2105 otags = self->priv->tags;
2106 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2108 gst_tag_list_unref (otags);
2109 self->priv->tags_changed = TRUE;
2110 GST_OBJECT_UNLOCK (self);
2114 * gst_aggregator_set_latency:
2115 * @self: a #GstAggregator
2116 * @min_latency: minimum latency
2117 * @max_latency: maximum latency
2119 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2120 * latency is. Will also post a LATENCY message on the bus so the pipeline
2121 * can reconfigure its global latency.
2124 gst_aggregator_set_latency (GstAggregator * self,
2125 GstClockTime min_latency, GstClockTime max_latency)
2127 gboolean changed = FALSE;
2129 g_return_if_fail (GST_IS_AGGREGATOR (self));
2130 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2131 g_return_if_fail (max_latency >= min_latency);
2134 if (self->priv->sub_latency_min != min_latency) {
2135 self->priv->sub_latency_min = min_latency;
2138 if (self->priv->sub_latency_max != max_latency) {
2139 self->priv->sub_latency_max = max_latency;
2144 SRC_BROADCAST (self);
2148 gst_element_post_message (GST_ELEMENT_CAST (self),
2149 gst_message_new_latency (GST_OBJECT_CAST (self)));