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 * @short_description: manages a set of pads with the purpose of
25 * aggregating their buffers.
26 * @see_also: gstcollectpads for historical reasons.
28 * Manages a set of pads with the purpose of aggregating their buffers.
29 * 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.
36 * When data is queued on all pads, tha aggregate vmethod is called.
39 * One can peek at the data on any given GstAggregatorPad with the
40 * gst_aggregator_pad_get_buffer () method, and take ownership of it
41 * with the gst_aggregator_pad_steal_buffer () method. When a buffer
42 * has been taken with steal_buffer (), a new buffer can be queued
46 * If the subclass wishes to push a buffer downstream in its aggregate
47 * implementation, it should do so through the
48 * gst_aggregator_finish_buffer () method. This method will take care
49 * of sending and ordering mandatory events such as stream start, caps
53 * Same goes for EOS events, which should not be pushed directly by the
54 * subclass, it should instead return GST_FLOW_EOS in its aggregate
58 * Note that the aggregator logic regarding gap event handling is to turn
59 * these into gap buffers with matching PTS and duration. It will also
60 * flag these buffers with GST_BUFFER_FLAG_GAP and GST_BUFFER_FLAG_DROPPABLE
61 * to ease their identification and subsequent processing.
70 #include <string.h> /* strlen */
72 #include "gstaggregator.h"
75 /* Might become API */
76 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
77 const GstTagList * tags, GstTagMergeMode mode);
78 static void gst_aggregator_set_latency_property (GstAggregator * agg,
80 static gint64 gst_aggregator_get_latency_property (GstAggregator * agg);
83 /* Locking order, locks in this element must always be taken in this order
85 * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
86 * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
87 * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
88 * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
89 * standard element object lock -> GST_OBJECT_LOCK(agg)
90 * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
91 * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
92 * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
96 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
98 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
99 #define GST_CAT_DEFAULT aggregator_debug
101 /* GstAggregatorPad definitions */
102 #define PAD_LOCK(pad) G_STMT_START { \
103 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
105 g_mutex_lock(&pad->priv->lock); \
106 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
110 #define PAD_UNLOCK(pad) G_STMT_START { \
111 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
113 g_mutex_unlock(&pad->priv->lock); \
114 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
119 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
120 GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p", \
122 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
123 (&((GstAggregatorPad*)pad)->priv->lock)); \
124 GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
128 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
129 GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p", \
131 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
135 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
136 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
138 g_mutex_lock(&pad->priv->flush_lock); \
139 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
143 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
144 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
146 g_mutex_unlock(&pad->priv->flush_lock); \
147 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
151 #define SRC_LOCK(self) G_STMT_START { \
152 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
154 g_mutex_lock(&self->priv->src_lock); \
155 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
159 #define SRC_UNLOCK(self) G_STMT_START { \
160 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
162 g_mutex_unlock(&self->priv->src_lock); \
163 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
167 #define SRC_WAIT(self) G_STMT_START { \
168 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
170 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
171 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
175 #define SRC_BROADCAST(self) G_STMT_START { \
176 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
178 if (self->priv->aggregate_id) \
179 gst_clock_id_unschedule (self->priv->aggregate_id); \
180 g_cond_broadcast(&(self->priv->src_cond)); \
183 struct _GstAggregatorPadPrivate
185 /* Following fields are protected by the PAD_LOCK */
186 GstFlowReturn flow_return;
187 gboolean pending_flush_start;
188 gboolean pending_flush_stop;
189 gboolean pending_eos;
196 /* This lock prevents a flush start processing happening while
197 * the chain function is also happening.
203 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
205 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
208 aggpad->priv->pending_eos = FALSE;
209 aggpad->priv->eos = FALSE;
210 aggpad->priv->flow_return = GST_FLOW_OK;
214 return klass->flush (aggpad, agg);
219 /*************************************
220 * GstAggregator implementation *
221 *************************************/
222 static GstElementClass *aggregator_parent_class = NULL;
224 /* All members are protected by the object lock unless otherwise noted */
226 struct _GstAggregatorPrivate
230 /* Our state is >= PAUSED */
231 gboolean running; /* protected by src_lock */
234 gboolean send_stream_start; /* protected by srcpad stream lock */
235 gboolean send_segment;
236 gboolean flush_seeking;
237 gboolean pending_flush_start;
238 gboolean send_eos; /* protected by srcpad stream lock */
240 GstCaps *srccaps; /* protected by the srcpad stream lock */
243 gboolean tags_changed;
245 gboolean peer_latency_live; /* protected by src_lock */
246 GstClockTime peer_latency_min; /* protected by src_lock */
247 GstClockTime peer_latency_max; /* protected by src_lock */
248 gboolean has_peer_latency;
250 GstClockTime sub_latency_min; /* protected by src_lock */
251 GstClockTime sub_latency_max; /* protected by src_lock */
254 GstClockID aggregate_id; /* protected by src_lock */
268 gboolean one_actually_seeked;
271 #define DEFAULT_LATENCY 0
281 * gst_aggregator_iterate_sinkpads:
282 * @self: The #GstAggregator
283 * @func: (scope call): The function to call.
284 * @user_data: (closure): The data to pass to @func.
286 * Iterate the sinkpads of aggregator to call a function on them.
288 * This method guarantees that @func will be called only once for each
292 gst_aggregator_iterate_sinkpads (GstAggregator * self,
293 GstAggregatorPadForeachFunc func, gpointer user_data)
295 gboolean result = FALSE;
297 gboolean done = FALSE;
298 GValue item = { 0, };
299 GList *seen_pads = NULL;
301 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
307 switch (gst_iterator_next (iter, &item)) {
308 case GST_ITERATOR_OK:
310 GstAggregatorPad *pad;
312 pad = g_value_get_object (&item);
314 /* if already pushed, skip. FIXME, find something faster to tag pads */
315 if (pad == NULL || g_list_find (seen_pads, pad)) {
316 g_value_reset (&item);
320 GST_LOG_OBJECT (pad, "calling function %s on pad",
321 GST_DEBUG_FUNCPTR_NAME (func));
323 result = func (self, pad, user_data);
327 seen_pads = g_list_prepend (seen_pads, pad);
329 g_value_reset (&item);
332 case GST_ITERATOR_RESYNC:
333 gst_iterator_resync (iter);
335 case GST_ITERATOR_ERROR:
336 GST_ERROR_OBJECT (self,
337 "Could not iterate over internally linked pads");
340 case GST_ITERATOR_DONE:
345 g_value_unset (&item);
346 gst_iterator_free (iter);
348 if (seen_pads == NULL) {
349 GST_DEBUG_OBJECT (self, "No pad seen");
353 g_list_free (seen_pads);
360 gst_aggregator_check_pads_ready (GstAggregator * self)
362 GstAggregatorPad *pad;
365 GST_LOG_OBJECT (self, "checking pads");
367 GST_OBJECT_LOCK (self);
369 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
370 if (sinkpads == NULL)
373 for (l = sinkpads; l != NULL; l = l->next) {
377 if (pad->priv->buffer == NULL && !pad->priv->eos) {
385 GST_OBJECT_UNLOCK (self);
386 GST_LOG_OBJECT (self, "pads are ready");
391 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
392 GST_OBJECT_UNLOCK (self);
397 GST_LOG_OBJECT (pad, "pad not ready to be aggregated yet");
398 GST_OBJECT_UNLOCK (self);
404 gst_aggregator_reset_flow_values (GstAggregator * self)
406 GST_OBJECT_LOCK (self);
407 self->priv->send_stream_start = TRUE;
408 self->priv->send_segment = TRUE;
409 gst_segment_init (&self->segment, GST_FORMAT_TIME);
410 GST_OBJECT_UNLOCK (self);
414 gst_aggregator_push_mandatory_events (GstAggregator * self)
416 GstAggregatorPrivate *priv = self->priv;
417 GstEvent *segment = NULL;
418 GstEvent *tags = NULL;
420 if (self->priv->send_stream_start) {
423 GST_INFO_OBJECT (self, "pushing stream start");
424 /* stream-start (FIXME: create id based on input ids) */
425 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
426 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
427 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
429 self->priv->send_stream_start = FALSE;
432 if (self->priv->srccaps) {
434 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
435 self->priv->srccaps);
436 if (!gst_pad_push_event (self->srcpad,
437 gst_event_new_caps (self->priv->srccaps))) {
438 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
440 gst_caps_unref (self->priv->srccaps);
441 self->priv->srccaps = NULL;
444 GST_OBJECT_LOCK (self);
445 if (self->priv->send_segment && !self->priv->flush_seeking) {
446 segment = gst_event_new_segment (&self->segment);
448 if (!self->priv->seqnum)
449 self->priv->seqnum = gst_event_get_seqnum (segment);
451 gst_event_set_seqnum (segment, self->priv->seqnum);
452 self->priv->send_segment = FALSE;
454 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
457 if (priv->tags && priv->tags_changed && !self->priv->flush_seeking) {
458 tags = gst_event_new_tag (gst_tag_list_ref (priv->tags));
459 priv->tags_changed = FALSE;
461 GST_OBJECT_UNLOCK (self);
464 gst_pad_push_event (self->srcpad, segment);
466 gst_pad_push_event (self->srcpad, tags);
471 * gst_aggregator_set_src_caps:
472 * @self: The #GstAggregator
473 * @caps: The #GstCaps to set on the src pad.
475 * Sets the caps to be used on the src pad.
478 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
480 GST_PAD_STREAM_LOCK (self->srcpad);
481 gst_caps_replace (&self->priv->srccaps, caps);
482 gst_aggregator_push_mandatory_events (self);
483 GST_PAD_STREAM_UNLOCK (self->srcpad);
487 * gst_aggregator_finish_buffer:
488 * @self: The #GstAggregator
489 * @buffer: (transfer full): the #GstBuffer to push.
491 * This method will push the provided output buffer downstream. If needed,
492 * mandatory events such as stream-start, caps, and segment events will be
493 * sent before pushing the buffer.
496 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
498 gst_aggregator_push_mandatory_events (self);
500 GST_OBJECT_LOCK (self);
501 if (!self->priv->flush_seeking && gst_pad_is_active (self->srcpad)) {
502 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
503 GST_OBJECT_UNLOCK (self);
504 return gst_pad_push (self->srcpad, buffer);
506 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
507 self->priv->flush_seeking, gst_pad_is_active (self->srcpad));
508 GST_OBJECT_UNLOCK (self);
509 gst_buffer_unref (buffer);
515 gst_aggregator_push_eos (GstAggregator * self)
518 gst_aggregator_push_mandatory_events (self);
520 event = gst_event_new_eos ();
522 GST_OBJECT_LOCK (self);
523 self->priv->send_eos = FALSE;
524 gst_event_set_seqnum (event, self->priv->seqnum);
525 GST_OBJECT_UNLOCK (self);
527 gst_pad_push_event (self->srcpad, event);
531 gst_aggregator_get_next_time (GstAggregator * self)
533 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
535 if (klass->get_next_time)
536 return klass->get_next_time (self);
538 return GST_CLOCK_TIME_NONE;
542 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
544 GstClockTime latency;
552 latency = gst_aggregator_get_latency_unlocked (self);
554 if (gst_aggregator_check_pads_ready (self)) {
555 GST_DEBUG_OBJECT (self, "all pads have data");
561 /* Before waiting, check if we're actually still running */
562 if (!self->priv->running || !self->priv->send_eos) {
568 start = gst_aggregator_get_next_time (self);
570 if (!GST_CLOCK_TIME_IS_VALID (latency) ||
571 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
572 !GST_CLOCK_TIME_IS_VALID (start)) {
573 /* We wake up here when something happened, and below
574 * then check if we're ready now. If we return FALSE,
575 * we will be directly called again.
579 GstClockTime base_time, time;
581 GstClockReturn status;
582 GstClockTimeDiff jitter;
584 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
585 GST_TIME_ARGS (start));
587 GST_OBJECT_LOCK (self);
588 base_time = GST_ELEMENT_CAST (self)->base_time;
589 clock = GST_ELEMENT_CLOCK (self);
591 gst_object_ref (clock);
592 GST_OBJECT_UNLOCK (self);
594 time = base_time + start;
597 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
598 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
599 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
600 GST_TIME_ARGS (time),
601 GST_TIME_ARGS (GST_ELEMENT_CAST (self)->base_time),
602 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
603 GST_TIME_ARGS (gst_clock_get_time (clock)));
606 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
607 gst_object_unref (clock);
611 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
614 if (self->priv->aggregate_id) {
615 gst_clock_id_unref (self->priv->aggregate_id);
616 self->priv->aggregate_id = NULL;
619 GST_DEBUG_OBJECT (self,
620 "clock returned %d (jitter: %s%" GST_TIME_FORMAT ")",
621 status, (jitter < 0 ? "-" : " "),
622 GST_TIME_ARGS ((jitter < 0 ? -jitter : jitter)));
625 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
632 res = gst_aggregator_check_pads_ready (self);
639 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
640 GstFlowReturn flow_return)
643 if (flow_return == GST_FLOW_NOT_LINKED)
644 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
646 aggpad->priv->flow_return = flow_return;
647 gst_buffer_replace (&aggpad->priv->buffer, NULL);
648 PAD_BROADCAST_EVENT (aggpad);
653 gst_aggregator_aggregate_func (GstAggregator * self)
655 GstAggregatorPrivate *priv = self->priv;
656 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
657 gboolean timeout = FALSE;
659 if (self->priv->running == FALSE) {
660 GST_DEBUG_OBJECT (self, "Not running anymore");
664 GST_LOG_OBJECT (self, "Checking aggregate");
665 while (priv->send_eos && priv->running) {
666 GstFlowReturn flow_return;
668 if (!gst_aggregator_wait_and_check (self, &timeout))
671 GST_TRACE_OBJECT (self, "Actually aggregating!");
673 flow_return = klass->aggregate (self, timeout);
675 GST_OBJECT_LOCK (self);
676 if (flow_return == GST_FLOW_FLUSHING && priv->flush_seeking) {
677 /* We don't want to set the pads to flushing, but we want to
678 * stop the thread, so just break here */
679 GST_OBJECT_UNLOCK (self);
682 GST_OBJECT_UNLOCK (self);
684 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
685 gst_aggregator_push_eos (self);
688 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
690 if (flow_return != GST_FLOW_OK) {
693 GST_OBJECT_LOCK (self);
694 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
695 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
697 gst_aggregator_pad_set_flushing (aggpad, flow_return);
699 GST_OBJECT_UNLOCK (self);
704 /* Pause the task here, the only ways to get here are:
705 * 1) We're stopping, in which case the task is stopped anyway
706 * 2) We got a flow error above, in which case it might take
707 * some time to forward the flow return upstream and we
708 * would otherwise call the task function over and over
709 * again without doing anything
711 gst_pad_pause_task (self->srcpad);
715 gst_aggregator_start (GstAggregator * self)
717 GstAggregatorClass *klass;
720 self->priv->running = TRUE;
721 self->priv->send_stream_start = TRUE;
722 self->priv->send_segment = TRUE;
723 self->priv->send_eos = TRUE;
724 self->priv->srccaps = NULL;
726 klass = GST_AGGREGATOR_GET_CLASS (self);
729 result = klass->start (self);
737 _check_pending_flush_stop (GstAggregatorPad * pad)
742 res = (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
749 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
753 GST_INFO_OBJECT (self, "%s srcpad task",
754 flush_start ? "Pausing" : "Stopping");
757 self->priv->running = FALSE;
758 SRC_BROADCAST (self);
762 res = gst_pad_push_event (self->srcpad, flush_start);
765 gst_pad_stop_task (self->srcpad);
771 gst_aggregator_start_srcpad_task (GstAggregator * self)
773 GST_INFO_OBJECT (self, "Starting srcpad task");
775 self->priv->running = TRUE;
776 gst_pad_start_task (GST_PAD (self->srcpad),
777 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
781 gst_aggregator_flush (GstAggregator * self)
783 GstFlowReturn ret = GST_FLOW_OK;
784 GstAggregatorPrivate *priv = self->priv;
785 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
787 GST_DEBUG_OBJECT (self, "Flushing everything");
788 GST_OBJECT_LOCK (self);
789 priv->send_segment = TRUE;
790 priv->flush_seeking = FALSE;
791 priv->tags_changed = FALSE;
792 GST_OBJECT_UNLOCK (self);
794 ret = klass->flush (self);
800 /* Called with GstAggregator's object lock held */
803 gst_aggregator_all_flush_stop_received_locked (GstAggregator * self)
806 GstAggregatorPad *tmppad;
808 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
809 tmppad = (GstAggregatorPad *) tmp->data;
811 if (_check_pending_flush_stop (tmppad) == FALSE) {
812 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
813 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
822 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
825 GstAggregatorPrivate *priv = self->priv;
826 GstAggregatorPadPrivate *padpriv = aggpad->priv;
828 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING);
830 PAD_FLUSH_LOCK (aggpad);
832 if (padpriv->pending_flush_start) {
833 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
835 padpriv->pending_flush_start = FALSE;
836 padpriv->pending_flush_stop = TRUE;
840 GST_OBJECT_LOCK (self);
841 if (priv->flush_seeking) {
842 /* If flush_seeking we forward the first FLUSH_START */
843 if (priv->pending_flush_start) {
844 priv->pending_flush_start = FALSE;
845 GST_OBJECT_UNLOCK (self);
847 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
848 gst_aggregator_stop_srcpad_task (self, event);
850 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
851 GST_PAD_STREAM_LOCK (self->srcpad);
852 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
855 GST_OBJECT_UNLOCK (self);
856 gst_event_unref (event);
859 GST_OBJECT_UNLOCK (self);
860 gst_event_unref (event);
862 PAD_FLUSH_UNLOCK (aggpad);
864 gst_aggregator_pad_drop_buffer (aggpad);
867 /* GstAggregator vmethods default implementations */
869 gst_aggregator_default_sink_event (GstAggregator * self,
870 GstAggregatorPad * aggpad, GstEvent * event)
873 GstPad *pad = GST_PAD (aggpad);
874 GstAggregatorPrivate *priv = self->priv;
876 switch (GST_EVENT_TYPE (event)) {
877 case GST_EVENT_FLUSH_START:
879 gst_aggregator_flush_start (self, aggpad, event);
880 /* We forward only in one case: right after flush_seeking */
884 case GST_EVENT_FLUSH_STOP:
886 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
888 gst_aggregator_pad_flush (aggpad, self);
889 GST_OBJECT_LOCK (self);
890 if (priv->flush_seeking) {
891 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
892 if (gst_aggregator_all_flush_stop_received_locked (self)) {
893 GST_OBJECT_UNLOCK (self);
894 /* That means we received FLUSH_STOP/FLUSH_STOP on
895 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
896 gst_aggregator_flush (self);
897 gst_pad_push_event (self->srcpad, event);
900 priv->send_eos = TRUE;
901 SRC_BROADCAST (self);
904 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
905 GST_PAD_STREAM_UNLOCK (self->srcpad);
906 gst_aggregator_start_srcpad_task (self);
908 GST_OBJECT_UNLOCK (self);
911 GST_OBJECT_UNLOCK (self);
914 /* We never forward the event */
919 GST_DEBUG_OBJECT (aggpad, "EOS");
921 /* We still have a buffer, and we don't want the subclass to have to
922 * check for it. Mark pending_eos, eos will be set when steal_buffer is
927 if (!aggpad->priv->buffer) {
928 aggpad->priv->eos = TRUE;
930 aggpad->priv->pending_eos = TRUE;
934 SRC_BROADCAST (self);
938 case GST_EVENT_SEGMENT:
940 GST_OBJECT_LOCK (aggpad);
941 gst_event_copy_segment (event, &aggpad->segment);
942 GST_OBJECT_UNLOCK (aggpad);
944 GST_OBJECT_LOCK (self);
945 self->priv->seqnum = gst_event_get_seqnum (event);
946 GST_OBJECT_UNLOCK (self);
949 case GST_EVENT_STREAM_START:
956 GstClockTime duration;
959 gst_event_parse_gap (event, &pts, &duration);
960 gapbuf = gst_buffer_new ();
962 GST_BUFFER_PTS (gapbuf) = pts;
963 GST_BUFFER_DURATION (gapbuf) = duration;
964 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
965 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
967 if (gst_pad_chain (pad, gapbuf) != GST_FLOW_OK) {
968 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
978 gst_event_parse_tag (event, &tags);
980 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
981 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
982 gst_event_unref (event);
994 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
995 return gst_pad_event_default (pad, GST_OBJECT (self), event);
998 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1000 gst_event_unref (event);
1005 static inline gboolean
1006 gst_aggregator_stop_pad (GstAggregator * self, GstAggregatorPad * pad,
1007 gpointer unused_udata)
1009 gst_aggregator_pad_flush (pad, self);
1015 gst_aggregator_stop (GstAggregator * agg)
1017 GstAggregatorClass *klass;
1020 gst_aggregator_reset_flow_values (agg);
1022 gst_aggregator_iterate_sinkpads (agg, gst_aggregator_stop_pad, NULL);
1024 klass = GST_AGGREGATOR_GET_CLASS (agg);
1027 result = klass->stop (agg);
1031 agg->priv->has_peer_latency = FALSE;
1032 agg->priv->peer_latency_live = FALSE;
1033 agg->priv->peer_latency_min = agg->priv->peer_latency_max = FALSE;
1035 if (agg->priv->tags)
1036 gst_tag_list_unref (agg->priv->tags);
1037 agg->priv->tags = NULL;
1042 /* GstElement vmethods implementations */
1043 static GstStateChangeReturn
1044 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
1046 GstStateChangeReturn ret;
1047 GstAggregator *self = GST_AGGREGATOR (element);
1049 switch (transition) {
1050 case GST_STATE_CHANGE_READY_TO_PAUSED:
1051 if (!gst_aggregator_start (self))
1059 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
1060 transition)) == GST_STATE_CHANGE_FAILURE)
1064 switch (transition) {
1065 case GST_STATE_CHANGE_PAUSED_TO_READY:
1066 if (!gst_aggregator_stop (self)) {
1067 /* What to do in this case? Error out? */
1068 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
1080 GST_ERROR_OBJECT (element, "parent failed state change");
1085 GST_ERROR_OBJECT (element, "Subclass failed to start");
1086 return GST_STATE_CHANGE_FAILURE;
1091 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
1093 GstAggregator *self = GST_AGGREGATOR (element);
1094 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1096 GST_INFO_OBJECT (pad, "Removing pad");
1099 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING);
1100 gst_element_remove_pad (element, pad);
1102 SRC_BROADCAST (self);
1107 gst_aggregator_request_new_pad (GstElement * element,
1108 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1110 GstAggregator *self;
1111 GstAggregatorPad *agg_pad;
1113 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1114 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
1116 self = GST_AGGREGATOR (element);
1118 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1122 GST_OBJECT_LOCK (element);
1123 if (req_name == NULL || strlen (req_name) < 6
1124 || !g_str_has_prefix (req_name, "sink_")) {
1125 /* no name given when requesting the pad, use next available int */
1128 /* parse serial number from requested padname */
1129 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1130 if (serial >= priv->padcount)
1131 priv->padcount = serial;
1134 name = g_strdup_printf ("sink_%u", priv->padcount);
1135 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
1136 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
1139 GST_OBJECT_UNLOCK (element);
1145 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
1148 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
1150 /* add the pad to the element */
1151 gst_element_add_pad (element, GST_PAD (agg_pad));
1153 return GST_PAD (agg_pad);
1156 /* Must be called with SRC_LOCK held */
1159 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
1161 gboolean query_ret, live;
1162 GstClockTime our_latency, min, max;
1164 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1167 GST_WARNING_OBJECT (self, "Latency query failed");
1171 gst_query_parse_latency (query, &live, &min, &max);
1173 our_latency = self->priv->latency;
1175 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
1176 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
1177 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
1181 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
1182 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
1183 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
1184 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1185 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1189 self->priv->peer_latency_live = live;
1190 self->priv->peer_latency_min = min;
1191 self->priv->peer_latency_max = max;
1192 self->priv->has_peer_latency = TRUE;
1196 min += self->priv->sub_latency_min;
1197 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1198 && GST_CLOCK_TIME_IS_VALID (max))
1199 max += self->priv->sub_latency_max;
1201 max = GST_CLOCK_TIME_NONE;
1203 if (live && min > max) {
1204 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1205 ("%s", "Latency too big"),
1206 ("The requested latency value is too big for the current pipeline. "
1207 "Limiting to %" G_GINT64_FORMAT, max));
1209 /* FIXME: This could in theory become negative, but in
1210 * that case all is lost anyway */
1211 self->priv->latency -= min - max;
1212 /* FIXME: shouldn't we g_object_notify() the change here? */
1215 SRC_BROADCAST (self);
1217 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
1218 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1220 gst_query_set_latency (query, live, min, max);
1226 * MUST be called with the src_lock held.
1228 * See gst_aggregator_get_latency() for doc
1231 gst_aggregator_get_latency_unlocked (GstAggregator * self)
1233 GstClockTime latency;
1235 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
1237 if (!self->priv->has_peer_latency) {
1238 GstQuery *query = gst_query_new_latency ();
1241 ret = gst_aggregator_query_latency_unlocked (self, query);
1242 gst_query_unref (query);
1244 return GST_CLOCK_TIME_NONE;
1247 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
1248 return GST_CLOCK_TIME_NONE;
1250 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
1251 latency = self->priv->peer_latency_min;
1254 latency += self->priv->latency;
1255 latency += self->priv->sub_latency_min;
1261 * gst_aggregator_get_latency:
1262 * @self: a #GstAggregator
1264 * Retrieves the latency values reported by @self in response to the latency
1265 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
1266 * will not wait for the clock.
1268 * Typically only called by subclasses.
1270 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
1273 gst_aggregator_get_latency (GstAggregator * self)
1278 ret = gst_aggregator_get_latency_unlocked (self);
1285 gst_aggregator_send_event (GstElement * element, GstEvent * event)
1287 GstAggregator *self = GST_AGGREGATOR (element);
1289 GST_STATE_LOCK (element);
1290 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
1291 GST_STATE (element) < GST_STATE_PAUSED) {
1295 GstSeekType start_type, stop_type;
1298 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1299 &start, &stop_type, &stop);
1301 GST_OBJECT_LOCK (self);
1302 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1303 stop_type, stop, NULL);
1304 self->priv->seqnum = gst_event_get_seqnum (event);
1305 GST_OBJECT_UNLOCK (self);
1307 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
1309 GST_STATE_UNLOCK (element);
1312 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
1317 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
1319 gboolean res = TRUE;
1321 switch (GST_QUERY_TYPE (query)) {
1322 case GST_QUERY_SEEKING:
1326 /* don't pass it along as some (file)sink might claim it does
1327 * whereas with a collectpads in between that will not likely work */
1328 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1329 gst_query_set_seeking (query, format, FALSE, 0, -1);
1334 case GST_QUERY_LATENCY:
1336 res = gst_aggregator_query_latency_unlocked (self, query);
1340 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
1347 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
1349 EventData *evdata = user_data;
1350 gboolean ret = TRUE;
1351 GstPad *peer = gst_pad_get_peer (pad);
1352 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1355 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
1356 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
1357 gst_object_unref (peer);
1361 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
1362 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
1364 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
1365 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
1367 if (gst_pad_query (peer, seeking)) {
1370 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
1372 if (seekable == FALSE) {
1373 GST_INFO_OBJECT (pad,
1374 "Source not seekable, We failed but it does not matter!");
1379 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
1382 gst_query_unref (seeking);
1385 if (evdata->flush) {
1387 aggpad->priv->pending_flush_start = FALSE;
1388 aggpad->priv->pending_flush_stop = FALSE;
1389 PAD_UNLOCK (aggpad);
1392 evdata->one_actually_seeked = TRUE;
1395 evdata->result &= ret;
1397 /* Always send to all pads */
1402 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
1403 GstEvent * event, gboolean flush)
1407 evdata.event = event;
1408 evdata.result = TRUE;
1409 evdata.flush = flush;
1410 evdata.one_actually_seeked = FALSE;
1412 /* We first need to set all pads as flushing in a first pass
1413 * as flush_start flush_stop is sometimes sent synchronously
1414 * while we send the seek event */
1418 GST_OBJECT_LOCK (self);
1419 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1420 GstAggregatorPad *pad = l->data;
1423 pad->priv->pending_flush_start = TRUE;
1424 pad->priv->pending_flush_stop = FALSE;
1427 GST_OBJECT_UNLOCK (self);
1430 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, &evdata);
1432 gst_event_unref (event);
1438 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
1443 GstSeekType start_type, stop_type;
1447 GstAggregatorPrivate *priv = self->priv;
1449 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1450 &start, &stop_type, &stop);
1452 GST_INFO_OBJECT (self, "starting SEEK");
1454 flush = flags & GST_SEEK_FLAG_FLUSH;
1456 GST_OBJECT_LOCK (self);
1458 priv->pending_flush_start = TRUE;
1459 priv->flush_seeking = TRUE;
1462 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1463 stop_type, stop, NULL);
1464 GST_OBJECT_UNLOCK (self);
1466 /* forward the seek upstream */
1467 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, flush);
1470 if (!evdata.result || !evdata.one_actually_seeked) {
1471 GST_OBJECT_LOCK (self);
1472 priv->flush_seeking = FALSE;
1473 priv->pending_flush_start = FALSE;
1474 GST_OBJECT_UNLOCK (self);
1477 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1479 return evdata.result;
1483 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
1486 gboolean res = TRUE;
1488 switch (GST_EVENT_TYPE (event)) {
1489 case GST_EVENT_SEEK:
1491 gst_event_ref (event);
1492 res = gst_aggregator_do_seek (self, event);
1493 gst_event_unref (event);
1497 case GST_EVENT_NAVIGATION:
1499 /* navigation is rather pointless. */
1501 gst_event_unref (event);
1510 evdata = gst_aggregator_forward_event_to_all_sinkpads (self, event, FALSE);
1511 res = evdata.result;
1518 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
1521 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1523 return klass->src_event (GST_AGGREGATOR (parent), event);
1527 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
1530 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1532 return klass->src_query (GST_AGGREGATOR (parent), query);
1536 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
1537 GstObject * parent, GstPadMode mode, gboolean active)
1539 GstAggregator *self = GST_AGGREGATOR (parent);
1540 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1542 if (klass->src_activate) {
1543 if (klass->src_activate (self, mode, active) == FALSE) {
1548 if (active == TRUE) {
1550 case GST_PAD_MODE_PUSH:
1552 GST_INFO_OBJECT (pad, "Activating pad!");
1553 gst_aggregator_start_srcpad_task (self);
1558 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1565 GST_INFO_OBJECT (self, "Deactivating srcpad");
1566 gst_aggregator_stop_srcpad_task (self, FALSE);
1572 gst_aggregator_default_sink_query (GstAggregator * self,
1573 GstAggregatorPad * aggpad, GstQuery * query)
1575 GstPad *pad = GST_PAD (aggpad);
1577 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1581 gst_aggregator_finalize (GObject * object)
1583 GstAggregator *self = (GstAggregator *) object;
1585 g_mutex_clear (&self->priv->src_lock);
1586 g_cond_clear (&self->priv->src_cond);
1588 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1592 * gst_aggregator_set_latency_property:
1593 * @agg: a #GstAggregator
1594 * @latency: the new latency value.
1596 * Sets the new latency value to @latency. This value is used to limit the
1597 * amount of time a pad waits for data to appear before considering the pad
1601 gst_aggregator_set_latency_property (GstAggregator * self, gint64 latency)
1604 GstClockTime min, max;
1606 g_return_if_fail (GST_IS_AGGREGATOR (self));
1607 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
1610 if (self->priv->peer_latency_live) {
1611 min = self->priv->peer_latency_min;
1612 max = self->priv->peer_latency_max;
1615 min += self->priv->sub_latency_min;
1616 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
1617 && GST_CLOCK_TIME_IS_VALID (max))
1618 max += self->priv->sub_latency_max;
1620 max = GST_CLOCK_TIME_NONE;
1622 if (GST_CLOCK_TIME_IS_VALID (max) && min > max) {
1623 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION,
1624 ("%s", "Latency too big"),
1625 ("The requested latency value is too big for the latency in the "
1626 "current pipeline. Limiting to %" G_GINT64_FORMAT, max));
1627 /* FIXME: This could in theory become negative, but in
1628 * that case all is lost anyway */
1629 latency -= min - max;
1630 /* FIXME: shouldn't we g_object_notify() the change here? */
1634 changed = (self->priv->latency != latency);
1635 self->priv->latency = latency;
1638 SRC_BROADCAST (self);
1642 gst_element_post_message (GST_ELEMENT_CAST (self),
1643 gst_message_new_latency (GST_OBJECT_CAST (self)));
1647 * gst_aggregator_get_latency_property:
1648 * @agg: a #GstAggregator
1650 * Gets the latency value. See gst_aggregator_set_latency for
1653 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
1654 * before a pad is deemed unresponsive. A value of -1 means an
1658 gst_aggregator_get_latency_property (GstAggregator * agg)
1662 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), -1);
1664 GST_OBJECT_LOCK (agg);
1665 res = agg->priv->latency;
1666 GST_OBJECT_UNLOCK (agg);
1672 gst_aggregator_set_property (GObject * object, guint prop_id,
1673 const GValue * value, GParamSpec * pspec)
1675 GstAggregator *agg = GST_AGGREGATOR (object);
1679 gst_aggregator_set_latency_property (agg, g_value_get_int64 (value));
1682 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1688 gst_aggregator_get_property (GObject * object, guint prop_id,
1689 GValue * value, GParamSpec * pspec)
1691 GstAggregator *agg = GST_AGGREGATOR (object);
1695 g_value_set_int64 (value, gst_aggregator_get_latency_property (agg));
1698 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1703 /* GObject vmethods implementations */
1705 gst_aggregator_class_init (GstAggregatorClass * klass)
1707 GObjectClass *gobject_class = (GObjectClass *) klass;
1708 GstElementClass *gstelement_class = (GstElementClass *) klass;
1710 aggregator_parent_class = g_type_class_peek_parent (klass);
1711 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1713 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1714 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1716 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1718 klass->sink_event = gst_aggregator_default_sink_event;
1719 klass->sink_query = gst_aggregator_default_sink_query;
1721 klass->src_event = gst_aggregator_default_src_event;
1722 klass->src_query = gst_aggregator_default_src_query;
1724 gstelement_class->request_new_pad =
1725 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
1726 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
1727 gstelement_class->release_pad =
1728 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
1729 gstelement_class->change_state =
1730 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
1732 gobject_class->set_property = gst_aggregator_set_property;
1733 gobject_class->get_property = gst_aggregator_get_property;
1734 gobject_class->finalize = gst_aggregator_finalize;
1736 g_object_class_install_property (gobject_class, PROP_LATENCY,
1737 g_param_spec_int64 ("latency", "Buffer latency",
1738 "Additional latency in live mode to allow upstream "
1739 "to take longer to produce buffers for the current "
1741 (G_MAXLONG == G_MAXINT64) ? G_MAXINT64 : (G_MAXLONG * GST_SECOND - 1),
1742 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1744 GST_DEBUG_REGISTER_FUNCPTR (gst_aggregator_stop_pad);
1748 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1750 GstPadTemplate *pad_template;
1751 GstAggregatorPrivate *priv;
1753 g_return_if_fail (klass->aggregate != NULL);
1756 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1757 GstAggregatorPrivate);
1762 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1763 g_return_if_fail (pad_template != NULL);
1765 priv->padcount = -1;
1766 priv->tags_changed = FALSE;
1768 self->priv->peer_latency_live = FALSE;
1769 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
1770 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
1771 self->priv->has_peer_latency = FALSE;
1772 gst_aggregator_reset_flow_values (self);
1774 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1776 gst_pad_set_event_function (self->srcpad,
1777 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
1778 gst_pad_set_query_function (self->srcpad,
1779 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
1780 gst_pad_set_activatemode_function (self->srcpad,
1781 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
1783 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1785 self->priv->latency = DEFAULT_LATENCY;
1787 g_mutex_init (&self->priv->src_lock);
1788 g_cond_init (&self->priv->src_cond);
1791 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1792 * method to get to the padtemplates */
1794 gst_aggregator_get_type (void)
1796 static volatile gsize type = 0;
1798 if (g_once_init_enter (&type)) {
1800 static const GTypeInfo info = {
1801 sizeof (GstAggregatorClass),
1804 (GClassInitFunc) gst_aggregator_class_init,
1807 sizeof (GstAggregator),
1809 (GInstanceInitFunc) gst_aggregator_init,
1812 _type = g_type_register_static (GST_TYPE_ELEMENT,
1813 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1814 g_once_init_leave (&type, _type);
1819 static GstFlowReturn
1820 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1822 GstBuffer *actual_buf = buffer;
1823 GstAggregator *self = GST_AGGREGATOR (object);
1824 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1825 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1826 GstFlowReturn flow_return;
1828 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1830 PAD_FLUSH_LOCK (aggpad);
1833 flow_return = aggpad->priv->flow_return;
1834 if (flow_return != GST_FLOW_OK)
1837 if (aggpad->priv->pending_eos == TRUE)
1840 while (aggpad->priv->buffer && aggpad->priv->flow_return == GST_FLOW_OK)
1841 PAD_WAIT_EVENT (aggpad);
1843 flow_return = aggpad->priv->flow_return;
1844 if (flow_return != GST_FLOW_OK)
1847 PAD_UNLOCK (aggpad);
1849 if (aggclass->clip) {
1850 aggclass->clip (self, aggpad, buffer, &actual_buf);
1855 if (aggpad->priv->buffer)
1856 gst_buffer_unref (aggpad->priv->buffer);
1857 aggpad->priv->buffer = actual_buf;
1859 flow_return = aggpad->priv->flow_return;
1861 PAD_UNLOCK (aggpad);
1862 PAD_FLUSH_UNLOCK (aggpad);
1864 SRC_BROADCAST (self);
1867 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1872 PAD_UNLOCK (aggpad);
1873 PAD_FLUSH_UNLOCK (aggpad);
1875 gst_buffer_unref (buffer);
1876 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
1877 gst_flow_get_name (flow_return));
1882 PAD_UNLOCK (aggpad);
1883 PAD_FLUSH_UNLOCK (aggpad);
1885 gst_buffer_unref (buffer);
1886 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1888 return GST_FLOW_EOS;
1892 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
1895 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1896 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1898 if (GST_QUERY_IS_SERIALIZED (query)) {
1901 while (aggpad->priv->buffer && aggpad->priv->flow_return == GST_FLOW_OK)
1902 PAD_WAIT_EVENT (aggpad);
1904 if (aggpad->priv->flow_return != GST_FLOW_OK)
1907 PAD_UNLOCK (aggpad);
1910 return klass->sink_query (GST_AGGREGATOR (parent),
1911 GST_AGGREGATOR_PAD (pad), query);
1914 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
1915 gst_flow_get_name (aggpad->priv->flow_return));
1916 PAD_UNLOCK (aggpad);
1921 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
1924 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1925 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1927 if (GST_EVENT_IS_SERIALIZED (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS
1928 && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT_DONE) {
1932 while (aggpad->priv->buffer && aggpad->priv->flow_return == GST_FLOW_OK)
1933 PAD_WAIT_EVENT (aggpad);
1935 if (aggpad->priv->flow_return != GST_FLOW_OK
1936 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP)
1939 PAD_UNLOCK (aggpad);
1942 return klass->sink_event (GST_AGGREGATOR (parent),
1943 GST_AGGREGATOR_PAD (pad), event);
1946 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
1947 gst_flow_get_name (aggpad->priv->flow_return));
1948 PAD_UNLOCK (aggpad);
1949 if (GST_EVENT_IS_STICKY (event))
1950 gst_pad_store_sticky_event (pad, event);
1951 gst_event_unref (event);
1956 gst_aggregator_pad_activate_mode_func (GstPad * pad,
1957 GstObject * parent, GstPadMode mode, gboolean active)
1959 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1961 if (active == FALSE) {
1962 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING);
1965 aggpad->priv->flow_return = GST_FLOW_OK;
1966 PAD_BROADCAST_EVENT (aggpad);
1967 PAD_UNLOCK (aggpad);
1973 /***********************************
1974 * GstAggregatorPad implementation *
1975 ************************************/
1976 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1979 gst_aggregator_pad_constructed (GObject * object)
1981 GstPad *pad = GST_PAD (object);
1983 gst_pad_set_chain_function (pad,
1984 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
1985 gst_pad_set_event_function (pad,
1986 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func));
1987 gst_pad_set_query_function (pad,
1988 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
1989 gst_pad_set_activatemode_function (pad,
1990 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
1994 gst_aggregator_pad_finalize (GObject * object)
1996 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1998 g_cond_clear (&pad->priv->event_cond);
1999 g_mutex_clear (&pad->priv->flush_lock);
2000 g_mutex_clear (&pad->priv->lock);
2002 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
2006 gst_aggregator_pad_dispose (GObject * object)
2008 GstAggregatorPad *pad = (GstAggregatorPad *) object;
2010 gst_aggregator_pad_drop_buffer (pad);
2012 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
2016 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
2018 GObjectClass *gobject_class = (GObjectClass *) klass;
2020 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
2022 gobject_class->constructed = gst_aggregator_pad_constructed;
2023 gobject_class->finalize = gst_aggregator_pad_finalize;
2024 gobject_class->dispose = gst_aggregator_pad_dispose;
2028 gst_aggregator_pad_init (GstAggregatorPad * pad)
2031 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
2032 GstAggregatorPadPrivate);
2034 pad->priv->buffer = NULL;
2035 g_cond_init (&pad->priv->event_cond);
2037 g_mutex_init (&pad->priv->flush_lock);
2038 g_mutex_init (&pad->priv->lock);
2042 * gst_aggregator_pad_steal_buffer:
2043 * @pad: the pad to get buffer from
2045 * Steal the ref to the buffer currently queued in @pad.
2047 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
2048 * queued. You should unref the buffer after usage.
2051 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
2053 GstBuffer *buffer = NULL;
2056 if (pad->priv->buffer) {
2057 GST_TRACE_OBJECT (pad, "Consuming buffer");
2058 buffer = pad->priv->buffer;
2059 pad->priv->buffer = NULL;
2060 if (pad->priv->pending_eos) {
2061 pad->priv->pending_eos = FALSE;
2062 pad->priv->eos = TRUE;
2064 PAD_BROADCAST_EVENT (pad);
2065 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
2073 * gst_aggregator_pad_drop_buffer:
2074 * @pad: the pad where to drop any pending buffer
2076 * Drop the buffer currently queued in @pad.
2078 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
2081 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
2085 buf = gst_aggregator_pad_steal_buffer (pad);
2090 gst_buffer_unref (buf);
2095 * gst_aggregator_pad_get_buffer:
2096 * @pad: the pad to get buffer from
2098 * Returns: (transfer full): A reference to the buffer in @pad or
2099 * NULL if no buffer was queued. You should unref the buffer after
2103 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
2105 GstBuffer *buffer = NULL;
2108 if (pad->priv->buffer)
2109 buffer = gst_buffer_ref (pad->priv->buffer);
2116 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
2121 is_eos = pad->priv->eos;
2128 * gst_aggregator_merge_tags:
2129 * @self: a #GstAggregator
2130 * @tags: a #GstTagList to merge
2131 * @mode: the #GstTagMergeMode to use
2133 * Adds tags to so-called pending tags, which will be processed
2134 * before pushing out data downstream.
2136 * Note that this is provided for convenience, and the subclass is
2137 * not required to use this and can still do tag handling on its own.
2142 gst_aggregator_merge_tags (GstAggregator * self,
2143 const GstTagList * tags, GstTagMergeMode mode)
2147 g_return_if_fail (GST_IS_AGGREGATOR (self));
2148 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2150 /* FIXME Check if we can use OBJECT lock here! */
2151 GST_OBJECT_LOCK (self);
2153 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
2154 otags = self->priv->tags;
2155 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
2157 gst_tag_list_unref (otags);
2158 self->priv->tags_changed = TRUE;
2159 GST_OBJECT_UNLOCK (self);
2163 * gst_aggregator_set_latency:
2164 * @self: a #GstAggregator
2165 * @min_latency: minimum latency
2166 * @max_latency: maximum latency
2168 * Lets #GstAggregator sub-classes tell the baseclass what their internal
2169 * latency is. Will also post a LATENCY message on the bus so the pipeline
2170 * can reconfigure its global latency.
2173 gst_aggregator_set_latency (GstAggregator * self,
2174 GstClockTime min_latency, GstClockTime max_latency)
2176 gboolean changed = FALSE;
2178 g_return_if_fail (GST_IS_AGGREGATOR (self));
2179 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2180 g_return_if_fail (max_latency >= min_latency);
2183 if (self->priv->sub_latency_min != min_latency) {
2184 self->priv->sub_latency_min = min_latency;
2187 if (self->priv->sub_latency_max != max_latency) {
2188 self->priv->sub_latency_max = max_latency;
2193 SRC_BROADCAST (self);
2197 gst_element_post_message (GST_ELEMENT_CAST (self),
2198 gst_message_new_latency (GST_OBJECT_CAST (self)));