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;
240 GstClockTime latency_min;
241 GstClockTime latency_max;
243 GstClockTime sub_latency_min;
244 GstClockTime sub_latency_max;
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;
535 /* called with the src STREAM lock */
537 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
539 GstClockTime latency_max, latency_min;
547 GST_OBJECT_LOCK (self);
548 gst_aggregator_get_latency_unlocked (self, &live, &latency_min, &latency_max);
549 GST_OBJECT_UNLOCK (self);
551 if (gst_aggregator_check_pads_ready (self)) {
552 GST_DEBUG_OBJECT (self, "all pads have data");
558 /* Before waiting, check if we're actually still running */
559 if (!self->priv->running || !self->priv->send_eos) {
565 start = gst_aggregator_get_next_time (self);
567 if (!live || !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self))
568 || !GST_CLOCK_TIME_IS_VALID (start)) {
569 /* We wake up here when something happened, and below
570 * then check if we're ready now. If we return FALSE,
571 * we will be directly called again.
575 GstClockTime base_time, time;
577 GstClockReturn status;
578 GstClockTimeDiff jitter;
580 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
581 GST_TIME_ARGS (start));
583 GST_OBJECT_LOCK (self);
584 base_time = GST_ELEMENT_CAST (self)->base_time;
585 clock = GST_ELEMENT_CLOCK (self);
587 gst_object_ref (clock);
589 time = base_time + start;
592 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
593 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
594 " latency max %" GST_TIME_FORMAT " latency min %" GST_TIME_FORMAT
595 " current %" GST_TIME_FORMAT ")", GST_TIME_ARGS (time),
596 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
597 GST_TIME_ARGS (start), GST_TIME_ARGS (latency_max),
598 GST_TIME_ARGS (latency_min),
599 GST_TIME_ARGS (gst_clock_get_time (clock)));
601 GST_OBJECT_UNLOCK (self);
603 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
604 gst_object_unref (clock);
608 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
611 if (self->priv->aggregate_id) {
612 gst_clock_id_unref (self->priv->aggregate_id);
613 self->priv->aggregate_id = NULL;
616 GST_DEBUG_OBJECT (self,
617 "clock returned %d (jitter: %s%" GST_TIME_FORMAT ")",
618 status, (jitter < 0 ? "-" : " "),
619 GST_TIME_ARGS ((jitter < 0 ? -jitter : jitter)));
622 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
629 res = gst_aggregator_check_pads_ready (self);
636 gst_aggregator_aggregate_func (GstAggregator * self)
638 GstAggregatorPrivate *priv = self->priv;
639 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
640 gboolean timeout = FALSE;
642 if (self->priv->running == FALSE) {
643 GST_DEBUG_OBJECT (self, "Not running anymore");
647 GST_LOG_OBJECT (self, "Checking aggregate");
648 while (priv->send_eos && priv->running) {
649 GstFlowReturn flow_return;
651 if (!gst_aggregator_wait_and_check (self, &timeout))
654 GST_TRACE_OBJECT (self, "Actually aggregating!");
656 flow_return = klass->aggregate (self, timeout);
658 GST_OBJECT_LOCK (self);
659 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking)
660 priv->flow_return = GST_FLOW_OK;
662 priv->flow_return = flow_return;
663 GST_OBJECT_UNLOCK (self);
665 if (flow_return == GST_FLOW_EOS) {
666 gst_aggregator_push_eos (self);
669 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
671 if (flow_return != GST_FLOW_OK)
675 /* Pause the task here, the only ways to get here are:
676 * 1) We're stopping, in which case the task is stopped anyway
677 * 2) We got a flow error above, in which case it might take
678 * some time to forward the flow return upstream and we
679 * would otherwise call the task function over and over
680 * again without doing anything
682 gst_pad_pause_task (self->srcpad);
686 gst_aggregator_start (GstAggregator * self)
688 GstAggregatorClass *klass;
691 self->priv->running = TRUE;
692 self->priv->send_stream_start = TRUE;
693 self->priv->send_segment = TRUE;
694 self->priv->send_eos = TRUE;
695 self->priv->srccaps = NULL;
696 self->priv->flow_return = GST_FLOW_OK;
698 klass = GST_AGGREGATOR_GET_CLASS (self);
701 result = klass->start (self);
709 _check_pending_flush_stop (GstAggregatorPad * pad)
714 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
721 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
725 GST_INFO_OBJECT (self, "%s srcpad task",
726 flush_start ? "Pausing" : "Stopping");
729 self->priv->running = FALSE;
730 SRC_BROADCAST (self);
734 res = gst_pad_push_event (self->srcpad, flush_start);
737 gst_pad_stop_task (self->srcpad);
743 gst_aggregator_start_srcpad_task (GstAggregator * self)
745 GST_INFO_OBJECT (self, "Starting srcpad task");
747 self->priv->running = TRUE;
748 gst_pad_start_task (GST_PAD (self->srcpad),
749 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
753 gst_aggregator_flush (GstAggregator * self)
755 GstFlowReturn ret = GST_FLOW_OK;
756 GstAggregatorPrivate *priv = self->priv;
757 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
759 GST_DEBUG_OBJECT (self, "Flushing everything");
760 GST_OBJECT_LOCK (self);
761 priv->send_segment = TRUE;
762 priv->flush_seeking = FALSE;
763 priv->tags_changed = FALSE;
764 GST_OBJECT_UNLOCK (self);
766 ret = klass->flush (self);
772 /* Called with GstAggregator's object lock held */
775 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
778 GstAggregatorPad *tmppad;
780 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
781 tmppad = (GstAggregatorPad *) tmp->data;
783 if (_check_pending_flush_stop (tmppad) == FALSE) {
784 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
785 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
794 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
797 GstAggregatorPrivate *priv = self->priv;
798 GstAggregatorPadPrivate *padpriv = aggpad->priv;
800 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
802 /* Remove pad buffer and wake up the streaming thread */
803 gst_aggregator_pad_drop_buffer (aggpad);
805 PAD_FLUSH_LOCK (aggpad);
807 if (padpriv->pending_flush_start) {
808 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
810 padpriv->pending_flush_start = FALSE;
811 padpriv->pending_flush_stop = TRUE;
815 GST_OBJECT_LOCK (self);
816 if (priv->flush_seeking) {
817 /* If flush_seeking we forward the first FLUSH_START */
818 if (priv->pending_flush_start) {
819 priv->pending_flush_start = FALSE;
820 GST_OBJECT_UNLOCK (self);
822 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
823 gst_aggregator_stop_srcpad_task (self, event);
824 priv->flow_return = GST_FLOW_OK;
826 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
827 GST_PAD_STREAM_LOCK (self->srcpad);
828 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
831 GST_OBJECT_UNLOCK (self);
832 gst_event_unref (event);
835 GST_OBJECT_UNLOCK (self);
836 gst_event_unref (event);
838 PAD_FLUSH_UNLOCK (aggpad);
840 gst_aggregator_pad_drop_buffer (aggpad);
843 /* GstAggregator vmethods default implementations */
845 gst_aggregator_default_sink_event (GstAggregator * self,
846 GstAggregatorPad * aggpad, GstEvent * event)
849 GstPad *pad = GST_PAD (aggpad);
850 GstAggregatorPrivate *priv = self->priv;
852 switch (GST_EVENT_TYPE (event)) {
853 case GST_EVENT_FLUSH_START:
855 gst_aggregator_flush_start (self, aggpad, event);
856 /* We forward only in one case: right after flush_seeking */
860 case GST_EVENT_FLUSH_STOP:
862 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
864 gst_aggregator_pad_flush (aggpad, self);
865 GST_OBJECT_LOCK (self);
866 if (priv->flush_seeking) {
867 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
868 if (gst_aggregator_all_flush_stop_received_locked (self)) {
869 GST_OBJECT_UNLOCK (self);
870 /* That means we received FLUSH_STOP/FLUSH_STOP on
871 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
872 gst_aggregator_flush (self);
873 gst_pad_push_event (self->srcpad, event);
876 priv->send_eos = TRUE;
877 SRC_BROADCAST (self);
880 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
881 GST_PAD_STREAM_UNLOCK (self->srcpad);
882 gst_aggregator_start_srcpad_task (self);
884 GST_OBJECT_UNLOCK (self);
887 GST_OBJECT_UNLOCK (self);
890 /* We never forward the event */
895 GST_DEBUG_OBJECT (aggpad, "EOS");
897 /* We still have a buffer, and we don't want the subclass to have to
898 * check for it. Mark pending_eos, eos will be set when steal_buffer is
903 if (!aggpad->priv->buffer) {
904 aggpad->priv->eos = TRUE;
906 aggpad->priv->pending_eos = TRUE;
910 SRC_BROADCAST (self);
914 case GST_EVENT_SEGMENT:
916 GST_OBJECT_LOCK (aggpad);
917 gst_event_copy_segment (event, &aggpad->segment);
918 GST_OBJECT_UNLOCK (aggpad);
920 GST_OBJECT_LOCK (self);
921 self->priv->seqnum = gst_event_get_seqnum (event);
922 GST_OBJECT_UNLOCK (self);
925 case GST_EVENT_STREAM_START:
931 /* FIXME: need API to handle GAP events properly */
932 GST_FIXME_OBJECT (self, "implement support for GAP events");
933 /* don't forward GAP events downstream */
940 gst_event_parse_tag (event, &tags);
942 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
943 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
944 gst_event_unref (event);
956 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
957 return gst_pad_event_default (pad, GST_OBJECT (self), event);
960 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
962 gst_event_unref (event);
967 static inline gboolean
968 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
969 gpointer unused_udata)
971 gst_aggregator_pad_flush (pad, self);
977 gst_aggregator_stop (GstAggregator * agg)
979 GstAggregatorClass *klass;
982 gst_aggregator_reset_flow_values (agg);
984 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
986 klass = GST_AGGREGATOR_GET_CLASS (agg);
989 result = klass->stop (agg);
994 gst_tag_list_unref (agg->priv->tags);
995 agg->priv->tags = NULL;
1000 /* GstElement vmethods implementations */
1001 static GstStateChangeReturn
1002 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1004 GstStateChangeReturn ret;
1005 GstAggregator *self = GST_AGGREGATOR (element);
1007 switch (transition) {
1008 case GST_STATE_CHANGE_READY_TO_PAUSED:
1009 if (!gst_aggregator_start (self))
1017 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1018 transition)) == GST_STATE_CHANGE_FAILURE)
1022 switch (transition) {
1023 case GST_STATE_CHANGE_PAUSED_TO_READY:
1024 if (!gst_aggregator_stop (self)) {
1025 /* What to do in this case? Error out? */
1026 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1038 GST_ERROR_OBJECT (element, "parent failed state change");
1043 GST_ERROR_OBJECT (element, "Subclass failed to start");
1044 return GST_STATE_CHANGE_FAILURE;
1049 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1051 GstAggregator *self = GST_AGGREGATOR (element);
1053 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1055 GST_INFO_OBJECT (pad, "Removing pad");
1058 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1059 gst_aggregator_pad_drop_buffer (aggpad);
1060 gst_element_remove_pad (element, pad);
1062 SRC_BROADCAST (self);
1067 gst_aggregator_request_new_pad (GstElement * element,
1068 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1070 GstAggregator *self;
1071 GstAggregatorPad *agg_pad;
1073 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1074 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1076 self = GST_AGGREGATOR (element);
1078 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1082 GST_OBJECT_LOCK (element);
1083 if (req_name == NULL || strlen (req_name) < 6
1084 || !g_str_has_prefix (req_name, "sink_")) {
1085 /* no name given when requesting the pad, use next available int */
1088 /* parse serial number from requested padname */
1089 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1090 if (serial >= priv->padcount)
1091 priv->padcount = serial;
1094 name = g_strdup_printf ("sink_%u", priv->padcount);
1095 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1096 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1099 GST_OBJECT_UNLOCK (element);
1105 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1108 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1110 /* add the pad to the element */
1111 gst_element_add_pad (element, GST_PAD (agg_pad));
1113 return GST_PAD (agg_pad);
1118 GstClockTime min, max;
1123 query_upstream_latency_fold (const GValue * item, GValue * ret,
1126 GstPad *pad = g_value_get_object (item);
1127 LatencyData *data = user_data;
1128 GstClockTime min, max;
1132 query = gst_query_new_latency ();
1133 res = gst_pad_peer_query (GST_PAD_CAST (pad), query);
1136 gst_query_parse_latency (query, &live, &min, &max);
1138 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
1139 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1142 if (min > data->min)
1145 if (data->max == GST_CLOCK_TIME_NONE)
1147 else if (max < data->max)
1153 GST_LOG_OBJECT (pad, "latency query failed");
1154 g_value_set_boolean (ret, FALSE);
1157 gst_query_unref (query);
1163 gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
1166 GstIteratorResult res;
1167 GValue ret = G_VALUE_INIT;
1170 GstClockTime our_latency;
1172 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (self));
1173 g_value_init (&ret, G_TYPE_BOOLEAN);
1177 data.max = GST_CLOCK_TIME_NONE;
1179 g_value_set_boolean (&ret, TRUE);
1181 /* query upstream's latency */
1182 res = gst_iterator_fold (it, query_upstream_latency_fold, &ret, &data);
1184 case GST_ITERATOR_OK:
1185 g_assert_not_reached ();
1187 case GST_ITERATOR_DONE:
1189 case GST_ITERATOR_ERROR:
1190 g_value_set_boolean (&ret, FALSE);
1192 case GST_ITERATOR_RESYNC:
1193 gst_iterator_resync (it);
1196 g_assert_not_reached ();
1199 gst_iterator_free (it);
1200 query_ret = g_value_get_boolean (&ret);
1202 GST_WARNING_OBJECT (self, "Latency query failed");
1206 GST_OBJECT_LOCK (self);
1207 our_latency = self->priv->latency;
1209 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (data.min))) {
1210 GST_WARNING_OBJECT (self, "Invalid minimum latency, using 0");
1214 if (G_UNLIKELY (data.min > data.max)) {
1215 GST_WARNING_OBJECT (self, "Minimum latency is greater than maximum latency "
1216 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT "). "
1217 "Clamping it at the maximum latency", data.min, data.max);
1218 data.min = data.max;
1221 self->priv->latency_live = data.live;
1222 self->priv->latency_min = data.min;
1223 self->priv->latency_max = data.max;
1226 data.min += our_latency;
1227 data.min += self->priv->sub_latency_min;
1228 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1229 && GST_CLOCK_TIME_IS_VALID (data.max))
1230 data.max += self->priv->sub_latency_max;
1232 data.max = GST_CLOCK_TIME_NONE;
1234 if (data.live && data.min > data.max) {
1235 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1236 ("%s", "Latency too big"),
1237 ("The requested latency value is too big for the current pipeline. "
1238 "Limiting to %" G_GINT64_FORMAT, data.max));
1239 data.min = data.max;
1240 /* FIXME: This could in theory become negative, but in
1241 * that case all is lost anyway */
1242 self->priv->latency -= data.min - data.max;
1243 /* FIXME: shouldn't we g_object_notify() the change here? */
1246 GST_OBJECT_UNLOCK (self);
1248 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1249 " max:%" G_GINT64_FORMAT, data.live ? "true" : "false", data.min,
1252 gst_query_set_latency (query, data.live, data.min, data.max);
1258 * gst_aggregator_get_latency_unlocked:
1259 * @self: a #GstAggregator
1260 * @live: (out) (allow-none): whether @self is live
1261 * @min_latency: (out) (allow-none): the configured minimum latency of @self
1262 * @max_latency: (out) (allow-none): the configured maximum latency of @self
1264 * Retreives the latency values reported by @self in response to the latency
1267 * Typically only called by subclasses.
1269 * MUST be called with the object lock held.
1272 gst_aggregator_get_latency_unlocked (GstAggregator * self, gboolean * live,
1273 GstClockTime * min_latency, GstClockTime * max_latency)
1275 GstClockTime min, max;
1277 g_return_if_fail (GST_IS_AGGREGATOR (self));
1279 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1280 min = self->priv->latency_min;
1281 max = self->priv->latency_max;
1284 min += self->priv->latency;
1285 min += self->priv->sub_latency_min;
1286 if (GST_CLOCK_TIME_IS_VALID (max)
1287 && GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max))
1288 max += self->priv->sub_latency_max;
1290 max = GST_CLOCK_TIME_NONE;
1293 *live = self->priv->latency_live;
1301 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1303 GstAggregator *self = GST_AGGREGATOR (element);
1305 GST_STATE_LOCK (element);
1306 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1307 GST_STATE (element) < GST_STATE_PAUSED) {
1311 GstSeekType start_type, stop_type;
1314 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1315 &start, &stop_type, &stop);
1317 GST_OBJECT_LOCK (self);
1318 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1319 stop_type, stop, NULL);
1320 self->priv->seqnum = gst_event_get_seqnum (event);
1321 GST_OBJECT_UNLOCK (self);
1323 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1325 GST_STATE_UNLOCK (element);
1328 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1333 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1335 gboolean res = TRUE;
1337 switch (GST_QUERY_TYPE (query)) {
1338 case GST_QUERY_SEEKING:
1342 /* don't pass it along as some (file)sink might claim it does
1343 * whereas with a collectpads in between that will not likely work */
1344 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1345 gst_query_set_seeking (query, format, FALSE, 0, -1);
1350 case GST_QUERY_LATENCY:
1354 ret = gst_aggregator_query_latency (self, query);
1355 /* Wake up the src thread again, due to changed latencies
1356 * or changed live-ness we might have to adjust if we wait
1357 * on a deadline at all and how long.
1358 * This is only to unschedule the clock id, we don't really care
1359 * about the GCond here.
1362 SRC_BROADCAST (self);
1370 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1377 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1379 EventData *evdata = user_data;
1380 gboolean ret = TRUE;
1381 GstPad *peer = gst_pad_get_peer (pad);
1382 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1385 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1386 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1387 gst_object_unref (peer);
1391 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1392 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1394 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1395 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1397 if (gst_pad_query (peer, seeking)) {
1400 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1402 if (seekable == FALSE) {
1403 GST_INFO_OBJECT (pad,
1404 "Source not seekable, We failed but it does not matter!");
1409 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1412 gst_query_unref (seeking);
1415 if (evdata->flush) {
1417 aggpad->priv->pending_flush_start = FALSE;
1418 aggpad->priv->pending_flush_stop = FALSE;
1419 PAD_UNLOCK (aggpad);
1422 evdata->one_actually_seeked = TRUE;
1425 evdata->result &= ret;
1427 /* Always send to all pads */
1432 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1433 GstEvent * event, gboolean flush)
1437 evdata.event = event;
1438 evdata.result = TRUE;
1439 evdata.flush = flush;
1440 evdata.one_actually_seeked = FALSE;
1442 /* We first need to set all pads as flushing in a first pass
1443 * as flush_start flush_stop is sometimes sent synchronously
1444 * while we send the seek event */
1448 GST_OBJECT_LOCK (self);
1449 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1450 GstAggregatorPad *pad = l->data;
1453 pad->priv->pending_flush_start = TRUE;
1454 pad->priv->pending_flush_stop = FALSE;
1457 GST_OBJECT_UNLOCK (self);
1460 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1462 gst_event_unref (event);
1468 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1473 GstSeekType start_type, stop_type;
1477 GstAggregatorPrivate *priv = self->priv;
1479 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1480 &start, &stop_type, &stop);
1482 GST_INFO_OBJECT (self, "starting SEEK");
1484 flush = flags & GST_SEEK_FLAG_FLUSH;
1486 GST_OBJECT_LOCK (self);
1488 priv->pending_flush_start = TRUE;
1489 priv->flush_seeking = TRUE;
1492 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1493 stop_type, stop, NULL);
1494 GST_OBJECT_UNLOCK (self);
1496 /* forward the seek upstream */
1497 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, flush);
1500 if (!evdata.result || !evdata.one_actually_seeked) {
1501 GST_OBJECT_LOCK (self);
1502 priv->flush_seeking = FALSE;
1503 priv->pending_flush_start = FALSE;
1504 GST_OBJECT_UNLOCK (self);
1507 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1509 return evdata.result;
1513 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1516 gboolean res = TRUE;
1518 switch (GST_EVENT_TYPE (event)) {
1519 case GST_EVENT_SEEK:
1521 gst_event_ref (event);
1522 res = gst_aggregator_do_seek (self, event);
1523 gst_event_unref (event);
1527 case GST_EVENT_NAVIGATION:
1529 /* navigation is rather pointless. */
1531 gst_event_unref (event);
1540 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE);
1541 res = evdata.result;
1548 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1551 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1553 return klass->src_event (GST_AGGREGATOR (parent), event);
1557 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1560 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1562 return klass->src_query (GST_AGGREGATOR (parent), query);
1566 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1567 GstObject * parent, GstPadMode mode, gboolean active)
1569 GstAggregator *self = GST_AGGREGATOR (parent);
1570 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1572 if (klass->src_activate) {
1573 if (klass->src_activate (self, mode, active) == FALSE) {
1578 if (active == TRUE) {
1580 case GST_PAD_MODE_PUSH:
1582 GST_INFO_OBJECT (pad, "Activating pad!");
1583 gst_aggregator_start_srcpad_task (self);
1588 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1595 GST_INFO_OBJECT (self, "Deactivating srcpad");
1596 gst_aggregator_stop_srcpad_task (self, FALSE);
1602 gst_aggregator_default_sink_query (GstAggregator * self,
1603 GstAggregatorPad * aggpad, GstQuery * query)
1605 GstPad *pad = GST_PAD (aggpad);
1607 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1611 gst_aggregator_finalize (GObject * object)
1613 GstAggregator *self = (GstAggregator *) object;
1615 g_mutex_clear (&self->priv->src_lock);
1616 g_cond_clear (&self->priv->src_cond);
1618 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1622 * gst_aggregator_set_latency_property:
1623 * @agg: a #GstAggregator
1624 * @latency: the new latency value.
1626 * Sets the new latency value to @latency. This value is used to limit the
1627 * amount of time a pad waits for data to appear before considering the pad
1631 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1634 GstClockTime min, max;
1636 g_return_if_fail (GST_IS_AGGREGATOR (self));
1637 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1639 GST_OBJECT_LOCK (self);
1640 if (self->priv->latency_live) {
1641 min = self->priv->latency_min;
1642 max = self->priv->latency_max;
1645 min += self->priv->sub_latency_min;
1646 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1647 && GST_CLOCK_TIME_IS_VALID (max))
1648 max += self->priv->sub_latency_max;
1650 max = GST_CLOCK_TIME_NONE;
1652 if (GST_CLOCK_TIME_IS_VALID (max) && min > max) {
1653 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1654 ("%s", "Latency too big"),
1655 ("The requested latency value is too big for the latency in the "
1656 "current pipeline. Limiting to %" G_GINT64_FORMAT, max));
1657 /* FIXME: This could in theory become negative, but in
1658 * that case all is lost anyway */
1659 latency -= min - max;
1660 /* FIXME: shouldn't we g_object_notify() the change here? */
1664 changed = (self->priv->latency != latency);
1665 self->priv->latency = latency;
1666 GST_OBJECT_UNLOCK (self);
1669 gst_element_post_message (GST_ELEMENT_CAST (self),
1670 gst_message_new_latency (GST_OBJECT_CAST (self)));
1674 * gst_aggregator_get_latency_property:
1675 * @agg: a #GstAggregator
1677 * Gets the latency value. See gst_aggregator_set_latency for
1680 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1681 * before a pad is deemed unresponsive. A value of -1 means an
1685 gst_aggregator_get_latency_property (GstAggregator * agg)
1689 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1691 GST_OBJECT_LOCK (agg);
1692 res = agg->priv->latency;
1693 GST_OBJECT_UNLOCK (agg);
1699 gst_aggregator_set_property (GObject * object, guint prop_id,
1700 const GValue * value, GParamSpec * pspec)
1702 GstAggregator *agg = GST_AGGREGATOR (object);
1706 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1709 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1715 gst_aggregator_get_property (GObject * object, guint prop_id,
1716 GValue * value, GParamSpec * pspec)
1718 GstAggregator *agg = GST_AGGREGATOR (object);
1722 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1725 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1730 /* GObject vmethods implementations */
1732 gst_aggregator_class_init (GstAggregatorClass * klass)
1734 GObjectClass *gobject_class = (GObjectClass *) klass;
1735 GstElementClass *gstelement_class = (GstElementClass *) klass;
1737 aggregator_parent_class = g_type_class_peek_parent (klass);
1738 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1740 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1741 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1743 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1745 klass->sink_event = gst_aggregator_default_sink_event;
1746 klass->sink_query = gst_aggregator_default_sink_query;
1748 klass->src_event = gst_aggregator_default_src_event;
1749 klass->src_query = gst_aggregator_default_src_query;
1751 gstelement_class->request_new_pad =
1752 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1753 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1754 gstelement_class->release_pad =
1755 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1756 gstelement_class->change_state =
1757 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1759 gobject_class->set_property = gst_aggregator_set_property;
1760 gobject_class->get_property = gst_aggregator_get_property;
1761 gobject_class->finalize = gst_aggregator_finalize;
1763 g_object_class_install_property (gobject_class, PROP_LATENCY,
1764 g_param_spec_int64 ("latency", "Buffer latency",
1765 "Additional latency in live mode to allow upstream "
1766 "to take longer to produce buffers for the current "
1768 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1769 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1771 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1775 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1777 GstPadTemplate *pad_template;
1778 GstAggregatorPrivate *priv;
1780 g_return_if_fail (klass->aggregate != NULL);
1783 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1784 GstAggregatorPrivate);
1789 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1790 g_return_if_fail (pad_template != NULL);
1792 priv->padcount = -1;
1793 priv->tags_changed = FALSE;
1795 self->priv->latency_live = FALSE;
1796 self->priv->latency_min = self->priv->sub_latency_min = 0;
1797 self->priv->latency_max = self->priv->sub_latency_max = 0;
1798 gst_aggregator_reset_flow_values (self);
1800 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1802 gst_pad_set_event_function (self->srcpad,
1803 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
1804 gst_pad_set_query_function (self->srcpad,
1805 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
1806 gst_pad_set_activatemode_function (self->srcpad,
1807 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
1809 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1811 self->priv->latency = DEFAULT_LATENCY;
1813 g_mutex_init (&self->priv->src_lock);
1814 g_cond_init (&self->priv->src_cond);
1817 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1818 * method to get to the padtemplates */
1820 gst_aggregator_get_type (void)
1822 static volatile gsize type = 0;
1824 if (g_once_init_enter (&type)) {
1826 static const GTypeInfo info = {
1827 sizeof (GstAggregatorClass),
1830 (GClassInitFunc) gst_aggregator_class_init,
1833 sizeof (GstAggregator),
1835 (GInstanceInitFunc) gst_aggregator_init,
1838 _type = g_type_register_static (GST_TYPE_ELEMENT,
1839 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1840 g_once_init_leave (&type, _type);
1845 static GstFlowReturn
1846 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1848 GstBuffer *actual_buf = buffer;
1849 GstAggregator *self = GST_AGGREGATOR (object);
1850 GstAggregatorPrivate *priv = self->priv;
1851 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1852 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1853 GstFlowReturn flow_return;
1855 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1857 PAD_FLUSH_LOCK (aggpad);
1859 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1863 if (aggpad->priv->pending_eos == TRUE)
1866 while (aggpad->priv->buffer
1867 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1868 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1869 PAD_WAIT_EVENT (aggpad);
1871 PAD_UNLOCK (aggpad);
1873 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1876 if (aggclass->clip) {
1877 aggclass->clip (self, aggpad, buffer, &actual_buf);
1882 if (aggpad->priv->buffer)
1883 gst_buffer_unref (aggpad->priv->buffer);
1884 aggpad->priv->buffer = actual_buf;
1885 PAD_UNLOCK (aggpad);
1886 PAD_FLUSH_UNLOCK (aggpad);
1888 SRC_BROADCAST (self);
1891 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1893 GST_OBJECT_LOCK (self);
1894 flow_return = priv->flow_return;
1895 GST_OBJECT_UNLOCK (self);
1900 PAD_FLUSH_UNLOCK (aggpad);
1902 gst_buffer_unref (buffer);
1903 GST_DEBUG_OBJECT (aggpad, "We are flushing");
1905 return GST_FLOW_FLUSHING;
1908 PAD_UNLOCK (aggpad);
1909 PAD_FLUSH_UNLOCK (aggpad);
1911 gst_buffer_unref (buffer);
1912 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1914 return GST_FLOW_EOS;
1918 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
1921 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1922 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1924 if (GST_QUERY_IS_SERIALIZED (query)) {
1927 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
1928 PAD_UNLOCK (aggpad);
1932 while (aggpad->priv->buffer
1933 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1934 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1935 PAD_WAIT_EVENT (aggpad);
1937 PAD_UNLOCK (aggpad);
1939 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1943 return klass->sink_query (GST_AGGREGATOR (parent),
1944 GST_AGGREGATOR_PAD (pad), query);
1947 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping query");
1952 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
1955 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1956 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1958 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1959 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1962 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1963 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
1964 PAD_UNLOCK (aggpad);
1968 while (aggpad->priv->buffer
1969 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1970 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1971 PAD_WAIT_EVENT (aggpad);
1973 PAD_UNLOCK (aggpad);
1975 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1976 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1980 return klass->sink_event (GST_AGGREGATOR (parent),
1981 GST_AGGREGATOR_PAD (pad), event);
1984 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping event");
1985 if (GST_EVENT_IS_STICKY (event))
1986 gst_pad_store_sticky_event (pad, event);
1987 gst_event_unref (event);
1992 gst_aggregator_pad_activate_mode_func (GstPad * pad,
1993 GstObject * parent, GstPadMode mode, gboolean active)
1995 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1997 if (active == FALSE) {
1999 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
2000 gst_buffer_replace (&aggpad->priv->buffer, NULL);
2001 PAD_BROADCAST_EVENT (aggpad);
2002 PAD_UNLOCK (aggpad);
2005 g_atomic_int_set (&aggpad->priv->flushing, FALSE);
2006 PAD_BROADCAST_EVENT (aggpad);
2007 PAD_UNLOCK (aggpad);
2013 /***********************************
2014 * GstAggregatorPad implementation *
2015 ************************************/
2016 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2019 gst_aggregator_pad_constructed (GObject * object)
2021 GstPad *pad = GST_PAD (object);
2023 gst_pad_set_chain_function (pad,
2024 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2025 gst_pad_set_event_function (pad,
2026 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
2027 gst_pad_set_query_function (pad,
2028 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2029 gst_pad_set_activatemode_function (pad,
2030 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2034 gst_aggregator_pad_finalize (GObject * object)
2036 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2038 g_cond_clear (&pad->priv->event_cond);
2039 g_mutex_clear (&pad->priv->flush_lock);
2040 g_mutex_clear (&pad->priv->lock);
2042 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2046 gst_aggregator_pad_dispose (GObject * object)
2048 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2050 gst_aggregator_pad_drop_buffer (pad);
2052 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2056 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2058 GObjectClass *gobject_class = (GObjectClass *) klass;
2060 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2062 gobject_class->constructed = gst_aggregator_pad_constructed;
2063 gobject_class->finalize = gst_aggregator_pad_finalize;
2064 gobject_class->dispose = gst_aggregator_pad_dispose;
2068 gst_aggregator_pad_init (GstAggregatorPad * pad)
2071 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2072 GstAggregatorPadPrivate);
2074 pad->priv->buffer = NULL;
2075 g_cond_init (&pad->priv->event_cond);
2077 g_mutex_init (&pad->priv->flush_lock);
2078 g_mutex_init (&pad->priv->lock);
2082 * gst_aggregator_pad_steal_buffer:
2083 * @pad: the pad to get buffer from
2085 * Steal the ref to the buffer currently queued in @pad.
2087 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2088 * queued. You should unref the buffer after usage.
2091 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2093 GstBuffer *buffer = NULL;
2096 if (pad->priv->buffer) {
2097 GST_TRACE_OBJECT (pad, "Consuming buffer");
2098 buffer = pad->priv->buffer;
2099 pad->priv->buffer = NULL;
2100 if (pad->priv->pending_eos) {
2101 pad->priv->pending_eos = FALSE;
2102 pad->priv->eos = TRUE;
2104 PAD_BROADCAST_EVENT (pad);
2105 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2113 * gst_aggregator_pad_drop_buffer:
2114 * @pad: the pad where to drop any pending buffer
2116 * Drop the buffer currently queued in @pad.
2118 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2121 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2125 buf = gst_aggregator_pad_steal_buffer (pad);
2130 gst_buffer_unref (buf);
2135 * gst_aggregator_pad_get_buffer:
2136 * @pad: the pad to get buffer from
2138 * Returns: (transfer full): A reference to the buffer in @pad or
2139 * NULL if no buffer was queued. You should unref the buffer after
2143 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2145 GstBuffer *buffer = NULL;
2148 if (pad->priv->buffer)
2149 buffer = gst_buffer_ref (pad->priv->buffer);
2156 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2161 is_eos = pad->priv->eos;
2168 * gst_aggregator_merge_tags:
2169 * @self: a #GstAggregator
2170 * @tags: a #GstTagList to merge
2171 * @mode: the #GstTagMergeMode to use
2173 * Adds tags to so-called pending tags, which will be processed
2174 * before pushing out data downstream.
2176 * Note that this is provided for convenience, and the subclass is
2177 * not required to use this and can still do tag handling on its own.
2182 gst_aggregator_merge_tags (GstAggregator * self,
2183 const GstTagList * tags, GstTagMergeMode mode)
2187 g_return_if_fail (GST_IS_AGGREGATOR (self));
2188 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2190 /* FIXME Check if we can use OBJECT lock here! */
2191 GST_OBJECT_LOCK (self);
2193 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2194 otags = self->priv->tags;
2195 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2197 gst_tag_list_unref (otags);
2198 self->priv->tags_changed = TRUE;
2199 GST_OBJECT_UNLOCK (self);
2203 * gst_aggregator_set_latency:
2204 * @self: a #GstAggregator
2205 * @min_latency: minimum latency
2206 * @max_latency: maximum latency
2208 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2209 * latency is. Will also post a LATENCY message on the bus so the pipeline
2210 * can reconfigure its global latency.
2213 gst_aggregator_set_latency (GstAggregator * self,
2214 GstClockTime min_latency, GstClockTime max_latency)
2216 gboolean changed = FALSE;
2218 g_return_if_fail (GST_IS_AGGREGATOR (self));
2219 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2220 g_return_if_fail (max_latency >= min_latency);
2222 GST_OBJECT_LOCK (self);
2223 if (self->priv->sub_latency_min != min_latency) {
2224 self->priv->sub_latency_min = min_latency;
2227 if (self->priv->sub_latency_max != max_latency) {
2228 self->priv->sub_latency_max = max_latency;
2231 GST_OBJECT_UNLOCK (self);
2234 gst_element_post_message (GST_ELEMENT_CAST (self),
2235 gst_message_new_latency (GST_OBJECT_CAST (self)));