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 EVENT on thread %p", \
116 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
117 (&((GstAggregatorPad*)pad)->priv->lock)); \
118 GST_LOG_OBJECT (pad, "DONE Waiting for EVENT on thread %p", \
122 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
123 GST_LOG_OBJECT (pad, "Signaling EVENT 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 /* To always be used atomically */
182 /* Following fields are protected by the PAD_LOCK */
183 gboolean pending_flush_start;
184 gboolean pending_flush_stop;
185 gboolean pending_eos;
192 /* This lock prevents a flush start processing happening while
193 * the chain function is also happening.
199 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
201 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
204 aggpad->priv->pending_eos = FALSE;
205 aggpad->priv->eos = FALSE;
206 aggpad->priv->flushing = FALSE;
210 return klass->flush (aggpad, agg);
215 /*************************************
216 * GstAggregator implementation *
217 *************************************/
218 static GstElementClass *aggregator_parent_class = NULL;
220 /* All members are protected by the object lock unless otherwise noted */
222 struct _GstAggregatorPrivate
226 /* Our state is >= PAUSED */
227 gboolean running; /* protected by src_lock */
230 gboolean send_stream_start; /* protected by srcpad stream lock */
231 gboolean send_segment;
232 gboolean flush_seeking;
233 gboolean pending_flush_start;
234 gboolean send_eos; /* protected by srcpad stream lock */
235 GstFlowReturn flow_return;
237 GstCaps *srccaps; /* protected by the srcpad stream lock */
240 gboolean tags_changed;
242 gboolean peer_latency_live; /* protected by src_lock */
243 GstClockTime peer_latency_min; /* protected by src_lock */
244 GstClockTime peer_latency_max; /* protected by src_lock */
245 gboolean has_peer_latency;
247 GstClockTime sub_latency_min; /* protected by src_lock */
248 GstClockTime sub_latency_max; /* protected by src_lock */
251 GstClockID aggregate_id; /* protected by src_lock */
265 gboolean one_actually_seeked;
268 #define DEFAULT_LATENCY 0
278 * gst_aggregator_iterate_sinkpads:
279 * @self: The #GstAggregator
280 * @func: (scope call): The function to call.
281 * @user_data: (closure): The data to pass to @func.
283 * Iterate the sinkpads of aggregator to call a function on them.
285 * This method guarantees that @func will be called only once for each
289 gst_aggregator_iterate_sinkpads (GstAggregator * self,
290 GstAggregatorPadForeachFunc func, gpointer user_data)
292 gboolean result = FALSE;
294 gboolean done = FALSE;
295 GValue item = { 0, };
296 GList *seen_pads = NULL;
298 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
304 switch (gst_iterator_next (iter, &item)) {
305 case GST_ITERATOR_OK:
307 GstAggregatorPad *pad;
309 pad = g_value_get_object (&item);
311 /* if already pushed, skip. FIXME, find something faster to tag pads */
312 if (pad == NULL || g_list_find (seen_pads, pad)) {
313 g_value_reset (&item);
317 GST_LOG_OBJECT (pad, "calling function %s on pad",
318 GST_DEBUG_FUNCPTR_NAME (func));
320 result = func (self, pad, user_data);
324 seen_pads = g_list_prepend (seen_pads, pad);
326 g_value_reset (&item);
329 case GST_ITERATOR_RESYNC:
330 gst_iterator_resync (iter);
332 case GST_ITERATOR_ERROR:
333 GST_ERROR_OBJECT (self,
334 "Could not iterate over internally linked pads");
337 case GST_ITERATOR_DONE:
342 g_value_unset (&item);
343 gst_iterator_free (iter);
345 if (seen_pads == NULL) {
346 GST_DEBUG_OBJECT (self, "No pad seen");
350 g_list_free (seen_pads);
357 gst_aggregator_check_pads_ready (GstAggregator * self)
359 GstAggregatorPad *pad;
362 GST_LOG_OBJECT (self, "checking pads");
364 GST_OBJECT_LOCK (self);
366 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
367 if (sinkpads == NULL)
370 for (l = sinkpads; l != NULL; l = l->next) {
374 if (pad->priv->buffer == NULL && !pad->priv->eos) {
382 GST_OBJECT_UNLOCK (self);
383 GST_LOG_OBJECT (self, "pads are ready");
388 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
389 GST_OBJECT_UNLOCK (self);
394 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
395 GST_OBJECT_UNLOCK (self);
401 gst_aggregator_reset_flow_values (GstAggregator * self)
403 GST_OBJECT_LOCK (self);
404 self->priv->flow_return = GST_FLOW_FLUSHING;
405 self->priv->send_stream_start = TRUE;
406 self->priv->send_segment = TRUE;
407 gst_segment_init (&self->segment, GST_FORMAT_TIME);
408 GST_OBJECT_UNLOCK (self);
412 gst_aggregator_push_mandatory_events (GstAggregator * self)
414 GstAggregatorPrivate *priv = self->priv;
415 GstEvent *segment = NULL;
416 GstEvent *tags = NULL;
418 if (self->priv->send_stream_start) {
421 GST_INFO_OBJECT (self, "pushing stream start");
422 /* stream-start (FIXME: create id based on input ids) */
423 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
424 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
425 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
427 self->priv->send_stream_start = FALSE;
430 if (self->priv->srccaps) {
432 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
433 self->priv->srccaps);
434 if (!gst_pad_push_event (self->srcpad,
435 gst_event_new_caps (self->priv->srccaps))) {
436 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
438 gst_caps_unref (self->priv->srccaps);
439 self->priv->srccaps = NULL;
442 GST_OBJECT_LOCK (self);
443 if (self->priv->send_segment && !self->priv->flush_seeking) {
444 segment = gst_event_new_segment (&self->segment);
446 if (!self->priv->seqnum)
447 self->priv->seqnum = gst_event_get_seqnum (segment);
449 gst_event_set_seqnum (segment, self->priv->seqnum);
450 self->priv->send_segment = FALSE;
452 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
455 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
456 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
457 priv->tags_changed = FALSE;
459 GST_OBJECT_UNLOCK (self);
462 gst_pad_push_event (self->srcpad, segment);
464 gst_pad_push_event (self->srcpad, tags);
469 * gst_aggregator_set_src_caps:
470 * @self: The #GstAggregator
471 * @caps: The #GstCaps to set on the src pad.
473 * Sets the caps to be used on the src pad.
476 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
478 GST_PAD_STREAM_LOCK (self->srcpad);
479 gst_caps_replace (&self->priv->srccaps, caps);
480 gst_aggregator_push_mandatory_events (self);
481 GST_PAD_STREAM_UNLOCK (self->srcpad);
485 * gst_aggregator_finish_buffer:
486 * @self: The #GstAggregator
487 * @buffer: (transfer full): the #GstBuffer to push.
489 * This method will push the provided output buffer downstream. If needed,
490 * mandatory events such as stream-start, caps, and segment events will be
491 * sent before pushing the buffer.
494 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
496 gst_aggregator_push_mandatory_events (self);
498 GST_OBJECT_LOCK (self);
499 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
500 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
501 GST_OBJECT_UNLOCK (self);
502 return gst_pad_push (self->srcpad, buffer);
504 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
505 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
506 GST_OBJECT_UNLOCK (self);
507 gst_buffer_unref (buffer);
513 gst_aggregator_push_eos (GstAggregator * self)
516 gst_aggregator_push_mandatory_events (self);
518 event = gst_event_new_eos ();
520 GST_OBJECT_LOCK (self);
521 self->priv->send_eos = FALSE;
522 gst_event_set_seqnum (event, self->priv->seqnum);
523 GST_OBJECT_UNLOCK (self);
525 gst_pad_push_event (self->srcpad, event);
529 gst_aggregator_get_next_time (GstAggregator * self)
531 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
533 if (klass->get_next_time)
534 return klass->get_next_time (self);
536 return GST_CLOCK_TIME_NONE;
540 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
542 GstClockTime latency;
550 latency = gst_aggregator_get_latency_unlocked (self);
552 if (gst_aggregator_check_pads_ready (self)) {
553 GST_DEBUG_OBJECT (self, "all pads have data");
559 /* Before waiting, check if we're actually still running */
560 if (!self->priv->running || !self->priv->send_eos) {
566 start = gst_aggregator_get_next_time (self);
568 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
569 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
570 !GST_CLOCK_TIME_IS_VALID (start)) {
571 /* We wake up here when something happened, and below
572 * then check if we're ready now. If we return FALSE,
573 * we will be directly called again.
577 GstClockTime base_time, time;
579 GstClockReturn status;
580 GstClockTimeDiff jitter;
582 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
583 GST_TIME_ARGS (start));
585 GST_OBJECT_LOCK (self);
586 base_time = GST_ELEMENT_CAST (self)->base_time;
587 clock = GST_ELEMENT_CLOCK (self);
589 gst_object_ref (clock);
590 GST_OBJECT_UNLOCK (self);
592 time = base_time + start;
595 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
596 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
597 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
598 GST_TIME_ARGS (time),
599 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
600 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
601 GST_TIME_ARGS (gst_clock_get_time (clock)));
604 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
605 gst_object_unref (clock);
609 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
612 if (self->priv->aggregate_id) {
613 gst_clock_id_unref (self->priv->aggregate_id);
614 self->priv->aggregate_id = NULL;
617 GST_DEBUG_OBJECT (self,
618 "clock returned %d (jitter: %s%" GST_TIME_FORMAT ")",
619 status, (jitter < 0 ? "-" : " "),
620 GST_TIME_ARGS ((jitter < 0 ? -jitter : jitter)));
623 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
630 res = gst_aggregator_check_pads_ready (self);
637 gst_aggregator_aggregate_func (GstAggregator * self)
639 GstAggregatorPrivate *priv = self->priv;
640 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
641 gboolean timeout = FALSE;
643 if (self->priv->running == FALSE) {
644 GST_DEBUG_OBJECT (self, "Not running anymore");
648 GST_LOG_OBJECT (self, "Checking aggregate");
649 while (priv->send_eos && priv->running) {
650 GstFlowReturn flow_return;
652 if (!gst_aggregator_wait_and_check (self, &timeout))
655 GST_TRACE_OBJECT (self, "Actually aggregating!");
657 flow_return = klass->aggregate (self, timeout);
659 GST_OBJECT_LOCK (self);
660 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking)
661 priv->flow_return = GST_FLOW_OK;
663 priv->flow_return = flow_return;
664 GST_OBJECT_UNLOCK (self);
666 if (flow_return == GST_FLOW_EOS) {
667 gst_aggregator_push_eos (self);
670 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
672 if (flow_return != GST_FLOW_OK)
676 /* Pause the task here, the only ways to get here are:
677 * 1) We're stopping, in which case the task is stopped anyway
678 * 2) We got a flow error above, in which case it might take
679 * some time to forward the flow return upstream and we
680 * would otherwise call the task function over and over
681 * again without doing anything
683 gst_pad_pause_task (self->srcpad);
687 gst_aggregator_start (GstAggregator * self)
689 GstAggregatorClass *klass;
692 self->priv->running = TRUE;
693 self->priv->send_stream_start = TRUE;
694 self->priv->send_segment = TRUE;
695 self->priv->send_eos = TRUE;
696 self->priv->srccaps = NULL;
697 self->priv->flow_return = GST_FLOW_OK;
699 klass = GST_AGGREGATOR_GET_CLASS (self);
702 result = klass->start (self);
710 _check_pending_flush_stop (GstAggregatorPad * pad)
715 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
722 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
726 GST_INFO_OBJECT (self, "%s srcpad task",
727 flush_start ? "Pausing" : "Stopping");
730 self->priv->running = FALSE;
731 SRC_BROADCAST (self);
735 res = gst_pad_push_event (self->srcpad, flush_start);
738 gst_pad_stop_task (self->srcpad);
744 gst_aggregator_start_srcpad_task (GstAggregator * self)
746 GST_INFO_OBJECT (self, "Starting srcpad task");
748 self->priv->running = TRUE;
749 gst_pad_start_task (GST_PAD (self->srcpad),
750 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
754 gst_aggregator_flush (GstAggregator * self)
756 GstFlowReturn ret = GST_FLOW_OK;
757 GstAggregatorPrivate *priv = self->priv;
758 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
760 GST_DEBUG_OBJECT (self, "Flushing everything");
761 GST_OBJECT_LOCK (self);
762 priv->send_segment = TRUE;
763 priv->flush_seeking = FALSE;
764 priv->tags_changed = FALSE;
765 GST_OBJECT_UNLOCK (self);
767 ret = klass->flush (self);
773 /* Called with GstAggregator's object lock held */
776 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
779 GstAggregatorPad *tmppad;
781 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
782 tmppad = (GstAggregatorPad *) tmp->data;
784 if (_check_pending_flush_stop (tmppad) == FALSE) {
785 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
786 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
795 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad)
798 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
799 gst_buffer_replace (&aggpad->priv->buffer, NULL);
800 PAD_BROADCAST_EVENT (aggpad);
806 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
809 GstAggregatorPrivate *priv = self->priv;
810 GstAggregatorPadPrivate *padpriv = aggpad->priv;
812 gst_aggregator_pad_set_flushing (aggpad);
814 PAD_FLUSH_LOCK (aggpad);
816 if (padpriv->pending_flush_start) {
817 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
819 padpriv->pending_flush_start = FALSE;
820 padpriv->pending_flush_stop = TRUE;
824 GST_OBJECT_LOCK (self);
825 if (priv->flush_seeking) {
826 /* If flush_seeking we forward the first FLUSH_START */
827 if (priv->pending_flush_start) {
828 priv->pending_flush_start = FALSE;
829 GST_OBJECT_UNLOCK (self);
831 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
832 gst_aggregator_stop_srcpad_task (self, event);
833 priv->flow_return = GST_FLOW_OK;
835 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
836 GST_PAD_STREAM_LOCK (self->srcpad);
837 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
840 GST_OBJECT_UNLOCK (self);
841 gst_event_unref (event);
844 GST_OBJECT_UNLOCK (self);
845 gst_event_unref (event);
847 PAD_FLUSH_UNLOCK (aggpad);
849 gst_aggregator_pad_drop_buffer (aggpad);
852 /* GstAggregator vmethods default implementations */
854 gst_aggregator_default_sink_event (GstAggregator * self,
855 GstAggregatorPad * aggpad, GstEvent * event)
858 GstPad *pad = GST_PAD (aggpad);
859 GstAggregatorPrivate *priv = self->priv;
861 switch (GST_EVENT_TYPE (event)) {
862 case GST_EVENT_FLUSH_START:
864 gst_aggregator_flush_start (self, aggpad, event);
865 /* We forward only in one case: right after flush_seeking */
869 case GST_EVENT_FLUSH_STOP:
871 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
873 gst_aggregator_pad_flush (aggpad, self);
874 GST_OBJECT_LOCK (self);
875 if (priv->flush_seeking) {
876 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
877 if (gst_aggregator_all_flush_stop_received_locked (self)) {
878 GST_OBJECT_UNLOCK (self);
879 /* That means we received FLUSH_STOP/FLUSH_STOP on
880 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
881 gst_aggregator_flush (self);
882 gst_pad_push_event (self->srcpad, event);
885 priv->send_eos = TRUE;
886 SRC_BROADCAST (self);
889 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
890 GST_PAD_STREAM_UNLOCK (self->srcpad);
891 gst_aggregator_start_srcpad_task (self);
893 GST_OBJECT_UNLOCK (self);
896 GST_OBJECT_UNLOCK (self);
899 /* We never forward the event */
904 GST_DEBUG_OBJECT (aggpad, "EOS");
906 /* We still have a buffer, and we don't want the subclass to have to
907 * check for it. Mark pending_eos, eos will be set when steal_buffer is
912 if (!aggpad->priv->buffer) {
913 aggpad->priv->eos = TRUE;
915 aggpad->priv->pending_eos = TRUE;
919 SRC_BROADCAST (self);
923 case GST_EVENT_SEGMENT:
925 GST_OBJECT_LOCK (aggpad);
926 gst_event_copy_segment (event, &aggpad->segment);
927 GST_OBJECT_UNLOCK (aggpad);
929 GST_OBJECT_LOCK (self);
930 self->priv->seqnum = gst_event_get_seqnum (event);
931 GST_OBJECT_UNLOCK (self);
934 case GST_EVENT_STREAM_START:
940 /* FIXME: need API to handle GAP events properly */
941 GST_FIXME_OBJECT (self, "implement support for GAP events");
942 /* don't forward GAP events downstream */
949 gst_event_parse_tag (event, &tags);
951 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
952 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
953 gst_event_unref (event);
965 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
966 return gst_pad_event_default (pad, GST_OBJECT (self), event);
969 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
971 gst_event_unref (event);
976 static inline gboolean
977 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
978 gpointer unused_udata)
980 gst_aggregator_pad_flush (pad, self);
986 gst_aggregator_stop (GstAggregator * agg)
988 GstAggregatorClass *klass;
991 gst_aggregator_reset_flow_values (agg);
993 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
995 klass = GST_AGGREGATOR_GET_CLASS (agg);
998 result = klass->stop (agg);
1002 agg->priv->has_peer_latency = FALSE;
1003 agg->priv->peer_latency_live = FALSE;
1004 agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1006 if (agg->priv->tags)
1007 gst_tag_list_unref (agg->priv->tags);
1008 agg->priv->tags = NULL;
1013 /* GstElement vmethods implementations */
1014 static GstStateChangeReturn
1015 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1017 GstStateChangeReturn ret;
1018 GstAggregator *self = GST_AGGREGATOR (element);
1020 switch (transition) {
1021 case GST_STATE_CHANGE_READY_TO_PAUSED:
1022 if (!gst_aggregator_start (self))
1030 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1031 transition)) == GST_STATE_CHANGE_FAILURE)
1035 switch (transition) {
1036 case GST_STATE_CHANGE_PAUSED_TO_READY:
1037 if (!gst_aggregator_stop (self)) {
1038 /* What to do in this case? Error out? */
1039 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1051 GST_ERROR_OBJECT (element, "parent failed state change");
1056 GST_ERROR_OBJECT (element, "Subclass failed to start");
1057 return GST_STATE_CHANGE_FAILURE;
1062 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1064 GstAggregator *self = GST_AGGREGATOR (element);
1065 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1067 GST_INFO_OBJECT (pad, "Removing pad");
1070 gst_aggregator_pad_set_flushing (aggpad);
1071 gst_element_remove_pad (element, pad);
1073 SRC_BROADCAST (self);
1078 gst_aggregator_request_new_pad (GstElement * element,
1079 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1081 GstAggregator *self;
1082 GstAggregatorPad *agg_pad;
1084 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1085 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1087 self = GST_AGGREGATOR (element);
1089 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1093 GST_OBJECT_LOCK (element);
1094 if (req_name == NULL || strlen (req_name) < 6
1095 || !g_str_has_prefix (req_name, "sink_")) {
1096 /* no name given when requesting the pad, use next available int */
1099 /* parse serial number from requested padname */
1100 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1101 if (serial >= priv->padcount)
1102 priv->padcount = serial;
1105 name = g_strdup_printf ("sink_%u", priv->padcount);
1106 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1107 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1110 GST_OBJECT_UNLOCK (element);
1116 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1119 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1121 /* add the pad to the element */
1122 gst_element_add_pad (element, GST_PAD (agg_pad));
1124 return GST_PAD (agg_pad);
1127 /* Must be called with SRC_LOCK held */
1130 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1132 gboolean query_ret, live;
1133 GstClockTime our_latency, min, max;
1135 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1138 GST_WARNING_OBJECT (self, "Latency query failed");
1142 gst_query_parse_latency (query, &live, &min, &max);
1144 our_latency = self->priv->latency;
1146 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1147 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1148 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1152 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1153 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1154 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1155 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1156 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1160 self->priv->peer_latency_live = live;
1161 self->priv->peer_latency_min = min;
1162 self->priv->peer_latency_max = max;
1163 self->priv->has_peer_latency = TRUE;
1167 min += self->priv->sub_latency_min;
1168 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1169 && GST_CLOCK_TIME_IS_VALID (max))
1170 max += self->priv->sub_latency_max;
1172 max = GST_CLOCK_TIME_NONE;
1174 if (live && min > max) {
1175 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1176 ("%s", "Latency too big"),
1177 ("The requested latency value is too big for the current pipeline. "
1178 "Limiting to %" G_GINT64_FORMAT, max));
1180 /* FIXME: This could in theory become negative, but in
1181 * that case all is lost anyway */
1182 self->priv->latency -= min - max;
1183 /* FIXME: shouldn't we g_object_notify() the change here? */
1186 SRC_BROADCAST (self);
1188 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1189 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1191 gst_query_set_latency (query, live, min, max);
1197 * MUST be called with the src_lock held.
1199 * See gst_aggregator_get_latency() for doc
1202 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1204 GstClockTime latency;
1206 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1208 if (!self->priv->has_peer_latency) {
1209 GstQuery *query = gst_query_new_latency ();
1212 ret = gst_aggregator_query_latency_unlocked (self, query);
1213 gst_query_unref (query);
1215 return GST_CLOCK_TIME_NONE;
1218 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1219 return GST_CLOCK_TIME_NONE;
1221 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1222 latency = self->priv->peer_latency_min;
1225 latency += self->priv->latency;
1226 latency += self->priv->sub_latency_min;
1232 * gst_aggregator_get_latency:
1233 * @self: a #GstAggregator
1235 * Retrieves the latency values reported by @self in response to the latency
1236 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1237 * will not wait for the clock.
1239 * Typically only called by subclasses.
1241 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1244 gst_aggregator_get_latency (GstAggregator * self)
1249 ret = gst_aggregator_get_latency_unlocked (self);
1256 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1258 GstAggregator *self = GST_AGGREGATOR (element);
1260 GST_STATE_LOCK (element);
1261 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1262 GST_STATE (element) < GST_STATE_PAUSED) {
1266 GstSeekType start_type, stop_type;
1269 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1270 &start, &stop_type, &stop);
1272 GST_OBJECT_LOCK (self);
1273 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1274 stop_type, stop, NULL);
1275 self->priv->seqnum = gst_event_get_seqnum (event);
1276 GST_OBJECT_UNLOCK (self);
1278 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1280 GST_STATE_UNLOCK (element);
1283 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1288 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1290 gboolean res = TRUE;
1292 switch (GST_QUERY_TYPE (query)) {
1293 case GST_QUERY_SEEKING:
1297 /* don't pass it along as some (file)sink might claim it does
1298 * whereas with a collectpads in between that will not likely work */
1299 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1300 gst_query_set_seeking (query, format, FALSE, 0, -1);
1305 case GST_QUERY_LATENCY:
1307 res = gst_aggregator_query_latency_unlocked (self, query);
1311 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1318 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1320 EventData *evdata = user_data;
1321 gboolean ret = TRUE;
1322 GstPad *peer = gst_pad_get_peer (pad);
1323 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1326 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1327 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1328 gst_object_unref (peer);
1332 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1333 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1335 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1336 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1338 if (gst_pad_query (peer, seeking)) {
1341 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1343 if (seekable == FALSE) {
1344 GST_INFO_OBJECT (pad,
1345 "Source not seekable, We failed but it does not matter!");
1350 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1353 gst_query_unref (seeking);
1356 if (evdata->flush) {
1358 aggpad->priv->pending_flush_start = FALSE;
1359 aggpad->priv->pending_flush_stop = FALSE;
1360 PAD_UNLOCK (aggpad);
1363 evdata->one_actually_seeked = TRUE;
1366 evdata->result &= ret;
1368 /* Always send to all pads */
1373 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1374 GstEvent * event, gboolean flush)
1378 evdata.event = event;
1379 evdata.result = TRUE;
1380 evdata.flush = flush;
1381 evdata.one_actually_seeked = FALSE;
1383 /* We first need to set all pads as flushing in a first pass
1384 * as flush_start flush_stop is sometimes sent synchronously
1385 * while we send the seek event */
1389 GST_OBJECT_LOCK (self);
1390 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1391 GstAggregatorPad *pad = l->data;
1394 pad->priv->pending_flush_start = TRUE;
1395 pad->priv->pending_flush_stop = FALSE;
1398 GST_OBJECT_UNLOCK (self);
1401 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1403 gst_event_unref (event);
1409 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1414 GstSeekType start_type, stop_type;
1418 GstAggregatorPrivate *priv = self->priv;
1420 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1421 &start, &stop_type, &stop);
1423 GST_INFO_OBJECT (self, "starting SEEK");
1425 flush = flags & GST_SEEK_FLAG_FLUSH;
1427 GST_OBJECT_LOCK (self);
1429 priv->pending_flush_start = TRUE;
1430 priv->flush_seeking = TRUE;
1433 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1434 stop_type, stop, NULL);
1435 GST_OBJECT_UNLOCK (self);
1437 /* forward the seek upstream */
1438 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, flush);
1441 if (!evdata.result || !evdata.one_actually_seeked) {
1442 GST_OBJECT_LOCK (self);
1443 priv->flush_seeking = FALSE;
1444 priv->pending_flush_start = FALSE;
1445 GST_OBJECT_UNLOCK (self);
1448 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1450 return evdata.result;
1454 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1457 gboolean res = TRUE;
1459 switch (GST_EVENT_TYPE (event)) {
1460 case GST_EVENT_SEEK:
1462 gst_event_ref (event);
1463 res = gst_aggregator_do_seek (self, event);
1464 gst_event_unref (event);
1468 case GST_EVENT_NAVIGATION:
1470 /* navigation is rather pointless. */
1472 gst_event_unref (event);
1481 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE);
1482 res = evdata.result;
1489 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1492 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1494 return klass->src_event (GST_AGGREGATOR (parent), event);
1498 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1501 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1503 return klass->src_query (GST_AGGREGATOR (parent), query);
1507 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1508 GstObject * parent, GstPadMode mode, gboolean active)
1510 GstAggregator *self = GST_AGGREGATOR (parent);
1511 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1513 if (klass->src_activate) {
1514 if (klass->src_activate (self, mode, active) == FALSE) {
1519 if (active == TRUE) {
1521 case GST_PAD_MODE_PUSH:
1523 GST_INFO_OBJECT (pad, "Activating pad!");
1524 gst_aggregator_start_srcpad_task (self);
1529 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1536 GST_INFO_OBJECT (self, "Deactivating srcpad");
1537 gst_aggregator_stop_srcpad_task (self, FALSE);
1543 gst_aggregator_default_sink_query (GstAggregator * self,
1544 GstAggregatorPad * aggpad, GstQuery * query)
1546 GstPad *pad = GST_PAD (aggpad);
1548 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1552 gst_aggregator_finalize (GObject * object)
1554 GstAggregator *self = (GstAggregator *) object;
1556 g_mutex_clear (&self->priv->src_lock);
1557 g_cond_clear (&self->priv->src_cond);
1559 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1563 * gst_aggregator_set_latency_property:
1564 * @agg: a #GstAggregator
1565 * @latency: the new latency value.
1567 * Sets the new latency value to @latency. This value is used to limit the
1568 * amount of time a pad waits for data to appear before considering the pad
1572 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1575 GstClockTime min, max;
1577 g_return_if_fail (GST_IS_AGGREGATOR (self));
1578 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1581 if (self->priv->peer_latency_live) {
1582 min = self->priv->peer_latency_min;
1583 max = self->priv->peer_latency_max;
1586 min += self->priv->sub_latency_min;
1587 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1588 && GST_CLOCK_TIME_IS_VALID (max))
1589 max += self->priv->sub_latency_max;
1591 max = GST_CLOCK_TIME_NONE;
1593 if (GST_CLOCK_TIME_IS_VALID (max) && min > max) {
1594 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1595 ("%s", "Latency too big"),
1596 ("The requested latency value is too big for the latency in the "
1597 "current pipeline. Limiting to %" G_GINT64_FORMAT, max));
1598 /* FIXME: This could in theory become negative, but in
1599 * that case all is lost anyway */
1600 latency -= min - max;
1601 /* FIXME: shouldn't we g_object_notify() the change here? */
1605 changed = (self->priv->latency != latency);
1606 self->priv->latency = latency;
1609 SRC_BROADCAST (self);
1613 gst_element_post_message (GST_ELEMENT_CAST (self),
1614 gst_message_new_latency (GST_OBJECT_CAST (self)));
1618 * gst_aggregator_get_latency_property:
1619 * @agg: a #GstAggregator
1621 * Gets the latency value. See gst_aggregator_set_latency for
1624 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1625 * before a pad is deemed unresponsive. A value of -1 means an
1629 gst_aggregator_get_latency_property (GstAggregator * agg)
1633 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1635 GST_OBJECT_LOCK (agg);
1636 res = agg->priv->latency;
1637 GST_OBJECT_UNLOCK (agg);
1643 gst_aggregator_set_property (GObject * object, guint prop_id,
1644 const GValue * value, GParamSpec * pspec)
1646 GstAggregator *agg = GST_AGGREGATOR (object);
1650 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1653 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1659 gst_aggregator_get_property (GObject * object, guint prop_id,
1660 GValue * value, GParamSpec * pspec)
1662 GstAggregator *agg = GST_AGGREGATOR (object);
1666 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1669 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1674 /* GObject vmethods implementations */
1676 gst_aggregator_class_init (GstAggregatorClass * klass)
1678 GObjectClass *gobject_class = (GObjectClass *) klass;
1679 GstElementClass *gstelement_class = (GstElementClass *) klass;
1681 aggregator_parent_class = g_type_class_peek_parent (klass);
1682 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1684 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1685 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1687 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1689 klass->sink_event = gst_aggregator_default_sink_event;
1690 klass->sink_query = gst_aggregator_default_sink_query;
1692 klass->src_event = gst_aggregator_default_src_event;
1693 klass->src_query = gst_aggregator_default_src_query;
1695 gstelement_class->request_new_pad =
1696 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1697 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1698 gstelement_class->release_pad =
1699 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1700 gstelement_class->change_state =
1701 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1703 gobject_class->set_property = gst_aggregator_set_property;
1704 gobject_class->get_property = gst_aggregator_get_property;
1705 gobject_class->finalize = gst_aggregator_finalize;
1707 g_object_class_install_property (gobject_class, PROP_LATENCY,
1708 g_param_spec_int64 ("latency", "Buffer latency",
1709 "Additional latency in live mode to allow upstream "
1710 "to take longer to produce buffers for the current "
1712 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1713 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1715 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1719 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1721 GstPadTemplate *pad_template;
1722 GstAggregatorPrivate *priv;
1724 g_return_if_fail (klass->aggregate != NULL);
1727 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1728 GstAggregatorPrivate);
1733 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1734 g_return_if_fail (pad_template != NULL);
1736 priv->padcount = -1;
1737 priv->tags_changed = FALSE;
1739 self->priv->peer_latency_live = FALSE;
1740 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
1741 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
1742 self->priv->has_peer_latency = FALSE;
1743 gst_aggregator_reset_flow_values (self);
1745 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1747 gst_pad_set_event_function (self->srcpad,
1748 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
1749 gst_pad_set_query_function (self->srcpad,
1750 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
1751 gst_pad_set_activatemode_function (self->srcpad,
1752 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
1754 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1756 self->priv->latency = DEFAULT_LATENCY;
1758 g_mutex_init (&self->priv->src_lock);
1759 g_cond_init (&self->priv->src_cond);
1762 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1763 * method to get to the padtemplates */
1765 gst_aggregator_get_type (void)
1767 static volatile gsize type = 0;
1769 if (g_once_init_enter (&type)) {
1771 static const GTypeInfo info = {
1772 sizeof (GstAggregatorClass),
1775 (GClassInitFunc) gst_aggregator_class_init,
1778 sizeof (GstAggregator),
1780 (GInstanceInitFunc) gst_aggregator_init,
1783 _type = g_type_register_static (GST_TYPE_ELEMENT,
1784 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1785 g_once_init_leave (&type, _type);
1790 static GstFlowReturn
1791 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1793 GstBuffer *actual_buf = buffer;
1794 GstAggregator *self = GST_AGGREGATOR (object);
1795 GstAggregatorPrivate *priv = self->priv;
1796 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1797 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1798 GstFlowReturn flow_return;
1800 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1802 PAD_FLUSH_LOCK (aggpad);
1804 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1808 if (aggpad->priv->pending_eos == TRUE)
1811 while (aggpad->priv->buffer
1812 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1813 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1814 PAD_WAIT_EVENT (aggpad);
1816 PAD_UNLOCK (aggpad);
1818 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1821 if (aggclass->clip) {
1822 aggclass->clip (self, aggpad, buffer, &actual_buf);
1827 if (aggpad->priv->buffer)
1828 gst_buffer_unref (aggpad->priv->buffer);
1829 aggpad->priv->buffer = actual_buf;
1830 PAD_UNLOCK (aggpad);
1831 PAD_FLUSH_UNLOCK (aggpad);
1833 SRC_BROADCAST (self);
1836 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1838 GST_OBJECT_LOCK (self);
1839 flow_return = priv->flow_return;
1840 GST_OBJECT_UNLOCK (self);
1845 PAD_FLUSH_UNLOCK (aggpad);
1847 gst_buffer_unref (buffer);
1848 GST_DEBUG_OBJECT (aggpad, "We are flushing");
1850 return GST_FLOW_FLUSHING;
1853 PAD_UNLOCK (aggpad);
1854 PAD_FLUSH_UNLOCK (aggpad);
1856 gst_buffer_unref (buffer);
1857 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1859 return GST_FLOW_EOS;
1863 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
1866 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1867 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1869 if (GST_QUERY_IS_SERIALIZED (query)) {
1872 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
1873 PAD_UNLOCK (aggpad);
1877 while (aggpad->priv->buffer
1878 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1879 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1880 PAD_WAIT_EVENT (aggpad);
1882 PAD_UNLOCK (aggpad);
1884 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1888 return klass->sink_query (GST_AGGREGATOR (parent),
1889 GST_AGGREGATOR_PAD (pad), query);
1892 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping query");
1897 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
1900 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1901 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1903 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1904 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1907 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1908 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
1909 PAD_UNLOCK (aggpad);
1913 while (aggpad->priv->buffer
1914 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1915 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1916 PAD_WAIT_EVENT (aggpad);
1918 PAD_UNLOCK (aggpad);
1920 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1921 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1925 return klass->sink_event (GST_AGGREGATOR (parent),
1926 GST_AGGREGATOR_PAD (pad), event);
1929 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping event");
1930 if (GST_EVENT_IS_STICKY (event))
1931 gst_pad_store_sticky_event (pad, event);
1932 gst_event_unref (event);
1937 gst_aggregator_pad_activate_mode_func (GstPad * pad,
1938 GstObject * parent, GstPadMode mode, gboolean active)
1940 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1942 if (active == FALSE) {
1943 gst_aggregator_pad_set_flushing (aggpad);
1946 g_atomic_int_set (&aggpad->priv->flushing, FALSE);
1947 PAD_BROADCAST_EVENT (aggpad);
1948 PAD_UNLOCK (aggpad);
1954 /***********************************
1955 * GstAggregatorPad implementation *
1956 ************************************/
1957 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1960 gst_aggregator_pad_constructed (GObject * object)
1962 GstPad *pad = GST_PAD (object);
1964 gst_pad_set_chain_function (pad,
1965 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
1966 gst_pad_set_event_function (pad,
1967 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
1968 gst_pad_set_query_function (pad,
1969 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
1970 gst_pad_set_activatemode_function (pad,
1971 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
1975 gst_aggregator_pad_finalize (GObject * object)
1977 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1979 g_cond_clear (&pad->priv->event_cond);
1980 g_mutex_clear (&pad->priv->flush_lock);
1981 g_mutex_clear (&pad->priv->lock);
1983 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
1987 gst_aggregator_pad_dispose (GObject * object)
1989 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1991 gst_aggregator_pad_drop_buffer (pad);
1993 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
1997 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
1999 GObjectClass *gobject_class = (GObjectClass *) klass;
2001 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2003 gobject_class->constructed = gst_aggregator_pad_constructed;
2004 gobject_class->finalize = gst_aggregator_pad_finalize;
2005 gobject_class->dispose = gst_aggregator_pad_dispose;
2009 gst_aggregator_pad_init (GstAggregatorPad * pad)
2012 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2013 GstAggregatorPadPrivate);
2015 pad->priv->buffer = NULL;
2016 g_cond_init (&pad->priv->event_cond);
2018 g_mutex_init (&pad->priv->flush_lock);
2019 g_mutex_init (&pad->priv->lock);
2023 * gst_aggregator_pad_steal_buffer:
2024 * @pad: the pad to get buffer from
2026 * Steal the ref to the buffer currently queued in @pad.
2028 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2029 * queued. You should unref the buffer after usage.
2032 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2034 GstBuffer *buffer = NULL;
2037 if (pad->priv->buffer) {
2038 GST_TRACE_OBJECT (pad, "Consuming buffer");
2039 buffer = pad->priv->buffer;
2040 pad->priv->buffer = NULL;
2041 if (pad->priv->pending_eos) {
2042 pad->priv->pending_eos = FALSE;
2043 pad->priv->eos = TRUE;
2045 PAD_BROADCAST_EVENT (pad);
2046 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2054 * gst_aggregator_pad_drop_buffer:
2055 * @pad: the pad where to drop any pending buffer
2057 * Drop the buffer currently queued in @pad.
2059 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2062 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2066 buf = gst_aggregator_pad_steal_buffer (pad);
2071 gst_buffer_unref (buf);
2076 * gst_aggregator_pad_get_buffer:
2077 * @pad: the pad to get buffer from
2079 * Returns: (transfer full): A reference to the buffer in @pad or
2080 * NULL if no buffer was queued. You should unref the buffer after
2084 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2086 GstBuffer *buffer = NULL;
2089 if (pad->priv->buffer)
2090 buffer = gst_buffer_ref (pad->priv->buffer);
2097 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2102 is_eos = pad->priv->eos;
2109 * gst_aggregator_merge_tags:
2110 * @self: a #GstAggregator
2111 * @tags: a #GstTagList to merge
2112 * @mode: the #GstTagMergeMode to use
2114 * Adds tags to so-called pending tags, which will be processed
2115 * before pushing out data downstream.
2117 * Note that this is provided for convenience, and the subclass is
2118 * not required to use this and can still do tag handling on its own.
2123 gst_aggregator_merge_tags (GstAggregator * self,
2124 const GstTagList * tags, GstTagMergeMode mode)
2128 g_return_if_fail (GST_IS_AGGREGATOR (self));
2129 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2131 /* FIXME Check if we can use OBJECT lock here! */
2132 GST_OBJECT_LOCK (self);
2134 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2135 otags = self->priv->tags;
2136 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2138 gst_tag_list_unref (otags);
2139 self->priv->tags_changed = TRUE;
2140 GST_OBJECT_UNLOCK (self);
2144 * gst_aggregator_set_latency:
2145 * @self: a #GstAggregator
2146 * @min_latency: minimum latency
2147 * @max_latency: maximum latency
2149 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2150 * latency is. Will also post a LATENCY message on the bus so the pipeline
2151 * can reconfigure its global latency.
2154 gst_aggregator_set_latency (GstAggregator * self,
2155 GstClockTime min_latency, GstClockTime max_latency)
2157 gboolean changed = FALSE;
2159 g_return_if_fail (GST_IS_AGGREGATOR (self));
2160 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2161 g_return_if_fail (max_latency >= min_latency);
2164 if (self->priv->sub_latency_min != min_latency) {
2165 self->priv->sub_latency_min = min_latency;
2168 if (self->priv->sub_latency_max != max_latency) {
2169 self->priv->sub_latency_max = max_latency;
2174 SRC_BROADCAST (self);
2178 gst_element_post_message (GST_ELEMENT_CAST (self),
2179 gst_message_new_latency (GST_OBJECT_CAST (self)));