2 * Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>.
3 * Copyright (C) 2011 Nokia Corporation. All rights reserved.
4 * Contact: Stefan Kost <stefan.kost@nokia.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:gstaudioencoder
24 * @short_description: Base class for audio encoders
25 * @see_also: #GstBaseTransform
28 * This base class is for audio encoders turning raw audio samples into
31 * GstAudioEncoder and subclass should cooperate as follows.
34 * <itemizedlist><title>Configuration</title>
36 * Initially, GstAudioEncoder calls @start when the encoder element
37 * is activated, which allows subclass to perform any global setup.
40 * GstAudioEncoder calls @set_format to inform subclass of the format
41 * of input audio data that it is about to receive. Subclass should
42 * setup for encoding and configure various base class parameters
43 * appropriately, notably those directing desired input data handling.
44 * While unlikely, it might be called more than once, if changing input
45 * parameters require reconfiguration.
48 * GstAudioEncoder calls @stop at end of all processing.
52 * As of configuration stage, and throughout processing, GstAudioEncoder
53 * maintains various parameters that provide required context,
54 * e.g. describing the format of input audio data.
55 * Conversely, subclass can and should configure these context parameters
56 * to inform base class of its expectation w.r.t. buffer handling.
59 * <title>Data processing</title>
61 * Base class gathers input sample data (as directed by the context's
62 * frame_samples and frame_max) and provides this to subclass' @handle_frame.
65 * If codec processing results in encoded data, subclass should call
66 * @gst_audio_encoder_finish_frame to have encoded data pushed
67 * downstream. Alternatively, it might also call to indicate dropped
68 * (non-encoded) samples.
71 * Just prior to actually pushing a buffer downstream,
72 * it is passed to @pre_push.
75 * During the parsing process GstAudioEncoderClass will handle both
76 * srcpad and sinkpad events. Sink events will be passed to subclass
77 * if @event callback has been provided.
82 * <itemizedlist><title>Shutdown phase</title>
84 * GstAudioEncoder class calls @stop to inform the subclass that data
85 * parsing will be stopped.
91 * Subclass is responsible for providing pad template caps for
92 * source and sink pads. The pads need to be named "sink" and "src". It also
93 * needs to set the fixed caps on srcpad, when the format is ensured. This
94 * is typically when base class calls subclass' @set_format function, though
95 * it might be delayed until calling @gst_audio_encoder_finish_frame.
97 * In summary, above process should have subclass concentrating on
98 * codec data processing while leaving other matters to base class,
99 * such as most notably timestamp handling. While it may exert more control
100 * in this area (see e.g. @pre_push), it is very much not recommended.
102 * In particular, base class will either favor tracking upstream timestamps
103 * (at the possible expense of jitter) or aim to arrange for a perfect stream of
104 * output timestamps, depending on #GstAudioEncoder:perfect-timestamp.
105 * However, in the latter case, the input may not be so perfect or ideal, which
106 * is handled as follows. An input timestamp is compared with the expected
107 * timestamp as dictated by input sample stream and if the deviation is less
108 * than #GstAudioEncoder:tolerance, the deviation is discarded.
109 * Otherwise, it is considered a discontuinity and subsequent output timestamp
110 * is resynced to the new position after performing configured discontinuity
111 * processing. In the non-perfect-timestamp case, an upstream variation
112 * exceeding tolerance only leads to marking DISCONT on subsequent outgoing
113 * (while timestamps are adjusted to upstream regardless of variation).
114 * While DISCONT is also marked in the perfect-timestamp case, this one
115 * optionally (see #GstAudioEncoder:hard-resync)
116 * performs some additional steps, such as clipping of (early) input samples
117 * or draining all currently remaining input data, depending on the direction
118 * of the discontuinity.
120 * If perfect timestamps are arranged, it is also possible to request baseclass
121 * (usually set by subclass) to provide additional buffer metadata (in OFFSET
122 * and OFFSET_END) fields according to granule defined semantics currently
123 * needed by oggmux. Specifically, OFFSET is set to granulepos (= sample count
124 * including buffer) and OFFSET_END to corresponding timestamp (as determined
125 * by same sample count and sample rate).
127 * Things that subclass need to take care of:
129 * <listitem><para>Provide pad templates</para></listitem>
131 * Set source pad caps when appropriate
134 * Inform base class of buffer processing needs using context's
135 * frame_samples and frame_bytes.
138 * Set user-configurable properties to sane defaults for format and
139 * implementing codec at hand, e.g. those controlling timestamp behaviour
140 * and discontinuity processing.
143 * Accept data in @handle_frame and provide encoded results to
144 * @gst_audio_encoder_finish_frame.
154 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
155 * with newer GLib versions (>= 2.31.0) */
156 #define GLIB_DISABLE_DEPRECATION_WARNINGS
158 #include "gstaudioencoder.h"
159 #include <gst/base/gstadapter.h>
160 #include <gst/audio/audio.h>
161 #include <gst/pbutils/descriptions.h>
167 GST_DEBUG_CATEGORY_STATIC (gst_audio_encoder_debug);
168 #define GST_CAT_DEFAULT gst_audio_encoder_debug
170 #define GST_AUDIO_ENCODER_GET_PRIVATE(obj) \
171 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_AUDIO_ENCODER, \
172 GstAudioEncoderPrivate))
183 #define DEFAULT_PERFECT_TS FALSE
184 #define DEFAULT_GRANULE FALSE
185 #define DEFAULT_HARD_RESYNC FALSE
186 #define DEFAULT_TOLERANCE 40000000
187 #define DEFAULT_HARD_MIN FALSE
188 #define DEFAULT_DRAINABLE TRUE
190 typedef struct _GstAudioEncoderContext
196 gint frame_samples_min, frame_samples_max;
199 /* MT-protected (with LOCK) */
200 GstClockTime min_latency;
201 GstClockTime max_latency;
202 } GstAudioEncoderContext;
204 struct _GstAudioEncoderPrivate
206 /* activation status */
209 /* input base/first ts as basis for output ts;
210 * kept nearly constant for perfect_ts,
211 * otherwise resyncs to upstream ts */
212 GstClockTime base_ts;
213 /* corresponding base granulepos */
215 /* input samples processed and sent downstream so far (w.r.t. base_ts) */
218 /* currently collected sample data */
220 /* offset in adapter up to which already supplied to encoder */
222 /* mark outgoing discont */
224 /* to guess duration of drained data */
225 GstClockTime last_duration;
227 /* subclass provided data in processing round */
229 /* subclass gave all it could already */
231 /* subclass currently being forcibly drained */
234 /* output bps estimatation */
235 /* global in samples seen */
237 /* global bytes sent out */
240 /* context storage */
241 GstAudioEncoderContext ctx;
246 gboolean hard_resync;
253 /* pending serialized sink events, will be sent from finish_frame() */
254 GList *pending_events;
258 static GstElementClass *parent_class = NULL;
260 static void gst_audio_encoder_class_init (GstAudioEncoderClass * klass);
261 static void gst_audio_encoder_init (GstAudioEncoder * parse,
262 GstAudioEncoderClass * klass);
265 gst_audio_encoder_get_type (void)
267 static GType audio_encoder_type = 0;
269 if (!audio_encoder_type) {
270 static const GTypeInfo audio_encoder_info = {
271 sizeof (GstAudioEncoderClass),
272 (GBaseInitFunc) NULL,
273 (GBaseFinalizeFunc) NULL,
274 (GClassInitFunc) gst_audio_encoder_class_init,
277 sizeof (GstAudioEncoder),
279 (GInstanceInitFunc) gst_audio_encoder_init,
281 const GInterfaceInfo preset_interface_info = {
282 NULL, /* interface_init */
283 NULL, /* interface_finalize */
284 NULL /* interface_data */
287 audio_encoder_type = g_type_register_static (GST_TYPE_ELEMENT,
288 "GstAudioEncoder", &audio_encoder_info, G_TYPE_FLAG_ABSTRACT);
290 g_type_add_interface_static (audio_encoder_type, GST_TYPE_PRESET,
291 &preset_interface_info);
293 return audio_encoder_type;
296 static void gst_audio_encoder_finalize (GObject * object);
297 static void gst_audio_encoder_reset (GstAudioEncoder * enc, gboolean full);
299 static void gst_audio_encoder_set_property (GObject * object,
300 guint prop_id, const GValue * value, GParamSpec * pspec);
301 static void gst_audio_encoder_get_property (GObject * object,
302 guint prop_id, GValue * value, GParamSpec * pspec);
304 static gboolean gst_audio_encoder_sink_activate_push (GstPad * pad,
307 static gboolean gst_audio_encoder_sink_event (GstPad * pad, GstEvent * event);
308 static gboolean gst_audio_encoder_sink_setcaps (GstPad * pad, GstCaps * caps);
309 static GstFlowReturn gst_audio_encoder_chain (GstPad * pad, GstBuffer * buffer);
310 static gboolean gst_audio_encoder_src_query (GstPad * pad, GstQuery * query);
311 static gboolean gst_audio_encoder_sink_query (GstPad * pad, GstQuery * query);
312 static const GstQueryType *gst_audio_encoder_get_query_types (GstPad * pad);
313 static GstCaps *gst_audio_encoder_sink_getcaps (GstPad * pad);
317 gst_audio_encoder_class_init (GstAudioEncoderClass * klass)
319 GObjectClass *gobject_class;
321 gobject_class = G_OBJECT_CLASS (klass);
322 parent_class = g_type_class_peek_parent (klass);
324 GST_DEBUG_CATEGORY_INIT (gst_audio_encoder_debug, "audioencoder", 0,
325 "audio encoder base class");
327 g_type_class_add_private (klass, sizeof (GstAudioEncoderPrivate));
329 gobject_class->set_property = gst_audio_encoder_set_property;
330 gobject_class->get_property = gst_audio_encoder_get_property;
332 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_audio_encoder_finalize);
335 g_object_class_install_property (gobject_class, PROP_PERFECT_TS,
336 g_param_spec_boolean ("perfect-timestamp", "Perfect Timestamps",
337 "Favour perfect timestamps over tracking upstream timestamps",
338 DEFAULT_PERFECT_TS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
339 g_object_class_install_property (gobject_class, PROP_GRANULE,
340 g_param_spec_boolean ("mark-granule", "Granule Marking",
341 "Apply granule semantics to buffer metadata (implies perfect-timestamp)",
342 DEFAULT_GRANULE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
343 g_object_class_install_property (gobject_class, PROP_HARD_RESYNC,
344 g_param_spec_boolean ("hard-resync", "Hard Resync",
345 "Perform clipping and sample flushing upon discontinuity",
346 DEFAULT_HARD_RESYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
347 g_object_class_install_property (gobject_class, PROP_TOLERANCE,
348 g_param_spec_int64 ("tolerance", "Tolerance",
349 "Consider discontinuity if timestamp jitter/imperfection exceeds tolerance (ns)",
350 0, G_MAXINT64, DEFAULT_TOLERANCE,
351 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
355 gst_audio_encoder_init (GstAudioEncoder * enc, GstAudioEncoderClass * bclass)
357 GstPadTemplate *pad_template;
359 GST_DEBUG_OBJECT (enc, "gst_audio_encoder_init");
361 enc->priv = GST_AUDIO_ENCODER_GET_PRIVATE (enc);
363 /* only push mode supported */
365 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
366 g_return_if_fail (pad_template != NULL);
367 enc->sinkpad = gst_pad_new_from_template (pad_template, "sink");
368 gst_pad_set_event_function (enc->sinkpad,
369 GST_DEBUG_FUNCPTR (gst_audio_encoder_sink_event));
370 gst_pad_set_setcaps_function (enc->sinkpad,
371 GST_DEBUG_FUNCPTR (gst_audio_encoder_sink_setcaps));
372 gst_pad_set_getcaps_function (enc->sinkpad,
373 GST_DEBUG_FUNCPTR (gst_audio_encoder_sink_getcaps));
374 gst_pad_set_query_function (enc->sinkpad,
375 GST_DEBUG_FUNCPTR (gst_audio_encoder_sink_query));
376 gst_pad_set_chain_function (enc->sinkpad,
377 GST_DEBUG_FUNCPTR (gst_audio_encoder_chain));
378 gst_pad_set_activatepush_function (enc->sinkpad,
379 GST_DEBUG_FUNCPTR (gst_audio_encoder_sink_activate_push));
380 gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad);
382 GST_DEBUG_OBJECT (enc, "sinkpad created");
384 /* and we don't mind upstream traveling stuff that much ... */
386 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
387 g_return_if_fail (pad_template != NULL);
388 enc->srcpad = gst_pad_new_from_template (pad_template, "src");
389 gst_pad_set_query_function (enc->srcpad,
390 GST_DEBUG_FUNCPTR (gst_audio_encoder_src_query));
391 gst_pad_set_query_type_function (enc->srcpad,
392 GST_DEBUG_FUNCPTR (gst_audio_encoder_get_query_types));
393 gst_pad_use_fixed_caps (enc->srcpad);
394 gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad);
395 GST_DEBUG_OBJECT (enc, "src created");
397 enc->priv->adapter = gst_adapter_new ();
399 g_static_rec_mutex_init (&enc->stream_lock);
401 /* property default */
402 enc->priv->granule = DEFAULT_GRANULE;
403 enc->priv->perfect_ts = DEFAULT_PERFECT_TS;
404 enc->priv->hard_resync = DEFAULT_HARD_RESYNC;
405 enc->priv->tolerance = DEFAULT_TOLERANCE;
406 enc->priv->hard_min = DEFAULT_HARD_MIN;
407 enc->priv->drainable = DEFAULT_DRAINABLE;
410 gst_audio_encoder_reset (enc, TRUE);
411 GST_DEBUG_OBJECT (enc, "init ok");
415 gst_audio_encoder_reset (GstAudioEncoder * enc, gboolean full)
417 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
419 GST_LOG_OBJECT (enc, "reset full %d", full);
422 enc->priv->active = FALSE;
423 enc->priv->samples_in = 0;
424 enc->priv->bytes_out = 0;
425 gst_audio_info_clear (&enc->priv->ctx.info);
426 memset (&enc->priv->ctx, 0, sizeof (enc->priv->ctx));
429 gst_tag_list_free (enc->priv->tags);
430 enc->priv->tags = NULL;
432 g_list_foreach (enc->priv->pending_events, (GFunc) gst_event_unref, NULL);
433 g_list_free (enc->priv->pending_events);
434 enc->priv->pending_events = NULL;
437 gst_segment_init (&enc->segment, GST_FORMAT_TIME);
439 gst_adapter_clear (enc->priv->adapter);
440 enc->priv->got_data = FALSE;
441 enc->priv->drained = TRUE;
442 enc->priv->offset = 0;
443 enc->priv->base_ts = GST_CLOCK_TIME_NONE;
444 enc->priv->base_gp = -1;
445 enc->priv->samples = 0;
446 enc->priv->discont = FALSE;
448 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
452 gst_audio_encoder_finalize (GObject * object)
454 GstAudioEncoder *enc = GST_AUDIO_ENCODER (object);
456 g_object_unref (enc->priv->adapter);
458 g_static_rec_mutex_free (&enc->stream_lock);
460 G_OBJECT_CLASS (parent_class)->finalize (object);
464 * gst_audio_encoder_finish_frame:
465 * @enc: a #GstAudioEncoder
466 * @buffer: encoded data
467 * @samples: number of samples (per channel) represented by encoded data
469 * Collects encoded data and pushes encoded data downstream.
470 * Source pad caps must be set when this is called.
472 * If @samples < 0, then best estimate is all samples provided to encoder
473 * (subclass) so far. @buf may be NULL, in which case next number of @samples
474 * are considered discarded, e.g. as a result of discontinuous transmission,
475 * and a discontinuity is marked.
477 * Note that samples received in gst_audio_encoder_handle_frame()
478 * may be invalidated by a call to this function.
480 * Returns: a #GstFlowReturn that should be escalated to caller (of caller)
485 gst_audio_encoder_finish_frame (GstAudioEncoder * enc, GstBuffer * buf,
488 GstAudioEncoderClass *klass;
489 GstAudioEncoderPrivate *priv;
490 GstAudioEncoderContext *ctx;
491 GstFlowReturn ret = GST_FLOW_OK;
493 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
495 ctx = &enc->priv->ctx;
497 /* subclass should know what it is producing by now */
498 g_return_val_if_fail (GST_PAD_CAPS (enc->srcpad) != NULL, GST_FLOW_ERROR);
499 /* subclass should not hand us no data */
500 g_return_val_if_fail (buf == NULL || GST_BUFFER_SIZE (buf) > 0,
503 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
505 GST_LOG_OBJECT (enc, "accepting %d bytes encoded data as %d samples",
506 buf ? GST_BUFFER_SIZE (buf) : -1, samples);
508 /* mark subclass still alive and providing */
510 priv->got_data = TRUE;
512 if (priv->pending_events) {
513 GList *pending_events, *l;
515 pending_events = priv->pending_events;
516 priv->pending_events = NULL;
518 GST_DEBUG_OBJECT (enc, "Pushing pending events");
519 for (l = pending_events; l; l = l->next)
520 gst_pad_push_event (enc->srcpad, l->data);
521 g_list_free (pending_events);
524 /* send after pending events, which likely includes newsegment event */
525 if (G_UNLIKELY (enc->priv->tags)) {
528 /* add codec info to pending tags */
529 tags = enc->priv->tags;
530 /* no more pending */
531 enc->priv->tags = NULL;
532 gst_pb_utils_add_codec_description_to_tag_list (tags, GST_TAG_CODEC,
533 GST_PAD_CAPS (enc->srcpad));
534 gst_pb_utils_add_codec_description_to_tag_list (tags, GST_TAG_AUDIO_CODEC,
535 GST_PAD_CAPS (enc->srcpad));
536 GST_DEBUG_OBJECT (enc, "sending tags %" GST_PTR_FORMAT, tags);
537 gst_element_found_tags_for_pad (GST_ELEMENT (enc), enc->srcpad, tags);
540 /* remove corresponding samples from input */
542 samples = (enc->priv->offset / ctx->info.bpf);
544 if (G_LIKELY (samples)) {
545 /* track upstream ts if so configured */
546 if (!enc->priv->perfect_ts) {
547 guint64 ts, distance;
549 ts = gst_adapter_prev_timestamp (priv->adapter, &distance);
550 g_assert (distance % ctx->info.bpf == 0);
551 distance /= ctx->info.bpf;
552 GST_LOG_OBJECT (enc, "%" G_GUINT64_FORMAT " samples past prev_ts %"
553 GST_TIME_FORMAT, distance, GST_TIME_ARGS (ts));
554 GST_LOG_OBJECT (enc, "%" G_GUINT64_FORMAT " samples past base_ts %"
555 GST_TIME_FORMAT, priv->samples, GST_TIME_ARGS (priv->base_ts));
556 /* when draining adapter might be empty and no ts to offer */
557 if (GST_CLOCK_TIME_IS_VALID (ts) && ts != priv->base_ts) {
558 GstClockTimeDiff diff;
559 GstClockTime old_ts, next_ts;
561 /* passed into another buffer;
562 * mild check for discontinuity and only mark if so */
564 gst_util_uint64_scale (distance, GST_SECOND, ctx->info.rate);
565 old_ts = priv->base_ts +
566 gst_util_uint64_scale (priv->samples, GST_SECOND, ctx->info.rate);
567 diff = GST_CLOCK_DIFF (next_ts, old_ts);
568 GST_LOG_OBJECT (enc, "ts diff %d ms", (gint) (diff / GST_MSECOND));
569 /* only mark discontinuity if beyond tolerance */
570 if (G_UNLIKELY (diff < -enc->priv->tolerance ||
571 diff > enc->priv->tolerance)) {
572 GST_DEBUG_OBJECT (enc, "marked discont");
573 priv->discont = TRUE;
575 if (diff > GST_SECOND / ctx->info.rate / 2 ||
576 diff < -GST_SECOND / ctx->info.rate / 2) {
577 GST_LOG_OBJECT (enc, "new upstream ts %" GST_TIME_FORMAT
578 " at distance %" G_GUINT64_FORMAT, GST_TIME_ARGS (ts), distance);
579 /* re-sync to upstream ts */
581 priv->samples = distance;
583 GST_LOG_OBJECT (enc, "new upstream ts only introduces jitter");
587 /* advance sample view */
588 if (G_UNLIKELY (samples * ctx->info.bpf > priv->offset)) {
589 if (G_LIKELY (!priv->force)) {
590 /* no way we can let this pass */
591 g_assert_not_reached ();
596 if (samples * ctx->info.bpf >= gst_adapter_available (priv->adapter))
597 gst_adapter_clear (priv->adapter);
599 gst_adapter_flush (priv->adapter, samples * ctx->info.bpf);
602 gst_adapter_flush (priv->adapter, samples * ctx->info.bpf);
603 priv->offset -= samples * ctx->info.bpf;
604 /* avoid subsequent stray prev_ts */
605 if (G_UNLIKELY (gst_adapter_available (priv->adapter) == 0))
606 gst_adapter_clear (priv->adapter);
608 /* sample count advanced below after buffer handling */
612 if (G_LIKELY (buf)) {
613 GST_LOG_OBJECT (enc, "taking %d bytes for output", GST_BUFFER_SIZE (buf));
614 buf = gst_buffer_make_metadata_writable (buf);
617 gst_buffer_set_caps (buf, GST_PAD_CAPS (enc->srcpad));
618 if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (priv->base_ts))) {
619 /* FIXME ? lookahead could lead to weird ts and duration ?
620 * (particularly if not in perfect mode) */
621 /* mind sample rounding and produce perfect output */
622 GST_BUFFER_TIMESTAMP (buf) = priv->base_ts +
623 gst_util_uint64_scale (priv->samples - ctx->lookahead, GST_SECOND,
625 GST_DEBUG_OBJECT (enc, "out samples %d", samples);
626 if (G_LIKELY (samples > 0)) {
627 priv->samples += samples;
628 GST_BUFFER_DURATION (buf) = priv->base_ts +
629 gst_util_uint64_scale (priv->samples - ctx->lookahead, GST_SECOND,
630 ctx->info.rate) - GST_BUFFER_TIMESTAMP (buf);
631 priv->last_duration = GST_BUFFER_DURATION (buf);
633 /* duration forecast in case of handling remainder;
634 * the last one is probably like the previous one ... */
635 GST_BUFFER_DURATION (buf) = priv->last_duration;
637 if (priv->base_gp >= 0) {
639 /* FIXME: in longer run, muxer should take care of this ... */
640 /* offset_end = granulepos for ogg muxer */
641 GST_BUFFER_OFFSET_END (buf) = priv->base_gp + priv->samples -
642 enc->priv->ctx.lookahead;
643 /* offset = timestamp corresponding to granulepos for ogg muxer */
644 GST_BUFFER_OFFSET (buf) =
645 GST_FRAMES_TO_CLOCK_TIME (GST_BUFFER_OFFSET_END (buf),
648 GST_BUFFER_OFFSET (buf) = priv->bytes_out;
649 GST_BUFFER_OFFSET_END (buf) = priv->bytes_out + GST_BUFFER_SIZE (buf);
653 priv->bytes_out += GST_BUFFER_SIZE (buf);
655 if (G_UNLIKELY (priv->discont)) {
656 GST_LOG_OBJECT (enc, "marking discont");
657 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
658 priv->discont = FALSE;
661 if (klass->pre_push) {
662 /* last chance for subclass to do some dirty stuff */
663 ret = klass->pre_push (enc, &buf);
664 if (ret != GST_FLOW_OK || !buf) {
665 GST_DEBUG_OBJECT (enc, "subclass returned %s, buf %p",
666 gst_flow_get_name (ret), buf);
668 gst_buffer_unref (buf);
673 GST_LOG_OBJECT (enc, "pushing buffer of size %d with ts %" GST_TIME_FORMAT
674 ", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buf),
675 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
676 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
678 ret = gst_pad_push (enc->srcpad, buf);
679 GST_LOG_OBJECT (enc, "buffer pushed: %s", gst_flow_get_name (ret));
681 /* merely advance samples, most work for that already done above */
682 priv->samples += samples;
686 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
693 GST_ELEMENT_ERROR (enc, STREAM, ENCODE,
694 ("received more encoded samples %d than provided %d",
695 samples, priv->offset / ctx->info.bpf), (NULL));
697 gst_buffer_unref (buf);
698 ret = GST_FLOW_ERROR;
703 /* adapter tracking idea:
704 * - start of adapter corresponds with what has already been encoded
705 * (i.e. really returned by encoder subclass)
706 * - start + offset is what needs to be fed to subclass next */
708 gst_audio_encoder_push_buffers (GstAudioEncoder * enc, gboolean force)
710 GstAudioEncoderClass *klass;
711 GstAudioEncoderPrivate *priv;
712 GstAudioEncoderContext *ctx;
715 GstFlowReturn ret = GST_FLOW_OK;
717 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
719 g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
722 ctx = &enc->priv->ctx;
724 while (ret == GST_FLOW_OK) {
727 av = gst_adapter_available (priv->adapter);
729 g_assert (priv->offset <= av);
733 ctx->frame_samples_min >
734 0 ? ctx->frame_samples_min * ctx->info.bpf : av;
735 GST_LOG_OBJECT (enc, "available: %d, needed: %d, force: %d", av, need,
738 if ((need > av) || !av) {
739 if (G_UNLIKELY (force)) {
749 if (ctx->frame_samples_max > 0)
750 need = MIN (av, ctx->frame_samples_max * ctx->info.bpf);
752 if (ctx->frame_samples_min == ctx->frame_samples_max) {
753 /* if we have some extra metadata,
754 * provide for integer multiple of frames to allow for better granularity
756 if (ctx->frame_samples_min > 0 && need) {
757 if (ctx->frame_max > 1)
758 need = need * MIN ((av / need), ctx->frame_max);
759 else if (ctx->frame_max == 0)
760 need = need * (av / need);
764 priv->got_data = FALSE;
765 if (G_LIKELY (need)) {
766 buf = gst_buffer_new ();
767 GST_BUFFER_DATA (buf) = (guint8 *)
768 gst_adapter_peek (priv->adapter, priv->offset + need) + priv->offset;
769 GST_BUFFER_SIZE (buf) = need;
770 } else if (!priv->drainable) {
771 GST_DEBUG_OBJECT (enc, "non-drainable and no more data");
775 GST_LOG_OBJECT (enc, "providing subclass with %d bytes at offset %d",
778 /* mark this already as consumed,
779 * which it should be when subclass gives us data in exchange for samples */
780 priv->offset += need;
781 priv->samples_in += need / ctx->info.bpf;
783 /* subclass might not want to be bothered with leftover data,
784 * so take care of that here if so, otherwise pass along */
785 if (G_UNLIKELY (priv->force && priv->hard_min && buf)) {
786 GST_DEBUG_OBJECT (enc, "bypassing subclass with leftover");
787 ret = gst_audio_encoder_finish_frame (enc, NULL, -1);
789 ret = klass->handle_frame (enc, buf);
793 gst_buffer_unref (buf);
796 /* no data to feed, no leftover provided, then bail out */
797 if (G_UNLIKELY (!buf && !priv->got_data)) {
798 priv->drained = TRUE;
799 GST_LOG_OBJECT (enc, "no more data drained from subclass");
808 gst_audio_encoder_drain (GstAudioEncoder * enc)
810 GST_DEBUG_OBJECT (enc, "draining");
811 if (enc->priv->drained)
814 GST_DEBUG_OBJECT (enc, "... really");
815 return gst_audio_encoder_push_buffers (enc, TRUE);
820 gst_audio_encoder_set_base_gp (GstAudioEncoder * enc)
824 if (!enc->priv->granule)
827 /* use running time for granule */
828 /* incoming data is clipped, so a valid input should yield a valid output */
829 ts = gst_segment_to_running_time (&enc->segment, GST_FORMAT_TIME,
831 if (GST_CLOCK_TIME_IS_VALID (ts)) {
833 GST_CLOCK_TIME_TO_FRAMES (enc->priv->base_ts, enc->priv->ctx.info.rate);
834 GST_DEBUG_OBJECT (enc, "new base gp %" G_GINT64_FORMAT, enc->priv->base_gp);
836 /* should reasonably have a valid base,
837 * otherwise start at 0 if we did not already start there earlier */
838 if (enc->priv->base_gp < 0) {
839 enc->priv->base_gp = 0;
840 GST_DEBUG_OBJECT (enc, "new base gp %" G_GINT64_FORMAT,
847 gst_audio_encoder_chain (GstPad * pad, GstBuffer * buffer)
849 GstAudioEncoder *enc;
850 GstAudioEncoderPrivate *priv;
851 GstAudioEncoderContext *ctx;
852 GstFlowReturn ret = GST_FLOW_OK;
855 enc = GST_AUDIO_ENCODER (GST_OBJECT_PARENT (pad));
858 ctx = &enc->priv->ctx;
860 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
862 /* should know what is coming by now */
867 "received buffer of size %d with ts %" GST_TIME_FORMAT
868 ", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buffer),
869 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
870 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
872 /* input shoud be whole number of sample frames */
873 if (GST_BUFFER_SIZE (buffer) % ctx->info.bpf)
876 #ifndef GST_DISABLE_GST_DEBUG
878 GstClockTime duration;
879 GstClockTimeDiff diff;
881 /* verify buffer duration */
882 duration = gst_util_uint64_scale (GST_BUFFER_SIZE (buffer), GST_SECOND,
883 ctx->info.rate * ctx->info.bpf);
884 diff = GST_CLOCK_DIFF (duration, GST_BUFFER_DURATION (buffer));
885 if (GST_BUFFER_DURATION (buffer) != GST_CLOCK_TIME_NONE &&
886 (diff > GST_SECOND / ctx->info.rate / 2 ||
887 diff < -GST_SECOND / ctx->info.rate / 2)) {
888 GST_DEBUG_OBJECT (enc, "incoming buffer had incorrect duration %"
889 GST_TIME_FORMAT ", expected duration %" GST_TIME_FORMAT,
890 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
891 GST_TIME_ARGS (duration));
896 discont = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT);
897 if (G_UNLIKELY (discont)) {
898 GST_LOG_OBJECT (buffer, "marked discont");
899 enc->priv->discont = discont;
902 /* clip to segment */
903 /* NOTE: slightly painful linking -laudio only for this one ... */
904 buffer = gst_audio_buffer_clip (buffer, &enc->segment, ctx->info.rate,
906 if (G_UNLIKELY (!buffer)) {
907 GST_DEBUG_OBJECT (buffer, "no data after clipping to segment");
912 "buffer after segment clipping has size %d with ts %" GST_TIME_FORMAT
913 ", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buffer),
914 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
915 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
917 if (!GST_CLOCK_TIME_IS_VALID (priv->base_ts)) {
918 priv->base_ts = GST_BUFFER_TIMESTAMP (buffer);
919 GST_DEBUG_OBJECT (enc, "new base ts %" GST_TIME_FORMAT,
920 GST_TIME_ARGS (priv->base_ts));
921 gst_audio_encoder_set_base_gp (enc);
924 /* check for continuity;
925 * checked elsewhere in non-perfect case */
926 if (enc->priv->perfect_ts) {
927 GstClockTimeDiff diff = 0;
928 GstClockTime next_ts = 0;
930 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
931 GST_CLOCK_TIME_IS_VALID (priv->base_ts)) {
934 samples = priv->samples +
935 gst_adapter_available (priv->adapter) / ctx->info.bpf;
936 next_ts = priv->base_ts +
937 gst_util_uint64_scale (samples, GST_SECOND, ctx->info.rate);
938 GST_LOG_OBJECT (enc, "buffer is %" G_GUINT64_FORMAT
939 " samples past base_ts %" GST_TIME_FORMAT
940 ", expected ts %" GST_TIME_FORMAT, samples,
941 GST_TIME_ARGS (priv->base_ts), GST_TIME_ARGS (next_ts));
942 diff = GST_CLOCK_DIFF (next_ts, GST_BUFFER_TIMESTAMP (buffer));
943 GST_LOG_OBJECT (enc, "ts diff %d ms", (gint) (diff / GST_MSECOND));
944 /* if within tolerance,
945 * discard buffer ts and carry on producing perfect stream,
946 * otherwise clip or resync to ts */
947 if (G_UNLIKELY (diff < -enc->priv->tolerance ||
948 diff > enc->priv->tolerance)) {
949 GST_DEBUG_OBJECT (enc, "marked discont");
954 /* do some fancy tweaking in hard resync case */
955 if (discont && enc->priv->hard_resync) {
959 GST_WARNING_OBJECT (enc, "Buffer is older than expected ts %"
960 GST_TIME_FORMAT ". Clipping buffer", GST_TIME_ARGS (next_ts));
963 GST_CLOCK_TIME_TO_FRAMES (-diff, ctx->info.rate) * ctx->info.bpf;
964 if (diff_bytes >= GST_BUFFER_SIZE (buffer)) {
965 gst_buffer_unref (buffer);
968 buffer = gst_buffer_make_metadata_writable (buffer);
969 GST_BUFFER_DATA (buffer) += diff_bytes;
970 GST_BUFFER_SIZE (buffer) -= diff_bytes;
972 GST_BUFFER_TIMESTAMP (buffer) += diff;
973 /* care even less about duration after this */
975 /* drain stuff prior to resync */
976 gst_audio_encoder_drain (enc);
981 priv->base_ts += diff;
982 gst_audio_encoder_set_base_gp (enc);
983 priv->discont |= discont;
987 gst_adapter_push (enc->priv->adapter, buffer);
988 /* new stuff, so we can push subclass again */
989 enc->priv->drained = FALSE;
991 ret = gst_audio_encoder_push_buffers (enc, FALSE);
994 GST_LOG_OBJECT (enc, "chain leaving");
996 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1003 GST_ELEMENT_ERROR (enc, CORE, NEGOTIATION, (NULL),
1004 ("encoder not initialized"));
1005 gst_buffer_unref (buffer);
1006 ret = GST_FLOW_NOT_NEGOTIATED;
1011 GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
1012 ("buffer size %d not a multiple of %d", GST_BUFFER_SIZE (buffer),
1014 gst_buffer_unref (buffer);
1015 ret = GST_FLOW_ERROR;
1021 audio_info_is_equal (GstAudioInfo * from, GstAudioInfo * to)
1025 if (from->finfo == NULL || to->finfo == NULL)
1027 if (GST_AUDIO_INFO_FORMAT (from) != GST_AUDIO_INFO_FORMAT (to))
1029 if (GST_AUDIO_INFO_RATE (from) != GST_AUDIO_INFO_RATE (to))
1031 if (GST_AUDIO_INFO_CHANNELS (from) != GST_AUDIO_INFO_CHANNELS (to))
1033 if (GST_AUDIO_INFO_CHANNELS (from) > 64)
1035 return memcmp (from->position, to->position,
1036 GST_AUDIO_INFO_CHANNELS (from) * sizeof (to->position[0]));
1040 gst_audio_encoder_sink_setcaps (GstPad * pad, GstCaps * caps)
1042 GstAudioEncoder *enc;
1043 GstAudioEncoderClass *klass;
1044 GstAudioEncoderContext *ctx;
1045 GstAudioInfo *state, *old_state;
1046 gboolean res = TRUE, changed = FALSE;
1049 enc = GST_AUDIO_ENCODER (GST_PAD_PARENT (pad));
1050 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1052 /* subclass must do something here ... */
1053 g_return_val_if_fail (klass->set_format != NULL, FALSE);
1055 ctx = &enc->priv->ctx;
1058 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1060 GST_DEBUG_OBJECT (enc, "caps: %" GST_PTR_FORMAT, caps);
1062 if (!gst_caps_is_fixed (caps))
1065 /* adjust ts tracking to new sample rate */
1066 old_rate = GST_AUDIO_INFO_RATE (state);
1067 if (GST_CLOCK_TIME_IS_VALID (enc->priv->base_ts) && old_rate) {
1068 enc->priv->base_ts +=
1069 GST_FRAMES_TO_CLOCK_TIME (enc->priv->samples, old_rate);
1070 enc->priv->samples = 0;
1073 old_state = gst_audio_info_copy (state);
1074 if (!gst_audio_info_from_caps (state, caps))
1077 changed = !audio_info_is_equal (state, old_state);
1078 gst_audio_info_free (old_state);
1081 GstClockTime old_min_latency;
1082 GstClockTime old_max_latency;
1084 /* drain any pending old data stuff */
1085 gst_audio_encoder_drain (enc);
1087 /* context defaults */
1088 enc->priv->ctx.frame_samples_min = 0;
1089 enc->priv->ctx.frame_samples_max = 0;
1090 enc->priv->ctx.frame_max = 0;
1091 enc->priv->ctx.lookahead = 0;
1093 /* element might report latency */
1094 GST_OBJECT_LOCK (enc);
1095 old_min_latency = ctx->min_latency;
1096 old_max_latency = ctx->max_latency;
1097 GST_OBJECT_UNLOCK (enc);
1099 if (klass->set_format)
1100 res = klass->set_format (enc, state);
1102 /* invalidate state to ensure no casual carrying on */
1104 GST_DEBUG_OBJECT (enc, "subclass did not accept format");
1105 gst_audio_info_clear (state);
1109 /* notify if new latency */
1110 GST_OBJECT_LOCK (enc);
1111 if ((ctx->min_latency > 0 && ctx->min_latency != old_min_latency) ||
1112 (ctx->max_latency > 0 && ctx->max_latency != old_max_latency)) {
1113 GST_OBJECT_UNLOCK (enc);
1114 /* post latency message on the bus */
1115 gst_element_post_message (GST_ELEMENT (enc),
1116 gst_message_new_latency (GST_OBJECT (enc)));
1117 GST_OBJECT_LOCK (enc);
1119 GST_OBJECT_UNLOCK (enc);
1121 GST_DEBUG_OBJECT (enc, "new audio format identical to configured format");
1126 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1133 GST_WARNING_OBJECT (enc, "rejected caps %" GST_PTR_FORMAT, caps);
1140 * gst_audio_encoder_proxy_getcaps:
1141 * @enc: a #GstAudioEncoder
1142 * @caps: initial caps
1144 * Returns caps that express @caps (or sink template caps if @caps == NULL)
1145 * restricted to channel/rate combinations supported by downstream elements
1148 * Returns: a #GstCaps owned by caller
1153 gst_audio_encoder_proxy_getcaps (GstAudioEncoder * enc, GstCaps * caps)
1155 const GstCaps *templ_caps;
1156 GstCaps *allowed = NULL;
1157 GstCaps *fcaps, *filter_caps;
1160 /* we want to be able to communicate to upstream elements like audioconvert
1161 * and audioresample any rate/channel restrictions downstream (e.g. muxer
1162 * only accepting certain sample rates) */
1163 templ_caps = caps ? caps : gst_pad_get_pad_template_caps (enc->sinkpad);
1164 allowed = gst_pad_get_allowed_caps (enc->srcpad);
1165 if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
1166 fcaps = gst_caps_copy (templ_caps);
1170 GST_LOG_OBJECT (enc, "template caps %" GST_PTR_FORMAT, templ_caps);
1171 GST_LOG_OBJECT (enc, "allowed caps %" GST_PTR_FORMAT, allowed);
1173 filter_caps = gst_caps_new_empty ();
1175 for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
1178 q_name = gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
1180 /* pick rate + channel fields from allowed caps */
1181 for (j = 0; j < gst_caps_get_size (allowed); j++) {
1182 const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
1186 s = gst_structure_id_empty_new (q_name);
1187 if ((val = gst_structure_get_value (allowed_s, "rate")))
1188 gst_structure_set_value (s, "rate", val);
1189 if ((val = gst_structure_get_value (allowed_s, "channels")))
1190 gst_structure_set_value (s, "channels", val);
1191 /* following might also make sense for some encoded formats,
1193 if ((val = gst_structure_get_value (allowed_s, "width")))
1194 gst_structure_set_value (s, "width", val);
1195 if ((val = gst_structure_get_value (allowed_s, "depth")))
1196 gst_structure_set_value (s, "depth", val);
1197 if ((val = gst_structure_get_value (allowed_s, "endianness")))
1198 gst_structure_set_value (s, "endianness", val);
1199 if ((val = gst_structure_get_value (allowed_s, "signed")))
1200 gst_structure_set_value (s, "signed", val);
1201 if ((val = gst_structure_get_value (allowed_s, "channel-positions")))
1202 gst_structure_set_value (s, "channel-positions", val);
1204 gst_caps_merge_structure (filter_caps, s);
1208 fcaps = gst_caps_intersect (filter_caps, templ_caps);
1209 gst_caps_unref (filter_caps);
1212 gst_caps_replace (&allowed, NULL);
1214 GST_LOG_OBJECT (enc, "proxy caps %" GST_PTR_FORMAT, fcaps);
1220 gst_audio_encoder_sink_getcaps (GstPad * pad)
1222 GstAudioEncoder *enc;
1223 GstAudioEncoderClass *klass;
1226 enc = GST_AUDIO_ENCODER (gst_pad_get_parent (pad));
1227 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1228 g_assert (pad == enc->sinkpad);
1231 caps = klass->getcaps (enc);
1233 caps = gst_audio_encoder_proxy_getcaps (enc, NULL);
1234 gst_object_unref (enc);
1236 GST_LOG_OBJECT (enc, "returning caps %" GST_PTR_FORMAT, caps);
1242 gst_audio_encoder_sink_eventfunc (GstAudioEncoder * enc, GstEvent * event)
1244 GstAudioEncoderClass *klass;
1245 gboolean handled = FALSE;
1247 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1249 switch (GST_EVENT_TYPE (event)) {
1250 case GST_EVENT_NEWSEGMENT:
1253 gdouble rate, arate;
1254 gint64 start, stop, time;
1257 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1258 &start, &stop, &time);
1260 if (format == GST_FORMAT_TIME) {
1261 GST_DEBUG_OBJECT (enc, "received TIME NEW_SEGMENT %" GST_TIME_FORMAT
1262 " -- %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT
1263 ", rate %g, applied_rate %g",
1264 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (time),
1267 GST_DEBUG_OBJECT (enc, "received NEW_SEGMENT %" G_GINT64_FORMAT
1268 " -- %" G_GINT64_FORMAT ", time %" G_GINT64_FORMAT
1269 ", rate %g, applied_rate %g", start, stop, time, rate, arate);
1270 GST_DEBUG_OBJECT (enc, "unsupported format; ignoring");
1274 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1275 /* finish current segment */
1276 gst_audio_encoder_drain (enc);
1277 /* reset partially for new segment */
1278 gst_audio_encoder_reset (enc, FALSE);
1279 /* and follow along with segment */
1280 gst_segment_set_newsegment_full (&enc->segment, update, rate, arate,
1281 format, start, stop, time);
1282 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1286 case GST_EVENT_FLUSH_START:
1289 case GST_EVENT_FLUSH_STOP:
1290 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1291 /* discard any pending stuff */
1292 /* TODO route through drain ?? */
1293 if (!enc->priv->drained && klass->flush)
1295 /* and get (re)set for the sequel */
1296 gst_audio_encoder_reset (enc, FALSE);
1298 g_list_foreach (enc->priv->pending_events, (GFunc) gst_event_unref, NULL);
1299 g_list_free (enc->priv->pending_events);
1300 enc->priv->pending_events = NULL;
1301 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1306 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1307 gst_audio_encoder_drain (enc);
1308 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1315 gst_event_parse_tag (event, &tags);
1316 tags = gst_tag_list_copy (tags);
1317 gst_event_unref (event);
1319 /* FIXME: make generic based on GST_TAG_FLAG_ENCODED */
1320 gst_tag_list_remove_tag (tags, GST_TAG_CODEC);
1321 gst_tag_list_remove_tag (tags, GST_TAG_AUDIO_CODEC);
1322 gst_tag_list_remove_tag (tags, GST_TAG_VIDEO_CODEC);
1323 gst_tag_list_remove_tag (tags, GST_TAG_SUBTITLE_CODEC);
1324 gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1325 gst_tag_list_remove_tag (tags, GST_TAG_BITRATE);
1326 gst_tag_list_remove_tag (tags, GST_TAG_NOMINAL_BITRATE);
1327 gst_tag_list_remove_tag (tags, GST_TAG_MAXIMUM_BITRATE);
1328 gst_tag_list_remove_tag (tags, GST_TAG_MINIMUM_BITRATE);
1329 gst_tag_list_remove_tag (tags, GST_TAG_ENCODER);
1330 gst_tag_list_remove_tag (tags, GST_TAG_ENCODER_VERSION);
1331 event = gst_event_new_tag (tags);
1333 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1334 enc->priv->pending_events =
1335 g_list_append (enc->priv->pending_events, event);
1336 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1349 gst_audio_encoder_sink_event (GstPad * pad, GstEvent * event)
1351 GstAudioEncoder *enc;
1352 GstAudioEncoderClass *klass;
1353 gboolean handled = FALSE;
1354 gboolean ret = TRUE;
1356 enc = GST_AUDIO_ENCODER (gst_pad_get_parent (pad));
1357 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1359 GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
1360 GST_EVENT_TYPE_NAME (event));
1363 handled = klass->event (enc, event);
1366 handled = gst_audio_encoder_sink_eventfunc (enc, event);
1369 /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
1370 * For EOS this is required because no buffer or serialized event
1371 * will come after EOS and nothing could trigger another
1372 * _finish_frame() call.
1374 * For FLUSH_STOP this is required because it is expected
1375 * to be forwarded immediately and no buffers are queued anyway.
1377 if (!GST_EVENT_IS_SERIALIZED (event)
1378 || GST_EVENT_TYPE (event) == GST_EVENT_EOS
1379 || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
1380 ret = gst_pad_event_default (pad, event);
1382 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1383 enc->priv->pending_events =
1384 g_list_append (enc->priv->pending_events, event);
1385 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1390 GST_DEBUG_OBJECT (enc, "event handled");
1392 gst_object_unref (enc);
1397 gst_audio_encoder_sink_query (GstPad * pad, GstQuery * query)
1399 gboolean res = TRUE;
1400 GstAudioEncoder *enc;
1402 enc = GST_AUDIO_ENCODER (gst_pad_get_parent (pad));
1404 switch (GST_QUERY_TYPE (query)) {
1405 case GST_QUERY_FORMATS:
1407 gst_query_set_formats (query, 3,
1408 GST_FORMAT_TIME, GST_FORMAT_BYTES, GST_FORMAT_DEFAULT);
1412 case GST_QUERY_CONVERT:
1414 GstFormat src_fmt, dest_fmt;
1415 gint64 src_val, dest_val;
1417 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1418 if (!(res = gst_audio_info_convert (&enc->priv->ctx.info,
1419 src_fmt, src_val, dest_fmt, &dest_val)))
1421 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1425 res = gst_pad_query_default (pad, query);
1430 gst_object_unref (enc);
1434 static const GstQueryType *
1435 gst_audio_encoder_get_query_types (GstPad * pad)
1437 static const GstQueryType gst_audio_encoder_src_query_types[] = {
1445 return gst_audio_encoder_src_query_types;
1449 * gst_audio_encoded_audio_convert:
1450 * @fmt: audio format of the encoded audio
1451 * @bytes: number of encoded bytes
1452 * @samples: number of encoded samples
1453 * @src_format: source format
1454 * @src_value: source value
1455 * @dest_format: destination format
1456 * @dest_value: destination format
1458 * Helper function to convert @src_value in @src_format to @dest_value in
1459 * @dest_format for encoded audio data. Conversion is possible between
1460 * BYTE and TIME format by using estimated bitrate based on
1461 * @samples and @bytes (and @fmt).
1465 /* FIXME: make gst_audio_encoded_audio_convert() public? */
1467 gst_audio_encoded_audio_convert (GstAudioInfo * fmt,
1468 gint64 bytes, gint64 samples, GstFormat src_format,
1469 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
1471 gboolean res = FALSE;
1473 g_return_val_if_fail (dest_format != NULL, FALSE);
1474 g_return_val_if_fail (dest_value != NULL, FALSE);
1476 if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
1479 *dest_value = src_value;
1483 if (samples == 0 || bytes == 0 || fmt->rate == 0) {
1484 GST_DEBUG ("not enough metadata yet to convert");
1490 switch (src_format) {
1491 case GST_FORMAT_BYTES:
1492 switch (*dest_format) {
1493 case GST_FORMAT_TIME:
1494 *dest_value = gst_util_uint64_scale (src_value,
1495 GST_SECOND * samples, bytes);
1502 case GST_FORMAT_TIME:
1503 switch (*dest_format) {
1504 case GST_FORMAT_BYTES:
1505 *dest_value = gst_util_uint64_scale (src_value, bytes,
1506 samples * GST_SECOND);
1521 /* FIXME ? are any of these queries (other than latency) an encoder's business
1522 * also, the conversion stuff might seem to make sense, but seems to not mind
1523 * segment stuff etc at all
1524 * Supposedly that's backward compatibility ... */
1526 gst_audio_encoder_src_query (GstPad * pad, GstQuery * query)
1528 GstAudioEncoder *enc;
1530 gboolean res = FALSE;
1532 enc = GST_AUDIO_ENCODER (GST_PAD_PARENT (pad));
1533 if (G_UNLIKELY (enc == NULL))
1536 peerpad = gst_pad_get_peer (GST_PAD (enc->sinkpad));
1538 GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
1540 switch (GST_QUERY_TYPE (query)) {
1541 case GST_QUERY_POSITION:
1543 GstFormat fmt, req_fmt;
1546 if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1547 GST_LOG_OBJECT (enc, "returning peer response");
1552 GST_LOG_OBJECT (enc, "no peer");
1556 gst_query_parse_position (query, &req_fmt, NULL);
1557 fmt = GST_FORMAT_TIME;
1558 if (!(res = gst_pad_query_position (peerpad, &fmt, &pos)))
1561 if ((res = gst_pad_query_convert (peerpad, fmt, pos, &req_fmt, &val))) {
1562 gst_query_set_position (query, req_fmt, val);
1566 case GST_QUERY_DURATION:
1568 GstFormat fmt, req_fmt;
1571 if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1572 GST_LOG_OBJECT (enc, "returning peer response");
1577 GST_LOG_OBJECT (enc, "no peer");
1581 gst_query_parse_duration (query, &req_fmt, NULL);
1582 fmt = GST_FORMAT_TIME;
1583 if (!(res = gst_pad_query_duration (peerpad, &fmt, &dur)))
1586 if ((res = gst_pad_query_convert (peerpad, fmt, dur, &req_fmt, &val))) {
1587 gst_query_set_duration (query, req_fmt, val);
1591 case GST_QUERY_FORMATS:
1593 gst_query_set_formats (query, 2, GST_FORMAT_TIME, GST_FORMAT_BYTES);
1597 case GST_QUERY_CONVERT:
1599 GstFormat src_fmt, dest_fmt;
1600 gint64 src_val, dest_val;
1602 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1603 if (!(res = gst_audio_encoded_audio_convert (&enc->priv->ctx.info,
1604 enc->priv->bytes_out, enc->priv->samples_in, src_fmt, src_val,
1605 &dest_fmt, &dest_val)))
1607 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1610 case GST_QUERY_LATENCY:
1612 if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1614 GstClockTime min_latency, max_latency;
1616 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1617 GST_DEBUG_OBJECT (enc, "Peer latency: live %d, min %"
1618 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1619 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1621 GST_OBJECT_LOCK (enc);
1622 /* add our latency */
1623 if (min_latency != -1)
1624 min_latency += enc->priv->ctx.min_latency;
1625 if (max_latency != -1)
1626 max_latency += enc->priv->ctx.max_latency;
1627 GST_OBJECT_UNLOCK (enc);
1629 gst_query_set_latency (query, live, min_latency, max_latency);
1634 res = gst_pad_query_default (pad, query);
1638 gst_object_unref (peerpad);
1643 gst_audio_encoder_set_property (GObject * object, guint prop_id,
1644 const GValue * value, GParamSpec * pspec)
1646 GstAudioEncoder *enc;
1648 enc = GST_AUDIO_ENCODER (object);
1651 case PROP_PERFECT_TS:
1652 if (enc->priv->granule && !g_value_get_boolean (value))
1653 GST_WARNING_OBJECT (enc, "perfect-timestamp can not be set FALSE "
1654 "while granule handling is enabled");
1656 enc->priv->perfect_ts = g_value_get_boolean (value);
1658 case PROP_HARD_RESYNC:
1659 enc->priv->hard_resync = g_value_get_boolean (value);
1661 case PROP_TOLERANCE:
1662 enc->priv->tolerance = g_value_get_int64 (value);
1665 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1671 gst_audio_encoder_get_property (GObject * object, guint prop_id,
1672 GValue * value, GParamSpec * pspec)
1674 GstAudioEncoder *enc;
1676 enc = GST_AUDIO_ENCODER (object);
1679 case PROP_PERFECT_TS:
1680 g_value_set_boolean (value, enc->priv->perfect_ts);
1683 g_value_set_boolean (value, enc->priv->granule);
1685 case PROP_HARD_RESYNC:
1686 g_value_set_boolean (value, enc->priv->hard_resync);
1688 case PROP_TOLERANCE:
1689 g_value_set_int64 (value, enc->priv->tolerance);
1692 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1698 gst_audio_encoder_activate (GstAudioEncoder * enc, gboolean active)
1700 GstAudioEncoderClass *klass;
1701 gboolean result = FALSE;
1703 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1705 g_return_val_if_fail (!enc->priv->granule || enc->priv->perfect_ts, FALSE);
1707 GST_DEBUG_OBJECT (enc, "activate %d", active);
1711 if (enc->priv->tags)
1712 gst_tag_list_free (enc->priv->tags);
1713 enc->priv->tags = gst_tag_list_new ();
1715 if (!enc->priv->active && klass->start)
1716 result = klass->start (enc);
1718 /* We must make sure streaming has finished before resetting things
1719 * and calling the ::stop vfunc */
1720 GST_PAD_STREAM_LOCK (enc->sinkpad);
1721 GST_PAD_STREAM_UNLOCK (enc->sinkpad);
1723 if (enc->priv->active && klass->stop)
1724 result = klass->stop (enc);
1727 gst_audio_encoder_reset (enc, TRUE);
1729 GST_DEBUG_OBJECT (enc, "activate return: %d", result);
1735 gst_audio_encoder_sink_activate_push (GstPad * pad, gboolean active)
1737 gboolean result = TRUE;
1738 GstAudioEncoder *enc;
1740 enc = GST_AUDIO_ENCODER (gst_pad_get_parent (pad));
1742 GST_DEBUG_OBJECT (enc, "sink activate push %d", active);
1744 result = gst_audio_encoder_activate (enc, active);
1747 enc->priv->active = active;
1749 GST_DEBUG_OBJECT (enc, "sink activate push return: %d", result);
1751 gst_object_unref (enc);
1756 * gst_audio_encoder_get_audio_info:
1757 * @enc: a #GstAudioEncoder
1759 * Returns: a #GstAudioInfo describing the input audio format
1764 gst_audio_encoder_get_audio_info (GstAudioEncoder * enc)
1766 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), NULL);
1768 return &enc->priv->ctx.info;
1772 * gst_audio_encoder_set_frame_samples_min:
1773 * @enc: a #GstAudioEncoder
1774 * @num: number of samples per frame
1776 * Sets number of samples (per channel) subclass needs to be handed,
1777 * at least or will be handed all available if 0.
1779 * If an exact number of samples is required, gst_audio_encoder_set_frame_samples_max()
1780 * must be called with the same number.
1785 gst_audio_encoder_set_frame_samples_min (GstAudioEncoder * enc, gint num)
1787 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1789 enc->priv->ctx.frame_samples_min = num;
1793 * gst_audio_encoder_get_frame_samples_min:
1794 * @enc: a #GstAudioEncoder
1796 * Returns: currently minimum requested samples per frame
1801 gst_audio_encoder_get_frame_samples_min (GstAudioEncoder * enc)
1803 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
1805 return enc->priv->ctx.frame_samples_min;
1809 * gst_audio_encoder_set_frame_samples_max:
1810 * @enc: a #GstAudioEncoder
1811 * @num: number of samples per frame
1813 * Sets number of samples (per channel) subclass needs to be handed,
1814 * at most or will be handed all available if 0.
1816 * If an exact number of samples is required, gst_audio_encoder_set_frame_samples_min()
1817 * must be called with the same number.
1822 gst_audio_encoder_set_frame_samples_max (GstAudioEncoder * enc, gint num)
1824 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1826 enc->priv->ctx.frame_samples_max = num;
1830 * gst_audio_encoder_get_frame_samples_min:
1831 * @enc: a #GstAudioEncoder
1833 * Returns: currently maximum requested samples per frame
1838 gst_audio_encoder_get_frame_samples_max (GstAudioEncoder * enc)
1840 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
1842 return enc->priv->ctx.frame_samples_max;
1846 * gst_audio_encoder_set_frame_max:
1847 * @enc: a #GstAudioEncoder
1848 * @num: number of frames
1850 * Sets max number of frames accepted at once (assumed minimally 1).
1851 * Requires @frame_samples_min and @frame_samples_max to be the equal.
1856 gst_audio_encoder_set_frame_max (GstAudioEncoder * enc, gint num)
1858 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1860 enc->priv->ctx.frame_max = num;
1864 * gst_audio_encoder_get_frame_max:
1865 * @enc: a #GstAudioEncoder
1867 * Returns: currently configured maximum handled frames
1872 gst_audio_encoder_get_frame_max (GstAudioEncoder * enc)
1874 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
1876 return enc->priv->ctx.frame_max;
1880 * gst_audio_encoder_set_lookahead:
1881 * @enc: a #GstAudioEncoder
1884 * Sets encoder lookahead (in units of input rate samples)
1889 gst_audio_encoder_set_lookahead (GstAudioEncoder * enc, gint num)
1891 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1893 enc->priv->ctx.lookahead = num;
1897 * gst_audio_encoder_get_lookahead:
1898 * @enc: a #GstAudioEncoder
1900 * Returns: currently configured encoder lookahead
1903 gst_audio_encoder_get_lookahead (GstAudioEncoder * enc)
1905 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
1907 return enc->priv->ctx.lookahead;
1911 * gst_audio_encoder_set_latency:
1912 * @enc: a #GstAudioEncoder
1913 * @min: minimum latency
1914 * @max: maximum latency
1916 * Sets encoder latency.
1921 gst_audio_encoder_set_latency (GstAudioEncoder * enc,
1922 GstClockTime min, GstClockTime max)
1924 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1926 GST_OBJECT_LOCK (enc);
1927 enc->priv->ctx.min_latency = min;
1928 enc->priv->ctx.max_latency = max;
1929 GST_OBJECT_UNLOCK (enc);
1933 * gst_audio_encoder_get_latency:
1934 * @enc: a #GstAudioEncoder
1935 * @min: (out) (allow-none): a pointer to storage to hold minimum latency
1936 * @max: (out) (allow-none): a pointer to storage to hold maximum latency
1938 * Sets the variables pointed to by @min and @max to the currently configured
1944 gst_audio_encoder_get_latency (GstAudioEncoder * enc,
1945 GstClockTime * min, GstClockTime * max)
1947 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1949 GST_OBJECT_LOCK (enc);
1951 *min = enc->priv->ctx.min_latency;
1953 *max = enc->priv->ctx.max_latency;
1954 GST_OBJECT_UNLOCK (enc);
1958 * gst_audio_encoder_set_mark_granule:
1959 * @enc: a #GstAudioEncoder
1960 * @enabled: new state
1962 * Enable or disable encoder granule handling.
1969 gst_audio_encoder_set_mark_granule (GstAudioEncoder * enc, gboolean enabled)
1971 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1973 GST_LOG_OBJECT (enc, "enabled: %d", enabled);
1975 GST_OBJECT_LOCK (enc);
1976 enc->priv->granule = enabled;
1977 GST_OBJECT_UNLOCK (enc);
1981 * gst_audio_encoder_get_mark_granule:
1982 * @enc: a #GstAudioEncoder
1984 * Queries if the encoder will handle granule marking.
1986 * Returns: TRUE if granule marking is enabled.
1993 gst_audio_encoder_get_mark_granule (GstAudioEncoder * enc)
1997 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), FALSE);
1999 GST_OBJECT_LOCK (enc);
2000 result = enc->priv->granule;
2001 GST_OBJECT_UNLOCK (enc);
2007 * gst_audio_encoder_set_perfect_timestamp:
2008 * @enc: a #GstAudioEncoder
2009 * @enabled: new state
2011 * Enable or disable encoder perfect output timestamp preference.
2018 gst_audio_encoder_set_perfect_timestamp (GstAudioEncoder * enc,
2021 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
2023 GST_LOG_OBJECT (enc, "enabled: %d", enabled);
2025 GST_OBJECT_LOCK (enc);
2026 enc->priv->perfect_ts = enabled;
2027 GST_OBJECT_UNLOCK (enc);
2031 * gst_audio_encoder_get_perfect_timestamp:
2032 * @enc: a #GstAudioEncoder
2034 * Queries encoder perfect timestamp behaviour.
2036 * Returns: TRUE if perfect timestamp setting enabled.
2043 gst_audio_encoder_get_perfect_timestamp (GstAudioEncoder * enc)
2047 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), FALSE);
2049 GST_OBJECT_LOCK (enc);
2050 result = enc->priv->perfect_ts;
2051 GST_OBJECT_UNLOCK (enc);
2057 * gst_audio_encoder_set_hard_sync:
2058 * @enc: a #GstAudioEncoder
2059 * @enabled: new state
2061 * Sets encoder hard resync handling.
2068 gst_audio_encoder_set_hard_resync (GstAudioEncoder * enc, gboolean enabled)
2070 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
2072 GST_LOG_OBJECT (enc, "enabled: %d", enabled);
2074 GST_OBJECT_LOCK (enc);
2075 enc->priv->hard_resync = enabled;
2076 GST_OBJECT_UNLOCK (enc);
2080 * gst_audio_encoder_get_hard_sync:
2081 * @enc: a #GstAudioEncoder
2083 * Queries encoder's hard resync setting.
2085 * Returns: TRUE if hard resync is enabled.
2092 gst_audio_encoder_get_hard_resync (GstAudioEncoder * enc)
2096 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), FALSE);
2098 GST_OBJECT_LOCK (enc);
2099 result = enc->priv->hard_resync;
2100 GST_OBJECT_UNLOCK (enc);
2106 * gst_audio_encoder_set_tolerance:
2107 * @enc: a #GstAudioEncoder
2108 * @tolerance: new tolerance
2110 * Configures encoder audio jitter tolerance threshold.
2117 gst_audio_encoder_set_tolerance (GstAudioEncoder * enc, gint64 tolerance)
2119 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
2121 GST_OBJECT_LOCK (enc);
2122 enc->priv->tolerance = tolerance;
2123 GST_OBJECT_UNLOCK (enc);
2127 * gst_audio_encoder_get_tolerance:
2128 * @enc: a #GstAudioEncoder
2130 * Queries current audio jitter tolerance threshold.
2132 * Returns: encoder audio jitter tolerance threshold.
2139 gst_audio_encoder_get_tolerance (GstAudioEncoder * enc)
2143 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
2145 GST_OBJECT_LOCK (enc);
2146 result = enc->priv->tolerance;
2147 GST_OBJECT_UNLOCK (enc);
2153 * gst_audio_encoder_set_hard_min:
2154 * @enc: a #GstAudioEncoder
2155 * @enabled: new state
2157 * Configures encoder hard minimum handling. If enabled, subclass
2158 * will never be handed less samples than it configured, which otherwise
2159 * might occur near end-of-data handling. Instead, the leftover samples
2160 * will simply be discarded.
2167 gst_audio_encoder_set_hard_min (GstAudioEncoder * enc, gboolean enabled)
2169 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
2171 GST_OBJECT_LOCK (enc);
2172 enc->priv->hard_min = enabled;
2173 GST_OBJECT_UNLOCK (enc);
2177 * gst_audio_encoder_get_hard_min:
2178 * @enc: a #GstAudioEncoder
2180 * Queries encoder hard minimum handling.
2182 * Returns: TRUE if hard minimum handling is enabled.
2189 gst_audio_encoder_get_hard_min (GstAudioEncoder * enc)
2193 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
2195 GST_OBJECT_LOCK (enc);
2196 result = enc->priv->hard_min;
2197 GST_OBJECT_UNLOCK (enc);
2203 * gst_audio_encoder_set_drainable:
2204 * @enc: a #GstAudioEncoder
2205 * @enabled: new state
2207 * Configures encoder drain handling. If drainable, subclass might
2208 * be handed a NULL buffer to have it return any leftover encoded data.
2209 * Otherwise, it is not considered so capable and will only ever be passed
2217 gst_audio_encoder_set_drainable (GstAudioEncoder * enc, gboolean enabled)
2219 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
2221 GST_OBJECT_LOCK (enc);
2222 enc->priv->drainable = enabled;
2223 GST_OBJECT_UNLOCK (enc);
2227 * gst_audio_encoder_get_drainable:
2228 * @enc: a #GstAudioEncoder
2230 * Queries encoder drain handling.
2232 * Returns: TRUE if drainable handling is enabled.
2239 gst_audio_encoder_get_drainable (GstAudioEncoder * enc)
2243 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
2245 GST_OBJECT_LOCK (enc);
2246 result = enc->priv->drainable;
2247 GST_OBJECT_UNLOCK (enc);
2253 * gst_audio_encoder_merge_tags:
2254 * @enc: a #GstAudioEncoder
2255 * @tags: a #GstTagList to merge
2256 * @mode: the #GstTagMergeMode to use
2258 * Adds tags to so-called pending tags, which will be processed
2259 * before pushing out data downstream.
2261 * Note that this is provided for convenience, and the subclass is
2262 * not required to use this and can still do tag handling on its own,
2263 * although it should be aware that baseclass already takes care
2264 * of the usual CODEC/AUDIO_CODEC tags.
2271 gst_audio_encoder_merge_tags (GstAudioEncoder * enc,
2272 const GstTagList * tags, GstTagMergeMode mode)
2276 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
2277 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2279 GST_OBJECT_LOCK (enc);
2281 GST_DEBUG_OBJECT (enc, "merging tags %" GST_PTR_FORMAT, tags);
2282 otags = enc->priv->tags;
2283 enc->priv->tags = gst_tag_list_merge (enc->priv->tags, tags, mode);
2285 gst_tag_list_free (otags);
2286 GST_OBJECT_UNLOCK (enc);