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 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
92 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
93 #define GST_CAT_DEFAULT aggregator_debug
95 /* GstAggregatorPad definitions */
96 #define PAD_LOCK(pad) G_STMT_START { \
97 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
99 g_mutex_lock(&pad->priv->lock); \
100 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
104 #define PAD_UNLOCK(pad) G_STMT_START { \
105 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
107 g_mutex_unlock(&pad->priv->lock); \
108 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
113 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
114 GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p", \
116 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
117 (&((GstAggregatorPad*)pad)->priv->lock)); \
118 GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
122 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
123 GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p", \
125 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
129 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
130 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
132 g_mutex_lock(&pad->priv->flush_lock); \
133 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
137 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
138 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
140 g_mutex_unlock(&pad->priv->flush_lock); \
141 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
145 #define SRC_LOCK(self) G_STMT_START { \
146 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
148 g_mutex_lock(&self->priv->src_lock); \
149 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
153 #define SRC_UNLOCK(self) G_STMT_START { \
154 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
156 g_mutex_unlock(&self->priv->src_lock); \
157 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
161 #define SRC_WAIT(self) G_STMT_START { \
162 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
164 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
165 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
169 #define SRC_BROADCAST(self) G_STMT_START { \
170 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
172 if (self->priv->aggregate_id) \
173 gst_clock_id_unschedule (self->priv->aggregate_id); \
174 g_cond_broadcast(&(self->priv->src_cond)); \
177 struct _GstAggregatorPadPrivate
179 /* Following fields are protected by the PAD_LOCK */
180 GstFlowReturn flow_return;
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->pending_eos = FALSE;
203 aggpad->priv->eos = FALSE;
204 aggpad->priv->flow_return = GST_FLOW_OK;
208 return klass->flush (aggpad, agg);
213 /*************************************
214 * GstAggregator implementation *
215 *************************************/
216 static GstElementClass *aggregator_parent_class = NULL;
218 /* All members are protected by the object lock unless otherwise noted */
220 struct _GstAggregatorPrivate
224 /* Our state is >= PAUSED */
225 gboolean running; /* protected by src_lock */
228 gboolean send_stream_start; /* protected by srcpad stream lock */
229 gboolean send_segment;
230 gboolean flush_seeking;
231 gboolean pending_flush_start;
232 gboolean send_eos; /* protected by srcpad stream lock */
234 GstCaps *srccaps; /* protected by the srcpad stream lock */
237 gboolean tags_changed;
239 gboolean peer_latency_live; /* protected by src_lock */
240 GstClockTime peer_latency_min; /* protected by src_lock */
241 GstClockTime peer_latency_max; /* protected by src_lock */
242 gboolean has_peer_latency;
244 GstClockTime sub_latency_min; /* protected by src_lock */
245 GstClockTime sub_latency_max; /* protected by src_lock */
248 GstClockID aggregate_id; /* protected by src_lock */
262 gboolean one_actually_seeked;
265 #define DEFAULT_LATENCY 0
275 * gst_aggregator_iterate_sinkpads:
276 * @self: The #GstAggregator
277 * @func: (scope call): The function to call.
278 * @user_data: (closure): The data to pass to @func.
280 * Iterate the sinkpads of aggregator to call a function on them.
282 * This method guarantees that @func will be called only once for each
286 gst_aggregator_iterate_sinkpads (GstAggregator * self,
287 GstAggregatorPadForeachFunc func, gpointer user_data)
289 gboolean result = FALSE;
291 gboolean done = FALSE;
292 GValue item = { 0, };
293 GList *seen_pads = NULL;
295 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
301 switch (gst_iterator_next (iter, &item)) {
302 case GST_ITERATOR_OK:
304 GstAggregatorPad *pad;
306 pad = g_value_get_object (&item);
308 /* if already pushed, skip. FIXME, find something faster to tag pads */
309 if (pad == NULL || g_list_find (seen_pads, pad)) {
310 g_value_reset (&item);
314 GST_LOG_OBJECT (pad, "calling function %s on pad",
315 GST_DEBUG_FUNCPTR_NAME (func));
317 result = func (self, pad, user_data);
321 seen_pads = g_list_prepend (seen_pads, pad);
323 g_value_reset (&item);
326 case GST_ITERATOR_RESYNC:
327 gst_iterator_resync (iter);
329 case GST_ITERATOR_ERROR:
330 GST_ERROR_OBJECT (self,
331 "Could not iterate over internally linked pads");
334 case GST_ITERATOR_DONE:
339 g_value_unset (&item);
340 gst_iterator_free (iter);
342 if (seen_pads == NULL) {
343 GST_DEBUG_OBJECT (self, "No pad seen");
347 g_list_free (seen_pads);
354 gst_aggregator_check_pads_ready (GstAggregator * self)
356 GstAggregatorPad *pad;
359 GST_LOG_OBJECT (self, "checking pads");
361 GST_OBJECT_LOCK (self);
363 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
364 if (sinkpads == NULL)
367 for (l = sinkpads; l != NULL; l = l->next) {
371 if (pad->priv->buffer == NULL && !pad->priv->eos) {
379 GST_OBJECT_UNLOCK (self);
380 GST_LOG_OBJECT (self, "pads are ready");
385 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
386 GST_OBJECT_UNLOCK (self);
391 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
392 GST_OBJECT_UNLOCK (self);
398 gst_aggregator_reset_flow_values (GstAggregator * self)
400 GST_OBJECT_LOCK (self);
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;
546 latency = gst_aggregator_get_latency_unlocked (self);
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 (!GST_CLOCK_TIME_IS_VALID (latency) ||
565 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
566 !GST_CLOCK_TIME_IS_VALID (start)) {
567 /* We wake up here when something happened, and below
568 * then check if we're ready now. If we return FALSE,
569 * we will be directly called again.
573 GstClockTime base_time, time;
575 GstClockReturn status;
576 GstClockTimeDiff jitter;
578 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
579 GST_TIME_ARGS (start));
581 GST_OBJECT_LOCK (self);
582 base_time = GST_ELEMENT_CAST (self)->base_time;
583 clock = GST_ELEMENT_CLOCK (self);
585 gst_object_ref (clock);
586 GST_OBJECT_UNLOCK (self);
588 time = base_time + start;
591 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
592 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
593 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
594 GST_TIME_ARGS (time),
595 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
596 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
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_pad_set_flushing (GstAggregatorPad * aggpad,
634 GstFlowReturn flow_return)
637 if (flow_return == GST_FLOW_NOT_LINKED)
638 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
640 aggpad->priv->flow_return = flow_return;
641 gst_buffer_replace (&aggpad->priv->buffer, NULL);
642 PAD_BROADCAST_EVENT (aggpad);
647 gst_aggregator_aggregate_func (GstAggregator * self)
649 GstAggregatorPrivate *priv = self->priv;
650 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
651 gboolean timeout = FALSE;
653 if (self->priv->running == FALSE) {
654 GST_DEBUG_OBJECT (self, "Not running anymore");
658 GST_LOG_OBJECT (self, "Checking aggregate");
659 while (priv->send_eos && priv->running) {
660 GstFlowReturn flow_return;
662 if (!gst_aggregator_wait_and_check (self, &timeout))
665 GST_TRACE_OBJECT (self, "Actually aggregating!");
667 flow_return = klass->aggregate (self, timeout);
669 GST_OBJECT_LOCK (self);
670 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
671 /* We don't want to set the pads to flushing, but we want to
672 * stop the thread, so just break here */
673 GST_OBJECT_UNLOCK (self);
676 GST_OBJECT_UNLOCK (self);
678 if (flow_return == GST_FLOW_EOS) {
679 gst_aggregator_push_eos (self);
682 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
684 if (flow_return != GST_FLOW_OK) {
687 GST_OBJECT_LOCK (self);
688 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
689 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
691 gst_aggregator_pad_set_flushing (aggpad, flow_return);
693 GST_OBJECT_UNLOCK (self);
698 /* Pause the task here, the only ways to get here are:
699 * 1) We're stopping, in which case the task is stopped anyway
700 * 2) We got a flow error above, in which case it might take
701 * some time to forward the flow return upstream and we
702 * would otherwise call the task function over and over
703 * again without doing anything
705 gst_pad_pause_task (self->srcpad);
709 gst_aggregator_start (GstAggregator * self)
711 GstAggregatorClass *klass;
714 self->priv->running = TRUE;
715 self->priv->send_stream_start = TRUE;
716 self->priv->send_segment = TRUE;
717 self->priv->send_eos = TRUE;
718 self->priv->srccaps = NULL;
720 klass = GST_AGGREGATOR_GET_CLASS (self);
723 result = klass->start (self);
731 _check_pending_flush_stop (GstAggregatorPad * pad)
736 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
743 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
747 GST_INFO_OBJECT (self, "%s srcpad task",
748 flush_start ? "Pausing" : "Stopping");
751 self->priv->running = FALSE;
752 SRC_BROADCAST (self);
756 res = gst_pad_push_event (self->srcpad, flush_start);
759 gst_pad_stop_task (self->srcpad);
765 gst_aggregator_start_srcpad_task (GstAggregator * self)
767 GST_INFO_OBJECT (self, "Starting srcpad task");
769 self->priv->running = TRUE;
770 gst_pad_start_task (GST_PAD (self->srcpad),
771 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
775 gst_aggregator_flush (GstAggregator * self)
777 GstFlowReturn ret = GST_FLOW_OK;
778 GstAggregatorPrivate *priv = self->priv;
779 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
781 GST_DEBUG_OBJECT (self, "Flushing everything");
782 GST_OBJECT_LOCK (self);
783 priv->send_segment = TRUE;
784 priv->flush_seeking = FALSE;
785 priv->tags_changed = FALSE;
786 GST_OBJECT_UNLOCK (self);
788 ret = klass->flush (self);
794 /* Called with GstAggregator's object lock held */
797 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
800 GstAggregatorPad *tmppad;
802 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
803 tmppad = (GstAggregatorPad *) tmp->data;
805 if (_check_pending_flush_stop (tmppad) == FALSE) {
806 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
807 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
816 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
819 GstAggregatorPrivate *priv = self->priv;
820 GstAggregatorPadPrivate *padpriv = aggpad->priv;
822 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING);
824 PAD_FLUSH_LOCK (aggpad);
826 if (padpriv->pending_flush_start) {
827 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
829 padpriv->pending_flush_start = FALSE;
830 padpriv->pending_flush_stop = TRUE;
834 GST_OBJECT_LOCK (self);
835 if (priv->flush_seeking) {
836 /* If flush_seeking we forward the first FLUSH_START */
837 if (priv->pending_flush_start) {
838 priv->pending_flush_start = FALSE;
839 GST_OBJECT_UNLOCK (self);
841 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
842 gst_aggregator_stop_srcpad_task (self, event);
844 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
845 GST_PAD_STREAM_LOCK (self->srcpad);
846 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
849 GST_OBJECT_UNLOCK (self);
850 gst_event_unref (event);
853 GST_OBJECT_UNLOCK (self);
854 gst_event_unref (event);
856 PAD_FLUSH_UNLOCK (aggpad);
858 gst_aggregator_pad_drop_buffer (aggpad);
861 /* GstAggregator vmethods default implementations */
863 gst_aggregator_default_sink_event (GstAggregator * self,
864 GstAggregatorPad * aggpad, GstEvent * event)
867 GstPad *pad = GST_PAD (aggpad);
868 GstAggregatorPrivate *priv = self->priv;
870 switch (GST_EVENT_TYPE (event)) {
871 case GST_EVENT_FLUSH_START:
873 gst_aggregator_flush_start (self, aggpad, event);
874 /* We forward only in one case: right after flush_seeking */
878 case GST_EVENT_FLUSH_STOP:
880 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
882 gst_aggregator_pad_flush (aggpad, self);
883 GST_OBJECT_LOCK (self);
884 if (priv->flush_seeking) {
885 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
886 if (gst_aggregator_all_flush_stop_received_locked (self)) {
887 GST_OBJECT_UNLOCK (self);
888 /* That means we received FLUSH_STOP/FLUSH_STOP on
889 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
890 gst_aggregator_flush (self);
891 gst_pad_push_event (self->srcpad, event);
894 priv->send_eos = TRUE;
895 SRC_BROADCAST (self);
898 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
899 GST_PAD_STREAM_UNLOCK (self->srcpad);
900 gst_aggregator_start_srcpad_task (self);
902 GST_OBJECT_UNLOCK (self);
905 GST_OBJECT_UNLOCK (self);
908 /* We never forward the event */
913 GST_DEBUG_OBJECT (aggpad, "EOS");
915 /* We still have a buffer, and we don't want the subclass to have to
916 * check for it. Mark pending_eos, eos will be set when steal_buffer is
921 if (!aggpad->priv->buffer) {
922 aggpad->priv->eos = TRUE;
924 aggpad->priv->pending_eos = TRUE;
928 SRC_BROADCAST (self);
932 case GST_EVENT_SEGMENT:
934 GST_OBJECT_LOCK (aggpad);
935 gst_event_copy_segment (event, &aggpad->segment);
936 GST_OBJECT_UNLOCK (aggpad);
938 GST_OBJECT_LOCK (self);
939 self->priv->seqnum = gst_event_get_seqnum (event);
940 GST_OBJECT_UNLOCK (self);
943 case GST_EVENT_STREAM_START:
949 /* FIXME: need API to handle GAP events properly */
950 GST_FIXME_OBJECT (self, "implement support for GAP events");
951 /* don't forward GAP events downstream */
958 gst_event_parse_tag (event, &tags);
960 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
961 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
962 gst_event_unref (event);
974 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
975 return gst_pad_event_default (pad, GST_OBJECT (self), event);
978 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
980 gst_event_unref (event);
985 static inline gboolean
986 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
987 gpointer unused_udata)
989 gst_aggregator_pad_flush (pad, self);
995 gst_aggregator_stop (GstAggregator * agg)
997 GstAggregatorClass *klass;
1000 gst_aggregator_reset_flow_values (agg);
1002 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1004 klass = GST_AGGREGATOR_GET_CLASS (agg);
1007 result = klass->stop (agg);
1011 agg->priv->has_peer_latency = FALSE;
1012 agg->priv->peer_latency_live = FALSE;
1013 agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1015 if (agg->priv->tags)
1016 gst_tag_list_unref (agg->priv->tags);
1017 agg->priv->tags = NULL;
1022 /* GstElement vmethods implementations */
1023 static GstStateChangeReturn
1024 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1026 GstStateChangeReturn ret;
1027 GstAggregator *self = GST_AGGREGATOR (element);
1029 switch (transition) {
1030 case GST_STATE_CHANGE_READY_TO_PAUSED:
1031 if (!gst_aggregator_start (self))
1039 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1040 transition)) == GST_STATE_CHANGE_FAILURE)
1044 switch (transition) {
1045 case GST_STATE_CHANGE_PAUSED_TO_READY:
1046 if (!gst_aggregator_stop (self)) {
1047 /* What to do in this case? Error out? */
1048 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1060 GST_ERROR_OBJECT (element, "parent failed state change");
1065 GST_ERROR_OBJECT (element, "Subclass failed to start");
1066 return GST_STATE_CHANGE_FAILURE;
1071 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1073 GstAggregator *self = GST_AGGREGATOR (element);
1074 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1076 GST_INFO_OBJECT (pad, "Removing pad");
1079 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING);
1080 gst_element_remove_pad (element, pad);
1082 SRC_BROADCAST (self);
1087 gst_aggregator_request_new_pad (GstElement * element,
1088 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1090 GstAggregator *self;
1091 GstAggregatorPad *agg_pad;
1093 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1094 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1096 self = GST_AGGREGATOR (element);
1098 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1102 GST_OBJECT_LOCK (element);
1103 if (req_name == NULL || strlen (req_name) < 6
1104 || !g_str_has_prefix (req_name, "sink_")) {
1105 /* no name given when requesting the pad, use next available int */
1108 /* parse serial number from requested padname */
1109 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1110 if (serial >= priv->padcount)
1111 priv->padcount = serial;
1114 name = g_strdup_printf ("sink_%u", priv->padcount);
1115 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1116 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1119 GST_OBJECT_UNLOCK (element);
1125 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1128 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1130 /* add the pad to the element */
1131 gst_element_add_pad (element, GST_PAD (agg_pad));
1133 return GST_PAD (agg_pad);
1136 /* Must be called with SRC_LOCK held */
1139 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1141 gboolean query_ret, live;
1142 GstClockTime our_latency, min, max;
1144 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1147 GST_WARNING_OBJECT (self, "Latency query failed");
1151 gst_query_parse_latency (query, &live, &min, &max);
1153 our_latency = self->priv->latency;
1155 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1156 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1157 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1161 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1162 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1163 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1164 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1165 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1169 self->priv->peer_latency_live = live;
1170 self->priv->peer_latency_min = min;
1171 self->priv->peer_latency_max = max;
1172 self->priv->has_peer_latency = TRUE;
1176 min += self->priv->sub_latency_min;
1177 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1178 && GST_CLOCK_TIME_IS_VALID (max))
1179 max += self->priv->sub_latency_max;
1181 max = GST_CLOCK_TIME_NONE;
1183 if (live && min > max) {
1184 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1185 ("%s", "Latency too big"),
1186 ("The requested latency value is too big for the current pipeline. "
1187 "Limiting to %" G_GINT64_FORMAT, max));
1189 /* FIXME: This could in theory become negative, but in
1190 * that case all is lost anyway */
1191 self->priv->latency -= min - max;
1192 /* FIXME: shouldn't we g_object_notify() the change here? */
1195 SRC_BROADCAST (self);
1197 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1198 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1200 gst_query_set_latency (query, live, min, max);
1206 * MUST be called with the src_lock held.
1208 * See gst_aggregator_get_latency() for doc
1211 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1213 GstClockTime latency;
1215 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1217 if (!self->priv->has_peer_latency) {
1218 GstQuery *query = gst_query_new_latency ();
1221 ret = gst_aggregator_query_latency_unlocked (self, query);
1222 gst_query_unref (query);
1224 return GST_CLOCK_TIME_NONE;
1227 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1228 return GST_CLOCK_TIME_NONE;
1230 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1231 latency = self->priv->peer_latency_min;
1234 latency += self->priv->latency;
1235 latency += self->priv->sub_latency_min;
1241 * gst_aggregator_get_latency:
1242 * @self: a #GstAggregator
1244 * Retrieves the latency values reported by @self in response to the latency
1245 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1246 * will not wait for the clock.
1248 * Typically only called by subclasses.
1250 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1253 gst_aggregator_get_latency (GstAggregator * self)
1258 ret = gst_aggregator_get_latency_unlocked (self);
1265 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1267 GstAggregator *self = GST_AGGREGATOR (element);
1269 GST_STATE_LOCK (element);
1270 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1271 GST_STATE (element) < GST_STATE_PAUSED) {
1275 GstSeekType start_type, stop_type;
1278 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1279 &start, &stop_type, &stop);
1281 GST_OBJECT_LOCK (self);
1282 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1283 stop_type, stop, NULL);
1284 self->priv->seqnum = gst_event_get_seqnum (event);
1285 GST_OBJECT_UNLOCK (self);
1287 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1289 GST_STATE_UNLOCK (element);
1292 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1297 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1299 gboolean res = TRUE;
1301 switch (GST_QUERY_TYPE (query)) {
1302 case GST_QUERY_SEEKING:
1306 /* don't pass it along as some (file)sink might claim it does
1307 * whereas with a collectpads in between that will not likely work */
1308 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1309 gst_query_set_seeking (query, format, FALSE, 0, -1);
1314 case GST_QUERY_LATENCY:
1316 res = gst_aggregator_query_latency_unlocked (self, query);
1320 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1327 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1329 EventData *evdata = user_data;
1330 gboolean ret = TRUE;
1331 GstPad *peer = gst_pad_get_peer (pad);
1332 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1335 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1336 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1337 gst_object_unref (peer);
1341 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1342 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1344 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1345 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1347 if (gst_pad_query (peer, seeking)) {
1350 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1352 if (seekable == FALSE) {
1353 GST_INFO_OBJECT (pad,
1354 "Source not seekable, We failed but it does not matter!");
1359 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1362 gst_query_unref (seeking);
1365 if (evdata->flush) {
1367 aggpad->priv->pending_flush_start = FALSE;
1368 aggpad->priv->pending_flush_stop = FALSE;
1369 PAD_UNLOCK (aggpad);
1372 evdata->one_actually_seeked = TRUE;
1375 evdata->result &= ret;
1377 /* Always send to all pads */
1382 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1383 GstEvent * event, gboolean flush)
1387 evdata.event = event;
1388 evdata.result = TRUE;
1389 evdata.flush = flush;
1390 evdata.one_actually_seeked = FALSE;
1392 /* We first need to set all pads as flushing in a first pass
1393 * as flush_start flush_stop is sometimes sent synchronously
1394 * while we send the seek event */
1398 GST_OBJECT_LOCK (self);
1399 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1400 GstAggregatorPad *pad = l->data;
1403 pad->priv->pending_flush_start = TRUE;
1404 pad->priv->pending_flush_stop = FALSE;
1407 GST_OBJECT_UNLOCK (self);
1410 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1412 gst_event_unref (event);
1418 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1423 GstSeekType start_type, stop_type;
1427 GstAggregatorPrivate *priv = self->priv;
1429 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1430 &start, &stop_type, &stop);
1432 GST_INFO_OBJECT (self, "starting SEEK");
1434 flush = flags & GST_SEEK_FLAG_FLUSH;
1436 GST_OBJECT_LOCK (self);
1438 priv->pending_flush_start = TRUE;
1439 priv->flush_seeking = TRUE;
1442 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1443 stop_type, stop, NULL);
1444 GST_OBJECT_UNLOCK (self);
1446 /* forward the seek upstream */
1447 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, flush);
1450 if (!evdata.result || !evdata.one_actually_seeked) {
1451 GST_OBJECT_LOCK (self);
1452 priv->flush_seeking = FALSE;
1453 priv->pending_flush_start = FALSE;
1454 GST_OBJECT_UNLOCK (self);
1457 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1459 return evdata.result;
1463 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1466 gboolean res = TRUE;
1468 switch (GST_EVENT_TYPE (event)) {
1469 case GST_EVENT_SEEK:
1471 gst_event_ref (event);
1472 res = gst_aggregator_do_seek (self, event);
1473 gst_event_unref (event);
1477 case GST_EVENT_NAVIGATION:
1479 /* navigation is rather pointless. */
1481 gst_event_unref (event);
1490 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE);
1491 res = evdata.result;
1498 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1501 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1503 return klass->src_event (GST_AGGREGATOR (parent), event);
1507 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1510 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1512 return klass->src_query (GST_AGGREGATOR (parent), query);
1516 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1517 GstObject * parent, GstPadMode mode, gboolean active)
1519 GstAggregator *self = GST_AGGREGATOR (parent);
1520 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1522 if (klass->src_activate) {
1523 if (klass->src_activate (self, mode, active) == FALSE) {
1528 if (active == TRUE) {
1530 case GST_PAD_MODE_PUSH:
1532 GST_INFO_OBJECT (pad, "Activating pad!");
1533 gst_aggregator_start_srcpad_task (self);
1538 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1545 GST_INFO_OBJECT (self, "Deactivating srcpad");
1546 gst_aggregator_stop_srcpad_task (self, FALSE);
1552 gst_aggregator_default_sink_query (GstAggregator * self,
1553 GstAggregatorPad * aggpad, GstQuery * query)
1555 GstPad *pad = GST_PAD (aggpad);
1557 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1561 gst_aggregator_finalize (GObject * object)
1563 GstAggregator *self = (GstAggregator *) object;
1565 g_mutex_clear (&self->priv->src_lock);
1566 g_cond_clear (&self->priv->src_cond);
1568 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1572 * gst_aggregator_set_latency_property:
1573 * @agg: a #GstAggregator
1574 * @latency: the new latency value.
1576 * Sets the new latency value to @latency. This value is used to limit the
1577 * amount of time a pad waits for data to appear before considering the pad
1581 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1584 GstClockTime min, max;
1586 g_return_if_fail (GST_IS_AGGREGATOR (self));
1587 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1590 if (self->priv->peer_latency_live) {
1591 min = self->priv->peer_latency_min;
1592 max = self->priv->peer_latency_max;
1595 min += self->priv->sub_latency_min;
1596 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1597 && GST_CLOCK_TIME_IS_VALID (max))
1598 max += self->priv->sub_latency_max;
1600 max = GST_CLOCK_TIME_NONE;
1602 if (GST_CLOCK_TIME_IS_VALID (max) && min > max) {
1603 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1604 ("%s", "Latency too big"),
1605 ("The requested latency value is too big for the latency in the "
1606 "current pipeline. Limiting to %" G_GINT64_FORMAT, max));
1607 /* FIXME: This could in theory become negative, but in
1608 * that case all is lost anyway */
1609 latency -= min - max;
1610 /* FIXME: shouldn't we g_object_notify() the change here? */
1614 changed = (self->priv->latency != latency);
1615 self->priv->latency = latency;
1618 SRC_BROADCAST (self);
1622 gst_element_post_message (GST_ELEMENT_CAST (self),
1623 gst_message_new_latency (GST_OBJECT_CAST (self)));
1627 * gst_aggregator_get_latency_property:
1628 * @agg: a #GstAggregator
1630 * Gets the latency value. See gst_aggregator_set_latency for
1633 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1634 * before a pad is deemed unresponsive. A value of -1 means an
1638 gst_aggregator_get_latency_property (GstAggregator * agg)
1642 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1644 GST_OBJECT_LOCK (agg);
1645 res = agg->priv->latency;
1646 GST_OBJECT_UNLOCK (agg);
1652 gst_aggregator_set_property (GObject * object, guint prop_id,
1653 const GValue * value, GParamSpec * pspec)
1655 GstAggregator *agg = GST_AGGREGATOR (object);
1659 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1662 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1668 gst_aggregator_get_property (GObject * object, guint prop_id,
1669 GValue * value, GParamSpec * pspec)
1671 GstAggregator *agg = GST_AGGREGATOR (object);
1675 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1678 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1683 /* GObject vmethods implementations */
1685 gst_aggregator_class_init (GstAggregatorClass * klass)
1687 GObjectClass *gobject_class = (GObjectClass *) klass;
1688 GstElementClass *gstelement_class = (GstElementClass *) klass;
1690 aggregator_parent_class = g_type_class_peek_parent (klass);
1691 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1693 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1694 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1696 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1698 klass->sink_event = gst_aggregator_default_sink_event;
1699 klass->sink_query = gst_aggregator_default_sink_query;
1701 klass->src_event = gst_aggregator_default_src_event;
1702 klass->src_query = gst_aggregator_default_src_query;
1704 gstelement_class->request_new_pad =
1705 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1706 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1707 gstelement_class->release_pad =
1708 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1709 gstelement_class->change_state =
1710 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1712 gobject_class->set_property = gst_aggregator_set_property;
1713 gobject_class->get_property = gst_aggregator_get_property;
1714 gobject_class->finalize = gst_aggregator_finalize;
1716 g_object_class_install_property (gobject_class, PROP_LATENCY,
1717 g_param_spec_int64 ("latency", "Buffer latency",
1718 "Additional latency in live mode to allow upstream "
1719 "to take longer to produce buffers for the current "
1721 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1722 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1724 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1728 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1730 GstPadTemplate *pad_template;
1731 GstAggregatorPrivate *priv;
1733 g_return_if_fail (klass->aggregate != NULL);
1736 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1737 GstAggregatorPrivate);
1742 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1743 g_return_if_fail (pad_template != NULL);
1745 priv->padcount = -1;
1746 priv->tags_changed = FALSE;
1748 self->priv->peer_latency_live = FALSE;
1749 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
1750 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
1751 self->priv->has_peer_latency = FALSE;
1752 gst_aggregator_reset_flow_values (self);
1754 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1756 gst_pad_set_event_function (self->srcpad,
1757 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
1758 gst_pad_set_query_function (self->srcpad,
1759 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
1760 gst_pad_set_activatemode_function (self->srcpad,
1761 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
1763 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1765 self->priv->latency = DEFAULT_LATENCY;
1767 g_mutex_init (&self->priv->src_lock);
1768 g_cond_init (&self->priv->src_cond);
1771 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1772 * method to get to the padtemplates */
1774 gst_aggregator_get_type (void)
1776 static volatile gsize type = 0;
1778 if (g_once_init_enter (&type)) {
1780 static const GTypeInfo info = {
1781 sizeof (GstAggregatorClass),
1784 (GClassInitFunc) gst_aggregator_class_init,
1787 sizeof (GstAggregator),
1789 (GInstanceInitFunc) gst_aggregator_init,
1792 _type = g_type_register_static (GST_TYPE_ELEMENT,
1793 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1794 g_once_init_leave (&type, _type);
1799 static GstFlowReturn
1800 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1802 GstBuffer *actual_buf = buffer;
1803 GstAggregator *self = GST_AGGREGATOR (object);
1804 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1805 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1806 GstFlowReturn flow_return;
1808 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1810 PAD_FLUSH_LOCK (aggpad);
1813 flow_return = aggpad->priv->flow_return;
1814 if (flow_return != GST_FLOW_OK)
1817 if (aggpad->priv->pending_eos == TRUE)
1820 while (aggpad->priv->buffer && aggpad->priv->flow_return == GST_FLOW_OK)
1821 PAD_WAIT_EVENT (aggpad);
1823 flow_return = aggpad->priv->flow_return;
1824 if (flow_return != GST_FLOW_OK)
1827 PAD_UNLOCK (aggpad);
1829 if (aggclass->clip) {
1830 aggclass->clip (self, aggpad, buffer, &actual_buf);
1835 if (aggpad->priv->buffer)
1836 gst_buffer_unref (aggpad->priv->buffer);
1837 aggpad->priv->buffer = actual_buf;
1839 flow_return = aggpad->priv->flow_return;
1841 PAD_UNLOCK (aggpad);
1842 PAD_FLUSH_UNLOCK (aggpad);
1844 SRC_BROADCAST (self);
1847 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1852 PAD_UNLOCK (aggpad);
1853 PAD_FLUSH_UNLOCK (aggpad);
1855 gst_buffer_unref (buffer);
1856 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
1857 gst_flow_get_name (flow_return));
1862 PAD_UNLOCK (aggpad);
1863 PAD_FLUSH_UNLOCK (aggpad);
1865 gst_buffer_unref (buffer);
1866 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1868 return GST_FLOW_EOS;
1872 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
1875 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1876 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1878 if (GST_QUERY_IS_SERIALIZED (query)) {
1881 while (aggpad->priv->buffer && aggpad->priv->flow_return == GST_FLOW_OK)
1882 PAD_WAIT_EVENT (aggpad);
1884 if (aggpad->priv->flow_return != GST_FLOW_OK)
1887 PAD_UNLOCK (aggpad);
1890 return klass->sink_query (GST_AGGREGATOR (parent),
1891 GST_AGGREGATOR_PAD (pad), query);
1894 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
1895 gst_flow_get_name (aggpad->priv->flow_return));
1896 PAD_UNLOCK (aggpad);
1901 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
1904 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1905 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1907 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1908 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1912 while (aggpad->priv->buffer && aggpad->priv->flow_return == GST_FLOW_OK)
1913 PAD_WAIT_EVENT (aggpad);
1915 if (aggpad->priv->flow_return != GST_FLOW_OK
1916 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1919 PAD_UNLOCK (aggpad);
1922 return klass->sink_event (GST_AGGREGATOR (parent),
1923 GST_AGGREGATOR_PAD (pad), event);
1926 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
1927 gst_flow_get_name (aggpad->priv->flow_return));
1928 PAD_UNLOCK (aggpad);
1929 if (GST_EVENT_IS_STICKY (event))
1930 gst_pad_store_sticky_event (pad, event);
1931 gst_event_unref (event);
1936 gst_aggregator_pad_activate_mode_func (GstPad * pad,
1937 GstObject * parent, GstPadMode mode, gboolean active)
1939 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1941 if (active == FALSE) {
1942 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING);
1945 aggpad->priv->flow_return = GST_FLOW_OK;
1946 PAD_BROADCAST_EVENT (aggpad);
1947 PAD_UNLOCK (aggpad);
1953 /***********************************
1954 * GstAggregatorPad implementation *
1955 ************************************/
1956 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1959 gst_aggregator_pad_constructed (GObject * object)
1961 GstPad *pad = GST_PAD (object);
1963 gst_pad_set_chain_function (pad,
1964 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
1965 gst_pad_set_event_function (pad,
1966 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
1967 gst_pad_set_query_function (pad,
1968 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
1969 gst_pad_set_activatemode_function (pad,
1970 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
1974 gst_aggregator_pad_finalize (GObject * object)
1976 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1978 g_cond_clear (&pad->priv->event_cond);
1979 g_mutex_clear (&pad->priv->flush_lock);
1980 g_mutex_clear (&pad->priv->lock);
1982 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
1986 gst_aggregator_pad_dispose (GObject * object)
1988 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1990 gst_aggregator_pad_drop_buffer (pad);
1992 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
1996 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
1998 GObjectClass *gobject_class = (GObjectClass *) klass;
2000 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2002 gobject_class->constructed = gst_aggregator_pad_constructed;
2003 gobject_class->finalize = gst_aggregator_pad_finalize;
2004 gobject_class->dispose = gst_aggregator_pad_dispose;
2008 gst_aggregator_pad_init (GstAggregatorPad * pad)
2011 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2012 GstAggregatorPadPrivate);
2014 pad->priv->buffer = NULL;
2015 g_cond_init (&pad->priv->event_cond);
2017 g_mutex_init (&pad->priv->flush_lock);
2018 g_mutex_init (&pad->priv->lock);
2022 * gst_aggregator_pad_steal_buffer:
2023 * @pad: the pad to get buffer from
2025 * Steal the ref to the buffer currently queued in @pad.
2027 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2028 * queued. You should unref the buffer after usage.
2031 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2033 GstBuffer *buffer = NULL;
2036 if (pad->priv->buffer) {
2037 GST_TRACE_OBJECT (pad, "Consuming buffer");
2038 buffer = pad->priv->buffer;
2039 pad->priv->buffer = NULL;
2040 if (pad->priv->pending_eos) {
2041 pad->priv->pending_eos = FALSE;
2042 pad->priv->eos = TRUE;
2044 PAD_BROADCAST_EVENT (pad);
2045 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2053 * gst_aggregator_pad_drop_buffer:
2054 * @pad: the pad where to drop any pending buffer
2056 * Drop the buffer currently queued in @pad.
2058 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2061 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2065 buf = gst_aggregator_pad_steal_buffer (pad);
2070 gst_buffer_unref (buf);
2075 * gst_aggregator_pad_get_buffer:
2076 * @pad: the pad to get buffer from
2078 * Returns: (transfer full): A reference to the buffer in @pad or
2079 * NULL if no buffer was queued. You should unref the buffer after
2083 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2085 GstBuffer *buffer = NULL;
2088 if (pad->priv->buffer)
2089 buffer = gst_buffer_ref (pad->priv->buffer);
2096 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2101 is_eos = pad->priv->eos;
2108 * gst_aggregator_merge_tags:
2109 * @self: a #GstAggregator
2110 * @tags: a #GstTagList to merge
2111 * @mode: the #GstTagMergeMode to use
2113 * Adds tags to so-called pending tags, which will be processed
2114 * before pushing out data downstream.
2116 * Note that this is provided for convenience, and the subclass is
2117 * not required to use this and can still do tag handling on its own.
2122 gst_aggregator_merge_tags (GstAggregator * self,
2123 const GstTagList * tags, GstTagMergeMode mode)
2127 g_return_if_fail (GST_IS_AGGREGATOR (self));
2128 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2130 /* FIXME Check if we can use OBJECT lock here! */
2131 GST_OBJECT_LOCK (self);
2133 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2134 otags = self->priv->tags;
2135 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2137 gst_tag_list_unref (otags);
2138 self->priv->tags_changed = TRUE;
2139 GST_OBJECT_UNLOCK (self);
2143 * gst_aggregator_set_latency:
2144 * @self: a #GstAggregator
2145 * @min_latency: minimum latency
2146 * @max_latency: maximum latency
2148 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2149 * latency is. Will also post a LATENCY message on the bus so the pipeline
2150 * can reconfigure its global latency.
2153 gst_aggregator_set_latency (GstAggregator * self,
2154 GstClockTime min_latency, GstClockTime max_latency)
2156 gboolean changed = FALSE;
2158 g_return_if_fail (GST_IS_AGGREGATOR (self));
2159 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2160 g_return_if_fail (max_latency >= min_latency);
2163 if (self->priv->sub_latency_min != min_latency) {
2164 self->priv->sub_latency_min = min_latency;
2167 if (self->priv->sub_latency_max != max_latency) {
2168 self->priv->sub_latency_max = max_latency;
2173 SRC_BROADCAST (self);
2177 gst_element_post_message (GST_ELEMENT_CAST (self),
2178 gst_message_new_latency (GST_OBJECT_CAST (self)));