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));
1393 self->priv->has_peer_latency = FALSE;
1396 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1398 /* add the pad to the element */
1399 gst_element_add_pad (element, GST_PAD (agg_pad));
1401 return GST_PAD (agg_pad);
1404 /* Must be called with SRC_LOCK held */
1407 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1409 gboolean query_ret, live;
1410 GstClockTime our_latency, min, max;
1412 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1415 GST_WARNING_OBJECT (self, "Latency query failed");
1419 gst_query_parse_latency (query, &live, &min, &max);
1421 our_latency = self->priv->latency;
1423 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1424 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1425 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1429 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1430 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1431 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1432 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1433 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1437 self->priv->peer_latency_live = live;
1438 self->priv->peer_latency_min = min;
1439 self->priv->peer_latency_max = max;
1440 self->priv->has_peer_latency = TRUE;
1444 min += self->priv->sub_latency_min;
1445 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1446 && GST_CLOCK_TIME_IS_VALID (max))
1447 max += self->priv->sub_latency_max + our_latency;
1449 max = GST_CLOCK_TIME_NONE;
1451 SRC_BROADCAST (self);
1453 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1454 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1456 gst_query_set_latency (query, live, min, max);
1462 * MUST be called with the src_lock held.
1464 * See gst_aggregator_get_latency() for doc
1467 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1469 GstClockTime latency;
1471 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1473 if (!self->priv->has_peer_latency) {
1474 GstQuery *query = gst_query_new_latency ();
1477 ret = gst_aggregator_query_latency_unlocked (self, query);
1478 gst_query_unref (query);
1480 return GST_CLOCK_TIME_NONE;
1483 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1484 return GST_CLOCK_TIME_NONE;
1486 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1487 latency = self->priv->peer_latency_min;
1490 latency += self->priv->latency;
1491 latency += self->priv->sub_latency_min;
1497 * gst_aggregator_get_latency:
1498 * @self: a #GstAggregator
1500 * Retrieves the latency values reported by @self in response to the latency
1501 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1502 * will not wait for the clock.
1504 * Typically only called by subclasses.
1506 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1509 gst_aggregator_get_latency (GstAggregator * self)
1514 ret = gst_aggregator_get_latency_unlocked (self);
1521 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1523 GstAggregator *self = GST_AGGREGATOR (element);
1525 GST_STATE_LOCK (element);
1526 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1527 GST_STATE (element) < GST_STATE_PAUSED) {
1531 GstSeekType start_type, stop_type;
1534 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1535 &start, &stop_type, &stop);
1537 GST_OBJECT_LOCK (self);
1538 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1539 stop_type, stop, NULL);
1540 self->priv->seqnum = gst_event_get_seqnum (event);
1541 self->priv->first_buffer = FALSE;
1542 GST_OBJECT_UNLOCK (self);
1544 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1546 GST_STATE_UNLOCK (element);
1549 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1554 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1556 gboolean res = TRUE;
1558 switch (GST_QUERY_TYPE (query)) {
1559 case GST_QUERY_SEEKING:
1563 /* don't pass it along as some (file)sink might claim it does
1564 * whereas with a collectpads in between that will not likely work */
1565 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1566 gst_query_set_seeking (query, format, FALSE, 0, -1);
1571 case GST_QUERY_LATENCY:
1573 res = gst_aggregator_query_latency_unlocked (self, query);
1577 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1584 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1586 EventData *evdata = user_data;
1587 gboolean ret = TRUE;
1588 GstPad *peer = gst_pad_get_peer (pad);
1589 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1592 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1593 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1596 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1597 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1598 gst_object_unref (peer);
1603 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1604 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1606 GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1608 if (gst_pad_query (peer, seeking)) {
1611 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1613 if (seekable == FALSE) {
1614 GST_INFO_OBJECT (pad,
1615 "Source not seekable, We failed but it does not matter!");
1620 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1623 gst_query_unref (seeking);
1626 if (evdata->flush) {
1628 aggpad->priv->pending_flush_start = FALSE;
1629 aggpad->priv->pending_flush_stop = FALSE;
1630 PAD_UNLOCK (aggpad);
1633 evdata->one_actually_seeked = TRUE;
1636 evdata->result &= ret;
1638 /* Always send to all pads */
1643 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1644 GstEvent * event, gboolean flush, gboolean only_to_active_pads)
1648 evdata.event = event;
1649 evdata.result = TRUE;
1650 evdata.flush = flush;
1651 evdata.one_actually_seeked = FALSE;
1652 evdata.only_to_active_pads = only_to_active_pads;
1654 /* We first need to set all pads as flushing in a first pass
1655 * as flush_start flush_stop is sometimes sent synchronously
1656 * while we send the seek event */
1660 GST_OBJECT_LOCK (self);
1661 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1662 GstAggregatorPad *pad = l->data;
1665 pad->priv->pending_flush_start = TRUE;
1666 pad->priv->pending_flush_stop = FALSE;
1669 GST_OBJECT_UNLOCK (self);
1672 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1674 gst_event_unref (event);
1680 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1685 GstSeekType start_type, stop_type;
1689 GstAggregatorPrivate *priv = self->priv;
1691 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1692 &start, &stop_type, &stop);
1694 GST_INFO_OBJECT (self, "starting SEEK");
1696 flush = flags & GST_SEEK_FLAG_FLUSH;
1698 GST_OBJECT_LOCK (self);
1700 priv->pending_flush_start = TRUE;
1701 priv->flush_seeking = TRUE;
1704 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1705 stop_type, stop, NULL);
1707 /* Seeking sets a position */
1708 self->priv->first_buffer = FALSE;
1709 GST_OBJECT_UNLOCK (self);
1711 /* forward the seek upstream */
1713 gst_aggregator_forward_event_to_all_sinkpads (self, event, flush, FALSE);
1716 if (!evdata.result || !evdata.one_actually_seeked) {
1717 GST_OBJECT_LOCK (self);
1718 priv->flush_seeking = FALSE;
1719 priv->pending_flush_start = FALSE;
1720 GST_OBJECT_UNLOCK (self);
1723 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1725 return evdata.result;
1729 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1732 gboolean res = TRUE;
1734 switch (GST_EVENT_TYPE (event)) {
1735 case GST_EVENT_SEEK:
1737 gst_event_ref (event);
1738 res = gst_aggregator_do_seek (self, event);
1739 gst_event_unref (event);
1743 case GST_EVENT_NAVIGATION:
1745 /* navigation is rather pointless. */
1747 gst_event_unref (event);
1756 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
1757 * they will receive a QOS event that has earliest_time=0 (because we can't
1758 * have negative timestamps), and consider their buffer as too late */
1760 gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE,
1761 GST_EVENT_TYPE (event) == GST_EVENT_QOS);
1762 res = evdata.result;
1769 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1772 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1774 return klass->src_event (GST_AGGREGATOR (parent), event);
1778 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1781 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1783 return klass->src_query (GST_AGGREGATOR (parent), query);
1787 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1788 GstObject * parent, GstPadMode mode, gboolean active)
1790 GstAggregator *self = GST_AGGREGATOR (parent);
1791 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1793 if (klass->src_activate) {
1794 if (klass->src_activate (self, mode, active) == FALSE) {
1799 if (active == TRUE) {
1801 case GST_PAD_MODE_PUSH:
1803 GST_INFO_OBJECT (pad, "Activating pad!");
1804 gst_aggregator_start_srcpad_task (self);
1809 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1816 GST_INFO_OBJECT (self, "Deactivating srcpad");
1817 gst_aggregator_stop_srcpad_task (self, FALSE);
1823 gst_aggregator_default_sink_query (GstAggregator * self,
1824 GstAggregatorPad * aggpad, GstQuery * query)
1826 GstPad *pad = GST_PAD (aggpad);
1828 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1832 gst_aggregator_finalize (GObject * object)
1834 GstAggregator *self = (GstAggregator *) object;
1836 g_mutex_clear (&self->priv->src_lock);
1837 g_cond_clear (&self->priv->src_cond);
1839 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1843 * gst_aggregator_set_latency_property:
1844 * @agg: a #GstAggregator
1845 * @latency: the new latency value (in nanoseconds).
1847 * Sets the new latency value to @latency. This value is used to limit the
1848 * amount of time a pad waits for data to appear before considering the pad
1852 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1856 g_return_if_fail (GST_IS_AGGREGATOR (self));
1857 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1860 changed = (self->priv->latency != latency);
1865 GST_OBJECT_LOCK (self);
1866 /* First lock all the pads */
1867 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1868 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1872 self->priv->latency = latency;
1874 SRC_BROADCAST (self);
1876 /* Now wake up the pads */
1877 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1878 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1879 PAD_BROADCAST_EVENT (aggpad);
1880 PAD_UNLOCK (aggpad);
1882 GST_OBJECT_UNLOCK (self);
1888 gst_element_post_message (GST_ELEMENT_CAST (self),
1889 gst_message_new_latency (GST_OBJECT_CAST (self)));
1893 * gst_aggregator_get_latency_property:
1894 * @agg: a #GstAggregator
1896 * Gets the latency value. See gst_aggregator_set_latency for
1899 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1900 * before a pad is deemed unresponsive. A value of -1 means an
1904 gst_aggregator_get_latency_property (GstAggregator * agg)
1908 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1910 GST_OBJECT_LOCK (agg);
1911 res = agg->priv->latency;
1912 GST_OBJECT_UNLOCK (agg);
1918 gst_aggregator_set_property (GObject * object, guint prop_id,
1919 const GValue * value, GParamSpec * pspec)
1921 GstAggregator *agg = GST_AGGREGATOR (object);
1925 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1927 case PROP_START_TIME_SELECTION:
1928 agg->priv->start_time_selection = g_value_get_enum (value);
1930 case PROP_START_TIME:
1931 agg->priv->start_time = g_value_get_uint64 (value);
1934 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1940 gst_aggregator_get_property (GObject * object, guint prop_id,
1941 GValue * value, GParamSpec * pspec)
1943 GstAggregator *agg = GST_AGGREGATOR (object);
1947 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1949 case PROP_START_TIME_SELECTION:
1950 g_value_set_enum (value, agg->priv->start_time_selection);
1952 case PROP_START_TIME:
1953 g_value_set_uint64 (value, agg->priv->start_time);
1956 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1961 /* GObject vmethods implementations */
1963 gst_aggregator_class_init (GstAggregatorClass * klass)
1965 GObjectClass *gobject_class = (GObjectClass *) klass;
1966 GstElementClass *gstelement_class = (GstElementClass *) klass;
1968 aggregator_parent_class = g_type_class_peek_parent (klass);
1969 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1971 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1972 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1974 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1976 klass->sink_event = gst_aggregator_default_sink_event;
1977 klass->sink_query = gst_aggregator_default_sink_query;
1979 klass->src_event = gst_aggregator_default_src_event;
1980 klass->src_query = gst_aggregator_default_src_query;
1982 klass->create_new_pad = gst_aggregator_default_create_new_pad;
1984 gstelement_class->request_new_pad =
1985 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1986 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1987 gstelement_class->release_pad =
1988 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1989 gstelement_class->change_state =
1990 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1992 gobject_class->set_property = gst_aggregator_set_property;
1993 gobject_class->get_property = gst_aggregator_get_property;
1994 gobject_class->finalize = gst_aggregator_finalize;
1996 g_object_class_install_property (gobject_class, PROP_LATENCY,
1997 g_param_spec_int64 ("latency", "Buffer latency",
1998 "Additional latency in live mode to allow upstream "
1999 "to take longer to produce buffers for the current "
2000 "position (in nanoseconds)", 0,
2001 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
2002 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2004 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
2005 g_param_spec_enum ("start-time-selection", "Start Time Selection",
2006 "Decides which start time is output",
2007 gst_aggregator_start_time_selection_get_type (),
2008 DEFAULT_START_TIME_SELECTION,
2009 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2011 g_object_class_install_property (gobject_class, PROP_START_TIME,
2012 g_param_spec_uint64 ("start-time", "Start Time",
2013 "Start time to use if start-time-selection=set", 0,
2015 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2017 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
2021 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
2023 GstPadTemplate *pad_template;
2024 GstAggregatorPrivate *priv;
2026 g_return_if_fail (klass->aggregate != NULL);
2029 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2030 GstAggregatorPrivate);
2035 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2036 g_return_if_fail (pad_template != NULL);
2038 priv->max_padserial = -1;
2039 priv->tags_changed = FALSE;
2041 self->priv->peer_latency_live = FALSE;
2042 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2043 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2044 self->priv->has_peer_latency = FALSE;
2045 gst_aggregator_reset_flow_values (self);
2047 self->srcpad = gst_pad_new_from_template (pad_template, "src");
2049 gst_pad_set_event_function (self->srcpad,
2050 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2051 gst_pad_set_query_function (self->srcpad,
2052 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2053 gst_pad_set_activatemode_function (self->srcpad,
2054 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2056 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2058 self->priv->latency = DEFAULT_LATENCY;
2059 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2060 self->priv->start_time = DEFAULT_START_TIME;
2062 g_mutex_init (&self->priv->src_lock);
2063 g_cond_init (&self->priv->src_cond);
2066 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2067 * method to get to the padtemplates */
2069 gst_aggregator_get_type (void)
2071 static volatile gsize type = 0;
2073 if (g_once_init_enter (&type)) {
2075 static const GTypeInfo info = {
2076 sizeof (GstAggregatorClass),
2079 (GClassInitFunc) gst_aggregator_class_init,
2082 sizeof (GstAggregator),
2084 (GInstanceInitFunc) gst_aggregator_init,
2087 _type = g_type_register_static (GST_TYPE_ELEMENT,
2088 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2089 g_once_init_leave (&type, _type);
2094 /* Must be called with SRC lock and PAD lock held */
2096 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2098 /* Empty queue always has space */
2099 if (aggpad->priv->num_buffers == 0 && aggpad->priv->clipped_buffer == NULL)
2102 /* We also want at least two buffers, one is being processed and one is ready
2103 * for the next iteration when we operate in live mode. */
2104 if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2107 /* zero latency, if there is a buffer, it's full */
2108 if (self->priv->latency == 0)
2111 /* Allow no more buffers than the latency */
2112 return (aggpad->priv->time_level <= self->priv->latency);
2115 /* Must be called with the PAD_LOCK held */
2117 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2119 GstClockTime timestamp;
2121 if (GST_BUFFER_DTS_IS_VALID (buffer))
2122 timestamp = GST_BUFFER_DTS (buffer);
2124 timestamp = GST_BUFFER_PTS (buffer);
2126 if (timestamp == GST_CLOCK_TIME_NONE) {
2128 timestamp = aggpad->priv->head_position;
2130 timestamp = aggpad->priv->tail_position;
2134 if (GST_BUFFER_DURATION_IS_VALID (buffer))
2135 timestamp += GST_BUFFER_DURATION (buffer);
2138 aggpad->priv->head_position = timestamp;
2140 aggpad->priv->tail_position = timestamp;
2142 update_time_level (aggpad, head);
2145 static GstFlowReturn
2146 gst_aggregator_pad_chain_internal (GstAggregator * self,
2147 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2149 GstFlowReturn flow_return;
2150 GstClockTime buf_pts;
2152 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2154 PAD_FLUSH_LOCK (aggpad);
2157 flow_return = aggpad->priv->flow_return;
2158 if (flow_return != GST_FLOW_OK)
2161 if (aggpad->priv->pending_eos == TRUE)
2164 PAD_UNLOCK (aggpad);
2166 buf_pts = GST_BUFFER_PTS (buffer);
2168 aggpad->priv->first_buffer = FALSE;
2172 GST_OBJECT_LOCK (self);
2174 if (gst_aggregator_pad_has_space (self, aggpad)
2175 && aggpad->priv->flow_return == GST_FLOW_OK) {
2177 g_queue_push_head (&aggpad->priv->buffers, buffer);
2179 g_queue_push_tail (&aggpad->priv->buffers, buffer);
2180 apply_buffer (aggpad, buffer, head);
2181 aggpad->priv->num_buffers++;
2183 SRC_BROADCAST (self);
2187 flow_return = aggpad->priv->flow_return;
2188 if (flow_return != GST_FLOW_OK) {
2189 GST_OBJECT_UNLOCK (self);
2193 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2194 GST_OBJECT_UNLOCK (self);
2196 PAD_WAIT_EVENT (aggpad);
2198 PAD_UNLOCK (aggpad);
2201 if (self->priv->first_buffer) {
2202 GstClockTime start_time;
2204 switch (self->priv->start_time_selection) {
2205 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2209 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2210 GST_OBJECT_LOCK (aggpad);
2211 if (aggpad->priv->head_segment.format == GST_FORMAT_TIME) {
2212 start_time = buf_pts;
2213 if (start_time != -1) {
2214 start_time = MAX (start_time, aggpad->priv->head_segment.start);
2216 gst_segment_to_running_time (&aggpad->priv->head_segment,
2217 GST_FORMAT_TIME, start_time);
2221 GST_WARNING_OBJECT (aggpad,
2222 "Ignoring request of selecting the first start time "
2223 "as the segment is a %s segment instead of a time segment",
2224 gst_format_get_name (aggpad->segment.format));
2226 GST_OBJECT_UNLOCK (aggpad);
2228 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2229 start_time = self->priv->start_time;
2230 if (start_time == -1)
2235 if (start_time != -1) {
2236 if (self->segment.position == -1)
2237 self->segment.position = start_time;
2239 self->segment.position = MIN (start_time, self->segment.position);
2241 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2242 GST_TIME_ARGS (start_time));
2246 PAD_UNLOCK (aggpad);
2247 GST_OBJECT_UNLOCK (self);
2250 PAD_FLUSH_UNLOCK (aggpad);
2252 GST_DEBUG_OBJECT (aggpad, "Done chaining");
2257 PAD_UNLOCK (aggpad);
2258 PAD_FLUSH_UNLOCK (aggpad);
2260 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2261 gst_flow_get_name (flow_return));
2263 gst_buffer_unref (buffer);
2268 PAD_UNLOCK (aggpad);
2269 PAD_FLUSH_UNLOCK (aggpad);
2271 gst_buffer_unref (buffer);
2272 GST_DEBUG_OBJECT (aggpad, "We are EOS already...");
2274 return GST_FLOW_EOS;
2277 static GstFlowReturn
2278 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2280 return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2281 GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2285 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2288 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2289 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2291 if (GST_QUERY_IS_SERIALIZED (query)) {
2294 while (!gst_aggregator_pad_queue_is_empty (aggpad)
2295 && aggpad->priv->flow_return == GST_FLOW_OK) {
2296 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2297 PAD_WAIT_EVENT (aggpad);
2300 if (aggpad->priv->flow_return != GST_FLOW_OK)
2303 PAD_UNLOCK (aggpad);
2306 return klass->sink_query (GST_AGGREGATOR (parent),
2307 GST_AGGREGATOR_PAD (pad), query);
2310 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2311 gst_flow_get_name (aggpad->priv->flow_return));
2312 PAD_UNLOCK (aggpad);
2316 static GstFlowReturn
2317 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2320 GstFlowReturn ret = GST_FLOW_OK;
2321 GstAggregator *self = GST_AGGREGATOR (parent);
2322 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2323 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2325 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
2326 /* && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE */ ) {
2330 if (aggpad->priv->flow_return != GST_FLOW_OK
2331 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2332 ret = aggpad->priv->flow_return;
2336 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2337 GST_OBJECT_LOCK (aggpad);
2338 gst_event_copy_segment (event, &aggpad->priv->head_segment);
2339 aggpad->priv->head_position = aggpad->priv->head_segment.position;
2340 update_time_level (aggpad, TRUE);
2341 GST_OBJECT_UNLOCK (aggpad);
2344 if (!gst_aggregator_pad_queue_is_empty (aggpad) &&
2345 GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2346 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT,
2348 g_queue_push_head (&aggpad->priv->buffers, event);
2350 SRC_BROADCAST (self);
2352 PAD_UNLOCK (aggpad);
2357 gboolean is_caps = (GST_EVENT_TYPE (event) == GST_EVENT_CAPS);
2359 if (!klass->sink_event (self, aggpad, event)) {
2360 /* Copied from GstPad to convert boolean to a GstFlowReturn in
2361 * the event handling func */
2362 ret = is_caps ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_ERROR;
2369 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2370 gst_flow_get_name (aggpad->priv->flow_return));
2371 PAD_UNLOCK (aggpad);
2373 if (GST_EVENT_IS_STICKY (event))
2374 gst_pad_store_sticky_event (pad, event);
2375 gst_event_unref (event);
2381 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2382 GstObject * parent, GstPadMode mode, gboolean active)
2384 GstAggregator *self = GST_AGGREGATOR (parent);
2385 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2387 if (active == FALSE) {
2389 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2390 SRC_BROADCAST (self);
2394 aggpad->priv->flow_return = GST_FLOW_OK;
2395 PAD_BROADCAST_EVENT (aggpad);
2396 PAD_UNLOCK (aggpad);
2402 /***********************************
2403 * GstAggregatorPad implementation *
2404 ************************************/
2405 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2408 gst_aggregator_pad_constructed (GObject * object)
2410 GstPad *pad = GST_PAD (object);
2412 gst_pad_set_chain_function (pad,
2413 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2414 gst_pad_set_event_full_function_full (pad,
2415 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
2416 gst_pad_set_query_function (pad,
2417 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2418 gst_pad_set_activatemode_function (pad,
2419 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2423 gst_aggregator_pad_finalize (GObject * object)
2425 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2427 g_cond_clear (&pad->priv->event_cond);
2428 g_mutex_clear (&pad->priv->flush_lock);
2429 g_mutex_clear (&pad->priv->lock);
2431 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2435 gst_aggregator_pad_dispose (GObject * object)
2437 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2439 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2441 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2445 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2447 GObjectClass *gobject_class = (GObjectClass *) klass;
2449 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2451 gobject_class->constructed = gst_aggregator_pad_constructed;
2452 gobject_class->finalize = gst_aggregator_pad_finalize;
2453 gobject_class->dispose = gst_aggregator_pad_dispose;
2457 gst_aggregator_pad_init (GstAggregatorPad * pad)
2460 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2461 GstAggregatorPadPrivate);
2463 g_queue_init (&pad->priv->buffers);
2464 g_cond_init (&pad->priv->event_cond);
2466 g_mutex_init (&pad->priv->flush_lock);
2467 g_mutex_init (&pad->priv->lock);
2469 gst_aggregator_pad_reset_unlocked (pad);
2472 /* Must be called with the PAD_LOCK held */
2474 gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad)
2476 pad->priv->num_buffers--;
2477 GST_TRACE_OBJECT (pad, "Consuming buffer");
2478 if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
2479 pad->priv->pending_eos = FALSE;
2480 pad->priv->eos = TRUE;
2482 PAD_BROADCAST_EVENT (pad);
2485 /* Must be called with the PAD_LOCK held */
2487 gst_aggregator_pad_clip_buffer_unlocked (GstAggregatorPad * pad)
2489 GstAggregator *self = NULL;
2490 GstAggregatorClass *aggclass;
2491 GstBuffer *buffer = NULL;
2493 while (pad->priv->clipped_buffer == NULL &&
2494 GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->buffers))) {
2495 buffer = g_queue_pop_tail (&pad->priv->buffers);
2497 apply_buffer (pad, buffer, FALSE);
2499 /* We only take the parent here so that it's not taken if the buffer is
2500 * already clipped or if the queue is empty.
2503 self = GST_AGGREGATOR (gst_pad_get_parent_element (GST_PAD (pad)));
2505 gst_buffer_unref (buffer);
2509 aggclass = GST_AGGREGATOR_GET_CLASS (self);
2512 if (aggclass->clip) {
2513 GST_TRACE_OBJECT (pad, "Clipping: %" GST_PTR_FORMAT, buffer);
2515 buffer = aggclass->clip (self, pad, buffer);
2517 if (buffer == NULL) {
2518 gst_aggregator_pad_buffer_consumed (pad);
2519 GST_TRACE_OBJECT (pad, "Clipping consumed the buffer");
2523 pad->priv->clipped_buffer = buffer;
2527 gst_object_unref (self);
2531 * gst_aggregator_pad_steal_buffer:
2532 * @pad: the pad to get buffer from
2534 * Steal the ref to the buffer currently queued in @pad.
2536 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2537 * queued. You should unref the buffer after usage.
2540 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2546 gst_aggregator_pad_clip_buffer_unlocked (pad);
2548 buffer = pad->priv->clipped_buffer;
2549 pad->priv->clipped_buffer = NULL;
2552 gst_aggregator_pad_buffer_consumed (pad);
2553 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2562 * gst_aggregator_pad_drop_buffer:
2563 * @pad: the pad where to drop any pending buffer
2565 * Drop the buffer currently queued in @pad.
2567 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2570 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2574 buf = gst_aggregator_pad_steal_buffer (pad);
2579 gst_buffer_unref (buf);
2584 * gst_aggregator_pad_get_buffer:
2585 * @pad: the pad to get buffer from
2587 * Returns: (transfer full): A reference to the buffer in @pad or
2588 * NULL if no buffer was queued. You should unref the buffer after
2592 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2598 gst_aggregator_pad_clip_buffer_unlocked (pad);
2600 if (pad->priv->clipped_buffer) {
2601 buffer = gst_buffer_ref (pad->priv->clipped_buffer);
2611 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2616 is_eos = pad->priv->eos;
2623 * gst_aggregator_merge_tags:
2624 * @self: a #GstAggregator
2625 * @tags: a #GstTagList to merge
2626 * @mode: the #GstTagMergeMode to use
2628 * Adds tags to so-called pending tags, which will be processed
2629 * before pushing out data downstream.
2631 * Note that this is provided for convenience, and the subclass is
2632 * not required to use this and can still do tag handling on its own.
2637 gst_aggregator_merge_tags (GstAggregator * self,
2638 const GstTagList * tags, GstTagMergeMode mode)
2642 g_return_if_fail (GST_IS_AGGREGATOR (self));
2643 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2645 /* FIXME Check if we can use OBJECT lock here! */
2646 GST_OBJECT_LOCK (self);
2648 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2649 otags = self->priv->tags;
2650 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2652 gst_tag_list_unref (otags);
2653 self->priv->tags_changed = TRUE;
2654 GST_OBJECT_UNLOCK (self);
2658 * gst_aggregator_set_latency:
2659 * @self: a #GstAggregator
2660 * @min_latency: minimum latency
2661 * @max_latency: maximum latency
2663 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2664 * latency is. Will also post a LATENCY message on the bus so the pipeline
2665 * can reconfigure its global latency.
2668 gst_aggregator_set_latency (GstAggregator * self,
2669 GstClockTime min_latency, GstClockTime max_latency)
2671 gboolean changed = FALSE;
2673 g_return_if_fail (GST_IS_AGGREGATOR (self));
2674 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2675 g_return_if_fail (max_latency >= min_latency);
2678 if (self->priv->sub_latency_min != min_latency) {
2679 self->priv->sub_latency_min = min_latency;
2682 if (self->priv->sub_latency_max != max_latency) {
2683 self->priv->sub_latency_max = max_latency;
2688 SRC_BROADCAST (self);
2692 gst_element_post_message (GST_ELEMENT_CAST (self),
2693 gst_message_new_latency (GST_OBJECT_CAST (self)));