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 * @short_description: manages a set of pads with the purpose of
29 * aggregating their buffers for raw audio
30 * @see_also: #GstAggregator
32 * #GstAudioAggregator will perform conversion on the data arriving
33 * on its sink pads, based on the format expected downstream.
35 * Subclasses can opt out of the conversion behaviour by setting
36 * #GstAudioAggregator.convert_buffer() to %NULL.
38 * Subclasses that wish to use the default conversion implementation
39 * should use a (subclass of) #GstAudioAggregatorConvertPad as their
40 * #GstAggregatorClass.sinkpads_type, as it will cache the created
41 * #GstAudioConverter and install a property allowing to configure it,
42 * #GstAudioAggregatorPadClass:converter-config.
44 * Subclasses that wish to perform custom conversion should override
45 * #GstAudioAggregator.convert_buffer().
47 * When conversion is enabled, #GstAudioAggregator will accept
48 * any type of raw audio caps and perform conversion
49 * on the data arriving on its sink pads, with whatever downstream
50 * expects as the target format.
52 * In case downstream caps are not fully fixated, it will use
53 * the first configured sink pad to finish fixating its source pad
56 * Additionally, handling audio conversion directly in the element
57 * means that this base class supports safely reconfiguring its
60 * A notable exception for now is the sample rate, sink pads must
61 * have the same sample rate as either the downstream requirement,
62 * or the first configured pad, or a combination of both (when
63 * downstream specifies a range or a set of acceptable rates).
71 #include "gstaudioaggregator.h"
75 GST_DEBUG_CATEGORY_STATIC (audio_aggregator_debug);
76 #define GST_CAT_DEFAULT audio_aggregator_debug
78 struct _GstAudioAggregatorPadPrivate
80 /* All members are protected by the pad object lock */
82 GstBuffer *buffer; /* current buffer we're mixing, for
83 comparison with a new input buffer from
84 aggregator to see if we need to update our
87 guint position, size; /* position in the input buffer and size of the
88 input buffer in number of samples */
90 GstBuffer *input_buffer;
92 guint64 output_offset; /* Sample offset in output segment relative to
93 pad.segment.start that position refers to
94 in the current buffer. */
96 guint64 next_offset; /* Next expected sample offset relative to
99 /* Last time we noticed a discont */
100 GstClockTime discont_time;
102 /* A new unhandled segment event has been received */
103 gboolean new_segment;
107 /*****************************************
108 * GstAudioAggregatorPad implementation *
109 *****************************************/
110 G_DEFINE_TYPE (GstAudioAggregatorPad, gst_audio_aggregator_pad,
111 GST_TYPE_AGGREGATOR_PAD);
116 PROP_PAD_CONVERTER_CONFIG,
120 gst_audio_aggregator_pad_flush_pad (GstAggregatorPad * aggpad,
121 GstAggregator * aggregator);
124 gst_audio_aggregator_pad_finalize (GObject * object)
126 GstAudioAggregatorPad *pad = (GstAudioAggregatorPad *) object;
128 gst_buffer_replace (&pad->priv->buffer, NULL);
129 gst_buffer_replace (&pad->priv->input_buffer, NULL);
131 G_OBJECT_CLASS (gst_audio_aggregator_pad_parent_class)->finalize (object);
135 gst_audio_aggregator_pad_class_init (GstAudioAggregatorPadClass * klass)
137 GObjectClass *gobject_class = (GObjectClass *) klass;
138 GstAggregatorPadClass *aggpadclass = (GstAggregatorPadClass *) klass;
140 g_type_class_add_private (klass, sizeof (GstAudioAggregatorPadPrivate));
142 gobject_class->finalize = gst_audio_aggregator_pad_finalize;
143 aggpadclass->flush = GST_DEBUG_FUNCPTR (gst_audio_aggregator_pad_flush_pad);
147 gst_audio_aggregator_pad_init (GstAudioAggregatorPad * pad)
150 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AUDIO_AGGREGATOR_PAD,
151 GstAudioAggregatorPadPrivate);
153 gst_audio_info_init (&pad->info);
155 pad->priv->buffer = NULL;
156 pad->priv->input_buffer = NULL;
157 pad->priv->position = 0;
159 pad->priv->output_offset = -1;
160 pad->priv->next_offset = -1;
161 pad->priv->discont_time = GST_CLOCK_TIME_NONE;
166 gst_audio_aggregator_pad_flush_pad (GstAggregatorPad * aggpad,
167 GstAggregator * aggregator)
169 GstAudioAggregatorPad *pad = GST_AUDIO_AGGREGATOR_PAD (aggpad);
171 GST_OBJECT_LOCK (aggpad);
172 pad->priv->position = pad->priv->size = 0;
173 pad->priv->output_offset = pad->priv->next_offset = -1;
174 pad->priv->discont_time = GST_CLOCK_TIME_NONE;
175 gst_buffer_replace (&pad->priv->buffer, NULL);
176 gst_buffer_replace (&pad->priv->input_buffer, NULL);
177 GST_OBJECT_UNLOCK (aggpad);
182 struct _GstAudioAggregatorConvertPadPrivate
184 /* All members are protected by the pad object lock */
185 GstAudioConverter *converter;
186 GstStructure *converter_config;
187 gboolean converter_config_changed;
191 G_DEFINE_TYPE (GstAudioAggregatorConvertPad, gst_audio_aggregator_convert_pad,
192 GST_TYPE_AUDIO_AGGREGATOR_PAD);
195 gst_audio_aggregator_convert_pad_update_converter (GstAudioAggregatorConvertPad
196 * aaggcpad, GstAudioInfo * in_info, GstAudioInfo * out_info)
198 if (!aaggcpad->priv->converter_config_changed)
201 if (aaggcpad->priv->converter) {
202 gst_audio_converter_free (aaggcpad->priv->converter);
203 aaggcpad->priv->converter = NULL;
206 if (gst_audio_info_is_equal (in_info, out_info) ||
207 in_info->finfo->format == GST_AUDIO_FORMAT_UNKNOWN) {
208 if (aaggcpad->priv->converter) {
209 gst_audio_converter_free (aaggcpad->priv->converter);
210 aaggcpad->priv->converter = NULL;
213 /* If we haven't received caps yet, this pad should not have
214 * a buffer to convert anyway */
215 aaggcpad->priv->converter =
216 gst_audio_converter_new (GST_AUDIO_CONVERTER_FLAG_NONE,
218 aaggcpad->priv->converter_config ? gst_structure_copy (aaggcpad->
219 priv->converter_config) : NULL);
222 aaggcpad->priv->converter_config_changed = FALSE;
226 gst_audio_aggregator_convert_pad_convert_buffer (GstAudioAggregatorConvertPad *
227 aaggcpad, GstAudioInfo * in_info, GstAudioInfo * out_info,
228 GstBuffer * input_buffer)
232 gst_audio_aggregator_convert_pad_update_converter (aaggcpad, in_info,
235 if (aaggcpad->priv->converter) {
236 gint insize = gst_buffer_get_size (input_buffer);
237 gsize insamples = insize / in_info->bpf;
239 gst_audio_converter_get_out_frames (aaggcpad->priv->converter,
241 gint outsize = outsamples * out_info->bpf;
242 GstMapInfo inmap, outmap;
244 res = gst_buffer_new_allocate (NULL, outsize, NULL);
246 /* We create a perfectly similar buffer, except obviously for
247 * its converted contents */
248 gst_buffer_copy_into (res, input_buffer,
249 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
250 GST_BUFFER_COPY_META, 0, -1);
252 gst_buffer_map (input_buffer, &inmap, GST_MAP_READ);
253 gst_buffer_map (res, &outmap, GST_MAP_WRITE);
255 gst_audio_converter_samples (aaggcpad->priv->converter,
256 GST_AUDIO_CONVERTER_FLAG_NONE,
257 (gpointer *) & inmap.data, insamples,
258 (gpointer *) & outmap.data, outsamples);
260 gst_buffer_unmap (input_buffer, &inmap);
261 gst_buffer_unmap (res, &outmap);
263 res = gst_buffer_ref (input_buffer);
270 gst_audio_aggregator_convert_pad_finalize (GObject * object)
272 GstAudioAggregatorConvertPad *pad = (GstAudioAggregatorConvertPad *) object;
274 if (pad->priv->converter)
275 gst_audio_converter_free (pad->priv->converter);
277 if (pad->priv->converter_config)
278 gst_structure_free (pad->priv->converter_config);
280 G_OBJECT_CLASS (gst_audio_aggregator_convert_pad_parent_class)->finalize
285 gst_audio_aggregator_convert_pad_get_property (GObject * object, guint prop_id,
286 GValue * value, GParamSpec * pspec)
288 GstAudioAggregatorConvertPad *pad = GST_AUDIO_AGGREGATOR_CONVERT_PAD (object);
291 case PROP_PAD_CONVERTER_CONFIG:
292 GST_OBJECT_LOCK (pad);
293 if (pad->priv->converter_config)
294 g_value_set_boxed (value, pad->priv->converter_config);
295 GST_OBJECT_UNLOCK (pad);
298 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
304 gst_audio_aggregator_convert_pad_set_property (GObject * object, guint prop_id,
305 const GValue * value, GParamSpec * pspec)
307 GstAudioAggregatorConvertPad *pad = GST_AUDIO_AGGREGATOR_CONVERT_PAD (object);
310 case PROP_PAD_CONVERTER_CONFIG:
311 GST_OBJECT_LOCK (pad);
312 if (pad->priv->converter_config)
313 gst_structure_free (pad->priv->converter_config);
314 pad->priv->converter_config = g_value_dup_boxed (value);
315 pad->priv->converter_config_changed = TRUE;
316 GST_OBJECT_UNLOCK (pad);
319 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
325 gst_audio_aggregator_convert_pad_class_init (GstAudioAggregatorConvertPadClass *
328 GObjectClass *gobject_class = (GObjectClass *) klass;
329 g_type_class_add_private (klass,
330 sizeof (GstAudioAggregatorConvertPadPrivate));
332 gobject_class->set_property = gst_audio_aggregator_convert_pad_set_property;
333 gobject_class->get_property = gst_audio_aggregator_convert_pad_get_property;
335 g_object_class_install_property (gobject_class, PROP_PAD_CONVERTER_CONFIG,
336 g_param_spec_boxed ("converter-config", "Converter configuration",
337 "A GstStructure describing the configuration that should be used "
338 "when converting this pad's audio buffers",
339 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
341 gobject_class->finalize = gst_audio_aggregator_convert_pad_finalize;
345 gst_audio_aggregator_convert_pad_init (GstAudioAggregatorConvertPad * pad)
348 G_TYPE_INSTANCE_GET_PRIVATE (pad, GST_TYPE_AUDIO_AGGREGATOR_CONVERT_PAD,
349 GstAudioAggregatorConvertPadPrivate);
352 /**************************************
353 * GstAudioAggregator implementation *
354 **************************************/
356 struct _GstAudioAggregatorPrivate
360 /* All three properties are unprotected, can't be modified while streaming */
361 /* Size in frames that is output per buffer */
362 GstClockTime output_buffer_duration;
363 GstClockTime alignment_threshold;
364 GstClockTime discont_wait;
366 /* Protected by srcpad stream clock */
367 /* Output buffer starting at offset containing blocksize frames (calculated
368 * from output_buffer_duration) */
369 GstBuffer *current_buffer;
371 /* counters to keep track of timestamps */
372 /* Readable with object lock, writable with both aag lock and object lock */
374 /* Sample offset starting from 0 at aggregator.segment.start */
378 #define GST_AUDIO_AGGREGATOR_LOCK(self) g_mutex_lock (&(self)->priv->mutex);
379 #define GST_AUDIO_AGGREGATOR_UNLOCK(self) g_mutex_unlock (&(self)->priv->mutex);
381 static void gst_audio_aggregator_set_property (GObject * object, guint prop_id,
382 const GValue * value, GParamSpec * pspec);
383 static void gst_audio_aggregator_get_property (GObject * object, guint prop_id,
384 GValue * value, GParamSpec * pspec);
385 static void gst_audio_aggregator_dispose (GObject * object);
387 static gboolean gst_audio_aggregator_src_event (GstAggregator * agg,
389 static gboolean gst_audio_aggregator_sink_event (GstAggregator * agg,
390 GstAggregatorPad * aggpad, GstEvent * event);
391 static gboolean gst_audio_aggregator_src_query (GstAggregator * agg,
394 gst_audio_aggregator_sink_query (GstAggregator * agg, GstAggregatorPad * aggpad,
396 static gboolean gst_audio_aggregator_start (GstAggregator * agg);
397 static gboolean gst_audio_aggregator_stop (GstAggregator * agg);
398 static GstFlowReturn gst_audio_aggregator_flush (GstAggregator * agg);
400 static GstBuffer *gst_audio_aggregator_create_output_buffer (GstAudioAggregator
401 * aagg, guint num_frames);
402 static GstBuffer *gst_audio_aggregator_do_clip (GstAggregator * agg,
403 GstAggregatorPad * bpad, GstBuffer * buffer);
404 static GstFlowReturn gst_audio_aggregator_aggregate (GstAggregator * agg,
406 static gboolean sync_pad_values (GstElement * aagg, GstPad * pad, gpointer ud);
407 static gboolean gst_audio_aggregator_negotiated_src_caps (GstAggregator * agg,
410 gst_audio_aggregator_update_src_caps (GstAggregator * agg,
411 GstCaps * caps, GstCaps ** ret);
412 static GstCaps *gst_audio_aggregator_fixate_src_caps (GstAggregator * agg,
415 #define DEFAULT_OUTPUT_BUFFER_DURATION (10 * GST_MSECOND)
416 #define DEFAULT_ALIGNMENT_THRESHOLD (40 * GST_MSECOND)
417 #define DEFAULT_DISCONT_WAIT (1 * GST_SECOND)
422 PROP_OUTPUT_BUFFER_DURATION,
423 PROP_ALIGNMENT_THRESHOLD,
427 G_DEFINE_ABSTRACT_TYPE (GstAudioAggregator, gst_audio_aggregator,
428 GST_TYPE_AGGREGATOR);
431 gst_audio_aggregator_get_next_time (GstAggregator * agg)
433 GstClockTime next_time;
435 GST_OBJECT_LOCK (agg);
436 if (agg->segment.position == -1 || agg->segment.position < agg->segment.start)
437 next_time = agg->segment.start;
439 next_time = agg->segment.position;
441 if (agg->segment.stop != -1 && next_time > agg->segment.stop)
442 next_time = agg->segment.stop;
445 gst_segment_to_running_time (&agg->segment, GST_FORMAT_TIME, next_time);
446 GST_OBJECT_UNLOCK (agg);
452 gst_audio_aggregator_convert_once (GstAudioAggregator * aagg, GstPad * pad,
453 GstAudioInfo * in_info, GstAudioInfo * out_info, GstBuffer * buffer)
455 GstAudioConverter *converter =
456 gst_audio_converter_new (GST_AUDIO_CONVERTER_FLAG_NONE,
457 in_info, out_info, NULL);
458 gint insize = gst_buffer_get_size (buffer);
459 gsize insamples = insize / in_info->bpf;
460 gsize outsamples = gst_audio_converter_get_out_frames (converter,
462 gint outsize = outsamples * out_info->bpf;
463 GstMapInfo inmap, outmap;
464 GstBuffer *converted = gst_buffer_new_allocate (NULL, outsize, NULL);
466 gst_buffer_copy_into (converted, buffer,
467 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
468 GST_BUFFER_COPY_META, 0, -1);
470 gst_buffer_map (buffer, &inmap, GST_MAP_READ);
471 gst_buffer_map (converted, &outmap, GST_MAP_WRITE);
473 gst_audio_converter_samples (converter,
474 GST_AUDIO_CONVERTER_FLAG_NONE,
475 (gpointer *) & inmap.data, insamples,
476 (gpointer *) & outmap.data, outsamples);
478 gst_buffer_unmap (buffer, &inmap);
479 gst_buffer_unmap (converted, &outmap);
480 gst_audio_converter_free (converter);
486 gst_audio_aggregator_default_convert_buffer (GstAudioAggregator * aagg,
487 GstPad * pad, GstAudioInfo * in_info, GstAudioInfo * out_info,
490 if (GST_IS_AUDIO_AGGREGATOR_CONVERT_PAD (pad))
492 gst_audio_aggregator_convert_pad_convert_buffer
493 (GST_AUDIO_AGGREGATOR_CONVERT_PAD (pad),
494 &GST_AUDIO_AGGREGATOR_PAD (pad)->info, out_info, buffer);
496 return gst_audio_aggregator_convert_once (aagg, pad, in_info, out_info,
501 gst_audio_aggregator_convert_buffer (GstAudioAggregator * aagg, GstPad * pad,
502 GstAudioInfo * in_info, GstAudioInfo * out_info, GstBuffer * buffer)
504 GstAudioAggregatorClass *klass = GST_AUDIO_AGGREGATOR_GET_CLASS (aagg);
506 g_assert (klass->convert_buffer);
508 return klass->convert_buffer (aagg, pad, in_info, out_info, buffer);
512 gst_audio_aggregator_class_init (GstAudioAggregatorClass * klass)
514 GObjectClass *gobject_class = (GObjectClass *) klass;
515 GstAggregatorClass *gstaggregator_class = (GstAggregatorClass *) klass;
517 g_type_class_add_private (klass, sizeof (GstAudioAggregatorPrivate));
519 gobject_class->set_property = gst_audio_aggregator_set_property;
520 gobject_class->get_property = gst_audio_aggregator_get_property;
521 gobject_class->dispose = gst_audio_aggregator_dispose;
523 gstaggregator_class->src_event =
524 GST_DEBUG_FUNCPTR (gst_audio_aggregator_src_event);
525 gstaggregator_class->sink_event =
526 GST_DEBUG_FUNCPTR (gst_audio_aggregator_sink_event);
527 gstaggregator_class->src_query =
528 GST_DEBUG_FUNCPTR (gst_audio_aggregator_src_query);
529 gstaggregator_class->sink_query = gst_audio_aggregator_sink_query;
530 gstaggregator_class->start = gst_audio_aggregator_start;
531 gstaggregator_class->stop = gst_audio_aggregator_stop;
532 gstaggregator_class->flush = gst_audio_aggregator_flush;
533 gstaggregator_class->aggregate =
534 GST_DEBUG_FUNCPTR (gst_audio_aggregator_aggregate);
535 gstaggregator_class->clip = GST_DEBUG_FUNCPTR (gst_audio_aggregator_do_clip);
536 gstaggregator_class->get_next_time = gst_audio_aggregator_get_next_time;
537 gstaggregator_class->update_src_caps =
538 GST_DEBUG_FUNCPTR (gst_audio_aggregator_update_src_caps);
539 gstaggregator_class->fixate_src_caps = gst_audio_aggregator_fixate_src_caps;
540 gstaggregator_class->negotiated_src_caps =
541 gst_audio_aggregator_negotiated_src_caps;
543 klass->create_output_buffer = gst_audio_aggregator_create_output_buffer;
544 klass->convert_buffer = gst_audio_aggregator_default_convert_buffer;
546 GST_DEBUG_CATEGORY_INIT (audio_aggregator_debug, "audioaggregator",
547 GST_DEBUG_FG_MAGENTA, "GstAudioAggregator");
549 g_object_class_install_property (gobject_class, PROP_OUTPUT_BUFFER_DURATION,
550 g_param_spec_uint64 ("output-buffer-duration", "Output Buffer Duration",
551 "Output block size in nanoseconds", 1,
552 G_MAXUINT64, DEFAULT_OUTPUT_BUFFER_DURATION,
553 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
555 g_object_class_install_property (gobject_class, PROP_ALIGNMENT_THRESHOLD,
556 g_param_spec_uint64 ("alignment-threshold", "Alignment Threshold",
557 "Timestamp alignment threshold in nanoseconds", 0,
558 G_MAXUINT64 - 1, DEFAULT_ALIGNMENT_THRESHOLD,
559 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
561 g_object_class_install_property (gobject_class, PROP_DISCONT_WAIT,
562 g_param_spec_uint64 ("discont-wait", "Discont Wait",
563 "Window of time in nanoseconds to wait before "
564 "creating a discontinuity", 0,
565 G_MAXUINT64 - 1, DEFAULT_DISCONT_WAIT,
566 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
570 gst_audio_aggregator_init (GstAudioAggregator * aagg)
573 G_TYPE_INSTANCE_GET_PRIVATE (aagg, GST_TYPE_AUDIO_AGGREGATOR,
574 GstAudioAggregatorPrivate);
576 g_mutex_init (&aagg->priv->mutex);
578 aagg->priv->output_buffer_duration = DEFAULT_OUTPUT_BUFFER_DURATION;
579 aagg->priv->alignment_threshold = DEFAULT_ALIGNMENT_THRESHOLD;
580 aagg->priv->discont_wait = DEFAULT_DISCONT_WAIT;
582 aagg->current_caps = NULL;
583 gst_audio_info_init (&aagg->info);
585 gst_aggregator_set_latency (GST_AGGREGATOR (aagg),
586 aagg->priv->output_buffer_duration, aagg->priv->output_buffer_duration);
590 gst_audio_aggregator_dispose (GObject * object)
592 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (object);
594 gst_caps_replace (&aagg->current_caps, NULL);
596 g_mutex_clear (&aagg->priv->mutex);
598 G_OBJECT_CLASS (gst_audio_aggregator_parent_class)->dispose (object);
602 gst_audio_aggregator_set_property (GObject * object, guint prop_id,
603 const GValue * value, GParamSpec * pspec)
605 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (object);
608 case PROP_OUTPUT_BUFFER_DURATION:
609 aagg->priv->output_buffer_duration = g_value_get_uint64 (value);
610 gst_aggregator_set_latency (GST_AGGREGATOR (aagg),
611 aagg->priv->output_buffer_duration,
612 aagg->priv->output_buffer_duration);
614 case PROP_ALIGNMENT_THRESHOLD:
615 aagg->priv->alignment_threshold = g_value_get_uint64 (value);
617 case PROP_DISCONT_WAIT:
618 aagg->priv->discont_wait = g_value_get_uint64 (value);
621 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
627 gst_audio_aggregator_get_property (GObject * object, guint prop_id,
628 GValue * value, GParamSpec * pspec)
630 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (object);
633 case PROP_OUTPUT_BUFFER_DURATION:
634 g_value_set_uint64 (value, aagg->priv->output_buffer_duration);
636 case PROP_ALIGNMENT_THRESHOLD:
637 g_value_set_uint64 (value, aagg->priv->alignment_threshold);
639 case PROP_DISCONT_WAIT:
640 g_value_set_uint64 (value, aagg->priv->discont_wait);
643 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
648 /* Caps negotiation */
650 /* Unref after usage */
651 static GstAudioAggregatorPad *
652 gst_audio_aggregator_get_first_configured_pad (GstAggregator * agg)
654 GstAudioAggregatorPad *res = NULL;
657 GST_OBJECT_LOCK (agg);
658 for (l = GST_ELEMENT (agg)->sinkpads; l; l = l->next) {
659 GstAudioAggregatorPad *aaggpad = l->data;
661 if (GST_AUDIO_INFO_FORMAT (&aaggpad->info) != GST_AUDIO_FORMAT_UNKNOWN) {
662 res = gst_object_ref (aaggpad);
666 GST_OBJECT_UNLOCK (agg);
672 gst_audio_aggregator_sink_getcaps (GstPad * pad, GstAggregator * agg,
675 GstAudioAggregatorPad *first_configured_pad =
676 gst_audio_aggregator_get_first_configured_pad (agg);
677 GstCaps *sink_template_caps = gst_pad_get_pad_template_caps (pad);
678 GstCaps *downstream_caps = gst_pad_get_allowed_caps (agg->srcpad);
680 GstStructure *s, *s2;
681 gint downstream_rate;
683 sink_template_caps = gst_caps_make_writable (sink_template_caps);
684 s = gst_caps_get_structure (sink_template_caps, 0);
686 if (downstream_caps && !gst_caps_is_empty (downstream_caps))
687 s2 = gst_caps_get_structure (downstream_caps, 0);
691 if (s2 && gst_structure_get_int (s2, "rate", &downstream_rate)) {
692 gst_structure_fixate_field_nearest_int (s, "rate", downstream_rate);
693 } else if (first_configured_pad) {
694 gst_structure_fixate_field_nearest_int (s, "rate",
695 first_configured_pad->info.rate);
698 if (first_configured_pad)
699 gst_object_unref (first_configured_pad);
701 sink_caps = filter ? gst_caps_intersect (sink_template_caps,
702 filter) : gst_caps_ref (sink_template_caps);
704 GST_INFO_OBJECT (pad, "Getting caps with filter %" GST_PTR_FORMAT, filter);
705 GST_DEBUG_OBJECT (pad, "sink template caps : %" GST_PTR_FORMAT,
707 GST_DEBUG_OBJECT (pad, "downstream caps %" GST_PTR_FORMAT, downstream_caps);
708 GST_INFO_OBJECT (pad, "returned sink caps : %" GST_PTR_FORMAT, sink_caps);
710 gst_caps_unref (sink_template_caps);
713 gst_caps_unref (downstream_caps);
719 gst_audio_aggregator_sink_setcaps (GstAudioAggregatorPad * aaggpad,
720 GstAggregator * agg, GstCaps * caps)
722 GstAudioAggregatorPad *first_configured_pad =
723 gst_audio_aggregator_get_first_configured_pad (agg);
724 GstCaps *downstream_caps = gst_pad_get_allowed_caps (agg->srcpad);
727 gint downstream_rate;
730 if (!downstream_caps || gst_caps_is_empty (downstream_caps)) {
735 gst_audio_info_from_caps (&info, caps);
736 s = gst_caps_get_structure (downstream_caps, 0);
738 /* TODO: handle different rates on sinkpads, a bit complex
739 * because offsets will have to be updated, and audio resampling
740 * has a latency to take into account
742 if ((gst_structure_get_int (s, "rate", &downstream_rate)
743 && info.rate != downstream_rate) || (first_configured_pad
744 && info.rate != first_configured_pad->info.rate)) {
745 gst_pad_push_event (GST_PAD (aaggpad), gst_event_new_reconfigure ());
748 GST_OBJECT_LOCK (aaggpad);
749 gst_audio_info_from_caps (&aaggpad->info, caps);
750 if (GST_IS_AUDIO_AGGREGATOR_CONVERT_PAD (aaggpad))
751 GST_AUDIO_AGGREGATOR_CONVERT_PAD (aaggpad)->
752 priv->converter_config_changed = TRUE;
753 GST_OBJECT_UNLOCK (aaggpad);
757 if (first_configured_pad)
758 gst_object_unref (first_configured_pad);
761 gst_caps_unref (downstream_caps);
767 gst_audio_aggregator_update_src_caps (GstAggregator * agg,
768 GstCaps * caps, GstCaps ** ret)
770 GstCaps *src_template_caps = gst_pad_get_pad_template_caps (agg->srcpad);
771 GstCaps *downstream_caps =
772 gst_pad_peer_query_caps (agg->srcpad, src_template_caps);
774 gst_caps_unref (src_template_caps);
776 *ret = gst_caps_intersect (caps, downstream_caps);
778 GST_INFO ("Updated src caps to %" GST_PTR_FORMAT, *ret);
781 gst_caps_unref (downstream_caps);
786 /* At that point if the caps are not fixed, this means downstream
787 * didn't have fully specified requirements, we'll just go ahead
788 * and fixate raw audio fields using our first configured pad, we don't for
789 * now need a more complicated heuristic
792 gst_audio_aggregator_fixate_src_caps (GstAggregator * agg, GstCaps * caps)
794 GstAudioAggregatorClass *aaggclass = GST_AUDIO_AGGREGATOR_GET_CLASS (agg);
795 GstAudioAggregatorPad *first_configured_pad;
797 if (!aaggclass->convert_buffer)
800 (gst_audio_aggregator_parent_class)->fixate_src_caps (agg, caps);
802 first_configured_pad = gst_audio_aggregator_get_first_configured_pad (agg);
804 if (first_configured_pad) {
805 GstStructure *s, *s2;
806 GstCaps *first_configured_caps =
807 gst_audio_info_to_caps (&first_configured_pad->info);
808 gint first_configured_rate, first_configured_channels;
810 caps = gst_caps_make_writable (caps);
811 s = gst_caps_get_structure (caps, 0);
812 s2 = gst_caps_get_structure (first_configured_caps, 0);
814 gst_structure_get_int (s2, "rate", &first_configured_rate);
815 gst_structure_get_int (s2, "channels", &first_configured_channels);
817 gst_structure_fixate_field_string (s, "format",
818 gst_structure_get_string (s2, "format"));
819 gst_structure_fixate_field_string (s, "layout",
820 gst_structure_get_string (s2, "layout"));
821 gst_structure_fixate_field_nearest_int (s, "rate", first_configured_rate);
822 gst_structure_fixate_field_nearest_int (s, "channels",
823 first_configured_channels);
825 gst_caps_unref (first_configured_caps);
826 gst_object_unref (first_configured_pad);
829 if (!gst_caps_is_fixed (caps))
830 caps = gst_caps_fixate (caps);
832 GST_INFO_OBJECT (agg, "Fixated src caps to %" GST_PTR_FORMAT, caps);
837 /* Must be called with OBJECT_LOCK taken */
839 gst_audio_aggregator_update_converters (GstAudioAggregator * aagg,
840 GstAudioInfo * new_info)
844 for (l = GST_ELEMENT (aagg)->sinkpads; l; l = l->next) {
845 GstAudioAggregatorPad *aaggpad = l->data;
847 if (GST_IS_AUDIO_AGGREGATOR_CONVERT_PAD (aaggpad))
848 GST_AUDIO_AGGREGATOR_CONVERT_PAD (aaggpad)->
849 priv->converter_config_changed = TRUE;
851 /* If we currently were mixing a buffer, we need to convert it to the new
853 if (aaggpad->priv->buffer) {
854 GstBuffer *new_converted_buffer =
855 gst_audio_aggregator_convert_buffer (aagg, GST_PAD (aaggpad),
856 &aaggpad->info, new_info, aaggpad->priv->input_buffer);
857 gst_buffer_replace (&aaggpad->priv->buffer, new_converted_buffer);
862 /* We now have our final output caps, we can create the required converters */
864 gst_audio_aggregator_negotiated_src_caps (GstAggregator * agg, GstCaps * caps)
866 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (agg);
867 GstAudioAggregatorClass *aaggclass = GST_AUDIO_AGGREGATOR_GET_CLASS (agg);
870 GST_INFO_OBJECT (agg, "src caps negotiated %" GST_PTR_FORMAT, caps);
872 if (!gst_audio_info_from_caps (&info, caps)) {
873 GST_WARNING_OBJECT (aagg, "Rejecting invalid caps: %" GST_PTR_FORMAT, caps);
877 GST_AUDIO_AGGREGATOR_LOCK (aagg);
878 GST_OBJECT_LOCK (aagg);
880 if (aaggclass->convert_buffer) {
881 gst_audio_aggregator_update_converters (aagg, &info);
883 if (aagg->priv->current_buffer
884 && !gst_audio_info_is_equal (&aagg->info, &info)) {
885 GstBuffer *converted =
886 gst_audio_aggregator_convert_buffer (aagg, agg->srcpad, &aagg->info,
887 &info, aagg->priv->current_buffer);
888 gst_buffer_unref (aagg->priv->current_buffer);
889 aagg->priv->current_buffer = converted;
893 if (!gst_audio_info_is_equal (&info, &aagg->info)) {
894 GST_INFO_OBJECT (aagg, "setting caps to %" GST_PTR_FORMAT, caps);
895 gst_caps_replace (&aagg->current_caps, caps);
897 memcpy (&aagg->info, &info, sizeof (info));
900 GST_OBJECT_UNLOCK (aagg);
901 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
905 (gst_audio_aggregator_parent_class)->negotiated_src_caps (agg, caps);
911 gst_audio_aggregator_src_event (GstAggregator * agg, GstEvent * event)
915 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (agg);
916 GST_DEBUG_OBJECT (agg->srcpad, "Got %s event on src pad",
917 GST_EVENT_TYPE_NAME (event));
919 switch (GST_EVENT_TYPE (event)) {
921 /* QoS might be tricky */
922 gst_event_unref (event);
924 case GST_EVENT_NAVIGATION:
925 /* navigation is rather pointless. */
926 gst_event_unref (event);
933 GstSeekType start_type, stop_type;
935 GstFormat seek_format, dest_format;
937 /* parse the seek parameters */
938 gst_event_parse_seek (event, &rate, &seek_format, &flags, &start_type,
939 &start, &stop_type, &stop);
941 /* Check the seeking parameters before linking up */
942 if ((start_type != GST_SEEK_TYPE_NONE)
943 && (start_type != GST_SEEK_TYPE_SET)) {
945 GST_DEBUG_OBJECT (aagg,
946 "seeking failed, unhandled seek type for start: %d", start_type);
949 if ((stop_type != GST_SEEK_TYPE_NONE) && (stop_type != GST_SEEK_TYPE_SET)) {
951 GST_DEBUG_OBJECT (aagg,
952 "seeking failed, unhandled seek type for end: %d", stop_type);
956 GST_OBJECT_LOCK (agg);
957 dest_format = agg->segment.format;
958 GST_OBJECT_UNLOCK (agg);
959 if (seek_format != dest_format) {
961 GST_DEBUG_OBJECT (aagg,
962 "seeking failed, unhandled seek format: %s",
963 gst_format_get_name (seek_format));
973 GST_AGGREGATOR_CLASS (gst_audio_aggregator_parent_class)->src_event (agg,
982 gst_audio_aggregator_sink_event (GstAggregator * agg,
983 GstAggregatorPad * aggpad, GstEvent * event)
985 GstAudioAggregatorPad *aaggpad = GST_AUDIO_AGGREGATOR_PAD (aggpad);
988 GST_DEBUG_OBJECT (aggpad, "Got %s event on sink pad",
989 GST_EVENT_TYPE_NAME (event));
991 switch (GST_EVENT_TYPE (event)) {
992 case GST_EVENT_SEGMENT:
994 const GstSegment *segment;
995 gst_event_parse_segment (event, &segment);
997 if (segment->format != GST_FORMAT_TIME) {
998 GST_ERROR_OBJECT (agg, "Segment of type %s are not supported,"
999 " only TIME segments are supported",
1000 gst_format_get_name (segment->format));
1001 gst_event_unref (event);
1007 GST_OBJECT_LOCK (agg);
1008 if (segment->rate != agg->segment.rate) {
1009 GST_ERROR_OBJECT (aggpad,
1010 "Got segment event with wrong rate %lf, expected %lf",
1011 segment->rate, agg->segment.rate);
1013 gst_event_unref (event);
1015 } else if (segment->rate < 0.0) {
1016 GST_ERROR_OBJECT (aggpad, "Negative rates not supported yet");
1018 gst_event_unref (event);
1021 GstAudioAggregatorPad *pad = GST_AUDIO_AGGREGATOR_PAD (aggpad);
1023 GST_OBJECT_LOCK (pad);
1024 pad->priv->new_segment = TRUE;
1025 GST_OBJECT_UNLOCK (pad);
1027 GST_OBJECT_UNLOCK (agg);
1031 case GST_EVENT_CAPS:
1035 gst_event_parse_caps (event, &caps);
1036 GST_INFO_OBJECT (aggpad, "Got caps %" GST_PTR_FORMAT, caps);
1037 res = gst_audio_aggregator_sink_setcaps (aaggpad, agg, caps);
1038 gst_event_unref (event);
1048 GST_AGGREGATOR_CLASS (gst_audio_aggregator_parent_class)->sink_event
1049 (agg, aggpad, event);
1055 gst_audio_aggregator_sink_query (GstAggregator * agg, GstAggregatorPad * aggpad,
1058 gboolean res = FALSE;
1060 switch (GST_QUERY_TYPE (query)) {
1061 case GST_QUERY_CAPS:
1063 GstCaps *filter, *caps;
1065 gst_query_parse_caps (query, &filter);
1066 caps = gst_audio_aggregator_sink_getcaps (GST_PAD (aggpad), agg, filter);
1067 gst_query_set_caps_result (query, caps);
1068 gst_caps_unref (caps);
1074 GST_AGGREGATOR_CLASS (gst_audio_aggregator_parent_class)->sink_query
1075 (agg, aggpad, query);
1083 /* FIXME, the duration query should reflect how long you will produce
1084 * data, that is the amount of stream time until you will emit EOS.
1086 * For synchronized mixing this is always the max of all the durations
1087 * of upstream since we emit EOS when all of them finished.
1089 * We don't do synchronized mixing so this really depends on where the
1090 * streams where punched in and what their relative offsets are against
1091 * eachother which we can get from the first timestamps we see.
1093 * When we add a new stream (or remove a stream) the duration might
1094 * also become invalid again and we need to post a new DURATION
1095 * message to notify this fact to the parent.
1096 * For now we take the max of all the upstream elements so the simple
1097 * cases work at least somewhat.
1100 gst_audio_aggregator_query_duration (GstAudioAggregator * aagg,
1108 GValue item = { 0, };
1111 gst_query_parse_duration (query, &format, NULL);
1117 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (aagg));
1119 GstIteratorResult ires;
1121 ires = gst_iterator_next (it, &item);
1123 case GST_ITERATOR_DONE:
1126 case GST_ITERATOR_OK:
1128 GstPad *pad = g_value_get_object (&item);
1131 /* ask sink peer for duration */
1132 res &= gst_pad_peer_query_duration (pad, format, &duration);
1133 /* take max from all valid return values */
1135 /* valid unknown length, stop searching */
1136 if (duration == -1) {
1140 /* else see if bigger than current max */
1141 else if (duration > max)
1144 g_value_reset (&item);
1147 case GST_ITERATOR_RESYNC:
1150 gst_iterator_resync (it);
1158 g_value_unset (&item);
1159 gst_iterator_free (it);
1162 /* and store the max */
1163 GST_DEBUG_OBJECT (aagg, "Total duration in format %s: %"
1164 GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
1165 gst_query_set_duration (query, format, max);
1173 gst_audio_aggregator_src_query (GstAggregator * agg, GstQuery * query)
1175 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (agg);
1176 gboolean res = FALSE;
1178 switch (GST_QUERY_TYPE (query)) {
1179 case GST_QUERY_DURATION:
1180 res = gst_audio_aggregator_query_duration (aagg, query);
1182 case GST_QUERY_POSITION:
1186 gst_query_parse_position (query, &format, NULL);
1188 GST_OBJECT_LOCK (aagg);
1191 case GST_FORMAT_TIME:
1192 gst_query_set_position (query, format,
1193 gst_segment_to_stream_time (&agg->segment, GST_FORMAT_TIME,
1194 agg->segment.position));
1197 case GST_FORMAT_BYTES:
1198 if (GST_AUDIO_INFO_BPF (&aagg->info)) {
1199 gst_query_set_position (query, format, aagg->priv->offset *
1200 GST_AUDIO_INFO_BPF (&aagg->info));
1204 case GST_FORMAT_DEFAULT:
1205 gst_query_set_position (query, format, aagg->priv->offset);
1212 GST_OBJECT_UNLOCK (aagg);
1218 GST_AGGREGATOR_CLASS (gst_audio_aggregator_parent_class)->src_query
1228 gst_audio_aggregator_set_sink_caps (GstAudioAggregator * aagg,
1229 GstAudioAggregatorPad * pad, GstCaps * caps)
1231 #ifndef G_DISABLE_ASSERT
1234 GST_OBJECT_LOCK (pad);
1235 valid = gst_audio_info_from_caps (&pad->info, caps);
1237 GST_OBJECT_UNLOCK (pad);
1239 GST_OBJECT_LOCK (pad);
1240 (void) gst_audio_info_from_caps (&pad->info, caps);
1241 GST_OBJECT_UNLOCK (pad);
1245 /* Must hold object lock and aagg lock to call */
1248 gst_audio_aggregator_reset (GstAudioAggregator * aagg)
1250 GstAggregator *agg = GST_AGGREGATOR (aagg);
1252 GST_AUDIO_AGGREGATOR_LOCK (aagg);
1253 GST_OBJECT_LOCK (aagg);
1254 agg->segment.position = -1;
1255 aagg->priv->offset = -1;
1256 gst_audio_info_init (&aagg->info);
1257 gst_caps_replace (&aagg->current_caps, NULL);
1258 gst_buffer_replace (&aagg->priv->current_buffer, NULL);
1259 GST_OBJECT_UNLOCK (aagg);
1260 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1264 gst_audio_aggregator_start (GstAggregator * agg)
1266 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (agg);
1268 gst_audio_aggregator_reset (aagg);
1274 gst_audio_aggregator_stop (GstAggregator * agg)
1276 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (agg);
1278 gst_audio_aggregator_reset (aagg);
1283 static GstFlowReturn
1284 gst_audio_aggregator_flush (GstAggregator * agg)
1286 GstAudioAggregator *aagg = GST_AUDIO_AGGREGATOR (agg);
1288 GST_AUDIO_AGGREGATOR_LOCK (aagg);
1289 GST_OBJECT_LOCK (aagg);
1290 agg->segment.position = -1;
1291 aagg->priv->offset = -1;
1292 gst_buffer_replace (&aagg->priv->current_buffer, NULL);
1293 GST_OBJECT_UNLOCK (aagg);
1294 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1300 gst_audio_aggregator_do_clip (GstAggregator * agg,
1301 GstAggregatorPad * bpad, GstBuffer * buffer)
1303 GstAudioAggregatorPad *pad = GST_AUDIO_AGGREGATOR_PAD (bpad);
1306 rate = GST_AUDIO_INFO_RATE (&pad->info);
1307 bpf = GST_AUDIO_INFO_BPF (&pad->info);
1309 GST_OBJECT_LOCK (bpad);
1310 buffer = gst_audio_buffer_clip (buffer, &bpad->segment, rate, bpf);
1311 GST_OBJECT_UNLOCK (bpad);
1316 /* Called with the object lock for both the element and pad held,
1317 * as well as the aagg lock
1319 * Replace the current buffer with input and update GstAudioAggregatorPadPrivate
1323 gst_audio_aggregator_fill_buffer (GstAudioAggregator * aagg,
1324 GstAudioAggregatorPad * pad)
1326 GstAudioAggregatorClass *aaggclass = GST_AUDIO_AGGREGATOR_GET_CLASS (aagg);
1327 GstClockTime start_time, end_time;
1328 gboolean discont = FALSE;
1329 guint64 start_offset, end_offset;
1332 GstAggregator *agg = GST_AGGREGATOR (aagg);
1333 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
1335 if (aaggclass->convert_buffer) {
1336 rate = GST_AUDIO_INFO_RATE (&aagg->info);
1337 bpf = GST_AUDIO_INFO_BPF (&aagg->info);
1339 rate = GST_AUDIO_INFO_RATE (&pad->info);
1340 bpf = GST_AUDIO_INFO_BPF (&pad->info);
1343 pad->priv->position = 0;
1344 pad->priv->size = gst_buffer_get_size (pad->priv->buffer) / bpf;
1346 if (pad->priv->size == 0) {
1347 if (!GST_BUFFER_DURATION_IS_VALID (pad->priv->buffer) ||
1348 !GST_BUFFER_FLAG_IS_SET (pad->priv->buffer, GST_BUFFER_FLAG_GAP)) {
1349 GST_WARNING_OBJECT (pad, "Dropping 0-sized buffer missing either a"
1350 " duration or a GAP flag: %" GST_PTR_FORMAT, pad->priv->buffer);
1355 gst_util_uint64_scale (GST_BUFFER_DURATION (pad->priv->buffer), rate,
1359 if (!GST_BUFFER_PTS_IS_VALID (pad->priv->buffer)) {
1360 if (pad->priv->output_offset == -1)
1361 pad->priv->output_offset = aagg->priv->offset;
1362 if (pad->priv->next_offset == -1)
1363 pad->priv->next_offset = pad->priv->size;
1365 pad->priv->next_offset += pad->priv->size;
1369 start_time = GST_BUFFER_PTS (pad->priv->buffer);
1371 start_time + gst_util_uint64_scale_ceil (pad->priv->size, GST_SECOND,
1374 /* Clipping should've ensured this */
1375 g_assert (start_time >= aggpad->segment.start);
1378 gst_util_uint64_scale (start_time - aggpad->segment.start, rate,
1380 end_offset = start_offset + pad->priv->size;
1382 if (GST_BUFFER_IS_DISCONT (pad->priv->buffer)
1383 || GST_BUFFER_FLAG_IS_SET (pad->priv->buffer, GST_BUFFER_FLAG_RESYNC)
1384 || pad->priv->new_segment || pad->priv->next_offset == -1) {
1386 pad->priv->new_segment = FALSE;
1388 guint64 diff, max_sample_diff;
1390 /* Check discont, based on audiobasesink */
1391 if (start_offset <= pad->priv->next_offset)
1392 diff = pad->priv->next_offset - start_offset;
1394 diff = start_offset - pad->priv->next_offset;
1397 gst_util_uint64_scale_int (aagg->priv->alignment_threshold, rate,
1401 if (G_UNLIKELY (diff >= max_sample_diff)) {
1402 if (aagg->priv->discont_wait > 0) {
1403 if (pad->priv->discont_time == GST_CLOCK_TIME_NONE) {
1404 pad->priv->discont_time = start_time;
1405 } else if (start_time - pad->priv->discont_time >=
1406 aagg->priv->discont_wait) {
1408 pad->priv->discont_time = GST_CLOCK_TIME_NONE;
1413 } else if (G_UNLIKELY (pad->priv->discont_time != GST_CLOCK_TIME_NONE)) {
1414 /* we have had a discont, but are now back on track! */
1415 pad->priv->discont_time = GST_CLOCK_TIME_NONE;
1420 /* Have discont, need resync */
1421 if (pad->priv->next_offset != -1)
1422 GST_DEBUG_OBJECT (pad, "Have discont. Expected %"
1423 G_GUINT64_FORMAT ", got %" G_GUINT64_FORMAT,
1424 pad->priv->next_offset, start_offset);
1425 pad->priv->output_offset = -1;
1426 pad->priv->next_offset = end_offset;
1428 pad->priv->next_offset += pad->priv->size;
1431 if (pad->priv->output_offset == -1) {
1432 GstClockTime start_running_time;
1433 GstClockTime end_running_time;
1434 GstClockTime segment_pos;
1435 guint64 start_output_offset = -1;
1436 guint64 end_output_offset = -1;
1438 start_running_time =
1439 gst_segment_to_running_time (&aggpad->segment,
1440 GST_FORMAT_TIME, start_time);
1442 gst_segment_to_running_time (&aggpad->segment,
1443 GST_FORMAT_TIME, end_time);
1445 /* Convert to position in the output segment */
1447 gst_segment_position_from_running_time (&agg->segment, GST_FORMAT_TIME,
1448 start_running_time);
1449 if (GST_CLOCK_TIME_IS_VALID (segment_pos))
1450 start_output_offset =
1451 gst_util_uint64_scale (segment_pos - agg->segment.start, rate,
1455 gst_segment_position_from_running_time (&agg->segment, GST_FORMAT_TIME,
1457 if (GST_CLOCK_TIME_IS_VALID (segment_pos))
1459 gst_util_uint64_scale (segment_pos - agg->segment.start, rate,
1462 if (start_output_offset == -1 && end_output_offset == -1) {
1463 /* Outside output segment, drop */
1464 pad->priv->position = 0;
1465 pad->priv->size = 0;
1466 pad->priv->output_offset = -1;
1467 GST_DEBUG_OBJECT (pad, "Buffer outside output segment");
1471 /* Calculate end_output_offset if it was outside the output segment */
1472 if (end_output_offset == -1)
1473 end_output_offset = start_output_offset + pad->priv->size;
1475 if (end_output_offset < aagg->priv->offset) {
1476 pad->priv->position = 0;
1477 pad->priv->size = 0;
1478 pad->priv->output_offset = -1;
1479 GST_DEBUG_OBJECT (pad,
1480 "Buffer before segment or current position: %" G_GUINT64_FORMAT " < %"
1481 G_GINT64_FORMAT, end_output_offset, aagg->priv->offset);
1485 if (start_output_offset == -1 || start_output_offset < aagg->priv->offset) {
1488 if (start_output_offset == -1 && end_output_offset < pad->priv->size) {
1489 diff = pad->priv->size - end_output_offset + aagg->priv->offset;
1490 } else if (start_output_offset == -1) {
1491 start_output_offset = end_output_offset - pad->priv->size;
1493 if (start_output_offset < aagg->priv->offset)
1494 diff = aagg->priv->offset - start_output_offset;
1498 diff = aagg->priv->offset - start_output_offset;
1501 pad->priv->position += diff;
1502 if (pad->priv->position >= pad->priv->size) {
1503 /* Empty buffer, drop */
1504 pad->priv->position = 0;
1505 pad->priv->size = 0;
1506 pad->priv->output_offset = -1;
1507 GST_DEBUG_OBJECT (pad,
1508 "Buffer before segment or current position: %" G_GUINT64_FORMAT
1509 " < %" G_GINT64_FORMAT, end_output_offset, aagg->priv->offset);
1514 if (start_output_offset == -1 || start_output_offset < aagg->priv->offset)
1515 pad->priv->output_offset = aagg->priv->offset;
1517 pad->priv->output_offset = start_output_offset;
1519 GST_DEBUG_OBJECT (pad,
1520 "Buffer resynced: Pad offset %" G_GUINT64_FORMAT
1521 ", current audio aggregator offset %" G_GINT64_FORMAT,
1522 pad->priv->output_offset, aagg->priv->offset);
1527 GST_LOG_OBJECT (pad,
1528 "Queued new buffer at offset %" G_GUINT64_FORMAT,
1529 pad->priv->output_offset);
1534 /* Called with pad object lock held */
1537 gst_audio_aggregator_mix_buffer (GstAudioAggregator * aagg,
1538 GstAudioAggregatorPad * pad, GstBuffer * inbuf, GstBuffer * outbuf,
1545 gboolean pad_changed = FALSE;
1547 /* Overlap => mix */
1548 if (aagg->priv->offset < pad->priv->output_offset)
1549 out_start = pad->priv->output_offset - aagg->priv->offset;
1553 overlap = pad->priv->size - pad->priv->position;
1554 if (overlap > blocksize - out_start)
1555 overlap = blocksize - out_start;
1557 if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP)) {
1558 /* skip gap buffer */
1559 GST_LOG_OBJECT (pad, "skipping GAP buffer");
1560 pad->priv->output_offset += pad->priv->size - pad->priv->position;
1561 pad->priv->position = pad->priv->size;
1563 gst_buffer_replace (&pad->priv->buffer, NULL);
1564 gst_buffer_replace (&pad->priv->input_buffer, NULL);
1568 gst_buffer_ref (inbuf);
1569 in_offset = pad->priv->position;
1570 GST_OBJECT_UNLOCK (pad);
1571 GST_OBJECT_UNLOCK (aagg);
1573 filled = GST_AUDIO_AGGREGATOR_GET_CLASS (aagg)->aggregate_one_buffer (aagg,
1574 pad, inbuf, in_offset, outbuf, out_start, overlap);
1576 GST_OBJECT_LOCK (aagg);
1577 GST_OBJECT_LOCK (pad);
1579 pad_changed = (inbuf != pad->priv->buffer);
1580 gst_buffer_unref (inbuf);
1583 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
1588 pad->priv->position += overlap;
1589 pad->priv->output_offset += overlap;
1591 if (pad->priv->position == pad->priv->size) {
1592 /* Buffer done, drop it */
1593 gst_buffer_replace (&pad->priv->buffer, NULL);
1594 gst_buffer_replace (&pad->priv->input_buffer, NULL);
1595 GST_LOG_OBJECT (pad, "Finished mixing buffer, waiting for next");
1603 gst_audio_aggregator_create_output_buffer (GstAudioAggregator * aagg,
1606 GstAllocator *allocator;
1607 GstAllocationParams params;
1611 gst_aggregator_get_allocator (GST_AGGREGATOR (aagg), &allocator, ¶ms);
1613 GST_DEBUG ("Creating output buffer with size %d",
1614 num_frames * GST_AUDIO_INFO_BPF (&aagg->info));
1616 outbuf = gst_buffer_new_allocate (allocator, num_frames *
1617 GST_AUDIO_INFO_BPF (&aagg->info), ¶ms);
1620 gst_object_unref (allocator);
1622 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
1623 gst_audio_format_fill_silence (aagg->info.finfo, outmap.data, outmap.size);
1624 gst_buffer_unmap (outbuf, &outmap);
1630 sync_pad_values (GstElement * aagg, GstPad * pad, gpointer user_data)
1632 GstAudioAggregatorPad *aapad = GST_AUDIO_AGGREGATOR_PAD (pad);
1633 GstAggregatorPad *bpad = GST_AGGREGATOR_PAD_CAST (pad);
1634 GstClockTime timestamp, stream_time;
1636 if (aapad->priv->buffer == NULL)
1639 timestamp = GST_BUFFER_PTS (aapad->priv->buffer);
1640 GST_OBJECT_LOCK (bpad);
1641 stream_time = gst_segment_to_stream_time (&bpad->segment, GST_FORMAT_TIME,
1643 GST_OBJECT_UNLOCK (bpad);
1645 /* sync object properties on stream time */
1646 /* TODO: Ideally we would want to do that on every sample */
1647 if (GST_CLOCK_TIME_IS_VALID (stream_time))
1648 gst_object_sync_values (GST_OBJECT_CAST (pad), stream_time);
1653 static GstFlowReturn
1654 gst_audio_aggregator_aggregate (GstAggregator * agg, gboolean timeout)
1656 /* Calculate the current output offset/timestamp and offset_end/timestamp_end.
1657 * Allocate a silence buffer for this and store it.
1660 * 1) Once per input buffer (cached)
1661 * 1) Check discont (flag and timestamp with tolerance)
1662 * 2) If discont or new, resync. That means:
1663 * 1) Drop all start data of the buffer that comes before
1664 * the current position/offset.
1665 * 2) Calculate the offset (output segment!) that the first
1666 * frame of the input buffer corresponds to. Base this on
1669 * 2) If the current pad's offset/offset_end overlaps with the output
1670 * offset/offset_end, mix it at the appropiate position in the output
1671 * buffer and advance the pad's position. Remember if this pad needs
1672 * a new buffer to advance behind the output offset_end.
1674 * If we had no pad with a buffer, go EOS.
1676 * If we had at least one pad that did not advance behind output
1677 * offset_end, let aggregate be called again for the current
1678 * output offset/offset_end.
1680 GstElement *element;
1681 GstAudioAggregator *aagg;
1684 GstBuffer *outbuf = NULL;
1686 gint64 next_timestamp;
1688 gboolean dropped = FALSE;
1689 gboolean is_eos = TRUE;
1690 gboolean is_done = TRUE;
1693 element = GST_ELEMENT (agg);
1694 aagg = GST_AUDIO_AGGREGATOR (agg);
1696 /* Sync pad properties to the stream time */
1697 gst_element_foreach_sink_pad (element, sync_pad_values, NULL);
1699 GST_AUDIO_AGGREGATOR_LOCK (aagg);
1700 GST_OBJECT_LOCK (agg);
1702 /* Update position from the segment start/stop if needed */
1703 if (agg->segment.position == -1) {
1704 if (agg->segment.rate > 0.0)
1705 agg->segment.position = agg->segment.start;
1707 agg->segment.position = agg->segment.stop;
1710 if (G_UNLIKELY (aagg->info.finfo->format == GST_AUDIO_FORMAT_UNKNOWN)) {
1712 GST_DEBUG_OBJECT (aagg,
1713 "Got timeout before receiving any caps, don't output anything");
1715 /* Advance position */
1716 if (agg->segment.rate > 0.0)
1717 agg->segment.position += aagg->priv->output_buffer_duration;
1718 else if (agg->segment.position > aagg->priv->output_buffer_duration)
1719 agg->segment.position -= aagg->priv->output_buffer_duration;
1721 agg->segment.position = 0;
1723 GST_OBJECT_UNLOCK (agg);
1724 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1725 return GST_AGGREGATOR_FLOW_NEED_DATA;
1727 GST_OBJECT_UNLOCK (agg);
1728 goto not_negotiated;
1732 rate = GST_AUDIO_INFO_RATE (&aagg->info);
1733 bpf = GST_AUDIO_INFO_BPF (&aagg->info);
1735 if (aagg->priv->offset == -1) {
1736 aagg->priv->offset =
1737 gst_util_uint64_scale (agg->segment.position - agg->segment.start, rate,
1739 GST_DEBUG_OBJECT (aagg, "Starting at offset %" G_GINT64_FORMAT,
1740 aagg->priv->offset);
1743 blocksize = gst_util_uint64_scale (aagg->priv->output_buffer_duration,
1745 blocksize = MAX (1, blocksize);
1747 /* FIXME: Reverse mixing does not work at all yet */
1748 if (agg->segment.rate > 0.0) {
1749 next_offset = aagg->priv->offset + blocksize;
1751 next_offset = aagg->priv->offset - blocksize;
1754 /* Use the sample counter, which will never accumulate rounding errors */
1756 agg->segment.start + gst_util_uint64_scale (next_offset, GST_SECOND,
1759 if (aagg->priv->current_buffer == NULL) {
1760 GST_OBJECT_UNLOCK (agg);
1761 aagg->priv->current_buffer =
1762 GST_AUDIO_AGGREGATOR_GET_CLASS (aagg)->create_output_buffer (aagg,
1764 /* Be careful, some things could have changed ? */
1765 GST_OBJECT_LOCK (agg);
1766 GST_BUFFER_FLAG_SET (aagg->priv->current_buffer, GST_BUFFER_FLAG_GAP);
1768 outbuf = aagg->priv->current_buffer;
1770 GST_LOG_OBJECT (agg,
1771 "Starting to mix %u samples for offset %" G_GINT64_FORMAT
1772 " with timestamp %" GST_TIME_FORMAT, blocksize,
1773 aagg->priv->offset, GST_TIME_ARGS (agg->segment.position));
1775 for (iter = element->sinkpads; iter; iter = iter->next) {
1776 GstAudioAggregatorPad *pad = (GstAudioAggregatorPad *) iter->data;
1777 GstAggregatorPad *aggpad = (GstAggregatorPad *) iter->data;
1778 gboolean pad_eos = gst_aggregator_pad_is_eos (aggpad);
1783 pad->priv->input_buffer = gst_aggregator_pad_peek_buffer (aggpad);
1785 GST_OBJECT_LOCK (pad);
1786 if (!pad->priv->input_buffer) {
1788 if (pad->priv->output_offset < next_offset) {
1789 gint64 diff = next_offset - pad->priv->output_offset;
1790 GST_DEBUG_OBJECT (pad, "Timeout, missing %" G_GINT64_FORMAT
1791 " frames (%" GST_TIME_FORMAT ")", diff,
1792 GST_TIME_ARGS (gst_util_uint64_scale (diff, GST_SECOND,
1793 GST_AUDIO_INFO_RATE (&aagg->info))));
1795 } else if (!pad_eos) {
1798 GST_OBJECT_UNLOCK (pad);
1803 if (!pad->priv->buffer) {
1804 if (GST_IS_AUDIO_AGGREGATOR_CONVERT_PAD (pad))
1806 gst_audio_aggregator_convert_buffer
1807 (aagg, GST_PAD (pad), &pad->info, &aagg->info,
1808 pad->priv->input_buffer);
1810 pad->priv->buffer = gst_buffer_ref (pad->priv->input_buffer);
1812 if (!gst_audio_aggregator_fill_buffer (aagg, pad)) {
1813 gst_buffer_replace (&pad->priv->buffer, NULL);
1814 gst_buffer_replace (&pad->priv->input_buffer, NULL);
1815 pad->priv->buffer = NULL;
1817 GST_OBJECT_UNLOCK (pad);
1819 gst_aggregator_pad_drop_buffer (aggpad);
1823 gst_buffer_unref (pad->priv->input_buffer);
1826 if (!pad->priv->buffer && !dropped && pad_eos) {
1827 GST_DEBUG_OBJECT (aggpad, "Pad is in EOS state");
1828 GST_OBJECT_UNLOCK (pad);
1832 g_assert (pad->priv->buffer);
1834 /* This pad is lagging behind, we need to update the offset
1835 * and maybe drop the current buffer */
1836 if (pad->priv->output_offset < aagg->priv->offset) {
1837 gint64 diff = aagg->priv->offset - pad->priv->output_offset;
1838 gint64 odiff = diff;
1840 if (pad->priv->position + diff > pad->priv->size)
1841 diff = pad->priv->size - pad->priv->position;
1842 pad->priv->position += diff;
1843 pad->priv->output_offset += diff;
1845 if (pad->priv->position == pad->priv->size) {
1846 GST_DEBUG_OBJECT (pad, "Buffer was late by %" GST_TIME_FORMAT
1847 ", dropping %" GST_PTR_FORMAT,
1848 GST_TIME_ARGS (gst_util_uint64_scale (odiff, GST_SECOND,
1849 GST_AUDIO_INFO_RATE (&aagg->info))), pad->priv->buffer);
1850 /* Buffer done, drop it */
1851 gst_buffer_replace (&pad->priv->buffer, NULL);
1852 gst_buffer_replace (&pad->priv->input_buffer, NULL);
1854 GST_OBJECT_UNLOCK (pad);
1855 gst_aggregator_pad_drop_buffer (aggpad);
1860 g_assert (pad->priv->buffer);
1862 if (pad->priv->output_offset >= aagg->priv->offset
1863 && pad->priv->output_offset < aagg->priv->offset + blocksize) {
1866 GST_LOG_OBJECT (aggpad, "Mixing buffer for current offset");
1867 drop_buf = !gst_audio_aggregator_mix_buffer (aagg, pad, pad->priv->buffer,
1869 if (pad->priv->output_offset >= next_offset) {
1870 GST_LOG_OBJECT (pad,
1871 "Pad is at or after current offset: %" G_GUINT64_FORMAT " >= %"
1872 G_GINT64_FORMAT, pad->priv->output_offset, next_offset);
1877 GST_OBJECT_UNLOCK (pad);
1878 gst_aggregator_pad_drop_buffer (aggpad);
1883 GST_OBJECT_UNLOCK (pad);
1885 GST_OBJECT_UNLOCK (agg);
1888 /* We dropped a buffer, retry */
1889 GST_LOG_OBJECT (aagg, "A pad dropped a buffer, wait for the next one");
1890 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1891 return GST_AGGREGATOR_FLOW_NEED_DATA;
1894 if (!is_done && !is_eos) {
1895 /* Get more buffers */
1896 GST_LOG_OBJECT (aagg,
1897 "We're not done yet for the current offset, waiting for more data");
1898 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1899 return GST_AGGREGATOR_FLOW_NEED_DATA;
1903 gint64 max_offset = 0;
1905 GST_DEBUG_OBJECT (aagg, "We're EOS");
1907 GST_OBJECT_LOCK (agg);
1908 for (iter = GST_ELEMENT (agg)->sinkpads; iter; iter = iter->next) {
1909 GstAudioAggregatorPad *pad = GST_AUDIO_AGGREGATOR_PAD (iter->data);
1911 max_offset = MAX ((gint64) max_offset, (gint64) pad->priv->output_offset);
1913 GST_OBJECT_UNLOCK (agg);
1915 /* This means EOS or nothing mixed in at all */
1916 if (aagg->priv->offset == max_offset) {
1917 gst_buffer_replace (&aagg->priv->current_buffer, NULL);
1918 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1919 return GST_FLOW_EOS;
1922 if (max_offset <= next_offset) {
1923 GST_DEBUG_OBJECT (aagg,
1924 "Last buffer is incomplete: %" G_GUINT64_FORMAT " <= %"
1925 G_GINT64_FORMAT, max_offset, next_offset);
1926 next_offset = max_offset;
1928 agg->segment.start + gst_util_uint64_scale (next_offset, GST_SECOND,
1931 if (next_offset > aagg->priv->offset)
1932 gst_buffer_resize (outbuf, 0, (next_offset - aagg->priv->offset) * bpf);
1936 /* set timestamps on the output buffer */
1937 GST_OBJECT_LOCK (agg);
1938 if (agg->segment.rate > 0.0) {
1939 GST_BUFFER_PTS (outbuf) = agg->segment.position;
1940 GST_BUFFER_OFFSET (outbuf) = aagg->priv->offset;
1941 GST_BUFFER_OFFSET_END (outbuf) = next_offset;
1942 GST_BUFFER_DURATION (outbuf) = next_timestamp - agg->segment.position;
1944 GST_BUFFER_PTS (outbuf) = next_timestamp;
1945 GST_BUFFER_OFFSET (outbuf) = next_offset;
1946 GST_BUFFER_OFFSET_END (outbuf) = aagg->priv->offset;
1947 GST_BUFFER_DURATION (outbuf) = agg->segment.position - next_timestamp;
1950 GST_OBJECT_UNLOCK (agg);
1953 GST_LOG_OBJECT (aagg,
1954 "pushing outbuf %p, timestamp %" GST_TIME_FORMAT " offset %"
1955 G_GINT64_FORMAT, outbuf, GST_TIME_ARGS (GST_BUFFER_PTS (outbuf)),
1956 GST_BUFFER_OFFSET (outbuf));
1958 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1960 ret = gst_aggregator_finish_buffer (agg, outbuf);
1961 aagg->priv->current_buffer = NULL;
1963 GST_LOG_OBJECT (aagg, "pushed outbuf, result = %s", gst_flow_get_name (ret));
1965 GST_AUDIO_AGGREGATOR_LOCK (aagg);
1966 GST_OBJECT_LOCK (agg);
1967 aagg->priv->offset = next_offset;
1968 agg->segment.position = next_timestamp;
1970 /* If there was a timeout and there was a gap in data in out of the streams,
1971 * then it's a very good time to for a resync with the timestamps.
1974 for (iter = element->sinkpads; iter; iter = iter->next) {
1975 GstAudioAggregatorPad *pad = GST_AUDIO_AGGREGATOR_PAD (iter->data);
1977 GST_OBJECT_LOCK (pad);
1978 if (pad->priv->output_offset < aagg->priv->offset)
1979 pad->priv->output_offset = -1;
1980 GST_OBJECT_UNLOCK (pad);
1983 GST_OBJECT_UNLOCK (agg);
1984 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1990 GST_AUDIO_AGGREGATOR_UNLOCK (aagg);
1991 GST_ELEMENT_ERROR (aagg, STREAM, FORMAT, (NULL),
1992 ("Unknown data received, not negotiated"));
1993 return GST_FLOW_NOT_NEGOTIATED;