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)); \
113 #define GST_AGGREGATOR_SETCAPS_LOCK(self) G_STMT_START { \
114 GST_TRACE_OBJECT (self, "Taking SETCAPS lock from thread %p", \
116 g_mutex_lock(&self->priv->setcaps_lock); \
117 GST_TRACE_OBJECT (self, "Took SETCAPS lock from thread %p", \
121 #define GST_AGGREGATOR_SETCAPS_UNLOCK(self) G_STMT_START { \
122 GST_TRACE_OBJECT (self, "Releasing SETCAPS lock from thread %p", \
124 g_mutex_unlock(&self->priv->setcaps_lock); \
125 GST_TRACE_OBJECT (self, "Took SETCAPS lock from thread %p", \
129 #define PAD_STREAM_LOCK(pad) G_STMT_START { \
130 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
132 g_mutex_lock(&pad->priv->stream_lock); \
133 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
137 #define PAD_STREAM_UNLOCK(pad) G_STMT_START { \
138 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
140 g_mutex_unlock(&pad->priv->stream_lock); \
141 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
145 #define SRC_STREAM_LOCK(self) G_STMT_START { \
146 GST_TRACE_OBJECT (self, "Taking src STREAM lock from thread %p", \
148 g_mutex_lock(&self->priv->src_lock); \
149 GST_TRACE_OBJECT (self, "Took src STREAM lock from thread %p", \
153 #define SRC_STREAM_UNLOCK(self) G_STMT_START { \
154 GST_TRACE_OBJECT (self, "Releasing src STREAM lock from thread %p", \
156 g_mutex_unlock(&self->priv->src_lock); \
157 GST_TRACE_OBJECT (self, "Released src STREAM lock from thread %p", \
161 #define SRC_STREAM_WAIT(self) G_STMT_START { \
162 GST_LOG_OBJECT (self, "Waiting for src STREAM on thread %p", \
164 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
165 GST_LOG_OBJECT (self, "DONE Waiting for src STREAM on thread %p", \
169 #define SRC_STREAM_BROADCAST(self) G_STMT_START { \
170 GST_LOG_OBJECT (self, "Signaling src STREAM from thread %p", \
172 if (self->priv->aggregate_id) \
173 gst_clock_id_unschedule (self->priv->aggregate_id); \
174 g_cond_broadcast(&(self->priv->src_cond)); \
177 struct _GstAggregatorPadPrivate
179 gboolean pending_flush_start;
180 gboolean pending_flush_stop;
181 gboolean pending_eos;
190 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
192 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
196 aggpad->priv->flushing = FALSE;
200 return klass->flush (aggpad, agg);
205 /*************************************
206 * GstAggregator implementation *
207 *************************************/
208 static GstElementClass *aggregator_parent_class = NULL;
210 struct _GstAggregatorPrivate
214 /* Our state is >= PAUSED */
215 gboolean running; /* protected by SRC_STREAM_LOCK */
218 gboolean send_stream_start;
219 gboolean send_segment;
220 gboolean flush_seeking;
221 gboolean pending_flush_start;
223 GstFlowReturn flow_return;
225 gboolean send_stream_start; /* protected by srcpad stream lock */
226 gboolean send_segment; /* protected by object lock */
227 gboolean flush_seeking; /* protected by object lock */
228 gboolean pending_flush_start; /* protected by object lock */
229 gboolean send_eos; /* protected by srcpad stream lock */
230 GstFlowReturn flow_return; /* protected by object lock */
234 /* protected by object lock */
236 gboolean tags_changed;
238 /* Lock to prevent two src setcaps from happening at the same time */
241 gboolean latency_live;
242 GstClockTime latency_min;
243 GstClockTime latency_max;
245 GstClockTime sub_latency_min;
246 GstClockTime sub_latency_max;
249 GstClockID aggregate_id;
263 gboolean one_actually_seeked;
266 #define DEFAULT_LATENCY 0
276 * gst_aggregator_iterate_sinkpads:
277 * @self: The #GstAggregator
278 * @func: (scope call): The function to call.
279 * @user_data: (closure): The data to pass to @func.
281 * Iterate the sinkpads of aggregator to call a function on them.
283 * This method guarantees that @func will be called only once for each
287 gst_aggregator_iterate_sinkpads (GstAggregator * self,
288 GstAggregatorPadForeachFunc func, gpointer user_data)
290 gboolean result = FALSE;
292 gboolean done = FALSE;
293 GValue item = { 0, };
294 GList *seen_pads = NULL;
296 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
302 switch (gst_iterator_next (iter, &item)) {
303 case GST_ITERATOR_OK:
305 GstAggregatorPad *pad;
307 pad = g_value_get_object (&item);
309 /* if already pushed, skip. FIXME, find something faster to tag pads */
310 if (pad == NULL || g_list_find (seen_pads, pad)) {
311 g_value_reset (&item);
315 GST_LOG_OBJECT (pad, "calling function %s on pad",
316 GST_DEBUG_FUNCPTR_NAME (func));
318 result = func (self, pad, user_data);
322 seen_pads = g_list_prepend (seen_pads, pad);
324 g_value_reset (&item);
327 case GST_ITERATOR_RESYNC:
328 gst_iterator_resync (iter);
330 case GST_ITERATOR_ERROR:
331 GST_ERROR_OBJECT (self,
332 "Could not iterate over internally linked pads");
335 case GST_ITERATOR_DONE:
340 g_value_unset (&item);
341 gst_iterator_free (iter);
343 if (seen_pads == NULL) {
344 GST_DEBUG_OBJECT (self, "No pad seen");
348 g_list_free (seen_pads);
355 gst_aggregator_check_pads_ready (GstAggregator * self)
357 GstAggregatorPad *pad;
360 GST_LOG_OBJECT (self, "checking pads");
362 GST_OBJECT_LOCK (self);
364 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
365 if (sinkpads == NULL)
368 for (l = sinkpads; l != NULL; l = l->next) {
372 if (pad->buffer == NULL && !pad->eos) {
373 GST_OBJECT_UNLOCK (pad);
380 GST_OBJECT_UNLOCK (self);
381 GST_LOG_OBJECT (self, "pads are ready");
386 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
387 GST_OBJECT_UNLOCK (self);
392 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
393 GST_OBJECT_UNLOCK (self);
399 gst_aggregator_reset_flow_values (GstAggregator * self)
401 GST_OBJECT_LOCK (self);
402 self->priv->flow_return = GST_FLOW_FLUSHING;
403 self->priv->send_stream_start = TRUE;
404 self->priv->send_segment = TRUE;
405 gst_segment_init (&self->segment, GST_FORMAT_TIME);
406 GST_OBJECT_UNLOCK (self);
410 gst_aggregator_push_mandatory_events (GstAggregator * self)
412 GstAggregatorPrivate *priv = self->priv;
413 GstEvent *segment = NULL;
414 GstEvent *tags = NULL;
416 if (self->priv->send_stream_start) {
419 GST_INFO_OBJECT (self, "pushing stream start");
420 /* stream-start (FIXME: create id based on input ids) */
421 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
422 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
423 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
425 self->priv->send_stream_start = FALSE;
428 if (self->priv->srccaps) {
430 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
431 self->priv->srccaps);
432 if (!gst_pad_push_event (self->srcpad,
433 gst_event_new_caps (self->priv->srccaps))) {
434 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
436 gst_caps_unref (self->priv->srccaps);
437 self->priv->srccaps = NULL;
440 GST_OBJECT_LOCK (self);
441 if (self->priv->send_segment && !self->priv->flush_seeking) {
442 segment = gst_event_new_segment (&self->segment);
444 if (!self->priv->seqnum)
445 self->priv->seqnum = gst_event_get_seqnum (segment);
447 gst_event_set_seqnum (segment, self->priv->seqnum);
448 self->priv->send_segment = FALSE;
450 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
453 if (priv->tags && priv->tags_changed) {
454 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
455 priv->tags_changed = FALSE;
457 GST_OBJECT_UNLOCK (self);
460 gst_pad_push_event (self->srcpad, segment);
462 gst_pad_push_event (self->srcpad, tags);
467 * gst_aggregator_set_src_caps:
468 * @self: The #GstAggregator
469 * @caps: The #GstCaps to set on the src pad.
471 * Sets the caps to be used on the src pad.
474 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
476 GST_AGGREGATOR_SETCAPS_LOCK (self);
477 gst_caps_replace (&self->priv->srccaps, caps);
478 gst_aggregator_push_mandatory_events (self);
479 GST_AGGREGATOR_SETCAPS_UNLOCK (self);
483 * gst_aggregator_finish_buffer:
484 * @self: The #GstAggregator
485 * @buffer: (transfer full): the #GstBuffer to push.
487 * This method will push the provided output buffer downstream. If needed,
488 * mandatory events such as stream-start, caps, and segment events will be
489 * sent before pushing the buffer.
492 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
494 gst_aggregator_push_mandatory_events (self);
496 GST_OBJECT_LOCK (self);
497 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
498 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
499 GST_OBJECT_UNLOCK (self);
500 return gst_pad_push (self->srcpad, buffer);
502 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
503 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
504 GST_OBJECT_UNLOCK (self);
505 gst_buffer_unref (buffer);
511 gst_aggregator_push_eos (GstAggregator * self)
514 gst_aggregator_push_mandatory_events (self);
516 self->priv->send_eos = FALSE;
517 event = gst_event_new_eos ();
518 gst_event_set_seqnum (event, self->priv->seqnum);
519 gst_pad_push_event (self->srcpad, event);
523 gst_aggregator_get_next_time (GstAggregator * self)
525 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
527 if (klass->get_next_time)
528 return klass->get_next_time (self);
530 return GST_CLOCK_TIME_NONE;
533 /* called with the src STREAM lock */
535 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
537 GstClockTime latency_max, latency_min;
543 SRC_STREAM_LOCK (self);
545 gst_aggregator_get_latency (self, &live, &latency_min, &latency_max);
547 if (gst_aggregator_check_pads_ready (self)) {
548 GST_DEBUG_OBJECT (self, "all pads have data");
549 SRC_STREAM_UNLOCK (self);
554 /* Before waiting, check if we're actually still running */
555 if (!self->priv->running || !self->priv->send_eos) {
556 SRC_STREAM_UNLOCK (self);
561 start = gst_aggregator_get_next_time (self);
563 if (!live || !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self))
564 || !GST_CLOCK_TIME_IS_VALID (start)) {
565 /* We wake up here when something happened, and below
566 * then check if we're ready now. If we return FALSE,
567 * we will be directly called again.
569 SRC_STREAM_WAIT (self);
571 GstClockTime base_time, time;
573 GstClockReturn status;
574 GstClockTimeDiff jitter;
576 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
577 GST_TIME_ARGS (start));
579 GST_OBJECT_LOCK (self);
580 base_time = GST_ELEMENT_CAST (self)->base_time;
581 clock = GST_ELEMENT_CLOCK (self);
583 gst_object_ref (clock);
584 GST_OBJECT_UNLOCK (self);
586 time = base_time + start;
588 if (GST_CLOCK_TIME_IS_VALID (latency_min)) {
591 time += self->priv->latency;
594 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
595 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
596 " latency max %" GST_TIME_FORMAT " latency min %" GST_TIME_FORMAT
597 " current %" GST_TIME_FORMAT ")", GST_TIME_ARGS (time),
598 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
599 GST_TIME_ARGS (start), GST_TIME_ARGS (latency_max),
600 GST_TIME_ARGS (latency_min),
601 GST_TIME_ARGS (gst_clock_get_time (clock)));
603 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
604 gst_object_unref (clock);
605 SRC_STREAM_UNLOCK (self);
608 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
610 SRC_STREAM_LOCK (self);
611 if (self->priv->aggregate_id) {
612 gst_clock_id_unref (self->priv->aggregate_id);
613 self->priv->aggregate_id = NULL;
616 GST_DEBUG_OBJECT (self,
617 "clock returned %d (jitter: %s%" GST_TIME_FORMAT ")",
618 status, (jitter < 0 ? "-" : " "),
619 GST_TIME_ARGS ((jitter < 0 ? -jitter : jitter)));
622 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
623 SRC_STREAM_UNLOCK (self);
629 res = gst_aggregator_check_pads_ready (self);
630 SRC_STREAM_UNLOCK (self);
636 gst_aggregator_aggregate_func (GstAggregator * self)
638 GstAggregatorPrivate *priv = self->priv;
639 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
640 gboolean timeout = FALSE;
642 if (self->priv->running == FALSE) {
643 GST_DEBUG_OBJECT (self, "Not running anymore");
647 GST_LOG_OBJECT (self, "Checking aggregate");
648 while (priv->send_eos && priv->running) {
649 GstFlowReturn flow_return;
651 if (!gst_aggregator_wait_and_check (self, &timeout))
654 GST_TRACE_OBJECT (self, "Actually aggregating!");
656 flow_return = klass->aggregate (self, timeout);
658 GST_OBJECT_LOCK (self);
659 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking)
660 priv->flow_return = GST_FLOW_OK;
662 priv->flow_return = flow_return;
663 GST_OBJECT_UNLOCK (self);
665 if (flow_return == GST_FLOW_EOS) {
666 gst_aggregator_push_eos (self);
669 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
671 if (flow_return != GST_FLOW_OK)
677 gst_aggregator_start (GstAggregator * self)
679 GstAggregatorClass *klass;
682 self->priv->running = TRUE;
683 self->priv->send_stream_start = TRUE;
684 self->priv->send_segment = TRUE;
685 self->priv->send_eos = TRUE;
686 self->priv->srccaps = NULL;
687 self->priv->flow_return = GST_FLOW_OK;
689 klass = GST_AGGREGATOR_GET_CLASS (self);
692 result = klass->start (self);
700 _check_pending_flush_stop (GstAggregatorPad * pad)
702 return (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
706 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
710 GST_INFO_OBJECT (self, "%s srcpad task",
711 flush_start ? "Pausing" : "Stopping");
713 SRC_STREAM_LOCK (self);
714 self->priv->running = FALSE;
715 SRC_STREAM_BROADCAST (self);
716 SRC_STREAM_UNLOCK (self);
719 res = gst_pad_push_event (self->srcpad, flush_start);
722 gst_pad_stop_task (self->srcpad);
728 gst_aggregator_start_srcpad_task (GstAggregator * self)
730 GST_INFO_OBJECT (self, "Starting srcpad task");
732 self->priv->running = TRUE;
733 gst_pad_start_task (GST_PAD (self->srcpad),
734 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
738 gst_aggregator_flush (GstAggregator * self)
740 GstFlowReturn ret = GST_FLOW_OK;
741 GstAggregatorPrivate *priv = self->priv;
742 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
744 GST_DEBUG_OBJECT (self, "Flushing everything");
745 GST_OBJECT_LOCK (self);
746 priv->send_segment = TRUE;
747 priv->flush_seeking = FALSE;
748 priv->tags_changed = FALSE;
749 GST_OBJECT_UNLOCK (self);
751 ret = klass->flush (self);
757 /* Called with GstAggregator's object lock held */
760 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
763 GstAggregatorPad *tmppad;
765 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
766 tmppad = (GstAggregatorPad *) tmp->data;
768 if (_check_pending_flush_stop (tmppad) == FALSE) {
769 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
770 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
779 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
783 GstAggregatorPrivate *priv = self->priv;
784 GstAggregatorPadPrivate *padpriv = aggpad->priv;
786 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
787 /* Remove pad buffer and wake up the streaming thread */
788 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
789 gst_buffer_replace (&tmpbuf, NULL);
790 PAD_STREAM_LOCK (aggpad);
791 if (g_atomic_int_compare_and_exchange (&padpriv->pending_flush_start,
792 TRUE, FALSE) == TRUE) {
793 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
794 g_atomic_int_set (&padpriv->pending_flush_stop, TRUE);
797 GST_OBJECT_LOCK (self);
798 if (priv->flush_seeking) {
799 /* If flush_seeking we forward the first FLUSH_START */
800 if (priv->pending_flush_start) {
801 priv->pending_flush_start = FALSE;
802 GST_OBJECT_UNLOCK (self);
804 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
805 gst_aggregator_stop_srcpad_task (self, event);
806 priv->flow_return = GST_FLOW_OK;
808 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
809 GST_PAD_STREAM_LOCK (self->srcpad);
810 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
813 GST_OBJECT_UNLOCK (self);
814 gst_event_unref (event);
817 GST_OBJECT_UNLOCK (self);
818 gst_event_unref (event);
820 PAD_STREAM_UNLOCK (aggpad);
822 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
823 gst_buffer_replace (&tmpbuf, NULL);
826 /* GstAggregator vmethods default implementations */
828 gst_aggregator_default_sink_event (GstAggregator * self,
829 GstAggregatorPad * aggpad, GstEvent * event)
832 GstPad *pad = GST_PAD (aggpad);
833 GstAggregatorPrivate *priv = self->priv;
835 switch (GST_EVENT_TYPE (event)) {
836 case GST_EVENT_FLUSH_START:
838 gst_aggregator_flush_start (self, aggpad, event);
839 /* We forward only in one case: right after flush_seeking */
843 case GST_EVENT_FLUSH_STOP:
845 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
847 gst_aggregator_pad_flush (aggpad, self);
848 GST_OBJECT_LOCK (self);
849 if (priv->flush_seeking) {
850 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
851 if (gst_aggregator_all_flush_stop_received_locked (self)) {
852 GST_OBJECT_UNLOCK (self);
853 /* That means we received FLUSH_STOP/FLUSH_STOP on
854 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
855 gst_aggregator_flush (self);
856 gst_pad_push_event (self->srcpad, event);
858 SRC_STREAM_LOCK (self);
859 priv->send_eos = TRUE;
860 SRC_STREAM_BROADCAST (self);
861 SRC_STREAM_UNLOCK (self);
863 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
864 GST_PAD_STREAM_UNLOCK (self->srcpad);
865 gst_aggregator_start_srcpad_task (self);
867 GST_OBJECT_UNLOCK (self);
870 GST_OBJECT_UNLOCK (self);
873 /* We never forward the event */
878 GST_DEBUG_OBJECT (aggpad, "EOS");
880 /* We still have a buffer, and we don't want the subclass to have to
881 * check for it. Mark pending_eos, eos will be set when steal_buffer is
884 SRC_STREAM_LOCK (self);
886 if (!aggpad->buffer) {
889 aggpad->priv->pending_eos = TRUE;
893 SRC_STREAM_BROADCAST (self);
894 SRC_STREAM_UNLOCK (self);
897 case GST_EVENT_SEGMENT:
900 gst_event_copy_segment (event, &aggpad->segment);
901 self->priv->seqnum = gst_event_get_seqnum (event);
905 case GST_EVENT_STREAM_START:
913 gst_event_parse_tag (event, &tags);
915 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
916 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
917 gst_event_unref (event);
929 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
930 return gst_pad_event_default (pad, GST_OBJECT (self), event);
933 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
935 gst_event_unref (event);
940 static inline gboolean
941 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
942 gpointer unused_udata)
944 gst_aggregator_pad_flush (pad, self);
950 gst_aggregator_stop (GstAggregator * agg)
952 GstAggregatorClass *klass;
955 gst_aggregator_reset_flow_values (agg);
957 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
959 klass = GST_AGGREGATOR_GET_CLASS (agg);
962 result = klass->stop (agg);
967 gst_tag_list_unref (agg->priv->tags);
968 agg->priv->tags = NULL;
973 /* GstElement vmethods implementations */
974 static GstStateChangeReturn
975 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
977 GstStateChangeReturn ret;
978 GstAggregator *self = GST_AGGREGATOR (element);
980 switch (transition) {
981 case GST_STATE_CHANGE_READY_TO_PAUSED:
982 if (!gst_aggregator_start (self))
990 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
991 transition)) == GST_STATE_CHANGE_FAILURE)
995 switch (transition) {
996 case GST_STATE_CHANGE_PAUSED_TO_READY:
997 if (!gst_aggregator_stop (self)) {
998 /* What to do in this case? Error out? */
999 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1011 GST_ERROR_OBJECT (element, "parent failed state change");
1016 GST_ERROR_OBJECT (element, "Subclass failed to start");
1017 return GST_STATE_CHANGE_FAILURE;
1022 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1024 GstAggregator *self = GST_AGGREGATOR (element);
1027 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1029 GST_INFO_OBJECT (pad, "Removing pad");
1031 SRC_STREAM_LOCK (self);
1032 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1033 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
1034 gst_buffer_replace (&tmpbuf, NULL);
1035 gst_element_remove_pad (element, pad);
1037 SRC_STREAM_BROADCAST (self);
1038 SRC_STREAM_UNLOCK (self);
1042 gst_aggregator_request_new_pad (GstElement * element,
1043 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1045 GstAggregator *self;
1046 GstAggregatorPad *agg_pad;
1048 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1049 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1051 self = GST_AGGREGATOR (element);
1053 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1057 GST_OBJECT_LOCK (element);
1058 if (req_name == NULL || strlen (req_name) < 6
1059 || !g_str_has_prefix (req_name, "sink_")) {
1060 /* no name given when requesting the pad, use next available int */
1063 /* parse serial number from requested padname */
1064 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1065 if (serial >= priv->padcount)
1066 priv->padcount = serial;
1069 name = g_strdup_printf ("sink_%u", priv->padcount);
1070 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1071 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1074 GST_OBJECT_UNLOCK (element);
1080 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1083 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1085 /* add the pad to the element */
1086 gst_element_add_pad (element, GST_PAD (agg_pad));
1088 return GST_PAD (agg_pad);
1093 GstClockTime min, max;
1098 gst_aggregator_query_sink_latency_foreach (GstAggregator * self,
1099 GstAggregatorPad * pad, gpointer user_data)
1101 LatencyData *data = user_data;
1102 GstClockTime min, max;
1106 query = gst_query_new_latency ();
1107 res = gst_pad_peer_query (GST_PAD_CAST (pad), query);
1110 gst_query_parse_latency (query, &live, &min, &max);
1112 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
1113 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1115 if (min != GST_CLOCK_TIME_NONE && min > data->min)
1118 if (max != GST_CLOCK_TIME_NONE &&
1119 ((data->max != GST_CLOCK_TIME_NONE && max < data->max) ||
1120 (data->max == GST_CLOCK_TIME_NONE)))
1126 gst_query_unref (query);
1132 * gst_aggregator_get_latency:
1133 * @self: a #GstAggregator
1134 * @live: (out) (allow-none): whether @self is live
1135 * @min_latency: (out) (allow-none): the configured minimum latency of @self
1136 * @max_latency: (out) (allow-none): the configured maximum latency of @self
1138 * Retreives the latency values reported by @self in response to the latency
1141 * Typically only called by subclasses.
1144 gst_aggregator_get_latency (GstAggregator * self, gboolean * live,
1145 GstClockTime * min_latency, GstClockTime * max_latency)
1147 GstClockTime our_latency;
1148 GstClockTime min, max;
1150 g_return_if_fail (GST_IS_AGGREGATOR (self));
1152 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1153 min = self->priv->latency_min;
1154 max = self->priv->latency_max;
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;
1161 our_latency = self->priv->latency;
1162 if (GST_CLOCK_TIME_IS_VALID (our_latency)) {
1164 if (GST_CLOCK_TIME_IS_VALID (max))
1169 *live = self->priv->latency_live;
1177 gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
1179 GstClockTime our_latency;
1183 data.max = GST_CLOCK_TIME_NONE;
1186 /* query upstream's latency */
1187 SRC_STREAM_LOCK (self);
1188 gst_aggregator_iterate_sinkpads (self,
1189 gst_aggregator_query_sink_latency_foreach, &data);
1190 SRC_STREAM_UNLOCK (self);
1192 our_latency = self->priv->latency;
1194 if (data.live && GST_CLOCK_TIME_IS_VALID (our_latency) &&
1195 our_latency > data.max) {
1196 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1197 ("%s", "Latency too big"),
1198 ("The requested latency value is too big for the current pipeline. "
1199 "Limiting to %" G_GINT64_FORMAT, data.max));
1200 self->priv->latency = data.max;
1201 /* FIXME: shouldn't we g_object_notify() the change here? */
1204 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (data.min))) {
1205 GST_WARNING_OBJECT (self, "Invalid minimum latency, using 0");
1209 if (G_UNLIKELY (data.min > data.max)) {
1210 GST_WARNING_OBJECT (self, "Minimum latency is greater than maximum latency "
1211 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT "). "
1212 "Clamping it at the maximum latency", data.min, data.max);
1213 data.min = data.max;
1216 self->priv->latency_live = data.live;
1217 self->priv->latency_min = data.min;
1218 self->priv->latency_max = data.max;
1221 if (GST_CLOCK_TIME_IS_VALID (our_latency)) {
1222 if (GST_CLOCK_TIME_IS_VALID (data.min))
1223 data.min += our_latency;
1224 if (GST_CLOCK_TIME_IS_VALID (data.max))
1225 data.max += our_latency;
1228 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_min)
1229 && GST_CLOCK_TIME_IS_VALID (data.min))
1230 data.min += self->priv->sub_latency_min;
1231 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1232 && GST_CLOCK_TIME_IS_VALID (data.max))
1233 data.max += self->priv->sub_latency_max;
1235 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1236 " max:%" G_GINT64_FORMAT, data.live ? "true" : "false", data.min,
1239 gst_query_set_latency (query, data.live, data.min, data.max);
1245 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1247 GstAggregator *self = GST_AGGREGATOR (element);
1249 GST_STATE_LOCK (element);
1250 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1251 GST_STATE (element) < GST_STATE_PAUSED) {
1255 GstSeekType start_type, stop_type;
1258 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1259 &start, &stop_type, &stop);
1260 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1261 stop_type, stop, NULL);
1263 self->priv->seqnum = gst_event_get_seqnum (event);
1264 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1266 GST_STATE_UNLOCK (element);
1269 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1274 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1276 gboolean res = TRUE;
1278 switch (GST_QUERY_TYPE (query)) {
1279 case GST_QUERY_SEEKING:
1283 /* don't pass it along as some (file)sink might claim it does
1284 * whereas with a collectpads in between that will not likely work */
1285 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1286 gst_query_set_seeking (query, format, FALSE, 0, -1);
1291 case GST_QUERY_LATENCY:
1295 ret = gst_aggregator_query_latency (self, query);
1296 /* Wake up the src thread again, due to changed latencies
1297 * or changed live-ness we might have to adjust if we wait
1298 * on a deadline at all and how long.
1299 * This is only to unschedule the clock id, we don't really care
1300 * about the GCond here.
1302 SRC_STREAM_LOCK (self);
1303 SRC_STREAM_BROADCAST (self);
1304 SRC_STREAM_UNLOCK (self);
1311 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1318 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1320 EventData *evdata = user_data;
1321 gboolean ret = TRUE;
1322 GstPad *peer = gst_pad_get_peer (pad);
1323 GstAggregatorPadPrivate *padpriv = GST_AGGREGATOR_PAD (pad)->priv;
1326 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1327 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1328 gst_object_unref (peer);
1332 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1333 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1335 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1336 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1338 if (gst_pad_query (peer, seeking)) {
1341 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1343 if (seekable == FALSE) {
1344 GST_INFO_OBJECT (pad,
1345 "Source not seekable, We failed but it does not matter!");
1350 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1353 gst_query_unref (seeking);
1356 if (evdata->flush) {
1357 padpriv->pending_flush_start = FALSE;
1358 padpriv->pending_flush_stop = FALSE;
1361 evdata->one_actually_seeked = TRUE;
1364 evdata->result &= ret;
1366 /* Always send to all pads */
1371 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1372 GstEvent * event, gboolean flush)
1376 evdata.event = event;
1377 evdata.result = TRUE;
1378 evdata.flush = flush;
1379 evdata.one_actually_seeked = FALSE;
1381 /* We first need to set all pads as flushing in a first pass
1382 * as flush_start flush_stop is sometimes sent synchronously
1383 * while we send the seek event */
1387 GST_OBJECT_LOCK (self);
1388 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1389 GstAggregatorPad *pad = l->data;
1391 pad->priv->pending_flush_start = TRUE;
1392 pad->priv->pending_flush_stop = FALSE;
1394 GST_OBJECT_UNLOCK (self);
1397 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1399 gst_event_unref (event);
1405 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1410 GstSeekType start_type, stop_type;
1414 GstAggregatorPrivate *priv = self->priv;
1416 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1417 &start, &stop_type, &stop);
1419 GST_INFO_OBJECT (self, "starting SEEK");
1421 flush = flags & GST_SEEK_FLAG_FLUSH;
1423 GST_OBJECT_LOCK (self);
1425 priv->pending_flush_start = TRUE;
1426 priv->flush_seeking = TRUE;
1429 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1430 stop_type, stop, NULL);
1431 GST_OBJECT_UNLOCK (self);
1433 /* forward the seek upstream */
1434 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, flush);
1437 if (!evdata.result || !evdata.one_actually_seeked) {
1438 GST_OBJECT_LOCK (self);
1439 priv->flush_seeking = FALSE;
1440 priv->pending_flush_start = FALSE;
1441 GST_OBJECT_UNLOCK (self);
1444 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1446 return evdata.result;
1450 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1453 gboolean res = TRUE;
1455 switch (GST_EVENT_TYPE (event)) {
1456 case GST_EVENT_SEEK:
1458 gst_event_ref (event);
1459 res = gst_aggregator_do_seek (self, event);
1460 gst_event_unref (event);
1464 case GST_EVENT_NAVIGATION:
1466 /* navigation is rather pointless. */
1468 gst_event_unref (event);
1477 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE);
1478 res = evdata.result;
1485 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1488 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1490 return klass->src_event (GST_AGGREGATOR (parent), event);
1494 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1497 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1499 return klass->src_query (GST_AGGREGATOR (parent), query);
1503 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1504 GstObject * parent, GstPadMode mode, gboolean active)
1506 GstAggregator *self = GST_AGGREGATOR (parent);
1507 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1509 if (klass->src_activate) {
1510 if (klass->src_activate (self, mode, active) == FALSE) {
1515 if (active == TRUE) {
1517 case GST_PAD_MODE_PUSH:
1519 GST_INFO_OBJECT (pad, "Activating pad!");
1520 gst_aggregator_start_srcpad_task (self);
1525 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1532 GST_INFO_OBJECT (self, "Deactivating srcpad");
1533 gst_aggregator_stop_srcpad_task (self, FALSE);
1539 gst_aggregator_default_sink_query (GstAggregator * self,
1540 GstAggregatorPad * aggpad, GstQuery * query)
1542 GstPad *pad = GST_PAD (aggpad);
1544 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1548 gst_aggregator_finalize (GObject * object)
1550 GstAggregator *self = (GstAggregator *) object;
1552 g_mutex_clear (&self->priv->setcaps_lock);
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)
1573 g_return_if_fail (GST_IS_AGGREGATOR (self));
1575 GST_OBJECT_LOCK (self);
1577 if (self->priv->latency_live && self->priv->latency_max != 0 &&
1578 GST_CLOCK_TIME_IS_VALID (latency) && latency > self->priv->latency_max) {
1579 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1580 ("%s", "Latency too big"),
1581 ("The requested latency value is too big for the latency in the "
1582 "current pipeline. Limiting to %" G_GINT64_FORMAT,
1583 self->priv->latency_max));
1584 latency = self->priv->latency_max;
1587 changed = (self->priv->latency != latency);
1588 self->priv->latency = latency;
1589 GST_OBJECT_UNLOCK (self);
1592 gst_element_post_message (GST_ELEMENT_CAST (self),
1593 gst_message_new_latency (GST_OBJECT_CAST (self)));
1597 * gst_aggregator_get_latency_property:
1598 * @agg: a #GstAggregator
1600 * Gets the latency value. See gst_aggregator_set_latency for
1603 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1604 * before a pad is deemed unresponsive. A value of -1 means an
1608 gst_aggregator_get_latency_property (GstAggregator * agg)
1612 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1614 GST_OBJECT_LOCK (agg);
1615 res = agg->priv->latency;
1616 GST_OBJECT_UNLOCK (agg);
1622 gst_aggregator_set_property (GObject * object, guint prop_id,
1623 const GValue * value, GParamSpec * pspec)
1625 GstAggregator *agg = GST_AGGREGATOR (object);
1629 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1632 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1638 gst_aggregator_get_property (GObject * object, guint prop_id,
1639 GValue * value, GParamSpec * pspec)
1641 GstAggregator *agg = GST_AGGREGATOR (object);
1645 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1648 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1653 /* GObject vmethods implementations */
1655 gst_aggregator_class_init (GstAggregatorClass * klass)
1657 GObjectClass *gobject_class = (GObjectClass *) klass;
1658 GstElementClass *gstelement_class = (GstElementClass *) klass;
1660 aggregator_parent_class = g_type_class_peek_parent (klass);
1661 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1663 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1664 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1666 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1668 klass->sink_event = gst_aggregator_default_sink_event;
1669 klass->sink_query = gst_aggregator_default_sink_query;
1671 klass->src_event = gst_aggregator_default_src_event;
1672 klass->src_query = gst_aggregator_default_src_query;
1674 gstelement_class->request_new_pad =
1675 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1676 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1677 gstelement_class->release_pad =
1678 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1679 gstelement_class->change_state =
1680 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1682 gobject_class->set_property = gst_aggregator_set_property;
1683 gobject_class->get_property = gst_aggregator_get_property;
1684 gobject_class->finalize = gst_aggregator_finalize;
1686 g_object_class_install_property (gobject_class, PROP_LATENCY,
1687 g_param_spec_int64 ("latency", "Buffer latency",
1688 "Additional latency in live mode to allow upstream "
1689 "to take longer to produce buffers for the current "
1691 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1692 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1694 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1695 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_query_sink_latency_foreach);
1699 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1701 GstPadTemplate *pad_template;
1702 GstAggregatorPrivate *priv;
1704 g_return_if_fail (klass->aggregate != NULL);
1707 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1708 GstAggregatorPrivate);
1713 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1714 g_return_if_fail (pad_template != NULL);
1716 priv->padcount = -1;
1717 priv->tags_changed = FALSE;
1719 self->priv->latency_live = FALSE;
1720 self->priv->latency_min = self->priv->sub_latency_min = 0;
1721 self->priv->latency_max = self->priv->sub_latency_max = GST_CLOCK_TIME_NONE;
1722 gst_aggregator_reset_flow_values (self);
1724 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1726 gst_pad_set_event_function (self->srcpad,
1727 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
1728 gst_pad_set_query_function (self->srcpad,
1729 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
1730 gst_pad_set_activatemode_function (self->srcpad,
1731 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
1733 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1735 self->priv->latency = DEFAULT_LATENCY;
1737 g_mutex_init (&self->priv->setcaps_lock);
1738 g_mutex_init (&self->priv->src_lock);
1739 g_cond_init (&self->priv->src_cond);
1742 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1743 * method to get to the padtemplates */
1745 gst_aggregator_get_type (void)
1747 static volatile gsize type = 0;
1749 if (g_once_init_enter (&type)) {
1751 static const GTypeInfo info = {
1752 sizeof (GstAggregatorClass),
1755 (GClassInitFunc) gst_aggregator_class_init,
1758 sizeof (GstAggregator),
1760 (GInstanceInitFunc) gst_aggregator_init,
1763 _type = g_type_register_static (GST_TYPE_ELEMENT,
1764 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1765 g_once_init_leave (&type, _type);
1770 static GstFlowReturn
1771 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1773 GstBuffer *actual_buf = buffer;
1774 GstAggregator *self = GST_AGGREGATOR (object);
1775 GstAggregatorPrivate *priv = self->priv;
1776 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1777 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1778 GstFlowReturn flow_return;
1780 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1782 PAD_STREAM_LOCK (aggpad);
1784 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1787 if (g_atomic_int_get (&aggpad->priv->pending_eos) == TRUE)
1792 while (aggpad->buffer && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1793 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1794 PAD_WAIT_EVENT (aggpad);
1796 PAD_UNLOCK (aggpad);
1798 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1801 if (aggclass->clip) {
1802 aggclass->clip (self, aggpad, buffer, &actual_buf);
1805 SRC_STREAM_LOCK (self);
1808 gst_buffer_unref (aggpad->buffer);
1809 aggpad->buffer = actual_buf;
1810 PAD_UNLOCK (aggpad);
1811 PAD_STREAM_UNLOCK (aggpad);
1813 if (gst_aggregator_check_pads_ready (self))
1814 SRC_STREAM_BROADCAST (self);
1815 SRC_STREAM_UNLOCK (self);
1817 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1819 GST_OBJECT_LOCK (self);
1820 flow_return = priv->flow_return;
1821 GST_OBJECT_UNLOCK (self);
1826 PAD_STREAM_UNLOCK (aggpad);
1828 gst_buffer_unref (buffer);
1829 GST_DEBUG_OBJECT (aggpad, "We are flushing");
1831 return GST_FLOW_FLUSHING;
1834 PAD_STREAM_UNLOCK (aggpad);
1836 gst_buffer_unref (buffer);
1837 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1839 return GST_FLOW_EOS;
1843 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
1846 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1847 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1849 if (GST_QUERY_IS_SERIALIZED (query)) {
1852 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
1853 PAD_UNLOCK (aggpad);
1857 while (aggpad->buffer
1858 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1859 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1860 PAD_WAIT_EVENT (aggpad);
1862 PAD_UNLOCK (aggpad);
1864 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1868 return klass->sink_query (GST_AGGREGATOR (parent),
1869 GST_AGGREGATOR_PAD (pad), query);
1872 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping query");
1877 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
1880 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1881 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1883 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1884 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1887 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1888 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
1889 PAD_UNLOCK (aggpad);
1893 while (aggpad->buffer
1894 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1895 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1896 PAD_WAIT_EVENT (aggpad);
1898 PAD_UNLOCK (aggpad);
1900 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1901 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1905 return klass->sink_event (GST_AGGREGATOR (parent),
1906 GST_AGGREGATOR_PAD (pad), event);
1909 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping event");
1910 if (GST_EVENT_IS_STICKY (event))
1911 gst_pad_store_sticky_event (pad, event);
1912 gst_event_unref (event);
1917 gst_aggregator_pad_activate_mode_func (GstPad * pad,
1918 GstObject * parent, GstPadMode mode, gboolean active)
1920 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1922 if (active == FALSE) {
1924 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1925 gst_buffer_replace (&aggpad->buffer, NULL);
1926 PAD_BROADCAST_EVENT (aggpad);
1927 PAD_UNLOCK (aggpad);
1930 g_atomic_int_set (&aggpad->priv->flushing, FALSE);
1931 PAD_BROADCAST_EVENT (aggpad);
1932 PAD_UNLOCK (aggpad);
1938 /***********************************
1939 * GstAggregatorPad implementation *
1940 ************************************/
1941 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1944 gst_aggregator_pad_constructed (GObject * object)
1946 GstPad *pad = GST_PAD (object);
1948 gst_pad_set_chain_function (pad,
1949 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
1950 gst_pad_set_event_function (pad,
1951 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
1952 gst_pad_set_query_function (pad,
1953 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
1954 gst_pad_set_activatemode_function (pad,
1955 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
1959 gst_aggregator_pad_finalize (GObject * object)
1961 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1963 g_cond_clear (&pad->priv->event_cond);
1964 g_mutex_clear (&pad->priv->stream_lock);
1966 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
1970 gst_aggregator_pad_dispose (GObject * object)
1972 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1975 buf = gst_aggregator_pad_steal_buffer (pad);
1977 gst_buffer_unref (buf);
1979 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
1983 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
1985 GObjectClass *gobject_class = (GObjectClass *) klass;
1987 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
1989 gobject_class->constructed = gst_aggregator_pad_constructed;
1990 gobject_class->finalize = gst_aggregator_pad_finalize;
1991 gobject_class->dispose = gst_aggregator_pad_dispose;
1995 gst_aggregator_pad_init (GstAggregatorPad * pad)
1998 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
1999 GstAggregatorPadPrivate);
2002 g_cond_init (&pad->priv->event_cond);
2004 g_mutex_init (&pad->priv->stream_lock);
2008 * gst_aggregator_pad_steal_buffer_unlocked:
2009 * @pad: the pad to get buffer from
2011 * Steal the ref to the buffer currently queued in @pad.
2013 * MUST be called with the pad's object lock held.
2015 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2016 * queued. You should unref the buffer after usage.
2019 gst_aggregator_pad_steal_buffer_unlocked (GstAggregatorPad * pad)
2021 GstBuffer *buffer = NULL;
2024 GST_TRACE_OBJECT (pad, "Consuming buffer");
2025 buffer = pad->buffer;
2027 if (pad->priv->pending_eos) {
2028 pad->priv->pending_eos = FALSE;
2031 PAD_BROADCAST_EVENT (pad);
2032 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2039 * gst_aggregator_pad_steal_buffer:
2040 * @pad: the pad to get buffer from
2042 * Steal the ref to the buffer currently queued in @pad.
2044 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2045 * queued. You should unref the buffer after usage.
2048 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2050 GstBuffer *buffer = NULL;
2053 buffer = gst_aggregator_pad_steal_buffer_unlocked (pad);
2060 * gst_aggregator_pad_get_buffer:
2061 * @pad: the pad to get buffer from
2063 * Returns: (transfer full): A reference to the buffer in @pad or
2064 * NULL if no buffer was queued. You should unref the buffer after
2068 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2070 GstBuffer *buffer = NULL;
2074 buffer = gst_buffer_ref (pad->buffer);
2081 * gst_aggregator_merge_tags:
2082 * @self: a #GstAggregator
2083 * @tags: a #GstTagList to merge
2084 * @mode: the #GstTagMergeMode to use
2086 * Adds tags to so-called pending tags, which will be processed
2087 * before pushing out data downstream.
2089 * Note that this is provided for convenience, and the subclass is
2090 * not required to use this and can still do tag handling on its own.
2095 gst_aggregator_merge_tags (GstAggregator * self,
2096 const GstTagList * tags, GstTagMergeMode mode)
2100 g_return_if_fail (GST_IS_AGGREGATOR (self));
2101 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2103 /* FIXME Check if we can use OBJECT lock here! */
2104 GST_OBJECT_LOCK (self);
2106 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2107 otags = self->priv->tags;
2108 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2110 gst_tag_list_unref (otags);
2111 self->priv->tags_changed = TRUE;
2112 GST_OBJECT_UNLOCK (self);
2116 * gst_aggregator_set_latency:
2117 * @self: a #GstAggregator
2118 * @min_latency: minimum latency
2119 * @max_latency: maximum latency
2121 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2122 * latency is. Will also post a LATENCY message on the bus so the pipeline
2123 * can reconfigure its global latency.
2126 gst_aggregator_set_latency (GstAggregator * self,
2127 GstClockTime min_latency, GstClockTime max_latency)
2129 g_return_if_fail (GST_IS_AGGREGATOR (self));
2130 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2131 g_return_if_fail (max_latency >= min_latency);
2133 GST_OBJECT_LOCK (self);
2134 self->priv->sub_latency_min = min_latency;
2135 self->priv->sub_latency_max = max_latency;
2136 GST_OBJECT_UNLOCK (self);
2138 gst_element_post_message (GST_ELEMENT_CAST (self),
2139 gst_message_new_latency (GST_OBJECT_CAST (self)));