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. Implementers should at least implement
33 * the aggregate () vmethod.
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
64 #include <string.h> /* strlen */
66 #include "gstaggregator.h"
69 /* Might become API */
70 static void gst_aggregator_merge_tags (GstAggregator * aggregator,
71 const GstTagList * tags, GstTagMergeMode mode);
73 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
74 #define GST_CAT_DEFAULT aggregator_debug
76 /* GstAggregatorPad definitions */
77 #define PAD_LOCK_EVENT(pad) G_STMT_START { \
78 GST_LOG_OBJECT (pad, "Taking EVENT lock from thread %p", \
80 g_mutex_lock(&pad->priv->event_lock); \
81 GST_LOG_OBJECT (pad, "Took EVENT lock from thread %p", \
85 #define PAD_UNLOCK_EVENT(pad) G_STMT_START { \
86 GST_LOG_OBJECT (pad, "Releasing EVENT lock from thread %p", \
88 g_mutex_unlock(&pad->priv->event_lock); \
89 GST_LOG_OBJECT (pad, "Release EVENT lock from thread %p", \
94 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
95 GST_LOG_OBJECT (pad, "Waiting for EVENT on thread %p", \
97 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
98 &(pad->priv->event_lock)); \
99 GST_LOG_OBJECT (pad, "DONE Waiting for EVENT on thread %p", \
103 #define PAD_BROADCAST_EVENT(pad) { \
104 GST_LOG_OBJECT (pad, "Signaling EVENT from thread %p", \
106 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
109 #define GST_AGGREGATOR_SETCAPS_LOCK(self) G_STMT_START { \
110 GST_LOG_OBJECT (self, "Taking SETCAPS lock from thread %p", \
112 g_mutex_lock(&self->priv->setcaps_lock); \
113 GST_LOG_OBJECT (self, "Took SETCAPS lock from thread %p", \
117 #define GST_AGGREGATOR_SETCAPS_UNLOCK(self) G_STMT_START { \
118 GST_LOG_OBJECT (self, "Releasing SETCAPS lock from thread %p", \
120 g_mutex_unlock(&self->priv->setcaps_lock); \
121 GST_LOG_OBJECT (self, "Took SETCAPS lock from thread %p", \
125 #define PAD_STREAM_LOCK(pad) G_STMT_START { \
126 GST_LOG_OBJECT (pad, "Taking lock from thread %p", \
128 g_mutex_lock(&pad->priv->stream_lock); \
129 GST_LOG_OBJECT (pad, "Took lock from thread %p", \
133 #define PAD_STREAM_UNLOCK(pad) G_STMT_START { \
134 GST_LOG_OBJECT (pad, "Releasing lock from thread %p", \
136 g_mutex_unlock(&pad->priv->stream_lock); \
137 GST_LOG_OBJECT (pad, "Release lock from thread %p", \
141 struct _GstAggregatorPadPrivate
143 gboolean pending_flush_start;
144 gboolean pending_flush_stop;
145 gboolean pending_eos;
155 _aggpad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
157 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
160 aggpad->priv->flushing = FALSE;
163 return klass->flush (aggpad, agg);
168 /*************************************
169 * GstAggregator implementation *
170 *************************************/
171 static GstElementClass *aggregator_parent_class = NULL;
173 #define MAIN_CONTEXT_LOCK(self) G_STMT_START { \
174 GST_LOG_OBJECT (self, "Getting MAIN_CONTEXT_LOCK in thread %p", \
176 g_mutex_lock(&((GstAggregator*)self)->priv->mcontext_lock); \
177 GST_LOG_OBJECT (self, "Got MAIN_CONTEXT_LOCK in thread %p", \
181 #define MAIN_CONTEXT_UNLOCK(self) G_STMT_START { \
182 g_mutex_unlock(&((GstAggregator*)self)->priv->mcontext_lock); \
183 GST_LOG_OBJECT (self, "Unlocked MAIN_CONTEXT_LOCK in thread %p", \
187 struct _GstAggregatorPrivate
191 GMainContext *mcontext;
193 /* Our state is >= PAUSED */
196 /* Ensure that when we remove all sources from the maincontext
197 * we can not add any source, avoiding:
198 * "g_source_attach: assertion '!SOURCE_DESTROYED (source)' failed" */
199 GMutex mcontext_lock;
203 gboolean send_stream_start;
204 gboolean send_segment;
205 gboolean flush_seeking;
206 gboolean pending_flush_start;
208 GstFlowReturn flow_return;
213 gboolean tags_changed;
215 /* Lock to prevent two src setcaps from happening at the same time */
225 gboolean one_actually_seeked;
229 * gst_aggregator_iterate_sinkpads:
230 * @self: The #GstAggregator
231 * @func: The function to call.
232 * @user_data: The data to pass to @func.
234 * Iterate the sinkpads of aggregator to call a function on them.
236 * This method guarantees that @func will be called only once for each
240 gst_aggregator_iterate_sinkpads (GstAggregator * self,
241 GstAggregatorPadForeachFunc func, gpointer user_data)
243 gboolean result = FALSE;
245 gboolean done = FALSE;
246 GValue item = { 0, };
247 GList *seen_pads = NULL;
249 iter = gst_element_iterate_sink_pads (GST_ELEMENT (self));
255 switch (gst_iterator_next (iter, &item)) {
256 case GST_ITERATOR_OK:
260 pad = g_value_get_object (&item);
262 /* if already pushed, skip. FIXME, find something faster to tag pads */
263 if (pad == NULL || g_list_find (seen_pads, pad)) {
264 g_value_reset (&item);
268 GST_LOG_OBJECT (self, "calling function on pad %s:%s",
269 GST_DEBUG_PAD_NAME (pad));
270 result = func (self, pad, user_data);
274 seen_pads = g_list_prepend (seen_pads, pad);
276 g_value_reset (&item);
279 case GST_ITERATOR_RESYNC:
280 gst_iterator_resync (iter);
282 case GST_ITERATOR_ERROR:
283 GST_ERROR_OBJECT (self,
284 "Could not iterate over internally linked pads");
287 case GST_ITERATOR_DONE:
292 g_value_unset (&item);
293 gst_iterator_free (iter);
295 if (seen_pads == NULL) {
296 GST_DEBUG_OBJECT (self, "No pad seen");
300 g_list_free (seen_pads);
306 static inline gboolean
307 _check_all_pads_with_data_or_eos (GstAggregator * self,
308 GstAggregatorPad * aggpad)
310 if (aggpad->buffer || aggpad->eos) {
314 GST_LOG_OBJECT (aggpad, "Not ready to be aggregated");
320 _reset_flow_values (GstAggregator * self)
322 self->priv->flow_return = GST_FLOW_FLUSHING;
323 self->priv->send_stream_start = TRUE;
324 self->priv->send_segment = TRUE;
325 gst_segment_init (&self->segment, GST_FORMAT_TIME);
329 _push_mandatory_events (GstAggregator * self)
331 GstAggregatorPrivate *priv = self->priv;
333 if (g_atomic_int_get (&self->priv->send_stream_start)) {
336 GST_INFO_OBJECT (self, "pushing stream start");
337 /* stream-start (FIXME: create id based on input ids) */
338 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
339 if (!gst_pad_push_event (self->srcpad, gst_event_new_stream_start (s_id))) {
340 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
342 g_atomic_int_set (&self->priv->send_stream_start, FALSE);
345 if (self->priv->srccaps) {
347 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
348 self->priv->srccaps);
349 if (!gst_pad_push_event (self->srcpad,
350 gst_event_new_caps (self->priv->srccaps))) {
351 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
353 gst_caps_unref (self->priv->srccaps);
354 self->priv->srccaps = NULL;
357 if (g_atomic_int_get (&self->priv->send_segment)) {
358 if (!g_atomic_int_get (&self->priv->flush_seeking)) {
359 GstEvent *segev = gst_event_new_segment (&self->segment);
361 if (!self->priv->seqnum)
362 self->priv->seqnum = gst_event_get_seqnum (segev);
364 gst_event_set_seqnum (segev, self->priv->seqnum);
366 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segev);
367 gst_pad_push_event (self->srcpad, segev);
368 g_atomic_int_set (&self->priv->send_segment, FALSE);
372 if (priv->tags && priv->tags_changed) {
373 gst_pad_push_event (self->srcpad,
374 gst_event_new_tag (gst_tag_list_ref (priv->tags)));
375 priv->tags_changed = FALSE;
380 * gst_aggregator_set_src_caps:
381 * @self: The #GstAggregator
382 * @caps: The #GstCaps to set on the src pad.
384 * Sets the caps to be used on the src pad.
387 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
389 GST_AGGREGATOR_SETCAPS_LOCK (self);
390 gst_caps_replace (&self->priv->srccaps, caps);
391 _push_mandatory_events (self);
392 GST_AGGREGATOR_SETCAPS_UNLOCK (self);
396 * gst_aggregator_finish_buffer:
397 * @self: The #GstAggregator
398 * @buffer: the #GstBuffer to push.
400 * This method will take care of sending mandatory events before pushing
401 * the provided buffer.
404 gst_aggregator_finish_buffer (GstAggregator * self, GstBuffer * buffer)
406 _push_mandatory_events (self);
408 if (!g_atomic_int_get (&self->priv->flush_seeking) &&
409 gst_pad_is_active (self->srcpad)) {
410 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
411 return gst_pad_push (self->srcpad, buffer);
413 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
414 g_atomic_int_get (&self->priv->flush_seeking),
415 gst_pad_is_active (self->srcpad));
416 gst_buffer_unref (buffer);
422 _push_eos (GstAggregator * self)
425 _push_mandatory_events (self);
427 self->priv->send_eos = FALSE;
428 event = gst_event_new_eos ();
429 gst_event_set_seqnum (event, self->priv->seqnum);
430 gst_pad_push_event (self->srcpad, event);
435 _destroy_gsource (GSource * source)
437 g_source_destroy (source);
438 g_source_unref (source);
442 _remove_all_sources (GstAggregator * self)
444 GstAggregatorPrivate *priv = self->priv;
446 MAIN_CONTEXT_LOCK (self);
447 g_list_free_full (priv->gsources, (GDestroyNotify) _destroy_gsource);
448 priv->gsources = NULL;
449 MAIN_CONTEXT_UNLOCK (self);
453 aggregate_func (GstAggregator * self)
455 GstAggregatorPrivate *priv = self->priv;
456 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
458 GST_LOG_OBJECT (self, "Checking aggregate");
459 while (priv->send_eos && gst_aggregator_iterate_sinkpads (self,
460 (GstAggregatorPadForeachFunc) _check_all_pads_with_data_or_eos,
461 NULL) && priv->running) {
462 GST_TRACE_OBJECT (self, "Actually aggregating!");
464 priv->flow_return = klass->aggregate (self);
466 if (priv->flow_return == GST_FLOW_EOS) {
467 g_main_context_wakeup (self->priv->mcontext);
468 _remove_all_sources (self);
472 if (priv->flow_return == GST_FLOW_FLUSHING &&
473 g_atomic_int_get (&priv->flush_seeking))
474 priv->flow_return = GST_FLOW_OK;
476 GST_LOG_OBJECT (self, "flow return is %s",
477 gst_flow_get_name (priv->flow_return));
479 if (priv->flow_return != GST_FLOW_OK)
483 return G_SOURCE_REMOVE;
487 iterate_main_context_func (GstAggregator * self)
489 if (self->priv->running == FALSE) {
490 GST_DEBUG_OBJECT (self, "Not running anymore");
495 g_main_context_iteration (self->priv->mcontext, TRUE);
499 _start (GstAggregator * self)
501 self->priv->running = TRUE;
502 self->priv->send_stream_start = TRUE;
503 self->priv->send_segment = TRUE;
504 self->priv->send_eos = TRUE;
505 self->priv->srccaps = NULL;
506 self->priv->flow_return = GST_FLOW_OK;
512 _check_pending_flush_stop (GstAggregatorPad * pad)
514 return (!pad->priv->pending_flush_stop && !pad->priv->pending_flush_start);
518 _stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
522 GST_INFO_OBJECT (self, "%s srcpad task",
523 flush_start ? "Pausing" : "Stopping");
525 self->priv->running = FALSE;
527 /* Clean the stack of GSource set on the MainContext */
528 g_main_context_wakeup (self->priv->mcontext);
529 _remove_all_sources (self);
531 res = gst_pad_push_event (self->srcpad, flush_start);
534 gst_pad_stop_task (self->srcpad);
540 _start_srcpad_task (GstAggregator * self)
542 GST_INFO_OBJECT (self, "Starting srcpad task");
544 self->priv->running = TRUE;
545 gst_pad_start_task (GST_PAD (self->srcpad),
546 (GstTaskFunction) iterate_main_context_func, self, NULL);
550 _add_aggregate_gsource (GstAggregator * self)
553 GstAggregatorPrivate *priv = self->priv;
555 MAIN_CONTEXT_LOCK (self);
556 source = g_idle_source_new ();
557 g_source_set_callback (source, (GSourceFunc) aggregate_func, self, NULL);
558 priv->gsources = g_list_prepend (priv->gsources, source);
559 g_source_attach (source, priv->mcontext);
560 MAIN_CONTEXT_UNLOCK (self);
564 _flush (GstAggregator * self)
566 GstFlowReturn ret = GST_FLOW_OK;
567 GstAggregatorPrivate *priv = self->priv;
568 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
570 GST_DEBUG_OBJECT (self, "Flushing everything");
571 g_atomic_int_set (&priv->send_segment, TRUE);
572 g_atomic_int_set (&priv->flush_seeking, FALSE);
573 g_atomic_int_set (&priv->tags_changed, FALSE);
575 ret = klass->flush (self);
581 _all_flush_stop_received (GstAggregator * self)
584 GstAggregatorPad *tmppad;
586 GST_OBJECT_LOCK (self);
587 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
588 tmppad = (GstAggregatorPad *) tmp->data;
590 if (_check_pending_flush_stop (tmppad) == FALSE) {
591 GST_DEBUG_OBJECT (tmppad, "Is not last %i -- %i",
592 tmppad->priv->pending_flush_start, tmppad->priv->pending_flush_stop);
593 GST_OBJECT_UNLOCK (self);
597 GST_OBJECT_UNLOCK (self);
603 _flush_start (GstAggregator * self, GstAggregatorPad * aggpad, GstEvent * event)
606 GstAggregatorPrivate *priv = self->priv;
607 GstAggregatorPadPrivate *padpriv = aggpad->priv;
609 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
610 /* Remove pad buffer and wake up the streaming thread */
611 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
612 gst_buffer_replace (&tmpbuf, NULL);
613 PAD_STREAM_LOCK (aggpad);
614 if (g_atomic_int_compare_and_exchange (&padpriv->pending_flush_start,
615 TRUE, FALSE) == TRUE) {
616 GST_DEBUG_OBJECT (aggpad, "Expecting FLUSH_STOP now");
617 g_atomic_int_set (&padpriv->pending_flush_stop, TRUE);
620 if (g_atomic_int_get (&priv->flush_seeking)) {
621 /* If flush_seeking we forward the first FLUSH_START */
622 if (g_atomic_int_compare_and_exchange (&priv->pending_flush_start,
623 TRUE, FALSE) == TRUE) {
625 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
626 _stop_srcpad_task (self, event);
627 priv->flow_return = GST_FLOW_OK;
629 GST_INFO_OBJECT (self, "Getting STREAM_LOCK while seeking");
630 GST_PAD_STREAM_LOCK (self->srcpad);
631 GST_LOG_OBJECT (self, "GOT STREAM_LOCK");
635 gst_event_unref (event);
637 PAD_STREAM_UNLOCK (aggpad);
639 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
640 gst_buffer_replace (&tmpbuf, NULL);
643 /* GstAggregator vmethods default implementations */
645 _sink_event (GstAggregator * self, GstAggregatorPad * aggpad, GstEvent * event)
648 GstPad *pad = GST_PAD (aggpad);
649 GstAggregatorPrivate *priv = self->priv;
651 switch (GST_EVENT_TYPE (event)) {
652 case GST_EVENT_FLUSH_START:
654 _flush_start (self, aggpad, event);
655 /* We forward only in one case: right after flush_seeking */
659 case GST_EVENT_FLUSH_STOP:
661 GST_DEBUG_OBJECT (aggpad, "Got FLUSH_STOP");
663 _aggpad_flush (aggpad, self);
664 if (g_atomic_int_get (&priv->flush_seeking)) {
665 g_atomic_int_set (&aggpad->priv->pending_flush_stop, FALSE);
667 if (g_atomic_int_get (&priv->flush_seeking)) {
668 if (_all_flush_stop_received (self)) {
669 /* That means we received FLUSH_STOP/FLUSH_STOP on
670 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
672 gst_pad_push_event (self->srcpad, event);
673 priv->send_eos = TRUE;
675 _add_aggregate_gsource (self);
677 GST_INFO_OBJECT (self, "Releasing source pad STREAM_LOCK");
678 GST_PAD_STREAM_UNLOCK (self->srcpad);
679 _start_srcpad_task (self);
684 /* We never forward the event */
689 GST_DEBUG_OBJECT (aggpad, "EOS");
691 /* We still have a buffer, and we don't want the subclass to have to
692 * check for it. Mark pending_eos, eos will be set when steal_buffer is
695 PAD_LOCK_EVENT (aggpad);
696 if (!aggpad->buffer) {
699 aggpad->priv->pending_eos = TRUE;
701 PAD_UNLOCK_EVENT (aggpad);
703 _add_aggregate_gsource (self);
706 case GST_EVENT_SEGMENT:
708 PAD_LOCK_EVENT (aggpad);
709 gst_event_copy_segment (event, &aggpad->segment);
710 PAD_UNLOCK_EVENT (aggpad);
713 case GST_EVENT_STREAM_START:
721 gst_event_parse_tag (event, &tags);
723 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
724 gst_aggregator_merge_tags (self, tags, GST_TAG_MERGE_REPLACE);
725 gst_event_unref (event);
737 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
738 return gst_pad_event_default (pad, GST_OBJECT (self), event);
741 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
743 gst_event_unref (event);
749 _flush_pad (GstAggregator * self, GstAggregatorPad * pad, gpointer unused_udata)
751 _aggpad_flush (pad, self);
757 _stop (GstAggregator * agg)
759 _reset_flow_values (agg);
761 gst_aggregator_iterate_sinkpads (agg,
762 (GstAggregatorPadForeachFunc) _flush_pad, NULL);
765 gst_tag_list_unref (agg->priv->tags);
766 agg->priv->tags = NULL;
771 /* GstElement vmethods implementations */
772 static GstStateChangeReturn
773 _change_state (GstElement * element, GstStateChange transition)
775 GstStateChangeReturn ret;
776 GstAggregator *self = GST_AGGREGATOR (element);
777 GstAggregatorClass *agg_class = GST_AGGREGATOR_GET_CLASS (self);
780 switch (transition) {
781 case GST_STATE_CHANGE_READY_TO_PAUSED:
782 agg_class->start (self);
789 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
790 transition)) == GST_STATE_CHANGE_FAILURE)
794 switch (transition) {
795 case GST_STATE_CHANGE_PAUSED_TO_READY:
796 agg_class->stop (self);
806 GST_ERROR_OBJECT (element, "parent failed state change");
812 _release_pad (GstElement * element, GstPad * pad)
816 GstAggregator *self = GST_AGGREGATOR (element);
817 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
819 GST_INFO_OBJECT (pad, "Removing pad");
821 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
822 tmpbuf = gst_aggregator_pad_steal_buffer (aggpad);
823 gst_buffer_replace (&tmpbuf, NULL);
824 gst_element_remove_pad (element, pad);
826 /* Something changed make sure we try to aggregate */
827 _add_aggregate_gsource (self);
831 _request_new_pad (GstElement * element,
832 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
835 GstAggregatorPad *agg_pad;
837 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
838 GstAggregatorPrivate *priv = GST_AGGREGATOR (element)->priv;
840 self = GST_AGGREGATOR (element);
842 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
846 GST_OBJECT_LOCK (element);
847 if (req_name == NULL || strlen (req_name) < 6
848 || !g_str_has_prefix (req_name, "sink_")) {
849 /* no name given when requesting the pad, use next available int */
852 /* parse serial number from requested padname */
853 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
854 if (serial >= priv->padcount)
855 priv->padcount = serial;
858 name = g_strdup_printf ("sink_%u", priv->padcount);
859 agg_pad = g_object_new (GST_AGGREGATOR_GET_CLASS (self)->sinkpads_type,
860 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
862 GST_OBJECT_UNLOCK (element);
868 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
871 gst_pad_set_active (GST_PAD (agg_pad), TRUE);
873 /* add the pad to the element */
874 gst_element_add_pad (element, GST_PAD (agg_pad));
876 return GST_PAD (agg_pad);
880 _send_event (GstElement * element, GstEvent * event)
882 GstAggregator *self = GST_AGGREGATOR (element);
884 GST_STATE_LOCK (element);
885 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
886 GST_STATE (element) < GST_STATE_PAUSED) {
890 GstSeekType start_type, stop_type;
893 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
894 &start, &stop_type, &stop);
895 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
896 stop_type, stop, NULL);
898 self->priv->seqnum = gst_event_get_seqnum (event);
899 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
901 GST_STATE_UNLOCK (element);
904 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
909 _src_query (GstAggregator * self, GstQuery * query)
913 switch (GST_QUERY_TYPE (query)) {
914 case GST_QUERY_SEEKING:
918 /* don't pass it along as some (file)sink might claim it does
919 * whereas with a collectpads in between that will not likely work */
920 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
921 gst_query_set_seeking (query, format, FALSE, 0, -1);
930 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
937 event_forward_func (GstPad * pad, EventData * evdata)
940 GstPad *peer = gst_pad_get_peer (pad);
941 GstAggregatorPadPrivate *padpriv = GST_AGGREGATOR_PAD (pad)->priv;
944 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
945 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
946 gst_object_unref (peer);
950 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK)
951 GST_ERROR_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
953 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
954 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
956 if (gst_pad_query (peer, seeking)) {
959 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
961 if (seekable == FALSE) {
962 GST_INFO_OBJECT (pad,
963 "Source not seekable, We failed but it does not matter!");
968 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
973 padpriv->pending_flush_start = FALSE;
974 padpriv->pending_flush_stop = FALSE;
977 evdata->one_actually_seeked = TRUE;
980 evdata->result &= ret;
982 /* Always send to all pads */
987 _set_flush_pending (GstAggregator * self, GstAggregatorPad * pad,
990 pad->priv->pending_flush_start = TRUE;
991 pad->priv->pending_flush_stop = FALSE;
997 _forward_event_to_all_sinkpads (GstAggregator * self, GstEvent * event,
1002 evdata.event = event;
1003 evdata.result = TRUE;
1004 evdata.flush = flush;
1005 evdata.one_actually_seeked = FALSE;
1007 /* We first need to set all pads as flushing in a first pass
1008 * as flush_start flush_stop is sometimes sent synchronously
1009 * while we send the seek event */
1011 gst_aggregator_iterate_sinkpads (self,
1012 (GstAggregatorPadForeachFunc) _set_flush_pending, NULL);
1013 gst_pad_forward (self->srcpad, (GstPadForwardFunction) event_forward_func,
1016 gst_event_unref (event);
1022 _do_seek (GstAggregator * self, GstEvent * event)
1027 GstSeekType start_type, stop_type;
1031 GstAggregatorPrivate *priv = self->priv;
1033 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1034 &start, &stop_type, &stop);
1036 GST_INFO_OBJECT (self, "starting SEEK");
1038 flush = flags & GST_SEEK_FLAG_FLUSH;
1041 g_atomic_int_set (&priv->pending_flush_start, TRUE);
1042 g_atomic_int_set (&priv->flush_seeking, TRUE);
1045 gst_segment_do_seek (&self->segment, rate, fmt, flags, start_type, start,
1046 stop_type, stop, NULL);
1048 /* forward the seek upstream */
1049 evdata = _forward_event_to_all_sinkpads (self, event, flush);
1052 if (!evdata.result || !evdata.one_actually_seeked) {
1053 g_atomic_int_set (&priv->flush_seeking, FALSE);
1054 g_atomic_int_set (&priv->pending_flush_start, FALSE);
1057 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
1059 return evdata.result;
1063 _src_event (GstAggregator * self, GstEvent * event)
1066 gboolean res = TRUE;
1068 switch (GST_EVENT_TYPE (event)) {
1069 case GST_EVENT_SEEK:
1071 gint old_seqnum = self->priv->seqnum;
1072 self->priv->seqnum = gst_event_get_seqnum (event);
1073 gst_event_ref (event);
1074 res = _do_seek (self, event);
1076 self->priv->seqnum = old_seqnum;
1077 gst_event_unref (event);
1081 case GST_EVENT_NAVIGATION:
1083 /* navigation is rather pointless. */
1085 gst_event_unref (event);
1094 evdata = _forward_event_to_all_sinkpads (self, event, FALSE);
1095 res = evdata.result;
1102 src_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
1104 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1106 return klass->src_event (GST_AGGREGATOR (parent), event);
1110 src_query_func (GstPad * pad, GstObject * parent, GstQuery * query)
1112 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1114 return klass->src_query (GST_AGGREGATOR (parent), query);
1118 src_activate_mode (GstPad * pad,
1119 GstObject * parent, GstPadMode mode, gboolean active)
1121 GstAggregator *self = GST_AGGREGATOR (parent);
1122 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1124 if (klass->src_activate) {
1125 if (klass->src_activate (self, mode, active) == FALSE) {
1130 if (active == TRUE) {
1132 case GST_PAD_MODE_PUSH:
1134 GST_INFO_OBJECT (pad, "Activating pad!");
1135 _start_srcpad_task (self);
1140 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
1147 GST_INFO_OBJECT (self, "Deactivating srcpad");
1148 _stop_srcpad_task (self, FALSE);
1154 _sink_query (GstAggregator * self, GstAggregatorPad * aggpad, GstQuery * query)
1156 GstPad *pad = GST_PAD (aggpad);
1158 return gst_pad_query_default (pad, GST_OBJECT (self), query);
1162 gst_aggregator_finalize (GObject * object)
1164 GstAggregator *self = (GstAggregator *) object;
1166 g_mutex_clear (&self->priv->mcontext_lock);
1167 g_mutex_clear (&self->priv->setcaps_lock);
1169 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
1173 gst_aggregator_dispose (GObject * object)
1175 GstAggregator *self = (GstAggregator *) object;
1177 G_OBJECT_CLASS (aggregator_parent_class)->dispose (object);
1179 g_main_context_unref (self->priv->mcontext);
1180 _remove_all_sources (self);
1183 /* GObject vmethods implementations */
1185 gst_aggregator_class_init (GstAggregatorClass * klass)
1187 GObjectClass *gobject_class = (GObjectClass *) klass;
1188 GstElementClass *gstelement_class = (GstElementClass *) klass;
1190 aggregator_parent_class = g_type_class_peek_parent (klass);
1191 g_type_class_add_private (klass, sizeof (GstAggregatorPrivate));
1193 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
1194 GST_DEBUG_FG_MAGENTA, "GstAggregator");
1196 klass->sinkpads_type = GST_TYPE_AGGREGATOR_PAD;
1197 klass->start = _start;
1198 klass->stop = _stop;
1200 klass->sink_event = _sink_event;
1201 klass->sink_query = _sink_query;
1203 klass->src_event = _src_event;
1204 klass->src_query = _src_query;
1206 gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR (_request_new_pad);
1207 gstelement_class->send_event = GST_DEBUG_FUNCPTR (_send_event);
1208 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (_release_pad);
1209 gstelement_class->change_state = GST_DEBUG_FUNCPTR (_change_state);
1211 gobject_class->finalize = gst_aggregator_finalize;
1212 gobject_class->dispose = gst_aggregator_dispose;
1216 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
1218 GstPadTemplate *pad_template;
1219 GstAggregatorPrivate *priv;
1221 g_return_if_fail (klass->aggregate != NULL);
1224 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_AGGREGATOR,
1225 GstAggregatorPrivate);
1230 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
1231 g_return_if_fail (pad_template != NULL);
1233 priv->padcount = -1;
1234 priv->tags_changed = FALSE;
1235 _reset_flow_values (self);
1237 priv->mcontext = g_main_context_new ();
1238 self->srcpad = gst_pad_new_from_template (pad_template, "src");
1240 gst_pad_set_event_function (self->srcpad,
1241 GST_DEBUG_FUNCPTR ((GstPadEventFunction) src_event_func));
1242 gst_pad_set_query_function (self->srcpad,
1243 GST_DEBUG_FUNCPTR ((GstPadQueryFunction) src_query_func));
1244 gst_pad_set_activatemode_function (self->srcpad,
1245 GST_DEBUG_FUNCPTR ((GstPadActivateModeFunction) src_activate_mode));
1247 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1249 g_mutex_init (&self->priv->mcontext_lock);
1250 g_mutex_init (&self->priv->setcaps_lock);
1253 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
1254 * method to get to the padtemplates */
1256 gst_aggregator_get_type (void)
1258 static volatile gsize type = 0;
1260 if (g_once_init_enter (&type)) {
1262 static const GTypeInfo info = {
1263 sizeof (GstAggregatorClass),
1266 (GClassInitFunc) gst_aggregator_class_init,
1269 sizeof (GstAggregator),
1271 (GInstanceInitFunc) gst_aggregator_init,
1274 _type = g_type_register_static (GST_TYPE_ELEMENT,
1275 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
1276 g_once_init_leave (&type, _type);
1281 static GstFlowReturn
1282 _chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
1284 GstBuffer *actual_buf = buffer;
1285 GstAggregator *self = GST_AGGREGATOR (object);
1286 GstAggregatorPrivate *priv = self->priv;
1287 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1288 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (object);
1290 GST_DEBUG_OBJECT (aggpad, "Start chaining a buffer %" GST_PTR_FORMAT, buffer);
1292 PAD_STREAM_LOCK (aggpad);
1293 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1296 if (g_atomic_int_get (&aggpad->priv->pending_eos) == TRUE)
1299 PAD_LOCK_EVENT (aggpad);
1300 if (aggpad->buffer) {
1301 GST_DEBUG_OBJECT (aggpad, "Waiting for buffer to be consumed");
1302 PAD_WAIT_EVENT (aggpad);
1304 PAD_UNLOCK_EVENT (aggpad);
1306 if (g_atomic_int_get (&aggpad->priv->flushing) == TRUE)
1309 if (aggclass->clip) {
1310 aggclass->clip (self, aggpad, buffer, &actual_buf);
1313 PAD_LOCK_EVENT (aggpad);
1315 gst_buffer_unref (aggpad->buffer);
1316 aggpad->buffer = actual_buf;
1317 PAD_UNLOCK_EVENT (aggpad);
1318 PAD_STREAM_UNLOCK (aggpad);
1320 _add_aggregate_gsource (self);
1322 GST_DEBUG_OBJECT (aggpad, "Done chaining");
1324 return priv->flow_return;
1327 PAD_STREAM_UNLOCK (aggpad);
1329 gst_buffer_unref (buffer);
1330 GST_DEBUG_OBJECT (aggpad, "We are flushing");
1332 return GST_FLOW_FLUSHING;
1335 PAD_STREAM_UNLOCK (aggpad);
1337 gst_buffer_unref (buffer);
1338 GST_DEBUG_OBJECT (pad, "We are EOS already...");
1340 return GST_FLOW_EOS;
1344 pad_query_func (GstPad * pad, GstObject * parent, GstQuery * query)
1346 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1348 return klass->sink_query (GST_AGGREGATOR (parent),
1349 GST_AGGREGATOR_PAD (pad), query);
1353 pad_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
1355 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
1357 return klass->sink_event (GST_AGGREGATOR (parent),
1358 GST_AGGREGATOR_PAD (pad), event);
1362 pad_activate_mode_func (GstPad * pad,
1363 GstObject * parent, GstPadMode mode, gboolean active)
1365 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1367 if (active == FALSE) {
1368 PAD_LOCK_EVENT (aggpad);
1369 g_atomic_int_set (&aggpad->priv->flushing, TRUE);
1370 gst_buffer_replace (&aggpad->buffer, NULL);
1371 PAD_BROADCAST_EVENT (aggpad);
1372 PAD_UNLOCK_EVENT (aggpad);
1374 g_atomic_int_set (&aggpad->priv->flushing, FALSE);
1375 PAD_LOCK_EVENT (aggpad);
1376 PAD_BROADCAST_EVENT (aggpad);
1377 PAD_UNLOCK_EVENT (aggpad);
1383 /***********************************
1384 * GstAggregatorPad implementation *
1385 ************************************/
1386 static GstPadClass *aggregator_pad_parent_class = NULL;
1387 G_DEFINE_TYPE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
1390 _pad_constructed (GObject * object)
1392 GstPad *pad = GST_PAD (object);
1394 gst_pad_set_chain_function (pad,
1395 GST_DEBUG_FUNCPTR ((GstPadChainFunction) _chain));
1396 gst_pad_set_event_function (pad,
1397 GST_DEBUG_FUNCPTR ((GstPadEventFunction) pad_event_func));
1398 gst_pad_set_query_function (pad,
1399 GST_DEBUG_FUNCPTR ((GstPadQueryFunction) pad_query_func));
1400 gst_pad_set_activatemode_function (pad,
1401 GST_DEBUG_FUNCPTR ((GstPadActivateModeFunction) pad_activate_mode_func));
1405 gst_aggregator_pad_finalize (GObject * object)
1407 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1409 g_mutex_clear (&pad->priv->event_lock);
1410 g_cond_clear (&pad->priv->event_cond);
1411 g_mutex_clear (&pad->priv->stream_lock);
1413 G_OBJECT_CLASS (aggregator_pad_parent_class)->finalize (object);
1417 gst_aggregator_pad_dispose (GObject * object)
1419 GstAggregatorPad *pad = (GstAggregatorPad *) object;
1422 buf = gst_aggregator_pad_steal_buffer (pad);
1424 gst_buffer_unref (buf);
1426 G_OBJECT_CLASS (aggregator_pad_parent_class)->dispose (object);
1430 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
1432 GObjectClass *gobject_class = (GObjectClass *) klass;
1434 aggregator_pad_parent_class = g_type_class_peek_parent (klass);
1435 g_type_class_add_private (klass, sizeof (GstAggregatorPadPrivate));
1437 gobject_class->constructed = GST_DEBUG_FUNCPTR (_pad_constructed);
1438 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_aggregator_pad_finalize);
1439 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_aggregator_pad_dispose);
1443 gst_aggregator_pad_init (GstAggregatorPad * pad)
1446 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AGGREGATOR_PAD,
1447 GstAggregatorPadPrivate);
1450 g_mutex_init (&pad->priv->event_lock);
1451 g_cond_init (&pad->priv->event_cond);
1453 g_mutex_init (&pad->priv->stream_lock);
1457 * gst_aggregator_pad_steal_buffer:
1458 * @pad: the pad to get buffer from
1460 * Steal the ref to the buffer currently queued in @pad.
1462 * Returns: (transfer full): The buffer in @pad or NULL if no buffer was
1463 * queued. You should unref the buffer after usage.
1466 gst_aggregator_pad_steal_buffer (GstAggregatorPad * pad)
1468 GstBuffer *buffer = NULL;
1470 PAD_LOCK_EVENT (pad);
1472 GST_TRACE_OBJECT (pad, "Consuming buffer");
1473 buffer = pad->buffer;
1475 if (pad->priv->pending_eos) {
1476 pad->priv->pending_eos = FALSE;
1479 PAD_BROADCAST_EVENT (pad);
1480 GST_DEBUG_OBJECT (pad, "Consummed: %" GST_PTR_FORMAT, buffer);
1482 PAD_UNLOCK_EVENT (pad);
1488 * gst_aggregator_pad_get_buffer:
1489 * @pad: the pad to get buffer from
1491 * Returns: (transfer full): A reference to the buffer in @pad or
1492 * NULL if no buffer was queued. You should unref the buffer after
1496 gst_aggregator_pad_get_buffer (GstAggregatorPad * pad)
1498 GstBuffer *buffer = NULL;
1500 PAD_LOCK_EVENT (pad);
1502 buffer = gst_buffer_ref (pad->buffer);
1503 PAD_UNLOCK_EVENT (pad);
1509 * gst_aggregator_merge_tags:
1510 * @self: a #GstAggregator
1511 * @tags: a #GstTagList to merge
1512 * @mode: the #GstTagMergeMode to use
1514 * Adds tags to so-called pending tags, which will be processed
1515 * before pushing out data downstream.
1517 * Note that this is provided for convenience, and the subclass is
1518 * not required to use this and can still do tag handling on its own.
1523 gst_aggregator_merge_tags (GstAggregator * self,
1524 const GstTagList * tags, GstTagMergeMode mode)
1528 g_return_if_fail (GST_IS_AGGREGATOR (self));
1529 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
1531 /* FIXME Check if we can use OBJECT lock here! */
1532 GST_OBJECT_LOCK (self);
1534 GST_DEBUG_OBJECT (self, "merging tags %" GST_PTR_FORMAT, tags);
1535 otags = self->priv->tags;
1536 self->priv->tags = gst_tag_list_merge (self->priv->tags, tags, mode);
1538 gst_tag_list_unref (otags);
1539 self->priv->tags_changed = TRUE;
1540 GST_OBJECT_UNLOCK (self);