base: port to 0.11
[platform/upstream/gstreamer.git] / gst-libs / gst / audio / gstbaseaudioencoder.c
1 /* GStreamer
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>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:gstbaseaudioencoder
24  * @short_description: Base class for audio encoders
25  * @see_also: #GstBaseTransform
26  * @since: 0.10.36
27  *
28  * This base class is for audio encoders turning raw audio samples into
29  * encoded audio data.
30  *
31  * GstBaseAudioEncoder and subclass should cooperate as follows.
32  * <orderedlist>
33  * <listitem>
34  *   <itemizedlist><title>Configuration</title>
35  *   <listitem><para>
36  *     Initially, GstBaseAudioEncoder calls @start when the encoder element
37  *     is activated, which allows subclass to perform any global setup.
38  *   </para></listitem>
39  *   <listitem><para>
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.
46  *   </para></listitem>
47  *   <listitem><para>
48  *     GstBaseAudioEncoder calls @stop at end of all processing.
49  *   </para></listitem>
50  *   </itemizedlist>
51  * </listitem>
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.
57  * <listitem>
58  *   <itemizedlist>
59  *   <title>Data processing</title>
60  *     <listitem><para>
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.
63  *     </para></listitem>
64  *     <listitem><para>
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.
69  *     </para></listitem>
70  *     <listitem><para>
71  *       Just prior to actually pushing a buffer downstream,
72  *       it is passed to @pre_push.
73  *     </para></listitem>
74  *     <listitem><para>
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.
78  *     </para></listitem>
79  *   </itemizedlist>
80  * </listitem>
81  * <listitem>
82  *   <itemizedlist><title>Shutdown phase</title>
83  *   <listitem><para>
84  *     GstBaseAudioEncoder class calls @stop to inform the subclass that data
85  *     parsing will be stopped.
86  *   </para></listitem>
87  *   </itemizedlist>
88  * </listitem>
89  * </orderedlist>
90  *
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.
96  *
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.
101  *
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.
119  *
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).
126  *
127  * Things that subclass need to take care of:
128  * <itemizedlist>
129  *   <listitem><para>Provide pad templates</para></listitem>
130  *   <listitem><para>
131  *      Set source pad caps when appropriate
132  *   </para></listitem>
133  *   <listitem><para>
134  *      Inform base class of buffer processing needs using context's
135  *      frame_samples and frame_bytes.
136  *   </para></listitem>
137  *   <listitem><para>
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.
141  *   </para></listitem>
142  *   <listitem><para>
143  *      Accept data in @handle_frame and provide encoded results to
144  *      @gst_base_audio_encoder_finish_frame.
145  *   </para></listitem>
146  * </itemizedlist>
147  *
148  */
149
150 #ifdef HAVE_CONFIG_H
151 #  include "config.h"
152 #endif
153
154 #include "gstbaseaudioencoder.h"
155 #include <gst/base/gstadapter.h>
156 #include <gst/audio/audio.h>
157
158 #include <stdlib.h>
159 #include <string.h>
160
161
162 GST_DEBUG_CATEGORY_STATIC (gst_base_audio_encoder_debug);
163 #define GST_CAT_DEFAULT gst_base_audio_encoder_debug
164
165 #define GST_BASE_AUDIO_ENCODER_GET_PRIVATE(obj)  \
166     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_AUDIO_ENCODER, \
167         GstBaseAudioEncoderPrivate))
168
169 enum
170 {
171   PROP_0,
172   PROP_PERFECT_TS,
173   PROP_GRANULE,
174   PROP_HARD_RESYNC,
175   PROP_TOLERANCE
176 };
177
178 #define DEFAULT_PERFECT_TS   FALSE
179 #define DEFAULT_GRANULE      FALSE
180 #define DEFAULT_HARD_RESYNC  FALSE
181 #define DEFAULT_TOLERANCE    40000000
182
183 typedef struct _GstBaseAudioEncoderContext
184 {
185   /* input */
186   GstAudioInfo info;
187
188   /* output */
189   gint frame_samples;
190   gint frame_max;
191   gint lookahead;
192   /* MT-protected (with LOCK) */
193   GstClockTime min_latency;
194   GstClockTime max_latency;
195 } GstBaseAudioEncoderContext;
196
197 struct _GstBaseAudioEncoderPrivate
198 {
199   /* activation status */
200   gboolean active;
201
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 */
207   gint64 base_gp;
208   /* input samples processed and sent downstream so far (w.r.t. base_ts) */
209   guint64 samples;
210
211   /* currently collected sample data */
212   GstAdapter *adapter;
213   /* offset in adapter up to which already supplied to encoder */
214   gint offset;
215   /* mark outgoing discont */
216   gboolean discont;
217   /* to guess duration of drained data */
218   GstClockTime last_duration;
219
220   /* subclass provided data in processing round */
221   gboolean got_data;
222   /* subclass gave all it could already */
223   gboolean drained;
224   /* subclass currently being forcibly drained */
225   gboolean force;
226
227   /* output bps estimatation */
228   /* global in samples seen */
229   guint64 samples_in;
230   /* global bytes sent out */
231   guint64 bytes_out;
232
233   /* context storage */
234   GstBaseAudioEncoderContext ctx;
235
236   /* properties */
237   gint64 tolerance;
238   gboolean perfect_ts;
239   gboolean hard_resync;
240   gboolean granule;
241 };
242
243
244 static GstElementClass *parent_class = NULL;
245
246 static void gst_base_audio_encoder_class_init (GstBaseAudioEncoderClass *
247     klass);
248 static void gst_base_audio_encoder_init (GstBaseAudioEncoder * parse,
249     GstBaseAudioEncoderClass * klass);
250
251 GType
252 gst_base_audio_encoder_get_type (void)
253 {
254   static GType base_audio_encoder_type = 0;
255
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,
262       NULL,
263       NULL,
264       sizeof (GstBaseAudioEncoder),
265       0,
266       (GInstanceInitFunc) gst_base_audio_encoder_init,
267     };
268     const GInterfaceInfo preset_interface_info = {
269       NULL,                     /* interface_init */
270       NULL,                     /* interface_finalize */
271       NULL                      /* interface_data */
272     };
273
274     base_audio_encoder_type = g_type_register_static (GST_TYPE_ELEMENT,
275         "GstBaseAudioEncoder", &base_audio_encoder_info, G_TYPE_FLAG_ABSTRACT);
276
277     g_type_add_interface_static (base_audio_encoder_type, GST_TYPE_PRESET,
278         &preset_interface_info);
279   }
280   return base_audio_encoder_type;
281 }
282
283 static void gst_base_audio_encoder_finalize (GObject * object);
284 static void gst_base_audio_encoder_reset (GstBaseAudioEncoder * enc,
285     gboolean full);
286
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);
291
292 static gboolean gst_base_audio_encoder_sink_activate_push (GstPad * pad,
293     gboolean active);
294
295 static gboolean gst_base_audio_encoder_sink_event (GstPad * pad,
296     GstEvent * event);
297 static GstFlowReturn gst_base_audio_encoder_chain (GstPad * pad,
298     GstBuffer * buffer);
299 static gboolean gst_base_audio_encoder_src_query (GstPad * pad,
300     GstQuery * query);
301 static gboolean gst_base_audio_encoder_sink_query (GstPad * pad,
302     GstQuery * query);
303 static const GstQueryType *gst_base_audio_encoder_get_query_types (GstPad *
304     pad);
305 static GstCaps *gst_base_audio_encoder_sink_getcaps (GstPad * pad,
306     GstCaps * filter);
307
308
309 static void
310 gst_base_audio_encoder_class_init (GstBaseAudioEncoderClass * klass)
311 {
312   GObjectClass *gobject_class;
313
314   gobject_class = G_OBJECT_CLASS (klass);
315   parent_class = g_type_class_peek_parent (klass);
316
317   GST_DEBUG_CATEGORY_INIT (gst_base_audio_encoder_debug, "baseaudioencoder", 0,
318       "baseaudioencoder element");
319
320   g_type_class_add_private (klass, sizeof (GstBaseAudioEncoderPrivate));
321
322   gobject_class->set_property = gst_base_audio_encoder_set_property;
323   gobject_class->get_property = gst_base_audio_encoder_get_property;
324
325   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_audio_encoder_finalize);
326
327   /* properties */
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));
345 }
346
347 static void
348 gst_base_audio_encoder_init (GstBaseAudioEncoder * enc,
349     GstBaseAudioEncoderClass * bclass)
350 {
351   GstPadTemplate *pad_template;
352
353   GST_DEBUG_OBJECT (enc, "gst_base_audio_encoder_init");
354
355   enc->priv = GST_BASE_AUDIO_ENCODER_GET_PRIVATE (enc);
356
357   /* only push mode supported */
358   pad_template =
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);
373
374   GST_DEBUG_OBJECT (enc, "sinkpad created");
375
376   /* and we don't mind upstream traveling stuff that much ... */
377   pad_template =
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");
388
389   enc->priv->adapter = gst_adapter_new ();
390
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;
396
397   /* init state */
398   gst_base_audio_encoder_reset (enc, TRUE);
399   GST_DEBUG_OBJECT (enc, "init ok");
400 }
401
402 static void
403 gst_base_audio_encoder_reset (GstBaseAudioEncoder * enc, gboolean full)
404 {
405   GST_OBJECT_LOCK (enc);
406
407   if (full) {
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));
413   }
414
415   gst_segment_init (&enc->segment, GST_FORMAT_TIME);
416
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;
425
426   GST_OBJECT_UNLOCK (enc);
427 }
428
429 static void
430 gst_base_audio_encoder_finalize (GObject * object)
431 {
432   GstBaseAudioEncoder *enc = GST_BASE_AUDIO_ENCODER (object);
433
434   g_object_unref (enc->priv->adapter);
435
436   G_OBJECT_CLASS (parent_class)->finalize (object);
437 }
438
439 /**
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
444  *
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.
454  *
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).
459  *
460  * Returns: a #GstFlowReturn that should be escalated to caller (of caller)
461  *
462  * Since: 0.10.36
463  */
464 GstFlowReturn
465 gst_base_audio_encoder_finish_frame (GstBaseAudioEncoder * enc, GstBuffer * buf,
466     gint samples)
467 {
468   GstBaseAudioEncoderClass *klass;
469   GstBaseAudioEncoderPrivate *priv;
470   GstBaseAudioEncoderContext *ctx;
471   GstFlowReturn ret = GST_FLOW_OK;
472
473   klass = GST_BASE_AUDIO_ENCODER_GET_CLASS (enc);
474   priv = enc->priv;
475   ctx = &enc->priv->ctx;
476
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,
481       GST_FLOW_ERROR);
482
483   GST_LOG_OBJECT (enc, "accepting %d bytes encoded data as %d samples",
484       buf ? gst_buffer_get_size (buf) : -1, samples);
485
486   /* mark subclass still alive and providing */
487   priv->got_data = TRUE;
488
489   /* remove corresponding samples from input */
490   if (samples < 0)
491     samples = (enc->priv->offset / ctx->info.bpf);
492
493   if (G_LIKELY (samples)) {
494     /* track upstream ts if so configured */
495     if (!enc->priv->perfect_ts) {
496       guint64 ts, distance;
497
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;
509
510         /* passed into another buffer;
511          * mild check for discontinuity and only mark if so */
512         next_ts = ts +
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;
523         }
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 */
529           priv->base_ts = ts;
530           priv->samples = distance;
531         } else {
532           GST_LOG_OBJECT (enc, "new upstream ts only introduces jitter");
533         }
534       }
535     }
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 ();
541         /* really no way */
542         goto overflow;
543       } else {
544         priv->offset = 0;
545         if (samples * ctx->info.bpf >= gst_adapter_available (priv->adapter))
546           gst_adapter_clear (priv->adapter);
547         else
548           gst_adapter_flush (priv->adapter, samples * ctx->info.bpf);
549       }
550     } else {
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);
556     }
557     /* sample count advanced below after buffer handling */
558   }
559
560   /* collect output */
561   if (G_LIKELY (buf)) {
562     gsize size;
563
564     size = gst_buffer_get_size (buf);
565
566     GST_LOG_OBJECT (enc, "taking %d bytes for output", size);
567     buf = gst_buffer_make_writable (buf);
568
569     /* decorate */
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,
576           ctx->info.rate);
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);
584       } else {
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;
588       }
589       if (priv->base_gp >= 0) {
590         /* pamper oggmux */
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),
598             ctx->info.rate);
599       } else {
600         GST_BUFFER_OFFSET (buf) = priv->bytes_out;
601         GST_BUFFER_OFFSET_END (buf) = priv->bytes_out + size;
602       }
603     }
604
605     priv->bytes_out += size;
606
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;
611     }
612
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);
619         if (buf)
620           gst_buffer_unref (buf);
621         goto exit;
622       }
623     }
624
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)));
629
630     ret = gst_pad_push (enc->srcpad, buf);
631     GST_LOG_OBJECT (enc, "buffer pushed: %s", gst_flow_get_name (ret));
632   } else {
633     /* merely advance samples, most work for that already done above */
634     priv->samples += samples;
635   }
636
637 exit:
638   return ret;
639
640   /* ERRORS */
641 overflow:
642   {
643     GST_ELEMENT_ERROR (enc, STREAM, ENCODE,
644         ("received more encoded samples %d than provided %d",
645             samples, priv->offset / ctx->info.bpf), (NULL));
646     if (buf)
647       gst_buffer_unref (buf);
648     return GST_FLOW_ERROR;
649   }
650 }
651
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 */
656 static GstFlowReturn
657 gst_base_audio_encoder_push_buffers (GstBaseAudioEncoder * enc, gboolean force)
658 {
659   GstBaseAudioEncoderClass *klass;
660   GstBaseAudioEncoderPrivate *priv;
661   GstBaseAudioEncoderContext *ctx;
662   gint av, need;
663   GstBuffer *buf;
664   GstFlowReturn ret = GST_FLOW_OK;
665
666   klass = GST_BASE_AUDIO_ENCODER_GET_CLASS (enc);
667
668   g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
669
670   priv = enc->priv;
671   ctx = &enc->priv->ctx;
672
673   while (ret == GST_FLOW_OK) {
674
675     buf = NULL;
676     av = gst_adapter_available (priv->adapter);
677
678     g_assert (priv->offset <= av);
679     av -= priv->offset;
680
681     need = ctx->frame_samples > 0 ? ctx->frame_samples * ctx->info.bpf : av;
682     GST_LOG_OBJECT (enc, "available: %d, needed: %d, force: %d",
683         av, need, force);
684
685     if ((need > av) || !av) {
686       if (G_UNLIKELY (force)) {
687         priv->force = TRUE;
688         need = av;
689       } else {
690         break;
691       }
692     } else {
693       priv->force = FALSE;
694     }
695
696     /* if we have some extra metadata,
697      * provide for integer multiple of frames to allow for better granularity
698      * of processing */
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);
704     }
705
706     if (need) {
707       const guint8 *data;
708
709       data = gst_adapter_map (priv->adapter, priv->offset + need);
710       buf =
711           gst_buffer_new_wrapped_full ((gpointer) data, NULL, priv->offset,
712           need);
713     }
714
715     GST_LOG_OBJECT (enc, "providing subclass with %d bytes at offset %d",
716         need, priv->offset);
717
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;
722
723     priv->got_data = FALSE;
724     ret = klass->handle_frame (enc, buf);
725
726     if (G_LIKELY (buf)) {
727       gst_buffer_unref (buf);
728       gst_adapter_unmap (priv->adapter, 0);
729     }
730
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");
735       break;
736     }
737   }
738
739   return ret;
740 }
741
742 static GstFlowReturn
743 gst_base_audio_encoder_drain (GstBaseAudioEncoder * enc)
744 {
745   if (enc->priv->drained)
746     return GST_FLOW_OK;
747   else
748     return gst_base_audio_encoder_push_buffers (enc, TRUE);
749 }
750
751 static void
752 gst_base_audio_encoder_set_base_gp (GstBaseAudioEncoder * enc)
753 {
754   GstClockTime ts;
755
756   if (!enc->priv->granule)
757     return;
758
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,
762       enc->priv->base_ts);
763   if (GST_CLOCK_TIME_IS_VALID (ts)) {
764     enc->priv->base_gp =
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);
767   } else {
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,
773           enc->priv->base_gp);
774     }
775   }
776 }
777
778 static GstFlowReturn
779 gst_base_audio_encoder_chain (GstPad * pad, GstBuffer * buffer)
780 {
781   GstBaseAudioEncoder *enc;
782   GstBaseAudioEncoderPrivate *priv;
783   GstBaseAudioEncoderContext *ctx;
784   GstFlowReturn ret = GST_FLOW_OK;
785   gboolean discont;
786   gsize size;
787
788   enc = GST_BASE_AUDIO_ENCODER (GST_OBJECT_PARENT (pad));
789
790   priv = enc->priv;
791   ctx = &enc->priv->ctx;
792
793   /* should know what is coming by now */
794   if (!ctx->info.bpf)
795     goto not_negotiated;
796
797   size = gst_buffer_get_size (buffer);
798
799   GST_LOG_OBJECT (enc,
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)));
804
805   /* input shoud be whole number of sample frames */
806   if (size % ctx->info.bpf)
807     goto wrong_buffer;
808
809 #ifndef GST_DISABLE_GST_DEBUG
810   {
811     GstClockTime duration;
812     GstClockTimeDiff diff;
813
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));
825     }
826   }
827 #endif
828
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;
833   }
834
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,
838       ctx->info.bpf);
839   if (G_UNLIKELY (!buffer)) {
840     GST_DEBUG_OBJECT (buffer, "no data after clipping to segment");
841     goto done;
842   }
843
844   size = gst_buffer_get_size (buffer);
845
846   GST_LOG_OBJECT (enc,
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)));
851
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);
857   }
858
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;
864
865     if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
866         GST_CLOCK_TIME_IS_VALID (priv->base_ts)) {
867       guint64 samples;
868
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");
885         discont = TRUE;
886       }
887     }
888
889     /* do some fancy tweaking in hard resync case */
890     if (discont && enc->priv->hard_resync) {
891       if (diff < 0) {
892         guint64 diff_bytes;
893
894         GST_WARNING_OBJECT (enc, "Buffer is older than expected ts %"
895             GST_TIME_FORMAT ".  Clipping buffer", GST_TIME_ARGS (next_ts));
896
897         diff_bytes =
898             GST_CLOCK_TIME_TO_FRAMES (-diff, ctx->info.rate) * ctx->info.bpf;
899         if (diff_bytes >= size) {
900           gst_buffer_unref (buffer);
901           goto done;
902         }
903         buffer = gst_buffer_make_writable (buffer);
904         gst_buffer_resize (buffer, diff_bytes, size - diff_bytes);
905
906         GST_BUFFER_TIMESTAMP (buffer) += diff;
907         /* care even less about duration after this */
908       } else {
909         /* drain stuff prior to resync */
910         gst_base_audio_encoder_drain (enc);
911       }
912     }
913     /* now re-sync ts */
914     priv->base_ts += diff;
915     gst_base_audio_encoder_set_base_gp (enc);
916     priv->discont |= discont;
917   }
918
919   gst_adapter_push (enc->priv->adapter, buffer);
920   /* new stuff, so we can push subclass again */
921   enc->priv->drained = FALSE;
922
923   ret = gst_base_audio_encoder_push_buffers (enc, FALSE);
924
925 done:
926   GST_LOG_OBJECT (enc, "chain leaving");
927   return ret;
928
929   /* ERRORS */
930 not_negotiated:
931   {
932     GST_ELEMENT_ERROR (enc, CORE, NEGOTIATION, (NULL),
933         ("encoder not initialized"));
934     gst_buffer_unref (buffer);
935     return GST_FLOW_NOT_NEGOTIATED;
936   }
937 wrong_buffer:
938   {
939     GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
940         ("buffer size %d not a multiple of %d", gst_buffer_get_size (buffer),
941             ctx->info.bpf));
942     gst_buffer_unref (buffer);
943     return GST_FLOW_ERROR;
944   }
945 }
946
947 static gboolean
948 audio_info_is_equal (GstAudioInfo * from, GstAudioInfo * to)
949 {
950   if (from == to)
951     return TRUE;
952   if (GST_AUDIO_INFO_FORMAT (from) != GST_AUDIO_INFO_FORMAT (to))
953     return FALSE;
954   if (GST_AUDIO_INFO_RATE (from) != GST_AUDIO_INFO_RATE (to))
955     return FALSE;
956   if (GST_AUDIO_INFO_CHANNELS (from) != GST_AUDIO_INFO_CHANNELS (to))
957     return FALSE;
958   if (GST_AUDIO_INFO_CHANNELS (from) > 64)
959     return TRUE;
960   return memcmp (from->position, to->position,
961       GST_AUDIO_INFO_CHANNELS (from) * sizeof (to->position[0]));
962 }
963
964 static gboolean
965 gst_base_audio_encoder_sink_setcaps (GstBaseAudioEncoder * enc, GstCaps * caps)
966 {
967   GstBaseAudioEncoderClass *klass;
968   GstBaseAudioEncoderContext *ctx;
969   GstAudioInfo state;
970   gboolean res = TRUE, changed = FALSE;
971   guint old_rate;
972
973   klass = GST_BASE_AUDIO_ENCODER_GET_CLASS (enc);
974
975   /* subclass must do something here ... */
976   g_return_val_if_fail (klass->set_format != NULL, FALSE);
977
978   ctx = &enc->priv->ctx;
979
980   GST_DEBUG_OBJECT (enc, "caps: %" GST_PTR_FORMAT, caps);
981
982   if (!gst_caps_is_fixed (caps))
983     goto refuse_caps;
984
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;
991   }
992
993   if (!gst_audio_info_from_caps (&state, caps))
994     goto refuse_caps;
995
996   changed = audio_info_is_equal (&state, &ctx->info);
997
998   if (changed) {
999     GstClockTime old_min_latency;
1000     GstClockTime old_max_latency;
1001
1002     /* drain any pending old data stuff */
1003     gst_base_audio_encoder_drain (enc);
1004
1005     /* context defaults */
1006     enc->priv->ctx.frame_samples = 0;
1007     enc->priv->ctx.frame_max = 0;
1008     enc->priv->ctx.lookahead = 0;
1009
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);
1015
1016     if (klass->set_format)
1017       res = klass->set_format (enc, &state);
1018
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);
1028     }
1029     GST_OBJECT_UNLOCK (enc);
1030   } else {
1031     GST_DEBUG_OBJECT (enc, "new audio format identical to configured format");
1032   }
1033
1034   return res;
1035
1036   /* ERRORS */
1037 refuse_caps:
1038   {
1039     GST_WARNING_OBJECT (enc, "rejected caps %" GST_PTR_FORMAT, caps);
1040     return res;
1041   }
1042 }
1043
1044
1045 /**
1046  * gst_base_audio_encoder_proxy_getcaps:
1047  * @enc: a #GstBaseAudioEncoder
1048  * @caps: initial caps
1049  *
1050  * Returns caps that express @caps (or sink template caps if @caps == NULL)
1051  * restricted to channel/rate combinations supported by downstream elements
1052  * (e.g. muxers).
1053  *
1054  * Returns: a #GstCaps owned by caller
1055  *
1056  * Since: 0.10.36
1057  */
1058 GstCaps *
1059 gst_base_audio_encoder_proxy_getcaps (GstBaseAudioEncoder * enc, GstCaps * caps)
1060 {
1061   const GstCaps *templ_caps;
1062   GstCaps *allowed = NULL;
1063   GstCaps *fcaps, *filter_caps;
1064   gint i, j;
1065
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);
1073     goto done;
1074   }
1075
1076   GST_LOG_OBJECT (enc, "template caps %" GST_PTR_FORMAT, templ_caps);
1077   GST_LOG_OBJECT (enc, "allowed caps %" GST_PTR_FORMAT, allowed);
1078
1079   filter_caps = gst_caps_new_empty ();
1080
1081   for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
1082     GQuark q_name;
1083
1084     q_name = gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
1085
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);
1089       const GValue *val;
1090       GstStructure *s;
1091
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);
1097
1098       gst_caps_merge_structure (filter_caps, s);
1099     }
1100   }
1101
1102   fcaps = gst_caps_intersect (filter_caps, templ_caps);
1103   gst_caps_unref (filter_caps);
1104
1105 done:
1106   gst_caps_replace (&allowed, NULL);
1107
1108   GST_LOG_OBJECT (enc, "proxy caps %" GST_PTR_FORMAT, fcaps);
1109
1110   return fcaps;
1111 }
1112
1113 static GstCaps *
1114 gst_base_audio_encoder_sink_getcaps (GstPad * pad, GstCaps * filter)
1115 {
1116   GstBaseAudioEncoder *enc;
1117   GstBaseAudioEncoderClass *klass;
1118   GstCaps *caps;
1119
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);
1123
1124   if (klass->getcaps)
1125     caps = klass->getcaps (enc, filter);
1126   else
1127     caps = gst_base_audio_encoder_proxy_getcaps (enc, NULL);
1128   gst_object_unref (enc);
1129
1130   GST_LOG_OBJECT (enc, "returning caps %" GST_PTR_FORMAT, caps);
1131
1132   return caps;
1133 }
1134
1135 static gboolean
1136 gst_base_audio_encoder_sink_eventfunc (GstBaseAudioEncoder * enc,
1137     GstEvent * event)
1138 {
1139   GstBaseAudioEncoderClass *klass;
1140   gboolean handled = FALSE;
1141
1142   klass = GST_BASE_AUDIO_ENCODER_GET_CLASS (enc);
1143
1144   switch (GST_EVENT_TYPE (event)) {
1145     case GST_EVENT_SEGMENT:
1146     {
1147       GstSegment seg;
1148
1149       gst_event_copy_segment (event, &seg);
1150
1151       if (seg.format == GST_FORMAT_TIME) {
1152         GST_DEBUG_OBJECT (enc, "received TIME SEGMENT %" GST_PTR_FORMAT, &seg);
1153       } else {
1154         GST_DEBUG_OBJECT (enc, "received SEGMENT %" GST_PTR_FORMAT, &seg);
1155         GST_DEBUG_OBJECT (enc, "unsupported format; ignoring");
1156         break;
1157       }
1158
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 */
1164       enc->segment = seg;
1165       break;
1166     }
1167
1168     case GST_EVENT_FLUSH_START:
1169       break;
1170
1171     case GST_EVENT_FLUSH_STOP:
1172       /* discard any pending stuff */
1173       /* TODO route through drain ?? */
1174       if (!enc->priv->drained && klass->flush)
1175         klass->flush (enc);
1176       /* and get (re)set for the sequel */
1177       gst_base_audio_encoder_reset (enc, FALSE);
1178       break;
1179
1180     case GST_EVENT_EOS:
1181       gst_base_audio_encoder_drain (enc);
1182       break;
1183
1184     case GST_EVENT_CAPS:
1185     {
1186       GstCaps *caps;
1187
1188       gst_event_parse_caps (event, &caps);
1189       gst_base_audio_encoder_sink_setcaps (enc, caps);
1190       gst_event_unref (event);
1191       handled = TRUE;
1192       break;
1193     }
1194
1195     default:
1196       break;
1197   }
1198
1199   return handled;
1200 }
1201
1202 static gboolean
1203 gst_base_audio_encoder_sink_event (GstPad * pad, GstEvent * event)
1204 {
1205   GstBaseAudioEncoder *enc;
1206   GstBaseAudioEncoderClass *klass;
1207   gboolean handled = FALSE;
1208   gboolean ret = TRUE;
1209
1210   enc = GST_BASE_AUDIO_ENCODER (gst_pad_get_parent (pad));
1211   klass = GST_BASE_AUDIO_ENCODER_GET_CLASS (enc);
1212
1213   GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
1214       GST_EVENT_TYPE_NAME (event));
1215
1216   if (klass->event)
1217     handled = klass->event (enc, event);
1218
1219   if (!handled)
1220     handled = gst_base_audio_encoder_sink_eventfunc (enc, event);
1221
1222   if (!handled)
1223     ret = gst_pad_event_default (pad, event);
1224
1225   GST_DEBUG_OBJECT (enc, "event handled");
1226
1227   gst_object_unref (enc);
1228   return ret;
1229 }
1230
1231 static gboolean
1232 gst_base_audio_encoder_sink_query (GstPad * pad, GstQuery * query)
1233 {
1234   gboolean res = TRUE;
1235   GstBaseAudioEncoder *enc;
1236
1237   enc = GST_BASE_AUDIO_ENCODER (gst_pad_get_parent (pad));
1238
1239   switch (GST_QUERY_TYPE (query)) {
1240     case GST_QUERY_FORMATS:
1241     {
1242       gst_query_set_formats (query, 3,
1243           GST_FORMAT_TIME, GST_FORMAT_BYTES, GST_FORMAT_DEFAULT);
1244       res = TRUE;
1245       break;
1246     }
1247     case GST_QUERY_CONVERT:
1248     {
1249       GstFormat src_fmt, dest_fmt;
1250       gint64 src_val, dest_val;
1251
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)))
1255         goto error;
1256       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1257       break;
1258     }
1259     default:
1260       res = gst_pad_query_default (pad, query);
1261       break;
1262   }
1263
1264 error:
1265   gst_object_unref (enc);
1266   return res;
1267 }
1268
1269 static const GstQueryType *
1270 gst_base_audio_encoder_get_query_types (GstPad * pad)
1271 {
1272   static const GstQueryType gst_base_audio_encoder_src_query_types[] = {
1273     GST_QUERY_POSITION,
1274     GST_QUERY_DURATION,
1275     GST_QUERY_CONVERT,
1276     GST_QUERY_LATENCY,
1277     0
1278   };
1279
1280   return gst_base_audio_encoder_src_query_types;
1281 }
1282
1283 /*
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
1292  *
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).
1297  *
1298  * Since: 0.10.36
1299  */
1300 /* FIXME: make gst_base_audio_encoded_audio_convert() public? */
1301 static gboolean
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)
1305 {
1306   gboolean res = FALSE;
1307
1308   g_return_val_if_fail (dest_format != NULL, FALSE);
1309   g_return_val_if_fail (dest_value != NULL, FALSE);
1310
1311   if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
1312           src_value == -1)) {
1313     if (dest_value)
1314       *dest_value = src_value;
1315     return TRUE;
1316   }
1317
1318   if (samples == 0 || bytes == 0 || fmt->rate == 0) {
1319     GST_DEBUG ("not enough metadata yet to convert");
1320     goto exit;
1321   }
1322
1323   bytes *= fmt->rate;
1324
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);
1331           res = TRUE;
1332           break;
1333         default:
1334           res = FALSE;
1335       }
1336       break;
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);
1342           res = TRUE;
1343           break;
1344         default:
1345           res = FALSE;
1346       }
1347       break;
1348     default:
1349       res = FALSE;
1350   }
1351
1352 exit:
1353   return res;
1354 }
1355
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 ... */
1360 static gboolean
1361 gst_base_audio_encoder_src_query (GstPad * pad, GstQuery * query)
1362 {
1363   GstBaseAudioEncoder *enc;
1364   GstPad *peerpad;
1365   gboolean res = FALSE;
1366
1367   enc = GST_BASE_AUDIO_ENCODER (GST_PAD_PARENT (pad));
1368   peerpad = gst_pad_get_peer (GST_PAD (enc->sinkpad));
1369
1370   GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
1371
1372   switch (GST_QUERY_TYPE (query)) {
1373     case GST_QUERY_POSITION:
1374     {
1375       GstFormat fmt, req_fmt;
1376       gint64 pos, val;
1377
1378       if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1379         GST_LOG_OBJECT (enc, "returning peer response");
1380         break;
1381       }
1382
1383       if (!peerpad) {
1384         GST_LOG_OBJECT (enc, "no peer");
1385         break;
1386       }
1387
1388       gst_query_parse_position (query, &req_fmt, NULL);
1389       fmt = GST_FORMAT_TIME;
1390       if (!(res = gst_pad_query_position (peerpad, fmt, &pos)))
1391         break;
1392
1393       if ((res = gst_pad_query_convert (peerpad, fmt, pos, req_fmt, &val))) {
1394         gst_query_set_position (query, req_fmt, val);
1395       }
1396       break;
1397     }
1398     case GST_QUERY_DURATION:
1399     {
1400       GstFormat fmt, req_fmt;
1401       gint64 dur, val;
1402
1403       if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1404         GST_LOG_OBJECT (enc, "returning peer response");
1405         break;
1406       }
1407
1408       if (!peerpad) {
1409         GST_LOG_OBJECT (enc, "no peer");
1410         break;
1411       }
1412
1413       gst_query_parse_duration (query, &req_fmt, NULL);
1414       fmt = GST_FORMAT_TIME;
1415       if (!(res = gst_pad_query_duration (peerpad, fmt, &dur)))
1416         break;
1417
1418       if ((res = gst_pad_query_convert (peerpad, fmt, dur, req_fmt, &val))) {
1419         gst_query_set_duration (query, req_fmt, val);
1420       }
1421       break;
1422     }
1423     case GST_QUERY_FORMATS:
1424     {
1425       gst_query_set_formats (query, 2, GST_FORMAT_TIME, GST_FORMAT_BYTES);
1426       res = TRUE;
1427       break;
1428     }
1429     case GST_QUERY_CONVERT:
1430     {
1431       GstFormat src_fmt, dest_fmt;
1432       gint64 src_val, dest_val;
1433
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)))
1438         break;
1439       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1440       break;
1441     }
1442     case GST_QUERY_LATENCY:
1443     {
1444       if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1445         gboolean live;
1446         GstClockTime min_latency, max_latency;
1447
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));
1452
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);
1460
1461         gst_query_set_latency (query, live, min_latency, max_latency);
1462       }
1463       break;
1464     }
1465     default:
1466       res = gst_pad_query_default (pad, query);
1467       break;
1468   }
1469
1470   gst_object_unref (peerpad);
1471   return res;
1472 }
1473
1474 static void
1475 gst_base_audio_encoder_set_property (GObject * object, guint prop_id,
1476     const GValue * value, GParamSpec * pspec)
1477 {
1478   GstBaseAudioEncoder *enc;
1479
1480   enc = GST_BASE_AUDIO_ENCODER (object);
1481
1482   switch (prop_id) {
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");
1486       else
1487         enc->priv->perfect_ts = g_value_get_boolean (value);
1488       break;
1489     case PROP_HARD_RESYNC:
1490       enc->priv->hard_resync = g_value_get_boolean (value);
1491       break;
1492     case PROP_TOLERANCE:
1493       enc->priv->tolerance = g_value_get_int64 (value);
1494       break;
1495     default:
1496       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1497       break;
1498   }
1499 }
1500
1501 static void
1502 gst_base_audio_encoder_get_property (GObject * object, guint prop_id,
1503     GValue * value, GParamSpec * pspec)
1504 {
1505   GstBaseAudioEncoder *enc;
1506
1507   enc = GST_BASE_AUDIO_ENCODER (object);
1508
1509   switch (prop_id) {
1510     case PROP_PERFECT_TS:
1511       g_value_set_boolean (value, enc->priv->perfect_ts);
1512       break;
1513     case PROP_GRANULE:
1514       g_value_set_boolean (value, enc->priv->granule);
1515       break;
1516     case PROP_HARD_RESYNC:
1517       g_value_set_boolean (value, enc->priv->hard_resync);
1518       break;
1519     case PROP_TOLERANCE:
1520       g_value_set_int64 (value, enc->priv->tolerance);
1521       break;
1522     default:
1523       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1524       break;
1525   }
1526 }
1527
1528 static gboolean
1529 gst_base_audio_encoder_activate (GstBaseAudioEncoder * enc, gboolean active)
1530 {
1531   GstBaseAudioEncoderClass *klass;
1532   gboolean result = FALSE;
1533
1534   klass = GST_BASE_AUDIO_ENCODER_GET_CLASS (enc);
1535
1536   g_return_val_if_fail (!enc->priv->granule || enc->priv->perfect_ts, FALSE);
1537
1538   GST_DEBUG_OBJECT (enc, "activate %d", active);
1539
1540   if (active) {
1541     if (!enc->priv->active && klass->start)
1542       result = klass->start (enc);
1543   } else {
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);
1548
1549     if (enc->priv->active && klass->stop)
1550       result = klass->stop (enc);
1551
1552     /* clean up */
1553     gst_base_audio_encoder_reset (enc, TRUE);
1554   }
1555   GST_DEBUG_OBJECT (enc, "activate return: %d", result);
1556   return result;
1557 }
1558
1559
1560 static gboolean
1561 gst_base_audio_encoder_sink_activate_push (GstPad * pad, gboolean active)
1562 {
1563   gboolean result = TRUE;
1564   GstBaseAudioEncoder *enc;
1565
1566   enc = GST_BASE_AUDIO_ENCODER (gst_pad_get_parent (pad));
1567
1568   GST_DEBUG_OBJECT (enc, "sink activate push %d", active);
1569
1570   result = gst_base_audio_encoder_activate (enc, active);
1571
1572   if (result)
1573     enc->priv->active = active;
1574
1575   GST_DEBUG_OBJECT (enc, "sink activate push return: %d", result);
1576
1577   gst_object_unref (enc);
1578   return result;
1579 }
1580
1581 /**
1582  * gst_base_audio_encoder_get_audio_info:
1583  * @enc: a #GstBaseAudioEncoder
1584  *
1585  * Returns: a #GstAudioInfo describing the input audio format
1586  *
1587  * Since: 0.10.36
1588  */
1589 GstAudioInfo *
1590 gst_base_audio_encoder_get_audio_info (GstBaseAudioEncoder * enc)
1591 {
1592   g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), NULL);
1593
1594   return &enc->priv->ctx.info;
1595 }
1596
1597 /**
1598  * gst_base_audio_encoder_set_frame_samples:
1599  * @enc: a #GstBaseAudioEncoder
1600  * @num: number of samples per frame
1601  *
1602  * Sets number of samples (per channel) subclass needs to be handed,
1603  * or will be handed all available if 0.
1604  *
1605  * Since: 0.10.36
1606  */
1607 void
1608 gst_base_audio_encoder_set_frame_samples (GstBaseAudioEncoder * enc, gint num)
1609 {
1610   g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1611
1612   enc->priv->ctx.frame_samples = num;
1613 }
1614
1615 /**
1616  * gst_base_audio_encoder_get_frame_samples:
1617  * @enc: a #GstBaseAudioEncoder
1618  *
1619  * Returns: currently requested samples per frame
1620  *
1621  * Since: 0.10.36
1622  */
1623 gint
1624 gst_base_audio_encoder_get_frame_samples (GstBaseAudioEncoder * enc)
1625 {
1626   g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), 0);
1627
1628   return enc->priv->ctx.frame_samples;
1629 }
1630
1631 /**
1632  * gst_base_audio_encoder_set_frame_max:
1633  * @enc: a #GstBaseAudioEncoder
1634  * @num: number of frames
1635  *
1636  * Sets max number of frames accepted at once (assumed minimally 1)
1637  *
1638  * Since: 0.10.36
1639  */
1640 void
1641 gst_base_audio_encoder_set_frame_max (GstBaseAudioEncoder * enc, gint num)
1642 {
1643   g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1644
1645   enc->priv->ctx.frame_max = num;
1646 }
1647
1648 /**
1649  * gst_base_audio_encoder_get_frame_max:
1650  * @enc: a #GstBaseAudioEncoder
1651  *
1652  * Returns: currently configured maximum handled frames
1653  *
1654  * Since: 0.10.36
1655  */
1656 gint
1657 gst_base_audio_encoder_get_frame_max (GstBaseAudioEncoder * enc)
1658 {
1659   g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), 0);
1660
1661   return enc->priv->ctx.frame_max;
1662 }
1663
1664 /**
1665  * gst_base_audio_encoder_set_lookahead:
1666  * @enc: a #GstBaseAudioEncoder
1667  * @num: lookahead
1668  *
1669  * Sets encoder lookahead (in units of input rate samples)
1670  *
1671  * Since: 0.10.36
1672  */
1673 void
1674 gst_base_audio_encoder_set_lookahead (GstBaseAudioEncoder * enc, gint num)
1675 {
1676   g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1677
1678   enc->priv->ctx.lookahead = num;
1679 }
1680
1681 /**
1682  * gst_base_audio_encoder_get_lookahead:
1683  * @enc: a #GstBaseAudioEncoder
1684  *
1685  * Returns: currently configured encoder lookahead
1686  */
1687 gint
1688 gst_base_audio_encoder_get_lookahead (GstBaseAudioEncoder * enc)
1689 {
1690   g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), 0);
1691
1692   return enc->priv->ctx.lookahead;
1693 }
1694
1695 /**
1696  * gst_base_audio_encoder_set_latency:
1697  * @enc: a #GstBaseAudioEncoder
1698  * @min: minimum latency
1699  * @max: maximum latency
1700  *
1701  * Sets encoder latency.
1702  *
1703  * Since: 0.10.36
1704  */
1705 void
1706 gst_base_audio_encoder_set_latency (GstBaseAudioEncoder * enc,
1707     GstClockTime min, GstClockTime max)
1708 {
1709   g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1710
1711   GST_OBJECT_LOCK (enc);
1712   enc->priv->ctx.min_latency = min;
1713   enc->priv->ctx.max_latency = max;
1714   GST_OBJECT_UNLOCK (enc);
1715 }
1716
1717 /**
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
1722  *
1723  * Returns currently configured latency.
1724  *
1725  * Since: 0.10.36
1726  */
1727 void
1728 gst_base_audio_encoder_get_latency (GstBaseAudioEncoder * enc,
1729     GstClockTime * min, GstClockTime * max)
1730 {
1731   g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1732
1733   GST_OBJECT_LOCK (enc);
1734   if (min)
1735     *min = enc->priv->ctx.min_latency;
1736   if (max)
1737     *max = enc->priv->ctx.max_latency;
1738   GST_OBJECT_UNLOCK (enc);
1739 }
1740
1741 /**
1742  * gst_base_audio_encoder_set_mark_granule:
1743  * @enc: a #GstBaseAudioEncoder
1744  * @enabled: new state
1745  *
1746  * Enable or disable encoder granule handling.
1747  *
1748  * MT safe.
1749  *
1750  * Since: 0.10.36
1751  */
1752 void
1753 gst_base_audio_encoder_set_mark_granule (GstBaseAudioEncoder * enc,
1754     gboolean enabled)
1755 {
1756   g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1757
1758   GST_LOG_OBJECT (enc, "enabled: %d", enabled);
1759
1760   GST_OBJECT_LOCK (enc);
1761   enc->priv->granule = enabled;
1762   GST_OBJECT_UNLOCK (enc);
1763 }
1764
1765 /**
1766  * gst_base_audio_encoder_get_mark_granule:
1767  * @enc: a #GstBaseAudioEncoder
1768  *
1769  * Queries if the encoder will handle granule marking.
1770  *
1771  * Returns: TRUE if granule marking is enabled.
1772  *
1773  * MT safe.
1774  *
1775  * Since: 0.10.36
1776  */
1777 gboolean
1778 gst_base_audio_encoder_get_mark_granule (GstBaseAudioEncoder * enc)
1779 {
1780   gboolean result;
1781
1782   g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), FALSE);
1783
1784   GST_OBJECT_LOCK (enc);
1785   result = enc->priv->granule;
1786   GST_OBJECT_UNLOCK (enc);
1787
1788   return result;
1789 }
1790
1791 /**
1792  * gst_base_audio_encoder_set_perfect_timestamp:
1793  * @enc: a #GstBaseAudioEncoder
1794  * @enabled: new state
1795  *
1796  * Enable or disable encoder perfect output timestamp preference.
1797  *
1798  * MT safe.
1799  *
1800  * Since: 0.10.36
1801  */
1802 void
1803 gst_base_audio_encoder_set_perfect_timestamp (GstBaseAudioEncoder * enc,
1804     gboolean enabled)
1805 {
1806   g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1807
1808   GST_LOG_OBJECT (enc, "enabled: %d", enabled);
1809
1810   GST_OBJECT_LOCK (enc);
1811   enc->priv->perfect_ts = enabled;
1812   GST_OBJECT_UNLOCK (enc);
1813 }
1814
1815 /**
1816  * gst_base_audio_encoder_get_perfect_timestamp:
1817  * @enc: a #GstBaseAudioEncoder
1818  *
1819  * Queries encoder perfect timestamp behaviour.
1820  *
1821  * Returns: TRUE if pefect timestamp setting enabled.
1822  *
1823  * MT safe.
1824  *
1825  * Since: 0.10.36
1826  */
1827 gboolean
1828 gst_base_audio_encoder_get_perfect_timestamp (GstBaseAudioEncoder * enc)
1829 {
1830   gboolean result;
1831
1832   g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), FALSE);
1833
1834   GST_OBJECT_LOCK (enc);
1835   result = enc->priv->perfect_ts;
1836   GST_OBJECT_UNLOCK (enc);
1837
1838   return result;
1839 }
1840
1841 /**
1842  * gst_base_audio_encoder_set_hard_sync:
1843  * @enc: a #GstBaseAudioEncoder
1844  * @enabled: new state
1845  *
1846  * Sets encoder hard resync handling.
1847  *
1848  * MT safe.
1849  *
1850  * Since: 0.10.36
1851  */
1852 void
1853 gst_base_audio_encoder_set_hard_resync (GstBaseAudioEncoder * enc,
1854     gboolean enabled)
1855 {
1856   g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1857
1858   GST_LOG_OBJECT (enc, "enabled: %d", enabled);
1859
1860   GST_OBJECT_LOCK (enc);
1861   enc->priv->hard_resync = enabled;
1862   GST_OBJECT_UNLOCK (enc);
1863 }
1864
1865 /**
1866  * gst_base_audio_encoder_get_hard_sync:
1867  * @enc: a #GstBaseAudioEncoder
1868  *
1869  * Queries encoder's hard resync setting.
1870  *
1871  * Returns: TRUE if hard resync is enabled.
1872  *
1873  * MT safe.
1874  *
1875  * Since: 0.10.36
1876  */
1877 gboolean
1878 gst_base_audio_encoder_get_hard_resync (GstBaseAudioEncoder * enc)
1879 {
1880   gboolean result;
1881
1882   g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), FALSE);
1883
1884   GST_OBJECT_LOCK (enc);
1885   result = enc->priv->hard_resync;
1886   GST_OBJECT_UNLOCK (enc);
1887
1888   return result;
1889 }
1890
1891 /**
1892  * gst_base_audio_encoder_set_tolerance:
1893  * @enc: a #GstBaseAudioEncoder
1894  * @tolerance: new tolerance
1895  *
1896  * Configures encoder audio jitter tolerance threshold.
1897  *
1898  * MT safe.
1899  *
1900  * Since: 0.10.36
1901  */
1902 void
1903 gst_base_audio_encoder_set_tolerance (GstBaseAudioEncoder * enc,
1904     gint64 tolerance)
1905 {
1906   g_return_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc));
1907
1908   GST_OBJECT_LOCK (enc);
1909   enc->priv->tolerance = tolerance;
1910   GST_OBJECT_UNLOCK (enc);
1911 }
1912
1913 /**
1914  * gst_base_audio_encoder_get_tolerance:
1915  * @enc: a #GstBaseAudioEncoder
1916  *
1917  * Queries current audio jitter tolerance threshold.
1918  *
1919  * Returns: encoder audio jitter tolerance threshold.
1920  *
1921  * MT safe.
1922  *
1923  * Since: 0.10.36
1924  */
1925 gint64
1926 gst_base_audio_encoder_get_tolerance (GstBaseAudioEncoder * enc)
1927 {
1928   gint64 result;
1929
1930   g_return_val_if_fail (GST_IS_BASE_AUDIO_ENCODER (enc), 0);
1931
1932   GST_OBJECT_LOCK (enc);
1933   result = enc->priv->tolerance;
1934   GST_OBJECT_UNLOCK (enc);
1935
1936   return result;
1937 }