2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2001 Thomas <thomas@apestaart.org>
4 * 2005,2006 Wim Taymans <wim@fluendo.com>
5 * 2013 Sebastian Dröge <sebastian@centricular.com>
7 * Olivier Crete <olivier.crete@collabora.com>
9 * gstaudioaggregator.c:
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
27 * SECTION: gstaudioaggregator
28 * @title: GstAudioAggregator
29 * @short_description: Base class that manages a set of audio input pads
30 * with the purpose of aggregating or mixing their raw audio input buffers
31 * @see_also: #GstAggregator, #GstAudioMixer
33 * Subclasses must use (a subclass of) #GstAudioAggregatorPad for both
34 * their source and sink pads,
35 * gst_element_class_add_static_pad_template_with_gtype() is a convenient
38 * #GstAudioAggregator can perform conversion on the data arriving
39 * on its sink pads, based on the format expected downstream: in order
40 * to enable that behaviour, the GType of the sink pads must either be
41 * a (subclass of) #GstAudioAggregatorConvertPad to use the default
42 * #GstAudioConverter implementation, or a subclass of #GstAudioAggregatorPad
43 * implementing #GstAudioAggregatorPad.convert_buffer.
45 * To allow for the output caps to change, the mechanism is the same as
46 * above, with the GType of the source pad.
48 * See #GstAudioMixer for an example.
50 * When conversion is enabled, #GstAudioAggregator will accept
51 * any type of raw audio caps and perform conversion
52 * on the data arriving on its sink pads, with whatever downstream
53 * expects as the target format.
55 * In case downstream caps are not fully fixated, it will use
56 * the first configured sink pad to finish fixating its source pad
59 * A notable exception for now is the sample rate, sink pads must
60 * have the same sample rate as either the downstream requirement,
61 * or the first configured pad, or a combination of both (when
62 * downstream specifies a range or a set of acceptable rates).
70 #include "gstaudioaggregator.h"
74 GST_DEBUG_CATEGORY_STATIC (audio_aggregator_debug);
75 #define GST_CAT_DEFAULT audio_aggregator_debug
77 struct _GstAudioAggregatorPadPrivate
79 /* All members are protected by the pad object lock */
81 GstBuffer *buffer; /* current buffer we're mixing, for
82 comparison with a new input buffer from
83 aggregator to see if we need to update our
86 guint position, size; /* position in the input buffer and size of the
87 input buffer in number of samples */
89 GstBuffer *input_buffer;
91 guint64 output_offset; /* Sample offset in output segment relative to
92 pad.segment.start that position refers to
93 in the current buffer. */
95 guint64 next_offset; /* Next expected sample offset relative to
98 /* Last time we noticed a discont */
99 GstClockTime discont_time;
101 /* A new unhandled segment event has been received */
102 gboolean new_segment;
106 /*****************************************
107 * GstAudioAggregatorPad implementation *
108 *****************************************/
109 G_DEFINE_TYPE (GstAudioAggregatorPad, gst_audio_aggregator_pad,
110 GST_TYPE_AGGREGATOR_PAD);
115 PROP_PAD_CONVERTER_CONFIG,
119 gst_audio_aggregator_pad_flush_pad (GstAggregatorPad * aggpad,
120 GstAggregator * aggregator);
123 gst_audio_aggregator_pad_finalize (GObject * object)
125 GstAudioAggregatorPad *pad = (GstAudioAggregatorPad *) object;
127 gst_buffer_replace (&pad->priv->buffer, NULL);
128 gst_buffer_replace (&pad->priv->input_buffer, NULL);
130 G_OBJECT_CLASS (gst_audio_aggregator_pad_parent_class)->finalize (object);
134 gst_audio_aggregator_pad_class_init (GstAudioAggregatorPadClass * klass)
136 GObjectClass *gobject_class = (GObjectClass *) klass;
137 GstAggregatorPadClass *aggpadclass = (GstAggregatorPadClass *) klass;
139 g_type_class_add_private (klass, sizeof (GstAudioAggregatorPadPrivate));
141 gobject_class->finalize = gst_audio_aggregator_pad_finalize;
142 aggpadclass->flush = GST_DEBUG_FUNCPTR (gst_audio_aggregator_pad_flush_pad);
146 gst_audio_aggregator_pad_init (GstAudioAggregatorPad * pad)
149 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AUDIO_AGGREGATOR_PAD,
150 GstAudioAggregatorPadPrivate);
152 gst_audio_info_init (&pad->info);
154 pad->priv->buffer = NULL;
155 pad->priv->input_buffer = NULL;
156 pad->priv->position = 0;
158 pad->priv->output_offset = -1;
159 pad->priv->next_offset = -1;
160 pad->priv->discont_time = GST_CLOCK_TIME_NONE;
165 gst_audio_aggregator_pad_flush_pad (GstAggregatorPad * aggpad,
166 GstAggregator * aggregator)
168 GstAudioAggregatorPad *pad = GST_AUDIO_AGGREGATOR_PAD (aggpad);
170 GST_OBJECT_LOCK (aggpad);
171 pad->priv->position = pad->priv->size = 0;
172 pad->priv->output_offset = pad->priv->next_offset = -1;
173 pad->priv->discont_time = GST_CLOCK_TIME_NONE;
174 gst_buffer_replace (&pad->priv->buffer, NULL);
175 gst_buffer_replace (&pad->priv->input_buffer, NULL);
176 GST_OBJECT_UNLOCK (aggpad);
181 struct _GstAudioAggregatorConvertPadPrivate
183 /* All members are protected by the pad object lock */
184 GstAudioConverter *converter;
185 GstStructure *converter_config;
186 gboolean converter_config_changed;
190 G_DEFINE_TYPE (GstAudioAggregatorConvertPad, gst_audio_aggregator_convert_pad,
191 GST_TYPE_AUDIO_AGGREGATOR_PAD);
194 gst_audio_aggregator_convert_pad_update_converter (GstAudioAggregatorConvertPad
195 * aaggcpad, GstAudioInfo * in_info, GstAudioInfo * out_info)
197 if (!aaggcpad->priv->converter_config_changed)
200 if (aaggcpad->priv->converter) {
201 gst_audio_converter_free (aaggcpad->priv->converter);
202 aaggcpad->priv->converter = NULL;
205 if (gst_audio_info_is_equal (in_info, out_info) ||
206 in_info->finfo->format == GST_AUDIO_FORMAT_UNKNOWN) {
207 if (aaggcpad->priv->converter) {
208 gst_audio_converter_free (aaggcpad->priv->converter);
209 aaggcpad->priv->converter = NULL;
212 /* If we haven't received caps yet, this pad should not have
213 * a buffer to convert anyway */
214 aaggcpad->priv->converter =
215 gst_audio_converter_new (GST_AUDIO_CONVERTER_FLAG_NONE,
217 aaggcpad->priv->converter_config ? gst_structure_copy (aaggcpad->
218 priv->converter_config) : NULL);
221 aaggcpad->priv->converter_config_changed = FALSE;
225 gst_audio_aggregator_pad_update_conversion_info (GstAudioAggregatorPad *
228 GST_AUDIO_AGGREGATOR_CONVERT_PAD (aaggpad)->priv->converter_config_changed =
233 gst_audio_aggregator_convert_pad_convert_buffer (GstAudioAggregatorPad *
234 aaggpad, GstAudioInfo * in_info, GstAudioInfo * out_info,
235 GstBuffer * input_buffer)
238 GstAudioAggregatorConvertPad *aaggcpad =
239 GST_AUDIO_AGGREGATOR_CONVERT_PAD (aaggpad);
241 gst_audio_aggregator_convert_pad_update_converter (aaggcpad, in_info,
244 if (aaggcpad->priv->converter) {
245 gint insize = gst_buffer_get_size (input_buffer);
246 gsize insamples = insize / in_info->bpf;
248 gst_audio_converter_get_out_frames (aaggcpad->priv->converter,
250 gint outsize = outsamples * out_info->bpf;
251 GstMapInfo inmap, outmap;
253 res = gst_buffer_new_allocate (NULL, outsize, NULL);
255 /* We create a perfectly similar buffer, except obviously for
256 * its converted contents */
257 gst_buffer_copy_into (res, input_buffer,
258 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
259 GST_BUFFER_COPY_META, 0, -1);
261 gst_buffer_map (input_buffer, &inmap, GST_MAP_READ);
262 gst_buffer_map (res, &outmap, GST_MAP_WRITE);
264 gst_audio_converter_samples (aaggcpad->priv->converter,
265 GST_AUDIO_CONVERTER_FLAG_NONE,
266 (gpointer *) & inmap.data, insamples,
267 (gpointer *) & outmap.data, outsamples);
269 gst_buffer_unmap (input_buffer, &inmap);
270 gst_buffer_unmap (res, &outmap);
272 res = gst_buffer_ref (input_buffer);
279 gst_audio_aggregator_convert_pad_finalize (GObject * object)
281 GstAudioAggregatorConvertPad *pad = (GstAudioAggregatorConvertPad *) object;
283 if (pad->priv->converter)
284 gst_audio_converter_free (pad->priv->converter);
286 if (pad->priv->converter_config)
287 gst_structure_free (pad->priv->converter_config);
289 G_OBJECT_CLASS (gst_audio_aggregator_convert_pad_parent_class)->finalize
294 gst_audio_aggregator_convert_pad_get_property (GObject * object, guint prop_id,
295 GValue * value, GParamSpec * pspec)
297 GstAudioAggregatorConvertPad *pad = GST_AUDIO_AGGREGATOR_CONVERT_PAD (object);
300 case PROP_PAD_CONVERTER_CONFIG:
301 GST_OBJECT_LOCK (pad);
302 if (pad->priv->converter_config)
303 g_value_set_boxed (value, pad->priv->converter_config);
304 GST_OBJECT_UNLOCK (pad);
307 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313 gst_audio_aggregator_convert_pad_set_property (GObject * object, guint prop_id,
314 const GValue * value, GParamSpec * pspec)
316 GstAudioAggregatorConvertPad *pad = GST_AUDIO_AGGREGATOR_CONVERT_PAD (object);
319 case PROP_PAD_CONVERTER_CONFIG:
320 GST_OBJECT_LOCK (pad);
321 if (pad->priv->converter_config)
322 gst_structure_free (pad->priv->converter_config);
323 pad->priv->converter_config = g_value_dup_boxed (value);
324 pad->priv->converter_config_changed = TRUE;
325 GST_OBJECT_UNLOCK (pad);
328 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
334 gst_audio_aggregator_convert_pad_class_init (GstAudioAggregatorConvertPadClass *
337 GObjectClass *gobject_class = (GObjectClass *) klass;
338 GstAudioAggregatorPadClass *aaggpad_class =
339 (GstAudioAggregatorPadClass *) klass;
340 g_type_class_add_private (klass,
341 sizeof (GstAudioAggregatorConvertPadPrivate));
343 gobject_class->set_property = gst_audio_aggregator_convert_pad_set_property;
344 gobject_class->get_property = gst_audio_aggregator_convert_pad_get_property;
346 g_object_class_install_property (gobject_class, PROP_PAD_CONVERTER_CONFIG,
347 g_param_spec_boxed ("converter-config", "Converter configuration",
348 "A GstStructure describing the configuration that should be used "
349 "when converting this pad's audio buffers",
350 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352 aaggpad_class->convert_buffer =
353 gst_audio_aggregator_convert_pad_convert_buffer;
355 aaggpad_class->update_conversion_info =
356 gst_audio_aggregator_pad_update_conversion_info;
358 gobject_class->finalize = gst_audio_aggregator_convert_pad_finalize;
362 gst_audio_aggregator_convert_pad_init (GstAudioAggregatorConvertPad * pad)
365 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AUDIO_AGGREGATOR_CONVERT_PAD,
366 GstAudioAggregatorConvertPadPrivate);
369 /**************************************
370 * GstAudioAggregator implementation *
371 **************************************/
373 struct _GstAudioAggregatorPrivate
377 /* All three properties are unprotected, can't be modified while streaming */
378 /* Size in frames that is output per buffer */
379 GstClockTime output_buffer_duration;
380 GstClockTime alignment_threshold;
381 GstClockTime discont_wait;
383 /* Protected by srcpad stream clock */
384 /* Output buffer starting at offset containing blocksize frames (calculated
385 * from output_buffer_duration) */
386 GstBuffer *current_buffer;
388 /* counters to keep track of timestamps */
389 /* Readable with object lock, writable with both aag lock and object lock */
391 /* Sample offset starting from 0 at aggregator.segment.start */
395 #define GST_AUDIO_AGGREGATOR_LOCK(self) g_mutex_lock (&(self)->priv->mutex);
396 #define GST_AUDIO_AGGREGATOR_UNLOCK(self) g_mutex_unlock (&(self)->priv->mutex);
398 static void gst_audio_aggregator_set_property (GObject * object, guint prop_id,
399 const GValue * value, GParamSpec * pspec);
400 static void gst_audio_aggregator_get_property (GObject * object, guint prop_id,
401 GValue * value, GParamSpec * pspec);
402 static void gst_audio_aggregator_dispose (GObject * object);
404 static gboolean gst_audio_aggregator_src_event (GstAggregator * agg,
406 static gboolean gst_audio_aggregator_sink_event (GstAggregator * agg,
407 GstAggregatorPad * aggpad, GstEvent * event);
408 static gboolean gst_audio_aggregator_src_query (GstAggregator * agg,
411 gst_audio_aggregator_sink_query (GstAggregator * agg, GstAggregatorPad * aggpad,
413 static gboolean gst_audio_aggregator_start (GstAggregator * agg);
414 static gboolean gst_audio_aggregator_stop (GstAggregator * agg);
415 static GstFlowReturn gst_audio_aggregator_flush (GstAggregator * agg);
417 static GstBuffer *gst_audio_aggregator_create_output_buffer (GstAudioAggregator
418 * aagg, guint num_frames);
419 static GstBuffer *gst_audio_aggregator_do_clip (GstAggregator * agg,
420 GstAggregatorPad * bpad, GstBuffer * buffer);
421 static GstFlowReturn gst_audio_aggregator_aggregate (GstAggregator * agg,
423 static gboolean sync_pad_values (GstElement * aagg, GstPad * pad, gpointer ud);
424 static gboolean gst_audio_aggregator_negotiated_src_caps (GstAggregator * agg,
427 gst_audio_aggregator_update_src_caps (GstAggregator * agg,
428 GstCaps * caps, GstCaps ** ret);
429 static GstCaps *gst_audio_aggregator_fixate_src_caps (GstAggregator * agg,
432 #define DEFAULT_OUTPUT_BUFFER_DURATION (10 * GST_MSECOND)
433 #define DEFAULT_ALIGNMENT_THRESHOLD (40 * GST_MSECOND)
434 #define DEFAULT_DISCONT_WAIT (1 * GST_SECOND)
439 PROP_OUTPUT_BUFFER_DURATION,
440 PROP_ALIGNMENT_THRESHOLD,
444 G_DEFINE_ABSTRACT_TYPE (GstAudioAggregator, gst_audio_aggregator,
445 GST_TYPE_AGGREGATOR);
448 gst_audio_aggregator_get_next_time (GstAggregator * agg)
450 GstClockTime next_time;
452 GST_OBJECT_LOCK (agg);
453 if (agg->segment.position == -1 || agg->segment.position < agg->segment.start)
454 next_time = agg->segment.start;
456 next_time = agg->segment.position;
458 if (agg->segment.stop != -1 && next_time > agg->segment.stop)
459 next_time = agg->segment.stop;
462 gst_segment_to_running_time (&agg->segment, GST_FORMAT_TIME, next_time);
463 GST_OBJECT_UNLOCK (agg);
469 gst_audio_aggregator_convert_buffer (GstAudioAggregator * aagg, GstPad * pad,
470 GstAudioInfo * in_info, GstAudioInfo * out_info, GstBuffer * buffer)
472 GstAudioAggregatorPadClass *klass = GST_AUDIO_AGGREGATOR_PAD_GET_CLASS (pad);
473 GstAudioAggregatorPad *aaggpad = GST_AUDIO_AGGREGATOR_PAD (pad);
475 g_assert (klass->convert_buffer);
477 return klass->convert_buffer (aaggpad, in_info, out_info, buffer);
481 gst_audio_aggregator_class_init (GstAudioAggregatorClass * klass)
483 GObjectClass *gobject_class = (GObjectClass *) klass;
484 GstAggregatorClass *gstaggregator_class = (GstAggregatorClass *) klass;
486 g_type_class_add_private (klass, sizeof (GstAudioAggregatorPrivate));
488 gobject_class->set_property = gst_audio_aggregator_set_property;
489 gobject_class->get_property = gst_audio_aggregator_get_property;
490 gobject_class->dispose = gst_audio_aggregator_dispose;
492 gstaggregator_class->src_event =
493 GST_DEBUG_FUNCPTR (gst_audio_aggregator_src_event);
494 gstaggregator_class->sink_event =
495 GST_DEBUG_FUNCPTR (gst_audio_aggregator_sink_event);
496 gstaggregator_class->src_query =
497 GST_DEBUG_FUNCPTR (gst_audio_aggregator_src_query);
498 gstaggregator_class->sink_query = gst_audio_aggregator_sink_query;
499 gstaggregator_class->start = gst_audio_aggregator_start;
500 gstaggregator_class->stop = gst_audio_aggregator_stop;
501 gstaggregator_class->flush = gst_audio_aggregator_flush;
502 gstaggregator_class->aggregate =
503 GST_DEBUG_FUNCPTR (gst_audio_aggregator_aggregate);
504 gstaggregator_class->clip = GST_DEBUG_FUNCPTR (gst_audio_aggregator_do_clip);
505 gstaggregator_class->get_next_time = gst_audio_aggregator_get_next_time;
506 gstaggregator_class->update_src_caps =
507 GST_DEBUG_FUNCPTR (gst_audio_aggregator_update_src_caps);
508 gstaggregator_class->fixate_src_caps = gst_audio_aggregator_fixate_src_caps;
509 gstaggregator_class->negotiated_src_caps =
510 gst_audio_aggregator_negotiated_src_caps;
512 klass->create_output_buffer = gst_audio_aggregator_create_output_buffer;
514 GST_DEBUG_CATEGORY_INIT (audio_aggregator_debug, "audioaggregator",
515 GST_DEBUG_FG_MAGENTA, "GstAudioAggregator");
517 g_object_class_install_property (gobject_class, PROP_OUTPUT_BUFFER_DURATION,
518 g_param_spec_uint64 ("output-buffer-duration", "Output Buffer Duration",
519 "Output block size in nanoseconds", 1,
520 G_MAXUINT64, DEFAULT_OUTPUT_BUFFER_DURATION,
521 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
523 g_object_class_install_property (gobject_class, PROP_ALIGNMENT_THRESHOLD,
524 g_param_spec_uint64 ("alignment-threshold", "Alignment Threshold",
525 "Timestamp alignment threshold in nanoseconds", 0,
526 G_MAXUINT64 - 1, DEFAULT_ALIGNMENT_THRESHOLD,
527 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
529 g_object_class_install_property (gobject_class, PROP_DISCONT_WAIT,
530 g_param_spec_uint64 ("discont-wait", "Discont Wait",
531 "Window of time in nanoseconds to wait before "
532 "creating a discontinuity", 0,
533 G_MAXUINT64 - 1, DEFAULT_DISCONT_WAIT,
534 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
538 gst_audio_aggregator_init (GstAudioAggregator * aagg)
541 G_TYPE_INSTANCE_GET_PRIVATE (aagg, GST_TYPE_AUDIO_AGGREGATOR,
542 GstAudioAggregatorPrivate);
544 g_mutex_init (&aagg->priv->mutex);
546 aagg->priv->output_buffer_duration = DEFAULT_OUTPUT_BUFFER_DURATION;
547 aagg->priv->alignment_threshold = DEFAULT_ALIGNMENT_THRESHOLD;
548 aagg->priv->discont_wait = DEFAULT_DISCONT_WAIT;
550 aagg->current_caps = NULL;
552 gst_aggregator_set_latency (GST_AGGREGATOR (aagg),
553 aagg->priv->output_buffer_duration, aagg->priv->output_buffer_duration);
557 gst_audio_aggregator_dispose (GObject * object)
559 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (object);
561 gst_caps_replace (&aagg->current_caps, NULL);
563 g_mutex_clear (&aagg->priv->mutex);
565 G_OBJECT_CLASS (gst_audio_aggregator_parent_class)->dispose (object);
569 gst_audio_aggregator_set_property (GObject * object, guint prop_id,
570 const GValue * value, GParamSpec * pspec)
572 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (object);
575 case PROP_OUTPUT_BUFFER_DURATION:
576 aagg->priv->output_buffer_duration = g_value_get_uint64 (value);
577 gst_aggregator_set_latency (GST_AGGREGATOR (aagg),
578 aagg->priv->output_buffer_duration,
579 aagg->priv->output_buffer_duration);
581 case PROP_ALIGNMENT_THRESHOLD:
582 aagg->priv->alignment_threshold = g_value_get_uint64 (value);
584 case PROP_DISCONT_WAIT:
585 aagg->priv->discont_wait = g_value_get_uint64 (value);
588 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
594 gst_audio_aggregator_get_property (GObject * object, guint prop_id,
595 GValue * value, GParamSpec * pspec)
597 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (object);
600 case PROP_OUTPUT_BUFFER_DURATION:
601 g_value_set_uint64 (value, aagg->priv->output_buffer_duration);
603 case PROP_ALIGNMENT_THRESHOLD:
604 g_value_set_uint64 (value, aagg->priv->alignment_threshold);
606 case PROP_DISCONT_WAIT:
607 g_value_set_uint64 (value, aagg->priv->discont_wait);
610 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
615 /* Caps negotiation */
617 /* Unref after usage */
618 static GstAudioAggregatorPad *
619 gst_audio_aggregator_get_first_configured_pad (GstAggregator * agg)
621 GstAudioAggregatorPad *res = NULL;
624 GST_OBJECT_LOCK (agg);
625 for (l = GST_ELEMENT (agg)->sinkpads; l; l = l->next) {
626 GstAudioAggregatorPad *aaggpad = l->data;
628 if (GST_AUDIO_INFO_FORMAT (&aaggpad->info) != GST_AUDIO_FORMAT_UNKNOWN) {
629 res = gst_object_ref (aaggpad);
633 GST_OBJECT_UNLOCK (agg);
639 gst_audio_aggregator_sink_getcaps (GstPad * pad, GstAggregator * agg,
642 GstAudioAggregatorPad *first_configured_pad =
643 gst_audio_aggregator_get_first_configured_pad (agg);
644 GstCaps *sink_template_caps = gst_pad_get_pad_template_caps (pad);
645 GstCaps *downstream_caps = gst_pad_get_allowed_caps (agg->srcpad);
647 GstStructure *s, *s2;
648 gint downstream_rate;
650 sink_template_caps = gst_caps_make_writable (sink_template_caps);
651 s = gst_caps_get_structure (sink_template_caps, 0);
653 if (downstream_caps && !gst_caps_is_empty (downstream_caps))
654 s2 = gst_caps_get_structure (downstream_caps, 0);
658 if (s2 && gst_structure_get_int (s2, "rate", &downstream_rate)) {
659 gst_structure_fixate_field_nearest_int (s, "rate", downstream_rate);
660 } else if (first_configured_pad) {
661 gst_structure_fixate_field_nearest_int (s, "rate",
662 first_configured_pad->info.rate);
665 if (first_configured_pad)
666 gst_object_unref (first_configured_pad);
668 sink_caps = filter ? gst_caps_intersect (sink_template_caps,
669 filter) : gst_caps_ref (sink_template_caps);
671 GST_INFO_OBJECT (pad, "Getting caps with filter %" GST_PTR_FORMAT, filter);
672 GST_DEBUG_OBJECT (pad, "sink template caps : %" GST_PTR_FORMAT,
674 GST_DEBUG_OBJECT (pad, "downstream caps %" GST_PTR_FORMAT, downstream_caps);
675 GST_INFO_OBJECT (pad, "returned sink caps : %" GST_PTR_FORMAT, sink_caps);
677 gst_caps_unref (sink_template_caps);
680 gst_caps_unref (downstream_caps);
686 gst_audio_aggregator_sink_setcaps (GstAudioAggregatorPad * aaggpad,
687 GstAggregator * agg, GstCaps * caps)
689 GstAudioAggregatorPad *first_configured_pad =
690 gst_audio_aggregator_get_first_configured_pad (agg);
691 GstCaps *downstream_caps = gst_pad_get_allowed_caps (agg->srcpad);
694 gint downstream_rate;
697 if (!downstream_caps || gst_caps_is_empty (downstream_caps)) {
702 gst_audio_info_from_caps (&info, caps);
703 s = gst_caps_get_structure (downstream_caps, 0);
705 /* TODO: handle different rates on sinkpads, a bit complex
706 * because offsets will have to be updated, and audio resampling
707 * has a latency to take into account
709 if ((gst_structure_get_int (s, "rate", &downstream_rate)
710 && info.rate != downstream_rate) || (first_configured_pad
711 && info.rate != first_configured_pad->info.rate)) {
712 gst_pad_push_event (GST_PAD (aaggpad), gst_event_new_reconfigure ());
715 GstAudioAggregatorPadClass *klass =
716 GST_AUDIO_AGGREGATOR_PAD_GET_CLASS (aaggpad);
717 GST_OBJECT_LOCK (aaggpad);
718 gst_audio_info_from_caps (&aaggpad->info, caps);
719 if (klass->update_conversion_info)
720 klass->update_conversion_info (aaggpad);
721 GST_OBJECT_UNLOCK (aaggpad);
725 if (first_configured_pad)
726 gst_object_unref (first_configured_pad);
729 gst_caps_unref (downstream_caps);
735 gst_audio_aggregator_update_src_caps (GstAggregator * agg,
736 GstCaps * caps, GstCaps ** ret)
738 GstCaps *src_template_caps = gst_pad_get_pad_template_caps (agg->srcpad);
739 GstCaps *downstream_caps =
740 gst_pad_peer_query_caps (agg->srcpad, src_template_caps);
742 gst_caps_unref (src_template_caps);
744 *ret = gst_caps_intersect (caps, downstream_caps);
746 GST_INFO ("Updated src caps to %" GST_PTR_FORMAT, *ret);
749 gst_caps_unref (downstream_caps);
754 /* At that point if the caps are not fixed, this means downstream
755 * didn't have fully specified requirements, we'll just go ahead
756 * and fixate raw audio fields using our first configured pad, we don't for
757 * now need a more complicated heuristic
760 gst_audio_aggregator_fixate_src_caps (GstAggregator * agg, GstCaps * caps)
762 GstAudioAggregatorPad *first_configured_pad;
764 if (!GST_AUDIO_AGGREGATOR_PAD_GET_CLASS (agg->srcpad)->convert_buffer)
767 (gst_audio_aggregator_parent_class)->fixate_src_caps (agg, caps);
769 first_configured_pad = gst_audio_aggregator_get_first_configured_pad (agg);
771 if (first_configured_pad) {
772 GstStructure *s, *s2;
773 GstCaps *first_configured_caps =
774 gst_audio_info_to_caps (&first_configured_pad->info);
775 gint first_configured_rate, first_configured_channels;
777 caps = gst_caps_make_writable (caps);
778 s = gst_caps_get_structure (caps, 0);
779 s2 = gst_caps_get_structure (first_configured_caps, 0);
781 gst_structure_get_int (s2, "rate", &first_configured_rate);
782 gst_structure_get_int (s2, "channels", &first_configured_channels);
784 gst_structure_fixate_field_string (s, "format",
785 gst_structure_get_string (s2, "format"));
786 gst_structure_fixate_field_string (s, "layout",
787 gst_structure_get_string (s2, "layout"));
788 gst_structure_fixate_field_nearest_int (s, "rate", first_configured_rate);
789 gst_structure_fixate_field_nearest_int (s, "channels",
790 first_configured_channels);
792 gst_caps_unref (first_configured_caps);
793 gst_object_unref (first_configured_pad);
796 if (!gst_caps_is_fixed (caps))
797 caps = gst_caps_fixate (caps);
799 GST_INFO_OBJECT (agg, "Fixated src caps to %" GST_PTR_FORMAT, caps);
804 /* Must be called with OBJECT_LOCK taken */
806 gst_audio_aggregator_update_converters (GstAudioAggregator * aagg,
807 GstAudioInfo * new_info)
811 for (l = GST_ELEMENT (aagg)->sinkpads; l; l = l->next) {
812 GstAudioAggregatorPad *aaggpad = l->data;
813 GstAudioAggregatorPadClass *klass =
814 GST_AUDIO_AGGREGATOR_PAD_GET_CLASS (aaggpad);
816 if (klass->update_conversion_info)
817 klass->update_conversion_info (aaggpad);
819 /* If we currently were mixing a buffer, we need to convert it to the new
821 if (aaggpad->priv->buffer) {
822 GstBuffer *new_converted_buffer =
823 gst_audio_aggregator_convert_buffer (aagg, GST_PAD (aaggpad),
824 &aaggpad->info, new_info, aaggpad->priv->input_buffer);
825 gst_buffer_replace (&aaggpad->priv->buffer, new_converted_buffer);
830 /* We now have our final output caps, we can create the required converters */
832 gst_audio_aggregator_negotiated_src_caps (GstAggregator * agg, GstCaps * caps)
834 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (agg);
836 GstAudioAggregatorPad *srcpad = GST_AUDIO_AGGREGATOR_PAD (agg->srcpad);
838 GST_INFO_OBJECT (agg, "src caps negotiated %" GST_PTR_FORMAT, caps);
840 if (!gst_audio_info_from_caps (&info, caps)) {
841 GST_WARNING_OBJECT (aagg, "Rejecting invalid caps: %" GST_PTR_FORMAT, caps);
845 GST_AUDIO_AGGREGATOR_LOCK (aagg);
846 GST_OBJECT_LOCK (aagg);
848 if (GST_AUDIO_AGGREGATOR_PAD_GET_CLASS (agg->srcpad)->convert_buffer) {
849 gst_audio_aggregator_update_converters (aagg, &info);
851 if (aagg->priv->current_buffer
852 && !gst_audio_info_is_equal (&srcpad->info, &info)) {
853 GstBuffer *converted;
854 GstAudioAggregatorPadClass *klass =
855 GST_AUDIO_AGGREGATOR_PAD_GET_CLASS (agg->srcpad);
857 if (klass->update_conversion_info)
858 klass->update_conversion_info (GST_AUDIO_AGGREGATOR_PAD (agg->srcpad));
861 gst_audio_aggregator_convert_buffer (aagg, agg->srcpad, &srcpad->info,
862 &info, aagg->priv->current_buffer);
863 gst_buffer_unref (aagg->priv->current_buffer);
864 aagg->priv->current_buffer = converted;
868 if (!gst_audio_info_is_equal (&info, &srcpad->info)) {
869 GST_INFO_OBJECT (aagg, "setting caps to %" GST_PTR_FORMAT, caps);
870 gst_caps_replace (&aagg->current_caps, caps);
872 memcpy (&srcpad->info, &info, sizeof (info));
875 GST_OBJECT_UNLOCK (aagg);
876 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
880 (gst_audio_aggregator_parent_class)->negotiated_src_caps (agg, caps);
886 gst_audio_aggregator_src_event (GstAggregator * agg, GstEvent * event)
890 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (agg);
891 GST_DEBUG_OBJECT (agg->srcpad, "Got %s event on src pad",
892 GST_EVENT_TYPE_NAME (event));
894 switch (GST_EVENT_TYPE (event)) {
896 /* QoS might be tricky */
897 gst_event_unref (event);
899 case GST_EVENT_NAVIGATION:
900 /* navigation is rather pointless. */
901 gst_event_unref (event);
908 GstSeekType start_type, stop_type;
910 GstFormat seek_format, dest_format;
912 /* parse the seek parameters */
913 gst_event_parse_seek (event, &rate, &seek_format, &flags, &start_type,
914 &start, &stop_type, &stop);
916 /* Check the seeking parameters before linking up */
917 if ((start_type != GST_SEEK_TYPE_NONE)
918 && (start_type != GST_SEEK_TYPE_SET)) {
920 GST_DEBUG_OBJECT (aagg,
921 "seeking failed, unhandled seek type for start: %d", start_type);
924 if ((stop_type != GST_SEEK_TYPE_NONE) && (stop_type != GST_SEEK_TYPE_SET)) {
926 GST_DEBUG_OBJECT (aagg,
927 "seeking failed, unhandled seek type for end: %d", stop_type);
931 GST_OBJECT_LOCK (agg);
932 dest_format = agg->segment.format;
933 GST_OBJECT_UNLOCK (agg);
934 if (seek_format != dest_format) {
936 GST_DEBUG_OBJECT (aagg,
937 "seeking failed, unhandled seek format: %s",
938 gst_format_get_name (seek_format));
948 GST_AGGREGATOR_CLASS (gst_audio_aggregator_parent_class)->src_event (agg,
957 gst_audio_aggregator_sink_event (GstAggregator * agg,
958 GstAggregatorPad * aggpad, GstEvent * event)
960 GstAudioAggregatorPad *aaggpad = GST_AUDIO_AGGREGATOR_PAD (aggpad);
963 GST_DEBUG_OBJECT (aggpad, "Got %s event on sink pad",
964 GST_EVENT_TYPE_NAME (event));
966 switch (GST_EVENT_TYPE (event)) {
967 case GST_EVENT_SEGMENT:
969 const GstSegment *segment;
970 gst_event_parse_segment (event, &segment);
972 if (segment->format != GST_FORMAT_TIME) {
973 GST_ERROR_OBJECT (agg, "Segment of type %s are not supported,"
974 " only TIME segments are supported",
975 gst_format_get_name (segment->format));
976 gst_event_unref (event);
982 GST_OBJECT_LOCK (agg);
983 if (segment->rate != agg->segment.rate) {
984 GST_ERROR_OBJECT (aggpad,
985 "Got segment event with wrong rate %lf, expected %lf",
986 segment->rate, agg->segment.rate);
988 gst_event_unref (event);
990 } else if (segment->rate < 0.0) {
991 GST_ERROR_OBJECT (aggpad, "Negative rates not supported yet");
993 gst_event_unref (event);
996 GstAudioAggregatorPad *pad = GST_AUDIO_AGGREGATOR_PAD (aggpad);
998 GST_OBJECT_LOCK (pad);
999 pad->priv->new_segment = TRUE;
1000 GST_OBJECT_UNLOCK (pad);
1002 GST_OBJECT_UNLOCK (agg);
1006 case GST_EVENT_CAPS:
1010 gst_event_parse_caps (event, &caps);
1011 GST_INFO_OBJECT (aggpad, "Got caps %" GST_PTR_FORMAT, caps);
1012 res = gst_audio_aggregator_sink_setcaps (aaggpad, agg, caps);
1013 gst_event_unref (event);
1023 GST_AGGREGATOR_CLASS (gst_audio_aggregator_parent_class)->sink_event
1024 (agg, aggpad, event);
1030 gst_audio_aggregator_sink_query (GstAggregator * agg, GstAggregatorPad * aggpad,
1033 gboolean res = FALSE;
1035 switch (GST_QUERY_TYPE (query)) {
1036 case GST_QUERY_CAPS:
1038 GstCaps *filter, *caps;
1040 gst_query_parse_caps (query, &filter);
1041 caps = gst_audio_aggregator_sink_getcaps (GST_PAD (aggpad), agg, filter);
1042 gst_query_set_caps_result (query, caps);
1043 gst_caps_unref (caps);
1049 GST_AGGREGATOR_CLASS (gst_audio_aggregator_parent_class)->sink_query
1050 (agg, aggpad, query);
1058 /* FIXME, the duration query should reflect how long you will produce
1059 * data, that is the amount of stream time until you will emit EOS.
1061 * For synchronized mixing this is always the max of all the durations
1062 * of upstream since we emit EOS when all of them finished.
1064 * We don't do synchronized mixing so this really depends on where the
1065 * streams where punched in and what their relative offsets are against
1066 * eachother which we can get from the first timestamps we see.
1068 * When we add a new stream (or remove a stream) the duration might
1069 * also become invalid again and we need to post a new DURATION
1070 * message to notify this fact to the parent.
1071 * For now we take the max of all the upstream elements so the simple
1072 * cases work at least somewhat.
1075 gst_audio_aggregator_query_duration (GstAudioAggregator * aagg,
1083 GValue item = { 0, };
1086 gst_query_parse_duration (query, &format, NULL);
1092 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (aagg));
1094 GstIteratorResult ires;
1096 ires = gst_iterator_next (it, &item);
1098 case GST_ITERATOR_DONE:
1101 case GST_ITERATOR_OK:
1103 GstPad *pad = g_value_get_object (&item);
1106 /* ask sink peer for duration */
1107 res &= gst_pad_peer_query_duration (pad, format, &duration);
1108 /* take max from all valid return values */
1110 /* valid unknown length, stop searching */
1111 if (duration == -1) {
1115 /* else see if bigger than current max */
1116 else if (duration > max)
1119 g_value_reset (&item);
1122 case GST_ITERATOR_RESYNC:
1125 gst_iterator_resync (it);
1133 g_value_unset (&item);
1134 gst_iterator_free (it);
1137 /* and store the max */
1138 GST_DEBUG_OBJECT (aagg, "Total duration in format %s: %"
1139 GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
1140 gst_query_set_duration (query, format, max);
1148 gst_audio_aggregator_src_query (GstAggregator * agg, GstQuery * query)
1150 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (agg);
1151 GstAudioAggregatorPad *srcpad = GST_AUDIO_AGGREGATOR_PAD (agg->srcpad);
1152 gboolean res = FALSE;
1154 switch (GST_QUERY_TYPE (query)) {
1155 case GST_QUERY_DURATION:
1156 res = gst_audio_aggregator_query_duration (aagg, query);
1158 case GST_QUERY_POSITION:
1162 gst_query_parse_position (query, &format, NULL);
1164 GST_OBJECT_LOCK (aagg);
1167 case GST_FORMAT_TIME:
1168 gst_query_set_position (query, format,
1169 gst_segment_to_stream_time (&agg->segment, GST_FORMAT_TIME,
1170 agg->segment.position));
1173 case GST_FORMAT_BYTES:
1174 if (GST_AUDIO_INFO_BPF (&srcpad->info)) {
1175 gst_query_set_position (query, format, aagg->priv->offset *
1176 GST_AUDIO_INFO_BPF (&srcpad->info));
1180 case GST_FORMAT_DEFAULT:
1181 gst_query_set_position (query, format, aagg->priv->offset);
1188 GST_OBJECT_UNLOCK (aagg);
1194 GST_AGGREGATOR_CLASS (gst_audio_aggregator_parent_class)->src_query
1204 gst_audio_aggregator_set_sink_caps (GstAudioAggregator * aagg,
1205 GstAudioAggregatorPad * pad, GstCaps * caps)
1207 #ifndef G_DISABLE_ASSERT
1210 GST_OBJECT_LOCK (pad);
1211 valid = gst_audio_info_from_caps (&pad->info, caps);
1213 GST_OBJECT_UNLOCK (pad);
1215 GST_OBJECT_LOCK (pad);
1216 (void) gst_audio_info_from_caps (&pad->info, caps);
1217 GST_OBJECT_UNLOCK (pad);
1221 /* Must hold object lock and aagg lock to call */
1224 gst_audio_aggregator_reset (GstAudioAggregator * aagg)
1226 GstAggregator *agg = GST_AGGREGATOR (aagg);
1228 GST_AUDIO_AGGREGATOR_LOCK (aagg);
1229 GST_OBJECT_LOCK (aagg);
1230 agg->segment.position = -1;
1231 aagg->priv->offset = -1;
1232 gst_audio_info_init (&GST_AUDIO_AGGREGATOR_PAD (agg->srcpad)->info);
1233 gst_caps_replace (&aagg->current_caps, NULL);
1234 gst_buffer_replace (&aagg->priv->current_buffer, NULL);
1235 GST_OBJECT_UNLOCK (aagg);
1236 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1240 gst_audio_aggregator_start (GstAggregator * agg)
1242 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (agg);
1244 gst_audio_aggregator_reset (aagg);
1250 gst_audio_aggregator_stop (GstAggregator * agg)
1252 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (agg);
1254 gst_audio_aggregator_reset (aagg);
1259 static GstFlowReturn
1260 gst_audio_aggregator_flush (GstAggregator * agg)
1262 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (agg);
1264 GST_AUDIO_AGGREGATOR_LOCK (aagg);
1265 GST_OBJECT_LOCK (aagg);
1266 agg->segment.position = -1;
1267 aagg->priv->offset = -1;
1268 gst_buffer_replace (&aagg->priv->current_buffer, NULL);
1269 GST_OBJECT_UNLOCK (aagg);
1270 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1276 gst_audio_aggregator_do_clip (GstAggregator * agg,
1277 GstAggregatorPad * bpad, GstBuffer * buffer)
1279 GstAudioAggregatorPad *pad = GST_AUDIO_AGGREGATOR_PAD (bpad);
1282 rate = GST_AUDIO_INFO_RATE (&pad->info);
1283 bpf = GST_AUDIO_INFO_BPF (&pad->info);
1285 GST_OBJECT_LOCK (bpad);
1286 buffer = gst_audio_buffer_clip (buffer, &bpad->segment, rate, bpf);
1287 GST_OBJECT_UNLOCK (bpad);
1292 /* Called with the object lock for both the element and pad held,
1293 * as well as the aagg lock
1295 * Replace the current buffer with input and update GstAudioAggregatorPadPrivate
1299 gst_audio_aggregator_fill_buffer (GstAudioAggregator * aagg,
1300 GstAudioAggregatorPad * pad)
1302 GstClockTime start_time, end_time;
1303 gboolean discont = FALSE;
1304 guint64 start_offset, end_offset;
1307 GstAggregator *agg = GST_AGGREGATOR (aagg);
1308 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1309 GstAudioAggregatorPad *srcpad = GST_AUDIO_AGGREGATOR_PAD (agg->srcpad);
1311 if (GST_AUDIO_AGGREGATOR_PAD_GET_CLASS (pad)->convert_buffer) {
1312 rate = GST_AUDIO_INFO_RATE (&srcpad->info);
1313 bpf = GST_AUDIO_INFO_BPF (&srcpad->info);
1315 rate = GST_AUDIO_INFO_RATE (&pad->info);
1316 bpf = GST_AUDIO_INFO_BPF (&pad->info);
1319 pad->priv->position = 0;
1320 pad->priv->size = gst_buffer_get_size (pad->priv->buffer) / bpf;
1322 if (pad->priv->size == 0) {
1323 if (!GST_BUFFER_DURATION_IS_VALID (pad->priv->buffer) ||
1324 !GST_BUFFER_FLAG_IS_SET (pad->priv->buffer, GST_BUFFER_FLAG_GAP)) {
1325 GST_WARNING_OBJECT (pad, "Dropping 0-sized buffer missing either a"
1326 " duration or a GAP flag: %" GST_PTR_FORMAT, pad->priv->buffer);
1331 gst_util_uint64_scale (GST_BUFFER_DURATION (pad->priv->buffer), rate,
1335 if (!GST_BUFFER_PTS_IS_VALID (pad->priv->buffer)) {
1336 if (pad->priv->output_offset == -1)
1337 pad->priv->output_offset = aagg->priv->offset;
1338 if (pad->priv->next_offset == -1)
1339 pad->priv->next_offset = pad->priv->size;
1341 pad->priv->next_offset += pad->priv->size;
1345 start_time = GST_BUFFER_PTS (pad->priv->buffer);
1347 start_time + gst_util_uint64_scale_ceil (pad->priv->size, GST_SECOND,
1350 /* Clipping should've ensured this */
1351 g_assert (start_time >= aggpad->segment.start);
1354 gst_util_uint64_scale (start_time - aggpad->segment.start, rate,
1356 end_offset = start_offset + pad->priv->size;
1358 if (GST_BUFFER_IS_DISCONT (pad->priv->buffer)
1359 || GST_BUFFER_FLAG_IS_SET (pad->priv->buffer, GST_BUFFER_FLAG_RESYNC)
1360 || pad->priv->new_segment || pad->priv->next_offset == -1) {
1362 pad->priv->new_segment = FALSE;
1364 guint64 diff, max_sample_diff;
1366 /* Check discont, based on audiobasesink */
1367 if (start_offset <= pad->priv->next_offset)
1368 diff = pad->priv->next_offset - start_offset;
1370 diff = start_offset - pad->priv->next_offset;
1373 gst_util_uint64_scale_int (aagg->priv->alignment_threshold, rate,
1377 if (G_UNLIKELY (diff >= max_sample_diff)) {
1378 if (aagg->priv->discont_wait > 0) {
1379 if (pad->priv->discont_time == GST_CLOCK_TIME_NONE) {
1380 pad->priv->discont_time = start_time;
1381 } else if (start_time - pad->priv->discont_time >=
1382 aagg->priv->discont_wait) {
1384 pad->priv->discont_time = GST_CLOCK_TIME_NONE;
1389 } else if (G_UNLIKELY (pad->priv->discont_time != GST_CLOCK_TIME_NONE)) {
1390 /* we have had a discont, but are now back on track! */
1391 pad->priv->discont_time = GST_CLOCK_TIME_NONE;
1396 /* Have discont, need resync */
1397 if (pad->priv->next_offset != -1)
1398 GST_DEBUG_OBJECT (pad, "Have discont. Expected %"
1399 G_GUINT64_FORMAT ", got %" G_GUINT64_FORMAT,
1400 pad->priv->next_offset, start_offset);
1401 pad->priv->output_offset = -1;
1402 pad->priv->next_offset = end_offset;
1404 pad->priv->next_offset += pad->priv->size;
1407 if (pad->priv->output_offset == -1) {
1408 GstClockTime start_running_time;
1409 GstClockTime end_running_time;
1410 GstClockTime segment_pos;
1411 guint64 start_output_offset = -1;
1412 guint64 end_output_offset = -1;
1414 start_running_time =
1415 gst_segment_to_running_time (&aggpad->segment,
1416 GST_FORMAT_TIME, start_time);
1418 gst_segment_to_running_time (&aggpad->segment,
1419 GST_FORMAT_TIME, end_time);
1421 /* Convert to position in the output segment */
1423 gst_segment_position_from_running_time (&agg->segment, GST_FORMAT_TIME,
1424 start_running_time);
1425 if (GST_CLOCK_TIME_IS_VALID (segment_pos))
1426 start_output_offset =
1427 gst_util_uint64_scale (segment_pos - agg->segment.start, rate,
1431 gst_segment_position_from_running_time (&agg->segment, GST_FORMAT_TIME,
1433 if (GST_CLOCK_TIME_IS_VALID (segment_pos))
1435 gst_util_uint64_scale (segment_pos - agg->segment.start, rate,
1438 if (start_output_offset == -1 && end_output_offset == -1) {
1439 /* Outside output segment, drop */
1440 pad->priv->position = 0;
1441 pad->priv->size = 0;
1442 pad->priv->output_offset = -1;
1443 GST_DEBUG_OBJECT (pad, "Buffer outside output segment");
1447 /* Calculate end_output_offset if it was outside the output segment */
1448 if (end_output_offset == -1)
1449 end_output_offset = start_output_offset + pad->priv->size;
1451 if (end_output_offset < aagg->priv->offset) {
1452 pad->priv->position = 0;
1453 pad->priv->size = 0;
1454 pad->priv->output_offset = -1;
1455 GST_DEBUG_OBJECT (pad,
1456 "Buffer before segment or current position: %" G_GUINT64_FORMAT " < %"
1457 G_GINT64_FORMAT, end_output_offset, aagg->priv->offset);
1461 if (start_output_offset == -1 || start_output_offset < aagg->priv->offset) {
1464 if (start_output_offset == -1 && end_output_offset < pad->priv->size) {
1465 diff = pad->priv->size - end_output_offset + aagg->priv->offset;
1466 } else if (start_output_offset == -1) {
1467 start_output_offset = end_output_offset - pad->priv->size;
1469 if (start_output_offset < aagg->priv->offset)
1470 diff = aagg->priv->offset - start_output_offset;
1474 diff = aagg->priv->offset - start_output_offset;
1477 pad->priv->position += diff;
1478 if (pad->priv->position >= pad->priv->size) {
1479 /* Empty buffer, drop */
1480 pad->priv->position = 0;
1481 pad->priv->size = 0;
1482 pad->priv->output_offset = -1;
1483 GST_DEBUG_OBJECT (pad,
1484 "Buffer before segment or current position: %" G_GUINT64_FORMAT
1485 " < %" G_GINT64_FORMAT, end_output_offset, aagg->priv->offset);
1490 if (start_output_offset == -1 || start_output_offset < aagg->priv->offset)
1491 pad->priv->output_offset = aagg->priv->offset;
1493 pad->priv->output_offset = start_output_offset;
1495 GST_DEBUG_OBJECT (pad,
1496 "Buffer resynced: Pad offset %" G_GUINT64_FORMAT
1497 ", current audio aggregator offset %" G_GINT64_FORMAT,
1498 pad->priv->output_offset, aagg->priv->offset);
1503 GST_LOG_OBJECT (pad,
1504 "Queued new buffer at offset %" G_GUINT64_FORMAT,
1505 pad->priv->output_offset);
1510 /* Called with pad object lock held */
1513 gst_audio_aggregator_mix_buffer (GstAudioAggregator * aagg,
1514 GstAudioAggregatorPad * pad, GstBuffer * inbuf, GstBuffer * outbuf,
1521 gboolean pad_changed = FALSE;
1523 /* Overlap => mix */
1524 if (aagg->priv->offset < pad->priv->output_offset)
1525 out_start = pad->priv->output_offset - aagg->priv->offset;
1529 overlap = pad->priv->size - pad->priv->position;
1530 if (overlap > blocksize - out_start)
1531 overlap = blocksize - out_start;
1533 if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP)) {
1534 /* skip gap buffer */
1535 GST_LOG_OBJECT (pad, "skipping GAP buffer");
1536 pad->priv->output_offset += pad->priv->size - pad->priv->position;
1537 pad->priv->position = pad->priv->size;
1539 gst_buffer_replace (&pad->priv->buffer, NULL);
1540 gst_buffer_replace (&pad->priv->input_buffer, NULL);
1544 gst_buffer_ref (inbuf);
1545 in_offset = pad->priv->position;
1546 GST_OBJECT_UNLOCK (pad);
1547 GST_OBJECT_UNLOCK (aagg);
1549 filled = GST_AUDIO_AGGREGATOR_GET_CLASS (aagg)->aggregate_one_buffer (aagg,
1550 pad, inbuf, in_offset, outbuf, out_start, overlap);
1552 GST_OBJECT_LOCK (aagg);
1553 GST_OBJECT_LOCK (pad);
1555 pad_changed = (inbuf != pad->priv->buffer);
1556 gst_buffer_unref (inbuf);
1559 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
1564 pad->priv->position += overlap;
1565 pad->priv->output_offset += overlap;
1567 if (pad->priv->position == pad->priv->size) {
1568 /* Buffer done, drop it */
1569 gst_buffer_replace (&pad->priv->buffer, NULL);
1570 gst_buffer_replace (&pad->priv->input_buffer, NULL);
1571 GST_LOG_OBJECT (pad, "Finished mixing buffer, waiting for next");
1579 gst_audio_aggregator_create_output_buffer (GstAudioAggregator * aagg,
1582 GstAllocator *allocator;
1583 GstAllocationParams params;
1586 GstAggregator *agg = GST_AGGREGATOR (aagg);
1587 GstAudioAggregatorPad *srcpad = GST_AUDIO_AGGREGATOR_PAD (agg->srcpad);
1589 gst_aggregator_get_allocator (GST_AGGREGATOR (aagg), &allocator, ¶ms);
1591 GST_DEBUG ("Creating output buffer with size %d",
1592 num_frames * GST_AUDIO_INFO_BPF (&srcpad->info));
1594 outbuf = gst_buffer_new_allocate (allocator, num_frames *
1595 GST_AUDIO_INFO_BPF (&srcpad->info), ¶ms);
1598 gst_object_unref (allocator);
1600 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
1601 gst_audio_format_fill_silence (srcpad->info.finfo, outmap.data, outmap.size);
1602 gst_buffer_unmap (outbuf, &outmap);
1608 sync_pad_values (GstElement * aagg, GstPad * pad, gpointer user_data)
1610 GstAudioAggregatorPad *aapad = GST_AUDIO_AGGREGATOR_PAD (pad);
1611 GstAggregatorPad *bpad = GST_AGGREGATOR_PAD_CAST (pad);
1612 GstClockTime timestamp, stream_time;
1614 if (aapad->priv->buffer == NULL)
1617 timestamp = GST_BUFFER_PTS (aapad->priv->buffer);
1618 GST_OBJECT_LOCK (bpad);
1619 stream_time = gst_segment_to_stream_time (&bpad->segment, GST_FORMAT_TIME,
1621 GST_OBJECT_UNLOCK (bpad);
1623 /* sync object properties on stream time */
1624 /* TODO: Ideally we would want to do that on every sample */
1625 if (GST_CLOCK_TIME_IS_VALID (stream_time))
1626 gst_object_sync_values (GST_OBJECT_CAST (pad), stream_time);
1631 static GstFlowReturn
1632 gst_audio_aggregator_aggregate (GstAggregator * agg, gboolean timeout)
1634 /* Calculate the current output offset/timestamp and offset_end/timestamp_end.
1635 * Allocate a silence buffer for this and store it.
1638 * 1) Once per input buffer (cached)
1639 * 1) Check discont (flag and timestamp with tolerance)
1640 * 2) If discont or new, resync. That means:
1641 * 1) Drop all start data of the buffer that comes before
1642 * the current position/offset.
1643 * 2) Calculate the offset (output segment!) that the first
1644 * frame of the input buffer corresponds to. Base this on
1647 * 2) If the current pad's offset/offset_end overlaps with the output
1648 * offset/offset_end, mix it at the appropiate position in the output
1649 * buffer and advance the pad's position. Remember if this pad needs
1650 * a new buffer to advance behind the output offset_end.
1652 * If we had no pad with a buffer, go EOS.
1654 * If we had at least one pad that did not advance behind output
1655 * offset_end, let aggregate be called again for the current
1656 * output offset/offset_end.
1658 GstElement *element;
1659 GstAudioAggregator *aagg;
1662 GstBuffer *outbuf = NULL;
1664 gint64 next_timestamp;
1666 gboolean dropped = FALSE;
1667 gboolean is_eos = TRUE;
1668 gboolean is_done = TRUE;
1670 GstAudioAggregatorPad *srcpad = GST_AUDIO_AGGREGATOR_PAD (agg->srcpad);
1672 element = GST_ELEMENT (agg);
1673 aagg = GST_AUDIO_AGGREGATOR (agg);
1675 /* Sync pad properties to the stream time */
1676 gst_element_foreach_sink_pad (element, sync_pad_values, NULL);
1678 GST_AUDIO_AGGREGATOR_LOCK (aagg);
1679 GST_OBJECT_LOCK (agg);
1681 /* Update position from the segment start/stop if needed */
1682 if (agg->segment.position == -1) {
1683 if (agg->segment.rate > 0.0)
1684 agg->segment.position = agg->segment.start;
1686 agg->segment.position = agg->segment.stop;
1689 if (G_UNLIKELY (srcpad->info.finfo->format == GST_AUDIO_FORMAT_UNKNOWN)) {
1691 GST_DEBUG_OBJECT (aagg,
1692 "Got timeout before receiving any caps, don't output anything");
1694 /* Advance position */
1695 if (agg->segment.rate > 0.0)
1696 agg->segment.position += aagg->priv->output_buffer_duration;
1697 else if (agg->segment.position > aagg->priv->output_buffer_duration)
1698 agg->segment.position -= aagg->priv->output_buffer_duration;
1700 agg->segment.position = 0;
1702 GST_OBJECT_UNLOCK (agg);
1703 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1704 return GST_AGGREGATOR_FLOW_NEED_DATA;
1706 GST_OBJECT_UNLOCK (agg);
1707 goto not_negotiated;
1711 rate = GST_AUDIO_INFO_RATE (&srcpad->info);
1712 bpf = GST_AUDIO_INFO_BPF (&srcpad->info);
1714 if (aagg->priv->offset == -1) {
1715 aagg->priv->offset =
1716 gst_util_uint64_scale (agg->segment.position - agg->segment.start, rate,
1718 GST_DEBUG_OBJECT (aagg, "Starting at offset %" G_GINT64_FORMAT,
1719 aagg->priv->offset);
1722 blocksize = gst_util_uint64_scale (aagg->priv->output_buffer_duration,
1724 blocksize = MAX (1, blocksize);
1726 /* FIXME: Reverse mixing does not work at all yet */
1727 if (agg->segment.rate > 0.0) {
1728 next_offset = aagg->priv->offset + blocksize;
1730 next_offset = aagg->priv->offset - blocksize;
1733 /* Use the sample counter, which will never accumulate rounding errors */
1735 agg->segment.start + gst_util_uint64_scale (next_offset, GST_SECOND,
1738 if (aagg->priv->current_buffer == NULL) {
1739 GST_OBJECT_UNLOCK (agg);
1740 aagg->priv->current_buffer =
1741 GST_AUDIO_AGGREGATOR_GET_CLASS (aagg)->create_output_buffer (aagg,
1743 /* Be careful, some things could have changed ? */
1744 GST_OBJECT_LOCK (agg);
1745 GST_BUFFER_FLAG_SET (aagg->priv->current_buffer, GST_BUFFER_FLAG_GAP);
1747 outbuf = aagg->priv->current_buffer;
1749 GST_LOG_OBJECT (agg,
1750 "Starting to mix %u samples for offset %" G_GINT64_FORMAT
1751 " with timestamp %" GST_TIME_FORMAT, blocksize,
1752 aagg->priv->offset, GST_TIME_ARGS (agg->segment.position));
1754 for (iter = element->sinkpads; iter; iter = iter->next) {
1755 GstAudioAggregatorPad *pad = (GstAudioAggregatorPad *) iter->data;
1756 GstAggregatorPad *aggpad = (GstAggregatorPad *) iter->data;
1757 gboolean pad_eos = gst_aggregator_pad_is_eos (aggpad);
1762 pad->priv->input_buffer = gst_aggregator_pad_peek_buffer (aggpad);
1764 GST_OBJECT_LOCK (pad);
1765 if (!pad->priv->input_buffer) {
1767 if (pad->priv->output_offset < next_offset) {
1768 gint64 diff = next_offset - pad->priv->output_offset;
1769 GST_DEBUG_OBJECT (pad, "Timeout, missing %" G_GINT64_FORMAT
1770 " frames (%" GST_TIME_FORMAT ")", diff,
1771 GST_TIME_ARGS (gst_util_uint64_scale (diff, GST_SECOND,
1772 GST_AUDIO_INFO_RATE (&srcpad->info))));
1774 } else if (!pad_eos) {
1777 GST_OBJECT_UNLOCK (pad);
1782 if (!pad->priv->buffer) {
1783 if (GST_AUDIO_AGGREGATOR_PAD_GET_CLASS (pad)->convert_buffer)
1785 gst_audio_aggregator_convert_buffer
1786 (aagg, GST_PAD (pad), &pad->info, &srcpad->info,
1787 pad->priv->input_buffer);
1789 pad->priv->buffer = gst_buffer_ref (pad->priv->input_buffer);
1791 if (!gst_audio_aggregator_fill_buffer (aagg, pad)) {
1792 gst_buffer_replace (&pad->priv->buffer, NULL);
1793 gst_buffer_replace (&pad->priv->input_buffer, NULL);
1794 pad->priv->buffer = NULL;
1796 GST_OBJECT_UNLOCK (pad);
1798 gst_aggregator_pad_drop_buffer (aggpad);
1802 gst_buffer_unref (pad->priv->input_buffer);
1805 if (!pad->priv->buffer && !dropped && pad_eos) {
1806 GST_DEBUG_OBJECT (aggpad, "Pad is in EOS state");
1807 GST_OBJECT_UNLOCK (pad);
1811 g_assert (pad->priv->buffer);
1813 /* This pad is lagging behind, we need to update the offset
1814 * and maybe drop the current buffer */
1815 if (pad->priv->output_offset < aagg->priv->offset) {
1816 gint64 diff = aagg->priv->offset - pad->priv->output_offset;
1817 gint64 odiff = diff;
1819 if (pad->priv->position + diff > pad->priv->size)
1820 diff = pad->priv->size - pad->priv->position;
1821 pad->priv->position += diff;
1822 pad->priv->output_offset += diff;
1824 if (pad->priv->position == pad->priv->size) {
1825 GST_DEBUG_OBJECT (pad, "Buffer was late by %" GST_TIME_FORMAT
1826 ", dropping %" GST_PTR_FORMAT,
1827 GST_TIME_ARGS (gst_util_uint64_scale (odiff, GST_SECOND,
1828 GST_AUDIO_INFO_RATE (&srcpad->info))), pad->priv->buffer);
1829 /* Buffer done, drop it */
1830 gst_buffer_replace (&pad->priv->buffer, NULL);
1831 gst_buffer_replace (&pad->priv->input_buffer, NULL);
1833 GST_OBJECT_UNLOCK (pad);
1834 gst_aggregator_pad_drop_buffer (aggpad);
1839 g_assert (pad->priv->buffer);
1841 if (pad->priv->output_offset >= aagg->priv->offset
1842 && pad->priv->output_offset < aagg->priv->offset + blocksize) {
1845 GST_LOG_OBJECT (aggpad, "Mixing buffer for current offset");
1846 drop_buf = !gst_audio_aggregator_mix_buffer (aagg, pad, pad->priv->buffer,
1848 if (pad->priv->output_offset >= next_offset) {
1849 GST_LOG_OBJECT (pad,
1850 "Pad is at or after current offset: %" G_GUINT64_FORMAT " >= %"
1851 G_GINT64_FORMAT, pad->priv->output_offset, next_offset);
1856 GST_OBJECT_UNLOCK (pad);
1857 gst_aggregator_pad_drop_buffer (aggpad);
1862 GST_OBJECT_UNLOCK (pad);
1864 GST_OBJECT_UNLOCK (agg);
1867 /* We dropped a buffer, retry */
1868 GST_LOG_OBJECT (aagg, "A pad dropped a buffer, wait for the next one");
1869 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1870 return GST_AGGREGATOR_FLOW_NEED_DATA;
1873 if (!is_done && !is_eos) {
1874 /* Get more buffers */
1875 GST_LOG_OBJECT (aagg,
1876 "We're not done yet for the current offset, waiting for more data");
1877 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1878 return GST_AGGREGATOR_FLOW_NEED_DATA;
1882 gint64 max_offset = 0;
1884 GST_DEBUG_OBJECT (aagg, "We're EOS");
1886 GST_OBJECT_LOCK (agg);
1887 for (iter = GST_ELEMENT (agg)->sinkpads; iter; iter = iter->next) {
1888 GstAudioAggregatorPad *pad = GST_AUDIO_AGGREGATOR_PAD (iter->data);
1890 max_offset = MAX ((gint64) max_offset, (gint64) pad->priv->output_offset);
1892 GST_OBJECT_UNLOCK (agg);
1894 /* This means EOS or nothing mixed in at all */
1895 if (aagg->priv->offset == max_offset) {
1896 gst_buffer_replace (&aagg->priv->current_buffer, NULL);
1897 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1898 return GST_FLOW_EOS;
1901 if (max_offset <= next_offset) {
1902 GST_DEBUG_OBJECT (aagg,
1903 "Last buffer is incomplete: %" G_GUINT64_FORMAT " <= %"
1904 G_GINT64_FORMAT, max_offset, next_offset);
1905 next_offset = max_offset;
1907 agg->segment.start + gst_util_uint64_scale (next_offset, GST_SECOND,
1910 if (next_offset > aagg->priv->offset)
1911 gst_buffer_resize (outbuf, 0, (next_offset - aagg->priv->offset) * bpf);
1915 /* set timestamps on the output buffer */
1916 GST_OBJECT_LOCK (agg);
1917 if (agg->segment.rate > 0.0) {
1918 GST_BUFFER_PTS (outbuf) = agg->segment.position;
1919 GST_BUFFER_OFFSET (outbuf) = aagg->priv->offset;
1920 GST_BUFFER_OFFSET_END (outbuf) = next_offset;
1921 GST_BUFFER_DURATION (outbuf) = next_timestamp - agg->segment.position;
1923 GST_BUFFER_PTS (outbuf) = next_timestamp;
1924 GST_BUFFER_OFFSET (outbuf) = next_offset;
1925 GST_BUFFER_OFFSET_END (outbuf) = aagg->priv->offset;
1926 GST_BUFFER_DURATION (outbuf) = agg->segment.position - next_timestamp;
1929 GST_OBJECT_UNLOCK (agg);
1932 GST_LOG_OBJECT (aagg,
1933 "pushing outbuf %p, timestamp %" GST_TIME_FORMAT " offset %"
1934 G_GINT64_FORMAT, outbuf, GST_TIME_ARGS (GST_BUFFER_PTS (outbuf)),
1935 GST_BUFFER_OFFSET (outbuf));
1937 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1939 ret = gst_aggregator_finish_buffer (agg, outbuf);
1940 aagg->priv->current_buffer = NULL;
1942 GST_LOG_OBJECT (aagg, "pushed outbuf, result = %s", gst_flow_get_name (ret));
1944 GST_AUDIO_AGGREGATOR_LOCK (aagg);
1945 GST_OBJECT_LOCK (agg);
1946 aagg->priv->offset = next_offset;
1947 agg->segment.position = next_timestamp;
1949 /* If there was a timeout and there was a gap in data in out of the streams,
1950 * then it's a very good time to for a resync with the timestamps.
1953 for (iter = element->sinkpads; iter; iter = iter->next) {
1954 GstAudioAggregatorPad *pad = GST_AUDIO_AGGREGATOR_PAD (iter->data);
1956 GST_OBJECT_LOCK (pad);
1957 if (pad->priv->output_offset < aagg->priv->offset)
1958 pad->priv->output_offset = -1;
1959 GST_OBJECT_UNLOCK (pad);
1962 GST_OBJECT_UNLOCK (agg);
1963 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1969 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1970 GST_ELEMENT_ERROR (aagg, STREAM, FORMAT, (NULL),
1971 ("Unknown data received, not negotiated"));
1972 return GST_FLOW_NOT_NEGOTIATED;