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 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
78 #define GST_CAT_DEFAULT aggregator_debug
80 /* GstAggregatorPad definitions */
81 #define PAD_LOCK(pad) G_STMT_START { \
82 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
84 GST_OBJECT_LOCK (pad); \
85 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
89 #define PAD_UNLOCK(pad) G_STMT_START { \
90 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
92 GST_OBJECT_UNLOCK (pad); \
93 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
98 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
99 GST_LOG_OBJECT (pad, "Waiting for EVENT on thread %p", \
101 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
102 GST_OBJECT_GET_LOCK (pad)); \
103 GST_LOG_OBJECT (pad, "DONE Waiting for EVENT on thread %p", \
107 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
108 GST_LOG_OBJECT (pad, "Signaling EVENT from thread %p", \
110 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
114 #define PAD_STREAM_LOCK(pad) G_STMT_START { \
115 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
117 g_mutex_lock(&pad->priv->stream_lock); \
118 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
122 #define PAD_STREAM_UNLOCK(pad) G_STMT_START { \
123 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
125 g_mutex_unlock(&pad->priv->stream_lock); \
126 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
130 #define SRC_STREAM_LOCK(self) G_STMT_START { \
131 GST_TRACE_OBJECT (self, "Taking src STREAM lock from thread %p", \
133 g_mutex_lock(&self->priv->src_lock); \
134 GST_TRACE_OBJECT (self, "Took src STREAM lock from thread %p", \
138 #define SRC_STREAM_UNLOCK(self) G_STMT_START { \
139 GST_TRACE_OBJECT (self, "Releasing src STREAM lock from thread %p", \
141 g_mutex_unlock(&self->priv->src_lock); \
142 GST_TRACE_OBJECT (self, "Released src STREAM lock from thread %p", \
146 #define SRC_STREAM_WAIT(self) G_STMT_START { \
147 GST_LOG_OBJECT (self, "Waiting for src STREAM on thread %p", \
149 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
150 GST_LOG_OBJECT (self, "DONE Waiting for src STREAM on thread %p", \
154 #define SRC_STREAM_BROADCAST(self) G_STMT_START { \
155 GST_LOG_OBJECT (self, "Signaling src STREAM from thread %p", \
157 if (self->priv->aggregate_id) \
158 gst_clock_id_unschedule (self->priv->aggregate_id); \
159 g_cond_broadcast(&(self->priv->src_cond)); \
162 struct _GstAggregatorPadPrivate
164 gboolean pending_flush_start;
165 gboolean pending_flush_stop;
166 gboolean pending_eos;
175 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
177 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
181 aggpad->priv->flushing = FALSE;
185 return klass->flush (aggpad, agg);
190 /*************************************
191 * GstAggregator implementation *
192 *************************************/
193 static GstElementClass *aggregator_parent_class = NULL;
195 struct _GstAggregatorPrivate
199 /* Our state is >= PAUSED */
200 gboolean running; /* protected by SRC_STREAM_LOCK */
203 gboolean send_stream_start;
204 gboolean send_segment;
205 gboolean flush_seeking;
206 gboolean pending_flush_start;
208 GstFlowReturn flow_return;
210 gboolean send_stream_start; /* protected by srcpad stream lock */
211 gboolean send_segment; /* protected by object lock */
212 gboolean flush_seeking; /* protected by object lock */
213 gboolean pending_flush_start; /* protected by object lock */
214 gboolean send_eos; /* protected by srcpad stream lock */
215 GstFlowReturn flow_return; /* protected by object lock */
217 GstCaps *srccaps; /* protected by the srcpad stream lock */
219 /* protected by object lock */
221 gboolean tags_changed;
223 gboolean latency_live;
224 GstClockTime latency_min;
225 GstClockTime latency_max;
227 GstClockTime sub_latency_min;
228 GstClockTime sub_latency_max;
231 GstClockID aggregate_id;
245 gboolean one_actually_seeked;
248 #define DEFAULT_LATENCY 0
258 * gst_aggregator_iterate_sinkpads:
259 * @self: The #GstAggregator
260 * @func: (scope call): The function to call.
261 * @user_data: (closure): The data to pass to @func.
263 * Iterate the sinkpads of aggregator to call a function on them.
265 * This method guarantees that @func will be called only once for each
269 gst_aggregator_iterate_sinkpads (GstAggregator * self,
270 GstAggregatorPadForeachFunc func, gpointer user_data)
272 gboolean result = FALSE;
274 gboolean done = FALSE;
275 GValue item = { 0, };
276 GList *seen_pads = NULL;
278 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
284 switch (gst_iterator_next (iter, &item)) {
285 case GST_ITERATOR_OK:
287 GstAggregatorPad *pad;
289 pad = g_value_get_object (&item);
291 /* if already pushed, skip. FIXME, find something faster to tag pads */
292 if (pad == NULL || g_list_find (seen_pads, pad)) {
293 g_value_reset (&item);
297 GST_LOG_OBJECT (pad, "calling function %s on pad",
298 GST_DEBUG_FUNCPTR_NAME (func));
300 result = func (self, pad, user_data);
304 seen_pads = g_list_prepend (seen_pads, pad);
306 g_value_reset (&item);
309 case GST_ITERATOR_RESYNC:
310 gst_iterator_resync (iter);
312 case GST_ITERATOR_ERROR:
313 GST_ERROR_OBJECT (self,
314 "Could not iterate over internally linked pads");
317 case GST_ITERATOR_DONE:
322 g_value_unset (&item);
323 gst_iterator_free (iter);
325 if (seen_pads == NULL) {
326 GST_DEBUG_OBJECT (self, "No pad seen");
330 g_list_free (seen_pads);
337 gst_aggregator_check_pads_ready (GstAggregator * self)
339 GstAggregatorPad *pad;
342 GST_LOG_OBJECT (self, "checking pads");
344 GST_OBJECT_LOCK (self);
346 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
347 if (sinkpads == NULL)
350 for (l = sinkpads; l != NULL; l = l->next) {
354 if (pad->buffer == NULL && !pad->eos) {
355 GST_OBJECT_UNLOCK (pad);
362 GST_OBJECT_UNLOCK (self);
363 GST_LOG_OBJECT (self, "pads are ready");
368 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
369 GST_OBJECT_UNLOCK (self);
374 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
375 GST_OBJECT_UNLOCK (self);
381 gst_aggregator_reset_flow_values (GstAggregator * self)
383 GST_OBJECT_LOCK (self);
384 self->priv->flow_return = GST_FLOW_FLUSHING;
385 self->priv->send_stream_start = TRUE;
386 self->priv->send_segment = TRUE;
387 gst_segment_init (&self->segment, GST_FORMAT_TIME);
388 GST_OBJECT_UNLOCK (self);
392 gst_aggregator_push_mandatory_events (GstAggregator * self)
394 GstAggregatorPrivate *priv = self->priv;
395 GstEvent *segment = NULL;
396 GstEvent *tags = NULL;
398 if (self->priv->send_stream_start) {
401 GST_INFO_OBJECT (self, "pushing stream start");
402 /* stream-start (FIXME: create id based on input ids) */
403 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
404 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
405 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
407 self->priv->send_stream_start = FALSE;
410 if (self->priv->srccaps) {
412 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
413 self->priv->srccaps);
414 if (!gst_pad_push_event (self->srcpad,
415 gst_event_new_caps (self->priv->srccaps))) {
416 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
418 gst_caps_unref (self->priv->srccaps);
419 self->priv->srccaps = NULL;
422 GST_OBJECT_LOCK (self);
423 if (self->priv->send_segment && !self->priv->flush_seeking) {
424 segment = gst_event_new_segment (&self->segment);
426 if (!self->priv->seqnum)
427 self->priv->seqnum = gst_event_get_seqnum (segment);
429 gst_event_set_seqnum (segment, self->priv->seqnum);
430 self->priv->send_segment = FALSE;
432 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
435 if (priv->tags && priv->tags_changed) {
436 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
437 priv->tags_changed = FALSE;
439 GST_OBJECT_UNLOCK (self);
442 gst_pad_push_event (self->srcpad, segment);
444 gst_pad_push_event (self->srcpad, tags);
449 * gst_aggregator_set_src_caps:
450 * @self: The #GstAggregator
451 * @caps: The #GstCaps to set on the src pad.
453 * Sets the caps to be used on the src pad.
456 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
458 GST_PAD_STREAM_LOCK (self->srcpad);
459 gst_caps_replace (&self->priv->srccaps, caps);
460 gst_aggregator_push_mandatory_events (self);
461 GST_PAD_STREAM_UNLOCK (self->srcpad);
465 * gst_aggregator_finish_buffer:
466 * @self: The #GstAggregator
467 * @buffer: (transfer full): the #GstBuffer to push.
469 * This method will push the provided output buffer downstream. If needed,
470 * mandatory events such as stream-start, caps, and segment events will be
471 * sent before pushing the buffer.
474 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
476 gst_aggregator_push_mandatory_events (self);
478 GST_OBJECT_LOCK (self);
479 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
480 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
481 GST_OBJECT_UNLOCK (self);
482 return gst_pad_push (self->srcpad, buffer);
484 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
485 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
486 GST_OBJECT_UNLOCK (self);
487 gst_buffer_unref (buffer);
493 gst_aggregator_push_eos (GstAggregator * self)
496 gst_aggregator_push_mandatory_events (self);
498 self->priv->send_eos = FALSE;
499 event = gst_event_new_eos ();
500 gst_event_set_seqnum (event, self->priv->seqnum);
501 gst_pad_push_event (self->srcpad, event);
505 gst_aggregator_get_next_time (GstAggregator * self)
507 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
509 if (klass->get_next_time)
510 return klass->get_next_time (self);
512 return GST_CLOCK_TIME_NONE;
515 /* called with the src STREAM lock */
517 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
519 GstClockTime latency_max, latency_min;
525 SRC_STREAM_LOCK (self);
527 GST_OBJECT_LOCK (self);
528 gst_aggregator_get_latency_unlocked (self, &live, &latency_min, &latency_max);
529 GST_OBJECT_UNLOCK (self);
531 if (gst_aggregator_check_pads_ready (self)) {
532 GST_DEBUG_OBJECT (self, "all pads have data");
533 SRC_STREAM_UNLOCK (self);
538 /* Before waiting, check if we're actually still running */
539 if (!self->priv->running || !self->priv->send_eos) {
540 SRC_STREAM_UNLOCK (self);
545 start = gst_aggregator_get_next_time (self);
547 if (!live || !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self))
548 || !GST_CLOCK_TIME_IS_VALID (start)) {
549 /* We wake up here when something happened, and below
550 * then check if we're ready now. If we return FALSE,
551 * we will be directly called again.
553 SRC_STREAM_WAIT (self);
555 GstClockTime base_time, time;
557 GstClockReturn status;
558 GstClockTimeDiff jitter;
560 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
561 GST_TIME_ARGS (start));
563 GST_OBJECT_LOCK (self);
564 base_time = GST_ELEMENT_CAST (self)->base_time;
565 clock = GST_ELEMENT_CLOCK (self);
567 gst_object_ref (clock);
568 GST_OBJECT_UNLOCK (self);
570 time = base_time + start;
572 if (GST_CLOCK_TIME_IS_VALID (latency_min)) {
575 time += self->priv->latency;
578 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
579 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
580 " latency max %" GST_TIME_FORMAT " latency min %" GST_TIME_FORMAT
581 " current %" GST_TIME_FORMAT ")", GST_TIME_ARGS (time),
582 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
583 GST_TIME_ARGS (start), GST_TIME_ARGS (latency_max),
584 GST_TIME_ARGS (latency_min),
585 GST_TIME_ARGS (gst_clock_get_time (clock)));
587 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
588 gst_object_unref (clock);
589 SRC_STREAM_UNLOCK (self);
592 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
594 SRC_STREAM_LOCK (self);
595 if (self->priv->aggregate_id) {
596 gst_clock_id_unref (self->priv->aggregate_id);
597 self->priv->aggregate_id = NULL;
600 GST_DEBUG_OBJECT (self,
601 "clock returned %d (jitter: %s%" GST_TIME_FORMAT ")",
602 status, (jitter < 0 ? "-" : " "),
603 GST_TIME_ARGS ((jitter < 0 ? -jitter : jitter)));
606 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
607 SRC_STREAM_UNLOCK (self);
613 res = gst_aggregator_check_pads_ready (self);
614 SRC_STREAM_UNLOCK (self);
620 gst_aggregator_aggregate_func (GstAggregator * self)
622 GstAggregatorPrivate *priv = self->priv;
623 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
624 gboolean timeout = FALSE;
626 if (self->priv->running == FALSE) {
627 GST_DEBUG_OBJECT (self, "Not running anymore");
631 GST_LOG_OBJECT (self, "Checking aggregate");
632 while (priv->send_eos && priv->running) {
633 GstFlowReturn flow_return;
635 if (!gst_aggregator_wait_and_check (self, &timeout))
638 GST_TRACE_OBJECT (self, "Actually aggregating!");
640 flow_return = klass->aggregate (self, timeout);
642 GST_OBJECT_LOCK (self);
643 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking)
644 priv->flow_return = GST_FLOW_OK;
646 priv->flow_return = flow_return;
647 GST_OBJECT_UNLOCK (self);
649 if (flow_return == GST_FLOW_EOS) {
650 gst_aggregator_push_eos (self);
653 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
655 if (flow_return != GST_FLOW_OK)
661 gst_aggregator_start (GstAggregator * self)
663 GstAggregatorClass *klass;
666 self->priv->running = TRUE;
667 self->priv->send_stream_start = TRUE;
668 self->priv->send_segment = TRUE;
669 self->priv->send_eos = TRUE;
670 self->priv->srccaps = NULL;
671 self->priv->flow_return = GST_FLOW_OK;
673 klass = GST_AGGREGATOR_GET_CLASS (self);
676 result = klass->start (self);
684 _check_pending_flush_stop (GstAggregatorPad * pad)
686 return (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
690 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
694 GST_INFO_OBJECT (self, "%s srcpad task",
695 flush_start ? "Pausing" : "Stopping");
697 SRC_STREAM_LOCK (self);
698 self->priv->running = FALSE;
699 SRC_STREAM_BROADCAST (self);
700 SRC_STREAM_UNLOCK (self);
703 res = gst_pad_push_event (self->srcpad, flush_start);
706 gst_pad_stop_task (self->srcpad);
712 gst_aggregator_start_srcpad_task (GstAggregator * self)
714 GST_INFO_OBJECT (self, "Starting srcpad task");
716 self->priv->running = TRUE;
717 gst_pad_start_task (GST_PAD (self->srcpad),
718 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
722 gst_aggregator_flush (GstAggregator * self)
724 GstFlowReturn ret = GST_FLOW_OK;
725 GstAggregatorPrivate *priv = self->priv;
726 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
728 GST_DEBUG_OBJECT (self, "Flushing everything");
729 GST_OBJECT_LOCK (self);
730 priv->send_segment = TRUE;
731 priv->flush_seeking = FALSE;
732 priv->tags_changed = FALSE;
733 GST_OBJECT_UNLOCK (self);
735 ret = klass->flush (self);
741 /* Called with GstAggregator's object lock held */
744 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
747 GstAggregatorPad *tmppad;
749 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
750 tmppad = (GstAggregatorPad *) tmp->data;
752 if (_check_pending_flush_stop (tmppad) == FALSE) {
753 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
754 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
763 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
767 GstAggregatorPrivate *priv = self->priv;
768 GstAggregatorPadPrivate *padpriv = aggpad->priv;
770 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
771 /* Remove pad buffer and wake up the streaming thread */
772 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
773 gst_buffer_replace (&tmpbuf, NULL);
774 PAD_STREAM_LOCK (aggpad);
775 if (g_atomic_int_compare_and_exchange (&padpriv->pending_flush_start,
776 TRUE, FALSE) == TRUE) {
777 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
778 g_atomic_int_set (&padpriv->pending_flush_stop, TRUE);
781 GST_OBJECT_LOCK (self);
782 if (priv->flush_seeking) {
783 /* If flush_seeking we forward the first FLUSH_START */
784 if (priv->pending_flush_start) {
785 priv->pending_flush_start = FALSE;
786 GST_OBJECT_UNLOCK (self);
788 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
789 gst_aggregator_stop_srcpad_task (self, event);
790 priv->flow_return = GST_FLOW_OK;
792 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
793 GST_PAD_STREAM_LOCK (self->srcpad);
794 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
797 GST_OBJECT_UNLOCK (self);
798 gst_event_unref (event);
801 GST_OBJECT_UNLOCK (self);
802 gst_event_unref (event);
804 PAD_STREAM_UNLOCK (aggpad);
806 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
807 gst_buffer_replace (&tmpbuf, NULL);
810 /* GstAggregator vmethods default implementations */
812 gst_aggregator_default_sink_event (GstAggregator * self,
813 GstAggregatorPad * aggpad, GstEvent * event)
816 GstPad *pad = GST_PAD (aggpad);
817 GstAggregatorPrivate *priv = self->priv;
819 switch (GST_EVENT_TYPE (event)) {
820 case GST_EVENT_FLUSH_START:
822 gst_aggregator_flush_start (self, aggpad, event);
823 /* We forward only in one case: right after flush_seeking */
827 case GST_EVENT_FLUSH_STOP:
829 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
831 gst_aggregator_pad_flush (aggpad, self);
832 GST_OBJECT_LOCK (self);
833 if (priv->flush_seeking) {
834 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
835 if (gst_aggregator_all_flush_stop_received_locked (self)) {
836 GST_OBJECT_UNLOCK (self);
837 /* That means we received FLUSH_STOP/FLUSH_STOP on
838 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
839 gst_aggregator_flush (self);
840 gst_pad_push_event (self->srcpad, event);
842 SRC_STREAM_LOCK (self);
843 priv->send_eos = TRUE;
844 SRC_STREAM_BROADCAST (self);
845 SRC_STREAM_UNLOCK (self);
847 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
848 GST_PAD_STREAM_UNLOCK (self->srcpad);
849 gst_aggregator_start_srcpad_task (self);
851 GST_OBJECT_UNLOCK (self);
854 GST_OBJECT_UNLOCK (self);
857 /* We never forward the event */
862 GST_DEBUG_OBJECT (aggpad, "EOS");
864 /* We still have a buffer, and we don't want the subclass to have to
865 * check for it. Mark pending_eos, eos will be set when steal_buffer is
868 SRC_STREAM_LOCK (self);
870 if (!aggpad->buffer) {
873 aggpad->priv->pending_eos = TRUE;
877 SRC_STREAM_BROADCAST (self);
878 SRC_STREAM_UNLOCK (self);
881 case GST_EVENT_SEGMENT:
884 gst_event_copy_segment (event, &aggpad->segment);
885 self->priv->seqnum = gst_event_get_seqnum (event);
889 case GST_EVENT_STREAM_START:
897 gst_event_parse_tag (event, &tags);
899 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
900 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
901 gst_event_unref (event);
913 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
914 return gst_pad_event_default (pad, GST_OBJECT (self), event);
917 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
919 gst_event_unref (event);
924 static inline gboolean
925 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
926 gpointer unused_udata)
928 gst_aggregator_pad_flush (pad, self);
934 gst_aggregator_stop (GstAggregator * agg)
936 GstAggregatorClass *klass;
939 gst_aggregator_reset_flow_values (agg);
941 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
943 klass = GST_AGGREGATOR_GET_CLASS (agg);
946 result = klass->stop (agg);
951 gst_tag_list_unref (agg->priv->tags);
952 agg->priv->tags = NULL;
957 /* GstElement vmethods implementations */
958 static GstStateChangeReturn
959 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
961 GstStateChangeReturn ret;
962 GstAggregator *self = GST_AGGREGATOR (element);
964 switch (transition) {
965 case GST_STATE_CHANGE_READY_TO_PAUSED:
966 if (!gst_aggregator_start (self))
974 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
975 transition)) == GST_STATE_CHANGE_FAILURE)
979 switch (transition) {
980 case GST_STATE_CHANGE_PAUSED_TO_READY:
981 if (!gst_aggregator_stop (self)) {
982 /* What to do in this case? Error out? */
983 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
995 GST_ERROR_OBJECT (element, "parent failed state change");
1000 GST_ERROR_OBJECT (element, "Subclass failed to start");
1001 return GST_STATE_CHANGE_FAILURE;
1006 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1008 GstAggregator *self = GST_AGGREGATOR (element);
1011 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1013 GST_INFO_OBJECT (pad, "Removing pad");
1015 SRC_STREAM_LOCK (self);
1016 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1017 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
1018 gst_buffer_replace (&tmpbuf, NULL);
1019 gst_element_remove_pad (element, pad);
1021 SRC_STREAM_BROADCAST (self);
1022 SRC_STREAM_UNLOCK (self);
1026 gst_aggregator_request_new_pad (GstElement * element,
1027 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1029 GstAggregator *self;
1030 GstAggregatorPad *agg_pad;
1032 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1033 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1035 self = GST_AGGREGATOR (element);
1037 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1041 GST_OBJECT_LOCK (element);
1042 if (req_name == NULL || strlen (req_name) < 6
1043 || !g_str_has_prefix (req_name, "sink_")) {
1044 /* no name given when requesting the pad, use next available int */
1047 /* parse serial number from requested padname */
1048 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1049 if (serial >= priv->padcount)
1050 priv->padcount = serial;
1053 name = g_strdup_printf ("sink_%u", priv->padcount);
1054 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1055 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1058 GST_OBJECT_UNLOCK (element);
1064 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1067 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1069 /* add the pad to the element */
1070 gst_element_add_pad (element, GST_PAD (agg_pad));
1072 return GST_PAD (agg_pad);
1077 GstClockTime min, max;
1082 gst_aggregator_query_sink_latency_foreach (GstAggregator * self,
1083 GstAggregatorPad * pad, gpointer user_data)
1085 LatencyData *data = user_data;
1086 GstClockTime min, max;
1090 query = gst_query_new_latency ();
1091 res = gst_pad_peer_query (GST_PAD_CAST (pad), query);
1094 gst_query_parse_latency (query, &live, &min, &max);
1096 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
1097 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1099 if (min != GST_CLOCK_TIME_NONE && min > data->min)
1102 if (max != GST_CLOCK_TIME_NONE &&
1103 ((data->max != GST_CLOCK_TIME_NONE && max < data->max) ||
1104 (data->max == GST_CLOCK_TIME_NONE)))
1110 gst_query_unref (query);
1116 * gst_aggregator_get_latency_unlocked:
1117 * @self: a #GstAggregator
1118 * @live: (out) (allow-none): whether @self is live
1119 * @min_latency: (out) (allow-none): the configured minimum latency of @self
1120 * @max_latency: (out) (allow-none): the configured maximum latency of @self
1122 * Retreives the latency values reported by @self in response to the latency
1125 * Typically only called by subclasses.
1127 * MUST be called with the object lock held.
1130 gst_aggregator_get_latency_unlocked (GstAggregator * self, gboolean * live,
1131 GstClockTime * min_latency, GstClockTime * max_latency)
1133 GstClockTime our_latency;
1134 GstClockTime min, max;
1136 g_return_if_fail (GST_IS_AGGREGATOR (self));
1138 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1139 min = self->priv->latency_min;
1140 max = self->priv->latency_max;
1142 min += self->priv->sub_latency_min;
1143 if (GST_CLOCK_TIME_IS_VALID (max)
1144 && GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max))
1145 max += self->priv->sub_latency_max;
1147 our_latency = self->priv->latency;
1148 if (GST_CLOCK_TIME_IS_VALID (our_latency)) {
1150 if (GST_CLOCK_TIME_IS_VALID (max))
1155 *live = self->priv->latency_live;
1163 gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
1165 GstClockTime our_latency;
1169 data.max = GST_CLOCK_TIME_NONE;
1172 /* query upstream's latency */
1173 SRC_STREAM_LOCK (self);
1174 gst_aggregator_iterate_sinkpads (self,
1175 gst_aggregator_query_sink_latency_foreach, &data);
1176 SRC_STREAM_UNLOCK (self);
1178 our_latency = self->priv->latency;
1180 if (data.live && GST_CLOCK_TIME_IS_VALID (our_latency) &&
1181 our_latency > data.max) {
1182 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1183 ("%s", "Latency too big"),
1184 ("The requested latency value is too big for the current pipeline. "
1185 "Limiting to %" G_GINT64_FORMAT, data.max));
1186 self->priv->latency = data.max;
1187 /* FIXME: shouldn't we g_object_notify() the change here? */
1190 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (data.min))) {
1191 GST_WARNING_OBJECT (self, "Invalid minimum latency, using 0");
1195 if (G_UNLIKELY (data.min > data.max)) {
1196 GST_WARNING_OBJECT (self, "Minimum latency is greater than maximum latency "
1197 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT "). "
1198 "Clamping it at the maximum latency", data.min, data.max);
1199 data.min = data.max;
1202 self->priv->latency_live = data.live;
1203 self->priv->latency_min = data.min;
1204 self->priv->latency_max = data.max;
1207 if (GST_CLOCK_TIME_IS_VALID (our_latency)) {
1208 if (GST_CLOCK_TIME_IS_VALID (data.min))
1209 data.min += our_latency;
1210 if (GST_CLOCK_TIME_IS_VALID (data.max))
1211 data.max += our_latency;
1214 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_min)
1215 && GST_CLOCK_TIME_IS_VALID (data.min))
1216 data.min += self->priv->sub_latency_min;
1217 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1218 && GST_CLOCK_TIME_IS_VALID (data.max))
1219 data.max += self->priv->sub_latency_max;
1221 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1222 " max:%" G_GINT64_FORMAT, data.live ? "true" : "false", data.min,
1225 gst_query_set_latency (query, data.live, data.min, data.max);
1231 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1233 GstAggregator *self = GST_AGGREGATOR (element);
1235 GST_STATE_LOCK (element);
1236 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1237 GST_STATE (element) < GST_STATE_PAUSED) {
1241 GstSeekType start_type, stop_type;
1244 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1245 &start, &stop_type, &stop);
1246 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1247 stop_type, stop, NULL);
1249 self->priv->seqnum = gst_event_get_seqnum (event);
1250 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1252 GST_STATE_UNLOCK (element);
1255 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1260 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1262 gboolean res = TRUE;
1264 switch (GST_QUERY_TYPE (query)) {
1265 case GST_QUERY_SEEKING:
1269 /* don't pass it along as some (file)sink might claim it does
1270 * whereas with a collectpads in between that will not likely work */
1271 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1272 gst_query_set_seeking (query, format, FALSE, 0, -1);
1277 case GST_QUERY_LATENCY:
1281 ret = gst_aggregator_query_latency (self, query);
1282 /* Wake up the src thread again, due to changed latencies
1283 * or changed live-ness we might have to adjust if we wait
1284 * on a deadline at all and how long.
1285 * This is only to unschedule the clock id, we don't really care
1286 * about the GCond here.
1288 SRC_STREAM_LOCK (self);
1289 SRC_STREAM_BROADCAST (self);
1290 SRC_STREAM_UNLOCK (self);
1297 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1304 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1306 EventData *evdata = user_data;
1307 gboolean ret = TRUE;
1308 GstPad *peer = gst_pad_get_peer (pad);
1309 GstAggregatorPadPrivate *padpriv = GST_AGGREGATOR_PAD (pad)->priv;
1312 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1313 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1314 gst_object_unref (peer);
1318 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1319 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1321 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1322 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1324 if (gst_pad_query (peer, seeking)) {
1327 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1329 if (seekable == FALSE) {
1330 GST_INFO_OBJECT (pad,
1331 "Source not seekable, We failed but it does not matter!");
1336 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1339 gst_query_unref (seeking);
1342 if (evdata->flush) {
1343 padpriv->pending_flush_start = FALSE;
1344 padpriv->pending_flush_stop = FALSE;
1347 evdata->one_actually_seeked = TRUE;
1350 evdata->result &= ret;
1352 /* Always send to all pads */
1357 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1358 GstEvent * event, gboolean flush)
1362 evdata.event = event;
1363 evdata.result = TRUE;
1364 evdata.flush = flush;
1365 evdata.one_actually_seeked = FALSE;
1367 /* We first need to set all pads as flushing in a first pass
1368 * as flush_start flush_stop is sometimes sent synchronously
1369 * while we send the seek event */
1373 GST_OBJECT_LOCK (self);
1374 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1375 GstAggregatorPad *pad = l->data;
1377 pad->priv->pending_flush_start = TRUE;
1378 pad->priv->pending_flush_stop = FALSE;
1380 GST_OBJECT_UNLOCK (self);
1383 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1385 gst_event_unref (event);
1391 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1396 GstSeekType start_type, stop_type;
1400 GstAggregatorPrivate *priv = self->priv;
1402 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1403 &start, &stop_type, &stop);
1405 GST_INFO_OBJECT (self, "starting SEEK");
1407 flush = flags & GST_SEEK_FLAG_FLUSH;
1409 GST_OBJECT_LOCK (self);
1411 priv->pending_flush_start = TRUE;
1412 priv->flush_seeking = TRUE;
1415 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1416 stop_type, stop, NULL);
1417 GST_OBJECT_UNLOCK (self);
1419 /* forward the seek upstream */
1420 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, flush);
1423 if (!evdata.result || !evdata.one_actually_seeked) {
1424 GST_OBJECT_LOCK (self);
1425 priv->flush_seeking = FALSE;
1426 priv->pending_flush_start = FALSE;
1427 GST_OBJECT_UNLOCK (self);
1430 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1432 return evdata.result;
1436 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1439 gboolean res = TRUE;
1441 switch (GST_EVENT_TYPE (event)) {
1442 case GST_EVENT_SEEK:
1444 gst_event_ref (event);
1445 res = gst_aggregator_do_seek (self, event);
1446 gst_event_unref (event);
1450 case GST_EVENT_NAVIGATION:
1452 /* navigation is rather pointless. */
1454 gst_event_unref (event);
1463 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE);
1464 res = evdata.result;
1471 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1474 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1476 return klass->src_event (GST_AGGREGATOR (parent), event);
1480 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1483 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1485 return klass->src_query (GST_AGGREGATOR (parent), query);
1489 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1490 GstObject * parent, GstPadMode mode, gboolean active)
1492 GstAggregator *self = GST_AGGREGATOR (parent);
1493 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1495 if (klass->src_activate) {
1496 if (klass->src_activate (self, mode, active) == FALSE) {
1501 if (active == TRUE) {
1503 case GST_PAD_MODE_PUSH:
1505 GST_INFO_OBJECT (pad, "Activating pad!");
1506 gst_aggregator_start_srcpad_task (self);
1511 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1518 GST_INFO_OBJECT (self, "Deactivating srcpad");
1519 gst_aggregator_stop_srcpad_task (self, FALSE);
1525 gst_aggregator_default_sink_query (GstAggregator * self,
1526 GstAggregatorPad * aggpad, GstQuery * query)
1528 GstPad *pad = GST_PAD (aggpad);
1530 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1534 gst_aggregator_finalize (GObject * object)
1536 GstAggregator *self = (GstAggregator *) object;
1538 g_mutex_clear (&self->priv->src_lock);
1539 g_cond_clear (&self->priv->src_cond);
1541 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1545 * gst_aggregator_set_latency_property:
1546 * @agg: a #GstAggregator
1547 * @latency: the new latency value.
1549 * Sets the new latency value to @latency. This value is used to limit the
1550 * amount of time a pad waits for data to appear before considering the pad
1554 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1558 g_return_if_fail (GST_IS_AGGREGATOR (self));
1560 GST_OBJECT_LOCK (self);
1562 if (self->priv->latency_live && self->priv->latency_max != 0 &&
1563 GST_CLOCK_TIME_IS_VALID (latency) && latency > self->priv->latency_max) {
1564 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1565 ("%s", "Latency too big"),
1566 ("The requested latency value is too big for the latency in the "
1567 "current pipeline. Limiting to %" G_GINT64_FORMAT,
1568 self->priv->latency_max));
1569 latency = self->priv->latency_max;
1572 changed = (self->priv->latency != latency);
1573 self->priv->latency = latency;
1574 GST_OBJECT_UNLOCK (self);
1577 gst_element_post_message (GST_ELEMENT_CAST (self),
1578 gst_message_new_latency (GST_OBJECT_CAST (self)));
1582 * gst_aggregator_get_latency_property:
1583 * @agg: a #GstAggregator
1585 * Gets the latency value. See gst_aggregator_set_latency for
1588 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1589 * before a pad is deemed unresponsive. A value of -1 means an
1593 gst_aggregator_get_latency_property (GstAggregator * agg)
1597 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1599 GST_OBJECT_LOCK (agg);
1600 res = agg->priv->latency;
1601 GST_OBJECT_UNLOCK (agg);
1607 gst_aggregator_set_property (GObject * object, guint prop_id,
1608 const GValue * value, GParamSpec * pspec)
1610 GstAggregator *agg = GST_AGGREGATOR (object);
1614 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1617 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1623 gst_aggregator_get_property (GObject * object, guint prop_id,
1624 GValue * value, GParamSpec * pspec)
1626 GstAggregator *agg = GST_AGGREGATOR (object);
1630 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1633 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1638 /* GObject vmethods implementations */
1640 gst_aggregator_class_init (GstAggregatorClass * klass)
1642 GObjectClass *gobject_class = (GObjectClass *) klass;
1643 GstElementClass *gstelement_class = (GstElementClass *) klass;
1645 aggregator_parent_class = g_type_class_peek_parent (klass);
1646 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1648 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1649 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1651 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1653 klass->sink_event = gst_aggregator_default_sink_event;
1654 klass->sink_query = gst_aggregator_default_sink_query;
1656 klass->src_event = gst_aggregator_default_src_event;
1657 klass->src_query = gst_aggregator_default_src_query;
1659 gstelement_class->request_new_pad =
1660 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1661 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1662 gstelement_class->release_pad =
1663 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1664 gstelement_class->change_state =
1665 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1667 gobject_class->set_property = gst_aggregator_set_property;
1668 gobject_class->get_property = gst_aggregator_get_property;
1669 gobject_class->finalize = gst_aggregator_finalize;
1671 g_object_class_install_property (gobject_class, PROP_LATENCY,
1672 g_param_spec_int64 ("latency", "Buffer latency",
1673 "Additional latency in live mode to allow upstream "
1674 "to take longer to produce buffers for the current "
1676 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1677 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1679 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1680 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_query_sink_latency_foreach);
1684 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1686 GstPadTemplate *pad_template;
1687 GstAggregatorPrivate *priv;
1689 g_return_if_fail (klass->aggregate != NULL);
1692 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1693 GstAggregatorPrivate);
1698 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1699 g_return_if_fail (pad_template != NULL);
1701 priv->padcount = -1;
1702 priv->tags_changed = FALSE;
1704 self->priv->latency_live = FALSE;
1705 self->priv->latency_min = self->priv->sub_latency_min = 0;
1706 self->priv->latency_max = self->priv->sub_latency_max = GST_CLOCK_TIME_NONE;
1707 gst_aggregator_reset_flow_values (self);
1709 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1711 gst_pad_set_event_function (self->srcpad,
1712 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
1713 gst_pad_set_query_function (self->srcpad,
1714 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
1715 gst_pad_set_activatemode_function (self->srcpad,
1716 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
1718 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1720 self->priv->latency = DEFAULT_LATENCY;
1722 g_mutex_init (&self->priv->src_lock);
1723 g_cond_init (&self->priv->src_cond);
1726 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1727 * method to get to the padtemplates */
1729 gst_aggregator_get_type (void)
1731 static volatile gsize type = 0;
1733 if (g_once_init_enter (&type)) {
1735 static const GTypeInfo info = {
1736 sizeof (GstAggregatorClass),
1739 (GClassInitFunc) gst_aggregator_class_init,
1742 sizeof (GstAggregator),
1744 (GInstanceInitFunc) gst_aggregator_init,
1747 _type = g_type_register_static (GST_TYPE_ELEMENT,
1748 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1749 g_once_init_leave (&type, _type);
1754 static GstFlowReturn
1755 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1757 GstBuffer *actual_buf = buffer;
1758 GstAggregator *self = GST_AGGREGATOR (object);
1759 GstAggregatorPrivate *priv = self->priv;
1760 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1761 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1762 GstFlowReturn flow_return;
1764 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1766 PAD_STREAM_LOCK (aggpad);
1768 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1771 if (g_atomic_int_get (&aggpad->priv->pending_eos) == TRUE)
1776 while (aggpad->buffer && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1777 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1778 PAD_WAIT_EVENT (aggpad);
1780 PAD_UNLOCK (aggpad);
1782 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1785 if (aggclass->clip) {
1786 aggclass->clip (self, aggpad, buffer, &actual_buf);
1789 SRC_STREAM_LOCK (self);
1792 gst_buffer_unref (aggpad->buffer);
1793 aggpad->buffer = actual_buf;
1794 PAD_UNLOCK (aggpad);
1795 PAD_STREAM_UNLOCK (aggpad);
1797 if (gst_aggregator_check_pads_ready (self))
1798 SRC_STREAM_BROADCAST (self);
1799 SRC_STREAM_UNLOCK (self);
1801 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1803 GST_OBJECT_LOCK (self);
1804 flow_return = priv->flow_return;
1805 GST_OBJECT_UNLOCK (self);
1810 PAD_STREAM_UNLOCK (aggpad);
1812 gst_buffer_unref (buffer);
1813 GST_DEBUG_OBJECT (aggpad, "We are flushing");
1815 return GST_FLOW_FLUSHING;
1818 PAD_STREAM_UNLOCK (aggpad);
1820 gst_buffer_unref (buffer);
1821 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1823 return GST_FLOW_EOS;
1827 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
1830 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1831 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1833 if (GST_QUERY_IS_SERIALIZED (query)) {
1836 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
1837 PAD_UNLOCK (aggpad);
1841 while (aggpad->buffer
1842 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1843 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1844 PAD_WAIT_EVENT (aggpad);
1846 PAD_UNLOCK (aggpad);
1848 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1852 return klass->sink_query (GST_AGGREGATOR (parent),
1853 GST_AGGREGATOR_PAD (pad), query);
1856 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping query");
1861 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
1864 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1865 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1867 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1868 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1871 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1872 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
1873 PAD_UNLOCK (aggpad);
1877 while (aggpad->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
1885 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1889 return klass->sink_event (GST_AGGREGATOR (parent),
1890 GST_AGGREGATOR_PAD (pad), event);
1893 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping event");
1894 if (GST_EVENT_IS_STICKY (event))
1895 gst_pad_store_sticky_event (pad, event);
1896 gst_event_unref (event);
1901 gst_aggregator_pad_activate_mode_func (GstPad * pad,
1902 GstObject * parent, GstPadMode mode, gboolean active)
1904 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1906 if (active == FALSE) {
1908 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1909 gst_buffer_replace (&aggpad->buffer, NULL);
1910 PAD_BROADCAST_EVENT (aggpad);
1911 PAD_UNLOCK (aggpad);
1914 g_atomic_int_set (&aggpad->priv->flushing, FALSE);
1915 PAD_BROADCAST_EVENT (aggpad);
1916 PAD_UNLOCK (aggpad);
1922 /***********************************
1923 * GstAggregatorPad implementation *
1924 ************************************/
1925 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1928 gst_aggregator_pad_constructed (GObject * object)
1930 GstPad *pad = GST_PAD (object);
1932 gst_pad_set_chain_function (pad,
1933 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
1934 gst_pad_set_event_function (pad,
1935 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
1936 gst_pad_set_query_function (pad,
1937 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
1938 gst_pad_set_activatemode_function (pad,
1939 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
1943 gst_aggregator_pad_finalize (GObject * object)
1945 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1947 g_cond_clear (&pad->priv->event_cond);
1948 g_mutex_clear (&pad->priv->stream_lock);
1950 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
1954 gst_aggregator_pad_dispose (GObject * object)
1956 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1959 buf = gst_aggregator_pad_steal_buffer (pad);
1961 gst_buffer_unref (buf);
1963 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
1967 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
1969 GObjectClass *gobject_class = (GObjectClass *) klass;
1971 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
1973 gobject_class->constructed = gst_aggregator_pad_constructed;
1974 gobject_class->finalize = gst_aggregator_pad_finalize;
1975 gobject_class->dispose = gst_aggregator_pad_dispose;
1979 gst_aggregator_pad_init (GstAggregatorPad * pad)
1982 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
1983 GstAggregatorPadPrivate);
1986 g_cond_init (&pad->priv->event_cond);
1988 g_mutex_init (&pad->priv->stream_lock);
1992 * gst_aggregator_pad_steal_buffer_unlocked:
1993 * @pad: the pad to get buffer from
1995 * Steal the ref to the buffer currently queued in @pad.
1997 * MUST be called with the pad's object lock held.
1999 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2000 * queued. You should unref the buffer after usage.
2003 gst_aggregator_pad_steal_buffer_unlocked (GstAggregatorPad * pad)
2005 GstBuffer *buffer = NULL;
2008 GST_TRACE_OBJECT (pad, "Consuming buffer");
2009 buffer = pad->buffer;
2011 if (pad->priv->pending_eos) {
2012 pad->priv->pending_eos = FALSE;
2015 PAD_BROADCAST_EVENT (pad);
2016 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
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 buffer = gst_aggregator_pad_steal_buffer_unlocked (pad);
2044 * gst_aggregator_pad_get_buffer:
2045 * @pad: the pad to get buffer from
2047 * Returns: (transfer full): A reference to the buffer in @pad or
2048 * NULL if no buffer was queued. You should unref the buffer after
2052 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2054 GstBuffer *buffer = NULL;
2058 buffer = gst_buffer_ref (pad->buffer);
2065 * gst_aggregator_merge_tags:
2066 * @self: a #GstAggregator
2067 * @tags: a #GstTagList to merge
2068 * @mode: the #GstTagMergeMode to use
2070 * Adds tags to so-called pending tags, which will be processed
2071 * before pushing out data downstream.
2073 * Note that this is provided for convenience, and the subclass is
2074 * not required to use this and can still do tag handling on its own.
2079 gst_aggregator_merge_tags (GstAggregator * self,
2080 const GstTagList * tags, GstTagMergeMode mode)
2084 g_return_if_fail (GST_IS_AGGREGATOR (self));
2085 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2087 /* FIXME Check if we can use OBJECT lock here! */
2088 GST_OBJECT_LOCK (self);
2090 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2091 otags = self->priv->tags;
2092 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2094 gst_tag_list_unref (otags);
2095 self->priv->tags_changed = TRUE;
2096 GST_OBJECT_UNLOCK (self);
2100 * gst_aggregator_set_latency:
2101 * @self: a #GstAggregator
2102 * @min_latency: minimum latency
2103 * @max_latency: maximum latency
2105 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2106 * latency is. Will also post a LATENCY message on the bus so the pipeline
2107 * can reconfigure its global latency.
2110 gst_aggregator_set_latency (GstAggregator * self,
2111 GstClockTime min_latency, GstClockTime max_latency)
2113 g_return_if_fail (GST_IS_AGGREGATOR (self));
2114 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2115 g_return_if_fail (max_latency >= min_latency);
2117 GST_OBJECT_LOCK (self);
2118 self->priv->sub_latency_min = min_latency;
2119 self->priv->sub_latency_max = max_latency;
2120 GST_OBJECT_UNLOCK (self);
2122 gst_element_post_message (GST_ELEMENT_CAST (self),
2123 gst_message_new_latency (GST_OBJECT_CAST (self)));