1 /* GStreamer aggregator base class
2 * Copyright (C) 2014 Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
3 * Copyright (C) 2014 Thibault Saunier <tsaunier@gnome.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 * SECTION: gstaggregator
24 * @short_description: manages a set of pads with the purpose of
25 * aggregating their buffers.
26 * @see_also: gstcollectpads for historical reasons.
28 * Manages a set of pads with the purpose of aggregating their buffers.
29 * Control is given to the subclass when all pads have data.
32 * Base class for mixers and muxers. Subclasses should at least implement
33 * the #GstAggregatorClass.aggregate() virtual method.
36 * When data is queued on all pads, tha aggregate vmethod is called.
39 * One can peek at the data on any given GstAggregatorPad with the
40 * gst_aggregator_pad_get_buffer () method, and take ownership of it
41 * with the gst_aggregator_pad_steal_buffer () method. When a buffer
42 * has been taken with steal_buffer (), a new buffer can be queued
46 * If the subclass wishes to push a buffer downstream in its aggregate
47 * implementation, it should do so through the
48 * gst_aggregator_finish_buffer () method. This method will take care
49 * of sending and ordering mandatory events such as stream start, caps
53 * Same goes for EOS events, which should not be pushed directly by the
54 * subclass, it should instead return GST_FLOW_EOS in its aggregate
64 #include <string.h> /* strlen */
66 #include "gstaggregator.h"
69 /* Might become API */
70 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
71 const GstTagList * tags, GstTagMergeMode mode);
72 static void gst_aggregator_set_latency_property (GstAggregator * agg,
74 static gint64 gst_aggregator_get_latency_property (GstAggregator * agg);
77 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
78 #define GST_CAT_DEFAULT aggregator_debug
80 /* GstAggregatorPad definitions */
81 #define PAD_LOCK(pad) G_STMT_START { \
82 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
84 GST_OBJECT_LOCK (pad); \
85 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
89 #define PAD_UNLOCK(pad) G_STMT_START { \
90 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
92 GST_OBJECT_UNLOCK (pad); \
93 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
98 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
99 GST_LOG_OBJECT (pad, "Waiting for EVENT on thread %p", \
101 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
102 GST_OBJECT_GET_LOCK (pad)); \
103 GST_LOG_OBJECT (pad, "DONE Waiting for EVENT on thread %p", \
107 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
108 GST_LOG_OBJECT (pad, "Signaling EVENT from thread %p", \
110 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
114 #define PAD_STREAM_LOCK(pad) G_STMT_START { \
115 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
117 g_mutex_lock(&pad->priv->stream_lock); \
118 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
122 #define PAD_STREAM_UNLOCK(pad) G_STMT_START { \
123 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
125 g_mutex_unlock(&pad->priv->stream_lock); \
126 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
130 #define SRC_STREAM_LOCK(self) G_STMT_START { \
131 GST_TRACE_OBJECT (self, "Taking src STREAM lock from thread %p", \
133 g_mutex_lock(&self->priv->src_lock); \
134 GST_TRACE_OBJECT (self, "Took src STREAM lock from thread %p", \
138 #define SRC_STREAM_UNLOCK(self) G_STMT_START { \
139 GST_TRACE_OBJECT (self, "Releasing src STREAM lock from thread %p", \
141 g_mutex_unlock(&self->priv->src_lock); \
142 GST_TRACE_OBJECT (self, "Released src STREAM lock from thread %p", \
146 #define SRC_STREAM_WAIT(self) G_STMT_START { \
147 GST_LOG_OBJECT (self, "Waiting for src STREAM on thread %p", \
149 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
150 GST_LOG_OBJECT (self, "DONE Waiting for src STREAM on thread %p", \
154 #define SRC_STREAM_BROADCAST(self) G_STMT_START { \
155 GST_LOG_OBJECT (self, "Signaling src STREAM from thread %p", \
157 if (self->priv->aggregate_id) \
158 gst_clock_id_unschedule (self->priv->aggregate_id); \
159 g_cond_broadcast(&(self->priv->src_cond)); \
162 struct _GstAggregatorPadPrivate
164 gboolean pending_flush_start;
165 gboolean pending_flush_stop;
166 gboolean pending_eos;
175 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
177 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
181 aggpad->priv->flushing = FALSE;
185 return klass->flush (aggpad, agg);
190 /*************************************
191 * GstAggregator implementation *
192 *************************************/
193 static GstElementClass *aggregator_parent_class = NULL;
195 /* All members are protected by the object lock unless otherwise noted */
197 struct _GstAggregatorPrivate
201 /* Our state is >= PAUSED */
202 gboolean running; /* protected by SRC_STREAM_LOCK */
205 gboolean send_stream_start; /* protected by srcpad stream lock */
206 gboolean send_segment;
207 gboolean flush_seeking;
208 gboolean pending_flush_start;
209 gboolean send_eos; /* protected by srcpad stream lock */
210 GstFlowReturn flow_return;
212 GstCaps *srccaps; /* protected by the srcpad stream lock */
215 gboolean tags_changed;
217 gboolean latency_live;
218 GstClockTime latency_min;
219 GstClockTime latency_max;
221 GstClockTime sub_latency_min;
222 GstClockTime sub_latency_max;
225 GstClockID aggregate_id; /* protected by src_lock */
239 gboolean one_actually_seeked;
242 #define DEFAULT_LATENCY 0
252 * gst_aggregator_iterate_sinkpads:
253 * @self: The #GstAggregator
254 * @func: (scope call): The function to call.
255 * @user_data: (closure): The data to pass to @func.
257 * Iterate the sinkpads of aggregator to call a function on them.
259 * This method guarantees that @func will be called only once for each
263 gst_aggregator_iterate_sinkpads (GstAggregator * self,
264 GstAggregatorPadForeachFunc func, gpointer user_data)
266 gboolean result = FALSE;
268 gboolean done = FALSE;
269 GValue item = { 0, };
270 GList *seen_pads = NULL;
272 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
278 switch (gst_iterator_next (iter, &item)) {
279 case GST_ITERATOR_OK:
281 GstAggregatorPad *pad;
283 pad = g_value_get_object (&item);
285 /* if already pushed, skip. FIXME, find something faster to tag pads */
286 if (pad == NULL || g_list_find (seen_pads, pad)) {
287 g_value_reset (&item);
291 GST_LOG_OBJECT (pad, "calling function %s on pad",
292 GST_DEBUG_FUNCPTR_NAME (func));
294 result = func (self, pad, user_data);
298 seen_pads = g_list_prepend (seen_pads, pad);
300 g_value_reset (&item);
303 case GST_ITERATOR_RESYNC:
304 gst_iterator_resync (iter);
306 case GST_ITERATOR_ERROR:
307 GST_ERROR_OBJECT (self,
308 "Could not iterate over internally linked pads");
311 case GST_ITERATOR_DONE:
316 g_value_unset (&item);
317 gst_iterator_free (iter);
319 if (seen_pads == NULL) {
320 GST_DEBUG_OBJECT (self, "No pad seen");
324 g_list_free (seen_pads);
331 gst_aggregator_check_pads_ready (GstAggregator * self)
333 GstAggregatorPad *pad;
336 GST_LOG_OBJECT (self, "checking pads");
338 GST_OBJECT_LOCK (self);
340 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
341 if (sinkpads == NULL)
344 for (l = sinkpads; l != NULL; l = l->next) {
348 if (pad->buffer == NULL && !pad->eos) {
349 GST_OBJECT_UNLOCK (pad);
356 GST_OBJECT_UNLOCK (self);
357 GST_LOG_OBJECT (self, "pads are ready");
362 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
363 GST_OBJECT_UNLOCK (self);
368 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
369 GST_OBJECT_UNLOCK (self);
375 gst_aggregator_reset_flow_values (GstAggregator * self)
377 GST_OBJECT_LOCK (self);
378 self->priv->flow_return = GST_FLOW_FLUSHING;
379 self->priv->send_stream_start = TRUE;
380 self->priv->send_segment = TRUE;
381 gst_segment_init (&self->segment, GST_FORMAT_TIME);
382 GST_OBJECT_UNLOCK (self);
386 gst_aggregator_push_mandatory_events (GstAggregator * self)
388 GstAggregatorPrivate *priv = self->priv;
389 GstEvent *segment = NULL;
390 GstEvent *tags = NULL;
392 if (self->priv->send_stream_start) {
395 GST_INFO_OBJECT (self, "pushing stream start");
396 /* stream-start (FIXME: create id based on input ids) */
397 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
398 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
399 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
401 self->priv->send_stream_start = FALSE;
404 if (self->priv->srccaps) {
406 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
407 self->priv->srccaps);
408 if (!gst_pad_push_event (self->srcpad,
409 gst_event_new_caps (self->priv->srccaps))) {
410 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
412 gst_caps_unref (self->priv->srccaps);
413 self->priv->srccaps = NULL;
416 GST_OBJECT_LOCK (self);
417 if (self->priv->send_segment && !self->priv->flush_seeking) {
418 segment = gst_event_new_segment (&self->segment);
420 if (!self->priv->seqnum)
421 self->priv->seqnum = gst_event_get_seqnum (segment);
423 gst_event_set_seqnum (segment, self->priv->seqnum);
424 self->priv->send_segment = FALSE;
426 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
429 if (priv->tags && priv->tags_changed) {
430 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
431 priv->tags_changed = FALSE;
433 GST_OBJECT_UNLOCK (self);
436 gst_pad_push_event (self->srcpad, segment);
438 gst_pad_push_event (self->srcpad, tags);
443 * gst_aggregator_set_src_caps:
444 * @self: The #GstAggregator
445 * @caps: The #GstCaps to set on the src pad.
447 * Sets the caps to be used on the src pad.
450 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
452 GST_PAD_STREAM_LOCK (self->srcpad);
453 gst_caps_replace (&self->priv->srccaps, caps);
454 gst_aggregator_push_mandatory_events (self);
455 GST_PAD_STREAM_UNLOCK (self->srcpad);
459 * gst_aggregator_finish_buffer:
460 * @self: The #GstAggregator
461 * @buffer: (transfer full): the #GstBuffer to push.
463 * This method will push the provided output buffer downstream. If needed,
464 * mandatory events such as stream-start, caps, and segment events will be
465 * sent before pushing the buffer.
468 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
470 gst_aggregator_push_mandatory_events (self);
472 GST_OBJECT_LOCK (self);
473 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
474 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
475 GST_OBJECT_UNLOCK (self);
476 return gst_pad_push (self->srcpad, buffer);
478 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
479 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
480 GST_OBJECT_UNLOCK (self);
481 gst_buffer_unref (buffer);
487 gst_aggregator_push_eos (GstAggregator * self)
490 gst_aggregator_push_mandatory_events (self);
492 self->priv->send_eos = FALSE;
493 event = gst_event_new_eos ();
494 gst_event_set_seqnum (event, self->priv->seqnum);
495 gst_pad_push_event (self->srcpad, event);
499 gst_aggregator_get_next_time (GstAggregator * self)
501 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
503 if (klass->get_next_time)
504 return klass->get_next_time (self);
506 return GST_CLOCK_TIME_NONE;
509 /* called with the src STREAM lock */
511 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
513 GstClockTime latency_max, latency_min;
519 SRC_STREAM_LOCK (self);
521 GST_OBJECT_LOCK (self);
522 gst_aggregator_get_latency_unlocked (self, &live, &latency_min, &latency_max);
523 GST_OBJECT_UNLOCK (self);
525 if (gst_aggregator_check_pads_ready (self)) {
526 GST_DEBUG_OBJECT (self, "all pads have data");
527 SRC_STREAM_UNLOCK (self);
532 /* Before waiting, check if we're actually still running */
533 if (!self->priv->running || !self->priv->send_eos) {
534 SRC_STREAM_UNLOCK (self);
539 start = gst_aggregator_get_next_time (self);
541 if (!live || !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self))
542 || !GST_CLOCK_TIME_IS_VALID (start)) {
543 /* We wake up here when something happened, and below
544 * then check if we're ready now. If we return FALSE,
545 * we will be directly called again.
547 SRC_STREAM_WAIT (self);
549 GstClockTime base_time, time;
551 GstClockReturn status;
552 GstClockTimeDiff jitter;
554 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
555 GST_TIME_ARGS (start));
557 GST_OBJECT_LOCK (self);
558 base_time = GST_ELEMENT_CAST (self)->base_time;
559 clock = GST_ELEMENT_CLOCK (self);
561 gst_object_ref (clock);
563 time = base_time + start;
565 if (GST_CLOCK_TIME_IS_VALID (latency_min)) {
568 time += self->priv->latency;
571 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
572 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
573 " latency max %" GST_TIME_FORMAT " latency min %" GST_TIME_FORMAT
574 " current %" GST_TIME_FORMAT ")", GST_TIME_ARGS (time),
575 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
576 GST_TIME_ARGS (start), GST_TIME_ARGS (latency_max),
577 GST_TIME_ARGS (latency_min),
578 GST_TIME_ARGS (gst_clock_get_time (clock)));
580 GST_OBJECT_UNLOCK (self);
582 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
583 gst_object_unref (clock);
584 SRC_STREAM_UNLOCK (self);
587 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
589 SRC_STREAM_LOCK (self);
590 if (self->priv->aggregate_id) {
591 gst_clock_id_unref (self->priv->aggregate_id);
592 self->priv->aggregate_id = NULL;
595 GST_DEBUG_OBJECT (self,
596 "clock returned %d (jitter: %s%" GST_TIME_FORMAT ")",
597 status, (jitter < 0 ? "-" : " "),
598 GST_TIME_ARGS ((jitter < 0 ? -jitter : jitter)));
601 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
602 SRC_STREAM_UNLOCK (self);
608 res = gst_aggregator_check_pads_ready (self);
609 SRC_STREAM_UNLOCK (self);
615 gst_aggregator_aggregate_func (GstAggregator * self)
617 GstAggregatorPrivate *priv = self->priv;
618 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
619 gboolean timeout = FALSE;
621 if (self->priv->running == FALSE) {
622 GST_DEBUG_OBJECT (self, "Not running anymore");
626 GST_LOG_OBJECT (self, "Checking aggregate");
627 while (priv->send_eos && priv->running) {
628 GstFlowReturn flow_return;
630 if (!gst_aggregator_wait_and_check (self, &timeout))
633 GST_TRACE_OBJECT (self, "Actually aggregating!");
635 flow_return = klass->aggregate (self, timeout);
637 GST_OBJECT_LOCK (self);
638 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking)
639 priv->flow_return = GST_FLOW_OK;
641 priv->flow_return = flow_return;
642 GST_OBJECT_UNLOCK (self);
644 if (flow_return == GST_FLOW_EOS) {
645 gst_aggregator_push_eos (self);
648 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
650 if (flow_return != GST_FLOW_OK)
656 gst_aggregator_start (GstAggregator * self)
658 GstAggregatorClass *klass;
661 self->priv->running = TRUE;
662 self->priv->send_stream_start = TRUE;
663 self->priv->send_segment = TRUE;
664 self->priv->send_eos = TRUE;
665 self->priv->srccaps = NULL;
666 self->priv->flow_return = GST_FLOW_OK;
668 klass = GST_AGGREGATOR_GET_CLASS (self);
671 result = klass->start (self);
679 _check_pending_flush_stop (GstAggregatorPad * pad)
681 return (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
685 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
689 GST_INFO_OBJECT (self, "%s srcpad task",
690 flush_start ? "Pausing" : "Stopping");
692 SRC_STREAM_LOCK (self);
693 self->priv->running = FALSE;
694 SRC_STREAM_BROADCAST (self);
695 SRC_STREAM_UNLOCK (self);
698 res = gst_pad_push_event (self->srcpad, flush_start);
701 gst_pad_stop_task (self->srcpad);
707 gst_aggregator_start_srcpad_task (GstAggregator * self)
709 GST_INFO_OBJECT (self, "Starting srcpad task");
711 self->priv->running = TRUE;
712 gst_pad_start_task (GST_PAD (self->srcpad),
713 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
717 gst_aggregator_flush (GstAggregator * self)
719 GstFlowReturn ret = GST_FLOW_OK;
720 GstAggregatorPrivate *priv = self->priv;
721 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
723 GST_DEBUG_OBJECT (self, "Flushing everything");
724 GST_OBJECT_LOCK (self);
725 priv->send_segment = TRUE;
726 priv->flush_seeking = FALSE;
727 priv->tags_changed = FALSE;
728 GST_OBJECT_UNLOCK (self);
730 ret = klass->flush (self);
736 /* Called with GstAggregator's object lock held */
739 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
742 GstAggregatorPad *tmppad;
744 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
745 tmppad = (GstAggregatorPad *) tmp->data;
747 if (_check_pending_flush_stop (tmppad) == FALSE) {
748 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
749 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
758 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
762 GstAggregatorPrivate *priv = self->priv;
763 GstAggregatorPadPrivate *padpriv = aggpad->priv;
765 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
766 /* Remove pad buffer and wake up the streaming thread */
767 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
768 gst_buffer_replace (&tmpbuf, NULL);
769 PAD_STREAM_LOCK (aggpad);
770 if (g_atomic_int_compare_and_exchange (&padpriv->pending_flush_start,
771 TRUE, FALSE) == TRUE) {
772 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
773 g_atomic_int_set (&padpriv->pending_flush_stop, TRUE);
776 GST_OBJECT_LOCK (self);
777 if (priv->flush_seeking) {
778 /* If flush_seeking we forward the first FLUSH_START */
779 if (priv->pending_flush_start) {
780 priv->pending_flush_start = FALSE;
781 GST_OBJECT_UNLOCK (self);
783 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
784 gst_aggregator_stop_srcpad_task (self, event);
785 priv->flow_return = GST_FLOW_OK;
787 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
788 GST_PAD_STREAM_LOCK (self->srcpad);
789 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
792 GST_OBJECT_UNLOCK (self);
793 gst_event_unref (event);
796 GST_OBJECT_UNLOCK (self);
797 gst_event_unref (event);
799 PAD_STREAM_UNLOCK (aggpad);
801 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
802 gst_buffer_replace (&tmpbuf, NULL);
805 /* GstAggregator vmethods default implementations */
807 gst_aggregator_default_sink_event (GstAggregator * self,
808 GstAggregatorPad * aggpad, GstEvent * event)
811 GstPad *pad = GST_PAD (aggpad);
812 GstAggregatorPrivate *priv = self->priv;
814 switch (GST_EVENT_TYPE (event)) {
815 case GST_EVENT_FLUSH_START:
817 gst_aggregator_flush_start (self, aggpad, event);
818 /* We forward only in one case: right after flush_seeking */
822 case GST_EVENT_FLUSH_STOP:
824 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
826 gst_aggregator_pad_flush (aggpad, self);
827 GST_OBJECT_LOCK (self);
828 if (priv->flush_seeking) {
829 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
830 if (gst_aggregator_all_flush_stop_received_locked (self)) {
831 GST_OBJECT_UNLOCK (self);
832 /* That means we received FLUSH_STOP/FLUSH_STOP on
833 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
834 gst_aggregator_flush (self);
835 gst_pad_push_event (self->srcpad, event);
837 SRC_STREAM_LOCK (self);
838 priv->send_eos = TRUE;
839 SRC_STREAM_BROADCAST (self);
840 SRC_STREAM_UNLOCK (self);
842 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
843 GST_PAD_STREAM_UNLOCK (self->srcpad);
844 gst_aggregator_start_srcpad_task (self);
846 GST_OBJECT_UNLOCK (self);
849 GST_OBJECT_UNLOCK (self);
852 /* We never forward the event */
857 GST_DEBUG_OBJECT (aggpad, "EOS");
859 /* We still have a buffer, and we don't want the subclass to have to
860 * check for it. Mark pending_eos, eos will be set when steal_buffer is
863 SRC_STREAM_LOCK (self);
865 if (!aggpad->buffer) {
868 aggpad->priv->pending_eos = TRUE;
872 SRC_STREAM_BROADCAST (self);
873 SRC_STREAM_UNLOCK (self);
876 case GST_EVENT_SEGMENT:
879 gst_event_copy_segment (event, &aggpad->segment);
880 self->priv->seqnum = gst_event_get_seqnum (event);
884 case GST_EVENT_STREAM_START:
892 gst_event_parse_tag (event, &tags);
894 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
895 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
896 gst_event_unref (event);
908 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
909 return gst_pad_event_default (pad, GST_OBJECT (self), event);
912 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
914 gst_event_unref (event);
919 static inline gboolean
920 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
921 gpointer unused_udata)
923 gst_aggregator_pad_flush (pad, self);
929 gst_aggregator_stop (GstAggregator * agg)
931 GstAggregatorClass *klass;
934 gst_aggregator_reset_flow_values (agg);
936 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
938 klass = GST_AGGREGATOR_GET_CLASS (agg);
941 result = klass->stop (agg);
946 gst_tag_list_unref (agg->priv->tags);
947 agg->priv->tags = NULL;
952 /* GstElement vmethods implementations */
953 static GstStateChangeReturn
954 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
956 GstStateChangeReturn ret;
957 GstAggregator *self = GST_AGGREGATOR (element);
959 switch (transition) {
960 case GST_STATE_CHANGE_READY_TO_PAUSED:
961 if (!gst_aggregator_start (self))
969 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
970 transition)) == GST_STATE_CHANGE_FAILURE)
974 switch (transition) {
975 case GST_STATE_CHANGE_PAUSED_TO_READY:
976 if (!gst_aggregator_stop (self)) {
977 /* What to do in this case? Error out? */
978 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
990 GST_ERROR_OBJECT (element, "parent failed state change");
995 GST_ERROR_OBJECT (element, "Subclass failed to start");
996 return GST_STATE_CHANGE_FAILURE;
1001 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1003 GstAggregator *self = GST_AGGREGATOR (element);
1006 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1008 GST_INFO_OBJECT (pad, "Removing pad");
1010 SRC_STREAM_LOCK (self);
1011 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1012 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
1013 gst_buffer_replace (&tmpbuf, NULL);
1014 gst_element_remove_pad (element, pad);
1016 SRC_STREAM_BROADCAST (self);
1017 SRC_STREAM_UNLOCK (self);
1021 gst_aggregator_request_new_pad (GstElement * element,
1022 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1024 GstAggregator *self;
1025 GstAggregatorPad *agg_pad;
1027 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1028 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1030 self = GST_AGGREGATOR (element);
1032 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1036 GST_OBJECT_LOCK (element);
1037 if (req_name == NULL || strlen (req_name) < 6
1038 || !g_str_has_prefix (req_name, "sink_")) {
1039 /* no name given when requesting the pad, use next available int */
1042 /* parse serial number from requested padname */
1043 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1044 if (serial >= priv->padcount)
1045 priv->padcount = serial;
1048 name = g_strdup_printf ("sink_%u", priv->padcount);
1049 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1050 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1053 GST_OBJECT_UNLOCK (element);
1059 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1062 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1064 /* add the pad to the element */
1065 gst_element_add_pad (element, GST_PAD (agg_pad));
1067 return GST_PAD (agg_pad);
1072 GstClockTime min, max;
1077 gst_aggregator_query_sink_latency_foreach (GstAggregator * self,
1078 GstAggregatorPad * pad, gpointer user_data)
1080 LatencyData *data = user_data;
1081 GstClockTime min, max;
1085 query = gst_query_new_latency ();
1086 res = gst_pad_peer_query (GST_PAD_CAST (pad), query);
1089 gst_query_parse_latency (query, &live, &min, &max);
1091 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
1092 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1094 if (min != GST_CLOCK_TIME_NONE && min > data->min)
1097 if (max != GST_CLOCK_TIME_NONE &&
1098 ((data->max != GST_CLOCK_TIME_NONE && max < data->max) ||
1099 (data->max == GST_CLOCK_TIME_NONE)))
1105 gst_query_unref (query);
1111 * gst_aggregator_get_latency_unlocked:
1112 * @self: a #GstAggregator
1113 * @live: (out) (allow-none): whether @self is live
1114 * @min_latency: (out) (allow-none): the configured minimum latency of @self
1115 * @max_latency: (out) (allow-none): the configured maximum latency of @self
1117 * Retreives the latency values reported by @self in response to the latency
1120 * Typically only called by subclasses.
1122 * MUST be called with the object lock held.
1125 gst_aggregator_get_latency_unlocked (GstAggregator * self, gboolean * live,
1126 GstClockTime * min_latency, GstClockTime * max_latency)
1128 GstClockTime our_latency;
1129 GstClockTime min, max;
1131 g_return_if_fail (GST_IS_AGGREGATOR (self));
1133 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1134 min = self->priv->latency_min;
1135 max = self->priv->latency_max;
1137 min += self->priv->sub_latency_min;
1138 if (GST_CLOCK_TIME_IS_VALID (max)
1139 && GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max))
1140 max += self->priv->sub_latency_max;
1142 our_latency = self->priv->latency;
1143 if (GST_CLOCK_TIME_IS_VALID (our_latency)) {
1145 if (GST_CLOCK_TIME_IS_VALID (max))
1150 *live = self->priv->latency_live;
1158 gst_aggregator_query_latency (GstAggregator * self, GstQuery * query)
1160 GstClockTime our_latency;
1164 data.max = GST_CLOCK_TIME_NONE;
1167 /* query upstream's latency */
1168 SRC_STREAM_LOCK (self);
1169 gst_aggregator_iterate_sinkpads (self,
1170 gst_aggregator_query_sink_latency_foreach, &data);
1171 SRC_STREAM_UNLOCK (self);
1173 GST_OBJECT_LOCK (self);
1174 our_latency = self->priv->latency;
1176 if (data.live && GST_CLOCK_TIME_IS_VALID (our_latency) &&
1177 our_latency > data.max) {
1178 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1179 ("%s", "Latency too big"),
1180 ("The requested latency value is too big for the current pipeline. "
1181 "Limiting to %" G_GINT64_FORMAT, data.max));
1182 self->priv->latency = data.max;
1183 /* FIXME: shouldn't we g_object_notify() the change here? */
1186 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (data.min))) {
1187 GST_WARNING_OBJECT (self, "Invalid minimum latency, using 0");
1191 if (G_UNLIKELY (data.min > data.max)) {
1192 GST_WARNING_OBJECT (self, "Minimum latency is greater than maximum latency "
1193 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT "). "
1194 "Clamping it at the maximum latency", data.min, data.max);
1195 data.min = data.max;
1198 self->priv->latency_live = data.live;
1199 self->priv->latency_min = data.min;
1200 self->priv->latency_max = data.max;
1203 if (GST_CLOCK_TIME_IS_VALID (our_latency)) {
1204 if (GST_CLOCK_TIME_IS_VALID (data.min))
1205 data.min += our_latency;
1206 if (GST_CLOCK_TIME_IS_VALID (data.max))
1207 data.max += our_latency;
1210 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_min)
1211 && GST_CLOCK_TIME_IS_VALID (data.min))
1212 data.min += self->priv->sub_latency_min;
1213 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1214 && GST_CLOCK_TIME_IS_VALID (data.max))
1215 data.max += self->priv->sub_latency_max;
1217 GST_OBJECT_UNLOCK (self);
1219 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1220 " max:%" G_GINT64_FORMAT, data.live ? "true" : "false", data.min,
1223 gst_query_set_latency (query, data.live, data.min, data.max);
1229 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1231 GstAggregator *self = GST_AGGREGATOR (element);
1233 GST_STATE_LOCK (element);
1234 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1235 GST_STATE (element) < GST_STATE_PAUSED) {
1239 GstSeekType start_type, stop_type;
1242 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1243 &start, &stop_type, &stop);
1244 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1245 stop_type, stop, NULL);
1247 self->priv->seqnum = gst_event_get_seqnum (event);
1248 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1250 GST_STATE_UNLOCK (element);
1253 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1258 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1260 gboolean res = TRUE;
1262 switch (GST_QUERY_TYPE (query)) {
1263 case GST_QUERY_SEEKING:
1267 /* don't pass it along as some (file)sink might claim it does
1268 * whereas with a collectpads in between that will not likely work */
1269 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1270 gst_query_set_seeking (query, format, FALSE, 0, -1);
1275 case GST_QUERY_LATENCY:
1279 ret = gst_aggregator_query_latency (self, query);
1280 /* Wake up the src thread again, due to changed latencies
1281 * or changed live-ness we might have to adjust if we wait
1282 * on a deadline at all and how long.
1283 * This is only to unschedule the clock id, we don't really care
1284 * about the GCond here.
1286 SRC_STREAM_LOCK (self);
1287 SRC_STREAM_BROADCAST (self);
1288 SRC_STREAM_UNLOCK (self);
1295 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1302 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1304 EventData *evdata = user_data;
1305 gboolean ret = TRUE;
1306 GstPad *peer = gst_pad_get_peer (pad);
1307 GstAggregatorPadPrivate *padpriv = GST_AGGREGATOR_PAD (pad)->priv;
1310 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1311 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1312 gst_object_unref (peer);
1316 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1317 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1319 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1320 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1322 if (gst_pad_query (peer, seeking)) {
1325 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1327 if (seekable == FALSE) {
1328 GST_INFO_OBJECT (pad,
1329 "Source not seekable, We failed but it does not matter!");
1334 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1337 gst_query_unref (seeking);
1340 if (evdata->flush) {
1341 padpriv->pending_flush_start = FALSE;
1342 padpriv->pending_flush_stop = FALSE;
1345 evdata->one_actually_seeked = TRUE;
1348 evdata->result &= ret;
1350 /* Always send to all pads */
1355 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1356 GstEvent * event, gboolean flush)
1360 evdata.event = event;
1361 evdata.result = TRUE;
1362 evdata.flush = flush;
1363 evdata.one_actually_seeked = FALSE;
1365 /* We first need to set all pads as flushing in a first pass
1366 * as flush_start flush_stop is sometimes sent synchronously
1367 * while we send the seek event */
1371 GST_OBJECT_LOCK (self);
1372 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1373 GstAggregatorPad *pad = l->data;
1375 pad->priv->pending_flush_start = TRUE;
1376 pad->priv->pending_flush_stop = FALSE;
1378 GST_OBJECT_UNLOCK (self);
1381 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1383 gst_event_unref (event);
1389 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1394 GstSeekType start_type, stop_type;
1398 GstAggregatorPrivate *priv = self->priv;
1400 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1401 &start, &stop_type, &stop);
1403 GST_INFO_OBJECT (self, "starting SEEK");
1405 flush = flags & GST_SEEK_FLAG_FLUSH;
1407 GST_OBJECT_LOCK (self);
1409 priv->pending_flush_start = TRUE;
1410 priv->flush_seeking = TRUE;
1413 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1414 stop_type, stop, NULL);
1415 GST_OBJECT_UNLOCK (self);
1417 /* forward the seek upstream */
1418 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, flush);
1421 if (!evdata.result || !evdata.one_actually_seeked) {
1422 GST_OBJECT_LOCK (self);
1423 priv->flush_seeking = FALSE;
1424 priv->pending_flush_start = FALSE;
1425 GST_OBJECT_UNLOCK (self);
1428 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1430 return evdata.result;
1434 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1437 gboolean res = TRUE;
1439 switch (GST_EVENT_TYPE (event)) {
1440 case GST_EVENT_SEEK:
1442 gst_event_ref (event);
1443 res = gst_aggregator_do_seek (self, event);
1444 gst_event_unref (event);
1448 case GST_EVENT_NAVIGATION:
1450 /* navigation is rather pointless. */
1452 gst_event_unref (event);
1461 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE);
1462 res = evdata.result;
1469 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1472 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1474 return klass->src_event (GST_AGGREGATOR (parent), event);
1478 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1481 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1483 return klass->src_query (GST_AGGREGATOR (parent), query);
1487 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1488 GstObject * parent, GstPadMode mode, gboolean active)
1490 GstAggregator *self = GST_AGGREGATOR (parent);
1491 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1493 if (klass->src_activate) {
1494 if (klass->src_activate (self, mode, active) == FALSE) {
1499 if (active == TRUE) {
1501 case GST_PAD_MODE_PUSH:
1503 GST_INFO_OBJECT (pad, "Activating pad!");
1504 gst_aggregator_start_srcpad_task (self);
1509 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1516 GST_INFO_OBJECT (self, "Deactivating srcpad");
1517 gst_aggregator_stop_srcpad_task (self, FALSE);
1523 gst_aggregator_default_sink_query (GstAggregator * self,
1524 GstAggregatorPad * aggpad, GstQuery * query)
1526 GstPad *pad = GST_PAD (aggpad);
1528 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1532 gst_aggregator_finalize (GObject * object)
1534 GstAggregator *self = (GstAggregator *) object;
1536 g_mutex_clear (&self->priv->src_lock);
1537 g_cond_clear (&self->priv->src_cond);
1539 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1543 * gst_aggregator_set_latency_property:
1544 * @agg: a #GstAggregator
1545 * @latency: the new latency value.
1547 * Sets the new latency value to @latency. This value is used to limit the
1548 * amount of time a pad waits for data to appear before considering the pad
1552 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1556 g_return_if_fail (GST_IS_AGGREGATOR (self));
1558 GST_OBJECT_LOCK (self);
1560 if (self->priv->latency_live && self->priv->latency_max != 0 &&
1561 GST_CLOCK_TIME_IS_VALID (latency) && latency > self->priv->latency_max) {
1562 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1563 ("%s", "Latency too big"),
1564 ("The requested latency value is too big for the latency in the "
1565 "current pipeline. Limiting to %" G_GINT64_FORMAT,
1566 self->priv->latency_max));
1567 latency = self->priv->latency_max;
1570 changed = (self->priv->latency != latency);
1571 self->priv->latency = latency;
1572 GST_OBJECT_UNLOCK (self);
1575 gst_element_post_message (GST_ELEMENT_CAST (self),
1576 gst_message_new_latency (GST_OBJECT_CAST (self)));
1580 * gst_aggregator_get_latency_property:
1581 * @agg: a #GstAggregator
1583 * Gets the latency value. See gst_aggregator_set_latency for
1586 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1587 * before a pad is deemed unresponsive. A value of -1 means an
1591 gst_aggregator_get_latency_property (GstAggregator * agg)
1595 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1597 GST_OBJECT_LOCK (agg);
1598 res = agg->priv->latency;
1599 GST_OBJECT_UNLOCK (agg);
1605 gst_aggregator_set_property (GObject * object, guint prop_id,
1606 const GValue * value, GParamSpec * pspec)
1608 GstAggregator *agg = GST_AGGREGATOR (object);
1612 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1615 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1621 gst_aggregator_get_property (GObject * object, guint prop_id,
1622 GValue * value, GParamSpec * pspec)
1624 GstAggregator *agg = GST_AGGREGATOR (object);
1628 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1631 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1636 /* GObject vmethods implementations */
1638 gst_aggregator_class_init (GstAggregatorClass * klass)
1640 GObjectClass *gobject_class = (GObjectClass *) klass;
1641 GstElementClass *gstelement_class = (GstElementClass *) klass;
1643 aggregator_parent_class = g_type_class_peek_parent (klass);
1644 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1646 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1647 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1649 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1651 klass->sink_event = gst_aggregator_default_sink_event;
1652 klass->sink_query = gst_aggregator_default_sink_query;
1654 klass->src_event = gst_aggregator_default_src_event;
1655 klass->src_query = gst_aggregator_default_src_query;
1657 gstelement_class->request_new_pad =
1658 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1659 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1660 gstelement_class->release_pad =
1661 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1662 gstelement_class->change_state =
1663 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1665 gobject_class->set_property = gst_aggregator_set_property;
1666 gobject_class->get_property = gst_aggregator_get_property;
1667 gobject_class->finalize = gst_aggregator_finalize;
1669 g_object_class_install_property (gobject_class, PROP_LATENCY,
1670 g_param_spec_int64 ("latency", "Buffer latency",
1671 "Additional latency in live mode to allow upstream "
1672 "to take longer to produce buffers for the current "
1674 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1675 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1677 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1678 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_query_sink_latency_foreach);
1682 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1684 GstPadTemplate *pad_template;
1685 GstAggregatorPrivate *priv;
1687 g_return_if_fail (klass->aggregate != NULL);
1690 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1691 GstAggregatorPrivate);
1696 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1697 g_return_if_fail (pad_template != NULL);
1699 priv->padcount = -1;
1700 priv->tags_changed = FALSE;
1702 self->priv->latency_live = FALSE;
1703 self->priv->latency_min = self->priv->sub_latency_min = 0;
1704 self->priv->latency_max = self->priv->sub_latency_max = GST_CLOCK_TIME_NONE;
1705 gst_aggregator_reset_flow_values (self);
1707 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1709 gst_pad_set_event_function (self->srcpad,
1710 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
1711 gst_pad_set_query_function (self->srcpad,
1712 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
1713 gst_pad_set_activatemode_function (self->srcpad,
1714 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
1716 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1718 self->priv->latency = DEFAULT_LATENCY;
1720 g_mutex_init (&self->priv->src_lock);
1721 g_cond_init (&self->priv->src_cond);
1724 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1725 * method to get to the padtemplates */
1727 gst_aggregator_get_type (void)
1729 static volatile gsize type = 0;
1731 if (g_once_init_enter (&type)) {
1733 static const GTypeInfo info = {
1734 sizeof (GstAggregatorClass),
1737 (GClassInitFunc) gst_aggregator_class_init,
1740 sizeof (GstAggregator),
1742 (GInstanceInitFunc) gst_aggregator_init,
1745 _type = g_type_register_static (GST_TYPE_ELEMENT,
1746 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1747 g_once_init_leave (&type, _type);
1752 static GstFlowReturn
1753 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1755 GstBuffer *actual_buf = buffer;
1756 GstAggregator *self = GST_AGGREGATOR (object);
1757 GstAggregatorPrivate *priv = self->priv;
1758 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1759 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1760 GstFlowReturn flow_return;
1762 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1764 PAD_STREAM_LOCK (aggpad);
1766 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1769 if (g_atomic_int_get (&aggpad->priv->pending_eos) == TRUE)
1774 while (aggpad->buffer && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1775 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1776 PAD_WAIT_EVENT (aggpad);
1778 PAD_UNLOCK (aggpad);
1780 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1783 if (aggclass->clip) {
1784 aggclass->clip (self, aggpad, buffer, &actual_buf);
1787 SRC_STREAM_LOCK (self);
1790 gst_buffer_unref (aggpad->buffer);
1791 aggpad->buffer = actual_buf;
1792 PAD_UNLOCK (aggpad);
1793 PAD_STREAM_UNLOCK (aggpad);
1795 if (gst_aggregator_check_pads_ready (self))
1796 SRC_STREAM_BROADCAST (self);
1797 SRC_STREAM_UNLOCK (self);
1799 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1801 GST_OBJECT_LOCK (self);
1802 flow_return = priv->flow_return;
1803 GST_OBJECT_UNLOCK (self);
1808 PAD_STREAM_UNLOCK (aggpad);
1810 gst_buffer_unref (buffer);
1811 GST_DEBUG_OBJECT (aggpad, "We are flushing");
1813 return GST_FLOW_FLUSHING;
1816 PAD_STREAM_UNLOCK (aggpad);
1818 gst_buffer_unref (buffer);
1819 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1821 return GST_FLOW_EOS;
1825 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
1828 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1829 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1831 if (GST_QUERY_IS_SERIALIZED (query)) {
1834 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE) {
1835 PAD_UNLOCK (aggpad);
1839 while (aggpad->buffer
1840 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1841 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1842 PAD_WAIT_EVENT (aggpad);
1844 PAD_UNLOCK (aggpad);
1846 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1850 return klass->sink_query (GST_AGGREGATOR (parent),
1851 GST_AGGREGATOR_PAD (pad), query);
1854 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping query");
1859 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
1862 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1863 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1865 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1866 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1869 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1870 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
1871 PAD_UNLOCK (aggpad);
1875 while (aggpad->buffer
1876 && g_atomic_int_get (&aggpad->priv->flushing) == FALSE) {
1877 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1878 PAD_WAIT_EVENT (aggpad);
1880 PAD_UNLOCK (aggpad);
1882 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE
1883 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1887 return klass->sink_event (GST_AGGREGATOR (parent),
1888 GST_AGGREGATOR_PAD (pad), event);
1891 GST_DEBUG_OBJECT (aggpad, "Pad is flushing, dropping event");
1892 if (GST_EVENT_IS_STICKY (event))
1893 gst_pad_store_sticky_event (pad, event);
1894 gst_event_unref (event);
1899 gst_aggregator_pad_activate_mode_func (GstPad * pad,
1900 GstObject * parent, GstPadMode mode, gboolean active)
1902 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1904 if (active == FALSE) {
1906 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1907 gst_buffer_replace (&aggpad->buffer, NULL);
1908 PAD_BROADCAST_EVENT (aggpad);
1909 PAD_UNLOCK (aggpad);
1912 g_atomic_int_set (&aggpad->priv->flushing, FALSE);
1913 PAD_BROADCAST_EVENT (aggpad);
1914 PAD_UNLOCK (aggpad);
1920 /***********************************
1921 * GstAggregatorPad implementation *
1922 ************************************/
1923 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1926 gst_aggregator_pad_constructed (GObject * object)
1928 GstPad *pad = GST_PAD (object);
1930 gst_pad_set_chain_function (pad,
1931 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
1932 gst_pad_set_event_function (pad,
1933 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
1934 gst_pad_set_query_function (pad,
1935 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
1936 gst_pad_set_activatemode_function (pad,
1937 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
1941 gst_aggregator_pad_finalize (GObject * object)
1943 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1945 g_cond_clear (&pad->priv->event_cond);
1946 g_mutex_clear (&pad->priv->stream_lock);
1948 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
1952 gst_aggregator_pad_dispose (GObject * object)
1954 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1957 buf = gst_aggregator_pad_steal_buffer (pad);
1959 gst_buffer_unref (buf);
1961 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
1965 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
1967 GObjectClass *gobject_class = (GObjectClass *) klass;
1969 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
1971 gobject_class->constructed = gst_aggregator_pad_constructed;
1972 gobject_class->finalize = gst_aggregator_pad_finalize;
1973 gobject_class->dispose = gst_aggregator_pad_dispose;
1977 gst_aggregator_pad_init (GstAggregatorPad * pad)
1980 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
1981 GstAggregatorPadPrivate);
1984 g_cond_init (&pad->priv->event_cond);
1986 g_mutex_init (&pad->priv->stream_lock);
1990 * gst_aggregator_pad_steal_buffer_unlocked:
1991 * @pad: the pad to get buffer from
1993 * Steal the ref to the buffer currently queued in @pad.
1995 * MUST be called with the pad's object lock held.
1997 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
1998 * queued. You should unref the buffer after usage.
2001 gst_aggregator_pad_steal_buffer_unlocked (GstAggregatorPad * pad)
2003 GstBuffer *buffer = NULL;
2006 GST_TRACE_OBJECT (pad, "Consuming buffer");
2007 buffer = pad->buffer;
2009 if (pad->priv->pending_eos) {
2010 pad->priv->pending_eos = FALSE;
2013 PAD_BROADCAST_EVENT (pad);
2014 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2021 * gst_aggregator_pad_steal_buffer:
2022 * @pad: the pad to get buffer from
2024 * Steal the ref to the buffer currently queued in @pad.
2026 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2027 * queued. You should unref the buffer after usage.
2030 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2032 GstBuffer *buffer = NULL;
2035 buffer = gst_aggregator_pad_steal_buffer_unlocked (pad);
2042 * gst_aggregator_pad_get_buffer:
2043 * @pad: the pad to get buffer from
2045 * Returns: (transfer full): A reference to the buffer in @pad or
2046 * NULL if no buffer was queued. You should unref the buffer after
2050 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2052 GstBuffer *buffer = NULL;
2056 buffer = gst_buffer_ref (pad->buffer);
2063 * gst_aggregator_merge_tags:
2064 * @self: a #GstAggregator
2065 * @tags: a #GstTagList to merge
2066 * @mode: the #GstTagMergeMode to use
2068 * Adds tags to so-called pending tags, which will be processed
2069 * before pushing out data downstream.
2071 * Note that this is provided for convenience, and the subclass is
2072 * not required to use this and can still do tag handling on its own.
2077 gst_aggregator_merge_tags (GstAggregator * self,
2078 const GstTagList * tags, GstTagMergeMode mode)
2082 g_return_if_fail (GST_IS_AGGREGATOR (self));
2083 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2085 /* FIXME Check if we can use OBJECT lock here! */
2086 GST_OBJECT_LOCK (self);
2088 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2089 otags = self->priv->tags;
2090 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2092 gst_tag_list_unref (otags);
2093 self->priv->tags_changed = TRUE;
2094 GST_OBJECT_UNLOCK (self);
2098 * gst_aggregator_set_latency:
2099 * @self: a #GstAggregator
2100 * @min_latency: minimum latency
2101 * @max_latency: maximum latency
2103 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2104 * latency is. Will also post a LATENCY message on the bus so the pipeline
2105 * can reconfigure its global latency.
2108 gst_aggregator_set_latency (GstAggregator * self,
2109 GstClockTime min_latency, GstClockTime max_latency)
2111 g_return_if_fail (GST_IS_AGGREGATOR (self));
2112 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2113 g_return_if_fail (max_latency >= min_latency);
2115 GST_OBJECT_LOCK (self);
2116 self->priv->sub_latency_min = min_latency;
2117 self->priv->sub_latency_max = max_latency;
2118 GST_OBJECT_UNLOCK (self);
2120 gst_element_post_message (GST_ELEMENT_CAST (self),
2121 gst_message_new_latency (GST_OBJECT_CAST (self)));