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 g_mutex_lock(&pad->priv->lock); \
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 g_mutex_unlock(&pad->priv->lock); \
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 (&((GstAggregatorPad*)pad)->priv->lock)); \
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 /* To always be used atomically */
167 /* Following fields are protected by the PAD_LOCK */
168 gboolean pending_flush_start;
169 gboolean pending_flush_stop;
170 gboolean pending_eos;
181 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
183 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
186 aggpad->priv->eos = FALSE;
187 aggpad->priv->flushing = FALSE;
191 return klass->flush (aggpad, agg);
196 /*************************************
197 * GstAggregator implementation *
198 *************************************/
199 static GstElementClass *aggregator_parent_class = NULL;
201 /* All members are protected by the object lock unless otherwise noted */
203 struct _GstAggregatorPrivate
207 /* Our state is >= PAUSED */
208 gboolean running; /* protected by SRC_STREAM_LOCK */
211 gboolean send_stream_start; /* protected by srcpad stream lock */
212 gboolean send_segment;
213 gboolean flush_seeking;
214 gboolean pending_flush_start;
215 gboolean send_eos; /* protected by srcpad stream lock */
216 GstFlowReturn flow_return;
218 GstCaps *srccaps; /* protected by the srcpad stream 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; /* protected by src_lock */
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->priv->buffer == NULL && !pad->priv->eos) {
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 event = gst_event_new_eos ();
500 GST_OBJECT_LOCK (self);
501 self->priv->send_eos = FALSE;
502 gst_event_set_seqnum (event, self->priv->seqnum);
503 GST_OBJECT_UNLOCK (self);
505 gst_pad_push_event (self->srcpad, event);
509 gst_aggregator_get_next_time (GstAggregator * self)
511 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
513 if (klass->get_next_time)
514 return klass->get_next_time (self);
516 return GST_CLOCK_TIME_NONE;
519 /* called with the src STREAM lock */
521 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
523 GstClockTime latency_max, latency_min;
529 SRC_STREAM_LOCK (self);
531 GST_OBJECT_LOCK (self);
532 gst_aggregator_get_latency_unlocked (self, &live, &latency_min, &latency_max);
533 GST_OBJECT_UNLOCK (self);
535 if (gst_aggregator_check_pads_ready (self)) {
536 GST_DEBUG_OBJECT (self, "all pads have data");
537 SRC_STREAM_UNLOCK (self);
542 /* Before waiting, check if we're actually still running */
543 if (!self->priv->running || !self->priv->send_eos) {
544 SRC_STREAM_UNLOCK (self);
549 start = gst_aggregator_get_next_time (self);
551 if (!live || !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self))
552 || !GST_CLOCK_TIME_IS_VALID (start)) {
553 /* We wake up here when something happened, and below
554 * then check if we're ready now. If we return FALSE,
555 * we will be directly called again.
557 SRC_STREAM_WAIT (self);
559 GstClockTime base_time, time;
561 GstClockReturn status;
562 GstClockTimeDiff jitter;
564 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
565 GST_TIME_ARGS (start));
567 GST_OBJECT_LOCK (self);
568 base_time = GST_ELEMENT_CAST (self)->base_time;
569 clock = GST_ELEMENT_CLOCK (self);
571 gst_object_ref (clock);
573 time = base_time + start;
576 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
577 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
578 " latency max %" GST_TIME_FORMAT " latency min %" GST_TIME_FORMAT
579 " current %" GST_TIME_FORMAT ")", GST_TIME_ARGS (time),
580 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
581 GST_TIME_ARGS (start), GST_TIME_ARGS (latency_max),
582 GST_TIME_ARGS (latency_min),
583 GST_TIME_ARGS (gst_clock_get_time (clock)));
585 GST_OBJECT_UNLOCK (self);
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)
689 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
696 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
700 GST_INFO_OBJECT (self, "%s srcpad task",
701 flush_start ? "Pausing" : "Stopping");
703 SRC_STREAM_LOCK (self);
704 self->priv->running = FALSE;
705 SRC_STREAM_BROADCAST (self);
706 SRC_STREAM_UNLOCK (self);
709 res = gst_pad_push_event (self->srcpad, flush_start);
712 gst_pad_stop_task (self->srcpad);
718 gst_aggregator_start_srcpad_task (GstAggregator * self)
720 GST_INFO_OBJECT (self, "Starting srcpad task");
722 self->priv->running = TRUE;
723 gst_pad_start_task (GST_PAD (self->srcpad),
724 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
728 gst_aggregator_flush (GstAggregator * self)
730 GstFlowReturn ret = GST_FLOW_OK;
731 GstAggregatorPrivate *priv = self->priv;
732 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
734 GST_DEBUG_OBJECT (self, "Flushing everything");
735 GST_OBJECT_LOCK (self);
736 priv->send_segment = TRUE;
737 priv->flush_seeking = FALSE;
738 priv->tags_changed = FALSE;
739 GST_OBJECT_UNLOCK (self);
741 ret = klass->flush (self);
747 /* Called with GstAggregator's object lock held */
750 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
753 GstAggregatorPad *tmppad;
755 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
756 tmppad = (GstAggregatorPad *) tmp->data;
758 if (_check_pending_flush_stop (tmppad) == FALSE) {
759 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
760 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
769 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
773 GstAggregatorPrivate *priv = self->priv;
774 GstAggregatorPadPrivate *padpriv = aggpad->priv;
776 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
777 /* Remove pad buffer and wake up the streaming thread */
778 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
779 gst_buffer_replace (&tmpbuf, NULL);
781 PAD_STREAM_LOCK (aggpad);
783 if (padpriv->pending_flush_start) {
784 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
786 padpriv->pending_flush_start = FALSE;
787 padpriv->pending_flush_stop = TRUE;
791 GST_OBJECT_LOCK (self);
792 if (priv->flush_seeking) {
793 /* If flush_seeking we forward the first FLUSH_START */
794 if (priv->pending_flush_start) {
795 priv->pending_flush_start = FALSE;
796 GST_OBJECT_UNLOCK (self);
798 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
799 gst_aggregator_stop_srcpad_task (self, event);
800 priv->flow_return = GST_FLOW_OK;
802 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
803 GST_PAD_STREAM_LOCK (self->srcpad);
804 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
807 GST_OBJECT_UNLOCK (self);
808 gst_event_unref (event);
811 GST_OBJECT_UNLOCK (self);
812 gst_event_unref (event);
814 PAD_STREAM_UNLOCK (aggpad);
816 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
817 gst_buffer_replace (&tmpbuf, NULL);
820 /* GstAggregator vmethods default implementations */
822 gst_aggregator_default_sink_event (GstAggregator * self,
823 GstAggregatorPad * aggpad, GstEvent * event)
826 GstPad *pad = GST_PAD (aggpad);
827 GstAggregatorPrivate *priv = self->priv;
829 switch (GST_EVENT_TYPE (event)) {
830 case GST_EVENT_FLUSH_START:
832 gst_aggregator_flush_start (self, aggpad, event);
833 /* We forward only in one case: right after flush_seeking */
837 case GST_EVENT_FLUSH_STOP:
839 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
841 gst_aggregator_pad_flush (aggpad, self);
842 GST_OBJECT_LOCK (self);
843 if (priv->flush_seeking) {
844 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
845 if (gst_aggregator_all_flush_stop_received_locked (self)) {
846 GST_OBJECT_UNLOCK (self);
847 /* That means we received FLUSH_STOP/FLUSH_STOP on
848 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
849 gst_aggregator_flush (self);
850 gst_pad_push_event (self->srcpad, event);
852 SRC_STREAM_LOCK (self);
853 priv->send_eos = TRUE;
854 SRC_STREAM_BROADCAST (self);
855 SRC_STREAM_UNLOCK (self);
857 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
858 GST_PAD_STREAM_UNLOCK (self->srcpad);
859 gst_aggregator_start_srcpad_task (self);
861 GST_OBJECT_UNLOCK (self);
864 GST_OBJECT_UNLOCK (self);
867 /* We never forward the event */
872 GST_DEBUG_OBJECT (aggpad, "EOS");
874 /* We still have a buffer, and we don't want the subclass to have to
875 * check for it. Mark pending_eos, eos will be set when steal_buffer is
878 SRC_STREAM_LOCK (self);
880 if (!aggpad->priv->buffer) {
881 aggpad->priv->eos = TRUE;
883 aggpad->priv->pending_eos = TRUE;
887 SRC_STREAM_BROADCAST (self);
888 SRC_STREAM_UNLOCK (self);
891 case GST_EVENT_SEGMENT:
893 GST_OBJECT_LOCK (aggpad);
894 gst_event_copy_segment (event, &aggpad->segment);
895 GST_OBJECT_UNLOCK (aggpad);
897 GST_OBJECT_LOCK (self);
898 self->priv->seqnum = gst_event_get_seqnum (event);
899 GST_OBJECT_UNLOCK (self);
902 case GST_EVENT_STREAM_START:
910 gst_event_parse_tag (event, &tags);
912 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
913 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
914 gst_event_unref (event);
926 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
927 return gst_pad_event_default (pad, GST_OBJECT (self), event);
930 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
932 gst_event_unref (event);
937 static inline gboolean
938 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
939 gpointer unused_udata)
941 gst_aggregator_pad_flush (pad, self);
947 gst_aggregator_stop (GstAggregator * agg)
949 GstAggregatorClass *klass;
952 gst_aggregator_reset_flow_values (agg);
954 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
956 klass = GST_AGGREGATOR_GET_CLASS (agg);
959 result = klass->stop (agg);
964 gst_tag_list_unref (agg->priv->tags);
965 agg->priv->tags = NULL;
970 /* GstElement vmethods implementations */
971 static GstStateChangeReturn
972 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
974 GstStateChangeReturn ret;
975 GstAggregator *self = GST_AGGREGATOR (element);
977 switch (transition) {
978 case GST_STATE_CHANGE_READY_TO_PAUSED:
979 if (!gst_aggregator_start (self))
987 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
988 transition)) == GST_STATE_CHANGE_FAILURE)
992 switch (transition) {
993 case GST_STATE_CHANGE_PAUSED_TO_READY:
994 if (!gst_aggregator_stop (self)) {
995 /* What to do in this case? Error out? */
996 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1008 GST_ERROR_OBJECT (element, "parent failed state change");
1013 GST_ERROR_OBJECT (element, "Subclass failed to start");
1014 return GST_STATE_CHANGE_FAILURE;
1019 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1021 GstAggregator *self = GST_AGGREGATOR (element);
1024 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1026 GST_INFO_OBJECT (pad, "Removing pad");
1028 SRC_STREAM_LOCK (self);
1029 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1030 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
1031 gst_buffer_replace (&tmpbuf, NULL);
1032 gst_element_remove_pad (element, pad);
1034 SRC_STREAM_BROADCAST (self);
1035 SRC_STREAM_UNLOCK (self);
1039 gst_aggregator_request_new_pad (GstElement * element,
1040 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1042 GstAggregator *self;
1043 GstAggregatorPad *agg_pad;
1045 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1046 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1048 self = GST_AGGREGATOR (element);
1050 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1054 GST_OBJECT_LOCK (element);
1055 if (req_name == NULL || strlen (req_name) < 6
1056 || !g_str_has_prefix (req_name, "sink_")) {
1057 /* no name given when requesting the pad, use next available int */
1060 /* parse serial number from requested padname */
1061 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1062 if (serial >= priv->padcount)
1063 priv->padcount = serial;
1066 name = g_strdup_printf ("sink_%u", priv->padcount);
1067 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1068 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1071 GST_OBJECT_UNLOCK (element);
1077 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1080 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1082 /* add the pad to the element */
1083 gst_element_add_pad (element, GST_PAD (agg_pad));
1085 return GST_PAD (agg_pad);
1090 GstClockTime min, max;
1095 gst_aggregator_query_sink_latency_foreach (GstAggregator * self,
1096 GstAggregatorPad * pad, gpointer user_data)
1098 LatencyData *data = user_data;
1099 GstClockTime min, max;
1103 query = gst_query_new_latency ();
1104 res = gst_pad_peer_query (GST_PAD_CAST (pad), query);
1107 gst_query_parse_latency (query, &live, &min, &max);
1109 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
1110 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1112 if (min != GST_CLOCK_TIME_NONE && min > data->min)
1115 if (max != GST_CLOCK_TIME_NONE &&
1116 ((data->max != GST_CLOCK_TIME_NONE && max < data->max) ||
1117 (data->max == GST_CLOCK_TIME_NONE)))
1123 gst_query_unref (query);
1129 * gst_aggregator_get_latency_unlocked:
1130 * @self: a #GstAggregator
1131 * @live: (out) (allow-none): whether @self is live
1132 * @min_latency: (out) (allow-none): the configured minimum latency of @self
1133 * @max_latency: (out) (allow-none): the configured maximum latency of @self
1135 * Retreives the latency values reported by @self in response to the latency
1138 * Typically only called by subclasses.
1140 * MUST be called with the object lock held.
1143 gst_aggregator_get_latency_unlocked (GstAggregator * self, gboolean * live,
1144 GstClockTime * min_latency, GstClockTime * max_latency)
1146 GstClockTime min, max;
1148 g_return_if_fail (GST_IS_AGGREGATOR (self));
1150 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1151 min = self->priv->latency_min;
1152 max = self->priv->latency_max;
1155 min += self->priv->latency;
1156 min += self->priv->sub_latency_min;
1157 if (GST_CLOCK_TIME_IS_VALID (max)
1158 && GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max))
1159 max += self->priv->sub_latency_max;
1160 else if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max))
1161 max = self->priv->sub_latency_max;
1164 *live = self->priv->latency_live;
1172 gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
1174 GstClockTime our_latency;
1178 data.max = GST_CLOCK_TIME_NONE;
1181 /* query upstream's latency */
1182 SRC_STREAM_LOCK (self);
1183 gst_aggregator_iterate_sinkpads (self,
1184 gst_aggregator_query_sink_latency_foreach, &data);
1185 SRC_STREAM_UNLOCK (self);
1187 GST_OBJECT_LOCK (self);
1188 our_latency = self->priv->latency;
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 data.min += our_latency;
1208 data.min += self->priv->sub_latency_min;
1209 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1210 && GST_CLOCK_TIME_IS_VALID (data.max))
1211 data.max += self->priv->sub_latency_max;
1212 else if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max))
1213 data.max = self->priv->sub_latency_max;
1215 if (data.live && data.min > data.max) {
1216 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1217 ("%s", "Latency too big"),
1218 ("The requested latency value is too big for the current pipeline. "
1219 "Limiting to %" G_GINT64_FORMAT, data.max));
1220 data.min = data.max;
1221 /* FIXME: This could in theory become negative, but in
1222 * that case all is lost anyway */
1223 self->priv->latency -= data.min - data.max;
1224 /* FIXME: shouldn't we g_object_notify() the change here? */
1227 GST_OBJECT_UNLOCK (self);
1229 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1230 " max:%" G_GINT64_FORMAT, data.live ? "true" : "false", data.min,
1233 gst_query_set_latency (query, data.live, data.min, data.max);
1239 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1241 GstAggregator *self = GST_AGGREGATOR (element);
1243 GST_STATE_LOCK (element);
1244 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1245 GST_STATE (element) < GST_STATE_PAUSED) {
1249 GstSeekType start_type, stop_type;
1252 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1253 &start, &stop_type, &stop);
1255 GST_OBJECT_LOCK (self);
1256 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1257 stop_type, stop, NULL);
1258 self->priv->seqnum = gst_event_get_seqnum (event);
1259 GST_OBJECT_UNLOCK (self);
1261 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1263 GST_STATE_UNLOCK (element);
1266 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1271 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1273 gboolean res = TRUE;
1275 switch (GST_QUERY_TYPE (query)) {
1276 case GST_QUERY_SEEKING:
1280 /* don't pass it along as some (file)sink might claim it does
1281 * whereas with a collectpads in between that will not likely work */
1282 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1283 gst_query_set_seeking (query, format, FALSE, 0, -1);
1288 case GST_QUERY_LATENCY:
1292 ret = gst_aggregator_query_latency (self, query);
1293 /* Wake up the src thread again, due to changed latencies
1294 * or changed live-ness we might have to adjust if we wait
1295 * on a deadline at all and how long.
1296 * This is only to unschedule the clock id, we don't really care
1297 * about the GCond here.
1299 SRC_STREAM_LOCK (self);
1300 SRC_STREAM_BROADCAST (self);
1301 SRC_STREAM_UNLOCK (self);
1308 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1315 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1317 EventData *evdata = user_data;
1318 gboolean ret = TRUE;
1319 GstPad *peer = gst_pad_get_peer (pad);
1320 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1323 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1324 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1325 gst_object_unref (peer);
1329 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1330 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1332 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1333 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1335 if (gst_pad_query (peer, seeking)) {
1338 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1340 if (seekable == FALSE) {
1341 GST_INFO_OBJECT (pad,
1342 "Source not seekable, We failed but it does not matter!");
1347 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1350 gst_query_unref (seeking);
1353 if (evdata->flush) {
1355 aggpad->priv->pending_flush_start = FALSE;
1356 aggpad->priv->pending_flush_stop = FALSE;
1357 PAD_UNLOCK (aggpad);
1360 evdata->one_actually_seeked = TRUE;
1363 evdata->result &= ret;
1365 /* Always send to all pads */
1370 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1371 GstEvent * event, gboolean flush)
1375 evdata.event = event;
1376 evdata.result = TRUE;
1377 evdata.flush = flush;
1378 evdata.one_actually_seeked = FALSE;
1380 /* We first need to set all pads as flushing in a first pass
1381 * as flush_start flush_stop is sometimes sent synchronously
1382 * while we send the seek event */
1386 GST_OBJECT_LOCK (self);
1387 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1388 GstAggregatorPad *pad = l->data;
1391 pad->priv->pending_flush_start = TRUE;
1392 pad->priv->pending_flush_stop = FALSE;
1395 GST_OBJECT_UNLOCK (self);
1398 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1400 gst_event_unref (event);
1406 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1411 GstSeekType start_type, stop_type;
1415 GstAggregatorPrivate *priv = self->priv;
1417 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1418 &start, &stop_type, &stop);
1420 GST_INFO_OBJECT (self, "starting SEEK");
1422 flush = flags & GST_SEEK_FLAG_FLUSH;
1424 GST_OBJECT_LOCK (self);
1426 priv->pending_flush_start = TRUE;
1427 priv->flush_seeking = TRUE;
1430 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1431 stop_type, stop, NULL);
1432 GST_OBJECT_UNLOCK (self);
1434 /* forward the seek upstream */
1435 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, flush);
1438 if (!evdata.result || !evdata.one_actually_seeked) {
1439 GST_OBJECT_LOCK (self);
1440 priv->flush_seeking = FALSE;
1441 priv->pending_flush_start = FALSE;
1442 GST_OBJECT_UNLOCK (self);
1445 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1447 return evdata.result;
1451 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1454 gboolean res = TRUE;
1456 switch (GST_EVENT_TYPE (event)) {
1457 case GST_EVENT_SEEK:
1459 gst_event_ref (event);
1460 res = gst_aggregator_do_seek (self, event);
1461 gst_event_unref (event);
1465 case GST_EVENT_NAVIGATION:
1467 /* navigation is rather pointless. */
1469 gst_event_unref (event);
1478 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE);
1479 res = evdata.result;
1486 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1489 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1491 return klass->src_event (GST_AGGREGATOR (parent), event);
1495 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1498 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1500 return klass->src_query (GST_AGGREGATOR (parent), query);
1504 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1505 GstObject * parent, GstPadMode mode, gboolean active)
1507 GstAggregator *self = GST_AGGREGATOR (parent);
1508 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1510 if (klass->src_activate) {
1511 if (klass->src_activate (self, mode, active) == FALSE) {
1516 if (active == TRUE) {
1518 case GST_PAD_MODE_PUSH:
1520 GST_INFO_OBJECT (pad, "Activating pad!");
1521 gst_aggregator_start_srcpad_task (self);
1526 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1533 GST_INFO_OBJECT (self, "Deactivating srcpad");
1534 gst_aggregator_stop_srcpad_task (self, FALSE);
1540 gst_aggregator_default_sink_query (GstAggregator * self,
1541 GstAggregatorPad * aggpad, GstQuery * query)
1543 GstPad *pad = GST_PAD (aggpad);
1545 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1549 gst_aggregator_finalize (GObject * object)
1551 GstAggregator *self = (GstAggregator *) object;
1553 g_mutex_clear (&self->priv->src_lock);
1554 g_cond_clear (&self->priv->src_cond);
1556 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1560 * gst_aggregator_set_latency_property:
1561 * @agg: a #GstAggregator
1562 * @latency: the new latency value.
1564 * Sets the new latency value to @latency. This value is used to limit the
1565 * amount of time a pad waits for data to appear before considering the pad
1569 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1572 GstClockTime min, max;
1574 g_return_if_fail (GST_IS_AGGREGATOR (self));
1575 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1577 GST_OBJECT_LOCK (self);
1578 if (self->priv->latency_live) {
1579 min = self->priv->latency_min;
1580 max = self->priv->latency_max;
1583 min += self->priv->sub_latency_min;
1584 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1585 && GST_CLOCK_TIME_IS_VALID (max))
1586 max += self->priv->sub_latency_max;
1587 else if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max))
1588 max = self->priv->sub_latency_max;
1590 if (GST_CLOCK_TIME_IS_VALID (max) && min > max) {
1591 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1592 ("%s", "Latency too big"),
1593 ("The requested latency value is too big for the latency in the "
1594 "current pipeline. Limiting to %" G_GINT64_FORMAT, max));
1595 /* FIXME: This could in theory become negative, but in
1596 * that case all is lost anyway */
1597 latency -= min - max;
1598 /* FIXME: shouldn't we g_object_notify() the change here? */
1602 changed = (self->priv->latency != latency);
1603 self->priv->latency = latency;
1604 GST_OBJECT_UNLOCK (self);
1607 gst_element_post_message (GST_ELEMENT_CAST (self),
1608 gst_message_new_latency (GST_OBJECT_CAST (self)));
1612 * gst_aggregator_get_latency_property:
1613 * @agg: a #GstAggregator
1615 * Gets the latency value. See gst_aggregator_set_latency for
1618 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1619 * before a pad is deemed unresponsive. A value of -1 means an
1623 gst_aggregator_get_latency_property (GstAggregator * agg)
1627 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1629 GST_OBJECT_LOCK (agg);
1630 res = agg->priv->latency;
1631 GST_OBJECT_UNLOCK (agg);
1637 gst_aggregator_set_property (GObject * object, guint prop_id,
1638 const GValue * value, GParamSpec * pspec)
1640 GstAggregator *agg = GST_AGGREGATOR (object);
1644 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1647 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1653 gst_aggregator_get_property (GObject * object, guint prop_id,
1654 GValue * value, GParamSpec * pspec)
1656 GstAggregator *agg = GST_AGGREGATOR (object);
1660 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1663 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1668 /* GObject vmethods implementations */
1670 gst_aggregator_class_init (GstAggregatorClass * klass)
1672 GObjectClass *gobject_class = (GObjectClass *) klass;
1673 GstElementClass *gstelement_class = (GstElementClass *) klass;
1675 aggregator_parent_class = g_type_class_peek_parent (klass);
1676 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1678 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1679 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1681 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1683 klass->sink_event = gst_aggregator_default_sink_event;
1684 klass->sink_query = gst_aggregator_default_sink_query;
1686 klass->src_event = gst_aggregator_default_src_event;
1687 klass->src_query = gst_aggregator_default_src_query;
1689 gstelement_class->request_new_pad =
1690 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1691 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1692 gstelement_class->release_pad =
1693 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1694 gstelement_class->change_state =
1695 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1697 gobject_class->set_property = gst_aggregator_set_property;
1698 gobject_class->get_property = gst_aggregator_get_property;
1699 gobject_class->finalize = gst_aggregator_finalize;
1701 g_object_class_install_property (gobject_class, PROP_LATENCY,
1702 g_param_spec_int64 ("latency", "Buffer latency",
1703 "Additional latency in live mode to allow upstream "
1704 "to take longer to produce buffers for the current "
1706 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1707 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1709 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1710 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_query_sink_latency_foreach);
1714 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1716 GstPadTemplate *pad_template;
1717 GstAggregatorPrivate *priv;
1719 g_return_if_fail (klass->aggregate != NULL);
1722 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1723 GstAggregatorPrivate);
1728 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1729 g_return_if_fail (pad_template != NULL);
1731 priv->padcount = -1;
1732 priv->tags_changed = FALSE;
1734 self->priv->latency_live = FALSE;
1735 self->priv->latency_min = self->priv->sub_latency_min = 0;
1736 self->priv->latency_max = self->priv->sub_latency_max = GST_CLOCK_TIME_NONE;
1737 gst_aggregator_reset_flow_values (self);
1739 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1741 gst_pad_set_event_function (self->srcpad,
1742 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
1743 gst_pad_set_query_function (self->srcpad,
1744 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
1745 gst_pad_set_activatemode_function (self->srcpad,
1746 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
1748 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1750 self->priv->latency = DEFAULT_LATENCY;
1752 g_mutex_init (&self->priv->src_lock);
1753 g_cond_init (&self->priv->src_cond);
1756 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1757 * method to get to the padtemplates */
1759 gst_aggregator_get_type (void)
1761 static volatile gsize type = 0;
1763 if (g_once_init_enter (&type)) {
1765 static const GTypeInfo info = {
1766 sizeof (GstAggregatorClass),
1769 (GClassInitFunc) gst_aggregator_class_init,
1772 sizeof (GstAggregator),
1774 (GInstanceInitFunc) gst_aggregator_init,
1777 _type = g_type_register_static (GST_TYPE_ELEMENT,
1778 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1779 g_once_init_leave (&type, _type);
1784 static GstFlowReturn
1785 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1787 GstBuffer *actual_buf = buffer;
1788 GstAggregator *self = GST_AGGREGATOR (object);
1789 GstAggregatorPrivate *priv = self->priv;
1790 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1791 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1792 GstFlowReturn flow_return;
1794 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1796 PAD_STREAM_LOCK (aggpad);
1798 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1802 if (aggpad->priv->pending_eos == TRUE)
1805 while (aggpad->priv->buffer
1806 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1807 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1808 PAD_WAIT_EVENT (aggpad);
1810 PAD_UNLOCK (aggpad);
1812 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1815 if (aggclass->clip) {
1816 aggclass->clip (self, aggpad, buffer, &actual_buf);
1819 SRC_STREAM_LOCK (self);
1821 if (aggpad->priv->buffer)
1822 gst_buffer_unref (aggpad->priv->buffer);
1823 aggpad->priv->buffer = actual_buf;
1824 PAD_UNLOCK (aggpad);
1825 PAD_STREAM_UNLOCK (aggpad);
1827 SRC_STREAM_BROADCAST (self);
1828 SRC_STREAM_UNLOCK (self);
1830 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1832 GST_OBJECT_LOCK (self);
1833 flow_return = priv->flow_return;
1834 GST_OBJECT_UNLOCK (self);
1839 PAD_STREAM_UNLOCK (aggpad);
1841 gst_buffer_unref (buffer);
1842 GST_DEBUG_OBJECT (aggpad, "We are flushing");
1844 return GST_FLOW_FLUSHING;
1847 PAD_UNLOCK (aggpad);
1848 PAD_STREAM_UNLOCK (aggpad);
1850 gst_buffer_unref (buffer);
1851 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1853 return GST_FLOW_EOS;
1857 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
1860 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1861 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1863 if (GST_QUERY_IS_SERIALIZED (query)) {
1866 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
1867 PAD_UNLOCK (aggpad);
1871 while (aggpad->priv->buffer
1872 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1873 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1874 PAD_WAIT_EVENT (aggpad);
1876 PAD_UNLOCK (aggpad);
1878 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1882 return klass->sink_query (GST_AGGREGATOR (parent),
1883 GST_AGGREGATOR_PAD (pad), query);
1886 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping query");
1891 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
1894 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1895 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1897 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1898 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1901 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1902 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
1903 PAD_UNLOCK (aggpad);
1907 while (aggpad->priv->buffer
1908 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1909 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1910 PAD_WAIT_EVENT (aggpad);
1912 PAD_UNLOCK (aggpad);
1914 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1915 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1919 return klass->sink_event (GST_AGGREGATOR (parent),
1920 GST_AGGREGATOR_PAD (pad), event);
1923 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping event");
1924 if (GST_EVENT_IS_STICKY (event))
1925 gst_pad_store_sticky_event (pad, event);
1926 gst_event_unref (event);
1931 gst_aggregator_pad_activate_mode_func (GstPad * pad,
1932 GstObject * parent, GstPadMode mode, gboolean active)
1934 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1936 if (active == FALSE) {
1938 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1939 gst_buffer_replace (&aggpad->priv->buffer, NULL);
1940 PAD_BROADCAST_EVENT (aggpad);
1941 PAD_UNLOCK (aggpad);
1944 g_atomic_int_set (&aggpad->priv->flushing, FALSE);
1945 PAD_BROADCAST_EVENT (aggpad);
1946 PAD_UNLOCK (aggpad);
1952 /***********************************
1953 * GstAggregatorPad implementation *
1954 ************************************/
1955 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1958 gst_aggregator_pad_constructed (GObject * object)
1960 GstPad *pad = GST_PAD (object);
1962 gst_pad_set_chain_function (pad,
1963 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
1964 gst_pad_set_event_function (pad,
1965 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
1966 gst_pad_set_query_function (pad,
1967 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
1968 gst_pad_set_activatemode_function (pad,
1969 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
1973 gst_aggregator_pad_finalize (GObject * object)
1975 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1977 g_cond_clear (&pad->priv->event_cond);
1978 g_mutex_clear (&pad->priv->stream_lock);
1979 g_mutex_clear (&pad->priv->lock);
1981 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
1985 gst_aggregator_pad_dispose (GObject * object)
1987 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1990 buf = gst_aggregator_pad_steal_buffer (pad);
1992 gst_buffer_unref (buf);
1994 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
1998 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2000 GObjectClass *gobject_class = (GObjectClass *) klass;
2002 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2004 gobject_class->constructed = gst_aggregator_pad_constructed;
2005 gobject_class->finalize = gst_aggregator_pad_finalize;
2006 gobject_class->dispose = gst_aggregator_pad_dispose;
2010 gst_aggregator_pad_init (GstAggregatorPad * pad)
2013 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2014 GstAggregatorPadPrivate);
2016 pad->priv->buffer = NULL;
2017 g_cond_init (&pad->priv->event_cond);
2019 g_mutex_init (&pad->priv->stream_lock);
2020 g_mutex_init (&pad->priv->lock);
2024 * gst_aggregator_pad_steal_buffer:
2025 * @pad: the pad to get buffer from
2027 * Steal the ref to the buffer currently queued in @pad.
2029 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2030 * queued. You should unref the buffer after usage.
2033 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2035 GstBuffer *buffer = NULL;
2038 if (pad->priv->buffer) {
2039 GST_TRACE_OBJECT (pad, "Consuming buffer");
2040 buffer = pad->priv->buffer;
2041 pad->priv->buffer = NULL;
2042 if (pad->priv->pending_eos) {
2043 pad->priv->pending_eos = FALSE;
2044 pad->priv->eos = TRUE;
2046 PAD_BROADCAST_EVENT (pad);
2047 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2055 * gst_aggregator_pad_get_buffer:
2056 * @pad: the pad to get buffer from
2058 * Returns: (transfer full): A reference to the buffer in @pad or
2059 * NULL if no buffer was queued. You should unref the buffer after
2063 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2065 GstBuffer *buffer = NULL;
2068 if (pad->priv->buffer)
2069 buffer = gst_buffer_ref (pad->priv->buffer);
2076 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2081 is_eos = pad->priv->eos;
2088 * gst_aggregator_merge_tags:
2089 * @self: a #GstAggregator
2090 * @tags: a #GstTagList to merge
2091 * @mode: the #GstTagMergeMode to use
2093 * Adds tags to so-called pending tags, which will be processed
2094 * before pushing out data downstream.
2096 * Note that this is provided for convenience, and the subclass is
2097 * not required to use this and can still do tag handling on its own.
2102 gst_aggregator_merge_tags (GstAggregator * self,
2103 const GstTagList * tags, GstTagMergeMode mode)
2107 g_return_if_fail (GST_IS_AGGREGATOR (self));
2108 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2110 /* FIXME Check if we can use OBJECT lock here! */
2111 GST_OBJECT_LOCK (self);
2113 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2114 otags = self->priv->tags;
2115 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2117 gst_tag_list_unref (otags);
2118 self->priv->tags_changed = TRUE;
2119 GST_OBJECT_UNLOCK (self);
2123 * gst_aggregator_set_latency:
2124 * @self: a #GstAggregator
2125 * @min_latency: minimum latency
2126 * @max_latency: maximum latency
2128 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2129 * latency is. Will also post a LATENCY message on the bus so the pipeline
2130 * can reconfigure its global latency.
2133 gst_aggregator_set_latency (GstAggregator * self,
2134 GstClockTime min_latency, GstClockTime max_latency)
2136 g_return_if_fail (GST_IS_AGGREGATOR (self));
2137 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2138 g_return_if_fail (max_latency >= min_latency);
2140 GST_OBJECT_LOCK (self);
2141 self->priv->sub_latency_min = min_latency;
2142 self->priv->sub_latency_max = max_latency;
2143 GST_OBJECT_UNLOCK (self);
2145 gst_element_post_message (GST_ELEMENT_CAST (self),
2146 gst_message_new_latency (GST_OBJECT_CAST (self)));