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 * @title: GstAggregator
25 * @short_description: manages a set of pads with the purpose of
26 * aggregating their buffers.
27 * @see_also: gstcollectpads for historical reasons.
29 * Manages a set of pads with the purpose of aggregating their buffers.
30 * 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.
35 * * When data is queued on all pads, tha aggregate vmethod is called.
37 * * One can peek at the data on any given GstAggregatorPad with the
38 * gst_aggregator_pad_get_buffer () method, and take ownership of it
39 * with the gst_aggregator_pad_steal_buffer () method. When a buffer
40 * has been taken with steal_buffer (), a new buffer can be queued
43 * * If the subclass wishes to push a buffer downstream in its aggregate
44 * implementation, it should do so through the
45 * gst_aggregator_finish_buffer () method. This method will take care
46 * of sending and ordering mandatory events such as stream start, caps
49 * * Same goes for EOS events, which should not be pushed directly by the
50 * subclass, it should instead return GST_FLOW_EOS in its aggregate
53 * * Note that the aggregator logic regarding gap event handling is to turn
54 * these into gap buffers with matching PTS and duration. It will also
55 * flag these buffers with GST_BUFFER_FLAG_GAP and GST_BUFFER_FLAG_DROPPABLE
56 * to ease their identification and subsequent processing.
64 #include <string.h> /* strlen */
66 #include "gstaggregator.h"
70 GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
71 GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
72 GST_AGGREGATOR_START_TIME_SELECTION_SET
73 } GstAggregatorStartTimeSelection;
76 gst_aggregator_start_time_selection_get_type (void)
78 static GType gtype = 0;
81 static const GEnumValue values[] = {
82 {GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
83 "Start at 0 running time (default)", "zero"},
84 {GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
85 "Start at first observed input running time", "first"},
86 {GST_AGGREGATOR_START_TIME_SELECTION_SET,
87 "Set start time with start-time property", "set"},
91 gtype = g_enum_register_static ("GstAggregatorStartTimeSelection", values);
96 /* Might become API */
97 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
98 const GstTagList * tags, GstTagMergeMode mode);
99 static void gst_aggregator_set_latency_property (GstAggregator * agg,
101 static gint64 gst_aggregator_get_latency_property (GstAggregator * agg);
104 /* Locking order, locks in this element must always be taken in this order
106 * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
107 * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
108 * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
109 * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
110 * standard element object lock -> GST_OBJECT_LOCK(agg)
111 * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
112 * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
113 * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
117 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
119 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
120 #define GST_CAT_DEFAULT aggregator_debug
122 /* GstAggregatorPad definitions */
123 #define PAD_LOCK(pad) G_STMT_START { \
124 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
126 g_mutex_lock(&pad->priv->lock); \
127 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
131 #define PAD_UNLOCK(pad) G_STMT_START { \
132 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
134 g_mutex_unlock(&pad->priv->lock); \
135 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
140 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
141 GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p", \
143 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
144 (&((GstAggregatorPad*)pad)->priv->lock)); \
145 GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
149 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
150 GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p", \
152 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
156 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
157 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
159 g_mutex_lock(&pad->priv->flush_lock); \
160 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
164 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
165 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
167 g_mutex_unlock(&pad->priv->flush_lock); \
168 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
172 #define SRC_LOCK(self) G_STMT_START { \
173 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
175 g_mutex_lock(&self->priv->src_lock); \
176 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
180 #define SRC_UNLOCK(self) G_STMT_START { \
181 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
183 g_mutex_unlock(&self->priv->src_lock); \
184 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
188 #define SRC_WAIT(self) G_STMT_START { \
189 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
191 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
192 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
196 #define SRC_BROADCAST(self) G_STMT_START { \
197 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
199 if (self->priv->aggregate_id) \
200 gst_clock_id_unschedule (self->priv->aggregate_id); \
201 g_cond_broadcast(&(self->priv->src_cond)); \
204 struct _GstAggregatorPadPrivate
206 /* Following fields are protected by the PAD_LOCK */
207 GstFlowReturn flow_return;
208 gboolean pending_flush_start;
209 gboolean pending_flush_stop;
210 gboolean pending_eos;
212 gboolean first_buffer;
216 GstClockTime head_position;
217 GstClockTime tail_position;
218 GstClockTime head_time;
219 GstClockTime tail_time;
220 GstClockTime time_level;
226 /* This lock prevents a flush start processing happening while
227 * the chain function is also happening.
232 /* Must be called with PAD_LOCK held */
234 gst_aggregator_pad_reset_unlocked (GstAggregatorPad * aggpad)
236 aggpad->priv->pending_eos = FALSE;
237 aggpad->priv->eos = FALSE;
238 aggpad->priv->flow_return = GST_FLOW_OK;
239 GST_OBJECT_LOCK (aggpad);
240 gst_segment_init (&aggpad->segment, GST_FORMAT_UNDEFINED);
241 gst_segment_init (&aggpad->clip_segment, GST_FORMAT_UNDEFINED);
242 GST_OBJECT_UNLOCK (aggpad);
243 aggpad->priv->head_position = GST_CLOCK_TIME_NONE;
244 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
245 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
246 aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
247 aggpad->priv->time_level = 0;
248 aggpad->priv->first_buffer = TRUE;
252 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
254 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
257 gst_aggregator_pad_reset_unlocked (aggpad);
261 return klass->flush (aggpad, agg);
266 /*************************************
267 * GstAggregator implementation *
268 *************************************/
269 static GstElementClass *aggregator_parent_class = NULL;
271 /* All members are protected by the object lock unless otherwise noted */
273 struct _GstAggregatorPrivate
277 /* Our state is >= PAUSED */
278 gboolean running; /* protected by src_lock */
281 gboolean send_stream_start; /* protected by srcpad stream lock */
282 gboolean send_segment;
283 gboolean flush_seeking;
284 gboolean pending_flush_start;
285 gboolean send_eos; /* protected by srcpad stream lock */
287 GstCaps *srccaps; /* protected by the srcpad stream lock */
290 gboolean tags_changed;
292 gboolean peer_latency_live; /* protected by src_lock */
293 GstClockTime peer_latency_min; /* protected by src_lock */
294 GstClockTime peer_latency_max; /* protected by src_lock */
295 gboolean has_peer_latency; /* protected by src_lock */
297 GstClockTime sub_latency_min; /* protected by src_lock */
298 GstClockTime sub_latency_max; /* protected by src_lock */
301 GstClockID aggregate_id; /* protected by src_lock */
305 gboolean first_buffer; /* protected by object lock */
306 GstAggregatorStartTimeSelection start_time_selection;
307 GstClockTime start_time;
310 gint64 latency; /* protected by both src_lock and all pad locks */
318 gboolean only_to_active_pads;
320 gboolean one_actually_seeked;
323 #define DEFAULT_LATENCY 0
324 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
325 #define DEFAULT_START_TIME (-1)
331 PROP_START_TIME_SELECTION,
336 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
337 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
340 * gst_aggregator_iterate_sinkpads:
341 * @self: The #GstAggregator
342 * @func: (scope call): The function to call.
343 * @user_data: (closure): The data to pass to @func.
345 * Iterate the sinkpads of aggregator to call a function on them.
347 * This method guarantees that @func will be called only once for each
351 gst_aggregator_iterate_sinkpads (GstAggregator * self,
352 GstAggregatorPadForeachFunc func, gpointer user_data)
354 gboolean result = FALSE;
356 gboolean done = FALSE;
357 GValue item = { 0, };
358 GList *seen_pads = NULL;
360 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
366 switch (gst_iterator_next (iter, &item)) {
367 case GST_ITERATOR_OK:
369 GstAggregatorPad *pad;
371 pad = g_value_get_object (&item);
373 /* if already pushed, skip. FIXME, find something faster to tag pads */
374 if (pad == NULL || g_list_find (seen_pads, pad)) {
375 g_value_reset (&item);
379 GST_LOG_OBJECT (pad, "calling function %s on pad",
380 GST_DEBUG_FUNCPTR_NAME (func));
382 result = func (self, pad, user_data);
386 seen_pads = g_list_prepend (seen_pads, pad);
388 g_value_reset (&item);
391 case GST_ITERATOR_RESYNC:
392 gst_iterator_resync (iter);
394 case GST_ITERATOR_ERROR:
395 GST_ERROR_OBJECT (self,
396 "Could not iterate over internally linked pads");
399 case GST_ITERATOR_DONE:
404 g_value_unset (&item);
405 gst_iterator_free (iter);
407 if (seen_pads == NULL) {
408 GST_DEBUG_OBJECT (self, "No pad seen");
412 g_list_free (seen_pads);
419 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
421 return (g_queue_peek_tail (&pad->priv->buffers) == NULL);
425 gst_aggregator_check_pads_ready (GstAggregator * self)
427 GstAggregatorPad *pad;
429 gboolean have_data = TRUE;
431 GST_LOG_OBJECT (self, "checking pads");
433 GST_OBJECT_LOCK (self);
435 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
436 if (sinkpads == NULL)
439 for (l = sinkpads; l != NULL; l = l->next) {
444 if (gst_aggregator_pad_queue_is_empty (pad)) {
445 if (!pad->priv->eos) {
448 /* If not live we need data on all pads, so leave the loop */
449 if (!self->priv->peer_latency_live) {
454 } else if (self->priv->peer_latency_live) {
455 /* In live mode, having a single pad with buffers is enough to
456 * generate a start time from it. In non-live mode all pads need
459 self->priv->first_buffer = FALSE;
468 self->priv->first_buffer = FALSE;
470 GST_OBJECT_UNLOCK (self);
471 GST_LOG_OBJECT (self, "pads are ready");
476 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
477 GST_OBJECT_UNLOCK (self);
482 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
483 GST_OBJECT_UNLOCK (self);
489 gst_aggregator_reset_flow_values (GstAggregator * self)
491 GST_OBJECT_LOCK (self);
492 self->priv->send_stream_start = TRUE;
493 self->priv->send_segment = TRUE;
494 gst_segment_init (&self->segment, GST_FORMAT_TIME);
495 self->priv->first_buffer = TRUE;
496 GST_OBJECT_UNLOCK (self);
500 gst_aggregator_push_mandatory_events (GstAggregator * self)
502 GstAggregatorPrivate *priv = self->priv;
503 GstEvent *segment = NULL;
504 GstEvent *tags = NULL;
506 if (self->priv->send_stream_start) {
509 GST_INFO_OBJECT (self, "pushing stream start");
510 /* stream-start (FIXME: create id based on input ids) */
511 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
512 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
513 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
515 self->priv->send_stream_start = FALSE;
518 if (self->priv->srccaps) {
520 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
521 self->priv->srccaps);
522 if (!gst_pad_push_event (self->srcpad,
523 gst_event_new_caps (self->priv->srccaps))) {
524 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
526 gst_caps_unref (self->priv->srccaps);
527 self->priv->srccaps = NULL;
530 GST_OBJECT_LOCK (self);
531 if (self->priv->send_segment && !self->priv->flush_seeking) {
532 segment = gst_event_new_segment (&self->segment);
534 if (!self->priv->seqnum)
535 self->priv->seqnum = gst_event_get_seqnum (segment);
537 gst_event_set_seqnum (segment, self->priv->seqnum);
538 self->priv->send_segment = FALSE;
540 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
543 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
544 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
545 priv->tags_changed = FALSE;
547 GST_OBJECT_UNLOCK (self);
550 gst_pad_push_event (self->srcpad, segment);
552 gst_pad_push_event (self->srcpad, tags);
557 * gst_aggregator_set_src_caps:
558 * @self: The #GstAggregator
559 * @caps: The #GstCaps to set on the src pad.
561 * Sets the caps to be used on the src pad.
564 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
566 GST_PAD_STREAM_LOCK (self->srcpad);
567 gst_caps_replace (&self->priv->srccaps, caps);
568 gst_aggregator_push_mandatory_events (self);
569 GST_PAD_STREAM_UNLOCK (self->srcpad);
573 * gst_aggregator_finish_buffer:
574 * @self: The #GstAggregator
575 * @buffer: (transfer full): the #GstBuffer to push.
577 * This method will push the provided output buffer downstream. If needed,
578 * mandatory events such as stream-start, caps, and segment events will be
579 * sent before pushing the buffer.
582 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
584 gst_aggregator_push_mandatory_events (self);
586 GST_OBJECT_LOCK (self);
587 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
588 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
589 GST_OBJECT_UNLOCK (self);
590 return gst_pad_push (self->srcpad, buffer);
592 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
593 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
594 GST_OBJECT_UNLOCK (self);
595 gst_buffer_unref (buffer);
601 gst_aggregator_push_eos (GstAggregator * self)
604 gst_aggregator_push_mandatory_events (self);
606 event = gst_event_new_eos ();
608 GST_OBJECT_LOCK (self);
609 self->priv->send_eos = FALSE;
610 gst_event_set_seqnum (event, self->priv->seqnum);
611 GST_OBJECT_UNLOCK (self);
613 gst_pad_push_event (self->srcpad, event);
617 gst_aggregator_get_next_time (GstAggregator * self)
619 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
621 if (klass->get_next_time)
622 return klass->get_next_time (self);
624 return GST_CLOCK_TIME_NONE;
628 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
630 GstClockTime latency;
638 latency = gst_aggregator_get_latency_unlocked (self);
640 if (gst_aggregator_check_pads_ready (self)) {
641 GST_DEBUG_OBJECT (self, "all pads have data");
647 /* Before waiting, check if we're actually still running */
648 if (!self->priv->running || !self->priv->send_eos) {
654 start = gst_aggregator_get_next_time (self);
656 /* If we're not live, or if we use the running time
657 * of the first buffer as start time, we wait until
658 * all pads have buffers.
659 * Otherwise (i.e. if we are live!), we wait on the clock
660 * and if a pad does not have a buffer in time we ignore
663 GST_OBJECT_LOCK (self);
664 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
665 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
666 !GST_CLOCK_TIME_IS_VALID (start) ||
667 (self->priv->first_buffer
668 && self->priv->start_time_selection ==
669 GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
670 /* We wake up here when something happened, and below
671 * then check if we're ready now. If we return FALSE,
672 * we will be directly called again.
674 GST_OBJECT_UNLOCK (self);
677 GstClockTime base_time, time;
679 GstClockReturn status;
680 GstClockTimeDiff jitter;
682 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
683 GST_TIME_ARGS (start));
685 base_time = GST_ELEMENT_CAST (self)->base_time;
686 clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
687 GST_OBJECT_UNLOCK (self);
689 time = base_time + start;
692 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
693 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
694 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
695 GST_TIME_ARGS (time),
696 GST_TIME_ARGS (base_time),
697 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
698 GST_TIME_ARGS (gst_clock_get_time (clock)));
700 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
701 gst_object_unref (clock);
705 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
708 if (self->priv->aggregate_id) {
709 gst_clock_id_unref (self->priv->aggregate_id);
710 self->priv->aggregate_id = NULL;
713 GST_DEBUG_OBJECT (self,
714 "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
715 status, GST_STIME_ARGS (jitter));
718 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
725 res = gst_aggregator_check_pads_ready (self);
732 check_events (GstAggregator * self, GstAggregatorPad * pad, gpointer user_data)
734 GstEvent *event = NULL;
735 GstAggregatorClass *klass = NULL;
736 gboolean *processed_event = user_data;
742 if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
743 pad->priv->pending_eos = FALSE;
744 pad->priv->eos = TRUE;
746 if (GST_IS_EVENT (g_queue_peek_tail (&pad->priv->buffers))) {
747 event = g_queue_pop_tail (&pad->priv->buffers);
748 PAD_BROADCAST_EVENT (pad);
753 *processed_event = TRUE;
755 klass = GST_AGGREGATOR_GET_CLASS (self);
757 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
758 klass->sink_event (self, pad, event);
760 } while (event != NULL);
766 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
767 GstFlowReturn flow_return, gboolean full)
772 if (flow_return == GST_FLOW_NOT_LINKED)
773 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
775 aggpad->priv->flow_return = flow_return;
777 item = g_queue_peek_head_link (&aggpad->priv->buffers);
779 GList *next = item->next;
781 /* In partial flush, we do like the pad, we get rid of non-sticky events
784 if (full || GST_IS_BUFFER (item->data) ||
785 GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
786 GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
787 !GST_EVENT_IS_STICKY (item->data)) {
788 gst_mini_object_unref (item->data);
789 g_queue_delete_link (&aggpad->priv->buffers, item);
793 aggpad->priv->num_buffers = 0;
795 PAD_BROADCAST_EVENT (aggpad);
800 gst_aggregator_aggregate_func (GstAggregator * self)
802 GstAggregatorPrivate *priv = self->priv;
803 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
804 gboolean timeout = FALSE;
806 if (self->priv->running == FALSE) {
807 GST_DEBUG_OBJECT (self, "Not running anymore");
811 GST_LOG_OBJECT (self, "Checking aggregate");
812 while (priv->send_eos && priv->running) {
813 GstFlowReturn flow_return;
814 gboolean processed_event = FALSE;
816 gst_aggregator_iterate_sinkpads (self, check_events, NULL);
818 if (!gst_aggregator_wait_and_check (self, &timeout))
821 gst_aggregator_iterate_sinkpads (self, check_events, &processed_event);
825 GST_TRACE_OBJECT (self, "Actually aggregating!");
826 flow_return = klass->aggregate (self, timeout);
828 GST_OBJECT_LOCK (self);
829 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
830 /* We don't want to set the pads to flushing, but we want to
831 * stop the thread, so just break here */
832 GST_OBJECT_UNLOCK (self);
835 GST_OBJECT_UNLOCK (self);
837 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
838 gst_aggregator_push_eos (self);
841 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
843 if (flow_return != GST_FLOW_OK) {
846 GST_OBJECT_LOCK (self);
847 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
848 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
850 gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
852 GST_OBJECT_UNLOCK (self);
857 /* Pause the task here, the only ways to get here are:
858 * 1) We're stopping, in which case the task is stopped anyway
859 * 2) We got a flow error above, in which case it might take
860 * some time to forward the flow return upstream and we
861 * would otherwise call the task function over and over
862 * again without doing anything
864 gst_pad_pause_task (self->srcpad);
868 gst_aggregator_start (GstAggregator * self)
870 GstAggregatorClass *klass;
873 self->priv->send_stream_start = TRUE;
874 self->priv->send_segment = TRUE;
875 self->priv->send_eos = TRUE;
876 self->priv->srccaps = NULL;
878 klass = GST_AGGREGATOR_GET_CLASS (self);
881 result = klass->start (self);
889 _check_pending_flush_stop (GstAggregatorPad * pad)
894 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
901 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
905 GST_INFO_OBJECT (self, "%s srcpad task",
906 flush_start ? "Pausing" : "Stopping");
909 self->priv->running = FALSE;
910 SRC_BROADCAST (self);
914 res = gst_pad_push_event (self->srcpad, flush_start);
917 gst_pad_stop_task (self->srcpad);
923 gst_aggregator_start_srcpad_task (GstAggregator * self)
925 GST_INFO_OBJECT (self, "Starting srcpad task");
927 self->priv->running = TRUE;
928 gst_pad_start_task (GST_PAD (self->srcpad),
929 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
933 gst_aggregator_flush (GstAggregator * self)
935 GstFlowReturn ret = GST_FLOW_OK;
936 GstAggregatorPrivate *priv = self->priv;
937 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
939 GST_DEBUG_OBJECT (self, "Flushing everything");
940 GST_OBJECT_LOCK (self);
941 priv->send_segment = TRUE;
942 priv->flush_seeking = FALSE;
943 priv->tags_changed = FALSE;
944 GST_OBJECT_UNLOCK (self);
946 ret = klass->flush (self);
952 /* Called with GstAggregator's object lock held */
955 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
958 GstAggregatorPad *tmppad;
960 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
961 tmppad = (GstAggregatorPad *) tmp->data;
963 if (_check_pending_flush_stop (tmppad) == FALSE) {
964 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
965 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
974 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
977 GstAggregatorPrivate *priv = self->priv;
978 GstAggregatorPadPrivate *padpriv = aggpad->priv;
980 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
982 PAD_FLUSH_LOCK (aggpad);
984 if (padpriv->pending_flush_start) {
985 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
987 padpriv->pending_flush_start = FALSE;
988 padpriv->pending_flush_stop = TRUE;
992 GST_OBJECT_LOCK (self);
993 if (priv->flush_seeking) {
994 /* If flush_seeking we forward the first FLUSH_START */
995 if (priv->pending_flush_start) {
996 priv->pending_flush_start = FALSE;
997 GST_OBJECT_UNLOCK (self);
999 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
1000 gst_aggregator_stop_srcpad_task (self, event);
1002 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
1003 GST_PAD_STREAM_LOCK (self->srcpad);
1004 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
1007 GST_OBJECT_UNLOCK (self);
1008 gst_event_unref (event);
1011 GST_OBJECT_UNLOCK (self);
1012 gst_event_unref (event);
1014 PAD_FLUSH_UNLOCK (aggpad);
1017 /* Must be called with the the PAD_LOCK held */
1019 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1022 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->head_position) &&
1023 aggpad->clip_segment.format == GST_FORMAT_TIME)
1024 aggpad->priv->head_time =
1025 gst_segment_to_running_time (&aggpad->clip_segment,
1026 GST_FORMAT_TIME, aggpad->priv->head_position);
1028 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
1030 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_position) &&
1031 aggpad->segment.format == GST_FORMAT_TIME)
1032 aggpad->priv->tail_time =
1033 gst_segment_to_running_time (&aggpad->segment,
1034 GST_FORMAT_TIME, aggpad->priv->tail_position);
1036 aggpad->priv->tail_time = aggpad->priv->head_time;
1039 if (aggpad->priv->head_time == GST_CLOCK_TIME_NONE ||
1040 aggpad->priv->tail_time == GST_CLOCK_TIME_NONE) {
1041 aggpad->priv->time_level = 0;
1045 if (aggpad->priv->tail_time > aggpad->priv->head_time)
1046 aggpad->priv->time_level = 0;
1048 aggpad->priv->time_level = aggpad->priv->head_time -
1049 aggpad->priv->tail_time;
1053 /* GstAggregator vmethods default implementations */
1055 gst_aggregator_default_sink_event (GstAggregator * self,
1056 GstAggregatorPad * aggpad, GstEvent * event)
1058 gboolean res = TRUE;
1059 GstPad *pad = GST_PAD (aggpad);
1060 GstAggregatorPrivate *priv = self->priv;
1062 switch (GST_EVENT_TYPE (event)) {
1063 case GST_EVENT_FLUSH_START:
1065 gst_aggregator_flush_start (self, aggpad, event);
1066 /* We forward only in one case: right after flush_seeking */
1070 case GST_EVENT_FLUSH_STOP:
1072 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
1074 gst_aggregator_pad_flush (aggpad, self);
1075 GST_OBJECT_LOCK (self);
1076 if (priv->flush_seeking) {
1077 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1078 if (gst_aggregator_all_flush_stop_received_locked (self)) {
1079 GST_OBJECT_UNLOCK (self);
1080 /* That means we received FLUSH_STOP/FLUSH_STOP on
1081 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1082 gst_aggregator_flush (self);
1083 gst_pad_push_event (self->srcpad, event);
1086 priv->send_eos = TRUE;
1087 SRC_BROADCAST (self);
1090 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1091 GST_PAD_STREAM_UNLOCK (self->srcpad);
1092 gst_aggregator_start_srcpad_task (self);
1094 GST_OBJECT_UNLOCK (self);
1097 GST_OBJECT_UNLOCK (self);
1100 /* We never forward the event */
1105 GST_DEBUG_OBJECT (aggpad, "EOS");
1107 /* We still have a buffer, and we don't want the subclass to have to
1108 * check for it. Mark pending_eos, eos will be set when steal_buffer is
1113 if (gst_aggregator_pad_queue_is_empty (aggpad)) {
1114 aggpad->priv->eos = TRUE;
1116 aggpad->priv->pending_eos = TRUE;
1118 PAD_UNLOCK (aggpad);
1120 SRC_BROADCAST (self);
1124 case GST_EVENT_SEGMENT:
1127 GST_OBJECT_LOCK (aggpad);
1128 gst_event_copy_segment (event, &aggpad->segment);
1129 update_time_level (aggpad, FALSE);
1130 GST_OBJECT_UNLOCK (aggpad);
1131 PAD_UNLOCK (aggpad);
1133 GST_OBJECT_LOCK (self);
1134 self->priv->seqnum = gst_event_get_seqnum (event);
1135 GST_OBJECT_UNLOCK (self);
1138 case GST_EVENT_STREAM_START:
1144 GstClockTime pts, endpts;
1145 GstClockTime duration;
1148 gst_event_parse_gap (event, &pts, &duration);
1149 gapbuf = gst_buffer_new ();
1151 if (GST_CLOCK_TIME_IS_VALID (duration))
1152 endpts = pts + duration;
1154 endpts = GST_CLOCK_TIME_NONE;
1156 GST_OBJECT_LOCK (aggpad);
1157 res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1159 GST_OBJECT_UNLOCK (aggpad);
1162 GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1166 if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1167 duration = endpts - pts;
1169 duration = GST_CLOCK_TIME_NONE;
1171 GST_BUFFER_PTS (gapbuf) = pts;
1172 GST_BUFFER_DURATION (gapbuf) = duration;
1173 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1174 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1176 if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1178 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1188 gst_event_parse_tag (event, &tags);
1190 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1191 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1192 gst_event_unref (event);
1204 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1205 return gst_pad_event_default (pad, GST_OBJECT (self), event);
1208 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1210 gst_event_unref (event);
1215 static inline gboolean
1216 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1217 gpointer unused_udata)
1219 gst_aggregator_pad_flush (pad, self);
1225 gst_aggregator_stop (GstAggregator * agg)
1227 GstAggregatorClass *klass;
1230 gst_aggregator_reset_flow_values (agg);
1232 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1234 klass = GST_AGGREGATOR_GET_CLASS (agg);
1237 result = klass->stop (agg);
1241 agg->priv->has_peer_latency = FALSE;
1242 agg->priv->peer_latency_live = FALSE;
1243 agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1245 if (agg->priv->tags)
1246 gst_tag_list_unref (agg->priv->tags);
1247 agg->priv->tags = NULL;
1252 /* GstElement vmethods implementations */
1253 static GstStateChangeReturn
1254 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1256 GstStateChangeReturn ret;
1257 GstAggregator *self = GST_AGGREGATOR (element);
1259 switch (transition) {
1260 case GST_STATE_CHANGE_READY_TO_PAUSED:
1261 if (!gst_aggregator_start (self))
1269 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1270 transition)) == GST_STATE_CHANGE_FAILURE)
1274 switch (transition) {
1275 case GST_STATE_CHANGE_PAUSED_TO_READY:
1276 if (!gst_aggregator_stop (self)) {
1277 /* What to do in this case? Error out? */
1278 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1290 GST_ERROR_OBJECT (element, "parent failed state change");
1295 GST_ERROR_OBJECT (element, "Subclass failed to start");
1296 return GST_STATE_CHANGE_FAILURE;
1301 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1303 GstAggregator *self = GST_AGGREGATOR (element);
1304 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1306 GST_INFO_OBJECT (pad, "Removing pad");
1309 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1310 gst_element_remove_pad (element, pad);
1312 self->priv->has_peer_latency = FALSE;
1313 SRC_BROADCAST (self);
1317 static GstAggregatorPad *
1318 gst_aggregator_default_create_new_pad (GstAggregator * self,
1319 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1321 GstAggregatorPad *agg_pad;
1322 GstAggregatorPrivate *priv = self->priv;
1326 if (templ->direction != GST_PAD_SINK ||
1327 g_strcmp0 (templ->name_template, "sink_%u") != 0)
1330 GST_OBJECT_LOCK (self);
1331 if (req_name == NULL || strlen (req_name) < 6
1332 || !g_str_has_prefix (req_name, "sink_")) {
1333 /* no name given when requesting the pad, use next available int */
1334 serial = ++priv->max_padserial;
1336 /* parse serial number from requested padname */
1337 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1338 if (serial > priv->max_padserial)
1339 priv->max_padserial = serial;
1342 name = g_strdup_printf ("sink_%u", serial);
1343 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1344 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1347 GST_OBJECT_UNLOCK (self);
1354 GST_WARNING_OBJECT (self, "request new pad that is not a SINK pad\n");
1360 gst_aggregator_request_new_pad (GstElement * element,
1361 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1363 GstAggregator *self;
1364 GstAggregatorPad *agg_pad;
1365 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1366 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1368 self = GST_AGGREGATOR (element);
1370 agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1372 GST_ERROR_OBJECT (element, "Couldn't create new pad");
1376 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1377 self->priv->has_peer_latency = FALSE;
1380 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1382 /* add the pad to the element */
1383 gst_element_add_pad (element, GST_PAD (agg_pad));
1385 return GST_PAD (agg_pad);
1388 /* Must be called with SRC_LOCK held */
1391 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1393 gboolean query_ret, live;
1394 GstClockTime our_latency, min, max;
1396 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1399 GST_WARNING_OBJECT (self, "Latency query failed");
1403 gst_query_parse_latency (query, &live, &min, &max);
1405 our_latency = self->priv->latency;
1407 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1408 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1409 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1413 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1414 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1415 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1416 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1417 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1421 self->priv->peer_latency_live = live;
1422 self->priv->peer_latency_min = min;
1423 self->priv->peer_latency_max = max;
1424 self->priv->has_peer_latency = TRUE;
1428 min += self->priv->sub_latency_min;
1429 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1430 && GST_CLOCK_TIME_IS_VALID (max))
1431 max += self->priv->sub_latency_max + our_latency;
1433 max = GST_CLOCK_TIME_NONE;
1435 SRC_BROADCAST (self);
1437 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1438 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1440 gst_query_set_latency (query, live, min, max);
1446 * MUST be called with the src_lock held.
1448 * See gst_aggregator_get_latency() for doc
1451 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1453 GstClockTime latency;
1455 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1457 if (!self->priv->has_peer_latency) {
1458 GstQuery *query = gst_query_new_latency ();
1461 ret = gst_aggregator_query_latency_unlocked (self, query);
1462 gst_query_unref (query);
1464 return GST_CLOCK_TIME_NONE;
1467 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1468 return GST_CLOCK_TIME_NONE;
1470 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1471 latency = self->priv->peer_latency_min;
1474 latency += self->priv->latency;
1475 latency += self->priv->sub_latency_min;
1481 * gst_aggregator_get_latency:
1482 * @self: a #GstAggregator
1484 * Retrieves the latency values reported by @self in response to the latency
1485 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1486 * will not wait for the clock.
1488 * Typically only called by subclasses.
1490 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1493 gst_aggregator_get_latency (GstAggregator * self)
1498 ret = gst_aggregator_get_latency_unlocked (self);
1505 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1507 GstAggregator *self = GST_AGGREGATOR (element);
1509 GST_STATE_LOCK (element);
1510 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1511 GST_STATE (element) < GST_STATE_PAUSED) {
1515 GstSeekType start_type, stop_type;
1518 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1519 &start, &stop_type, &stop);
1521 GST_OBJECT_LOCK (self);
1522 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1523 stop_type, stop, NULL);
1524 self->priv->seqnum = gst_event_get_seqnum (event);
1525 self->priv->first_buffer = FALSE;
1526 GST_OBJECT_UNLOCK (self);
1528 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1530 GST_STATE_UNLOCK (element);
1533 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1538 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1540 gboolean res = TRUE;
1542 switch (GST_QUERY_TYPE (query)) {
1543 case GST_QUERY_SEEKING:
1547 /* don't pass it along as some (file)sink might claim it does
1548 * whereas with a collectpads in between that will not likely work */
1549 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1550 gst_query_set_seeking (query, format, FALSE, 0, -1);
1555 case GST_QUERY_LATENCY:
1557 res = gst_aggregator_query_latency_unlocked (self, query);
1561 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1568 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1570 EventData *evdata = user_data;
1571 gboolean ret = TRUE;
1572 GstPad *peer = gst_pad_get_peer (pad);
1573 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1576 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1577 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1580 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1581 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1582 gst_object_unref (peer);
1587 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1588 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1590 GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1592 if (gst_pad_query (peer, seeking)) {
1595 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1597 if (seekable == FALSE) {
1598 GST_INFO_OBJECT (pad,
1599 "Source not seekable, We failed but it does not matter!");
1604 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1607 gst_query_unref (seeking);
1610 if (evdata->flush) {
1612 aggpad->priv->pending_flush_start = FALSE;
1613 aggpad->priv->pending_flush_stop = FALSE;
1614 PAD_UNLOCK (aggpad);
1617 evdata->one_actually_seeked = TRUE;
1620 evdata->result &= ret;
1622 /* Always send to all pads */
1627 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1628 GstEvent * event, gboolean flush, gboolean only_to_active_pads)
1632 evdata.event = event;
1633 evdata.result = TRUE;
1634 evdata.flush = flush;
1635 evdata.one_actually_seeked = FALSE;
1636 evdata.only_to_active_pads = only_to_active_pads;
1638 /* We first need to set all pads as flushing in a first pass
1639 * as flush_start flush_stop is sometimes sent synchronously
1640 * while we send the seek event */
1644 GST_OBJECT_LOCK (self);
1645 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1646 GstAggregatorPad *pad = l->data;
1649 pad->priv->pending_flush_start = TRUE;
1650 pad->priv->pending_flush_stop = FALSE;
1653 GST_OBJECT_UNLOCK (self);
1656 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1658 gst_event_unref (event);
1664 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1669 GstSeekType start_type, stop_type;
1673 GstAggregatorPrivate *priv = self->priv;
1675 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1676 &start, &stop_type, &stop);
1678 GST_INFO_OBJECT (self, "starting SEEK");
1680 flush = flags & GST_SEEK_FLAG_FLUSH;
1682 GST_OBJECT_LOCK (self);
1684 priv->pending_flush_start = TRUE;
1685 priv->flush_seeking = TRUE;
1688 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1689 stop_type, stop, NULL);
1691 /* Seeking sets a position */
1692 self->priv->first_buffer = FALSE;
1693 GST_OBJECT_UNLOCK (self);
1695 /* forward the seek upstream */
1697 gst_aggregator_forward_event_to_all_sinkpads (self, event, flush, FALSE);
1700 if (!evdata.result || !evdata.one_actually_seeked) {
1701 GST_OBJECT_LOCK (self);
1702 priv->flush_seeking = FALSE;
1703 priv->pending_flush_start = FALSE;
1704 GST_OBJECT_UNLOCK (self);
1707 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1709 return evdata.result;
1713 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1716 gboolean res = TRUE;
1718 switch (GST_EVENT_TYPE (event)) {
1719 case GST_EVENT_SEEK:
1721 gst_event_ref (event);
1722 res = gst_aggregator_do_seek (self, event);
1723 gst_event_unref (event);
1727 case GST_EVENT_NAVIGATION:
1729 /* navigation is rather pointless. */
1731 gst_event_unref (event);
1740 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
1741 * they will receive a QOS event that has earliest_time=0 (because we can't
1742 * have negative timestamps), and consider their buffer as too late */
1744 gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE,
1745 GST_EVENT_TYPE (event) == GST_EVENT_QOS);
1746 res = evdata.result;
1753 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1756 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1758 return klass->src_event (GST_AGGREGATOR (parent), event);
1762 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1765 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1767 return klass->src_query (GST_AGGREGATOR (parent), query);
1771 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1772 GstObject * parent, GstPadMode mode, gboolean active)
1774 GstAggregator *self = GST_AGGREGATOR (parent);
1775 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1777 if (klass->src_activate) {
1778 if (klass->src_activate (self, mode, active) == FALSE) {
1783 if (active == TRUE) {
1785 case GST_PAD_MODE_PUSH:
1787 GST_INFO_OBJECT (pad, "Activating pad!");
1788 gst_aggregator_start_srcpad_task (self);
1793 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1800 GST_INFO_OBJECT (self, "Deactivating srcpad");
1801 gst_aggregator_stop_srcpad_task (self, FALSE);
1807 gst_aggregator_default_sink_query (GstAggregator * self,
1808 GstAggregatorPad * aggpad, GstQuery * query)
1810 GstPad *pad = GST_PAD (aggpad);
1812 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1816 gst_aggregator_finalize (GObject * object)
1818 GstAggregator *self = (GstAggregator *) object;
1820 g_mutex_clear (&self->priv->src_lock);
1821 g_cond_clear (&self->priv->src_cond);
1823 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1827 * gst_aggregator_set_latency_property:
1828 * @agg: a #GstAggregator
1829 * @latency: the new latency value (in nanoseconds).
1831 * Sets the new latency value to @latency. This value is used to limit the
1832 * amount of time a pad waits for data to appear before considering the pad
1836 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1840 g_return_if_fail (GST_IS_AGGREGATOR (self));
1841 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1844 changed = (self->priv->latency != latency);
1849 GST_OBJECT_LOCK (self);
1850 /* First lock all the pads */
1851 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1852 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1856 self->priv->latency = latency;
1858 SRC_BROADCAST (self);
1860 /* Now wake up the pads */
1861 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1862 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1863 PAD_BROADCAST_EVENT (aggpad);
1864 PAD_UNLOCK (aggpad);
1866 GST_OBJECT_UNLOCK (self);
1872 gst_element_post_message (GST_ELEMENT_CAST (self),
1873 gst_message_new_latency (GST_OBJECT_CAST (self)));
1877 * gst_aggregator_get_latency_property:
1878 * @agg: a #GstAggregator
1880 * Gets the latency value. See gst_aggregator_set_latency for
1883 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1884 * before a pad is deemed unresponsive. A value of -1 means an
1888 gst_aggregator_get_latency_property (GstAggregator * agg)
1892 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1894 GST_OBJECT_LOCK (agg);
1895 res = agg->priv->latency;
1896 GST_OBJECT_UNLOCK (agg);
1902 gst_aggregator_set_property (GObject * object, guint prop_id,
1903 const GValue * value, GParamSpec * pspec)
1905 GstAggregator *agg = GST_AGGREGATOR (object);
1909 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1911 case PROP_START_TIME_SELECTION:
1912 agg->priv->start_time_selection = g_value_get_enum (value);
1914 case PROP_START_TIME:
1915 agg->priv->start_time = g_value_get_uint64 (value);
1918 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1924 gst_aggregator_get_property (GObject * object, guint prop_id,
1925 GValue * value, GParamSpec * pspec)
1927 GstAggregator *agg = GST_AGGREGATOR (object);
1931 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1933 case PROP_START_TIME_SELECTION:
1934 g_value_set_enum (value, agg->priv->start_time_selection);
1936 case PROP_START_TIME:
1937 g_value_set_uint64 (value, agg->priv->start_time);
1940 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1945 /* GObject vmethods implementations */
1947 gst_aggregator_class_init (GstAggregatorClass * klass)
1949 GObjectClass *gobject_class = (GObjectClass *) klass;
1950 GstElementClass *gstelement_class = (GstElementClass *) klass;
1952 aggregator_parent_class = g_type_class_peek_parent (klass);
1953 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1955 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1956 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1958 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1960 klass->sink_event = gst_aggregator_default_sink_event;
1961 klass->sink_query = gst_aggregator_default_sink_query;
1963 klass->src_event = gst_aggregator_default_src_event;
1964 klass->src_query = gst_aggregator_default_src_query;
1966 klass->create_new_pad = gst_aggregator_default_create_new_pad;
1968 gstelement_class->request_new_pad =
1969 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1970 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1971 gstelement_class->release_pad =
1972 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1973 gstelement_class->change_state =
1974 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1976 gobject_class->set_property = gst_aggregator_set_property;
1977 gobject_class->get_property = gst_aggregator_get_property;
1978 gobject_class->finalize = gst_aggregator_finalize;
1980 g_object_class_install_property (gobject_class, PROP_LATENCY,
1981 g_param_spec_int64 ("latency", "Buffer latency",
1982 "Additional latency in live mode to allow upstream "
1983 "to take longer to produce buffers for the current "
1984 "position (in nanoseconds)", 0,
1985 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1986 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1988 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
1989 g_param_spec_enum ("start-time-selection", "Start Time Selection",
1990 "Decides which start time is output",
1991 gst_aggregator_start_time_selection_get_type (),
1992 DEFAULT_START_TIME_SELECTION,
1993 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1995 g_object_class_install_property (gobject_class, PROP_START_TIME,
1996 g_param_spec_uint64 ("start-time", "Start Time",
1997 "Start time to use if start-time-selection=set", 0,
1999 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2001 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
2005 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
2007 GstPadTemplate *pad_template;
2008 GstAggregatorPrivate *priv;
2010 g_return_if_fail (klass->aggregate != NULL);
2013 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2014 GstAggregatorPrivate);
2019 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2020 g_return_if_fail (pad_template != NULL);
2022 priv->max_padserial = -1;
2023 priv->tags_changed = FALSE;
2025 self->priv->peer_latency_live = FALSE;
2026 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2027 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2028 self->priv->has_peer_latency = FALSE;
2029 gst_aggregator_reset_flow_values (self);
2031 self->srcpad = gst_pad_new_from_template (pad_template, "src");
2033 gst_pad_set_event_function (self->srcpad,
2034 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2035 gst_pad_set_query_function (self->srcpad,
2036 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2037 gst_pad_set_activatemode_function (self->srcpad,
2038 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2040 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2042 self->priv->latency = DEFAULT_LATENCY;
2043 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2044 self->priv->start_time = DEFAULT_START_TIME;
2046 g_mutex_init (&self->priv->src_lock);
2047 g_cond_init (&self->priv->src_cond);
2050 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2051 * method to get to the padtemplates */
2053 gst_aggregator_get_type (void)
2055 static volatile gsize type = 0;
2057 if (g_once_init_enter (&type)) {
2059 static const GTypeInfo info = {
2060 sizeof (GstAggregatorClass),
2063 (GClassInitFunc) gst_aggregator_class_init,
2066 sizeof (GstAggregator),
2068 (GInstanceInitFunc) gst_aggregator_init,
2071 _type = g_type_register_static (GST_TYPE_ELEMENT,
2072 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2073 g_once_init_leave (&type, _type);
2078 /* Must be called with SRC lock and PAD lock held */
2080 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2082 /* Empty queue always has space */
2083 if (g_queue_get_length (&aggpad->priv->buffers) == 0)
2086 /* We also want at least two buffers, one is being processed and one is ready
2087 * for the next iteration when we operate in live mode. */
2088 if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2091 /* zero latency, if there is a buffer, it's full */
2092 if (self->priv->latency == 0)
2095 /* Allow no more buffers than the latency */
2096 return (aggpad->priv->time_level <= self->priv->latency);
2099 /* Must be called with the PAD_LOCK held */
2101 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2103 GstClockTime timestamp;
2105 if (GST_BUFFER_DTS_IS_VALID (buffer))
2106 timestamp = GST_BUFFER_DTS (buffer);
2108 timestamp = GST_BUFFER_PTS (buffer);
2110 if (timestamp == GST_CLOCK_TIME_NONE) {
2112 timestamp = aggpad->priv->head_position;
2114 timestamp = aggpad->priv->tail_position;
2118 if (GST_BUFFER_DURATION_IS_VALID (buffer))
2119 timestamp += GST_BUFFER_DURATION (buffer);
2122 aggpad->priv->head_position = timestamp;
2124 aggpad->priv->tail_position = timestamp;
2126 update_time_level (aggpad, head);
2129 static GstFlowReturn
2130 gst_aggregator_pad_chain_internal (GstAggregator * self,
2131 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2133 GstBuffer *actual_buf = buffer;
2134 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
2135 GstFlowReturn flow_return;
2136 GstClockTime buf_pts;
2138 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2140 PAD_FLUSH_LOCK (aggpad);
2143 flow_return = aggpad->priv->flow_return;
2144 if (flow_return != GST_FLOW_OK)
2147 if (aggpad->priv->pending_eos == TRUE)
2150 PAD_UNLOCK (aggpad);
2152 if (aggclass->clip && head) {
2153 aggclass->clip (self, aggpad, buffer, &actual_buf);
2156 if (actual_buf == NULL) {
2157 GST_LOG_OBJECT (actual_buf, "Buffer dropped by clip function");
2161 buf_pts = GST_BUFFER_PTS (actual_buf);
2163 aggpad->priv->first_buffer = FALSE;
2167 GST_OBJECT_LOCK (self);
2169 if (gst_aggregator_pad_has_space (self, aggpad)
2170 && aggpad->priv->flow_return == GST_FLOW_OK) {
2172 g_queue_push_head (&aggpad->priv->buffers, actual_buf);
2174 g_queue_push_tail (&aggpad->priv->buffers, actual_buf);
2175 apply_buffer (aggpad, actual_buf, head);
2176 aggpad->priv->num_buffers++;
2177 actual_buf = buffer = NULL;
2178 SRC_BROADCAST (self);
2182 flow_return = aggpad->priv->flow_return;
2183 if (flow_return != GST_FLOW_OK) {
2184 GST_OBJECT_UNLOCK (self);
2188 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2189 GST_OBJECT_UNLOCK (self);
2191 PAD_WAIT_EVENT (aggpad);
2193 PAD_UNLOCK (aggpad);
2196 if (self->priv->first_buffer) {
2197 GstClockTime start_time;
2199 switch (self->priv->start_time_selection) {
2200 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2204 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2205 GST_OBJECT_LOCK (aggpad);
2206 if (aggpad->segment.format == GST_FORMAT_TIME) {
2207 start_time = buf_pts;
2208 if (start_time != -1) {
2209 start_time = MAX (start_time, aggpad->segment.start);
2211 gst_segment_to_running_time (&aggpad->segment, GST_FORMAT_TIME,
2216 GST_WARNING_OBJECT (aggpad,
2217 "Ignoring request of selecting the first start time "
2218 "as the segment is a %s segment instead of a time segment",
2219 gst_format_get_name (aggpad->segment.format));
2221 GST_OBJECT_UNLOCK (aggpad);
2223 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2224 start_time = self->priv->start_time;
2225 if (start_time == -1)
2230 if (start_time != -1) {
2231 if (self->segment.position == -1)
2232 self->segment.position = start_time;
2234 self->segment.position = MIN (start_time, self->segment.position);
2236 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2237 GST_TIME_ARGS (start_time));
2241 PAD_UNLOCK (aggpad);
2242 GST_OBJECT_UNLOCK (self);
2247 PAD_FLUSH_UNLOCK (aggpad);
2249 GST_DEBUG_OBJECT (aggpad, "Done chaining");
2254 PAD_UNLOCK (aggpad);
2255 PAD_FLUSH_UNLOCK (aggpad);
2257 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2258 gst_flow_get_name (flow_return));
2260 gst_buffer_unref (buffer);
2265 PAD_UNLOCK (aggpad);
2266 PAD_FLUSH_UNLOCK (aggpad);
2268 gst_buffer_unref (buffer);
2269 GST_DEBUG_OBJECT (aggpad, "We are EOS already...");
2271 return GST_FLOW_EOS;
2274 static GstFlowReturn
2275 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2277 return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2278 GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2282 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2285 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2286 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2288 if (GST_QUERY_IS_SERIALIZED (query)) {
2291 while (!gst_aggregator_pad_queue_is_empty (aggpad)
2292 && aggpad->priv->flow_return == GST_FLOW_OK) {
2293 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2294 PAD_WAIT_EVENT (aggpad);
2297 if (aggpad->priv->flow_return != GST_FLOW_OK)
2300 PAD_UNLOCK (aggpad);
2303 return klass->sink_query (GST_AGGREGATOR (parent),
2304 GST_AGGREGATOR_PAD (pad), query);
2307 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2308 gst_flow_get_name (aggpad->priv->flow_return));
2309 PAD_UNLOCK (aggpad);
2313 static GstFlowReturn
2314 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2317 GstFlowReturn ret = GST_FLOW_OK;
2318 GstAggregator *self = GST_AGGREGATOR (parent);
2319 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2320 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2322 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
2323 /* && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE */ ) {
2327 if (aggpad->priv->flow_return != GST_FLOW_OK
2328 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2329 ret = aggpad->priv->flow_return;
2333 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2334 GST_OBJECT_LOCK (aggpad);
2335 gst_event_copy_segment (event, &aggpad->clip_segment);
2336 aggpad->priv->head_position = aggpad->clip_segment.position;
2337 update_time_level (aggpad, TRUE);
2338 GST_OBJECT_UNLOCK (aggpad);
2341 if (!gst_aggregator_pad_queue_is_empty (aggpad) &&
2342 GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2343 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT,
2345 g_queue_push_head (&aggpad->priv->buffers, event);
2347 SRC_BROADCAST (self);
2349 PAD_UNLOCK (aggpad);
2354 gboolean is_caps = (GST_EVENT_TYPE (event) == GST_EVENT_CAPS);
2356 if (!klass->sink_event (self, aggpad, event)) {
2357 /* Copied from GstPad to convert boolean to a GstFlowReturn in
2358 * the event handling func */
2359 ret = is_caps ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_ERROR;
2366 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2367 gst_flow_get_name (aggpad->priv->flow_return));
2368 PAD_UNLOCK (aggpad);
2370 if (GST_EVENT_IS_STICKY (event))
2371 gst_pad_store_sticky_event (pad, event);
2372 gst_event_unref (event);
2378 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2379 GstObject * parent, GstPadMode mode, gboolean active)
2381 GstAggregator *self = GST_AGGREGATOR (parent);
2382 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2384 if (active == FALSE) {
2386 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2387 SRC_BROADCAST (self);
2391 aggpad->priv->flow_return = GST_FLOW_OK;
2392 PAD_BROADCAST_EVENT (aggpad);
2393 PAD_UNLOCK (aggpad);
2399 /***********************************
2400 * GstAggregatorPad implementation *
2401 ************************************/
2402 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2405 gst_aggregator_pad_constructed (GObject * object)
2407 GstPad *pad = GST_PAD (object);
2409 gst_pad_set_chain_function (pad,
2410 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2411 gst_pad_set_event_full_function_full (pad,
2412 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
2413 gst_pad_set_query_function (pad,
2414 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2415 gst_pad_set_activatemode_function (pad,
2416 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2420 gst_aggregator_pad_finalize (GObject * object)
2422 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2424 g_cond_clear (&pad->priv->event_cond);
2425 g_mutex_clear (&pad->priv->flush_lock);
2426 g_mutex_clear (&pad->priv->lock);
2428 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2432 gst_aggregator_pad_dispose (GObject * object)
2434 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2436 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2438 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2442 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2444 GObjectClass *gobject_class = (GObjectClass *) klass;
2446 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2448 gobject_class->constructed = gst_aggregator_pad_constructed;
2449 gobject_class->finalize = gst_aggregator_pad_finalize;
2450 gobject_class->dispose = gst_aggregator_pad_dispose;
2454 gst_aggregator_pad_init (GstAggregatorPad * pad)
2457 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2458 GstAggregatorPadPrivate);
2460 g_queue_init (&pad->priv->buffers);
2461 g_cond_init (&pad->priv->event_cond);
2463 g_mutex_init (&pad->priv->flush_lock);
2464 g_mutex_init (&pad->priv->lock);
2466 gst_aggregator_pad_reset_unlocked (pad);
2470 * gst_aggregator_pad_steal_buffer:
2471 * @pad: the pad to get buffer from
2473 * Steal the ref to the buffer currently queued in @pad.
2475 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2476 * queued. You should unref the buffer after usage.
2479 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2481 GstBuffer *buffer = NULL;
2484 if (GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->buffers)))
2485 buffer = g_queue_pop_tail (&pad->priv->buffers);
2488 apply_buffer (pad, buffer, FALSE);
2489 pad->priv->num_buffers--;
2490 GST_TRACE_OBJECT (pad, "Consuming buffer");
2491 if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
2492 pad->priv->pending_eos = FALSE;
2493 pad->priv->eos = TRUE;
2495 PAD_BROADCAST_EVENT (pad);
2496 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2504 * gst_aggregator_pad_drop_buffer:
2505 * @pad: the pad where to drop any pending buffer
2507 * Drop the buffer currently queued in @pad.
2509 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2512 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2516 buf = gst_aggregator_pad_steal_buffer (pad);
2521 gst_buffer_unref (buf);
2526 * gst_aggregator_pad_get_buffer:
2527 * @pad: the pad to get buffer from
2529 * Returns: (transfer full): A reference to the buffer in @pad or
2530 * NULL if no buffer was queued. You should unref the buffer after
2534 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2536 GstBuffer *buffer = NULL;
2539 buffer = g_queue_peek_tail (&pad->priv->buffers);
2540 /* The tail should always be a buffer, because if it is an event,
2541 * it will be consumed immeditaly in gst_aggregator_steal_buffer */
2543 if (GST_IS_BUFFER (buffer))
2544 gst_buffer_ref (buffer);
2553 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2558 is_eos = pad->priv->eos;
2565 * gst_aggregator_merge_tags:
2566 * @self: a #GstAggregator
2567 * @tags: a #GstTagList to merge
2568 * @mode: the #GstTagMergeMode to use
2570 * Adds tags to so-called pending tags, which will be processed
2571 * before pushing out data downstream.
2573 * Note that this is provided for convenience, and the subclass is
2574 * not required to use this and can still do tag handling on its own.
2579 gst_aggregator_merge_tags (GstAggregator * self,
2580 const GstTagList * tags, GstTagMergeMode mode)
2584 g_return_if_fail (GST_IS_AGGREGATOR (self));
2585 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2587 /* FIXME Check if we can use OBJECT lock here! */
2588 GST_OBJECT_LOCK (self);
2590 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2591 otags = self->priv->tags;
2592 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2594 gst_tag_list_unref (otags);
2595 self->priv->tags_changed = TRUE;
2596 GST_OBJECT_UNLOCK (self);
2600 * gst_aggregator_set_latency:
2601 * @self: a #GstAggregator
2602 * @min_latency: minimum latency
2603 * @max_latency: maximum latency
2605 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2606 * latency is. Will also post a LATENCY message on the bus so the pipeline
2607 * can reconfigure its global latency.
2610 gst_aggregator_set_latency (GstAggregator * self,
2611 GstClockTime min_latency, GstClockTime max_latency)
2613 gboolean changed = FALSE;
2615 g_return_if_fail (GST_IS_AGGREGATOR (self));
2616 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2617 g_return_if_fail (max_latency >= min_latency);
2620 if (self->priv->sub_latency_min != min_latency) {
2621 self->priv->sub_latency_min = min_latency;
2624 if (self->priv->sub_latency_max != max_latency) {
2625 self->priv->sub_latency_max = max_latency;
2630 SRC_BROADCAST (self);
2634 gst_element_post_message (GST_ELEMENT_CAST (self),
2635 gst_message_new_latency (GST_OBJECT_CAST (self)));