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:gstbaseaudioencoder
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 * GstBaseAudioEncoder and subclass should cooperate as follows.
34 * <itemizedlist><title>Configuration</title>
36 * Initially, GstBaseAudioEncoder calls @start when the encoder element
37 * is activated, which allows subclass to perform any global setup.
40 * GstBaseAudioEncoder 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 * GstBaseAudioEncoder calls @stop at end of all processing.
52 * As of configuration stage, and throughout processing, GstBaseAudioEncoder
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_base_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 GstBaseAudioEncoderClass 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 * GstBaseAudioEncoder 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_base_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 #GstBaseAudioEncoder:perfect-ts.
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 #GstBaseAudioEncoder: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-ts case, an upstream variation exceeding
112 * 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-ts case, this one optionally
115 * (see #GstBaseAudioEncoder: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_base_audio_encoder_finish_frame.
154 #include "gstbaseaudioencoder.h"
155 #include <gst/base/gstadapter.h>
156 #include <gst/audio/audio.h>
162 GST_DEBUG_CATEGORY_STATIC (gst_base_audio_encoder_debug);
163 #define GST_CAT_DEFAULT gst_base_audio_encoder_debug
165 #define GST_BASE_AUDIO_ENCODER_GET_PRIVATE(obj) \
166 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_AUDIO_ENCODER, \
167 GstBaseAudioEncoderPrivate))
178 #define DEFAULT_PERFECT_TS FALSE
179 #define DEFAULT_GRANULE FALSE
180 #define DEFAULT_HARD_RESYNC FALSE
181 #define DEFAULT_TOLERANCE 40000000
183 typedef struct _GstBaseAudioEncoderContext
192 /* MT-protected (with LOCK) */
193 GstClockTime min_latency;
194 GstClockTime max_latency;
195 } GstBaseAudioEncoderContext;
197 struct _GstBaseAudioEncoderPrivate
199 /* activation status */
202 /* input base/first ts as basis for output ts;
203 * kept nearly constant for perfect_ts,
204 * otherwise resyncs to upstream ts */
205 GstClockTime base_ts;
206 /* corresponding base granulepos */
208 /* input samples processed and sent downstream so far (w.r.t. base_ts) */
211 /* currently collected sample data */
213 /* offset in adapter up to which already supplied to encoder */
215 /* mark outgoing discont */
217 /* to guess duration of drained data */
218 GstClockTime last_duration;
220 /* subclass provided data in processing round */
222 /* subclass gave all it could already */
224 /* subclass currently being forcibly drained */
227 /* output bps estimatation */
228 /* global in samples seen */
230 /* global bytes sent out */
233 /* context storage */
234 GstBaseAudioEncoderContext ctx;
239 gboolean hard_resync;
244 static GstElementClass *parent_class = NULL;
246 static void gst_base_audio_encoder_class_init (GstBaseAudioEncoderClass *
248 static void gst_base_audio_encoder_init (GstBaseAudioEncoder * parse,
249 GstBaseAudioEncoderClass * klass);
252 gst_base_audio_encoder_get_type (void)
254 static GType base_audio_encoder_type = 0;
256 if (!base_audio_encoder_type) {
257 static const GTypeInfo base_audio_encoder_info = {
258 sizeof (GstBaseAudioEncoderClass),
259 (GBaseInitFunc) NULL,
260 (GBaseFinalizeFunc) NULL,
261 (GClassInitFunc) gst_base_audio_encoder_class_init,
264 sizeof (GstBaseAudioEncoder),
266 (GInstanceInitFunc) gst_base_audio_encoder_init,
268 const GInterfaceInfo preset_interface_info = {
269 NULL, /* interface_init */
270 NULL, /* interface_finalize */
271 NULL /* interface_data */
274 base_audio_encoder_type = g_type_register_static (GST_TYPE_ELEMENT,
275 "GstBaseAudioEncoder", &base_audio_encoder_info, G_TYPE_FLAG_ABSTRACT);
277 g_type_add_interface_static (base_audio_encoder_type, GST_TYPE_PRESET,
278 &preset_interface_info);
280 return base_audio_encoder_type;
283 static void gst_base_audio_encoder_finalize (GObject * object);
284 static void gst_base_audio_encoder_reset (GstBaseAudioEncoder * enc,
287 static void gst_base_audio_encoder_set_property (GObject * object,
288 guint prop_id, const GValue * value, GParamSpec * pspec);
289 static void gst_base_audio_encoder_get_property (GObject * object,
290 guint prop_id, GValue * value, GParamSpec * pspec);
292 static gboolean gst_base_audio_encoder_sink_activate_push (GstPad * pad,
295 static gboolean gst_base_audio_encoder_sink_event (GstPad * pad,
297 static GstFlowReturn gst_base_audio_encoder_chain (GstPad * pad,
299 static gboolean gst_base_audio_encoder_src_query (GstPad * pad,
301 static gboolean gst_base_audio_encoder_sink_query (GstPad * pad,
303 static const GstQueryType *gst_base_audio_encoder_get_query_types (GstPad *
305 static GstCaps *gst_base_audio_encoder_sink_getcaps (GstPad * pad,
310 gst_base_audio_encoder_class_init (GstBaseAudioEncoderClass * klass)
312 GObjectClass *gobject_class;
314 gobject_class = G_OBJECT_CLASS (klass);
315 parent_class = g_type_class_peek_parent (klass);
317 GST_DEBUG_CATEGORY_INIT (gst_base_audio_encoder_debug, "baseaudioencoder", 0,
318 "baseaudioencoder element");
320 g_type_class_add_private (klass, sizeof (GstBaseAudioEncoderPrivate));
322 gobject_class->set_property = gst_base_audio_encoder_set_property;
323 gobject_class->get_property = gst_base_audio_encoder_get_property;
325 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_audio_encoder_finalize);
328 g_object_class_install_property (gobject_class, PROP_PERFECT_TS,
329 g_param_spec_boolean ("perfect-timestamp", "Perfect Timestamps",
330 "Favour perfect timestamps over tracking upstream timestamps",
331 DEFAULT_PERFECT_TS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
332 g_object_class_install_property (gobject_class, PROP_GRANULE,
333 g_param_spec_boolean ("mark-granule", "Granule Marking",
334 "Apply granule semantics to buffer metadata (implies perfect-ts)",
335 DEFAULT_GRANULE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
336 g_object_class_install_property (gobject_class, PROP_HARD_RESYNC,
337 g_param_spec_boolean ("hard-resync", "Hard Resync",
338 "Perform clipping and sample flushing upon discontinuity",
339 DEFAULT_HARD_RESYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (gobject_class, PROP_TOLERANCE,
341 g_param_spec_int64 ("tolerance", "Tolerance",
342 "Consider discontinuity if timestamp jitter/imperfection exceeds tolerance (ns)",
343 0, G_MAXINT64, DEFAULT_TOLERANCE,
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348 gst_base_audio_encoder_init (GstBaseAudioEncoder * enc,
349 GstBaseAudioEncoderClass * bclass)
351 GstPadTemplate *pad_template;
353 GST_DEBUG_OBJECT (enc, "gst_base_audio_encoder_init");
355 enc->priv = GST_BASE_AUDIO_ENCODER_GET_PRIVATE (enc);
357 /* only push mode supported */
359 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
360 g_return_if_fail (pad_template != NULL);
361 enc->sinkpad = gst_pad_new_from_template (pad_template, "sink");
362 gst_pad_set_event_function (enc->sinkpad,
363 GST_DEBUG_FUNCPTR (gst_base_audio_encoder_sink_event));
364 gst_pad_set_getcaps_function (enc->sinkpad,
365 GST_DEBUG_FUNCPTR (gst_base_audio_encoder_sink_getcaps));
366 gst_pad_set_query_function (enc->sinkpad,
367 GST_DEBUG_FUNCPTR (gst_base_audio_encoder_sink_query));
368 gst_pad_set_chain_function (enc->sinkpad,
369 GST_DEBUG_FUNCPTR (gst_base_audio_encoder_chain));
370 gst_pad_set_activatepush_function (enc->sinkpad,
371 GST_DEBUG_FUNCPTR (gst_base_audio_encoder_sink_activate_push));
372 gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad);
374 GST_DEBUG_OBJECT (enc, "sinkpad created");
376 /* and we don't mind upstream traveling stuff that much ... */
378 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
379 g_return_if_fail (pad_template != NULL);
380 enc->srcpad = gst_pad_new_from_template (pad_template, "src");
381 gst_pad_set_query_function (enc->srcpad,
382 GST_DEBUG_FUNCPTR (gst_base_audio_encoder_src_query));
383 gst_pad_set_query_type_function (enc->srcpad,
384 GST_DEBUG_FUNCPTR (gst_base_audio_encoder_get_query_types));
385 gst_pad_use_fixed_caps (enc->srcpad);
386 gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad);
387 GST_DEBUG_OBJECT (enc, "src created");
389 enc->priv->adapter = gst_adapter_new ();
391 /* property default */
392 enc->priv->granule = DEFAULT_GRANULE;
393 enc->priv->perfect_ts = DEFAULT_PERFECT_TS;
394 enc->priv->hard_resync = DEFAULT_HARD_RESYNC;
395 enc->priv->tolerance = DEFAULT_TOLERANCE;
398 gst_base_audio_encoder_reset (enc, TRUE);
399 GST_DEBUG_OBJECT (enc, "init ok");
403 gst_base_audio_encoder_reset (GstBaseAudioEncoder * enc, gboolean full)
405 GST_OBJECT_LOCK (enc);
408 enc->priv->active = FALSE;
409 enc->priv->samples_in = 0;
410 enc->priv->bytes_out = 0;
411 gst_audio_info_init (&enc->priv->ctx.info);
412 memset (&enc->priv->ctx, 0, sizeof (enc->priv->ctx));
415 gst_segment_init (&enc->segment, GST_FORMAT_TIME);
417 gst_adapter_clear (enc->priv->adapter);
418 enc->priv->got_data = FALSE;
419 enc->priv->drained = TRUE;
420 enc->priv->offset = 0;
421 enc->priv->base_ts = GST_CLOCK_TIME_NONE;
422 enc->priv->base_gp = -1;
423 enc->priv->samples = 0;
424 enc->priv->discont = FALSE;
426 GST_OBJECT_UNLOCK (enc);
430 gst_base_audio_encoder_finalize (GObject * object)
432 GstBaseAudioEncoder *enc = GST_BASE_AUDIO_ENCODER (object);
434 g_object_unref (enc->priv->adapter);
436 G_OBJECT_CLASS (parent_class)->finalize (object);
440 * gst_base_audio_encoder_finish_frame:
441 * @enc: a #GstBaseAudioEncoder
442 * @buffer: encoded data
443 * @samples: number of samples (per channel) represented by encoded data
445 * Collects encoded data and/or pushes encoded data downstream.
446 * Source pad caps must be set when this is called. Depending on the nature
447 * of the (framing of) the format, subclass can decide whether to push
448 * encoded data directly or to collect various "frames" in a single buffer.
449 * Note that the latter behaviour is recommended whenever the format is allowed,
450 * as it incurs no additional latency and avoids otherwise generating a
451 * a multitude of (small) output buffers. If not explicitly pushed,
452 * any available encoded data is pushed at the end of each processing cycle,
453 * i.e. which encodes as much data as available input data allows.
455 * If @samples < 0, then best estimate is all samples provided to encoder
456 * (subclass) so far. @buf may be NULL, in which case next number of @samples
457 * are considered discarded, e.g. as a result of discontinuous transmission,
458 * and a discontinuity is marked (note that @buf == NULL => push == TRUE).
460 * Returns: a #GstFlowReturn that should be escalated to caller (of caller)
465 gst_base_audio_encoder_finish_frame (GstBaseAudioEncoder * enc, GstBuffer * buf,
468 GstBaseAudioEncoderClass *klass;
469 GstBaseAudioEncoderPrivate *priv;
470 GstBaseAudioEncoderContext *ctx;
471 GstFlowReturn ret = GST_FLOW_OK;
473 klass = GST_BASE_AUDIO_ENCODER_GET_CLASS (enc);
475 ctx = &enc->priv->ctx;
477 /* subclass should know what it is producing by now */
478 g_return_val_if_fail (gst_pad_has_current_caps (enc->srcpad), GST_FLOW_ERROR);
479 /* subclass should not hand us no data */
480 g_return_val_if_fail (buf == NULL || gst_buffer_get_size (buf) > 0,
483 GST_LOG_OBJECT (enc, "accepting %d bytes encoded data as %d samples",
484 buf ? gst_buffer_get_size (buf) : -1, samples);
486 /* mark subclass still alive and providing */
487 priv->got_data = TRUE;
489 /* remove corresponding samples from input */
491 samples = (enc->priv->offset / ctx->info.bpf);
493 if (G_LIKELY (samples)) {
494 /* track upstream ts if so configured */
495 if (!enc->priv->perfect_ts) {
496 guint64 ts, distance;
498 ts = gst_adapter_prev_timestamp (priv->adapter, &distance);
499 g_assert (distance % ctx->info.bpf == 0);
500 distance /= ctx->info.bpf;
501 GST_LOG_OBJECT (enc, "%" G_GUINT64_FORMAT " samples past prev_ts %"
502 GST_TIME_FORMAT, distance, GST_TIME_ARGS (ts));
503 GST_LOG_OBJECT (enc, "%" G_GUINT64_FORMAT " samples past base_ts %"
504 GST_TIME_FORMAT, priv->samples, GST_TIME_ARGS (priv->base_ts));
505 /* when draining adapter might be empty and no ts to offer */
506 if (GST_CLOCK_TIME_IS_VALID (ts) && ts != priv->base_ts) {
507 GstClockTimeDiff diff;
508 GstClockTime old_ts, next_ts;
510 /* passed into another buffer;
511 * mild check for discontinuity and only mark if so */
513 gst_util_uint64_scale (distance, GST_SECOND, ctx->info.rate);
514 old_ts = priv->base_ts +
515 gst_util_uint64_scale (priv->samples, GST_SECOND, ctx->info.rate);
516 diff = GST_CLOCK_DIFF (next_ts, old_ts);
517 GST_LOG_OBJECT (enc, "ts diff %d ms", (gint) (diff / GST_MSECOND));
518 /* only mark discontinuity if beyond tolerance */
519 if (G_UNLIKELY (diff < -enc->priv->tolerance ||
520 diff > enc->priv->tolerance)) {
521 GST_DEBUG_OBJECT (enc, "marked discont");
522 priv->discont = TRUE;
524 if (diff > GST_SECOND / ctx->info.rate / 2 ||
525 diff < -GST_SECOND / ctx->info.rate / 2) {
526 GST_LOG_OBJECT (enc, "new upstream ts %" GST_TIME_FORMAT
527 " at distance %" G_GUINT64_FORMAT, GST_TIME_ARGS (ts), distance);
528 /* re-sync to upstream ts */
530 priv->samples = distance;
532 GST_LOG_OBJECT (enc, "new upstream ts only introduces jitter");
536 /* advance sample view */
537 if (G_UNLIKELY (samples * ctx->info.bpf > priv->offset)) {
538 if (G_LIKELY (!priv->force)) {
539 /* no way we can let this pass */
540 g_assert_not_reached ();
545 if (samples * ctx->info.bpf >= gst_adapter_available (priv->adapter))
546 gst_adapter_clear (priv->adapter);
548 gst_adapter_flush (priv->adapter, samples * ctx->info.bpf);
551 gst_adapter_flush (priv->adapter, samples * ctx->info.bpf);
552 priv->offset -= samples * ctx->info.bpf;
553 /* avoid subsequent stray prev_ts */
554 if (G_UNLIKELY (gst_adapter_available (priv->adapter) == 0))
555 gst_adapter_clear (priv->adapter);
557 /* sample count advanced below after buffer handling */
561 if (G_LIKELY (buf)) {
564 size = gst_buffer_get_size (buf);
566 GST_LOG_OBJECT (enc, "taking %d bytes for output", size);
567 buf = gst_buffer_make_writable (buf);
570 if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (priv->base_ts))) {
571 /* FIXME ? lookahead could lead to weird ts and duration ?
572 * (particularly if not in perfect mode) */
573 /* mind sample rounding and produce perfect output */
574 GST_BUFFER_TIMESTAMP (buf) = priv->base_ts +
575 gst_util_uint64_scale (priv->samples - ctx->lookahead, GST_SECOND,
577 GST_DEBUG_OBJECT (enc, "out samples %d", samples);
578 if (G_LIKELY (samples > 0)) {
579 priv->samples += samples;
580 GST_BUFFER_DURATION (buf) = priv->base_ts +
581 gst_util_uint64_scale (priv->samples - ctx->lookahead, GST_SECOND,
582 ctx->info.rate) - GST_BUFFER_TIMESTAMP (buf);
583 priv->last_duration = GST_BUFFER_DURATION (buf);
585 /* duration forecast in case of handling remainder;
586 * the last one is probably like the previous one ... */
587 GST_BUFFER_DURATION (buf) = priv->last_duration;
589 if (priv->base_gp >= 0) {
591 /* FIXME: in longer run, muxer should take care of this ... */
592 /* offset_end = granulepos for ogg muxer */
593 GST_BUFFER_OFFSET_END (buf) = priv->base_gp + priv->samples -
594 enc->priv->ctx.lookahead;
595 /* offset = timestamp corresponding to granulepos for ogg muxer */
596 GST_BUFFER_OFFSET (buf) =
597 GST_FRAMES_TO_CLOCK_TIME (GST_BUFFER_OFFSET_END (buf),
600 GST_BUFFER_OFFSET (buf) = priv->bytes_out;
601 GST_BUFFER_OFFSET_END (buf) = priv->bytes_out + size;
605 priv->bytes_out += size;
607 if (G_UNLIKELY (priv->discont)) {
608 GST_LOG_OBJECT (enc, "marking discont");
609 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
610 priv->discont = FALSE;
613 if (klass->pre_push) {
614 /* last chance for subclass to do some dirty stuff */
615 ret = klass->pre_push (enc, &buf);
616 if (ret != GST_FLOW_OK || !buf) {
617 GST_DEBUG_OBJECT (enc, "subclass returned %s, buf %p",
618 gst_flow_get_name (ret), buf);
620 gst_buffer_unref (buf);
625 GST_LOG_OBJECT (enc, "pushing buffer of size %d with ts %" GST_TIME_FORMAT
626 ", duration %" GST_TIME_FORMAT, size,
627 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
628 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
630 ret = gst_pad_push (enc->srcpad, buf);
631 GST_LOG_OBJECT (enc, "buffer pushed: %s", gst_flow_get_name (ret));
633 /* merely advance samples, most work for that already done above */
634 priv->samples += samples;
643 GST_ELEMENT_ERROR (enc, STREAM, ENCODE,
644 ("received more encoded samples %d than provided %d",
645 samples, priv->offset / ctx->info.bpf), (NULL));
647 gst_buffer_unref (buf);
648 return GST_FLOW_ERROR;
652 /* adapter tracking idea:
653 * - start of adapter corresponds with what has already been encoded
654 * (i.e. really returned by encoder subclass)
655 * - start + offset is what needs to be fed to subclass next */
657 gst_base_audio_encoder_push_buffers (GstBaseAudioEncoder * enc, gboolean force)
659 GstBaseAudioEncoderClass *klass;
660 GstBaseAudioEncoderPrivate *priv;
661 GstBaseAudioEncoderContext *ctx;
664 GstFlowReturn ret = GST_FLOW_OK;
666 klass = GST_BASE_AUDIO_ENCODER_GET_CLASS (enc);
668 g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
671 ctx = &enc->priv->ctx;
673 while (ret == GST_FLOW_OK) {
676 av = gst_adapter_available (priv->adapter);
678 g_assert (priv->offset <= av);
681 need = ctx->frame_samples > 0 ? ctx->frame_samples * ctx->info.bpf : av;
682 GST_LOG_OBJECT (enc, "available: %d, needed: %d, force: %d",
685 if ((need > av) || !av) {
686 if (G_UNLIKELY (force)) {
696 /* if we have some extra metadata,
697 * provide for integer multiple of frames to allow for better granularity
699 if (ctx->frame_samples > 0 && need) {
700 if (ctx->frame_max > 1)
701 need = need * MIN ((av / need), ctx->frame_max);
702 else if (ctx->frame_max == 0)
703 need = need * (av / need);
709 data = gst_adapter_map (priv->adapter, priv->offset + need);
711 gst_buffer_new_wrapped_full ((gpointer) data, NULL, priv->offset,
715 GST_LOG_OBJECT (enc, "providing subclass with %d bytes at offset %d",
718 /* mark this already as consumed,
719 * which it should be when subclass gives us data in exchange for samples */
720 priv->offset += need;
721 priv->samples_in += need / ctx->info.bpf;
723 priv->got_data = FALSE;
724 ret = klass->handle_frame (enc, buf);
726 if (G_LIKELY (buf)) {
727 gst_buffer_unref (buf);
728 gst_adapter_unmap (priv->adapter, 0);
731 /* no data to feed, no leftover provided, then bail out */
732 if (G_UNLIKELY (!buf && !priv->got_data)) {
733 priv->drained = TRUE;
734 GST_LOG_OBJECT (enc, "no more data drained from subclass");
743 gst_base_audio_encoder_drain (GstBaseAudioEncoder * enc)
745 if (enc->priv->drained)
748 return gst_base_audio_encoder_push_buffers (enc, TRUE);
752 gst_base_audio_encoder_set_base_gp (GstBaseAudioEncoder * enc)
756 if (!enc->priv->granule)
759 /* use running time for granule */
760 /* incoming data is clipped, so a valid input should yield a valid output */
761 ts = gst_segment_to_running_time (&enc->segment, GST_FORMAT_TIME,
763 if (GST_CLOCK_TIME_IS_VALID (ts)) {
765 GST_CLOCK_TIME_TO_FRAMES (enc->priv->base_ts, enc->priv->ctx.info.rate);
766 GST_DEBUG_OBJECT (enc, "new base gp %" G_GINT64_FORMAT, enc->priv->base_gp);
768 /* should reasonably have a valid base,
769 * otherwise start at 0 if we did not already start there earlier */
770 if (enc->priv->base_gp < 0) {
771 enc->priv->base_gp = 0;
772 GST_DEBUG_OBJECT (enc, "new base gp %" G_GINT64_FORMAT,
779 gst_base_audio_encoder_chain (GstPad * pad, GstBuffer * buffer)
781 GstBaseAudioEncoder *enc;
782 GstBaseAudioEncoderPrivate *priv;
783 GstBaseAudioEncoderContext *ctx;
784 GstFlowReturn ret = GST_FLOW_OK;
788 enc = GST_BASE_AUDIO_ENCODER (GST_OBJECT_PARENT (pad));
791 ctx = &enc->priv->ctx;
793 /* should know what is coming by now */
797 size = gst_buffer_get_size (buffer);
800 "received buffer of size %d with ts %" GST_TIME_FORMAT
801 ", duration %" GST_TIME_FORMAT, size,
802 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
803 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
805 /* input shoud be whole number of sample frames */
806 if (size % ctx->info.bpf)
809 #ifndef GST_DISABLE_GST_DEBUG
811 GstClockTime duration;
812 GstClockTimeDiff diff;
814 /* verify buffer duration */
815 duration = gst_util_uint64_scale (size, GST_SECOND,
816 ctx->info.rate * ctx->info.bpf);
817 diff = GST_CLOCK_DIFF (duration, GST_BUFFER_DURATION (buffer));
818 if (GST_BUFFER_DURATION (buffer) != GST_CLOCK_TIME_NONE &&
819 (diff > GST_SECOND / ctx->info.rate / 2 ||
820 diff < -GST_SECOND / ctx->info.rate / 2)) {
821 GST_DEBUG_OBJECT (enc, "incoming buffer had incorrect duration %"
822 GST_TIME_FORMAT ", expected duration %" GST_TIME_FORMAT,
823 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
824 GST_TIME_ARGS (duration));
829 discont = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT);
830 if (G_UNLIKELY (discont)) {
831 GST_LOG_OBJECT (buffer, "marked discont");
832 enc->priv->discont = discont;
835 /* clip to segment */
836 /* NOTE: slightly painful linking -laudio only for this one ... */
837 buffer = gst_audio_buffer_clip (buffer, &enc->segment, ctx->info.rate,
839 if (G_UNLIKELY (!buffer)) {
840 GST_DEBUG_OBJECT (buffer, "no data after clipping to segment");
844 size = gst_buffer_get_size (buffer);
847 "buffer after segment clipping has size %d with ts %" GST_TIME_FORMAT
848 ", duration %" GST_TIME_FORMAT, size,
849 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
850 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
852 if (!GST_CLOCK_TIME_IS_VALID (priv->base_ts)) {
853 priv->base_ts = GST_BUFFER_TIMESTAMP (buffer);
854 GST_DEBUG_OBJECT (enc, "new base ts %" GST_TIME_FORMAT,
855 GST_TIME_ARGS (priv->base_ts));
856 gst_base_audio_encoder_set_base_gp (enc);
859 /* check for continuity;
860 * checked elsewhere in non-perfect case */
861 if (enc->priv->perfect_ts) {
862 GstClockTimeDiff diff = 0;
863 GstClockTime next_ts = 0;
865 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
866 GST_CLOCK_TIME_IS_VALID (priv->base_ts)) {
869 samples = priv->samples +
870 gst_adapter_available (priv->adapter) / ctx->info.bpf;
871 next_ts = priv->base_ts +
872 gst_util_uint64_scale (samples, GST_SECOND, ctx->info.rate);
873 GST_LOG_OBJECT (enc, "buffer is %" G_GUINT64_FORMAT
874 " samples past base_ts %" GST_TIME_FORMAT
875 ", expected ts %" GST_TIME_FORMAT, samples,
876 GST_TIME_ARGS (priv->base_ts), GST_TIME_ARGS (next_ts));
877 diff = GST_CLOCK_DIFF (next_ts, GST_BUFFER_TIMESTAMP (buffer));
878 GST_LOG_OBJECT (enc, "ts diff %d ms", (gint) (diff / GST_MSECOND));
879 /* if within tolerance,
880 * discard buffer ts and carry on producing perfect stream,
881 * otherwise clip or resync to ts */
882 if (G_UNLIKELY (diff < -enc->priv->tolerance ||
883 diff > enc->priv->tolerance)) {
884 GST_DEBUG_OBJECT (enc, "marked discont");
889 /* do some fancy tweaking in hard resync case */
890 if (discont && enc->priv->hard_resync) {
894 GST_WARNING_OBJECT (enc, "Buffer is older than expected ts %"
895 GST_TIME_FORMAT ". Clipping buffer", GST_TIME_ARGS (next_ts));
898 GST_CLOCK_TIME_TO_FRAMES (-diff, ctx->info.rate) * ctx->info.bpf;
899 if (diff_bytes >= size) {
900 gst_buffer_unref (buffer);
903 buffer = gst_buffer_make_writable (buffer);
904 gst_buffer_resize (buffer, diff_bytes, size - diff_bytes);
906 GST_BUFFER_TIMESTAMP (buffer) += diff;
907 /* care even less about duration after this */
909 /* drain stuff prior to resync */
910 gst_base_audio_encoder_drain (enc);
914 priv->base_ts += diff;
915 gst_base_audio_encoder_set_base_gp (enc);
916 priv->discont |= discont;
919 gst_adapter_push (enc->priv->adapter, buffer);
920 /* new stuff, so we can push subclass again */
921 enc->priv->drained = FALSE;
923 ret = gst_base_audio_encoder_push_buffers (enc, FALSE);
926 GST_LOG_OBJECT (enc, "chain leaving");
932 GST_ELEMENT_ERROR (enc, CORE, NEGOTIATION, (NULL),
933 ("encoder not initialized"));
934 gst_buffer_unref (buffer);
935 return GST_FLOW_NOT_NEGOTIATED;
939 GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
940 ("buffer size %d not a multiple of %d", gst_buffer_get_size (buffer),
942 gst_buffer_unref (buffer);
943 return GST_FLOW_ERROR;
948 audio_info_is_equal (GstAudioInfo * from, GstAudioInfo * to)
952 if (GST_AUDIO_INFO_FORMAT (from) != GST_AUDIO_INFO_FORMAT (to))
954 if (GST_AUDIO_INFO_RATE (from) != GST_AUDIO_INFO_RATE (to))
956 if (GST_AUDIO_INFO_CHANNELS (from) != GST_AUDIO_INFO_CHANNELS (to))
958 if (GST_AUDIO_INFO_CHANNELS (from) > 64)
960 return memcmp (from->position, to->position,
961 GST_AUDIO_INFO_CHANNELS (from) * sizeof (to->position[0]));
965 gst_base_audio_encoder_sink_setcaps (GstBaseAudioEncoder * enc, GstCaps * caps)
967 GstBaseAudioEncoderClass *klass;
968 GstBaseAudioEncoderContext *ctx;
970 gboolean res = TRUE, changed = FALSE;
973 klass = GST_BASE_AUDIO_ENCODER_GET_CLASS (enc);
975 /* subclass must do something here ... */
976 g_return_val_if_fail (klass->set_format != NULL, FALSE);
978 ctx = &enc->priv->ctx;
980 GST_DEBUG_OBJECT (enc, "caps: %" GST_PTR_FORMAT, caps);
982 if (!gst_caps_is_fixed (caps))
985 /* adjust ts tracking to new sample rate */
986 old_rate = GST_AUDIO_INFO_RATE (&ctx->info);
987 if (GST_CLOCK_TIME_IS_VALID (enc->priv->base_ts) && old_rate) {
988 enc->priv->base_ts +=
989 GST_FRAMES_TO_CLOCK_TIME (enc->priv->samples, old_rate);
990 enc->priv->samples = 0;
993 if (!gst_audio_info_from_caps (&state, caps))
996 changed = audio_info_is_equal (&state, &ctx->info);
999 GstClockTime old_min_latency;
1000 GstClockTime old_max_latency;
1002 /* drain any pending old data stuff */
1003 gst_base_audio_encoder_drain (enc);
1005 /* context defaults */
1006 enc->priv->ctx.frame_samples = 0;
1007 enc->priv->ctx.frame_max = 0;
1008 enc->priv->ctx.lookahead = 0;
1010 /* element might report latency */
1011 GST_OBJECT_LOCK (enc);
1012 old_min_latency = ctx->min_latency;
1013 old_max_latency = ctx->max_latency;
1014 GST_OBJECT_UNLOCK (enc);
1016 if (klass->set_format)
1017 res = klass->set_format (enc, &state);
1019 /* notify if new latency */
1020 GST_OBJECT_LOCK (enc);
1021 if ((ctx->min_latency > 0 && ctx->min_latency != old_min_latency) ||
1022 (ctx->max_latency > 0 && ctx->max_latency != old_max_latency)) {
1023 GST_OBJECT_UNLOCK (enc);
1024 /* post latency message on the bus */
1025 gst_element_post_message (GST_ELEMENT (enc),
1026 gst_message_new_latency (GST_OBJECT (enc)));
1027 GST_OBJECT_LOCK (enc);
1029 GST_OBJECT_UNLOCK (enc);
1031 GST_DEBUG_OBJECT (enc, "new audio format identical to configured format");
1039 GST_WARNING_OBJECT (enc, "rejected caps %" GST_PTR_FORMAT, caps);
1046 * gst_base_audio_encoder_proxy_getcaps:
1047 * @enc: a #GstBaseAudioEncoder
1048 * @caps: initial caps
1050 * Returns caps that express @caps (or sink template caps if @caps == NULL)
1051 * restricted to channel/rate combinations supported by downstream elements
1054 * Returns: a #GstCaps owned by caller
1059 gst_base_audio_encoder_proxy_getcaps (GstBaseAudioEncoder * enc, GstCaps * caps)
1061 const GstCaps *templ_caps;
1062 GstCaps *allowed = NULL;
1063 GstCaps *fcaps, *filter_caps;
1066 /* we want to be able to communicate to upstream elements like audioconvert
1067 * and audioresample any rate/channel restrictions downstream (e.g. muxer
1068 * only accepting certain sample rates) */
1069 templ_caps = caps ? caps : gst_pad_get_pad_template_caps (enc->sinkpad);
1070 allowed = gst_pad_get_allowed_caps (enc->srcpad);
1071 if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
1072 fcaps = gst_caps_copy (templ_caps);
1076 GST_LOG_OBJECT (enc, "template caps %" GST_PTR_FORMAT, templ_caps);
1077 GST_LOG_OBJECT (enc, "allowed caps %" GST_PTR_FORMAT, allowed);
1079 filter_caps = gst_caps_new_empty ();
1081 for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
1084 q_name = gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
1086 /* pick rate + channel fields from allowed caps */
1087 for (j = 0; j < gst_caps_get_size (allowed); j++) {
1088 const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
1092 s = gst_structure_id_empty_new (q_name);
1093 if ((val = gst_structure_get_value (allowed_s, "rate")))
1094 gst_structure_set_value (s, "rate", val);
1095 if ((val = gst_structure_get_value (allowed_s, "channels")))
1096 gst_structure_set_value (s, "channels", val);
1098 gst_caps_merge_structure (filter_caps, s);
1102 fcaps = gst_caps_intersect (filter_caps, templ_caps);
1103 gst_caps_unref (filter_caps);
1106 gst_caps_replace (&allowed, NULL);
1108 GST_LOG_OBJECT (enc, "proxy caps %" GST_PTR_FORMAT, fcaps);
1114 gst_base_audio_encoder_sink_getcaps (GstPad * pad, GstCaps * filter)
1116 GstBaseAudioEncoder *enc;
1117 GstBaseAudioEncoderClass *klass;
1120 enc = GST_BASE_AUDIO_ENCODER (gst_pad_get_parent (pad));
1121 klass = GST_BASE_AUDIO_ENCODER_GET_CLASS (enc);
1122 g_assert (pad == enc->sinkpad);
1125 caps = klass->getcaps (enc, filter);
1127 caps = gst_base_audio_encoder_proxy_getcaps (enc, NULL);
1128 gst_object_unref (enc);
1130 GST_LOG_OBJECT (enc, "returning caps %" GST_PTR_FORMAT, caps);
1136 gst_base_audio_encoder_sink_eventfunc (GstBaseAudioEncoder * enc,
1139 GstBaseAudioEncoderClass *klass;
1140 gboolean handled = FALSE;
1142 klass = GST_BASE_AUDIO_ENCODER_GET_CLASS (enc);
1144 switch (GST_EVENT_TYPE (event)) {
1145 case GST_EVENT_SEGMENT:
1149 gst_event_copy_segment (event, &seg);
1151 if (seg.format == GST_FORMAT_TIME) {
1152 GST_DEBUG_OBJECT (enc, "received TIME SEGMENT %" GST_PTR_FORMAT, &seg);
1154 GST_DEBUG_OBJECT (enc, "received SEGMENT %" GST_PTR_FORMAT, &seg);
1155 GST_DEBUG_OBJECT (enc, "unsupported format; ignoring");
1159 /* finish current segment */
1160 gst_base_audio_encoder_drain (enc);
1161 /* reset partially for new segment */
1162 gst_base_audio_encoder_reset (enc, FALSE);
1163 /* and follow along with segment */
1168 case GST_EVENT_FLUSH_START:
1171 case GST_EVENT_FLUSH_STOP:
1172 /* discard any pending stuff */
1173 /* TODO route through drain ?? */
1174 if (!enc->priv->drained && klass->flush)
1176 /* and get (re)set for the sequel */
1177 gst_base_audio_encoder_reset (enc, FALSE);
1181 gst_base_audio_encoder_drain (enc);
1184 case GST_EVENT_CAPS:
1188 gst_event_parse_caps (event, &caps);
1189 gst_base_audio_encoder_sink_setcaps (enc, caps);
1190 gst_event_unref (event);
1203 gst_base_audio_encoder_sink_event (GstPad * pad, GstEvent * event)
1205 GstBaseAudioEncoder *enc;
1206 GstBaseAudioEncoderClass *klass;
1207 gboolean handled = FALSE;
1208 gboolean ret = TRUE;
1210 enc = GST_BASE_AUDIO_ENCODER (gst_pad_get_parent (pad));
1211 klass = GST_BASE_AUDIO_ENCODER_GET_CLASS (enc);
1213 GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
1214 GST_EVENT_TYPE_NAME (event));
1217 handled = klass->event (enc, event);
1220 handled = gst_base_audio_encoder_sink_eventfunc (enc, event);
1223 ret = gst_pad_event_default (pad, event);
1225 GST_DEBUG_OBJECT (enc, "event handled");
1227 gst_object_unref (enc);
1232 gst_base_audio_encoder_sink_query (GstPad * pad, GstQuery * query)
1234 gboolean res = TRUE;
1235 GstBaseAudioEncoder *enc;
1237 enc = GST_BASE_AUDIO_ENCODER (gst_pad_get_parent (pad));
1239 switch (GST_QUERY_TYPE (query)) {
1240 case GST_QUERY_FORMATS:
1242 gst_query_set_formats (query, 3,
1243 GST_FORMAT_TIME, GST_FORMAT_BYTES, GST_FORMAT_DEFAULT);
1247 case GST_QUERY_CONVERT:
1249 GstFormat src_fmt, dest_fmt;
1250 gint64 src_val, dest_val;
1252 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1253 if (!(res = gst_audio_info_convert (&enc->priv->ctx.info,
1254 src_fmt, src_val, dest_fmt, &dest_val)))
1256 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1260 res = gst_pad_query_default (pad, query);
1265 gst_object_unref (enc);
1269 static const GstQueryType *
1270 gst_base_audio_encoder_get_query_types (GstPad * pad)
1272 static const GstQueryType gst_base_audio_encoder_src_query_types[] = {
1280 return gst_base_audio_encoder_src_query_types;
1284 * gst_base_audio_encoded_audio_convert:
1285 * @fmt: audio format of the encoded audio
1286 * @bytes: number of encoded bytes
1287 * @samples: number of encoded samples
1288 * @src_format: source format
1289 * @src_value: source value
1290 * @dest_format: destination format
1291 * @dest_value: destination format
1293 * Helper function to convert @src_value in @src_format to @dest_value in
1294 * @dest_format for encoded audio data. Conversion is possible between
1295 * BYTE and TIME format by using estimated bitrate based on
1296 * @samples and @bytes (and @fmt).
1300 /* FIXME: make gst_base_audio_encoded_audio_convert() public? */
1302 gst_base_audio_encoded_audio_convert (GstAudioInfo * fmt,
1303 gint64 bytes, gint64 samples, GstFormat src_format,
1304 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
1306 gboolean res = FALSE;
1308 g_return_val_if_fail (dest_format != NULL, FALSE);
1309 g_return_val_if_fail (dest_value != NULL, FALSE);
1311 if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
1314 *dest_value = src_value;
1318 if (samples == 0 || bytes == 0 || fmt->rate == 0) {
1319 GST_DEBUG ("not enough metadata yet to convert");
1325 switch (src_format) {
1326 case GST_FORMAT_BYTES:
1327 switch (*dest_format) {
1328 case GST_FORMAT_TIME:
1329 *dest_value = gst_util_uint64_scale (src_value,
1330 GST_SECOND * samples, bytes);
1337 case GST_FORMAT_TIME:
1338 switch (*dest_format) {
1339 case GST_FORMAT_BYTES:
1340 *dest_value = gst_util_uint64_scale (src_value, bytes,
1341 samples * GST_SECOND);
1356 /* FIXME ? are any of these queries (other than latency) an encoder's business
1357 * also, the conversion stuff might seem to make sense, but seems to not mind
1358 * segment stuff etc at all
1359 * Supposedly that's backward compatibility ... */
1361 gst_base_audio_encoder_src_query (GstPad * pad, GstQuery * query)
1363 GstBaseAudioEncoder *enc;
1365 gboolean res = FALSE;
1367 enc = GST_BASE_AUDIO_ENCODER (GST_PAD_PARENT (pad));
1368 peerpad = gst_pad_get_peer (GST_PAD (enc->sinkpad));
1370 GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
1372 switch (GST_QUERY_TYPE (query)) {
1373 case GST_QUERY_POSITION:
1375 GstFormat fmt, req_fmt;
1378 if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1379 GST_LOG_OBJECT (enc, "returning peer response");
1384 GST_LOG_OBJECT (enc, "no peer");
1388 gst_query_parse_position (query, &req_fmt, NULL);
1389 fmt = GST_FORMAT_TIME;
1390 if (!(res = gst_pad_query_position (peerpad, fmt, &pos)))
1393 if ((res = gst_pad_query_convert (peerpad, fmt, pos, req_fmt, &val))) {
1394 gst_query_set_position (query, req_fmt, val);
1398 case GST_QUERY_DURATION:
1400 GstFormat fmt, req_fmt;
1403 if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1404 GST_LOG_OBJECT (enc, "returning peer response");
1409 GST_LOG_OBJECT (enc, "no peer");
1413 gst_query_parse_duration (query, &req_fmt, NULL);
1414 fmt = GST_FORMAT_TIME;
1415 if (!(res = gst_pad_query_duration (peerpad, fmt, &dur)))
1418 if ((res = gst_pad_query_convert (peerpad, fmt, dur, req_fmt, &val))) {
1419 gst_query_set_duration (query, req_fmt, val);
1423 case GST_QUERY_FORMATS:
1425 gst_query_set_formats (query, 2, GST_FORMAT_TIME, GST_FORMAT_BYTES);
1429 case GST_QUERY_CONVERT:
1431 GstFormat src_fmt, dest_fmt;
1432 gint64 src_val, dest_val;
1434 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1435 if (!(res = gst_base_audio_encoded_audio_convert (&enc->priv->ctx.info,
1436 enc->priv->bytes_out, enc->priv->samples_in, src_fmt, src_val,
1437 &dest_fmt, &dest_val)))
1439 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1442 case GST_QUERY_LATENCY:
1444 if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1446 GstClockTime min_latency, max_latency;
1448 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1449 GST_DEBUG_OBJECT (enc, "Peer latency: live %d, min %"
1450 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1451 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1453 GST_OBJECT_LOCK (enc);
1454 /* add our latency */
1455 if (min_latency != -1)
1456 min_latency += enc->priv->ctx.min_latency;
1457 if (max_latency != -1)
1458 max_latency += enc->priv->ctx.max_latency;
1459 GST_OBJECT_UNLOCK (enc);
1461 gst_query_set_latency (query, live, min_latency, max_latency);
1466 res = gst_pad_query_default (pad, query);
1470 gst_object_unref (peerpad);
1475 gst_base_audio_encoder_set_property (GObject * object, guint prop_id,
1476 const GValue * value, GParamSpec * pspec)
1478 GstBaseAudioEncoder *enc;
1480 enc = GST_BASE_AUDIO_ENCODER (object);
1483 case PROP_PERFECT_TS:
1484 if (enc->priv->granule && !g_value_get_boolean (value))
1485 GST_WARNING_OBJECT (enc, "perfect-ts can not be set FALSE");
1487 enc->priv->perfect_ts = g_value_get_boolean (value);
1489 case PROP_HARD_RESYNC:
1490 enc->priv->hard_resync = g_value_get_boolean (value);
1492 case PROP_TOLERANCE:
1493 enc->priv->tolerance = g_value_get_int64 (value);
1496 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1502 gst_base_audio_encoder_get_property (GObject * object, guint prop_id,
1503 GValue * value, GParamSpec * pspec)
1505 GstBaseAudioEncoder *enc;
1507 enc = GST_BASE_AUDIO_ENCODER (object);
1510 case PROP_PERFECT_TS:
1511 g_value_set_boolean (value, enc->priv->perfect_ts);
1514 g_value_set_boolean (value, enc->priv->granule);
1516 case PROP_HARD_RESYNC:
1517 g_value_set_boolean (value, enc->priv->hard_resync);
1519 case PROP_TOLERANCE:
1520 g_value_set_int64 (value, enc->priv->tolerance);
1523 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1529 gst_base_audio_encoder_activate (GstBaseAudioEncoder * enc, gboolean active)
1531 GstBaseAudioEncoderClass *klass;
1532 gboolean result = FALSE;
1534 klass = GST_BASE_AUDIO_ENCODER_GET_CLASS (enc);
1536 g_return_val_if_fail (!enc->priv->granule || enc->priv->perfect_ts, FALSE);
1538 GST_DEBUG_OBJECT (enc, "activate %d", active);
1541 if (!enc->priv->active && klass->start)
1542 result = klass->start (enc);
1544 /* We must make sure streaming has finished before resetting things
1545 * and calling the ::stop vfunc */
1546 GST_PAD_STREAM_LOCK (enc->sinkpad);
1547 GST_PAD_STREAM_UNLOCK (enc->sinkpad);
1549 if (enc->priv->active && klass->stop)
1550 result = klass->stop (enc);
1553 gst_base_audio_encoder_reset (enc, TRUE);
1555 GST_DEBUG_OBJECT (enc, "activate return: %d", result);
1561 gst_base_audio_encoder_sink_activate_push (GstPad * pad, gboolean active)
1563 gboolean result = TRUE;
1564 GstBaseAudioEncoder *enc;
1566 enc = GST_BASE_AUDIO_ENCODER (gst_pad_get_parent (pad));
1568 GST_DEBUG_OBJECT (enc, "sink activate push %d", active);
1570 result = gst_base_audio_encoder_activate (enc, active);
1573 enc->priv->active = active;
1575 GST_DEBUG_OBJECT (enc, "sink activate push return: %d", result);
1577 gst_object_unref (enc);
1582 * gst_base_audio_encoder_get_audio_info:
1583 * @enc: a #GstBaseAudioEncoder
1585 * Returns: a #GstAudioInfo describing the input audio format
1590 gst_base_audio_encoder_get_audio_info (GstBaseAudioEncoder * enc)
1592 g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), NULL);
1594 return &enc->priv->ctx.info;
1598 * gst_base_audio_encoder_set_frame_samples:
1599 * @enc: a #GstBaseAudioEncoder
1600 * @num: number of samples per frame
1602 * Sets number of samples (per channel) subclass needs to be handed,
1603 * or will be handed all available if 0.
1608 gst_base_audio_encoder_set_frame_samples (GstBaseAudioEncoder * enc, gint num)
1610 g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1612 enc->priv->ctx.frame_samples = num;
1616 * gst_base_audio_encoder_get_frame_samples:
1617 * @enc: a #GstBaseAudioEncoder
1619 * Returns: currently requested samples per frame
1624 gst_base_audio_encoder_get_frame_samples (GstBaseAudioEncoder * enc)
1626 g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), 0);
1628 return enc->priv->ctx.frame_samples;
1632 * gst_base_audio_encoder_set_frame_max:
1633 * @enc: a #GstBaseAudioEncoder
1634 * @num: number of frames
1636 * Sets max number of frames accepted at once (assumed minimally 1)
1641 gst_base_audio_encoder_set_frame_max (GstBaseAudioEncoder * enc, gint num)
1643 g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1645 enc->priv->ctx.frame_max = num;
1649 * gst_base_audio_encoder_get_frame_max:
1650 * @enc: a #GstBaseAudioEncoder
1652 * Returns: currently configured maximum handled frames
1657 gst_base_audio_encoder_get_frame_max (GstBaseAudioEncoder * enc)
1659 g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), 0);
1661 return enc->priv->ctx.frame_max;
1665 * gst_base_audio_encoder_set_lookahead:
1666 * @enc: a #GstBaseAudioEncoder
1669 * Sets encoder lookahead (in units of input rate samples)
1674 gst_base_audio_encoder_set_lookahead (GstBaseAudioEncoder * enc, gint num)
1676 g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1678 enc->priv->ctx.lookahead = num;
1682 * gst_base_audio_encoder_get_lookahead:
1683 * @enc: a #GstBaseAudioEncoder
1685 * Returns: currently configured encoder lookahead
1688 gst_base_audio_encoder_get_lookahead (GstBaseAudioEncoder * enc)
1690 g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), 0);
1692 return enc->priv->ctx.lookahead;
1696 * gst_base_audio_encoder_set_latency:
1697 * @enc: a #GstBaseAudioEncoder
1698 * @min: minimum latency
1699 * @max: maximum latency
1701 * Sets encoder latency.
1706 gst_base_audio_encoder_set_latency (GstBaseAudioEncoder * enc,
1707 GstClockTime min, GstClockTime max)
1709 g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1711 GST_OBJECT_LOCK (enc);
1712 enc->priv->ctx.min_latency = min;
1713 enc->priv->ctx.max_latency = max;
1714 GST_OBJECT_UNLOCK (enc);
1718 * gst_base_audio_encoder_get_latency:
1719 * @enc: a #GstBaseAudioEncoder
1720 * @min: a pointer to storage to hold minimum latency
1721 * @max: a pointer to storage to hold maximum latency
1723 * Returns currently configured latency.
1728 gst_base_audio_encoder_get_latency (GstBaseAudioEncoder * enc,
1729 GstClockTime * min, GstClockTime * max)
1731 g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1733 GST_OBJECT_LOCK (enc);
1735 *min = enc->priv->ctx.min_latency;
1737 *max = enc->priv->ctx.max_latency;
1738 GST_OBJECT_UNLOCK (enc);
1742 * gst_base_audio_encoder_set_mark_granule:
1743 * @enc: a #GstBaseAudioEncoder
1744 * @enabled: new state
1746 * Enable or disable encoder granule handling.
1753 gst_base_audio_encoder_set_mark_granule (GstBaseAudioEncoder * enc,
1756 g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1758 GST_LOG_OBJECT (enc, "enabled: %d", enabled);
1760 GST_OBJECT_LOCK (enc);
1761 enc->priv->granule = enabled;
1762 GST_OBJECT_UNLOCK (enc);
1766 * gst_base_audio_encoder_get_mark_granule:
1767 * @enc: a #GstBaseAudioEncoder
1769 * Queries if the encoder will handle granule marking.
1771 * Returns: TRUE if granule marking is enabled.
1778 gst_base_audio_encoder_get_mark_granule (GstBaseAudioEncoder * enc)
1782 g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), FALSE);
1784 GST_OBJECT_LOCK (enc);
1785 result = enc->priv->granule;
1786 GST_OBJECT_UNLOCK (enc);
1792 * gst_base_audio_encoder_set_perfect_timestamp:
1793 * @enc: a #GstBaseAudioEncoder
1794 * @enabled: new state
1796 * Enable or disable encoder perfect output timestamp preference.
1803 gst_base_audio_encoder_set_perfect_timestamp (GstBaseAudioEncoder * enc,
1806 g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1808 GST_LOG_OBJECT (enc, "enabled: %d", enabled);
1810 GST_OBJECT_LOCK (enc);
1811 enc->priv->perfect_ts = enabled;
1812 GST_OBJECT_UNLOCK (enc);
1816 * gst_base_audio_encoder_get_perfect_timestamp:
1817 * @enc: a #GstBaseAudioEncoder
1819 * Queries encoder perfect timestamp behaviour.
1821 * Returns: TRUE if pefect timestamp setting enabled.
1828 gst_base_audio_encoder_get_perfect_timestamp (GstBaseAudioEncoder * enc)
1832 g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), FALSE);
1834 GST_OBJECT_LOCK (enc);
1835 result = enc->priv->perfect_ts;
1836 GST_OBJECT_UNLOCK (enc);
1842 * gst_base_audio_encoder_set_hard_sync:
1843 * @enc: a #GstBaseAudioEncoder
1844 * @enabled: new state
1846 * Sets encoder hard resync handling.
1853 gst_base_audio_encoder_set_hard_resync (GstBaseAudioEncoder * enc,
1856 g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1858 GST_LOG_OBJECT (enc, "enabled: %d", enabled);
1860 GST_OBJECT_LOCK (enc);
1861 enc->priv->hard_resync = enabled;
1862 GST_OBJECT_UNLOCK (enc);
1866 * gst_base_audio_encoder_get_hard_sync:
1867 * @enc: a #GstBaseAudioEncoder
1869 * Queries encoder's hard resync setting.
1871 * Returns: TRUE if hard resync is enabled.
1878 gst_base_audio_encoder_get_hard_resync (GstBaseAudioEncoder * enc)
1882 g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), FALSE);
1884 GST_OBJECT_LOCK (enc);
1885 result = enc->priv->hard_resync;
1886 GST_OBJECT_UNLOCK (enc);
1892 * gst_base_audio_encoder_set_tolerance:
1893 * @enc: a #GstBaseAudioEncoder
1894 * @tolerance: new tolerance
1896 * Configures encoder audio jitter tolerance threshold.
1903 gst_base_audio_encoder_set_tolerance (GstBaseAudioEncoder * enc,
1906 g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1908 GST_OBJECT_LOCK (enc);
1909 enc->priv->tolerance = tolerance;
1910 GST_OBJECT_UNLOCK (enc);
1914 * gst_base_audio_encoder_get_tolerance:
1915 * @enc: a #GstBaseAudioEncoder
1917 * Queries current audio jitter tolerance threshold.
1919 * Returns: encoder audio jitter tolerance threshold.
1926 gst_base_audio_encoder_get_tolerance (GstBaseAudioEncoder * enc)
1930 g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), 0);
1932 GST_OBJECT_LOCK (enc);
1933 result = enc->priv->tolerance;
1934 GST_OBJECT_UNLOCK (enc);