1 /* GStreamer aggregator base class
2 * Copyright (C) 2014 Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
3 * Copyright (C) 2014 Thibault Saunier <tsaunier@gnome.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 * SECTION: gstaggregator
24 * @title: GstAggregator
25 * @short_description: manages a set of pads with the purpose of
26 * aggregating their buffers.
27 * @see_also: gstcollectpads for historical reasons.
29 * Manages a set of pads with the purpose of aggregating their buffers.
30 * Control is given to the subclass when all pads have data.
32 * * Base class for mixers and muxers. Subclasses should at least implement
33 * the #GstAggregatorClass.aggregate() virtual method.
35 * * When data is queued on all pads, tha aggregate vmethod is called.
37 * * One can peek at the data on any given GstAggregatorPad with the
38 * gst_aggregator_pad_get_buffer () method, and take ownership of it
39 * with the gst_aggregator_pad_steal_buffer () method. When a buffer
40 * has been taken with steal_buffer (), a new buffer can be queued
43 * * If the subclass wishes to push a buffer downstream in its aggregate
44 * implementation, it should do so through the
45 * gst_aggregator_finish_buffer () method. This method will take care
46 * of sending and ordering mandatory events such as stream start, caps
49 * * Same goes for EOS events, which should not be pushed directly by the
50 * subclass, it should instead return GST_FLOW_EOS in its aggregate
53 * * Note that the aggregator logic regarding gap event handling is to turn
54 * these into gap buffers with matching PTS and duration. It will also
55 * flag these buffers with GST_BUFFER_FLAG_GAP and GST_BUFFER_FLAG_DROPPABLE
56 * to ease their identification and subsequent processing.
64 #include <string.h> /* strlen */
66 #include "gstaggregator.h"
70 GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
71 GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
72 GST_AGGREGATOR_START_TIME_SELECTION_SET
73 } GstAggregatorStartTimeSelection;
76 gst_aggregator_start_time_selection_get_type (void)
78 static GType gtype = 0;
81 static const GEnumValue values[] = {
82 {GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
83 "Start at 0 running time (default)", "zero"},
84 {GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
85 "Start at first observed input running time", "first"},
86 {GST_AGGREGATOR_START_TIME_SELECTION_SET,
87 "Set start time with start-time property", "set"},
91 gtype = g_enum_register_static ("GstAggregatorStartTimeSelection", values);
96 /* Might become API */
97 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
98 const GstTagList * tags, GstTagMergeMode mode);
99 static void gst_aggregator_set_latency_property (GstAggregator * agg,
101 static gint64 gst_aggregator_get_latency_property (GstAggregator * agg);
104 /* Locking order, locks in this element must always be taken in this order
106 * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
107 * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
108 * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
109 * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
110 * standard element object lock -> GST_OBJECT_LOCK(agg)
111 * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
112 * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
113 * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
117 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
119 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
120 #define GST_CAT_DEFAULT aggregator_debug
122 /* GstAggregatorPad definitions */
123 #define PAD_LOCK(pad) G_STMT_START { \
124 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
126 g_mutex_lock(&pad->priv->lock); \
127 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
131 #define PAD_UNLOCK(pad) G_STMT_START { \
132 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
134 g_mutex_unlock(&pad->priv->lock); \
135 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
140 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
141 GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p", \
143 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
144 (&((GstAggregatorPad*)pad)->priv->lock)); \
145 GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
149 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
150 GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p", \
152 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
156 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
157 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
159 g_mutex_lock(&pad->priv->flush_lock); \
160 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
164 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
165 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
167 g_mutex_unlock(&pad->priv->flush_lock); \
168 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
172 #define SRC_LOCK(self) G_STMT_START { \
173 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
175 g_mutex_lock(&self->priv->src_lock); \
176 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
180 #define SRC_UNLOCK(self) G_STMT_START { \
181 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
183 g_mutex_unlock(&self->priv->src_lock); \
184 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
188 #define SRC_WAIT(self) G_STMT_START { \
189 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
191 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
192 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
196 #define SRC_BROADCAST(self) G_STMT_START { \
197 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
199 if (self->priv->aggregate_id) \
200 gst_clock_id_unschedule (self->priv->aggregate_id); \
201 g_cond_broadcast(&(self->priv->src_cond)); \
204 struct _GstAggregatorPadPrivate
206 /* Following fields are protected by the PAD_LOCK */
207 GstFlowReturn flow_return;
208 gboolean pending_flush_start;
209 gboolean pending_flush_stop;
210 gboolean pending_eos;
212 gboolean first_buffer;
216 GstClockTime head_position;
217 GstClockTime tail_position;
218 GstClockTime head_time;
219 GstClockTime tail_time;
220 GstClockTime time_level;
226 /* This lock prevents a flush start processing happening while
227 * the chain function is also happening.
233 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
235 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (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->clip_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;
253 return klass->flush (aggpad, agg);
258 /*************************************
259 * GstAggregator implementation *
260 *************************************/
261 static GstElementClass *aggregator_parent_class = NULL;
263 /* All members are protected by the object lock unless otherwise noted */
265 struct _GstAggregatorPrivate
269 /* Our state is >= PAUSED */
270 gboolean running; /* protected by src_lock */
273 gboolean send_stream_start; /* protected by srcpad stream lock */
274 gboolean send_segment;
275 gboolean flush_seeking;
276 gboolean pending_flush_start;
277 gboolean send_eos; /* protected by srcpad stream lock */
279 GstCaps *srccaps; /* protected by the srcpad stream lock */
282 gboolean tags_changed;
284 gboolean peer_latency_live; /* protected by src_lock */
285 GstClockTime peer_latency_min; /* protected by src_lock */
286 GstClockTime peer_latency_max; /* protected by src_lock */
287 gboolean has_peer_latency; /* protected by src_lock */
289 GstClockTime sub_latency_min; /* protected by src_lock */
290 GstClockTime sub_latency_max; /* protected by src_lock */
293 GstClockID aggregate_id; /* protected by src_lock */
297 gboolean first_buffer; /* protected by object lock */
298 GstAggregatorStartTimeSelection start_time_selection;
299 GstClockTime start_time;
302 gint64 latency; /* protected by both src_lock and all pad locks */
310 gboolean only_to_active_pads;
312 gboolean one_actually_seeked;
315 #define DEFAULT_LATENCY 0
316 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
317 #define DEFAULT_START_TIME (-1)
323 PROP_START_TIME_SELECTION,
328 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
329 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
332 * gst_aggregator_iterate_sinkpads:
333 * @self: The #GstAggregator
334 * @func: (scope call): The function to call.
335 * @user_data: (closure): The data to pass to @func.
337 * Iterate the sinkpads of aggregator to call a function on them.
339 * This method guarantees that @func will be called only once for each
343 gst_aggregator_iterate_sinkpads (GstAggregator * self,
344 GstAggregatorPadForeachFunc func, gpointer user_data)
346 gboolean result = FALSE;
348 gboolean done = FALSE;
349 GValue item = { 0, };
350 GList *seen_pads = NULL;
352 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
358 switch (gst_iterator_next (iter, &item)) {
359 case GST_ITERATOR_OK:
361 GstAggregatorPad *pad;
363 pad = g_value_get_object (&item);
365 /* if already pushed, skip. FIXME, find something faster to tag pads */
366 if (pad == NULL || g_list_find (seen_pads, pad)) {
367 g_value_reset (&item);
371 GST_LOG_OBJECT (pad, "calling function %s on pad",
372 GST_DEBUG_FUNCPTR_NAME (func));
374 result = func (self, pad, user_data);
378 seen_pads = g_list_prepend (seen_pads, pad);
380 g_value_reset (&item);
383 case GST_ITERATOR_RESYNC:
384 gst_iterator_resync (iter);
386 case GST_ITERATOR_ERROR:
387 GST_ERROR_OBJECT (self,
388 "Could not iterate over internally linked pads");
391 case GST_ITERATOR_DONE:
396 g_value_unset (&item);
397 gst_iterator_free (iter);
399 if (seen_pads == NULL) {
400 GST_DEBUG_OBJECT (self, "No pad seen");
404 g_list_free (seen_pads);
411 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
413 return (g_queue_peek_tail (&pad->priv->buffers) == NULL);
417 gst_aggregator_check_pads_ready (GstAggregator * self)
419 GstAggregatorPad *pad;
421 gboolean have_data = TRUE;
423 GST_LOG_OBJECT (self, "checking pads");
425 GST_OBJECT_LOCK (self);
427 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
428 if (sinkpads == NULL)
431 for (l = sinkpads; l != NULL; l = l->next) {
436 if (gst_aggregator_pad_queue_is_empty (pad)) {
437 if (!pad->priv->eos) {
440 /* If not live we need data on all pads, so leave the loop */
441 if (!self->priv->peer_latency_live) {
446 } else if (self->priv->peer_latency_live) {
447 /* In live mode, having a single pad with buffers is enough to
448 * generate a start time from it. In non-live mode all pads need
451 self->priv->first_buffer = FALSE;
460 self->priv->first_buffer = FALSE;
462 GST_OBJECT_UNLOCK (self);
463 GST_LOG_OBJECT (self, "pads are ready");
468 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
469 GST_OBJECT_UNLOCK (self);
474 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
475 GST_OBJECT_UNLOCK (self);
481 gst_aggregator_reset_flow_values (GstAggregator * self)
483 GST_OBJECT_LOCK (self);
484 self->priv->send_stream_start = TRUE;
485 self->priv->send_segment = TRUE;
486 gst_segment_init (&self->segment, GST_FORMAT_TIME);
487 self->priv->first_buffer = TRUE;
488 GST_OBJECT_UNLOCK (self);
492 gst_aggregator_push_mandatory_events (GstAggregator * self)
494 GstAggregatorPrivate *priv = self->priv;
495 GstEvent *segment = NULL;
496 GstEvent *tags = NULL;
498 if (self->priv->send_stream_start) {
501 GST_INFO_OBJECT (self, "pushing stream start");
502 /* stream-start (FIXME: create id based on input ids) */
503 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
504 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
505 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
507 self->priv->send_stream_start = FALSE;
510 if (self->priv->srccaps) {
512 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
513 self->priv->srccaps);
514 if (!gst_pad_push_event (self->srcpad,
515 gst_event_new_caps (self->priv->srccaps))) {
516 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
518 gst_caps_unref (self->priv->srccaps);
519 self->priv->srccaps = NULL;
522 GST_OBJECT_LOCK (self);
523 if (self->priv->send_segment && !self->priv->flush_seeking) {
524 segment = gst_event_new_segment (&self->segment);
526 if (!self->priv->seqnum)
527 self->priv->seqnum = gst_event_get_seqnum (segment);
529 gst_event_set_seqnum (segment, self->priv->seqnum);
530 self->priv->send_segment = FALSE;
532 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
535 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
536 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
537 priv->tags_changed = FALSE;
539 GST_OBJECT_UNLOCK (self);
542 gst_pad_push_event (self->srcpad, segment);
544 gst_pad_push_event (self->srcpad, tags);
549 * gst_aggregator_set_src_caps:
550 * @self: The #GstAggregator
551 * @caps: The #GstCaps to set on the src pad.
553 * Sets the caps to be used on the src pad.
556 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
558 GST_PAD_STREAM_LOCK (self->srcpad);
559 gst_caps_replace (&self->priv->srccaps, caps);
560 gst_aggregator_push_mandatory_events (self);
561 GST_PAD_STREAM_UNLOCK (self->srcpad);
565 * gst_aggregator_finish_buffer:
566 * @self: The #GstAggregator
567 * @buffer: (transfer full): the #GstBuffer to push.
569 * This method will push the provided output buffer downstream. If needed,
570 * mandatory events such as stream-start, caps, and segment events will be
571 * sent before pushing the buffer.
574 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
576 gst_aggregator_push_mandatory_events (self);
578 GST_OBJECT_LOCK (self);
579 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
580 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
581 GST_OBJECT_UNLOCK (self);
582 return gst_pad_push (self->srcpad, buffer);
584 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
585 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
586 GST_OBJECT_UNLOCK (self);
587 gst_buffer_unref (buffer);
593 gst_aggregator_push_eos (GstAggregator * self)
596 gst_aggregator_push_mandatory_events (self);
598 event = gst_event_new_eos ();
600 GST_OBJECT_LOCK (self);
601 self->priv->send_eos = FALSE;
602 gst_event_set_seqnum (event, self->priv->seqnum);
603 GST_OBJECT_UNLOCK (self);
605 gst_pad_push_event (self->srcpad, event);
609 gst_aggregator_get_next_time (GstAggregator * self)
611 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
613 if (klass->get_next_time)
614 return klass->get_next_time (self);
616 return GST_CLOCK_TIME_NONE;
620 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
622 GstClockTime latency;
630 latency = gst_aggregator_get_latency_unlocked (self);
632 if (gst_aggregator_check_pads_ready (self)) {
633 GST_DEBUG_OBJECT (self, "all pads have data");
639 /* Before waiting, check if we're actually still running */
640 if (!self->priv->running || !self->priv->send_eos) {
646 start = gst_aggregator_get_next_time (self);
648 /* If we're not live, or if we use the running time
649 * of the first buffer as start time, we wait until
650 * all pads have buffers.
651 * Otherwise (i.e. if we are live!), we wait on the clock
652 * and if a pad does not have a buffer in time we ignore
655 GST_OBJECT_LOCK (self);
656 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
657 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
658 !GST_CLOCK_TIME_IS_VALID (start) ||
659 (self->priv->first_buffer
660 && self->priv->start_time_selection ==
661 GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
662 /* We wake up here when something happened, and below
663 * then check if we're ready now. If we return FALSE,
664 * we will be directly called again.
666 GST_OBJECT_UNLOCK (self);
669 GstClockTime base_time, time;
671 GstClockReturn status;
672 GstClockTimeDiff jitter;
674 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
675 GST_TIME_ARGS (start));
677 base_time = GST_ELEMENT_CAST (self)->base_time;
678 clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
679 GST_OBJECT_UNLOCK (self);
681 time = base_time + start;
684 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
685 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
686 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
687 GST_TIME_ARGS (time),
688 GST_TIME_ARGS (base_time),
689 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
690 GST_TIME_ARGS (gst_clock_get_time (clock)));
692 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
693 gst_object_unref (clock);
697 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
700 if (self->priv->aggregate_id) {
701 gst_clock_id_unref (self->priv->aggregate_id);
702 self->priv->aggregate_id = NULL;
705 GST_DEBUG_OBJECT (self,
706 "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
707 status, GST_STIME_ARGS (jitter));
710 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
717 res = gst_aggregator_check_pads_ready (self);
724 check_events (GstAggregator * self, GstAggregatorPad * pad, gpointer user_data)
726 GstEvent *event = NULL;
727 GstAggregatorClass *klass = NULL;
728 gboolean *processed_event = user_data;
734 if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
735 pad->priv->pending_eos = FALSE;
736 pad->priv->eos = TRUE;
738 if (GST_IS_EVENT (g_queue_peek_tail (&pad->priv->buffers))) {
739 event = g_queue_pop_tail (&pad->priv->buffers);
740 PAD_BROADCAST_EVENT (pad);
745 *processed_event = TRUE;
747 klass = GST_AGGREGATOR_GET_CLASS (self);
749 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
750 klass->sink_event (self, pad, event);
752 } while (event != NULL);
758 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
759 GstFlowReturn flow_return, gboolean full)
764 if (flow_return == GST_FLOW_NOT_LINKED)
765 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
767 aggpad->priv->flow_return = flow_return;
769 item = g_queue_peek_head_link (&aggpad->priv->buffers);
771 GList *next = item->next;
773 /* In partial flush, we do like the pad, we get rid of non-sticky events
776 if (full || GST_IS_BUFFER (item->data) ||
777 GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
778 GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
779 !GST_EVENT_IS_STICKY (item->data)) {
780 gst_mini_object_unref (item->data);
781 g_queue_delete_link (&aggpad->priv->buffers, item);
785 aggpad->priv->num_buffers = 0;
787 PAD_BROADCAST_EVENT (aggpad);
792 gst_aggregator_aggregate_func (GstAggregator * self)
794 GstAggregatorPrivate *priv = self->priv;
795 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
796 gboolean timeout = FALSE;
798 if (self->priv->running == FALSE) {
799 GST_DEBUG_OBJECT (self, "Not running anymore");
803 GST_LOG_OBJECT (self, "Checking aggregate");
804 while (priv->send_eos && priv->running) {
805 GstFlowReturn flow_return;
806 gboolean processed_event = FALSE;
808 gst_aggregator_iterate_sinkpads (self, check_events, NULL);
810 if (!gst_aggregator_wait_and_check (self, &timeout))
813 gst_aggregator_iterate_sinkpads (self, check_events, &processed_event);
817 GST_TRACE_OBJECT (self, "Actually aggregating!");
818 flow_return = klass->aggregate (self, timeout);
820 GST_OBJECT_LOCK (self);
821 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
822 /* We don't want to set the pads to flushing, but we want to
823 * stop the thread, so just break here */
824 GST_OBJECT_UNLOCK (self);
827 GST_OBJECT_UNLOCK (self);
829 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
830 gst_aggregator_push_eos (self);
833 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
835 if (flow_return != GST_FLOW_OK) {
838 GST_OBJECT_LOCK (self);
839 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
840 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
842 gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
844 GST_OBJECT_UNLOCK (self);
849 /* Pause the task here, the only ways to get here are:
850 * 1) We're stopping, in which case the task is stopped anyway
851 * 2) We got a flow error above, in which case it might take
852 * some time to forward the flow return upstream and we
853 * would otherwise call the task function over and over
854 * again without doing anything
856 gst_pad_pause_task (self->srcpad);
860 gst_aggregator_start (GstAggregator * self)
862 GstAggregatorClass *klass;
865 self->priv->send_stream_start = TRUE;
866 self->priv->send_segment = TRUE;
867 self->priv->send_eos = TRUE;
868 self->priv->srccaps = NULL;
870 klass = GST_AGGREGATOR_GET_CLASS (self);
873 result = klass->start (self);
881 _check_pending_flush_stop (GstAggregatorPad * pad)
886 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
893 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
897 GST_INFO_OBJECT (self, "%s srcpad task",
898 flush_start ? "Pausing" : "Stopping");
901 self->priv->running = FALSE;
902 SRC_BROADCAST (self);
906 res = gst_pad_push_event (self->srcpad, flush_start);
909 gst_pad_stop_task (self->srcpad);
915 gst_aggregator_start_srcpad_task (GstAggregator * self)
917 GST_INFO_OBJECT (self, "Starting srcpad task");
919 self->priv->running = TRUE;
920 gst_pad_start_task (GST_PAD (self->srcpad),
921 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
925 gst_aggregator_flush (GstAggregator * self)
927 GstFlowReturn ret = GST_FLOW_OK;
928 GstAggregatorPrivate *priv = self->priv;
929 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
931 GST_DEBUG_OBJECT (self, "Flushing everything");
932 GST_OBJECT_LOCK (self);
933 priv->send_segment = TRUE;
934 priv->flush_seeking = FALSE;
935 priv->tags_changed = FALSE;
936 GST_OBJECT_UNLOCK (self);
938 ret = klass->flush (self);
944 /* Called with GstAggregator's object lock held */
947 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
950 GstAggregatorPad *tmppad;
952 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
953 tmppad = (GstAggregatorPad *) tmp->data;
955 if (_check_pending_flush_stop (tmppad) == FALSE) {
956 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
957 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
966 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
969 GstAggregatorPrivate *priv = self->priv;
970 GstAggregatorPadPrivate *padpriv = aggpad->priv;
972 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
974 PAD_FLUSH_LOCK (aggpad);
976 if (padpriv->pending_flush_start) {
977 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
979 padpriv->pending_flush_start = FALSE;
980 padpriv->pending_flush_stop = TRUE;
984 GST_OBJECT_LOCK (self);
985 if (priv->flush_seeking) {
986 /* If flush_seeking we forward the first FLUSH_START */
987 if (priv->pending_flush_start) {
988 priv->pending_flush_start = FALSE;
989 GST_OBJECT_UNLOCK (self);
991 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
992 gst_aggregator_stop_srcpad_task (self, event);
994 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
995 GST_PAD_STREAM_LOCK (self->srcpad);
996 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
999 GST_OBJECT_UNLOCK (self);
1000 gst_event_unref (event);
1003 GST_OBJECT_UNLOCK (self);
1004 gst_event_unref (event);
1006 PAD_FLUSH_UNLOCK (aggpad);
1009 /* Must be called with the the PAD_LOCK held */
1011 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1014 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->head_position) &&
1015 aggpad->clip_segment.format == GST_FORMAT_TIME)
1016 aggpad->priv->head_time =
1017 gst_segment_to_running_time (&aggpad->clip_segment,
1018 GST_FORMAT_TIME, aggpad->priv->head_position);
1020 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
1022 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_position) &&
1023 aggpad->segment.format == GST_FORMAT_TIME)
1024 aggpad->priv->tail_time =
1025 gst_segment_to_running_time (&aggpad->segment,
1026 GST_FORMAT_TIME, aggpad->priv->tail_position);
1028 aggpad->priv->tail_time = aggpad->priv->head_time;
1031 if (aggpad->priv->head_time == GST_CLOCK_TIME_NONE ||
1032 aggpad->priv->tail_time == GST_CLOCK_TIME_NONE) {
1033 aggpad->priv->time_level = 0;
1037 if (aggpad->priv->tail_time > aggpad->priv->head_time)
1038 aggpad->priv->time_level = 0;
1040 aggpad->priv->time_level = aggpad->priv->head_time -
1041 aggpad->priv->tail_time;
1045 /* GstAggregator vmethods default implementations */
1047 gst_aggregator_default_sink_event (GstAggregator * self,
1048 GstAggregatorPad * aggpad, GstEvent * event)
1050 gboolean res = TRUE;
1051 GstPad *pad = GST_PAD (aggpad);
1052 GstAggregatorPrivate *priv = self->priv;
1054 switch (GST_EVENT_TYPE (event)) {
1055 case GST_EVENT_FLUSH_START:
1057 gst_aggregator_flush_start (self, aggpad, event);
1058 /* We forward only in one case: right after flush_seeking */
1062 case GST_EVENT_FLUSH_STOP:
1064 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
1066 gst_aggregator_pad_flush (aggpad, self);
1067 GST_OBJECT_LOCK (self);
1068 if (priv->flush_seeking) {
1069 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1070 if (gst_aggregator_all_flush_stop_received_locked (self)) {
1071 GST_OBJECT_UNLOCK (self);
1072 /* That means we received FLUSH_STOP/FLUSH_STOP on
1073 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1074 gst_aggregator_flush (self);
1075 gst_pad_push_event (self->srcpad, event);
1078 priv->send_eos = TRUE;
1079 SRC_BROADCAST (self);
1082 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1083 GST_PAD_STREAM_UNLOCK (self->srcpad);
1084 gst_aggregator_start_srcpad_task (self);
1086 GST_OBJECT_UNLOCK (self);
1089 GST_OBJECT_UNLOCK (self);
1092 aggpad->priv->first_buffer = TRUE;
1094 /* We never forward the event */
1099 GST_DEBUG_OBJECT (aggpad, "EOS");
1101 /* We still have a buffer, and we don't want the subclass to have to
1102 * check for it. Mark pending_eos, eos will be set when steal_buffer is
1107 if (gst_aggregator_pad_queue_is_empty (aggpad)) {
1108 aggpad->priv->eos = TRUE;
1110 aggpad->priv->pending_eos = TRUE;
1112 PAD_UNLOCK (aggpad);
1114 SRC_BROADCAST (self);
1118 case GST_EVENT_SEGMENT:
1121 GST_OBJECT_LOCK (aggpad);
1122 gst_event_copy_segment (event, &aggpad->segment);
1123 update_time_level (aggpad, FALSE);
1124 GST_OBJECT_UNLOCK (aggpad);
1125 PAD_UNLOCK (aggpad);
1127 GST_OBJECT_LOCK (self);
1128 self->priv->seqnum = gst_event_get_seqnum (event);
1129 GST_OBJECT_UNLOCK (self);
1132 case GST_EVENT_STREAM_START:
1138 GstClockTime pts, endpts;
1139 GstClockTime duration;
1142 gst_event_parse_gap (event, &pts, &duration);
1143 gapbuf = gst_buffer_new ();
1145 if (GST_CLOCK_TIME_IS_VALID (duration))
1146 endpts = pts + duration;
1148 endpts = GST_CLOCK_TIME_NONE;
1150 GST_OBJECT_LOCK (aggpad);
1151 res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1153 GST_OBJECT_UNLOCK (aggpad);
1156 GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1160 if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1161 duration = endpts - pts;
1163 duration = GST_CLOCK_TIME_NONE;
1165 GST_BUFFER_PTS (gapbuf) = pts;
1166 GST_BUFFER_DURATION (gapbuf) = duration;
1167 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1168 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1170 if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1172 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1182 gst_event_parse_tag (event, &tags);
1184 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1185 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1186 gst_event_unref (event);
1198 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1199 return gst_pad_event_default (pad, GST_OBJECT (self), event);
1202 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1204 gst_event_unref (event);
1209 static inline gboolean
1210 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1211 gpointer unused_udata)
1213 gst_aggregator_pad_flush (pad, self);
1219 gst_aggregator_stop (GstAggregator * agg)
1221 GstAggregatorClass *klass;
1224 gst_aggregator_reset_flow_values (agg);
1226 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1228 klass = GST_AGGREGATOR_GET_CLASS (agg);
1231 result = klass->stop (agg);
1235 agg->priv->has_peer_latency = FALSE;
1236 agg->priv->peer_latency_live = FALSE;
1237 agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1239 if (agg->priv->tags)
1240 gst_tag_list_unref (agg->priv->tags);
1241 agg->priv->tags = NULL;
1246 /* GstElement vmethods implementations */
1247 static GstStateChangeReturn
1248 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1250 GstStateChangeReturn ret;
1251 GstAggregator *self = GST_AGGREGATOR (element);
1253 switch (transition) {
1254 case GST_STATE_CHANGE_READY_TO_PAUSED:
1255 if (!gst_aggregator_start (self))
1263 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1264 transition)) == GST_STATE_CHANGE_FAILURE)
1268 switch (transition) {
1269 case GST_STATE_CHANGE_PAUSED_TO_READY:
1270 if (!gst_aggregator_stop (self)) {
1271 /* What to do in this case? Error out? */
1272 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1284 GST_ERROR_OBJECT (element, "parent failed state change");
1289 GST_ERROR_OBJECT (element, "Subclass failed to start");
1290 return GST_STATE_CHANGE_FAILURE;
1295 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1297 GstAggregator *self = GST_AGGREGATOR (element);
1298 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1300 GST_INFO_OBJECT (pad, "Removing pad");
1303 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1304 gst_element_remove_pad (element, pad);
1306 self->priv->has_peer_latency = FALSE;
1307 SRC_BROADCAST (self);
1311 static GstAggregatorPad *
1312 gst_aggregator_default_create_new_pad (GstAggregator * self,
1313 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1315 GstAggregatorPad *agg_pad;
1316 GstAggregatorPrivate *priv = self->priv;
1320 if (templ->direction != GST_PAD_SINK ||
1321 g_strcmp0 (templ->name_template, "sink_%u") != 0)
1324 GST_OBJECT_LOCK (self);
1325 if (req_name == NULL || strlen (req_name) < 6
1326 || !g_str_has_prefix (req_name, "sink_")) {
1327 /* no name given when requesting the pad, use next available int */
1328 serial = ++priv->max_padserial;
1330 /* parse serial number from requested padname */
1331 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1332 if (serial > priv->max_padserial)
1333 priv->max_padserial = serial;
1336 name = g_strdup_printf ("sink_%u", serial);
1337 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1338 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1341 GST_OBJECT_UNLOCK (self);
1348 GST_WARNING_OBJECT (self, "request new pad that is not a SINK pad\n");
1354 gst_aggregator_request_new_pad (GstElement * element,
1355 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1357 GstAggregator *self;
1358 GstAggregatorPad *agg_pad;
1359 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1360 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1362 self = GST_AGGREGATOR (element);
1364 agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1366 GST_ERROR_OBJECT (element, "Couldn't create new pad");
1370 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1371 self->priv->has_peer_latency = FALSE;
1374 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1376 /* add the pad to the element */
1377 gst_element_add_pad (element, GST_PAD (agg_pad));
1379 return GST_PAD (agg_pad);
1382 /* Must be called with SRC_LOCK held */
1385 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1387 gboolean query_ret, live;
1388 GstClockTime our_latency, min, max;
1390 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1393 GST_WARNING_OBJECT (self, "Latency query failed");
1397 gst_query_parse_latency (query, &live, &min, &max);
1399 our_latency = self->priv->latency;
1401 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1402 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1403 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1407 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1408 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1409 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1410 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1411 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1415 self->priv->peer_latency_live = live;
1416 self->priv->peer_latency_min = min;
1417 self->priv->peer_latency_max = max;
1418 self->priv->has_peer_latency = TRUE;
1422 min += self->priv->sub_latency_min;
1423 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1424 && GST_CLOCK_TIME_IS_VALID (max))
1425 max += self->priv->sub_latency_max + our_latency;
1427 max = GST_CLOCK_TIME_NONE;
1429 SRC_BROADCAST (self);
1431 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1432 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1434 gst_query_set_latency (query, live, min, max);
1440 * MUST be called with the src_lock held.
1442 * See gst_aggregator_get_latency() for doc
1445 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1447 GstClockTime latency;
1449 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1451 if (!self->priv->has_peer_latency) {
1452 GstQuery *query = gst_query_new_latency ();
1455 ret = gst_aggregator_query_latency_unlocked (self, query);
1456 gst_query_unref (query);
1458 return GST_CLOCK_TIME_NONE;
1461 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1462 return GST_CLOCK_TIME_NONE;
1464 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1465 latency = self->priv->peer_latency_min;
1468 latency += self->priv->latency;
1469 latency += self->priv->sub_latency_min;
1475 * gst_aggregator_get_latency:
1476 * @self: a #GstAggregator
1478 * Retrieves the latency values reported by @self in response to the latency
1479 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1480 * will not wait for the clock.
1482 * Typically only called by subclasses.
1484 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1487 gst_aggregator_get_latency (GstAggregator * self)
1492 ret = gst_aggregator_get_latency_unlocked (self);
1499 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1501 GstAggregator *self = GST_AGGREGATOR (element);
1503 GST_STATE_LOCK (element);
1504 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1505 GST_STATE (element) < GST_STATE_PAUSED) {
1509 GstSeekType start_type, stop_type;
1512 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1513 &start, &stop_type, &stop);
1515 GST_OBJECT_LOCK (self);
1516 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1517 stop_type, stop, NULL);
1518 self->priv->seqnum = gst_event_get_seqnum (event);
1519 self->priv->first_buffer = FALSE;
1520 GST_OBJECT_UNLOCK (self);
1522 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1524 GST_STATE_UNLOCK (element);
1527 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1532 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1534 gboolean res = TRUE;
1536 switch (GST_QUERY_TYPE (query)) {
1537 case GST_QUERY_SEEKING:
1541 /* don't pass it along as some (file)sink might claim it does
1542 * whereas with a collectpads in between that will not likely work */
1543 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1544 gst_query_set_seeking (query, format, FALSE, 0, -1);
1549 case GST_QUERY_LATENCY:
1551 res = gst_aggregator_query_latency_unlocked (self, query);
1555 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1562 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1564 EventData *evdata = user_data;
1565 gboolean ret = TRUE;
1566 GstPad *peer = gst_pad_get_peer (pad);
1567 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1570 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1571 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1574 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1575 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1576 gst_object_unref (peer);
1581 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1582 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1584 GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1586 if (gst_pad_query (peer, seeking)) {
1589 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1591 if (seekable == FALSE) {
1592 GST_INFO_OBJECT (pad,
1593 "Source not seekable, We failed but it does not matter!");
1598 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1601 gst_query_unref (seeking);
1604 if (evdata->flush) {
1606 aggpad->priv->pending_flush_start = FALSE;
1607 aggpad->priv->pending_flush_stop = FALSE;
1608 PAD_UNLOCK (aggpad);
1611 evdata->one_actually_seeked = TRUE;
1614 evdata->result &= ret;
1616 /* Always send to all pads */
1621 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1622 GstEvent * event, gboolean flush, gboolean only_to_active_pads)
1626 evdata.event = event;
1627 evdata.result = TRUE;
1628 evdata.flush = flush;
1629 evdata.one_actually_seeked = FALSE;
1630 evdata.only_to_active_pads = only_to_active_pads;
1632 /* We first need to set all pads as flushing in a first pass
1633 * as flush_start flush_stop is sometimes sent synchronously
1634 * while we send the seek event */
1638 GST_OBJECT_LOCK (self);
1639 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1640 GstAggregatorPad *pad = l->data;
1643 pad->priv->pending_flush_start = TRUE;
1644 pad->priv->pending_flush_stop = FALSE;
1647 GST_OBJECT_UNLOCK (self);
1650 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1652 gst_event_unref (event);
1658 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1663 GstSeekType start_type, stop_type;
1667 GstAggregatorPrivate *priv = self->priv;
1669 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1670 &start, &stop_type, &stop);
1672 GST_INFO_OBJECT (self, "starting SEEK");
1674 flush = flags & GST_SEEK_FLAG_FLUSH;
1676 GST_OBJECT_LOCK (self);
1678 priv->pending_flush_start = TRUE;
1679 priv->flush_seeking = TRUE;
1682 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1683 stop_type, stop, NULL);
1685 /* Seeking sets a position */
1686 self->priv->first_buffer = FALSE;
1687 GST_OBJECT_UNLOCK (self);
1689 /* forward the seek upstream */
1691 gst_aggregator_forward_event_to_all_sinkpads (self, event, flush, FALSE);
1694 if (!evdata.result || !evdata.one_actually_seeked) {
1695 GST_OBJECT_LOCK (self);
1696 priv->flush_seeking = FALSE;
1697 priv->pending_flush_start = FALSE;
1698 GST_OBJECT_UNLOCK (self);
1701 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1703 return evdata.result;
1707 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1710 gboolean res = TRUE;
1712 switch (GST_EVENT_TYPE (event)) {
1713 case GST_EVENT_SEEK:
1715 gst_event_ref (event);
1716 res = gst_aggregator_do_seek (self, event);
1717 gst_event_unref (event);
1721 case GST_EVENT_NAVIGATION:
1723 /* navigation is rather pointless. */
1725 gst_event_unref (event);
1734 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
1735 * they will receive a QOS event that has earliest_time=0 (because we can't
1736 * have negative timestamps), and consider their buffer as too late */
1738 gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE,
1739 GST_EVENT_TYPE (event) == GST_EVENT_QOS);
1740 res = evdata.result;
1747 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1750 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1752 return klass->src_event (GST_AGGREGATOR (parent), event);
1756 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1759 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1761 return klass->src_query (GST_AGGREGATOR (parent), query);
1765 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1766 GstObject * parent, GstPadMode mode, gboolean active)
1768 GstAggregator *self = GST_AGGREGATOR (parent);
1769 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1771 if (klass->src_activate) {
1772 if (klass->src_activate (self, mode, active) == FALSE) {
1777 if (active == TRUE) {
1779 case GST_PAD_MODE_PUSH:
1781 GST_INFO_OBJECT (pad, "Activating pad!");
1782 gst_aggregator_start_srcpad_task (self);
1787 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1794 GST_INFO_OBJECT (self, "Deactivating srcpad");
1795 gst_aggregator_stop_srcpad_task (self, FALSE);
1801 gst_aggregator_default_sink_query (GstAggregator * self,
1802 GstAggregatorPad * aggpad, GstQuery * query)
1804 GstPad *pad = GST_PAD (aggpad);
1806 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1810 gst_aggregator_finalize (GObject * object)
1812 GstAggregator *self = (GstAggregator *) object;
1814 g_mutex_clear (&self->priv->src_lock);
1815 g_cond_clear (&self->priv->src_cond);
1817 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1821 * gst_aggregator_set_latency_property:
1822 * @agg: a #GstAggregator
1823 * @latency: the new latency value (in nanoseconds).
1825 * Sets the new latency value to @latency. This value is used to limit the
1826 * amount of time a pad waits for data to appear before considering the pad
1830 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1834 g_return_if_fail (GST_IS_AGGREGATOR (self));
1835 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1838 changed = (self->priv->latency != latency);
1843 GST_OBJECT_LOCK (self);
1844 /* First lock all the pads */
1845 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1846 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1850 self->priv->latency = latency;
1852 SRC_BROADCAST (self);
1854 /* Now wake up the pads */
1855 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
1856 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1857 PAD_BROADCAST_EVENT (aggpad);
1858 PAD_UNLOCK (aggpad);
1860 GST_OBJECT_UNLOCK (self);
1866 gst_element_post_message (GST_ELEMENT_CAST (self),
1867 gst_message_new_latency (GST_OBJECT_CAST (self)));
1871 * gst_aggregator_get_latency_property:
1872 * @agg: a #GstAggregator
1874 * Gets the latency value. See gst_aggregator_set_latency for
1877 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1878 * before a pad is deemed unresponsive. A value of -1 means an
1882 gst_aggregator_get_latency_property (GstAggregator * agg)
1886 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1888 GST_OBJECT_LOCK (agg);
1889 res = agg->priv->latency;
1890 GST_OBJECT_UNLOCK (agg);
1896 gst_aggregator_set_property (GObject * object, guint prop_id,
1897 const GValue * value, GParamSpec * pspec)
1899 GstAggregator *agg = GST_AGGREGATOR (object);
1903 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1905 case PROP_START_TIME_SELECTION:
1906 agg->priv->start_time_selection = g_value_get_enum (value);
1908 case PROP_START_TIME:
1909 agg->priv->start_time = g_value_get_uint64 (value);
1912 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1918 gst_aggregator_get_property (GObject * object, guint prop_id,
1919 GValue * value, GParamSpec * pspec)
1921 GstAggregator *agg = GST_AGGREGATOR (object);
1925 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1927 case PROP_START_TIME_SELECTION:
1928 g_value_set_enum (value, agg->priv->start_time_selection);
1930 case PROP_START_TIME:
1931 g_value_set_uint64 (value, agg->priv->start_time);
1934 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1939 /* GObject vmethods implementations */
1941 gst_aggregator_class_init (GstAggregatorClass * klass)
1943 GObjectClass *gobject_class = (GObjectClass *) klass;
1944 GstElementClass *gstelement_class = (GstElementClass *) klass;
1946 aggregator_parent_class = g_type_class_peek_parent (klass);
1947 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1949 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1950 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1952 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1954 klass->sink_event = gst_aggregator_default_sink_event;
1955 klass->sink_query = gst_aggregator_default_sink_query;
1957 klass->src_event = gst_aggregator_default_src_event;
1958 klass->src_query = gst_aggregator_default_src_query;
1960 klass->create_new_pad = gst_aggregator_default_create_new_pad;
1962 gstelement_class->request_new_pad =
1963 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1964 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1965 gstelement_class->release_pad =
1966 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1967 gstelement_class->change_state =
1968 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1970 gobject_class->set_property = gst_aggregator_set_property;
1971 gobject_class->get_property = gst_aggregator_get_property;
1972 gobject_class->finalize = gst_aggregator_finalize;
1974 g_object_class_install_property (gobject_class, PROP_LATENCY,
1975 g_param_spec_int64 ("latency", "Buffer latency",
1976 "Additional latency in live mode to allow upstream "
1977 "to take longer to produce buffers for the current "
1978 "position (in nanoseconds)", 0,
1979 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1980 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1982 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
1983 g_param_spec_enum ("start-time-selection", "Start Time Selection",
1984 "Decides which start time is output",
1985 gst_aggregator_start_time_selection_get_type (),
1986 DEFAULT_START_TIME_SELECTION,
1987 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1989 g_object_class_install_property (gobject_class, PROP_START_TIME,
1990 g_param_spec_uint64 ("start-time", "Start Time",
1991 "Start time to use if start-time-selection=set", 0,
1993 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1995 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1999 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
2001 GstPadTemplate *pad_template;
2002 GstAggregatorPrivate *priv;
2004 g_return_if_fail (klass->aggregate != NULL);
2007 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2008 GstAggregatorPrivate);
2013 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2014 g_return_if_fail (pad_template != NULL);
2016 priv->max_padserial = -1;
2017 priv->tags_changed = FALSE;
2019 self->priv->peer_latency_live = FALSE;
2020 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2021 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2022 self->priv->has_peer_latency = FALSE;
2023 gst_aggregator_reset_flow_values (self);
2025 self->srcpad = gst_pad_new_from_template (pad_template, "src");
2027 gst_pad_set_event_function (self->srcpad,
2028 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2029 gst_pad_set_query_function (self->srcpad,
2030 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2031 gst_pad_set_activatemode_function (self->srcpad,
2032 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2034 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2036 self->priv->latency = DEFAULT_LATENCY;
2037 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2038 self->priv->start_time = DEFAULT_START_TIME;
2040 g_mutex_init (&self->priv->src_lock);
2041 g_cond_init (&self->priv->src_cond);
2044 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2045 * method to get to the padtemplates */
2047 gst_aggregator_get_type (void)
2049 static volatile gsize type = 0;
2051 if (g_once_init_enter (&type)) {
2053 static const GTypeInfo info = {
2054 sizeof (GstAggregatorClass),
2057 (GClassInitFunc) gst_aggregator_class_init,
2060 sizeof (GstAggregator),
2062 (GInstanceInitFunc) gst_aggregator_init,
2065 _type = g_type_register_static (GST_TYPE_ELEMENT,
2066 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2067 g_once_init_leave (&type, _type);
2072 /* Must be called with SRC lock and PAD lock held */
2074 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2076 /* Empty queue always has space */
2077 if (g_queue_get_length (&aggpad->priv->buffers) == 0)
2080 /* We also want at least two buffers, one is being processed and one is ready
2081 * for the next iteration when we operate in live mode. */
2082 if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2085 /* zero latency, if there is a buffer, it's full */
2086 if (self->priv->latency == 0)
2089 /* Allow no more buffers than the latency */
2090 return (aggpad->priv->time_level <= self->priv->latency);
2093 /* Must be called with the PAD_LOCK held */
2095 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2097 GstClockTime timestamp;
2099 if (GST_BUFFER_DTS_IS_VALID (buffer))
2100 timestamp = GST_BUFFER_DTS (buffer);
2102 timestamp = GST_BUFFER_PTS (buffer);
2104 if (timestamp == GST_CLOCK_TIME_NONE) {
2106 timestamp = aggpad->priv->head_position;
2108 timestamp = aggpad->priv->tail_position;
2112 if (GST_BUFFER_DURATION_IS_VALID (buffer))
2113 timestamp += GST_BUFFER_DURATION (buffer);
2116 aggpad->priv->head_position = timestamp;
2118 aggpad->priv->tail_position = timestamp;
2120 update_time_level (aggpad, head);
2123 static GstFlowReturn
2124 gst_aggregator_pad_chain_internal (GstAggregator * self,
2125 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2127 GstBuffer *actual_buf = buffer;
2128 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
2129 GstFlowReturn flow_return;
2130 GstClockTime buf_pts;
2132 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2134 PAD_FLUSH_LOCK (aggpad);
2137 flow_return = aggpad->priv->flow_return;
2138 if (flow_return != GST_FLOW_OK)
2141 if (aggpad->priv->pending_eos == TRUE)
2144 PAD_UNLOCK (aggpad);
2146 if (aggclass->clip && head) {
2147 aggclass->clip (self, aggpad, buffer, &actual_buf);
2150 if (actual_buf == NULL) {
2151 GST_LOG_OBJECT (actual_buf, "Buffer dropped by clip function");
2155 buf_pts = GST_BUFFER_PTS (actual_buf);
2157 aggpad->priv->first_buffer = FALSE;
2161 GST_OBJECT_LOCK (self);
2163 if (gst_aggregator_pad_has_space (self, aggpad)
2164 && aggpad->priv->flow_return == GST_FLOW_OK) {
2166 g_queue_push_head (&aggpad->priv->buffers, actual_buf);
2168 g_queue_push_tail (&aggpad->priv->buffers, actual_buf);
2169 apply_buffer (aggpad, actual_buf, head);
2170 aggpad->priv->num_buffers++;
2171 actual_buf = buffer = NULL;
2172 SRC_BROADCAST (self);
2176 flow_return = aggpad->priv->flow_return;
2177 if (flow_return != GST_FLOW_OK) {
2178 GST_OBJECT_UNLOCK (self);
2182 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2183 GST_OBJECT_UNLOCK (self);
2185 PAD_WAIT_EVENT (aggpad);
2187 PAD_UNLOCK (aggpad);
2190 if (self->priv->first_buffer) {
2191 GstClockTime start_time;
2193 switch (self->priv->start_time_selection) {
2194 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2198 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2199 GST_OBJECT_LOCK (aggpad);
2200 if (aggpad->segment.format == GST_FORMAT_TIME) {
2201 start_time = buf_pts;
2202 if (start_time != -1) {
2203 start_time = MAX (start_time, aggpad->segment.start);
2205 gst_segment_to_running_time (&aggpad->segment, GST_FORMAT_TIME,
2210 GST_WARNING_OBJECT (aggpad,
2211 "Ignoring request of selecting the first start time "
2212 "as the segment is a %s segment instead of a time segment",
2213 gst_format_get_name (aggpad->segment.format));
2215 GST_OBJECT_UNLOCK (aggpad);
2217 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2218 start_time = self->priv->start_time;
2219 if (start_time == -1)
2224 if (start_time != -1) {
2225 if (self->segment.position == -1)
2226 self->segment.position = start_time;
2228 self->segment.position = MIN (start_time, self->segment.position);
2230 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2231 GST_TIME_ARGS (start_time));
2235 PAD_UNLOCK (aggpad);
2236 GST_OBJECT_UNLOCK (self);
2241 PAD_FLUSH_UNLOCK (aggpad);
2243 GST_DEBUG_OBJECT (aggpad, "Done chaining");
2248 PAD_UNLOCK (aggpad);
2249 PAD_FLUSH_UNLOCK (aggpad);
2251 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2252 gst_flow_get_name (flow_return));
2254 gst_buffer_unref (buffer);
2259 PAD_UNLOCK (aggpad);
2260 PAD_FLUSH_UNLOCK (aggpad);
2262 gst_buffer_unref (buffer);
2263 GST_DEBUG_OBJECT (aggpad, "We are EOS already...");
2265 return GST_FLOW_EOS;
2268 static GstFlowReturn
2269 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2271 return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2272 GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2276 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2279 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2280 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2282 if (GST_QUERY_IS_SERIALIZED (query)) {
2285 while (!gst_aggregator_pad_queue_is_empty (aggpad)
2286 && aggpad->priv->flow_return == GST_FLOW_OK) {
2287 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2288 PAD_WAIT_EVENT (aggpad);
2291 if (aggpad->priv->flow_return != GST_FLOW_OK)
2294 PAD_UNLOCK (aggpad);
2297 return klass->sink_query (GST_AGGREGATOR (parent),
2298 GST_AGGREGATOR_PAD (pad), query);
2301 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2302 gst_flow_get_name (aggpad->priv->flow_return));
2303 PAD_UNLOCK (aggpad);
2307 static GstFlowReturn
2308 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2311 GstFlowReturn ret = GST_FLOW_OK;
2312 GstAggregator *self = GST_AGGREGATOR (parent);
2313 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2314 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2316 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
2317 /* && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE */ ) {
2321 if (aggpad->priv->flow_return != GST_FLOW_OK
2322 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2323 ret = aggpad->priv->flow_return;
2327 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2328 GST_OBJECT_LOCK (aggpad);
2329 gst_event_copy_segment (event, &aggpad->clip_segment);
2330 aggpad->priv->head_position = aggpad->clip_segment.position;
2331 update_time_level (aggpad, TRUE);
2332 GST_OBJECT_UNLOCK (aggpad);
2335 if (!gst_aggregator_pad_queue_is_empty (aggpad) &&
2336 GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2337 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT,
2339 g_queue_push_head (&aggpad->priv->buffers, event);
2341 SRC_BROADCAST (self);
2343 PAD_UNLOCK (aggpad);
2348 gboolean is_caps = (GST_EVENT_TYPE (event) == GST_EVENT_CAPS);
2350 if (!klass->sink_event (self, aggpad, event)) {
2351 /* Copied from GstPad to convert boolean to a GstFlowReturn in
2352 * the event handling func */
2353 ret = is_caps ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_ERROR;
2360 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2361 gst_flow_get_name (aggpad->priv->flow_return));
2362 PAD_UNLOCK (aggpad);
2364 if (GST_EVENT_IS_STICKY (event))
2365 gst_pad_store_sticky_event (pad, event);
2366 gst_event_unref (event);
2372 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2373 GstObject * parent, GstPadMode mode, gboolean active)
2375 GstAggregator *self = GST_AGGREGATOR (parent);
2376 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2378 if (active == FALSE) {
2380 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2381 SRC_BROADCAST (self);
2385 aggpad->priv->flow_return = GST_FLOW_OK;
2386 PAD_BROADCAST_EVENT (aggpad);
2387 PAD_UNLOCK (aggpad);
2393 /***********************************
2394 * GstAggregatorPad implementation *
2395 ************************************/
2396 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2399 gst_aggregator_pad_constructed (GObject * object)
2401 GstPad *pad = GST_PAD (object);
2403 gst_pad_set_chain_function (pad,
2404 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2405 gst_pad_set_event_full_function_full (pad,
2406 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
2407 gst_pad_set_query_function (pad,
2408 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2409 gst_pad_set_activatemode_function (pad,
2410 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2414 gst_aggregator_pad_finalize (GObject * object)
2416 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2418 g_cond_clear (&pad->priv->event_cond);
2419 g_mutex_clear (&pad->priv->flush_lock);
2420 g_mutex_clear (&pad->priv->lock);
2422 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2426 gst_aggregator_pad_dispose (GObject * object)
2428 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2430 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2432 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2436 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2438 GObjectClass *gobject_class = (GObjectClass *) klass;
2440 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2442 gobject_class->constructed = gst_aggregator_pad_constructed;
2443 gobject_class->finalize = gst_aggregator_pad_finalize;
2444 gobject_class->dispose = gst_aggregator_pad_dispose;
2448 gst_aggregator_pad_init (GstAggregatorPad * pad)
2451 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2452 GstAggregatorPadPrivate);
2454 g_queue_init (&pad->priv->buffers);
2455 g_cond_init (&pad->priv->event_cond);
2457 g_mutex_init (&pad->priv->flush_lock);
2458 g_mutex_init (&pad->priv->lock);
2460 pad->priv->first_buffer = TRUE;
2464 * gst_aggregator_pad_steal_buffer:
2465 * @pad: the pad to get buffer from
2467 * Steal the ref to the buffer currently queued in @pad.
2469 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2470 * queued. You should unref the buffer after usage.
2473 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2475 GstBuffer *buffer = NULL;
2478 if (GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->buffers)))
2479 buffer = g_queue_pop_tail (&pad->priv->buffers);
2482 apply_buffer (pad, buffer, FALSE);
2483 pad->priv->num_buffers--;
2484 GST_TRACE_OBJECT (pad, "Consuming buffer");
2485 if (gst_aggregator_pad_queue_is_empty (pad) && pad->priv->pending_eos) {
2486 pad->priv->pending_eos = FALSE;
2487 pad->priv->eos = TRUE;
2489 PAD_BROADCAST_EVENT (pad);
2490 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2498 * gst_aggregator_pad_drop_buffer:
2499 * @pad: the pad where to drop any pending buffer
2501 * Drop the buffer currently queued in @pad.
2503 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2506 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2510 buf = gst_aggregator_pad_steal_buffer (pad);
2515 gst_buffer_unref (buf);
2520 * gst_aggregator_pad_get_buffer:
2521 * @pad: the pad to get buffer from
2523 * Returns: (transfer full): A reference to the buffer in @pad or
2524 * NULL if no buffer was queued. You should unref the buffer after
2528 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2530 GstBuffer *buffer = NULL;
2533 buffer = g_queue_peek_tail (&pad->priv->buffers);
2534 /* The tail should always be a buffer, because if it is an event,
2535 * it will be consumed immeditaly in gst_aggregator_steal_buffer */
2537 if (GST_IS_BUFFER (buffer))
2538 gst_buffer_ref (buffer);
2547 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2552 is_eos = pad->priv->eos;
2559 * gst_aggregator_merge_tags:
2560 * @self: a #GstAggregator
2561 * @tags: a #GstTagList to merge
2562 * @mode: the #GstTagMergeMode to use
2564 * Adds tags to so-called pending tags, which will be processed
2565 * before pushing out data downstream.
2567 * Note that this is provided for convenience, and the subclass is
2568 * not required to use this and can still do tag handling on its own.
2573 gst_aggregator_merge_tags (GstAggregator * self,
2574 const GstTagList * tags, GstTagMergeMode mode)
2578 g_return_if_fail (GST_IS_AGGREGATOR (self));
2579 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2581 /* FIXME Check if we can use OBJECT lock here! */
2582 GST_OBJECT_LOCK (self);
2584 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2585 otags = self->priv->tags;
2586 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2588 gst_tag_list_unref (otags);
2589 self->priv->tags_changed = TRUE;
2590 GST_OBJECT_UNLOCK (self);
2594 * gst_aggregator_set_latency:
2595 * @self: a #GstAggregator
2596 * @min_latency: minimum latency
2597 * @max_latency: maximum latency
2599 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2600 * latency is. Will also post a LATENCY message on the bus so the pipeline
2601 * can reconfigure its global latency.
2604 gst_aggregator_set_latency (GstAggregator * self,
2605 GstClockTime min_latency, GstClockTime max_latency)
2607 gboolean changed = FALSE;
2609 g_return_if_fail (GST_IS_AGGREGATOR (self));
2610 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2611 g_return_if_fail (max_latency >= min_latency);
2614 if (self->priv->sub_latency_min != min_latency) {
2615 self->priv->sub_latency_min = min_latency;
2618 if (self->priv->sub_latency_max != max_latency) {
2619 self->priv->sub_latency_max = max_latency;
2624 SRC_BROADCAST (self);
2628 gst_element_post_message (GST_ELEMENT_CAST (self),
2629 gst_message_new_latency (GST_OBJECT_CAST (self)));