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 * * Installs a #GstPadChainFunction, a #GstPadEventFullFunction and a
36 * #GstPadQueryFunction to queue all serialized data packets per sink pad.
37 * Subclasses should not overwrite those, but instead implement
38 * #GstAggregatorClass.sink_event() and #GstAggregatorClass.sink_query() as
41 * * When data is queued on all pads, the aggregate vmethod is called.
43 * * One can peek at the data on any given GstAggregatorPad with the
44 * gst_aggregator_pad_get_buffer () method, and take ownership of it
45 * with the gst_aggregator_pad_steal_buffer () method. When a buffer
46 * has been taken with steal_buffer (), a new buffer can be queued
49 * * If the subclass wishes to push a buffer downstream in its aggregate
50 * implementation, it should do so through the
51 * gst_aggregator_finish_buffer () method. This method will take care
52 * of sending and ordering mandatory events such as stream start, caps
55 * * Same goes for EOS events, which should not be pushed directly by the
56 * subclass, it should instead return GST_FLOW_EOS in its aggregate
59 * * Note that the aggregator logic regarding gap event handling is to turn
60 * these into gap buffers with matching PTS and duration. It will also
61 * flag these buffers with GST_BUFFER_FLAG_GAP and GST_BUFFER_FLAG_DROPPABLE
62 * to ease their identification and subsequent processing.
70 #include <string.h> /* strlen */
72 #include "gstaggregator.h"
76 GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
77 GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
78 GST_AGGREGATOR_START_TIME_SELECTION_SET
79 } GstAggregatorStartTimeSelection;
82 gst_aggregator_start_time_selection_get_type (void)
84 static GType gtype = 0;
87 static const GEnumValue values[] = {
88 {GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
89 "Start at 0 running time (default)", "zero"},
90 {GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
91 "Start at first observed input running time", "first"},
92 {GST_AGGREGATOR_START_TIME_SELECTION_SET,
93 "Set start time with start-time property", "set"},
97 gtype = g_enum_register_static ("GstAggregatorStartTimeSelection", values);
102 /* Might become API */
103 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
104 const GstTagList * tags, GstTagMergeMode mode);
105 static void gst_aggregator_set_latency_property (GstAggregator * agg,
107 static gint64 gst_aggregator_get_latency_property (GstAggregator * agg);
109 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
111 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
112 #define GST_CAT_DEFAULT aggregator_debug
114 /* Locking order, locks in this element must always be taken in this order
116 * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
117 * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
118 * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
119 * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
120 * standard element object lock -> GST_OBJECT_LOCK(agg)
121 * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
122 * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
123 * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
126 /* GstAggregatorPad definitions */
127 #define PAD_LOCK(pad) G_STMT_START { \
128 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
130 g_mutex_lock(&pad->priv->lock); \
131 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
135 #define PAD_UNLOCK(pad) G_STMT_START { \
136 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
138 g_mutex_unlock(&pad->priv->lock); \
139 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
144 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
145 GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p", \
147 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
148 (&((GstAggregatorPad*)pad)->priv->lock)); \
149 GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
153 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
154 GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p", \
156 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
160 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
161 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
163 g_mutex_lock(&pad->priv->flush_lock); \
164 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
168 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
169 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
171 g_mutex_unlock(&pad->priv->flush_lock); \
172 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
176 #define SRC_LOCK(self) G_STMT_START { \
177 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
179 g_mutex_lock(&self->priv->src_lock); \
180 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
184 #define SRC_UNLOCK(self) G_STMT_START { \
185 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
187 g_mutex_unlock(&self->priv->src_lock); \
188 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
192 #define SRC_WAIT(self) G_STMT_START { \
193 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
195 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
196 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
200 #define SRC_BROADCAST(self) G_STMT_START { \
201 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
203 if (self->priv->aggregate_id) \
204 gst_clock_id_unschedule (self->priv->aggregate_id); \
205 g_cond_broadcast(&(self->priv->src_cond)); \
208 struct _GstAggregatorPadPrivate
210 /* Following fields are protected by the PAD_LOCK */
211 GstFlowReturn flow_return;
212 gboolean pending_flush_start;
213 gboolean pending_flush_stop;
215 gboolean first_buffer;
217 GQueue data; /* buffers, events and queries */
218 GstBuffer *clipped_buffer;
220 GstClockTime head_position;
221 GstClockTime tail_position;
222 GstClockTime head_time; /* running time */
223 GstClockTime tail_time;
224 GstClockTime time_level; /* how much head is ahead of tail */
225 GstSegment head_segment; /* segment before the queue */
233 /* This lock prevents a flush start processing happening while
234 * the chain function is also happening.
239 /* Must be called with PAD_LOCK held */
241 gst_aggregator_pad_reset_unlocked (GstAggregatorPad * aggpad)
243 aggpad->priv->eos = FALSE;
244 aggpad->priv->flow_return = GST_FLOW_OK;
245 GST_OBJECT_LOCK (aggpad);
246 gst_segment_init (&aggpad->segment, GST_FORMAT_UNDEFINED);
247 gst_segment_init (&aggpad->priv->head_segment, GST_FORMAT_UNDEFINED);
248 GST_OBJECT_UNLOCK (aggpad);
249 aggpad->priv->head_position = GST_CLOCK_TIME_NONE;
250 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
251 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
252 aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
253 aggpad->priv->time_level = 0;
254 aggpad->priv->first_buffer = TRUE;
258 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
260 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
263 gst_aggregator_pad_reset_unlocked (aggpad);
267 return klass->flush (aggpad, agg);
272 /*************************************
273 * GstAggregator implementation *
274 *************************************/
275 static GstElementClass *aggregator_parent_class = NULL;
277 /* All members are protected by the object lock unless otherwise noted */
279 struct _GstAggregatorPrivate
283 /* Our state is >= PAUSED */
284 gboolean running; /* protected by src_lock */
287 gboolean send_stream_start; /* protected by srcpad stream lock */
288 gboolean send_segment;
289 gboolean flush_seeking;
290 gboolean pending_flush_start;
291 gboolean send_eos; /* protected by srcpad stream lock */
293 GstCaps *srccaps; /* protected by the srcpad stream lock */
296 gboolean tags_changed;
298 gboolean peer_latency_live; /* protected by src_lock */
299 GstClockTime peer_latency_min; /* protected by src_lock */
300 GstClockTime peer_latency_max; /* protected by src_lock */
301 gboolean has_peer_latency; /* protected by src_lock */
303 GstClockTime sub_latency_min; /* protected by src_lock */
304 GstClockTime sub_latency_max; /* protected by src_lock */
307 GstClockID aggregate_id; /* protected by src_lock */
311 gboolean first_buffer; /* protected by object lock */
312 GstAggregatorStartTimeSelection start_time_selection;
313 GstClockTime start_time;
315 /* protected by the object lock */
316 GstQuery *allocation_query;
317 GstAllocator *allocator;
319 GstAllocationParams allocation_params;
322 gint64 latency; /* protected by both src_lock and all pad locks */
325 /* Seek event forwarding helper */
331 gboolean only_to_active_pads;
335 gboolean one_actually_seeked;
338 #define DEFAULT_LATENCY 0
339 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
340 #define DEFAULT_START_TIME (-1)
346 PROP_START_TIME_SELECTION,
351 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
352 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
355 * gst_aggregator_iterate_sinkpads:
356 * @self: The #GstAggregator
357 * @func: (scope call): The function to call.
358 * @user_data: (closure): The data to pass to @func.
360 * Iterate the sinkpads of aggregator to call a function on them.
362 * This method guarantees that @func will be called only once for each
365 * Returns: %FALSE if there are no sinkpads or if @func returned %FALSE
368 gst_aggregator_iterate_sinkpads (GstAggregator * self,
369 GstAggregatorPadForeachFunc func, gpointer user_data)
371 gboolean result = FALSE;
373 gboolean done = FALSE;
374 GValue item = { 0, };
375 GList *seen_pads = NULL;
377 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
383 switch (gst_iterator_next (iter, &item)) {
384 case GST_ITERATOR_OK:
386 GstAggregatorPad *pad;
388 pad = g_value_get_object (&item);
390 /* if already pushed, skip. FIXME, find something faster to tag pads */
391 if (pad == NULL || g_list_find (seen_pads, pad)) {
392 g_value_reset (&item);
396 GST_LOG_OBJECT (pad, "calling function %s on pad",
397 GST_DEBUG_FUNCPTR_NAME (func));
399 result = func (self, pad, user_data);
403 seen_pads = g_list_prepend (seen_pads, pad);
405 g_value_reset (&item);
408 case GST_ITERATOR_RESYNC:
409 gst_iterator_resync (iter);
411 case GST_ITERATOR_ERROR:
412 GST_ERROR_OBJECT (self,
413 "Could not iterate over internally linked pads");
416 case GST_ITERATOR_DONE:
421 g_value_unset (&item);
422 gst_iterator_free (iter);
424 if (seen_pads == NULL) {
425 GST_DEBUG_OBJECT (self, "No pad seen");
429 g_list_free (seen_pads);
436 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
438 return (g_queue_peek_tail (&pad->priv->data) == NULL &&
439 pad->priv->clipped_buffer == NULL);
443 gst_aggregator_check_pads_ready (GstAggregator * self)
445 GstAggregatorPad *pad;
447 gboolean have_buffer = TRUE;
448 gboolean have_event_or_query = FALSE;
450 GST_LOG_OBJECT (self, "checking pads");
452 GST_OBJECT_LOCK (self);
454 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
455 if (sinkpads == NULL)
458 for (l = sinkpads; l != NULL; l = l->next) {
463 if (pad->priv->num_buffers == 0) {
464 if (!gst_aggregator_pad_queue_is_empty (pad))
465 have_event_or_query = TRUE;
466 if (!pad->priv->eos) {
469 /* If not live we need data on all pads, so leave the loop */
470 if (!self->priv->peer_latency_live) {
475 } else if (self->priv->peer_latency_live) {
476 /* In live mode, having a single pad with buffers is enough to
477 * generate a start time from it. In non-live mode all pads need
480 self->priv->first_buffer = FALSE;
486 if (!have_buffer && !have_event_or_query)
490 self->priv->first_buffer = FALSE;
492 GST_OBJECT_UNLOCK (self);
493 GST_LOG_OBJECT (self, "pads are ready");
498 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
499 GST_OBJECT_UNLOCK (self);
504 if (have_event_or_query)
505 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet,"
506 " but waking up for serialized event");
508 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
509 GST_OBJECT_UNLOCK (self);
510 return have_event_or_query;
515 gst_aggregator_reset_flow_values (GstAggregator * self)
517 GST_OBJECT_LOCK (self);
518 self->priv->send_stream_start = TRUE;
519 self->priv->send_segment = TRUE;
520 gst_segment_init (&self->segment, GST_FORMAT_TIME);
521 self->priv->first_buffer = TRUE;
522 GST_OBJECT_UNLOCK (self);
526 gst_aggregator_push_mandatory_events (GstAggregator * self)
528 GstAggregatorPrivate *priv = self->priv;
529 GstEvent *segment = NULL;
530 GstEvent *tags = NULL;
532 if (self->priv->send_stream_start) {
535 GST_INFO_OBJECT (self, "pushing stream start");
536 /* stream-start (FIXME: create id based on input ids) */
537 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
538 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
539 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
541 self->priv->send_stream_start = FALSE;
544 if (self->priv->srccaps) {
546 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
547 self->priv->srccaps);
548 if (!gst_pad_push_event (self->srcpad,
549 gst_event_new_caps (self->priv->srccaps))) {
550 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
552 gst_caps_unref (self->priv->srccaps);
553 self->priv->srccaps = NULL;
556 GST_OBJECT_LOCK (self);
557 if (self->priv->send_segment && !self->priv->flush_seeking) {
558 segment = gst_event_new_segment (&self->segment);
560 if (!self->priv->seqnum)
561 self->priv->seqnum = gst_event_get_seqnum (segment);
563 gst_event_set_seqnum (segment, self->priv->seqnum);
564 self->priv->send_segment = FALSE;
566 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
569 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
570 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
571 priv->tags_changed = FALSE;
573 GST_OBJECT_UNLOCK (self);
576 gst_pad_push_event (self->srcpad, segment);
578 gst_pad_push_event (self->srcpad, tags);
583 * gst_aggregator_set_src_caps:
584 * @self: The #GstAggregator
585 * @caps: The #GstCaps to set on the src pad.
587 * Sets the caps to be used on the src pad.
590 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
592 GST_PAD_STREAM_LOCK (self->srcpad);
593 gst_caps_replace (&self->priv->srccaps, caps);
594 gst_aggregator_push_mandatory_events (self);
595 GST_PAD_STREAM_UNLOCK (self->srcpad);
599 * gst_aggregator_finish_buffer:
600 * @self: The #GstAggregator
601 * @buffer: (transfer full): the #GstBuffer to push.
603 * This method will push the provided output buffer downstream. If needed,
604 * mandatory events such as stream-start, caps, and segment events will be
605 * sent before pushing the buffer.
608 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
610 gst_aggregator_push_mandatory_events (self);
612 GST_OBJECT_LOCK (self);
613 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
614 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
615 GST_OBJECT_UNLOCK (self);
616 return gst_pad_push (self->srcpad, buffer);
618 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
619 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
620 GST_OBJECT_UNLOCK (self);
621 gst_buffer_unref (buffer);
627 gst_aggregator_push_eos (GstAggregator * self)
630 gst_aggregator_push_mandatory_events (self);
632 event = gst_event_new_eos ();
634 GST_OBJECT_LOCK (self);
635 self->priv->send_eos = FALSE;
636 gst_event_set_seqnum (event, self->priv->seqnum);
637 GST_OBJECT_UNLOCK (self);
639 gst_pad_push_event (self->srcpad, event);
643 gst_aggregator_get_next_time (GstAggregator * self)
645 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
647 if (klass->get_next_time)
648 return klass->get_next_time (self);
650 return GST_CLOCK_TIME_NONE;
654 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
656 GstClockTime latency;
664 latency = gst_aggregator_get_latency_unlocked (self);
666 if (gst_aggregator_check_pads_ready (self)) {
667 GST_DEBUG_OBJECT (self, "all pads have data");
673 /* Before waiting, check if we're actually still running */
674 if (!self->priv->running || !self->priv->send_eos) {
680 start = gst_aggregator_get_next_time (self);
682 /* If we're not live, or if we use the running time
683 * of the first buffer as start time, we wait until
684 * all pads have buffers.
685 * Otherwise (i.e. if we are live!), we wait on the clock
686 * and if a pad does not have a buffer in time we ignore
689 GST_OBJECT_LOCK (self);
690 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
691 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
692 !GST_CLOCK_TIME_IS_VALID (start) ||
693 (self->priv->first_buffer
694 && self->priv->start_time_selection ==
695 GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
696 /* We wake up here when something happened, and below
697 * then check if we're ready now. If we return FALSE,
698 * we will be directly called again.
700 GST_OBJECT_UNLOCK (self);
703 GstClockTime base_time, time;
705 GstClockReturn status;
706 GstClockTimeDiff jitter;
708 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
709 GST_TIME_ARGS (start));
711 base_time = GST_ELEMENT_CAST (self)->base_time;
712 clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
713 GST_OBJECT_UNLOCK (self);
715 time = base_time + start;
718 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
719 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
720 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
721 GST_TIME_ARGS (time),
722 GST_TIME_ARGS (base_time),
723 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
724 GST_TIME_ARGS (gst_clock_get_time (clock)));
726 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
727 gst_object_unref (clock);
731 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
734 if (self->priv->aggregate_id) {
735 gst_clock_id_unref (self->priv->aggregate_id);
736 self->priv->aggregate_id = NULL;
739 GST_DEBUG_OBJECT (self,
740 "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
741 status, GST_STIME_ARGS (jitter));
744 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
751 res = gst_aggregator_check_pads_ready (self);
758 gst_aggregator_do_events_and_queries (GstAggregator * self,
759 GstAggregatorPad * pad, gpointer user_data)
761 GstEvent *event = NULL;
762 GstQuery *query = NULL;
763 GstAggregatorClass *klass = NULL;
764 gboolean *processed_event = user_data;
771 if (pad->priv->clipped_buffer == NULL &&
772 !GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->data))) {
773 if (GST_IS_EVENT (g_queue_peek_tail (&pad->priv->data)))
774 event = gst_event_ref (g_queue_peek_tail (&pad->priv->data));
775 if (GST_IS_QUERY (g_queue_peek_tail (&pad->priv->data)))
776 query = g_queue_peek_tail (&pad->priv->data);
779 if (event || query) {
783 *processed_event = TRUE;
785 klass = GST_AGGREGATOR_GET_CLASS (self);
788 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
789 gst_event_ref (event);
790 ret = klass->sink_event (self, pad, event);
793 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS)
794 pad->priv->negotiated = ret;
795 if (g_queue_peek_tail (&pad->priv->data) == event)
796 gst_event_unref (g_queue_pop_tail (&pad->priv->data));
797 gst_event_unref (event);
799 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, query);
800 ret = klass->sink_query (self, pad, query);
803 if (g_queue_peek_tail (&pad->priv->data) == query) {
806 s = gst_query_writable_structure (query);
807 gst_structure_set (s, "gst-aggregator-retval", G_TYPE_BOOLEAN, ret,
809 g_queue_pop_tail (&pad->priv->data);
813 PAD_BROADCAST_EVENT (pad);
816 } while (event || query);
822 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
823 GstFlowReturn flow_return, gboolean full)
828 if (flow_return == GST_FLOW_NOT_LINKED)
829 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
831 aggpad->priv->flow_return = flow_return;
833 item = g_queue_peek_head_link (&aggpad->priv->data);
835 GList *next = item->next;
837 /* In partial flush, we do like the pad, we get rid of non-sticky events
840 if (full || GST_IS_BUFFER (item->data) ||
841 GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
842 GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
843 !GST_EVENT_IS_STICKY (item->data)) {
844 if (!GST_IS_QUERY (item->data))
845 gst_mini_object_unref (item->data);
846 g_queue_delete_link (&aggpad->priv->data, item);
850 aggpad->priv->num_buffers = 0;
851 gst_buffer_replace (&aggpad->priv->clipped_buffer, NULL);
853 PAD_BROADCAST_EVENT (aggpad);
858 gst_aggregator_default_update_src_caps (GstAggregator * agg, GstCaps * caps,
861 *ret = gst_caps_ref (caps);
867 gst_aggregator_default_fixate_src_caps (GstAggregator * agg, GstCaps * caps)
869 caps = gst_caps_fixate (caps);
875 gst_aggregator_default_negotiated_src_caps (GstAggregator * agg, GstCaps * caps)
881 /* takes ownership of the pool, allocator and query */
883 gst_aggregator_set_allocation (GstAggregator * self,
884 GstBufferPool * pool, GstAllocator * allocator,
885 GstAllocationParams * params, GstQuery * query)
887 GstAllocator *oldalloc;
888 GstBufferPool *oldpool;
891 GST_DEBUG ("storing allocation query");
893 GST_OBJECT_LOCK (self);
894 oldpool = self->priv->pool;
895 self->priv->pool = pool;
897 oldalloc = self->priv->allocator;
898 self->priv->allocator = allocator;
900 oldquery = self->priv->allocation_query;
901 self->priv->allocation_query = query;
904 self->priv->allocation_params = *params;
906 gst_allocation_params_init (&self->priv->allocation_params);
907 GST_OBJECT_UNLOCK (self);
910 GST_DEBUG_OBJECT (self, "deactivating old pool %p", oldpool);
911 gst_buffer_pool_set_active (oldpool, FALSE);
912 gst_object_unref (oldpool);
915 gst_object_unref (oldalloc);
918 gst_query_unref (oldquery);
925 gst_aggregator_decide_allocation (GstAggregator * self, GstQuery * query)
927 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
929 if (aggclass->decide_allocation)
930 if (!aggclass->decide_allocation (self, query))
937 gst_aggregator_do_allocation (GstAggregator * self, GstCaps * caps)
940 gboolean result = TRUE;
941 GstBufferPool *pool = NULL;
942 GstAllocator *allocator;
943 GstAllocationParams params;
945 /* find a pool for the negotiated caps now */
946 GST_DEBUG_OBJECT (self, "doing allocation query");
947 query = gst_query_new_allocation (caps, TRUE);
948 if (!gst_pad_peer_query (self->srcpad, query)) {
949 /* not a problem, just debug a little */
950 GST_DEBUG_OBJECT (self, "peer ALLOCATION query failed");
953 GST_DEBUG_OBJECT (self, "calling decide_allocation");
954 result = gst_aggregator_decide_allocation (self, query);
956 GST_DEBUG_OBJECT (self, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
960 goto no_decide_allocation;
962 /* we got configuration from our peer or the decide_allocation method,
964 if (gst_query_get_n_allocation_params (query) > 0) {
965 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
968 gst_allocation_params_init (¶ms);
971 if (gst_query_get_n_allocation_pools (query) > 0)
972 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
976 gst_aggregator_set_allocation (self, pool, allocator, ¶ms, query);
981 no_decide_allocation:
983 GST_WARNING_OBJECT (self, "Failed to decide allocation");
984 gst_query_unref (query);
991 /* WITH SRC_LOCK held */
993 gst_aggregator_update_src_caps (GstAggregator * self)
995 GstAggregatorClass *agg_klass = GST_AGGREGATOR_GET_CLASS (self);
996 GstCaps *downstream_caps, *template_caps, *caps = NULL;
997 GstFlowReturn ret = GST_FLOW_OK;
999 template_caps = gst_pad_get_pad_template_caps (self->srcpad);
1000 downstream_caps = gst_pad_peer_query_caps (self->srcpad, template_caps);
1002 if (gst_caps_is_empty (downstream_caps)) {
1003 GST_INFO_OBJECT (self, "Downstream caps (%"
1004 GST_PTR_FORMAT ") not compatible with pad template caps (%"
1005 GST_PTR_FORMAT ")", downstream_caps, template_caps);
1006 ret = GST_FLOW_NOT_NEGOTIATED;
1010 g_assert (agg_klass->update_src_caps);
1011 GST_DEBUG_OBJECT (self, "updating caps from %" GST_PTR_FORMAT,
1013 ret = agg_klass->update_src_caps (self, downstream_caps, &caps);
1014 if (ret < GST_FLOW_OK) {
1015 GST_WARNING_OBJECT (self, "Subclass failed to update provided caps");
1018 if ((caps == NULL || gst_caps_is_empty (caps)) && ret >= GST_FLOW_OK) {
1019 ret = GST_FLOW_NOT_NEGOTIATED;
1022 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1024 #ifdef GST_ENABLE_EXTRA_CHECKS
1025 if (!gst_caps_is_subset (caps, template_caps)) {
1026 GstCaps *intersection;
1028 GST_ERROR_OBJECT (self,
1029 "update_src_caps returned caps %" GST_PTR_FORMAT
1030 " which are not a real subset of the template caps %"
1031 GST_PTR_FORMAT, caps, template_caps);
1032 g_warning ("%s: update_src_caps returned caps which are not a real "
1033 "subset of the filter caps", GST_ELEMENT_NAME (self));
1036 gst_caps_intersect_full (template_caps, caps, GST_CAPS_INTERSECT_FIRST);
1037 gst_caps_unref (caps);
1038 caps = intersection;
1042 if (gst_caps_is_any (caps)) {
1046 if (!gst_caps_is_fixed (caps)) {
1047 g_assert (agg_klass->fixate_src_caps);
1049 GST_DEBUG_OBJECT (self, "fixate caps from %" GST_PTR_FORMAT, caps);
1050 if (!(caps = agg_klass->fixate_src_caps (self, caps))) {
1051 GST_WARNING_OBJECT (self, "Subclass failed to fixate provided caps");
1052 ret = GST_FLOW_NOT_NEGOTIATED;
1055 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1058 if (agg_klass->negotiated_src_caps) {
1059 if (!agg_klass->negotiated_src_caps (self, caps)) {
1060 GST_WARNING_OBJECT (self, "Subclass failed to accept negotiated caps");
1061 ret = GST_FLOW_NOT_NEGOTIATED;
1066 gst_aggregator_set_src_caps (self, caps);
1068 if (!gst_aggregator_do_allocation (self, caps)) {
1069 GST_WARNING_OBJECT (self, "Allocation negotiation failed");
1070 ret = GST_FLOW_NOT_NEGOTIATED;
1074 gst_caps_unref (downstream_caps);
1075 gst_caps_unref (template_caps);
1078 gst_caps_unref (caps);
1084 gst_aggregator_aggregate_func (GstAggregator * self)
1086 GstAggregatorPrivate *priv = self->priv;
1087 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1088 gboolean timeout = FALSE;
1090 if (self->priv->running == FALSE) {
1091 GST_DEBUG_OBJECT (self, "Not running anymore");
1095 GST_LOG_OBJECT (self, "Checking aggregate");
1096 while (priv->send_eos && priv->running) {
1097 GstFlowReturn flow_return = GST_FLOW_OK;
1098 gboolean processed_event = FALSE;
1100 gst_aggregator_iterate_sinkpads (self, gst_aggregator_do_events_and_queries,
1103 if (!gst_aggregator_wait_and_check (self, &timeout))
1106 gst_aggregator_iterate_sinkpads (self, gst_aggregator_do_events_and_queries,
1108 if (processed_event)
1111 if (gst_pad_check_reconfigure (GST_AGGREGATOR_SRC_PAD (self))) {
1112 flow_return = gst_aggregator_update_src_caps (self);
1113 if (flow_return != GST_FLOW_OK)
1114 gst_pad_mark_reconfigure (GST_AGGREGATOR_SRC_PAD (self));
1117 if (timeout || flow_return >= GST_FLOW_OK) {
1118 GST_TRACE_OBJECT (self, "Actually aggregating!");
1119 flow_return = klass->aggregate (self, timeout);
1122 if (flow_return == GST_AGGREGATOR_FLOW_NEED_DATA)
1125 GST_OBJECT_LOCK (self);
1126 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
1127 /* We don't want to set the pads to flushing, but we want to
1128 * stop the thread, so just break here */
1129 GST_OBJECT_UNLOCK (self);
1132 GST_OBJECT_UNLOCK (self);
1134 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
1135 gst_aggregator_push_eos (self);
1138 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
1140 if (flow_return != GST_FLOW_OK) {
1143 GST_OBJECT_LOCK (self);
1144 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
1145 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1147 gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
1149 GST_OBJECT_UNLOCK (self);
1154 /* Pause the task here, the only ways to get here are:
1155 * 1) We're stopping, in which case the task is stopped anyway
1156 * 2) We got a flow error above, in which case it might take
1157 * some time to forward the flow return upstream and we
1158 * would otherwise call the task function over and over
1159 * again without doing anything
1161 gst_pad_pause_task (self->srcpad);
1165 gst_aggregator_start (GstAggregator * self)
1167 GstAggregatorClass *klass;
1170 self->priv->send_stream_start = TRUE;
1171 self->priv->send_segment = TRUE;
1172 self->priv->send_eos = TRUE;
1173 self->priv->srccaps = NULL;
1175 gst_aggregator_set_allocation (self, NULL, NULL, NULL, NULL);
1177 klass = GST_AGGREGATOR_GET_CLASS (self);
1180 result = klass->start (self);
1188 _check_pending_flush_stop (GstAggregatorPad * pad)
1193 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
1200 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
1202 gboolean res = TRUE;
1204 GST_INFO_OBJECT (self, "%s srcpad task",
1205 flush_start ? "Pausing" : "Stopping");
1208 self->priv->running = FALSE;
1209 SRC_BROADCAST (self);
1213 res = gst_pad_push_event (self->srcpad, flush_start);
1216 gst_pad_stop_task (self->srcpad);
1222 gst_aggregator_start_srcpad_task (GstAggregator * self)
1224 GST_INFO_OBJECT (self, "Starting srcpad task");
1226 self->priv->running = TRUE;
1227 gst_pad_start_task (GST_PAD (self->srcpad),
1228 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
1231 static GstFlowReturn
1232 gst_aggregator_flush (GstAggregator * self)
1234 GstFlowReturn ret = GST_FLOW_OK;
1235 GstAggregatorPrivate *priv = self->priv;
1236 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1238 GST_DEBUG_OBJECT (self, "Flushing everything");
1239 GST_OBJECT_LOCK (self);
1240 priv->send_segment = TRUE;
1241 priv->flush_seeking = FALSE;
1242 priv->tags_changed = FALSE;
1243 GST_OBJECT_UNLOCK (self);
1245 ret = klass->flush (self);
1251 /* Called with GstAggregator's object lock held */
1254 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
1257 GstAggregatorPad *tmppad;
1259 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
1260 tmppad = (GstAggregatorPad *) tmp->data;
1262 if (_check_pending_flush_stop (tmppad) == FALSE) {
1263 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
1264 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
1273 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
1276 GstAggregatorPrivate *priv = self->priv;
1277 GstAggregatorPadPrivate *padpriv = aggpad->priv;
1279 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
1281 PAD_FLUSH_LOCK (aggpad);
1283 if (padpriv->pending_flush_start) {
1284 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
1286 padpriv->pending_flush_start = FALSE;
1287 padpriv->pending_flush_stop = TRUE;
1289 PAD_UNLOCK (aggpad);
1291 GST_OBJECT_LOCK (self);
1292 if (priv->flush_seeking) {
1293 /* If flush_seeking we forward the first FLUSH_START */
1294 if (priv->pending_flush_start) {
1295 priv->pending_flush_start = FALSE;
1296 GST_OBJECT_UNLOCK (self);
1298 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
1299 gst_aggregator_stop_srcpad_task (self, event);
1301 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
1302 GST_PAD_STREAM_LOCK (self->srcpad);
1303 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
1306 GST_OBJECT_UNLOCK (self);
1307 gst_event_unref (event);
1310 GST_OBJECT_UNLOCK (self);
1311 gst_event_unref (event);
1313 PAD_FLUSH_UNLOCK (aggpad);
1316 /* Must be called with the the PAD_LOCK held */
1318 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1321 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->head_position) &&
1322 aggpad->priv->head_segment.format == GST_FORMAT_TIME)
1323 aggpad->priv->head_time =
1324 gst_segment_to_running_time (&aggpad->priv->head_segment,
1325 GST_FORMAT_TIME, aggpad->priv->head_position);
1327 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
1329 if (!GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_time))
1330 aggpad->priv->tail_time = aggpad->priv->head_time;
1332 if (GST_CLOCK_TIME_IS_VALID (aggpad->priv->tail_position) &&
1333 aggpad->segment.format == GST_FORMAT_TIME)
1334 aggpad->priv->tail_time =
1335 gst_segment_to_running_time (&aggpad->segment,
1336 GST_FORMAT_TIME, aggpad->priv->tail_position);
1338 aggpad->priv->tail_time = aggpad->priv->head_time;
1341 if (aggpad->priv->head_time == GST_CLOCK_TIME_NONE ||
1342 aggpad->priv->tail_time == GST_CLOCK_TIME_NONE) {
1343 aggpad->priv->time_level = 0;
1347 if (aggpad->priv->tail_time > aggpad->priv->head_time)
1348 aggpad->priv->time_level = 0;
1350 aggpad->priv->time_level = aggpad->priv->head_time -
1351 aggpad->priv->tail_time;
1355 /* GstAggregator vmethods default implementations */
1357 gst_aggregator_default_sink_event (GstAggregator * self,
1358 GstAggregatorPad * aggpad, GstEvent * event)
1360 gboolean res = TRUE;
1361 GstPad *pad = GST_PAD (aggpad);
1362 GstAggregatorPrivate *priv = self->priv;
1364 GST_DEBUG_OBJECT (aggpad, "Got event: %" GST_PTR_FORMAT, event);
1366 switch (GST_EVENT_TYPE (event)) {
1367 case GST_EVENT_FLUSH_START:
1369 gst_aggregator_flush_start (self, aggpad, event);
1370 /* We forward only in one case: right after flush_seeking */
1374 case GST_EVENT_FLUSH_STOP:
1376 gst_aggregator_pad_flush (aggpad, self);
1377 GST_OBJECT_LOCK (self);
1378 if (priv->flush_seeking) {
1379 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
1380 if (gst_aggregator_all_flush_stop_received_locked (self)) {
1381 GST_OBJECT_UNLOCK (self);
1382 /* That means we received FLUSH_STOP/FLUSH_STOP on
1383 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1384 gst_aggregator_flush (self);
1385 gst_pad_push_event (self->srcpad, event);
1388 priv->send_eos = TRUE;
1389 SRC_BROADCAST (self);
1392 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
1393 GST_PAD_STREAM_UNLOCK (self->srcpad);
1394 gst_aggregator_start_srcpad_task (self);
1396 GST_OBJECT_UNLOCK (self);
1399 GST_OBJECT_UNLOCK (self);
1402 /* We never forward the event */
1409 g_assert (aggpad->priv->num_buffers == 0);
1410 aggpad->priv->eos = TRUE;
1411 PAD_UNLOCK (aggpad);
1412 SRC_BROADCAST (self);
1416 case GST_EVENT_SEGMENT:
1419 GST_OBJECT_LOCK (aggpad);
1420 gst_event_copy_segment (event, &aggpad->segment);
1421 /* We've got a new segment, tail_position is now meaningless
1422 * and may interfere with the time_level calculation
1424 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
1425 update_time_level (aggpad, FALSE);
1426 GST_OBJECT_UNLOCK (aggpad);
1427 PAD_UNLOCK (aggpad);
1429 GST_OBJECT_LOCK (self);
1430 self->priv->seqnum = gst_event_get_seqnum (event);
1431 GST_OBJECT_UNLOCK (self);
1434 case GST_EVENT_STREAM_START:
1440 GstClockTime pts, endpts;
1441 GstClockTime duration;
1444 gst_event_parse_gap (event, &pts, &duration);
1445 gapbuf = gst_buffer_new ();
1447 if (GST_CLOCK_TIME_IS_VALID (duration))
1448 endpts = pts + duration;
1450 endpts = GST_CLOCK_TIME_NONE;
1452 GST_OBJECT_LOCK (aggpad);
1453 res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1455 GST_OBJECT_UNLOCK (aggpad);
1458 GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1462 if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1463 duration = endpts - pts;
1465 duration = GST_CLOCK_TIME_NONE;
1467 GST_BUFFER_PTS (gapbuf) = pts;
1468 GST_BUFFER_DURATION (gapbuf) = duration;
1469 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1470 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1472 /* Remove GAP event so we can replace it with the buffer */
1473 if (g_queue_peek_tail (&aggpad->priv->data) == event)
1474 gst_event_unref (g_queue_pop_tail (&aggpad->priv->data));
1476 if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1478 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1488 gst_event_parse_tag (event, &tags);
1490 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1491 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
1492 gst_event_unref (event);
1504 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1505 return gst_pad_event_default (pad, GST_OBJECT (self), event);
1508 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1510 gst_event_unref (event);
1515 static inline gboolean
1516 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1517 gpointer unused_udata)
1519 gst_aggregator_pad_flush (pad, self);
1522 pad->priv->flow_return = GST_FLOW_FLUSHING;
1523 pad->priv->negotiated = FALSE;
1524 PAD_BROADCAST_EVENT (pad);
1531 gst_aggregator_stop (GstAggregator * agg)
1533 GstAggregatorClass *klass;
1536 gst_aggregator_reset_flow_values (agg);
1538 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1540 klass = GST_AGGREGATOR_GET_CLASS (agg);
1543 result = klass->stop (agg);
1547 agg->priv->has_peer_latency = FALSE;
1548 agg->priv->peer_latency_live = FALSE;
1549 agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1551 if (agg->priv->tags)
1552 gst_tag_list_unref (agg->priv->tags);
1553 agg->priv->tags = NULL;
1555 gst_aggregator_set_allocation (agg, NULL, NULL, NULL, NULL);
1560 /* GstElement vmethods implementations */
1561 static GstStateChangeReturn
1562 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1564 GstStateChangeReturn ret;
1565 GstAggregator *self = GST_AGGREGATOR (element);
1567 switch (transition) {
1568 case GST_STATE_CHANGE_READY_TO_PAUSED:
1569 if (!gst_aggregator_start (self))
1577 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1578 transition)) == GST_STATE_CHANGE_FAILURE)
1582 switch (transition) {
1583 case GST_STATE_CHANGE_PAUSED_TO_READY:
1584 if (!gst_aggregator_stop (self)) {
1585 /* What to do in this case? Error out? */
1586 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1598 GST_ERROR_OBJECT (element, "parent failed state change");
1603 GST_ERROR_OBJECT (element, "Subclass failed to start");
1604 return GST_STATE_CHANGE_FAILURE;
1609 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1611 GstAggregator *self = GST_AGGREGATOR (element);
1612 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1614 GST_INFO_OBJECT (pad, "Removing pad");
1617 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
1618 gst_element_remove_pad (element, pad);
1620 self->priv->has_peer_latency = FALSE;
1621 SRC_BROADCAST (self);
1625 static GstAggregatorPad *
1626 gst_aggregator_default_create_new_pad (GstAggregator * self,
1627 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1629 GstAggregatorPad *agg_pad;
1630 GstAggregatorPrivate *priv = self->priv;
1634 if (templ->direction != GST_PAD_SINK)
1637 if (templ->presence != GST_PAD_REQUEST)
1640 GST_OBJECT_LOCK (self);
1641 if (req_name == NULL || strlen (req_name) < 6
1642 || !g_str_has_prefix (req_name, "sink_")) {
1643 /* no name given when requesting the pad, use next available int */
1644 serial = ++priv->max_padserial;
1646 /* parse serial number from requested padname */
1647 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1648 if (serial > priv->max_padserial)
1649 priv->max_padserial = serial;
1652 name = g_strdup_printf ("sink_%u", serial);
1653 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1654 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1657 GST_OBJECT_UNLOCK (self);
1664 GST_WARNING_OBJECT (self, "request new pad that is not a SINK pad");
1669 GST_WARNING_OBJECT (self, "request new pad that is not a REQUEST pad");
1675 gst_aggregator_request_new_pad (GstElement * element,
1676 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1678 GstAggregator *self;
1679 GstAggregatorPad *agg_pad;
1680 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
1681 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1683 self = GST_AGGREGATOR (element);
1685 agg_pad = klass->create_new_pad (self, templ, req_name, caps);
1687 GST_ERROR_OBJECT (element, "Couldn't create new pad");
1691 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1694 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1696 /* add the pad to the element */
1697 gst_element_add_pad (element, GST_PAD (agg_pad));
1699 return GST_PAD (agg_pad);
1702 /* Must be called with SRC_LOCK held */
1705 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1707 gboolean query_ret, live;
1708 GstClockTime our_latency, min, max;
1710 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1713 GST_WARNING_OBJECT (self, "Latency query failed");
1717 gst_query_parse_latency (query, &live, &min, &max);
1719 our_latency = self->priv->latency;
1721 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1722 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1723 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1727 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1728 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1729 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1730 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1731 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1735 self->priv->peer_latency_live = live;
1736 self->priv->peer_latency_min = min;
1737 self->priv->peer_latency_max = max;
1738 self->priv->has_peer_latency = TRUE;
1742 min += self->priv->sub_latency_min;
1743 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1744 && GST_CLOCK_TIME_IS_VALID (max))
1745 max += self->priv->sub_latency_max + our_latency;
1747 max = GST_CLOCK_TIME_NONE;
1749 SRC_BROADCAST (self);
1751 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1752 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1754 gst_query_set_latency (query, live, min, max);
1760 * MUST be called with the src_lock held.
1762 * See gst_aggregator_get_latency() for doc
1765 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1767 GstClockTime latency;
1769 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1771 if (!self->priv->has_peer_latency) {
1772 GstQuery *query = gst_query_new_latency ();
1775 ret = gst_aggregator_query_latency_unlocked (self, query);
1776 gst_query_unref (query);
1778 return GST_CLOCK_TIME_NONE;
1781 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1782 return GST_CLOCK_TIME_NONE;
1784 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1785 latency = self->priv->peer_latency_min;
1788 latency += self->priv->latency;
1789 latency += self->priv->sub_latency_min;
1795 * gst_aggregator_get_latency:
1796 * @self: a #GstAggregator
1798 * Retrieves the latency values reported by @self in response to the latency
1799 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1800 * will not wait for the clock.
1802 * Typically only called by subclasses.
1804 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1807 gst_aggregator_get_latency (GstAggregator * self)
1812 ret = gst_aggregator_get_latency_unlocked (self);
1819 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1821 GstAggregator *self = GST_AGGREGATOR (element);
1823 GST_STATE_LOCK (element);
1824 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1825 GST_STATE (element) < GST_STATE_PAUSED) {
1829 GstSeekType start_type, stop_type;
1832 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1833 &start, &stop_type, &stop);
1835 GST_OBJECT_LOCK (self);
1836 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1837 stop_type, stop, NULL);
1838 self->priv->seqnum = gst_event_get_seqnum (event);
1839 self->priv->first_buffer = FALSE;
1840 GST_OBJECT_UNLOCK (self);
1842 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1844 GST_STATE_UNLOCK (element);
1847 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1852 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1854 gboolean res = TRUE;
1856 switch (GST_QUERY_TYPE (query)) {
1857 case GST_QUERY_SEEKING:
1861 /* don't pass it along as some (file)sink might claim it does
1862 * whereas with a collectpads in between that will not likely work */
1863 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1864 gst_query_set_seeking (query, format, FALSE, 0, -1);
1869 case GST_QUERY_LATENCY:
1871 res = gst_aggregator_query_latency_unlocked (self, query);
1875 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1882 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1884 EventData *evdata = user_data;
1885 gboolean ret = TRUE;
1886 GstPad *peer = gst_pad_get_peer (pad);
1887 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1890 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
1891 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
1894 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1895 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1896 gst_object_unref (peer);
1901 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1902 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1904 GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1906 if (gst_pad_query (peer, seeking)) {
1909 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1911 if (seekable == FALSE) {
1912 GST_INFO_OBJECT (pad,
1913 "Source not seekable, We failed but it does not matter!");
1918 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1921 gst_query_unref (seeking);
1924 if (evdata->flush) {
1926 aggpad->priv->pending_flush_start = FALSE;
1927 aggpad->priv->pending_flush_stop = FALSE;
1928 PAD_UNLOCK (aggpad);
1931 evdata->one_actually_seeked = TRUE;
1934 evdata->result &= ret;
1936 /* Always send to all pads */
1941 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1944 evdata->result = TRUE;
1945 evdata->one_actually_seeked = FALSE;
1947 /* We first need to set all pads as flushing in a first pass
1948 * as flush_start flush_stop is sometimes sent synchronously
1949 * while we send the seek event */
1950 if (evdata->flush) {
1953 GST_OBJECT_LOCK (self);
1954 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1955 GstAggregatorPad *pad = l->data;
1958 pad->priv->pending_flush_start = TRUE;
1959 pad->priv->pending_flush_stop = FALSE;
1962 GST_OBJECT_UNLOCK (self);
1965 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, evdata);
1967 gst_event_unref (evdata->event);
1971 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1976 GstSeekType start_type, stop_type;
1979 EventData evdata = { 0, };
1980 GstAggregatorPrivate *priv = self->priv;
1982 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1983 &start, &stop_type, &stop);
1985 GST_INFO_OBJECT (self, "starting SEEK");
1987 flush = flags & GST_SEEK_FLAG_FLUSH;
1989 GST_OBJECT_LOCK (self);
1991 priv->pending_flush_start = TRUE;
1992 priv->flush_seeking = TRUE;
1995 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1996 stop_type, stop, NULL);
1998 /* Seeking sets a position */
1999 self->priv->first_buffer = FALSE;
2000 GST_OBJECT_UNLOCK (self);
2002 /* forward the seek upstream */
2003 evdata.event = event;
2004 evdata.flush = flush;
2005 evdata.only_to_active_pads = FALSE;
2006 gst_aggregator_forward_event_to_all_sinkpads (self, &evdata);
2009 if (!evdata.result || !evdata.one_actually_seeked) {
2010 GST_OBJECT_LOCK (self);
2011 priv->flush_seeking = FALSE;
2012 priv->pending_flush_start = FALSE;
2013 GST_OBJECT_UNLOCK (self);
2016 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
2018 return evdata.result;
2022 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
2024 EventData evdata = { 0, };
2026 switch (GST_EVENT_TYPE (event)) {
2027 case GST_EVENT_SEEK:
2028 /* _do_seek() unrefs the event. */
2029 return gst_aggregator_do_seek (self, event);
2030 case GST_EVENT_NAVIGATION:
2031 /* navigation is rather pointless. */
2032 gst_event_unref (event);
2038 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
2039 * they will receive a QOS event that has earliest_time=0 (because we can't
2040 * have negative timestamps), and consider their buffer as too late */
2041 evdata.event = event;
2042 evdata.flush = FALSE;
2043 evdata.only_to_active_pads = GST_EVENT_TYPE (event) == GST_EVENT_QOS;
2044 gst_aggregator_forward_event_to_all_sinkpads (self, &evdata);
2045 return evdata.result;
2049 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
2052 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2054 return klass->src_event (GST_AGGREGATOR (parent), event);
2058 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
2061 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2063 return klass->src_query (GST_AGGREGATOR (parent), query);
2067 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
2068 GstObject * parent, GstPadMode mode, gboolean active)
2070 GstAggregator *self = GST_AGGREGATOR (parent);
2071 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2073 if (klass->src_activate) {
2074 if (klass->src_activate (self, mode, active) == FALSE) {
2079 if (active == TRUE) {
2081 case GST_PAD_MODE_PUSH:
2083 GST_INFO_OBJECT (pad, "Activating pad!");
2084 gst_aggregator_start_srcpad_task (self);
2089 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
2096 GST_INFO_OBJECT (self, "Deactivating srcpad");
2097 gst_aggregator_stop_srcpad_task (self, FALSE);
2103 gst_aggregator_default_sink_query (GstAggregator * self,
2104 GstAggregatorPad * aggpad, GstQuery * query)
2106 GstPad *pad = GST_PAD (aggpad);
2108 if (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION) {
2109 GstQuery *decide_query = NULL;
2110 GstAggregatorClass *agg_class;
2113 GST_OBJECT_LOCK (self);
2115 if (G_UNLIKELY (!aggpad->priv->negotiated)) {
2116 GST_DEBUG_OBJECT (self,
2117 "not negotiated yet, can't answer ALLOCATION query");
2118 PAD_UNLOCK (aggpad);
2119 GST_OBJECT_UNLOCK (self);
2124 if ((decide_query = self->priv->allocation_query))
2125 gst_query_ref (decide_query);
2126 PAD_UNLOCK (aggpad);
2127 GST_OBJECT_UNLOCK (self);
2129 GST_DEBUG_OBJECT (self,
2130 "calling propose allocation with query %" GST_PTR_FORMAT, decide_query);
2132 agg_class = GST_AGGREGATOR_GET_CLASS (self);
2134 /* pass the query to the propose_allocation vmethod if any */
2135 if (agg_class->propose_allocation)
2136 ret = agg_class->propose_allocation (self, aggpad, decide_query, query);
2141 gst_query_unref (decide_query);
2143 GST_DEBUG_OBJECT (self, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
2147 return gst_pad_query_default (pad, GST_OBJECT (self), query);
2151 gst_aggregator_finalize (GObject * object)
2153 GstAggregator *self = (GstAggregator *) object;
2155 g_mutex_clear (&self->priv->src_lock);
2156 g_cond_clear (&self->priv->src_cond);
2158 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
2162 * gst_aggregator_set_latency_property:
2163 * @agg: a #GstAggregator
2164 * @latency: the new latency value (in nanoseconds).
2166 * Sets the new latency value to @latency. This value is used to limit the
2167 * amount of time a pad waits for data to appear before considering the pad
2171 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
2175 g_return_if_fail (GST_IS_AGGREGATOR (self));
2176 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
2179 changed = (self->priv->latency != latency);
2184 GST_OBJECT_LOCK (self);
2185 /* First lock all the pads */
2186 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2187 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2191 self->priv->latency = latency;
2193 SRC_BROADCAST (self);
2195 /* Now wake up the pads */
2196 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2197 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2198 PAD_BROADCAST_EVENT (aggpad);
2199 PAD_UNLOCK (aggpad);
2201 GST_OBJECT_UNLOCK (self);
2207 gst_element_post_message (GST_ELEMENT_CAST (self),
2208 gst_message_new_latency (GST_OBJECT_CAST (self)));
2212 * gst_aggregator_get_latency_property:
2213 * @agg: a #GstAggregator
2215 * Gets the latency value. See gst_aggregator_set_latency for
2218 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
2219 * before a pad is deemed unresponsive. A value of -1 means an
2223 gst_aggregator_get_latency_property (GstAggregator * agg)
2227 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
2229 GST_OBJECT_LOCK (agg);
2230 res = agg->priv->latency;
2231 GST_OBJECT_UNLOCK (agg);
2237 gst_aggregator_set_property (GObject * object, guint prop_id,
2238 const GValue * value, GParamSpec * pspec)
2240 GstAggregator *agg = GST_AGGREGATOR (object);
2244 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
2246 case PROP_START_TIME_SELECTION:
2247 agg->priv->start_time_selection = g_value_get_enum (value);
2249 case PROP_START_TIME:
2250 agg->priv->start_time = g_value_get_uint64 (value);
2253 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2259 gst_aggregator_get_property (GObject * object, guint prop_id,
2260 GValue * value, GParamSpec * pspec)
2262 GstAggregator *agg = GST_AGGREGATOR (object);
2266 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
2268 case PROP_START_TIME_SELECTION:
2269 g_value_set_enum (value, agg->priv->start_time_selection);
2271 case PROP_START_TIME:
2272 g_value_set_uint64 (value, agg->priv->start_time);
2275 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2280 /* GObject vmethods implementations */
2282 gst_aggregator_class_init (GstAggregatorClass * klass)
2284 GObjectClass *gobject_class = (GObjectClass *) klass;
2285 GstElementClass *gstelement_class = (GstElementClass *) klass;
2287 aggregator_parent_class = g_type_class_peek_parent (klass);
2288 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
2290 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
2291 GST_DEBUG_FG_MAGENTA, "GstAggregator");
2293 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
2295 klass->sink_event = gst_aggregator_default_sink_event;
2296 klass->sink_query = gst_aggregator_default_sink_query;
2298 klass->src_event = gst_aggregator_default_src_event;
2299 klass->src_query = gst_aggregator_default_src_query;
2301 klass->create_new_pad = gst_aggregator_default_create_new_pad;
2302 klass->update_src_caps = gst_aggregator_default_update_src_caps;
2303 klass->fixate_src_caps = gst_aggregator_default_fixate_src_caps;
2304 klass->negotiated_src_caps = gst_aggregator_default_negotiated_src_caps;
2306 gstelement_class->request_new_pad =
2307 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
2308 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
2309 gstelement_class->release_pad =
2310 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
2311 gstelement_class->change_state =
2312 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
2314 gobject_class->set_property = gst_aggregator_set_property;
2315 gobject_class->get_property = gst_aggregator_get_property;
2316 gobject_class->finalize = gst_aggregator_finalize;
2318 g_object_class_install_property (gobject_class, PROP_LATENCY,
2319 g_param_spec_int64 ("latency", "Buffer latency",
2320 "Additional latency in live mode to allow upstream "
2321 "to take longer to produce buffers for the current "
2322 "position (in nanoseconds)", 0,
2323 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
2324 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2326 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
2327 g_param_spec_enum ("start-time-selection", "Start Time Selection",
2328 "Decides which start time is output",
2329 gst_aggregator_start_time_selection_get_type (),
2330 DEFAULT_START_TIME_SELECTION,
2331 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2333 g_object_class_install_property (gobject_class, PROP_START_TIME,
2334 g_param_spec_uint64 ("start-time", "Start Time",
2335 "Start time to use if start-time-selection=set", 0,
2337 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2339 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
2340 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_do_events_and_queries);
2344 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
2346 GstPadTemplate *pad_template;
2347 GstAggregatorPrivate *priv;
2349 g_return_if_fail (klass->aggregate != NULL);
2352 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
2353 GstAggregatorPrivate);
2358 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
2359 g_return_if_fail (pad_template != NULL);
2361 priv->max_padserial = -1;
2362 priv->tags_changed = FALSE;
2364 self->priv->peer_latency_live = FALSE;
2365 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
2366 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
2367 self->priv->has_peer_latency = FALSE;
2368 gst_aggregator_reset_flow_values (self);
2370 self->srcpad = gst_pad_new_from_template (pad_template, "src");
2372 gst_pad_set_event_function (self->srcpad,
2373 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
2374 gst_pad_set_query_function (self->srcpad,
2375 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
2376 gst_pad_set_activatemode_function (self->srcpad,
2377 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
2379 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
2381 self->priv->latency = DEFAULT_LATENCY;
2382 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
2383 self->priv->start_time = DEFAULT_START_TIME;
2385 g_mutex_init (&self->priv->src_lock);
2386 g_cond_init (&self->priv->src_cond);
2389 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
2390 * method to get to the padtemplates */
2392 gst_aggregator_get_type (void)
2394 static volatile gsize type = 0;
2396 if (g_once_init_enter (&type)) {
2398 static const GTypeInfo info = {
2399 sizeof (GstAggregatorClass),
2402 (GClassInitFunc) gst_aggregator_class_init,
2405 sizeof (GstAggregator),
2407 (GInstanceInitFunc) gst_aggregator_init,
2410 _type = g_type_register_static (GST_TYPE_ELEMENT,
2411 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
2412 g_once_init_leave (&type, _type);
2417 /* Must be called with SRC lock and PAD lock held */
2419 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
2421 /* Empty queue always has space */
2422 if (aggpad->priv->num_buffers == 0 && aggpad->priv->clipped_buffer == NULL)
2425 /* We also want at least two buffers, one is being processed and one is ready
2426 * for the next iteration when we operate in live mode. */
2427 if (self->priv->peer_latency_live && aggpad->priv->num_buffers < 2)
2430 /* zero latency, if there is a buffer, it's full */
2431 if (self->priv->latency == 0)
2434 /* Allow no more buffers than the latency */
2435 return (aggpad->priv->time_level <= self->priv->latency);
2438 /* Must be called with the PAD_LOCK held */
2440 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2442 GstClockTime timestamp;
2444 if (GST_BUFFER_DTS_IS_VALID (buffer))
2445 timestamp = GST_BUFFER_DTS (buffer);
2447 timestamp = GST_BUFFER_PTS (buffer);
2449 if (timestamp == GST_CLOCK_TIME_NONE) {
2451 timestamp = aggpad->priv->head_position;
2453 timestamp = aggpad->priv->tail_position;
2457 if (GST_BUFFER_DURATION_IS_VALID (buffer))
2458 timestamp += GST_BUFFER_DURATION (buffer);
2461 aggpad->priv->head_position = timestamp;
2463 aggpad->priv->tail_position = timestamp;
2465 update_time_level (aggpad, head);
2468 static GstFlowReturn
2469 gst_aggregator_pad_chain_internal (GstAggregator * self,
2470 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
2472 GstFlowReturn flow_return;
2473 GstClockTime buf_pts;
2475 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
2477 PAD_FLUSH_LOCK (aggpad);
2480 flow_return = aggpad->priv->flow_return;
2481 if (flow_return != GST_FLOW_OK)
2484 PAD_UNLOCK (aggpad);
2486 buf_pts = GST_BUFFER_PTS (buffer);
2490 GST_OBJECT_LOCK (self);
2493 if (aggpad->priv->first_buffer) {
2494 self->priv->has_peer_latency = FALSE;
2495 aggpad->priv->first_buffer = FALSE;
2498 if (gst_aggregator_pad_has_space (self, aggpad)
2499 && aggpad->priv->flow_return == GST_FLOW_OK) {
2501 g_queue_push_head (&aggpad->priv->data, buffer);
2503 g_queue_push_tail (&aggpad->priv->data, buffer);
2504 apply_buffer (aggpad, buffer, head);
2505 aggpad->priv->num_buffers++;
2507 SRC_BROADCAST (self);
2511 flow_return = aggpad->priv->flow_return;
2512 if (flow_return != GST_FLOW_OK) {
2513 GST_OBJECT_UNLOCK (self);
2517 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2518 GST_OBJECT_UNLOCK (self);
2520 PAD_WAIT_EVENT (aggpad);
2522 PAD_UNLOCK (aggpad);
2525 if (self->priv->first_buffer) {
2526 GstClockTime start_time;
2528 switch (self->priv->start_time_selection) {
2529 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
2533 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
2534 GST_OBJECT_LOCK (aggpad);
2535 if (aggpad->priv->head_segment.format == GST_FORMAT_TIME) {
2536 start_time = buf_pts;
2537 if (start_time != -1) {
2538 start_time = MAX (start_time, aggpad->priv->head_segment.start);
2540 gst_segment_to_running_time (&aggpad->priv->head_segment,
2541 GST_FORMAT_TIME, start_time);
2545 GST_WARNING_OBJECT (aggpad,
2546 "Ignoring request of selecting the first start time "
2547 "as the segment is a %s segment instead of a time segment",
2548 gst_format_get_name (aggpad->segment.format));
2550 GST_OBJECT_UNLOCK (aggpad);
2552 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
2553 start_time = self->priv->start_time;
2554 if (start_time == -1)
2559 if (start_time != -1) {
2560 if (self->segment.position == -1)
2561 self->segment.position = start_time;
2563 self->segment.position = MIN (start_time, self->segment.position);
2565 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
2566 GST_TIME_ARGS (start_time));
2570 PAD_UNLOCK (aggpad);
2571 GST_OBJECT_UNLOCK (self);
2574 PAD_FLUSH_UNLOCK (aggpad);
2576 GST_DEBUG_OBJECT (aggpad, "Done chaining");
2581 PAD_UNLOCK (aggpad);
2582 PAD_FLUSH_UNLOCK (aggpad);
2584 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
2585 gst_flow_get_name (flow_return));
2587 gst_buffer_unref (buffer);
2592 static GstFlowReturn
2593 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
2595 return gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
2596 GST_AGGREGATOR_PAD_CAST (pad), buffer, TRUE);
2600 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
2603 GstAggregator *self = GST_AGGREGATOR (parent);
2604 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2605 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2607 if (GST_QUERY_IS_SERIALIZED (query)) {
2609 gboolean ret = FALSE;
2614 if (aggpad->priv->flow_return != GST_FLOW_OK) {
2619 g_queue_push_head (&aggpad->priv->data, query);
2620 SRC_BROADCAST (self);
2623 while (!gst_aggregator_pad_queue_is_empty (aggpad)
2624 && aggpad->priv->flow_return == GST_FLOW_OK) {
2625 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
2626 PAD_WAIT_EVENT (aggpad);
2629 s = gst_query_writable_structure (query);
2630 if (gst_structure_get_boolean (s, "gst-aggregator-retval", &ret))
2631 gst_structure_remove_field (s, "gst-aggregator-retval");
2633 g_queue_remove (&aggpad->priv->data, query);
2635 if (aggpad->priv->flow_return != GST_FLOW_OK)
2638 PAD_UNLOCK (aggpad);
2643 return klass->sink_query (self, aggpad, query);
2646 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2647 gst_flow_get_name (aggpad->priv->flow_return));
2648 PAD_UNLOCK (aggpad);
2653 /* Queue serialized events and let the others go though directly.
2654 * The queued events with be handled from the src-pad task in
2655 * gst_aggregator_do_events_and_queries().
2657 static GstFlowReturn
2658 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
2661 GstFlowReturn ret = GST_FLOW_OK;
2662 GstAggregator *self = GST_AGGREGATOR (parent);
2663 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2664 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2666 if (GST_EVENT_IS_SERIALIZED (event)) {
2670 if (aggpad->priv->flow_return != GST_FLOW_OK
2671 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2672 ret = aggpad->priv->flow_return;
2676 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2677 GST_OBJECT_LOCK (aggpad);
2678 gst_event_copy_segment (event, &aggpad->priv->head_segment);
2679 aggpad->priv->head_position = aggpad->priv->head_segment.position;
2680 update_time_level (aggpad, TRUE);
2681 GST_OBJECT_UNLOCK (aggpad);
2684 if (GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
2685 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT,
2687 g_queue_push_head (&aggpad->priv->data, event);
2689 SRC_BROADCAST (self);
2691 PAD_UNLOCK (aggpad);
2696 if (!klass->sink_event (self, aggpad, event)) {
2697 /* Copied from GstPad to convert boolean to a GstFlowReturn in
2698 * the event handling func */
2699 ret = GST_FLOW_ERROR;
2706 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2707 gst_flow_get_name (aggpad->priv->flow_return));
2708 PAD_UNLOCK (aggpad);
2710 if (GST_EVENT_IS_STICKY (event))
2711 gst_pad_store_sticky_event (pad, event);
2712 gst_event_unref (event);
2718 gst_aggregator_pad_activate_mode_func (GstPad * pad,
2719 GstObject * parent, GstPadMode mode, gboolean active)
2721 GstAggregator *self = GST_AGGREGATOR (parent);
2722 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2724 if (active == FALSE) {
2726 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2727 SRC_BROADCAST (self);
2731 aggpad->priv->flow_return = GST_FLOW_OK;
2732 PAD_BROADCAST_EVENT (aggpad);
2733 PAD_UNLOCK (aggpad);
2739 /***********************************
2740 * GstAggregatorPad implementation *
2741 ************************************/
2742 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
2745 gst_aggregator_pad_constructed (GObject * object)
2747 GstPad *pad = GST_PAD (object);
2749 gst_pad_set_chain_function (pad,
2750 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
2751 gst_pad_set_event_full_function_full (pad,
2752 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
2753 gst_pad_set_query_function (pad,
2754 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
2755 gst_pad_set_activatemode_function (pad,
2756 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
2760 gst_aggregator_pad_finalize (GObject * object)
2762 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2764 g_cond_clear (&pad->priv->event_cond);
2765 g_mutex_clear (&pad->priv->flush_lock);
2766 g_mutex_clear (&pad->priv->lock);
2768 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2772 gst_aggregator_pad_dispose (GObject * object)
2774 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2776 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
2778 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2782 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2784 GObjectClass *gobject_class = (GObjectClass *) klass;
2786 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2788 gobject_class->constructed = gst_aggregator_pad_constructed;
2789 gobject_class->finalize = gst_aggregator_pad_finalize;
2790 gobject_class->dispose = gst_aggregator_pad_dispose;
2794 gst_aggregator_pad_init (GstAggregatorPad * pad)
2797 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2798 GstAggregatorPadPrivate);
2800 g_queue_init (&pad->priv->data);
2801 g_cond_init (&pad->priv->event_cond);
2803 g_mutex_init (&pad->priv->flush_lock);
2804 g_mutex_init (&pad->priv->lock);
2806 gst_aggregator_pad_reset_unlocked (pad);
2807 pad->priv->negotiated = FALSE;
2810 /* Must be called with the PAD_LOCK held */
2812 gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad)
2814 pad->priv->num_buffers--;
2815 GST_TRACE_OBJECT (pad, "Consuming buffer");
2816 PAD_BROADCAST_EVENT (pad);
2819 /* Must be called with the PAD_LOCK held */
2821 gst_aggregator_pad_clip_buffer_unlocked (GstAggregatorPad * pad)
2823 GstAggregator *self = NULL;
2824 GstAggregatorClass *aggclass = NULL;
2825 GstBuffer *buffer = NULL;
2827 while (pad->priv->clipped_buffer == NULL &&
2828 GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->data))) {
2829 buffer = g_queue_pop_tail (&pad->priv->data);
2831 apply_buffer (pad, buffer, FALSE);
2833 /* We only take the parent here so that it's not taken if the buffer is
2834 * already clipped or if the queue is empty.
2837 self = GST_AGGREGATOR (gst_pad_get_parent_element (GST_PAD (pad)));
2839 gst_buffer_unref (buffer);
2843 aggclass = GST_AGGREGATOR_GET_CLASS (self);
2846 if (aggclass->clip) {
2847 GST_TRACE_OBJECT (pad, "Clipping: %" GST_PTR_FORMAT, buffer);
2849 buffer = aggclass->clip (self, pad, buffer);
2851 if (buffer == NULL) {
2852 gst_aggregator_pad_buffer_consumed (pad);
2853 GST_TRACE_OBJECT (pad, "Clipping consumed the buffer");
2857 pad->priv->clipped_buffer = buffer;
2861 gst_object_unref (self);
2865 * gst_aggregator_pad_steal_buffer:
2866 * @pad: the pad to get buffer from
2868 * Steal the ref to the buffer currently queued in @pad.
2870 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2871 * queued. You should unref the buffer after usage.
2874 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2880 gst_aggregator_pad_clip_buffer_unlocked (pad);
2882 buffer = pad->priv->clipped_buffer;
2885 pad->priv->clipped_buffer = NULL;
2886 gst_aggregator_pad_buffer_consumed (pad);
2887 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2896 * gst_aggregator_pad_drop_buffer:
2897 * @pad: the pad where to drop any pending buffer
2899 * Drop the buffer currently queued in @pad.
2901 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2904 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2908 buf = gst_aggregator_pad_steal_buffer (pad);
2913 gst_buffer_unref (buf);
2918 * gst_aggregator_pad_get_buffer:
2919 * @pad: the pad to get buffer from
2921 * Returns: (transfer full): A reference to the buffer in @pad or
2922 * NULL if no buffer was queued. You should unref the buffer after
2926 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2932 gst_aggregator_pad_clip_buffer_unlocked (pad);
2934 if (pad->priv->clipped_buffer) {
2935 buffer = gst_buffer_ref (pad->priv->clipped_buffer);
2945 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2950 is_eos = pad->priv->eos;
2957 * gst_aggregator_merge_tags:
2958 * @self: a #GstAggregator
2959 * @tags: a #GstTagList to merge
2960 * @mode: the #GstTagMergeMode to use
2962 * Adds tags to so-called pending tags, which will be processed
2963 * before pushing out data downstream.
2965 * Note that this is provided for convenience, and the subclass is
2966 * not required to use this and can still do tag handling on its own.
2971 gst_aggregator_merge_tags (GstAggregator * self,
2972 const GstTagList * tags, GstTagMergeMode mode)
2976 g_return_if_fail (GST_IS_AGGREGATOR (self));
2977 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2979 /* FIXME Check if we can use OBJECT lock here! */
2980 GST_OBJECT_LOCK (self);
2982 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2983 otags = self->priv->tags;
2984 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2986 gst_tag_list_unref (otags);
2987 self->priv->tags_changed = TRUE;
2988 GST_OBJECT_UNLOCK (self);
2992 * gst_aggregator_set_latency:
2993 * @self: a #GstAggregator
2994 * @min_latency: minimum latency
2995 * @max_latency: maximum latency
2997 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2998 * latency is. Will also post a LATENCY message on the bus so the pipeline
2999 * can reconfigure its global latency.
3002 gst_aggregator_set_latency (GstAggregator * self,
3003 GstClockTime min_latency, GstClockTime max_latency)
3005 gboolean changed = FALSE;
3007 g_return_if_fail (GST_IS_AGGREGATOR (self));
3008 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
3009 g_return_if_fail (max_latency >= min_latency);
3012 if (self->priv->sub_latency_min != min_latency) {
3013 self->priv->sub_latency_min = min_latency;
3016 if (self->priv->sub_latency_max != max_latency) {
3017 self->priv->sub_latency_max = max_latency;
3022 SRC_BROADCAST (self);
3026 gst_element_post_message (GST_ELEMENT_CAST (self),
3027 gst_message_new_latency (GST_OBJECT_CAST (self)));
3032 * gst_aggregator_get_buffer_pool:
3033 * @self: a #GstAggregator
3035 * Returns: (transfer full): the instance of the #GstBufferPool used
3036 * by @trans; free it after use it
3039 gst_aggregator_get_buffer_pool (GstAggregator * self)
3041 GstBufferPool *pool;
3043 g_return_val_if_fail (GST_IS_AGGREGATOR (self), NULL);
3045 GST_OBJECT_LOCK (self);
3046 pool = self->priv->pool;
3048 gst_object_ref (pool);
3049 GST_OBJECT_UNLOCK (self);
3055 * gst_aggregator_get_allocator:
3056 * @self: a #GstAggregator
3057 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3059 * @params: (out) (allow-none) (transfer full): the
3060 * #GstAllocationParams of @allocator
3062 * Lets #GstAggregator sub-classes get the memory @allocator
3063 * acquired by the base class and its @params.
3065 * Unref the @allocator after use it.
3068 gst_aggregator_get_allocator (GstAggregator * self,
3069 GstAllocator ** allocator, GstAllocationParams * params)
3071 g_return_if_fail (GST_IS_AGGREGATOR (self));
3074 *allocator = self->priv->allocator ?
3075 gst_object_ref (self->priv->allocator) : NULL;
3078 *params = self->priv->allocation_params;