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:
950 GstClockTime duration;
953 gst_event_parse_gap (event, &pts, &duration);
954 gapbuf = gst_buffer_new ();
956 GST_BUFFER_PTS (gapbuf) = pts;
957 GST_BUFFER_DURATION (gapbuf) = duration;
958 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
959 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
961 if (gst_pad_chain (pad, gapbuf) != GST_FLOW_OK) {
962 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
972 gst_event_parse_tag (event, &tags);
974 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
975 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
976 gst_event_unref (event);
988 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
989 return gst_pad_event_default (pad, GST_OBJECT (self), event);
992 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
994 gst_event_unref (event);
999 static inline gboolean
1000 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1001 gpointer unused_udata)
1003 gst_aggregator_pad_flush (pad, self);
1009 gst_aggregator_stop (GstAggregator * agg)
1011 GstAggregatorClass *klass;
1014 gst_aggregator_reset_flow_values (agg);
1016 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1018 klass = GST_AGGREGATOR_GET_CLASS (agg);
1021 result = klass->stop (agg);
1025 agg->priv->has_peer_latency = FALSE;
1026 agg->priv->peer_latency_live = FALSE;
1027 agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1029 if (agg->priv->tags)
1030 gst_tag_list_unref (agg->priv->tags);
1031 agg->priv->tags = NULL;
1036 /* GstElement vmethods implementations */
1037 static GstStateChangeReturn
1038 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1040 GstStateChangeReturn ret;
1041 GstAggregator *self = GST_AGGREGATOR (element);
1043 switch (transition) {
1044 case GST_STATE_CHANGE_READY_TO_PAUSED:
1045 if (!gst_aggregator_start (self))
1053 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1054 transition)) == GST_STATE_CHANGE_FAILURE)
1058 switch (transition) {
1059 case GST_STATE_CHANGE_PAUSED_TO_READY:
1060 if (!gst_aggregator_stop (self)) {
1061 /* What to do in this case? Error out? */
1062 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1074 GST_ERROR_OBJECT (element, "parent failed state change");
1079 GST_ERROR_OBJECT (element, "Subclass failed to start");
1080 return GST_STATE_CHANGE_FAILURE;
1085 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1087 GstAggregator *self = GST_AGGREGATOR (element);
1088 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1090 GST_INFO_OBJECT (pad, "Removing pad");
1093 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING);
1094 gst_element_remove_pad (element, pad);
1096 SRC_BROADCAST (self);
1101 gst_aggregator_request_new_pad (GstElement * element,
1102 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1104 GstAggregator *self;
1105 GstAggregatorPad *agg_pad;
1107 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1108 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1110 self = GST_AGGREGATOR (element);
1112 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1116 GST_OBJECT_LOCK (element);
1117 if (req_name == NULL || strlen (req_name) < 6
1118 || !g_str_has_prefix (req_name, "sink_")) {
1119 /* no name given when requesting the pad, use next available int */
1122 /* parse serial number from requested padname */
1123 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1124 if (serial >= priv->padcount)
1125 priv->padcount = serial;
1128 name = g_strdup_printf ("sink_%u", priv->padcount);
1129 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1130 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1133 GST_OBJECT_UNLOCK (element);
1139 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1142 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1144 /* add the pad to the element */
1145 gst_element_add_pad (element, GST_PAD (agg_pad));
1147 return GST_PAD (agg_pad);
1150 /* Must be called with SRC_LOCK held */
1153 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1155 gboolean query_ret, live;
1156 GstClockTime our_latency, min, max;
1158 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1161 GST_WARNING_OBJECT (self, "Latency query failed");
1165 gst_query_parse_latency (query, &live, &min, &max);
1167 our_latency = self->priv->latency;
1169 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1170 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1171 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1175 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1176 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1177 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1178 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1179 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1183 self->priv->peer_latency_live = live;
1184 self->priv->peer_latency_min = min;
1185 self->priv->peer_latency_max = max;
1186 self->priv->has_peer_latency = TRUE;
1190 min += self->priv->sub_latency_min;
1191 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1192 && GST_CLOCK_TIME_IS_VALID (max))
1193 max += self->priv->sub_latency_max;
1195 max = GST_CLOCK_TIME_NONE;
1197 if (live && min > max) {
1198 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1199 ("%s", "Latency too big"),
1200 ("The requested latency value is too big for the current pipeline. "
1201 "Limiting to %" G_GINT64_FORMAT, max));
1203 /* FIXME: This could in theory become negative, but in
1204 * that case all is lost anyway */
1205 self->priv->latency -= min - max;
1206 /* FIXME: shouldn't we g_object_notify() the change here? */
1209 SRC_BROADCAST (self);
1211 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1212 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1214 gst_query_set_latency (query, live, min, max);
1220 * MUST be called with the src_lock held.
1222 * See gst_aggregator_get_latency() for doc
1225 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1227 GstClockTime latency;
1229 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1231 if (!self->priv->has_peer_latency) {
1232 GstQuery *query = gst_query_new_latency ();
1235 ret = gst_aggregator_query_latency_unlocked (self, query);
1236 gst_query_unref (query);
1238 return GST_CLOCK_TIME_NONE;
1241 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1242 return GST_CLOCK_TIME_NONE;
1244 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1245 latency = self->priv->peer_latency_min;
1248 latency += self->priv->latency;
1249 latency += self->priv->sub_latency_min;
1255 * gst_aggregator_get_latency:
1256 * @self: a #GstAggregator
1258 * Retrieves the latency values reported by @self in response to the latency
1259 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1260 * will not wait for the clock.
1262 * Typically only called by subclasses.
1264 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1267 gst_aggregator_get_latency (GstAggregator * self)
1272 ret = gst_aggregator_get_latency_unlocked (self);
1279 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1281 GstAggregator *self = GST_AGGREGATOR (element);
1283 GST_STATE_LOCK (element);
1284 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1285 GST_STATE (element) < GST_STATE_PAUSED) {
1289 GstSeekType start_type, stop_type;
1292 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1293 &start, &stop_type, &stop);
1295 GST_OBJECT_LOCK (self);
1296 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1297 stop_type, stop, NULL);
1298 self->priv->seqnum = gst_event_get_seqnum (event);
1299 GST_OBJECT_UNLOCK (self);
1301 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1303 GST_STATE_UNLOCK (element);
1306 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1311 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1313 gboolean res = TRUE;
1315 switch (GST_QUERY_TYPE (query)) {
1316 case GST_QUERY_SEEKING:
1320 /* don't pass it along as some (file)sink might claim it does
1321 * whereas with a collectpads in between that will not likely work */
1322 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1323 gst_query_set_seeking (query, format, FALSE, 0, -1);
1328 case GST_QUERY_LATENCY:
1330 res = gst_aggregator_query_latency_unlocked (self, query);
1334 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1341 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1343 EventData *evdata = user_data;
1344 gboolean ret = TRUE;
1345 GstPad *peer = gst_pad_get_peer (pad);
1346 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1349 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1350 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1351 gst_object_unref (peer);
1355 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1356 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1358 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1359 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1361 if (gst_pad_query (peer, seeking)) {
1364 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1366 if (seekable == FALSE) {
1367 GST_INFO_OBJECT (pad,
1368 "Source not seekable, We failed but it does not matter!");
1373 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1376 gst_query_unref (seeking);
1379 if (evdata->flush) {
1381 aggpad->priv->pending_flush_start = FALSE;
1382 aggpad->priv->pending_flush_stop = FALSE;
1383 PAD_UNLOCK (aggpad);
1386 evdata->one_actually_seeked = TRUE;
1389 evdata->result &= ret;
1391 /* Always send to all pads */
1396 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1397 GstEvent * event, gboolean flush)
1401 evdata.event = event;
1402 evdata.result = TRUE;
1403 evdata.flush = flush;
1404 evdata.one_actually_seeked = FALSE;
1406 /* We first need to set all pads as flushing in a first pass
1407 * as flush_start flush_stop is sometimes sent synchronously
1408 * while we send the seek event */
1412 GST_OBJECT_LOCK (self);
1413 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1414 GstAggregatorPad *pad = l->data;
1417 pad->priv->pending_flush_start = TRUE;
1418 pad->priv->pending_flush_stop = FALSE;
1421 GST_OBJECT_UNLOCK (self);
1424 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1426 gst_event_unref (event);
1432 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1437 GstSeekType start_type, stop_type;
1441 GstAggregatorPrivate *priv = self->priv;
1443 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1444 &start, &stop_type, &stop);
1446 GST_INFO_OBJECT (self, "starting SEEK");
1448 flush = flags & GST_SEEK_FLAG_FLUSH;
1450 GST_OBJECT_LOCK (self);
1452 priv->pending_flush_start = TRUE;
1453 priv->flush_seeking = TRUE;
1456 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1457 stop_type, stop, NULL);
1458 GST_OBJECT_UNLOCK (self);
1460 /* forward the seek upstream */
1461 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, flush);
1464 if (!evdata.result || !evdata.one_actually_seeked) {
1465 GST_OBJECT_LOCK (self);
1466 priv->flush_seeking = FALSE;
1467 priv->pending_flush_start = FALSE;
1468 GST_OBJECT_UNLOCK (self);
1471 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1473 return evdata.result;
1477 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1480 gboolean res = TRUE;
1482 switch (GST_EVENT_TYPE (event)) {
1483 case GST_EVENT_SEEK:
1485 gst_event_ref (event);
1486 res = gst_aggregator_do_seek (self, event);
1487 gst_event_unref (event);
1491 case GST_EVENT_NAVIGATION:
1493 /* navigation is rather pointless. */
1495 gst_event_unref (event);
1504 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE);
1505 res = evdata.result;
1512 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1515 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1517 return klass->src_event (GST_AGGREGATOR (parent), event);
1521 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1524 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1526 return klass->src_query (GST_AGGREGATOR (parent), query);
1530 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1531 GstObject * parent, GstPadMode mode, gboolean active)
1533 GstAggregator *self = GST_AGGREGATOR (parent);
1534 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1536 if (klass->src_activate) {
1537 if (klass->src_activate (self, mode, active) == FALSE) {
1542 if (active == TRUE) {
1544 case GST_PAD_MODE_PUSH:
1546 GST_INFO_OBJECT (pad, "Activating pad!");
1547 gst_aggregator_start_srcpad_task (self);
1552 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1559 GST_INFO_OBJECT (self, "Deactivating srcpad");
1560 gst_aggregator_stop_srcpad_task (self, FALSE);
1566 gst_aggregator_default_sink_query (GstAggregator * self,
1567 GstAggregatorPad * aggpad, GstQuery * query)
1569 GstPad *pad = GST_PAD (aggpad);
1571 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1575 gst_aggregator_finalize (GObject * object)
1577 GstAggregator *self = (GstAggregator *) object;
1579 g_mutex_clear (&self->priv->src_lock);
1580 g_cond_clear (&self->priv->src_cond);
1582 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1586 * gst_aggregator_set_latency_property:
1587 * @agg: a #GstAggregator
1588 * @latency: the new latency value.
1590 * Sets the new latency value to @latency. This value is used to limit the
1591 * amount of time a pad waits for data to appear before considering the pad
1595 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1598 GstClockTime min, max;
1600 g_return_if_fail (GST_IS_AGGREGATOR (self));
1601 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1604 if (self->priv->peer_latency_live) {
1605 min = self->priv->peer_latency_min;
1606 max = self->priv->peer_latency_max;
1609 min += self->priv->sub_latency_min;
1610 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1611 && GST_CLOCK_TIME_IS_VALID (max))
1612 max += self->priv->sub_latency_max;
1614 max = GST_CLOCK_TIME_NONE;
1616 if (GST_CLOCK_TIME_IS_VALID (max) && min > max) {
1617 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1618 ("%s", "Latency too big"),
1619 ("The requested latency value is too big for the latency in the "
1620 "current pipeline. Limiting to %" G_GINT64_FORMAT, max));
1621 /* FIXME: This could in theory become negative, but in
1622 * that case all is lost anyway */
1623 latency -= min - max;
1624 /* FIXME: shouldn't we g_object_notify() the change here? */
1628 changed = (self->priv->latency != latency);
1629 self->priv->latency = latency;
1632 SRC_BROADCAST (self);
1636 gst_element_post_message (GST_ELEMENT_CAST (self),
1637 gst_message_new_latency (GST_OBJECT_CAST (self)));
1641 * gst_aggregator_get_latency_property:
1642 * @agg: a #GstAggregator
1644 * Gets the latency value. See gst_aggregator_set_latency for
1647 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1648 * before a pad is deemed unresponsive. A value of -1 means an
1652 gst_aggregator_get_latency_property (GstAggregator * agg)
1656 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1658 GST_OBJECT_LOCK (agg);
1659 res = agg->priv->latency;
1660 GST_OBJECT_UNLOCK (agg);
1666 gst_aggregator_set_property (GObject * object, guint prop_id,
1667 const GValue * value, GParamSpec * pspec)
1669 GstAggregator *agg = GST_AGGREGATOR (object);
1673 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1676 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1682 gst_aggregator_get_property (GObject * object, guint prop_id,
1683 GValue * value, GParamSpec * pspec)
1685 GstAggregator *agg = GST_AGGREGATOR (object);
1689 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1692 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1697 /* GObject vmethods implementations */
1699 gst_aggregator_class_init (GstAggregatorClass * klass)
1701 GObjectClass *gobject_class = (GObjectClass *) klass;
1702 GstElementClass *gstelement_class = (GstElementClass *) klass;
1704 aggregator_parent_class = g_type_class_peek_parent (klass);
1705 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1707 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1708 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1710 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1712 klass->sink_event = gst_aggregator_default_sink_event;
1713 klass->sink_query = gst_aggregator_default_sink_query;
1715 klass->src_event = gst_aggregator_default_src_event;
1716 klass->src_query = gst_aggregator_default_src_query;
1718 gstelement_class->request_new_pad =
1719 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1720 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1721 gstelement_class->release_pad =
1722 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1723 gstelement_class->change_state =
1724 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1726 gobject_class->set_property = gst_aggregator_set_property;
1727 gobject_class->get_property = gst_aggregator_get_property;
1728 gobject_class->finalize = gst_aggregator_finalize;
1730 g_object_class_install_property (gobject_class, PROP_LATENCY,
1731 g_param_spec_int64 ("latency", "Buffer latency",
1732 "Additional latency in live mode to allow upstream "
1733 "to take longer to produce buffers for the current "
1735 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1736 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1738 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1742 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1744 GstPadTemplate *pad_template;
1745 GstAggregatorPrivate *priv;
1747 g_return_if_fail (klass->aggregate != NULL);
1750 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1751 GstAggregatorPrivate);
1756 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1757 g_return_if_fail (pad_template != NULL);
1759 priv->padcount = -1;
1760 priv->tags_changed = FALSE;
1762 self->priv->peer_latency_live = FALSE;
1763 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
1764 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
1765 self->priv->has_peer_latency = FALSE;
1766 gst_aggregator_reset_flow_values (self);
1768 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1770 gst_pad_set_event_function (self->srcpad,
1771 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
1772 gst_pad_set_query_function (self->srcpad,
1773 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
1774 gst_pad_set_activatemode_function (self->srcpad,
1775 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
1777 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1779 self->priv->latency = DEFAULT_LATENCY;
1781 g_mutex_init (&self->priv->src_lock);
1782 g_cond_init (&self->priv->src_cond);
1785 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1786 * method to get to the padtemplates */
1788 gst_aggregator_get_type (void)
1790 static volatile gsize type = 0;
1792 if (g_once_init_enter (&type)) {
1794 static const GTypeInfo info = {
1795 sizeof (GstAggregatorClass),
1798 (GClassInitFunc) gst_aggregator_class_init,
1801 sizeof (GstAggregator),
1803 (GInstanceInitFunc) gst_aggregator_init,
1806 _type = g_type_register_static (GST_TYPE_ELEMENT,
1807 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1808 g_once_init_leave (&type, _type);
1813 static GstFlowReturn
1814 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1816 GstBuffer *actual_buf = buffer;
1817 GstAggregator *self = GST_AGGREGATOR (object);
1818 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1819 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1820 GstFlowReturn flow_return;
1822 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1824 PAD_FLUSH_LOCK (aggpad);
1827 flow_return = aggpad->priv->flow_return;
1828 if (flow_return != GST_FLOW_OK)
1831 if (aggpad->priv->pending_eos == TRUE)
1834 while (aggpad->priv->buffer && aggpad->priv->flow_return == GST_FLOW_OK)
1835 PAD_WAIT_EVENT (aggpad);
1837 flow_return = aggpad->priv->flow_return;
1838 if (flow_return != GST_FLOW_OK)
1841 PAD_UNLOCK (aggpad);
1843 if (aggclass->clip) {
1844 aggclass->clip (self, aggpad, buffer, &actual_buf);
1849 if (aggpad->priv->buffer)
1850 gst_buffer_unref (aggpad->priv->buffer);
1851 aggpad->priv->buffer = actual_buf;
1853 flow_return = aggpad->priv->flow_return;
1855 PAD_UNLOCK (aggpad);
1856 PAD_FLUSH_UNLOCK (aggpad);
1858 SRC_BROADCAST (self);
1861 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1866 PAD_UNLOCK (aggpad);
1867 PAD_FLUSH_UNLOCK (aggpad);
1869 gst_buffer_unref (buffer);
1870 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
1871 gst_flow_get_name (flow_return));
1876 PAD_UNLOCK (aggpad);
1877 PAD_FLUSH_UNLOCK (aggpad);
1879 gst_buffer_unref (buffer);
1880 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1882 return GST_FLOW_EOS;
1886 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
1889 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1890 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1892 if (GST_QUERY_IS_SERIALIZED (query)) {
1895 while (aggpad->priv->buffer && aggpad->priv->flow_return == GST_FLOW_OK)
1896 PAD_WAIT_EVENT (aggpad);
1898 if (aggpad->priv->flow_return != GST_FLOW_OK)
1901 PAD_UNLOCK (aggpad);
1904 return klass->sink_query (GST_AGGREGATOR (parent),
1905 GST_AGGREGATOR_PAD (pad), query);
1908 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
1909 gst_flow_get_name (aggpad->priv->flow_return));
1910 PAD_UNLOCK (aggpad);
1915 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
1918 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1919 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1921 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1922 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1926 while (aggpad->priv->buffer && aggpad->priv->flow_return == GST_FLOW_OK)
1927 PAD_WAIT_EVENT (aggpad);
1929 if (aggpad->priv->flow_return != GST_FLOW_OK
1930 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1933 PAD_UNLOCK (aggpad);
1936 return klass->sink_event (GST_AGGREGATOR (parent),
1937 GST_AGGREGATOR_PAD (pad), event);
1940 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
1941 gst_flow_get_name (aggpad->priv->flow_return));
1942 PAD_UNLOCK (aggpad);
1943 if (GST_EVENT_IS_STICKY (event))
1944 gst_pad_store_sticky_event (pad, event);
1945 gst_event_unref (event);
1950 gst_aggregator_pad_activate_mode_func (GstPad * pad,
1951 GstObject * parent, GstPadMode mode, gboolean active)
1953 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1955 if (active == FALSE) {
1956 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING);
1959 aggpad->priv->flow_return = GST_FLOW_OK;
1960 PAD_BROADCAST_EVENT (aggpad);
1961 PAD_UNLOCK (aggpad);
1967 /***********************************
1968 * GstAggregatorPad implementation *
1969 ************************************/
1970 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1973 gst_aggregator_pad_constructed (GObject * object)
1975 GstPad *pad = GST_PAD (object);
1977 gst_pad_set_chain_function (pad,
1978 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
1979 gst_pad_set_event_function (pad,
1980 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
1981 gst_pad_set_query_function (pad,
1982 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
1983 gst_pad_set_activatemode_function (pad,
1984 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
1988 gst_aggregator_pad_finalize (GObject * object)
1990 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1992 g_cond_clear (&pad->priv->event_cond);
1993 g_mutex_clear (&pad->priv->flush_lock);
1994 g_mutex_clear (&pad->priv->lock);
1996 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2000 gst_aggregator_pad_dispose (GObject * object)
2002 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2004 gst_aggregator_pad_drop_buffer (pad);
2006 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2010 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2012 GObjectClass *gobject_class = (GObjectClass *) klass;
2014 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2016 gobject_class->constructed = gst_aggregator_pad_constructed;
2017 gobject_class->finalize = gst_aggregator_pad_finalize;
2018 gobject_class->dispose = gst_aggregator_pad_dispose;
2022 gst_aggregator_pad_init (GstAggregatorPad * pad)
2025 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2026 GstAggregatorPadPrivate);
2028 pad->priv->buffer = NULL;
2029 g_cond_init (&pad->priv->event_cond);
2031 g_mutex_init (&pad->priv->flush_lock);
2032 g_mutex_init (&pad->priv->lock);
2036 * gst_aggregator_pad_steal_buffer:
2037 * @pad: the pad to get buffer from
2039 * Steal the ref to the buffer currently queued in @pad.
2041 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2042 * queued. You should unref the buffer after usage.
2045 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2047 GstBuffer *buffer = NULL;
2050 if (pad->priv->buffer) {
2051 GST_TRACE_OBJECT (pad, "Consuming buffer");
2052 buffer = pad->priv->buffer;
2053 pad->priv->buffer = NULL;
2054 if (pad->priv->pending_eos) {
2055 pad->priv->pending_eos = FALSE;
2056 pad->priv->eos = TRUE;
2058 PAD_BROADCAST_EVENT (pad);
2059 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2067 * gst_aggregator_pad_drop_buffer:
2068 * @pad: the pad where to drop any pending buffer
2070 * Drop the buffer currently queued in @pad.
2072 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2075 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2079 buf = gst_aggregator_pad_steal_buffer (pad);
2084 gst_buffer_unref (buf);
2089 * gst_aggregator_pad_get_buffer:
2090 * @pad: the pad to get buffer from
2092 * Returns: (transfer full): A reference to the buffer in @pad or
2093 * NULL if no buffer was queued. You should unref the buffer after
2097 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2099 GstBuffer *buffer = NULL;
2102 if (pad->priv->buffer)
2103 buffer = gst_buffer_ref (pad->priv->buffer);
2110 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2115 is_eos = pad->priv->eos;
2122 * gst_aggregator_merge_tags:
2123 * @self: a #GstAggregator
2124 * @tags: a #GstTagList to merge
2125 * @mode: the #GstTagMergeMode to use
2127 * Adds tags to so-called pending tags, which will be processed
2128 * before pushing out data downstream.
2130 * Note that this is provided for convenience, and the subclass is
2131 * not required to use this and can still do tag handling on its own.
2136 gst_aggregator_merge_tags (GstAggregator * self,
2137 const GstTagList * tags, GstTagMergeMode mode)
2141 g_return_if_fail (GST_IS_AGGREGATOR (self));
2142 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2144 /* FIXME Check if we can use OBJECT lock here! */
2145 GST_OBJECT_LOCK (self);
2147 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2148 otags = self->priv->tags;
2149 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2151 gst_tag_list_unref (otags);
2152 self->priv->tags_changed = TRUE;
2153 GST_OBJECT_UNLOCK (self);
2157 * gst_aggregator_set_latency:
2158 * @self: a #GstAggregator
2159 * @min_latency: minimum latency
2160 * @max_latency: maximum latency
2162 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2163 * latency is. Will also post a LATENCY message on the bus so the pipeline
2164 * can reconfigure its global latency.
2167 gst_aggregator_set_latency (GstAggregator * self,
2168 GstClockTime min_latency, GstClockTime max_latency)
2170 gboolean changed = FALSE;
2172 g_return_if_fail (GST_IS_AGGREGATOR (self));
2173 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2174 g_return_if_fail (max_latency >= min_latency);
2177 if (self->priv->sub_latency_min != min_latency) {
2178 self->priv->sub_latency_min = min_latency;
2181 if (self->priv->sub_latency_max != max_latency) {
2182 self->priv->sub_latency_max = max_latency;
2187 SRC_BROADCAST (self);
2191 gst_element_post_message (GST_ELEMENT_CAST (self),
2192 gst_message_new_latency (GST_OBJECT_CAST (self)));