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;
215 GstBuffer *clipped_buffer;
217 GstClockTime head_position;
218 GstClockTime tail_position;
219 GstClockTime head_time;
220 GstClockTime tail_time;
221 GstClockTime time_level;
222 GstSegment head_segment; /* segment before the queue */
228 /* This lock prevents a flush start processing happening while
229 * the chain function is also happening.
234 /* Must be called with PAD_LOCK held */
236 gst_aggregator_pad_reset_unlocked (GstAggregatorPad * aggpad)
238 aggpad->priv->pending_eos = FALSE;
239 aggpad->priv->eos = FALSE;
240 aggpad->priv->flow_return = GST_FLOW_OK;
241 GST_OBJECT_LOCK (aggpad);
242 gst_segment_init (&aggpad->segment, GST_FORMAT_UNDEFINED);
243 gst_segment_init (&aggpad->priv->head_segment, GST_FORMAT_UNDEFINED);
244 GST_OBJECT_UNLOCK (aggpad);
245 aggpad->priv->head_position = GST_CLOCK_TIME_NONE;
246 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
247 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
248 aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
249 aggpad->priv->time_level = 0;
250 aggpad->priv->first_buffer = TRUE;
254 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
256 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
259 gst_aggregator_pad_reset_unlocked (aggpad);
263 return klass->flush (aggpad, agg);
268 /*************************************
269 * GstAggregator implementation *
270 *************************************/
271 static GstElementClass *aggregator_parent_class = NULL;
273 /* All members are protected by the object lock unless otherwise noted */
275 struct _GstAggregatorPrivate
279 /* Our state is >= PAUSED */
280 gboolean running; /* protected by src_lock */
283 gboolean send_stream_start; /* protected by srcpad stream lock */
284 gboolean send_segment;
285 gboolean flush_seeking;
286 gboolean pending_flush_start;
287 gboolean send_eos; /* protected by srcpad stream lock */
289 GstCaps *srccaps; /* protected by the srcpad stream lock */
292 gboolean tags_changed;
294 gboolean peer_latency_live; /* protected by src_lock */
295 GstClockTime peer_latency_min; /* protected by src_lock */
296 GstClockTime peer_latency_max; /* protected by src_lock */
297 gboolean has_peer_latency; /* protected by src_lock */
299 GstClockTime sub_latency_min; /* protected by src_lock */
300 GstClockTime sub_latency_max; /* protected by src_lock */
303 GstClockID aggregate_id; /* protected by src_lock */
307 gboolean first_buffer; /* protected by object lock */
308 GstAggregatorStartTimeSelection start_time_selection;
309 GstClockTime start_time;
312 gint64 latency; /* protected by both src_lock and all pad locks */
320 gboolean only_to_active_pads;
322 gboolean one_actually_seeked;
325 #define DEFAULT_LATENCY 0
326 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
327 #define DEFAULT_START_TIME (-1)
333 PROP_START_TIME_SELECTION,
338 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
339 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
342 * gst_aggregator_iterate_sinkpads:
343 * @self: The #GstAggregator
344 * @func: (scope call): The function to call.
345 * @user_data: (closure): The data to pass to @func.
347 * Iterate the sinkpads of aggregator to call a function on them.
349 * This method guarantees that @func will be called only once for each
353 gst_aggregator_iterate_sinkpads (GstAggregator * self,
354 GstAggregatorPadForeachFunc func, gpointer user_data)
356 gboolean result = FALSE;
358 gboolean done = FALSE;
359 GValue item = { 0, };
360 GList *seen_pads = NULL;
362 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
368 switch (gst_iterator_next (iter, &item)) {
369 case GST_ITERATOR_OK:
371 GstAggregatorPad *pad;
373 pad = g_value_get_object (&item);
375 /* if already pushed, skip. FIXME, find something faster to tag pads */
376 if (pad == NULL || g_list_find (seen_pads, pad)) {
377 g_value_reset (&item);
381 GST_LOG_OBJECT (pad, "calling function %s on pad",
382 GST_DEBUG_FUNCPTR_NAME (func));
384 result = func (self, pad, user_data);
388 seen_pads = g_list_prepend (seen_pads, pad);
390 g_value_reset (&item);
393 case GST_ITERATOR_RESYNC:
394 gst_iterator_resync (iter);
396 case GST_ITERATOR_ERROR:
397 GST_ERROR_OBJECT (self,
398 "Could not iterate over internally linked pads");
401 case GST_ITERATOR_DONE:
406 g_value_unset (&item);
407 gst_iterator_free (iter);
409 if (seen_pads == NULL) {
410 GST_DEBUG_OBJECT (self, "No pad seen");
414 g_list_free (seen_pads);
421 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
423 return (g_queue_peek_tail (&pad->priv->buffers) == NULL &&
424 pad->priv->clipped_buffer == NULL);
428 gst_aggregator_check_pads_ready (GstAggregator * self)
430 GstAggregatorPad *pad;
432 gboolean have_buffer = TRUE;
433 gboolean have_event = FALSE;
435 GST_LOG_OBJECT (self, "checking pads");
437 GST_OBJECT_LOCK (self);
439 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
440 if (sinkpads == NULL)
443 for (l = sinkpads; l != NULL; l = l->next) {
448 if (pad->priv->num_buffers == 0) {
449 if (!gst_aggregator_pad_queue_is_empty (pad))
451 if (!pad->priv->eos) {
454 /* If not live we need data on all pads, so leave the loop */
455 if (!self->priv->peer_latency_live) {
460 } else if (self->priv->peer_latency_live) {
461 /* In live mode, having a single pad with buffers is enough to
462 * generate a start time from it. In non-live mode all pads need
465 self->priv->first_buffer = FALSE;
471 if (!have_buffer && !have_event)
475 self->priv->first_buffer = FALSE;
477 GST_OBJECT_UNLOCK (self);
478 GST_LOG_OBJECT (self, "pads are ready");
483 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
484 GST_OBJECT_UNLOCK (self);
490 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet,"
491 " but waking up for serialized event");
493 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
494 GST_OBJECT_UNLOCK (self);
500 gst_aggregator_reset_flow_values (GstAggregator * self)
502 GST_OBJECT_LOCK (self);
503 self->priv->send_stream_start = TRUE;
504 self->priv->send_segment = TRUE;
505 gst_segment_init (&self->segment, GST_FORMAT_TIME);
506 self->priv->first_buffer = TRUE;
507 GST_OBJECT_UNLOCK (self);
511 gst_aggregator_push_mandatory_events (GstAggregator * self)
513 GstAggregatorPrivate *priv = self->priv;
514 GstEvent *segment = NULL;
515 GstEvent *tags = NULL;
517 if (self->priv->send_stream_start) {
520 GST_INFO_OBJECT (self, "pushing stream start");
521 /* stream-start (FIXME: create id based on input ids) */
522 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
523 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
524 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
526 self->priv->send_stream_start = FALSE;
529 if (self->priv->srccaps) {
531 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
532 self->priv->srccaps);
533 if (!gst_pad_push_event (self->srcpad,
534 gst_event_new_caps (self->priv->srccaps))) {
535 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
537 gst_caps_unref (self->priv->srccaps);
538 self->priv->srccaps = NULL;
541 GST_OBJECT_LOCK (self);
542 if (self->priv->send_segment && !self->priv->flush_seeking) {
543 segment = gst_event_new_segment (&self->segment);
545 if (!self->priv->seqnum)
546 self->priv->seqnum = gst_event_get_seqnum (segment);
548 gst_event_set_seqnum (segment, self->priv->seqnum);
549 self->priv->send_segment = FALSE;
551 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
554 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
555 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
556 priv->tags_changed = FALSE;
558 GST_OBJECT_UNLOCK (self);
561 gst_pad_push_event (self->srcpad, segment);
563 gst_pad_push_event (self->srcpad, tags);
568 * gst_aggregator_set_src_caps:
569 * @self: The #GstAggregator
570 * @caps: The #GstCaps to set on the src pad.
572 * Sets the caps to be used on the src pad.
575 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
577 GST_PAD_STREAM_LOCK (self->srcpad);
578 gst_caps_replace (&self->priv->srccaps, caps);
579 gst_aggregator_push_mandatory_events (self);
580 GST_PAD_STREAM_UNLOCK (self->srcpad);
584 * gst_aggregator_finish_buffer:
585 * @self: The #GstAggregator
586 * @buffer: (transfer full): the #GstBuffer to push.
588 * This method will push the provided output buffer downstream. If needed,
589 * mandatory events such as stream-start, caps, and segment events will be
590 * sent before pushing the buffer.
593 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
595 gst_aggregator_push_mandatory_events (self);
597 GST_OBJECT_LOCK (self);
598 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
599 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
600 GST_OBJECT_UNLOCK (self);
601 return gst_pad_push (self->srcpad, buffer);
603 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
604 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
605 GST_OBJECT_UNLOCK (self);
606 gst_buffer_unref (buffer);
612 gst_aggregator_push_eos (GstAggregator * self)
615 gst_aggregator_push_mandatory_events (self);
617 event = gst_event_new_eos ();
619 GST_OBJECT_LOCK (self);
620 self->priv->send_eos = FALSE;
621 gst_event_set_seqnum (event, self->priv->seqnum);
622 GST_OBJECT_UNLOCK (self);
624 gst_pad_push_event (self->srcpad, event);
628 gst_aggregator_get_next_time (GstAggregator * self)
630 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
632 if (klass->get_next_time)
633 return klass->get_next_time (self);
635 return GST_CLOCK_TIME_NONE;
639 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
641 GstClockTime latency;
649 latency = gst_aggregator_get_latency_unlocked (self);
651 if (gst_aggregator_check_pads_ready (self)) {
652 GST_DEBUG_OBJECT (self, "all pads have data");
658 /* Before waiting, check if we're actually still running */
659 if (!self->priv->running || !self->priv->send_eos) {
665 start = gst_aggregator_get_next_time (self);
667 /* If we're not live, or if we use the running time
668 * of the first buffer as start time, we wait until
669 * all pads have buffers.
670 * Otherwise (i.e. if we are live!), we wait on the clock
671 * and if a pad does not have a buffer in time we ignore
674 GST_OBJECT_LOCK (self);
675 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
676 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
677 !GST_CLOCK_TIME_IS_VALID (start) ||
678 (self->priv->first_buffer
679 && self->priv->start_time_selection ==
680 GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
681 /* We wake up here when something happened, and below
682 * then check if we're ready now. If we return FALSE,
683 * we will be directly called again.
685 GST_OBJECT_UNLOCK (self);
688 GstClockTime base_time, time;
690 GstClockReturn status;
691 GstClockTimeDiff jitter;
693 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
694 GST_TIME_ARGS (start));
696 base_time = GST_ELEMENT_CAST (self)->base_time;
697 clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
698 GST_OBJECT_UNLOCK (self);
700 time = base_time + start;
703 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
704 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
705 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
706 GST_TIME_ARGS (time),
707 GST_TIME_ARGS (base_time),
708 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
709 GST_TIME_ARGS (gst_clock_get_time (clock)));
711 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
712 gst_object_unref (clock);
716 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
719 if (self->priv->aggregate_id) {
720 gst_clock_id_unref (self->priv->aggregate_id);
721 self->priv->aggregate_id = NULL;
724 GST_DEBUG_OBJECT (self,
725 "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
726 status, GST_STIME_ARGS (jitter));
729 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
736 res = gst_aggregator_check_pads_ready (self);
743 check_events (GstAggregator * self, GstAggregatorPad * pad, gpointer user_data)
745 GstEvent *event = NULL;
746 GstAggregatorClass *klass = NULL;
747 gboolean *processed_event = user_data;
753 if (pad->priv->num_buffers == 0 && pad->priv->pending_eos) {
754 pad->priv->pending_eos = FALSE;
755 pad->priv->eos = TRUE;
757 if (pad->priv->clipped_buffer == NULL &&
758 GST_IS_EVENT (g_queue_peek_tail (&pad->priv->buffers))) {
759 event = g_queue_pop_tail (&pad->priv->buffers);
760 PAD_BROADCAST_EVENT (pad);
765 *processed_event = TRUE;
767 klass = GST_AGGREGATOR_GET_CLASS (self);
769 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
770 klass->sink_event (self, pad, event);
772 } while (event != NULL);
778 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
779 GstFlowReturn flow_return, gboolean full)
784 if (flow_return == GST_FLOW_NOT_LINKED)
785 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
787 aggpad->priv->flow_return = flow_return;
789 item = g_queue_peek_head_link (&aggpad->priv->buffers);
791 GList *next = item->next;
793 /* In partial flush, we do like the pad, we get rid of non-sticky events
796 if (full || GST_IS_BUFFER (item->data) ||
797 GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
798 GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
799 !GST_EVENT_IS_STICKY (item->data)) {
800 gst_mini_object_unref (item->data);
801 g_queue_delete_link (&aggpad->priv->buffers, item);
805 aggpad->priv->num_buffers = 0;
806 gst_buffer_replace (&aggpad->priv->clipped_buffer, NULL);
808 PAD_BROADCAST_EVENT (aggpad);
813 gst_aggregator_aggregate_func (GstAggregator * self)
815 GstAggregatorPrivate *priv = self->priv;
816 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
817 gboolean timeout = FALSE;
819 if (self->priv->running == FALSE) {
820 GST_DEBUG_OBJECT (self, "Not running anymore");
824 GST_LOG_OBJECT (self, "Checking aggregate");
825 while (priv->send_eos && priv->running) {
826 GstFlowReturn flow_return;
827 gboolean processed_event = FALSE;
829 gst_aggregator_iterate_sinkpads (self, check_events, NULL);
831 if (!gst_aggregator_wait_and_check (self, &timeout))
834 gst_aggregator_iterate_sinkpads (self, check_events, &processed_event);
838 GST_TRACE_OBJECT (self, "Actually aggregating!");
839 flow_return = klass->aggregate (self, timeout);
841 GST_OBJECT_LOCK (self);
842 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
843 /* We don't want to set the pads to flushing, but we want to
844 * stop the thread, so just break here */
845 GST_OBJECT_UNLOCK (self);
848 GST_OBJECT_UNLOCK (self);
850 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
851 gst_aggregator_push_eos (self);
854 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
856 if (flow_return != GST_FLOW_OK) {
859 GST_OBJECT_LOCK (self);
860 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
861 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
863 gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
865 GST_OBJECT_UNLOCK (self);
870 /* Pause the task here, the only ways to get here are:
871 * 1) We're stopping, in which case the task is stopped anyway
872 * 2) We got a flow error above, in which case it might take
873 * some time to forward the flow return upstream and we
874 * would otherwise call the task function over and over
875 * again without doing anything
877 gst_pad_pause_task (self->srcpad);
881 gst_aggregator_start (GstAggregator * self)
883 GstAggregatorClass *klass;
886 self->priv->send_stream_start = TRUE;
887 self->priv->send_segment = TRUE;
888 self->priv->send_eos = TRUE;
889 self->priv->srccaps = NULL;
891 klass = GST_AGGREGATOR_GET_CLASS (self);
894 result = klass->start (self);
902 _check_pending_flush_stop (GstAggregatorPad * pad)
907 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
914 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
918 GST_INFO_OBJECT (self, "%s srcpad task",
919 flush_start ? "Pausing" : "Stopping");
922 self->priv->running = FALSE;
923 SRC_BROADCAST (self);
927 res = gst_pad_push_event (self->srcpad, flush_start);
930 gst_pad_stop_task (self->srcpad);
936 gst_aggregator_start_srcpad_task (GstAggregator * self)
938 GST_INFO_OBJECT (self, "Starting srcpad task");
940 self->priv->running = TRUE;
941 gst_pad_start_task (GST_PAD (self->srcpad),
942 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
946 gst_aggregator_flush (GstAggregator * self)
948 GstFlowReturn ret = GST_FLOW_OK;
949 GstAggregatorPrivate *priv = self->priv;
950 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
952 GST_DEBUG_OBJECT (self, "Flushing everything");
953 GST_OBJECT_LOCK (self);
954 priv->send_segment = TRUE;
955 priv->flush_seeking = FALSE;
956 priv->tags_changed = FALSE;
957 GST_OBJECT_UNLOCK (self);
959 ret = klass->flush (self);
965 /* Called with GstAggregator's object lock held */
968 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
971 GstAggregatorPad *tmppad;
973 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
974 tmppad = (GstAggregatorPad *) tmp->data;
976 if (_check_pending_flush_stop (tmppad) == FALSE) {
977 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
978 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
987 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
990 GstAggregatorPrivate *priv = self->priv;
991 GstAggregatorPadPrivate *padpriv = aggpad->priv;
993 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
995 PAD_FLUSH_LOCK (aggpad);
997 if (padpriv->pending_flush_start) {
998 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
1000 padpriv->pending_flush_start = FALSE;
1001 padpriv->pending_flush_stop = TRUE;
1003 PAD_UNLOCK (aggpad);
1005 GST_OBJECT_LOCK (self);
1006 if (priv->flush_seeking) {
1007 /* If flush_seeking we forward the first FLUSH_START */
1008 if (priv->pending_flush_start) {
1009 priv->pending_flush_start = FALSE;
1010 GST_OBJECT_UNLOCK (self);
1012 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
1013 gst_aggregator_stop_srcpad_task (self, event);
1015 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
1016 GST_PAD_STREAM_LOCK (self->srcpad);
1017 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
1020 GST_OBJECT_UNLOCK (self);
1021 gst_event_unref (event);
1024 GST_OBJECT_UNLOCK (self);
1025 gst_event_unref (event);
1027 PAD_FLUSH_UNLOCK (aggpad);
1030 /* Must be called with the the PAD_LOCK held */
1032 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1035 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->head_position) &&
1036 aggpad->priv->head_segment.format == GST_FORMAT_TIME)
1037 aggpad->priv->head_time =
1038 gst_segment_to_running_time (&aggpad->priv->head_segment,
1039 GST_FORMAT_TIME, aggpad->priv->head_position);
1041 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
1043 if (!GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_time))
1044 aggpad->priv->tail_time = aggpad->priv->head_time;
1046 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_position) &&
1047 aggpad->segment.format == GST_FORMAT_TIME)
1048 aggpad->priv->tail_time =
1049 gst_segment_to_running_time (&aggpad->segment,
1050 GST_FORMAT_TIME, aggpad->priv->tail_position);
1052 aggpad->priv->tail_time = aggpad->priv->head_time;
1055 if (aggpad->priv->head_time == GST_CLOCK_TIME_NONE ||
1056 aggpad->priv->tail_time == GST_CLOCK_TIME_NONE) {
1057 aggpad->priv->time_level = 0;
1061 if (aggpad->priv->tail_time > aggpad->priv->head_time)
1062 aggpad->priv->time_level = 0;
1064 aggpad->priv->time_level = aggpad->priv->head_time -
1065 aggpad->priv->tail_time;
1069 /* GstAggregator vmethods default implementations */
1071 gst_aggregator_default_sink_event (GstAggregator * self,
1072 GstAggregatorPad * aggpad, GstEvent * event)
1074 gboolean res = TRUE;
1075 GstPad *pad = GST_PAD (aggpad);
1076 GstAggregatorPrivate *priv = self->priv;
1078 switch (GST_EVENT_TYPE (event)) {
1079 case GST_EVENT_FLUSH_START:
1081 gst_aggregator_flush_start (self, aggpad, event);
1082 /* We forward only in one case: right after flush_seeking */
1086 case GST_EVENT_FLUSH_STOP:
1088 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
1090 gst_aggregator_pad_flush (aggpad, self);
1091 GST_OBJECT_LOCK (self);
1092 if (priv->flush_seeking) {
1093 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1094 if (gst_aggregator_all_flush_stop_received_locked (self)) {
1095 GST_OBJECT_UNLOCK (self);
1096 /* That means we received FLUSH_STOP/FLUSH_STOP on
1097 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1098 gst_aggregator_flush (self);
1099 gst_pad_push_event (self->srcpad, event);
1102 priv->send_eos = TRUE;
1103 SRC_BROADCAST (self);
1106 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1107 GST_PAD_STREAM_UNLOCK (self->srcpad);
1108 gst_aggregator_start_srcpad_task (self);
1110 GST_OBJECT_UNLOCK (self);
1113 GST_OBJECT_UNLOCK (self);
1116 /* We never forward the event */
1121 GST_DEBUG_OBJECT (aggpad, "EOS");
1123 /* We still have a buffer, and we don't want the subclass to have to
1124 * check for it. Mark pending_eos, eos will be set when steal_buffer is
1129 if (aggpad->priv->num_buffers == 0) {
1130 aggpad->priv->eos = TRUE;
1132 aggpad->priv->pending_eos = TRUE;
1134 PAD_UNLOCK (aggpad);
1136 SRC_BROADCAST (self);
1140 case GST_EVENT_SEGMENT:
1143 GST_OBJECT_LOCK (aggpad);
1144 gst_event_copy_segment (event, &aggpad->segment);
1145 update_time_level (aggpad, FALSE);
1146 GST_OBJECT_UNLOCK (aggpad);
1147 PAD_UNLOCK (aggpad);
1149 GST_OBJECT_LOCK (self);
1150 self->priv->seqnum = gst_event_get_seqnum (event);
1151 GST_OBJECT_UNLOCK (self);
1154 case GST_EVENT_STREAM_START:
1160 GstClockTime pts, endpts;
1161 GstClockTime duration;
1164 gst_event_parse_gap (event, &pts, &duration);
1165 gapbuf = gst_buffer_new ();
1167 if (GST_CLOCK_TIME_IS_VALID (duration))
1168 endpts = pts + duration;
1170 endpts = GST_CLOCK_TIME_NONE;
1172 GST_OBJECT_LOCK (aggpad);
1173 res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1175 GST_OBJECT_UNLOCK (aggpad);
1178 GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1182 if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1183 duration = endpts - pts;
1185 duration = GST_CLOCK_TIME_NONE;
1187 GST_BUFFER_PTS (gapbuf) = pts;
1188 GST_BUFFER_DURATION (gapbuf) = duration;
1189 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1190 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1192 if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1194 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1204 gst_event_parse_tag (event, &tags);
1206 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1207 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1208 gst_event_unref (event);
1220 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1221 return gst_pad_event_default (pad, GST_OBJECT (self), event);
1224 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1226 gst_event_unref (event);
1231 static inline gboolean
1232 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1233 gpointer unused_udata)
1235 gst_aggregator_pad_flush (pad, self);
1241 gst_aggregator_stop (GstAggregator * agg)
1243 GstAggregatorClass *klass;
1246 gst_aggregator_reset_flow_values (agg);
1248 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1250 klass = GST_AGGREGATOR_GET_CLASS (agg);
1253 result = klass->stop (agg);
1257 agg->priv->has_peer_latency = FALSE;
1258 agg->priv->peer_latency_live = FALSE;
1259 agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1261 if (agg->priv->tags)
1262 gst_tag_list_unref (agg->priv->tags);
1263 agg->priv->tags = NULL;
1268 /* GstElement vmethods implementations */
1269 static GstStateChangeReturn
1270 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1272 GstStateChangeReturn ret;
1273 GstAggregator *self = GST_AGGREGATOR (element);
1275 switch (transition) {
1276 case GST_STATE_CHANGE_READY_TO_PAUSED:
1277 if (!gst_aggregator_start (self))
1285 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1286 transition)) == GST_STATE_CHANGE_FAILURE)
1290 switch (transition) {
1291 case GST_STATE_CHANGE_PAUSED_TO_READY:
1292 if (!gst_aggregator_stop (self)) {
1293 /* What to do in this case? Error out? */
1294 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1306 GST_ERROR_OBJECT (element, "parent failed state change");
1311 GST_ERROR_OBJECT (element, "Subclass failed to start");
1312 return GST_STATE_CHANGE_FAILURE;
1317 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1319 GstAggregator *self = GST_AGGREGATOR (element);
1320 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1322 GST_INFO_OBJECT (pad, "Removing pad");
1325 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1326 gst_element_remove_pad (element, pad);
1328 self->priv->has_peer_latency = FALSE;
1329 SRC_BROADCAST (self);
1333 static GstAggregatorPad *
1334 gst_aggregator_default_create_new_pad (GstAggregator * self,
1335 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1337 GstAggregatorPad *agg_pad;
1338 GstAggregatorPrivate *priv = self->priv;
1342 if (templ->direction != GST_PAD_SINK ||
1343 g_strcmp0 (templ->name_template, "sink_%u") != 0)
1346 GST_OBJECT_LOCK (self);
1347 if (req_name == NULL || strlen (req_name) < 6
1348 || !g_str_has_prefix (req_name, "sink_")) {
1349 /* no name given when requesting the pad, use next available int */
1350 serial = ++priv->max_padserial;
1352 /* parse serial number from requested padname */
1353 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1354 if (serial > priv->max_padserial)
1355 priv->max_padserial = serial;
1358 name = g_strdup_printf ("sink_%u", serial);
1359 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1360 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1363 GST_OBJECT_UNLOCK (self);
1370 GST_WARNING_OBJECT (self, "request new pad that is not a SINK pad\n");
1376 gst_aggregator_request_new_pad (GstElement * element,
1377 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1379 GstAggregator *self;
1380 GstAggregatorPad *agg_pad;
1381 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1382 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1384 self = GST_AGGREGATOR (element);
1386 agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1388 GST_ERROR_OBJECT (element, "Couldn't create new pad");
1392 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1395 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1397 /* add the pad to the element */
1398 gst_element_add_pad (element, GST_PAD (agg_pad));
1400 return GST_PAD (agg_pad);
1403 /* Must be called with SRC_LOCK held */
1406 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1408 gboolean query_ret, live;
1409 GstClockTime our_latency, min, max;
1411 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1414 GST_WARNING_OBJECT (self, "Latency query failed");
1418 gst_query_parse_latency (query, &live, &min, &max);
1420 our_latency = self->priv->latency;
1422 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1423 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1424 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1428 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1429 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1430 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1431 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1432 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1436 self->priv->peer_latency_live = live;
1437 self->priv->peer_latency_min = min;
1438 self->priv->peer_latency_max = max;
1439 self->priv->has_peer_latency = TRUE;
1443 min += self->priv->sub_latency_min;
1444 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1445 && GST_CLOCK_TIME_IS_VALID (max))
1446 max += self->priv->sub_latency_max + our_latency;
1448 max = GST_CLOCK_TIME_NONE;
1450 SRC_BROADCAST (self);
1452 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1453 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1455 gst_query_set_latency (query, live, min, max);
1461 * MUST be called with the src_lock held.
1463 * See gst_aggregator_get_latency() for doc
1466 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1468 GstClockTime latency;
1470 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1472 if (!self->priv->has_peer_latency) {
1473 GstQuery *query = gst_query_new_latency ();
1476 ret = gst_aggregator_query_latency_unlocked (self, query);
1477 gst_query_unref (query);
1479 return GST_CLOCK_TIME_NONE;
1482 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1483 return GST_CLOCK_TIME_NONE;
1485 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1486 latency = self->priv->peer_latency_min;
1489 latency += self->priv->latency;
1490 latency += self->priv->sub_latency_min;
1496 * gst_aggregator_get_latency:
1497 * @self: a #GstAggregator
1499 * Retrieves the latency values reported by @self in response to the latency
1500 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1501 * will not wait for the clock.
1503 * Typically only called by subclasses.
1505 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1508 gst_aggregator_get_latency (GstAggregator * self)
1513 ret = gst_aggregator_get_latency_unlocked (self);
1520 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1522 GstAggregator *self = GST_AGGREGATOR (element);
1524 GST_STATE_LOCK (element);
1525 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1526 GST_STATE (element) < GST_STATE_PAUSED) {
1530 GstSeekType start_type, stop_type;
1533 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1534 &start, &stop_type, &stop);
1536 GST_OBJECT_LOCK (self);
1537 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1538 stop_type, stop, NULL);
1539 self->priv->seqnum = gst_event_get_seqnum (event);
1540 self->priv->first_buffer = FALSE;
1541 GST_OBJECT_UNLOCK (self);
1543 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1545 GST_STATE_UNLOCK (element);
1548 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1553 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1555 gboolean res = TRUE;
1557 switch (GST_QUERY_TYPE (query)) {
1558 case GST_QUERY_SEEKING:
1562 /* don't pass it along as some (file)sink might claim it does
1563 * whereas with a collectpads in between that will not likely work */
1564 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1565 gst_query_set_seeking (query, format, FALSE, 0, -1);
1570 case GST_QUERY_LATENCY:
1572 res = gst_aggregator_query_latency_unlocked (self, query);
1576 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1583 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1585 EventData *evdata = user_data;
1586 gboolean ret = TRUE;
1587 GstPad *peer = gst_pad_get_peer (pad);
1588 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1591 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1592 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1595 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1596 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1597 gst_object_unref (peer);
1602 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1603 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1605 GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1607 if (gst_pad_query (peer, seeking)) {
1610 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1612 if (seekable == FALSE) {
1613 GST_INFO_OBJECT (pad,
1614 "Source not seekable, We failed but it does not matter!");
1619 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1622 gst_query_unref (seeking);
1625 if (evdata->flush) {
1627 aggpad->priv->pending_flush_start = FALSE;
1628 aggpad->priv->pending_flush_stop = FALSE;
1629 PAD_UNLOCK (aggpad);
1632 evdata->one_actually_seeked = TRUE;
1635 evdata->result &= ret;
1637 /* Always send to all pads */
1642 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1643 GstEvent * event, gboolean flush, gboolean only_to_active_pads)
1647 evdata.event = event;
1648 evdata.result = TRUE;
1649 evdata.flush = flush;
1650 evdata.one_actually_seeked = FALSE;
1651 evdata.only_to_active_pads = only_to_active_pads;
1653 /* We first need to set all pads as flushing in a first pass
1654 * as flush_start flush_stop is sometimes sent synchronously
1655 * while we send the seek event */
1659 GST_OBJECT_LOCK (self);
1660 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1661 GstAggregatorPad *pad = l->data;
1664 pad->priv->pending_flush_start = TRUE;
1665 pad->priv->pending_flush_stop = FALSE;
1668 GST_OBJECT_UNLOCK (self);
1671 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1673 gst_event_unref (event);
1679 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1684 GstSeekType start_type, stop_type;
1688 GstAggregatorPrivate *priv = self->priv;
1690 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1691 &start, &stop_type, &stop);
1693 GST_INFO_OBJECT (self, "starting SEEK");
1695 flush = flags & GST_SEEK_FLAG_FLUSH;
1697 GST_OBJECT_LOCK (self);
1699 priv->pending_flush_start = TRUE;
1700 priv->flush_seeking = TRUE;
1703 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1704 stop_type, stop, NULL);
1706 /* Seeking sets a position */
1707 self->priv->first_buffer = FALSE;
1708 GST_OBJECT_UNLOCK (self);
1710 /* forward the seek upstream */
1712 gst_aggregator_forward_event_to_all_sinkpads (self, event, flush, FALSE);
1715 if (!evdata.result || !evdata.one_actually_seeked) {
1716 GST_OBJECT_LOCK (self);
1717 priv->flush_seeking = FALSE;
1718 priv->pending_flush_start = FALSE;
1719 GST_OBJECT_UNLOCK (self);
1722 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1724 return evdata.result;
1728 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1731 gboolean res = TRUE;
1733 switch (GST_EVENT_TYPE (event)) {
1734 case GST_EVENT_SEEK:
1736 gst_event_ref (event);
1737 res = gst_aggregator_do_seek (self, event);
1738 gst_event_unref (event);
1742 case GST_EVENT_NAVIGATION:
1744 /* navigation is rather pointless. */
1746 gst_event_unref (event);
1755 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
1756 * they will receive a QOS event that has earliest_time=0 (because we can't
1757 * have negative timestamps), and consider their buffer as too late */
1759 gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE,
1760 GST_EVENT_TYPE (event) == GST_EVENT_QOS);
1761 res = evdata.result;
1768 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1771 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1773 return klass->src_event (GST_AGGREGATOR (parent), event);
1777 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1780 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1782 return klass->src_query (GST_AGGREGATOR (parent), query);
1786 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1787 GstObject * parent, GstPadMode mode, gboolean active)
1789 GstAggregator *self = GST_AGGREGATOR (parent);
1790 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1792 if (klass->src_activate) {
1793 if (klass->src_activate (self, mode, active) == FALSE) {
1798 if (active == TRUE) {
1800 case GST_PAD_MODE_PUSH:
1802 GST_INFO_OBJECT (pad, "Activating pad!");
1803 gst_aggregator_start_srcpad_task (self);
1808 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1815 GST_INFO_OBJECT (self, "Deactivating srcpad");
1816 gst_aggregator_stop_srcpad_task (self, FALSE);
1822 gst_aggregator_default_sink_query (GstAggregator * self,
1823 GstAggregatorPad * aggpad, GstQuery * query)
1825 GstPad *pad = GST_PAD (aggpad);
1827 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1831 gst_aggregator_finalize (GObject * object)
1833 GstAggregator *self = (GstAggregator *) object;
1835 g_mutex_clear (&self->priv->src_lock);
1836 g_cond_clear (&self->priv->src_cond);
1838 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1842 * gst_aggregator_set_latency_property:
1843 * @agg: a #GstAggregator
1844 * @latency: the new latency value (in nanoseconds).
1846 * Sets the new latency value to @latency. This value is used to limit the
1847 * amount of time a pad waits for data to appear before considering the pad
1851 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1855 g_return_if_fail (GST_IS_AGGREGATOR (self));
1856 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1859 changed = (self->priv->latency != latency);
1864 GST_OBJECT_LOCK (self);
1865 /* First lock all the pads */
1866 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1867 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1871 self->priv->latency = latency;
1873 SRC_BROADCAST (self);
1875 /* Now wake up the pads */
1876 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1877 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1878 PAD_BROADCAST_EVENT (aggpad);
1879 PAD_UNLOCK (aggpad);
1881 GST_OBJECT_UNLOCK (self);
1887 gst_element_post_message (GST_ELEMENT_CAST (self),
1888 gst_message_new_latency (GST_OBJECT_CAST (self)));
1892 * gst_aggregator_get_latency_property:
1893 * @agg: a #GstAggregator
1895 * Gets the latency value. See gst_aggregator_set_latency for
1898 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1899 * before a pad is deemed unresponsive. A value of -1 means an
1903 gst_aggregator_get_latency_property (GstAggregator * agg)
1907 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1909 GST_OBJECT_LOCK (agg);
1910 res = agg->priv->latency;
1911 GST_OBJECT_UNLOCK (agg);
1917 gst_aggregator_set_property (GObject * object, guint prop_id,
1918 const GValue * value, GParamSpec * pspec)
1920 GstAggregator *agg = GST_AGGREGATOR (object);
1924 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1926 case PROP_START_TIME_SELECTION:
1927 agg->priv->start_time_selection = g_value_get_enum (value);
1929 case PROP_START_TIME:
1930 agg->priv->start_time = g_value_get_uint64 (value);
1933 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1939 gst_aggregator_get_property (GObject * object, guint prop_id,
1940 GValue * value, GParamSpec * pspec)
1942 GstAggregator *agg = GST_AGGREGATOR (object);
1946 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1948 case PROP_START_TIME_SELECTION:
1949 g_value_set_enum (value, agg->priv->start_time_selection);
1951 case PROP_START_TIME:
1952 g_value_set_uint64 (value, agg->priv->start_time);
1955 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1960 /* GObject vmethods implementations */
1962 gst_aggregator_class_init (GstAggregatorClass * klass)
1964 GObjectClass *gobject_class = (GObjectClass *) klass;
1965 GstElementClass *gstelement_class = (GstElementClass *) klass;
1967 aggregator_parent_class = g_type_class_peek_parent (klass);
1968 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1970 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1971 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1973 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1975 klass->sink_event = gst_aggregator_default_sink_event;
1976 klass->sink_query = gst_aggregator_default_sink_query;
1978 klass->src_event = gst_aggregator_default_src_event;
1979 klass->src_query = gst_aggregator_default_src_query;
1981 klass->create_new_pad = gst_aggregator_default_create_new_pad;
1983 gstelement_class->request_new_pad =
1984 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1985 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1986 gstelement_class->release_pad =
1987 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1988 gstelement_class->change_state =
1989 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1991 gobject_class->set_property = gst_aggregator_set_property;
1992 gobject_class->get_property = gst_aggregator_get_property;
1993 gobject_class->finalize = gst_aggregator_finalize;
1995 g_object_class_install_property (gobject_class, PROP_LATENCY,
1996 g_param_spec_int64 ("latency", "Buffer latency",
1997 "Additional latency in live mode to allow upstream "
1998 "to take longer to produce buffers for the current "
1999 "position (in nanoseconds)", 0,
2000 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
2001 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2003 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
2004 g_param_spec_enum ("start-time-selection", "Start Time Selection",
2005 "Decides which start time is output",
2006 gst_aggregator_start_time_selection_get_type (),
2007 DEFAULT_START_TIME_SELECTION,
2008 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2010 g_object_class_install_property (gobject_class, PROP_START_TIME,
2011 g_param_spec_uint64 ("start-time", "Start Time",
2012 "Start time to use if start-time-selection=set", 0,
2014 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2016 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
2020 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
2022 GstPadTemplate *pad_template;
2023 GstAggregatorPrivate *priv;
2025 g_return_if_fail (klass->aggregate != NULL);
2028 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2029 GstAggregatorPrivate);
2034 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2035 g_return_if_fail (pad_template != NULL);
2037 priv->max_padserial = -1;
2038 priv->tags_changed = FALSE;
2040 self->priv->peer_latency_live = FALSE;
2041 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2042 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2043 self->priv->has_peer_latency = FALSE;
2044 gst_aggregator_reset_flow_values (self);
2046 self->srcpad = gst_pad_new_from_template (pad_template, "src");
2048 gst_pad_set_event_function (self->srcpad,
2049 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2050 gst_pad_set_query_function (self->srcpad,
2051 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2052 gst_pad_set_activatemode_function (self->srcpad,
2053 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2055 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2057 self->priv->latency = DEFAULT_LATENCY;
2058 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2059 self->priv->start_time = DEFAULT_START_TIME;
2061 g_mutex_init (&self->priv->src_lock);
2062 g_cond_init (&self->priv->src_cond);
2065 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2066 * method to get to the padtemplates */
2068 gst_aggregator_get_type (void)
2070 static volatile gsize type = 0;
2072 if (g_once_init_enter (&type)) {
2074 static const GTypeInfo info = {
2075 sizeof (GstAggregatorClass),
2078 (GClassInitFunc) gst_aggregator_class_init,
2081 sizeof (GstAggregator),
2083 (GInstanceInitFunc) gst_aggregator_init,
2086 _type = g_type_register_static (GST_TYPE_ELEMENT,
2087 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2088 g_once_init_leave (&type, _type);
2093 /* Must be called with SRC lock and PAD lock held */
2095 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2097 /* Empty queue always has space */
2098 if (aggpad->priv->num_buffers == 0 && aggpad->priv->clipped_buffer == NULL)
2101 /* We also want at least two buffers, one is being processed and one is ready
2102 * for the next iteration when we operate in live mode. */
2103 if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2106 /* zero latency, if there is a buffer, it's full */
2107 if (self->priv->latency == 0)
2110 /* Allow no more buffers than the latency */
2111 return (aggpad->priv->time_level <= self->priv->latency);
2114 /* Must be called with the PAD_LOCK held */
2116 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2118 GstClockTime timestamp;
2120 if (GST_BUFFER_DTS_IS_VALID (buffer))
2121 timestamp = GST_BUFFER_DTS (buffer);
2123 timestamp = GST_BUFFER_PTS (buffer);
2125 if (timestamp == GST_CLOCK_TIME_NONE) {
2127 timestamp = aggpad->priv->head_position;
2129 timestamp = aggpad->priv->tail_position;
2133 if (GST_BUFFER_DURATION_IS_VALID (buffer))
2134 timestamp += GST_BUFFER_DURATION (buffer);
2137 aggpad->priv->head_position = timestamp;
2139 aggpad->priv->tail_position = timestamp;
2141 update_time_level (aggpad, head);
2144 static GstFlowReturn
2145 gst_aggregator_pad_chain_internal (GstAggregator * self,
2146 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2148 GstFlowReturn flow_return;
2149 GstClockTime buf_pts;
2151 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2153 PAD_FLUSH_LOCK (aggpad);
2156 flow_return = aggpad->priv->flow_return;
2157 if (flow_return != GST_FLOW_OK)
2160 if (aggpad->priv->pending_eos == TRUE)
2163 PAD_UNLOCK (aggpad);
2165 buf_pts = GST_BUFFER_PTS (buffer);
2169 GST_OBJECT_LOCK (self);
2172 if (aggpad->priv->first_buffer) {
2173 self->priv->has_peer_latency = FALSE;
2174 aggpad->priv->first_buffer = FALSE;
2177 if (gst_aggregator_pad_has_space (self, aggpad)
2178 && aggpad->priv->flow_return == GST_FLOW_OK) {
2180 g_queue_push_head (&aggpad->priv->buffers, buffer);
2182 g_queue_push_tail (&aggpad->priv->buffers, buffer);
2183 apply_buffer (aggpad, buffer, head);
2184 aggpad->priv->num_buffers++;
2186 SRC_BROADCAST (self);
2190 flow_return = aggpad->priv->flow_return;
2191 if (flow_return != GST_FLOW_OK) {
2192 GST_OBJECT_UNLOCK (self);
2196 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2197 GST_OBJECT_UNLOCK (self);
2199 PAD_WAIT_EVENT (aggpad);
2201 PAD_UNLOCK (aggpad);
2204 if (self->priv->first_buffer) {
2205 GstClockTime start_time;
2207 switch (self->priv->start_time_selection) {
2208 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2212 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2213 GST_OBJECT_LOCK (aggpad);
2214 if (aggpad->priv->head_segment.format == GST_FORMAT_TIME) {
2215 start_time = buf_pts;
2216 if (start_time != -1) {
2217 start_time = MAX (start_time, aggpad->priv->head_segment.start);
2219 gst_segment_to_running_time (&aggpad->priv->head_segment,
2220 GST_FORMAT_TIME, start_time);
2224 GST_WARNING_OBJECT (aggpad,
2225 "Ignoring request of selecting the first start time "
2226 "as the segment is a %s segment instead of a time segment",
2227 gst_format_get_name (aggpad->segment.format));
2229 GST_OBJECT_UNLOCK (aggpad);
2231 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2232 start_time = self->priv->start_time;
2233 if (start_time == -1)
2238 if (start_time != -1) {
2239 if (self->segment.position == -1)
2240 self->segment.position = start_time;
2242 self->segment.position = MIN (start_time, self->segment.position);
2244 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2245 GST_TIME_ARGS (start_time));
2249 PAD_UNLOCK (aggpad);
2250 GST_OBJECT_UNLOCK (self);
2253 PAD_FLUSH_UNLOCK (aggpad);
2255 GST_DEBUG_OBJECT (aggpad, "Done chaining");
2260 PAD_UNLOCK (aggpad);
2261 PAD_FLUSH_UNLOCK (aggpad);
2263 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2264 gst_flow_get_name (flow_return));
2266 gst_buffer_unref (buffer);
2271 PAD_UNLOCK (aggpad);
2272 PAD_FLUSH_UNLOCK (aggpad);
2274 gst_buffer_unref (buffer);
2275 GST_DEBUG_OBJECT (aggpad, "We are EOS already...");
2277 return GST_FLOW_EOS;
2280 static GstFlowReturn
2281 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2283 return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2284 GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2288 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2291 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2292 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2294 if (GST_QUERY_IS_SERIALIZED (query)) {
2297 while (!gst_aggregator_pad_queue_is_empty (aggpad)
2298 && aggpad->priv->flow_return == GST_FLOW_OK) {
2299 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2300 PAD_WAIT_EVENT (aggpad);
2303 if (aggpad->priv->flow_return != GST_FLOW_OK)
2306 PAD_UNLOCK (aggpad);
2309 return klass->sink_query (GST_AGGREGATOR (parent),
2310 GST_AGGREGATOR_PAD (pad), query);
2313 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2314 gst_flow_get_name (aggpad->priv->flow_return));
2315 PAD_UNLOCK (aggpad);
2319 static GstFlowReturn
2320 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2323 GstFlowReturn ret = GST_FLOW_OK;
2324 GstAggregator *self = GST_AGGREGATOR (parent);
2325 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2326 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2328 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
2329 /* && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE */ ) {
2333 if (aggpad->priv->flow_return != GST_FLOW_OK
2334 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2335 ret = aggpad->priv->flow_return;
2339 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2340 GST_OBJECT_LOCK (aggpad);
2341 gst_event_copy_segment (event, &aggpad->priv->head_segment);
2342 aggpad->priv->head_position = aggpad->priv->head_segment.position;
2343 update_time_level (aggpad, TRUE);
2344 GST_OBJECT_UNLOCK (aggpad);
2347 if (GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2348 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT,
2350 g_queue_push_head (&aggpad->priv->buffers, event);
2352 SRC_BROADCAST (self);
2354 PAD_UNLOCK (aggpad);
2359 gboolean is_caps = (GST_EVENT_TYPE (event) == GST_EVENT_CAPS);
2361 if (!klass->sink_event (self, aggpad, event)) {
2362 /* Copied from GstPad to convert boolean to a GstFlowReturn in
2363 * the event handling func */
2364 ret = is_caps ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_ERROR;
2371 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2372 gst_flow_get_name (aggpad->priv->flow_return));
2373 PAD_UNLOCK (aggpad);
2375 if (GST_EVENT_IS_STICKY (event))
2376 gst_pad_store_sticky_event (pad, event);
2377 gst_event_unref (event);
2383 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2384 GstObject * parent, GstPadMode mode, gboolean active)
2386 GstAggregator *self = GST_AGGREGATOR (parent);
2387 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2389 if (active == FALSE) {
2391 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2392 SRC_BROADCAST (self);
2396 aggpad->priv->flow_return = GST_FLOW_OK;
2397 PAD_BROADCAST_EVENT (aggpad);
2398 PAD_UNLOCK (aggpad);
2404 /***********************************
2405 * GstAggregatorPad implementation *
2406 ************************************/
2407 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2410 gst_aggregator_pad_constructed (GObject * object)
2412 GstPad *pad = GST_PAD (object);
2414 gst_pad_set_chain_function (pad,
2415 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2416 gst_pad_set_event_full_function_full (pad,
2417 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
2418 gst_pad_set_query_function (pad,
2419 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2420 gst_pad_set_activatemode_function (pad,
2421 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2425 gst_aggregator_pad_finalize (GObject * object)
2427 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2429 g_cond_clear (&pad->priv->event_cond);
2430 g_mutex_clear (&pad->priv->flush_lock);
2431 g_mutex_clear (&pad->priv->lock);
2433 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2437 gst_aggregator_pad_dispose (GObject * object)
2439 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2441 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2443 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2447 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2449 GObjectClass *gobject_class = (GObjectClass *) klass;
2451 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2453 gobject_class->constructed = gst_aggregator_pad_constructed;
2454 gobject_class->finalize = gst_aggregator_pad_finalize;
2455 gobject_class->dispose = gst_aggregator_pad_dispose;
2459 gst_aggregator_pad_init (GstAggregatorPad * pad)
2462 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2463 GstAggregatorPadPrivate);
2465 g_queue_init (&pad->priv->buffers);
2466 g_cond_init (&pad->priv->event_cond);
2468 g_mutex_init (&pad->priv->flush_lock);
2469 g_mutex_init (&pad->priv->lock);
2471 gst_aggregator_pad_reset_unlocked (pad);
2474 /* Must be called with the PAD_LOCK held */
2476 gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad)
2478 pad->priv->num_buffers--;
2479 GST_TRACE_OBJECT (pad, "Consuming buffer");
2480 if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
2481 pad->priv->pending_eos = FALSE;
2482 pad->priv->eos = TRUE;
2484 PAD_BROADCAST_EVENT (pad);
2487 /* Must be called with the PAD_LOCK held */
2489 gst_aggregator_pad_clip_buffer_unlocked (GstAggregatorPad * pad)
2491 GstAggregator *self = NULL;
2492 GstAggregatorClass *aggclass;
2493 GstBuffer *buffer = NULL;
2495 while (pad->priv->clipped_buffer == NULL &&
2496 GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->buffers))) {
2497 buffer = g_queue_pop_tail (&pad->priv->buffers);
2499 apply_buffer (pad, buffer, FALSE);
2501 /* We only take the parent here so that it's not taken if the buffer is
2502 * already clipped or if the queue is empty.
2505 self = GST_AGGREGATOR (gst_pad_get_parent_element (GST_PAD (pad)));
2507 gst_buffer_unref (buffer);
2511 aggclass = GST_AGGREGATOR_GET_CLASS (self);
2514 if (aggclass->clip) {
2515 GST_TRACE_OBJECT (pad, "Clipping: %" GST_PTR_FORMAT, buffer);
2517 buffer = aggclass->clip (self, pad, buffer);
2519 if (buffer == NULL) {
2520 gst_aggregator_pad_buffer_consumed (pad);
2521 GST_TRACE_OBJECT (pad, "Clipping consumed the buffer");
2525 pad->priv->clipped_buffer = buffer;
2529 gst_object_unref (self);
2533 * gst_aggregator_pad_steal_buffer:
2534 * @pad: the pad to get buffer from
2536 * Steal the ref to the buffer currently queued in @pad.
2538 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2539 * queued. You should unref the buffer after usage.
2542 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2548 gst_aggregator_pad_clip_buffer_unlocked (pad);
2550 buffer = pad->priv->clipped_buffer;
2551 pad->priv->clipped_buffer = NULL;
2554 gst_aggregator_pad_buffer_consumed (pad);
2555 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2564 * gst_aggregator_pad_drop_buffer:
2565 * @pad: the pad where to drop any pending buffer
2567 * Drop the buffer currently queued in @pad.
2569 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2572 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2576 buf = gst_aggregator_pad_steal_buffer (pad);
2581 gst_buffer_unref (buf);
2586 * gst_aggregator_pad_get_buffer:
2587 * @pad: the pad to get buffer from
2589 * Returns: (transfer full): A reference to the buffer in @pad or
2590 * NULL if no buffer was queued. You should unref the buffer after
2594 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2600 gst_aggregator_pad_clip_buffer_unlocked (pad);
2602 if (pad->priv->clipped_buffer) {
2603 buffer = gst_buffer_ref (pad->priv->clipped_buffer);
2613 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2618 is_eos = pad->priv->eos;
2625 * gst_aggregator_merge_tags:
2626 * @self: a #GstAggregator
2627 * @tags: a #GstTagList to merge
2628 * @mode: the #GstTagMergeMode to use
2630 * Adds tags to so-called pending tags, which will be processed
2631 * before pushing out data downstream.
2633 * Note that this is provided for convenience, and the subclass is
2634 * not required to use this and can still do tag handling on its own.
2639 gst_aggregator_merge_tags (GstAggregator * self,
2640 const GstTagList * tags, GstTagMergeMode mode)
2644 g_return_if_fail (GST_IS_AGGREGATOR (self));
2645 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2647 /* FIXME Check if we can use OBJECT lock here! */
2648 GST_OBJECT_LOCK (self);
2650 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2651 otags = self->priv->tags;
2652 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2654 gst_tag_list_unref (otags);
2655 self->priv->tags_changed = TRUE;
2656 GST_OBJECT_UNLOCK (self);
2660 * gst_aggregator_set_latency:
2661 * @self: a #GstAggregator
2662 * @min_latency: minimum latency
2663 * @max_latency: maximum latency
2665 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2666 * latency is. Will also post a LATENCY message on the bus so the pipeline
2667 * can reconfigure its global latency.
2670 gst_aggregator_set_latency (GstAggregator * self,
2671 GstClockTime min_latency, GstClockTime max_latency)
2673 gboolean changed = FALSE;
2675 g_return_if_fail (GST_IS_AGGREGATOR (self));
2676 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2677 g_return_if_fail (max_latency >= min_latency);
2680 if (self->priv->sub_latency_min != min_latency) {
2681 self->priv->sub_latency_min = min_latency;
2684 if (self->priv->sub_latency_max != max_latency) {
2685 self->priv->sub_latency_max = max_latency;
2690 SRC_BROADCAST (self);
2694 gst_element_post_message (GST_ELEMENT_CAST (self),
2695 gst_message_new_latency (GST_OBJECT_CAST (self)));