Blame gst/audiorate/gstaudiorate.c

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
/**
Packit 971217
 * SECTION:element-audiorate
Packit 971217
 * @title: audiorate
Packit 971217
 * @see_also: #GstVideoRate
Packit 971217
 *
Packit 971217
 * This element takes an incoming stream of timestamped raw audio frames and
Packit 971217
 * produces a perfect stream by inserting or dropping samples as needed.
Packit 971217
 *
Packit 971217
 * This operation may be of use to link to elements that require or otherwise
Packit 971217
 * implicitly assume a perfect stream as they do not store timestamps,
Packit 971217
 * but derive this by some means (e.g. bitrate for some AVI cases).
Packit 971217
 *
Packit 971217
 * The properties #GstAudioRate:in, #GstAudioRate:out, #GstAudioRate:add
Packit 971217
 * and #GstAudioRate:drop can be read to obtain information about number of
Packit 971217
 * input samples, output samples, dropped samples (i.e. the number of unused
Packit 971217
 * input samples) and inserted samples (i.e. the number of samples added to
Packit 971217
 * stream).
Packit 971217
 *
Packit 971217
 * When the #GstAudioRate:silent property is set to FALSE, a GObject property
Packit 971217
 * notification will be emitted whenever one of the #GstAudioRate:add or
Packit 971217
 * #GstAudioRate:drop values changes.
Packit 971217
 * This can potentially cause performance degradation.
Packit 971217
 * Note that property notification will happen from the streaming thread, so
Packit 971217
 * applications should be prepared for this.
Packit 971217
 *
Packit 971217
 * If the #GstAudioRate:tolerance property is non-zero, and an incoming buffer's
Packit 971217
 * timestamp deviates less than the property indicates from what would make a
Packit 971217
 * 'perfect time', then no samples will be added or dropped.
Packit 971217
 * Note that the output is still guaranteed to be a perfect stream, which means
Packit 971217
 * that the incoming data is then simply shifted (by less than the indicated
Packit 971217
 * tolerance) to a perfect time.
Packit 971217
 *
Packit 971217
 * ## Example pipelines
Packit 971217
 * |[
Packit 971217
 * gst-launch-1.0 -v autoaudiosrc ! audiorate ! audioconvert ! wavenc ! filesink location=alsa.wav
Packit 971217
 * ]|
Packit 971217
 *  Capture audio from the sound card and turn it into a perfect stream
Packit 971217
 * for saving in a raw audio file.
Packit 971217
 * |[
Packit 971217
 * gst-launch-1.0 -v uridecodebin uri=file:///path/to/audio.file ! audiorate ! audioconvert ! wavenc ! filesink location=alsa.wav
Packit 971217
 * ]|
Packit 971217
 *  Decodes an audio file and transforms it into a perfect stream for saving
Packit 971217
 * in a raw audio WAV file. Without the audio rate, the timing might not be
Packit 971217
 * preserved correctly in the WAV file in case the decoded stream is jittery
Packit 971217
 * or there are samples missing.
Packit 971217
 *
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include <string.h>
Packit 971217
#include <stdlib.h>
Packit 971217
Packit 971217
#include "gstaudiorate.h"
Packit 971217
Packit 971217
#define GST_CAT_DEFAULT audio_rate_debug
Packit 971217
GST_DEBUG_CATEGORY_STATIC (audio_rate_debug);
Packit 971217
Packit 971217
/* GstAudioRate signals and args */
Packit 971217
enum
Packit 971217
{
Packit 971217
  /* FILL ME */
Packit 971217
  LAST_SIGNAL
Packit 971217
};
Packit 971217
Packit 971217
#define DEFAULT_SILENT     TRUE
Packit 971217
#define DEFAULT_TOLERANCE  (40 * GST_MSECOND)
Packit 971217
#define DEFAULT_SKIP_TO_FIRST FALSE
Packit 971217
Packit 971217
enum
Packit 971217
{
Packit 971217
  PROP_0,
Packit 971217
  PROP_IN,
Packit 971217
  PROP_OUT,
Packit 971217
  PROP_ADD,
Packit 971217
  PROP_DROP,
Packit 971217
  PROP_SILENT,
Packit 971217
  PROP_TOLERANCE,
Packit 971217
  PROP_SKIP_TO_FIRST
Packit 971217
};
Packit 971217
Packit 971217
static GstStaticPadTemplate gst_audio_rate_src_template =
Packit 971217
GST_STATIC_PAD_TEMPLATE ("src",
Packit 971217
    GST_PAD_SRC,
Packit 971217
    GST_PAD_ALWAYS,
Packit 971217
    GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE (GST_AUDIO_FORMATS_ALL)
Packit 971217
        ", layout = (string) { interleaved, non-interleaved }")
Packit 971217
    );
Packit 971217
Packit 971217
static GstStaticPadTemplate gst_audio_rate_sink_template =
Packit 971217
GST_STATIC_PAD_TEMPLATE ("sink",
Packit 971217
    GST_PAD_SINK,
Packit 971217
    GST_PAD_ALWAYS,
Packit 971217
    GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE (GST_AUDIO_FORMATS_ALL)
Packit 971217
        ", layout = (string) { interleaved, non-interleaved }")
Packit 971217
    );
Packit 971217
Packit 971217
static gboolean gst_audio_rate_sink_event (GstPad * pad, GstObject * parent,
Packit 971217
    GstEvent * event);
Packit 971217
static gboolean gst_audio_rate_src_event (GstPad * pad, GstObject * parent,
Packit 971217
    GstEvent * event);
Packit 971217
static GstFlowReturn gst_audio_rate_chain (GstPad * pad, GstObject * parent,
Packit 971217
    GstBuffer * buf);
Packit 971217
Packit 971217
static void gst_audio_rate_set_property (GObject * object,
Packit 971217
    guint prop_id, const GValue * value, GParamSpec * pspec);
Packit 971217
static void gst_audio_rate_get_property (GObject * object,
Packit 971217
    guint prop_id, GValue * value, GParamSpec * pspec);
Packit 971217
Packit 971217
static GstStateChangeReturn gst_audio_rate_change_state (GstElement * element,
Packit 971217
    GstStateChange transition);
Packit 971217
Packit 971217
/*static guint gst_audio_rate_signals[LAST_SIGNAL] = { 0 }; */
Packit 971217
Packit 971217
static GParamSpec *pspec_drop = NULL;
Packit 971217
static GParamSpec *pspec_add = NULL;
Packit 971217
Packit 971217
#define gst_audio_rate_parent_class parent_class
Packit 971217
G_DEFINE_TYPE (GstAudioRate, gst_audio_rate, GST_TYPE_ELEMENT);
Packit 971217
Packit 971217
static void
Packit 971217
gst_audio_rate_class_init (GstAudioRateClass * klass)
Packit 971217
{
Packit 971217
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
Packit 971217
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
Packit 971217
Packit 971217
  object_class->set_property = gst_audio_rate_set_property;
Packit 971217
  object_class->get_property = gst_audio_rate_get_property;
Packit 971217
Packit 971217
  g_object_class_install_property (object_class, PROP_IN,
Packit 971217
      g_param_spec_uint64 ("in", "In",
Packit 971217
          "Number of input samples", 0, G_MAXUINT64, 0,
Packit 971217
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Packit 971217
  g_object_class_install_property (object_class, PROP_OUT,
Packit 971217
      g_param_spec_uint64 ("out", "Out", "Number of output samples", 0,
Packit 971217
          G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Packit 971217
  pspec_add = g_param_spec_uint64 ("add", "Add", "Number of added samples",
Packit 971217
      0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
Packit 971217
  g_object_class_install_property (object_class, PROP_ADD, pspec_add);
Packit 971217
  pspec_drop = g_param_spec_uint64 ("drop", "Drop", "Number of dropped samples",
Packit 971217
      0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
Packit 971217
  g_object_class_install_property (object_class, PROP_DROP, pspec_drop);
Packit 971217
  g_object_class_install_property (object_class, PROP_SILENT,
Packit 971217
      g_param_spec_boolean ("silent", "silent",
Packit 971217
          "Don't emit notify for dropped and duplicated frames", DEFAULT_SILENT,
Packit 971217
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Packit 971217
  /**
Packit 971217
   * GstAudioRate:tolerance:
Packit 971217
   *
Packit 971217
   * The difference between incoming timestamp and next timestamp must exceed
Packit 971217
   * the given value for audiorate to add or drop samples.
Packit 971217
   */
Packit 971217
  g_object_class_install_property (object_class, PROP_TOLERANCE,
Packit 971217
      g_param_spec_uint64 ("tolerance", "tolerance",
Packit 971217
          "Only act if timestamp jitter/imperfection exceeds indicated tolerance (ns)",
Packit 971217
          0, G_MAXUINT64, DEFAULT_TOLERANCE,
Packit 971217
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Packit 971217
Packit 971217
  /**
Packit 971217
   * GstAudioRate:skip-to-first:
Packit 971217
   *
Packit 971217
   * Don't produce buffers before the first one we receive.
Packit 971217
   */
Packit 971217
  g_object_class_install_property (object_class, PROP_SKIP_TO_FIRST,
Packit 971217
      g_param_spec_boolean ("skip-to-first", "Skip to first buffer",
Packit 971217
          "Don't produce buffers before the first one we receive",
Packit 971217
          DEFAULT_SKIP_TO_FIRST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Packit 971217
Packit 971217
  gst_element_class_set_static_metadata (element_class,
Packit 971217
      "Audio rate adjuster", "Filter/Effect/Audio",
Packit 971217
      "Drops/duplicates/adjusts timestamps on audio samples to make a perfect stream",
Packit 971217
      "Wim Taymans <wim@fluendo.com>");
Packit 971217
Packit 971217
  gst_element_class_add_static_pad_template (element_class,
Packit 971217
      &gst_audio_rate_sink_template);
Packit 971217
  gst_element_class_add_static_pad_template (element_class,
Packit 971217
      &gst_audio_rate_src_template);
Packit 971217
Packit 971217
  element_class->change_state = gst_audio_rate_change_state;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_audio_rate_reset (GstAudioRate * audiorate)
Packit 971217
{
Packit 971217
  audiorate->next_offset = -1;
Packit 971217
  audiorate->next_ts = -1;
Packit 971217
  audiorate->discont = TRUE;
Packit 971217
  gst_segment_init (&audiorate->sink_segment, GST_FORMAT_UNDEFINED);
Packit 971217
  gst_segment_init (&audiorate->src_segment, GST_FORMAT_TIME);
Packit 971217
Packit 971217
  GST_DEBUG_OBJECT (audiorate, "handle reset");
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_audio_rate_setcaps (GstAudioRate * audiorate, GstCaps * caps)
Packit 971217
{
Packit 971217
  GstAudioInfo info;
Packit 971217
Packit 971217
  if (!gst_audio_info_from_caps (&info, caps))
Packit 971217
    goto wrong_caps;
Packit 971217
Packit 971217
  audiorate->info = info;
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
Packit 971217
  /* ERRORS */
Packit 971217
wrong_caps:
Packit 971217
  {
Packit 971217
    GST_DEBUG_OBJECT (audiorate, "could not parse caps");
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_audio_rate_init (GstAudioRate * audiorate)
Packit 971217
{
Packit 971217
  audiorate->sinkpad =
Packit 971217
      gst_pad_new_from_static_template (&gst_audio_rate_sink_template, "sink");
Packit 971217
  gst_pad_set_event_function (audiorate->sinkpad, gst_audio_rate_sink_event);
Packit 971217
  gst_pad_set_chain_function (audiorate->sinkpad, gst_audio_rate_chain);
Packit 971217
  GST_PAD_SET_PROXY_CAPS (audiorate->sinkpad);
Packit 971217
  gst_element_add_pad (GST_ELEMENT (audiorate), audiorate->sinkpad);
Packit 971217
Packit 971217
  audiorate->srcpad =
Packit 971217
      gst_pad_new_from_static_template (&gst_audio_rate_src_template, "src");
Packit 971217
  gst_pad_set_event_function (audiorate->srcpad, gst_audio_rate_src_event);
Packit 971217
  GST_PAD_SET_PROXY_CAPS (audiorate->srcpad);
Packit 971217
  gst_element_add_pad (GST_ELEMENT (audiorate), audiorate->srcpad);
Packit 971217
Packit 971217
  audiorate->in = 0;
Packit 971217
  audiorate->out = 0;
Packit 971217
  audiorate->drop = 0;
Packit 971217
  audiorate->add = 0;
Packit 971217
  audiorate->silent = DEFAULT_SILENT;
Packit 971217
  audiorate->tolerance = DEFAULT_TOLERANCE;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_audio_rate_fill_to_time (GstAudioRate * audiorate, GstClockTime time)
Packit 971217
{
Packit 971217
  GstBuffer *buf;
Packit 971217
Packit 971217
  GST_DEBUG_OBJECT (audiorate, "next_ts: %" GST_TIME_FORMAT
Packit 971217
      ", filling to %" GST_TIME_FORMAT, GST_TIME_ARGS (audiorate->next_ts),
Packit 971217
      GST_TIME_ARGS (time));
Packit 971217
Packit 971217
  if (!GST_CLOCK_TIME_IS_VALID (time) ||
Packit 971217
      !GST_CLOCK_TIME_IS_VALID (audiorate->next_ts))
Packit 971217
    return;
Packit 971217
Packit 971217
  /* feed an empty buffer to chain with the given timestamp,
Packit 971217
   * it will take care of filling */
Packit 971217
  buf = gst_buffer_new ();
Packit 971217
  GST_BUFFER_TIMESTAMP (buf) = time;
Packit 971217
  gst_audio_rate_chain (audiorate->sinkpad, GST_OBJECT_CAST (audiorate), buf);
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_audio_rate_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
Packit 971217
{
Packit 971217
  gboolean res;
Packit 971217
  GstAudioRate *audiorate;
Packit 971217
Packit 971217
  audiorate = GST_AUDIO_RATE (parent);
Packit 971217
Packit 971217
  switch (GST_EVENT_TYPE (event)) {
Packit 971217
    case GST_EVENT_CAPS:
Packit 971217
    {
Packit 971217
      GstCaps *caps;
Packit 971217
Packit 971217
      gst_event_parse_caps (event, &caps);
Packit 971217
      if ((res = gst_audio_rate_setcaps (audiorate, caps))) {
Packit 971217
        res = gst_pad_push_event (audiorate->srcpad, event);
Packit 971217
      } else {
Packit 971217
        gst_event_unref (event);
Packit 971217
      }
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    case GST_EVENT_FLUSH_STOP:
Packit 971217
      GST_DEBUG_OBJECT (audiorate, "handling FLUSH_STOP");
Packit 971217
      gst_audio_rate_reset (audiorate);
Packit 971217
      res = gst_pad_push_event (audiorate->srcpad, event);
Packit 971217
      break;
Packit 971217
    case GST_EVENT_SEGMENT:
Packit 971217
    {
Packit 971217
      gst_event_copy_segment (event, &audiorate->sink_segment);
Packit 971217
Packit 971217
      GST_DEBUG_OBJECT (audiorate, "handle NEWSEGMENT");
Packit 971217
#if 0
Packit 971217
      /* FIXME: bad things will likely happen if rate < 0 ... */
Packit 971217
      if (!update) {
Packit 971217
        /* a new segment starts. We need to figure out what will be the next
Packit 971217
         * sample offset. We mark the offsets as invalid so that the _chain
Packit 971217
         * function will perform this calculation. */
Packit 971217
        gst_audio_rate_fill_to_time (audiorate, audiorate->src_segment.stop);
Packit 971217
#endif
Packit 971217
        audiorate->next_offset = -1;
Packit 971217
        audiorate->next_ts = -1;
Packit 971217
#if 0
Packit 971217
      } else {
Packit 971217
        gst_audio_rate_fill_to_time (audiorate, audiorate->src_segment.start);
Packit 971217
      }
Packit 971217
#endif
Packit 971217
Packit 971217
      GST_DEBUG_OBJECT (audiorate, "updated segment: %" GST_SEGMENT_FORMAT,
Packit 971217
          &audiorate->sink_segment);
Packit 971217
Packit 971217
      if (audiorate->sink_segment.format == GST_FORMAT_TIME) {
Packit 971217
        /* TIME formats can be copied to src and forwarded */
Packit 971217
        res = gst_pad_push_event (audiorate->srcpad, event);
Packit 971217
        gst_segment_copy_into (&audiorate->sink_segment,
Packit 971217
            &audiorate->src_segment);
Packit 971217
      } else {
Packit 971217
        /* other formats will be handled in the _chain function */
Packit 971217
        gst_event_unref (event);
Packit 971217
        res = TRUE;
Packit 971217
      }
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    case GST_EVENT_EOS:
Packit 971217
      /* Fill segment until the end */
Packit 971217
      if (GST_CLOCK_TIME_IS_VALID (audiorate->src_segment.stop))
Packit 971217
        gst_audio_rate_fill_to_time (audiorate, audiorate->src_segment.stop);
Packit 971217
      res = gst_pad_push_event (audiorate->srcpad, event);
Packit 971217
      break;
Packit 971217
    case GST_EVENT_GAP:
Packit 971217
    {
Packit 971217
      /* Fill until end of gap */
Packit 971217
      GstClockTime timestamp, duration;
Packit 971217
      gst_event_parse_gap (event, &timestamp, &duration);
Packit 971217
      gst_event_unref (event);
Packit 971217
      if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
Packit 971217
        if (GST_CLOCK_TIME_IS_VALID (duration))
Packit 971217
          timestamp += duration;
Packit 971217
        gst_audio_rate_fill_to_time (audiorate, timestamp);
Packit 971217
      }
Packit 971217
      res = TRUE;
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    default:
Packit 971217
      res = gst_pad_event_default (pad, parent, event);
Packit 971217
      break;
Packit 971217
  }
Packit 971217
Packit 971217
  return res;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_audio_rate_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
Packit 971217
{
Packit 971217
  gboolean res;
Packit 971217
  GstAudioRate *audiorate;
Packit 971217
Packit 971217
  audiorate = GST_AUDIO_RATE (parent);
Packit 971217
Packit 971217
  switch (GST_EVENT_TYPE (event)) {
Packit 971217
    default:
Packit 971217
      res = gst_pad_push_event (audiorate->sinkpad, event);
Packit 971217
      break;
Packit 971217
  }
Packit 971217
Packit 971217
  return res;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_audio_rate_convert (GstAudioRate * audiorate,
Packit 971217
    GstFormat src_fmt, guint64 src_val, GstFormat dest_fmt, guint64 * dest_val)
Packit 971217
{
Packit 971217
  return gst_audio_info_convert (&audiorate->info, src_fmt, src_val, dest_fmt,
Packit 971217
      (gint64 *) dest_val);
Packit 971217
}
Packit 971217
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_audio_rate_convert_segments (GstAudioRate * audiorate)
Packit 971217
{
Packit 971217
  GstFormat src_fmt, dst_fmt;
Packit 971217
Packit 971217
  src_fmt = audiorate->sink_segment.format;
Packit 971217
  dst_fmt = audiorate->src_segment.format;
Packit 971217
Packit 971217
#define CONVERT_VAL(field) gst_audio_rate_convert (audiorate, \
Packit 971217
		src_fmt, audiorate->sink_segment.field,       \
Packit 971217
		dst_fmt, &audiorate->src_segment.field);
Packit 971217
Packit 971217
  audiorate->sink_segment.rate = audiorate->src_segment.rate;
Packit 971217
  audiorate->sink_segment.flags = audiorate->src_segment.flags;
Packit 971217
  audiorate->sink_segment.applied_rate = audiorate->src_segment.applied_rate;
Packit 971217
  CONVERT_VAL (start);
Packit 971217
  CONVERT_VAL (stop);
Packit 971217
  CONVERT_VAL (time);
Packit 971217
  CONVERT_VAL (base);
Packit 971217
  CONVERT_VAL (position);
Packit 971217
#undef CONVERT_VAL
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_audio_rate_notify_drop (GstAudioRate * audiorate)
Packit 971217
{
Packit 971217
  g_object_notify_by_pspec ((GObject *) audiorate, pspec_drop);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_audio_rate_notify_add (GstAudioRate * audiorate)
Packit 971217
{
Packit 971217
  g_object_notify_by_pspec ((GObject *) audiorate, pspec_add);
Packit 971217
}
Packit 971217
Packit 971217
static GstFlowReturn
Packit 971217
gst_audio_rate_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
Packit 971217
{
Packit 971217
  GstAudioRate *audiorate;
Packit 971217
  GstClockTime in_time;
Packit 971217
  guint64 in_offset, in_offset_end, in_samples;
Packit 971217
  guint in_size;
Packit 971217
  GstFlowReturn ret = GST_FLOW_OK;
Packit 971217
  GstClockTimeDiff diff;
Packit 971217
  gint rate, bpf;
Packit 971217
Packit 971217
  audiorate = GST_AUDIO_RATE (parent);
Packit 971217
Packit 971217
  rate = GST_AUDIO_INFO_RATE (&audiorate->info);
Packit 971217
  bpf = GST_AUDIO_INFO_BPF (&audiorate->info);
Packit 971217
Packit 971217
  /* need to be negotiated now */
Packit 971217
  if (bpf == 0)
Packit 971217
    goto not_negotiated;
Packit 971217
Packit 971217
  /* we have a new pending segment */
Packit 971217
  if (audiorate->next_offset == -1) {
Packit 971217
    gint64 pos;
Packit 971217
Packit 971217
    /* update the TIME segment */
Packit 971217
    gst_audio_rate_convert_segments (audiorate);
Packit 971217
Packit 971217
    /* first buffer, we are negotiated and we have a segment, calculate the
Packit 971217
     * current expected offsets based on the segment.start, which is the first
Packit 971217
     * media time of the segment and should match the media time of the first
Packit 971217
     * buffer in that segment, which is the offset expressed in DEFAULT units.
Packit 971217
     */
Packit 971217
    /* convert first timestamp of segment to sample position */
Packit 971217
    pos = gst_util_uint64_scale_int_round (audiorate->src_segment.start,
Packit 971217
        GST_AUDIO_INFO_RATE (&audiorate->info), GST_SECOND);
Packit 971217
Packit 971217
    GST_DEBUG_OBJECT (audiorate, "resync to offset %" G_GINT64_FORMAT, pos);
Packit 971217
Packit 971217
    /* resyncing is a discont */
Packit 971217
    audiorate->discont = TRUE;
Packit 971217
Packit 971217
    audiorate->next_offset = pos;
Packit 971217
    audiorate->next_ts =
Packit 971217
        gst_util_uint64_scale_int_round (audiorate->next_offset, GST_SECOND,
Packit 971217
        GST_AUDIO_INFO_RATE (&audiorate->info));
Packit 971217
Packit 971217
    if (audiorate->skip_to_first && GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
Packit 971217
      GST_DEBUG_OBJECT (audiorate, "but skipping to first buffer instead");
Packit 971217
      pos = gst_util_uint64_scale_int_round (GST_BUFFER_TIMESTAMP (buf),
Packit 971217
          GST_AUDIO_INFO_RATE (&audiorate->info), GST_SECOND);
Packit 971217
      GST_DEBUG_OBJECT (audiorate, "so resync to offset %" G_GINT64_FORMAT,
Packit 971217
          pos);
Packit 971217
      audiorate->next_offset = pos;
Packit 971217
      audiorate->next_ts = GST_BUFFER_TIMESTAMP (buf);
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  in_time = GST_BUFFER_TIMESTAMP (buf);
Packit 971217
  if (in_time == GST_CLOCK_TIME_NONE) {
Packit 971217
    GST_DEBUG_OBJECT (audiorate, "no timestamp, using expected next time");
Packit 971217
    in_time = audiorate->next_ts;
Packit 971217
  }
Packit 971217
Packit 971217
  in_size = gst_buffer_get_size (buf);
Packit 971217
  in_samples = in_size / bpf;
Packit 971217
  audiorate->in += in_samples;
Packit 971217
Packit 971217
  /* calculate the buffer offset */
Packit 971217
  in_offset = gst_util_uint64_scale_int_round (in_time, rate, GST_SECOND);
Packit 971217
  in_offset_end = in_offset + in_samples;
Packit 971217
Packit 971217
  GST_LOG_OBJECT (audiorate,
Packit 971217
      "in_time:%" GST_TIME_FORMAT ", in_duration:%" GST_TIME_FORMAT
Packit 971217
      ", in_size:%u, in_offset:%" G_GUINT64_FORMAT ", in_offset_end:%"
Packit 971217
      G_GUINT64_FORMAT ", ->next_offset:%" G_GUINT64_FORMAT ", ->next_ts:%"
Packit 971217
      GST_TIME_FORMAT, GST_TIME_ARGS (in_time),
Packit 971217
      GST_TIME_ARGS (GST_FRAMES_TO_CLOCK_TIME (in_samples, rate)),
Packit 971217
      in_size, in_offset, in_offset_end, audiorate->next_offset,
Packit 971217
      GST_TIME_ARGS (audiorate->next_ts));
Packit 971217
Packit 971217
  diff = in_time - audiorate->next_ts;
Packit 971217
  if (diff <= (GstClockTimeDiff) audiorate->tolerance &&
Packit 971217
      diff >= (GstClockTimeDiff) - audiorate->tolerance) {
Packit 971217
    /* buffer time close enough to expected time,
Packit 971217
     * so produce a perfect stream by simply 'shifting'
Packit 971217
     * it to next ts and offset and sending */
Packit 971217
    GST_LOG_OBJECT (audiorate, "within tolerance %" GST_TIME_FORMAT,
Packit 971217
        GST_TIME_ARGS (audiorate->tolerance));
Packit 971217
    /* The outgoing buffer's offset will be set to ->next_offset, we also
Packit 971217
     * need to adjust the offset_end value accordingly */
Packit 971217
    in_offset_end = audiorate->next_offset + in_samples;
Packit 971217
    audiorate->out += in_samples;
Packit 971217
    goto send;
Packit 971217
  }
Packit 971217
Packit 971217
  /* do we need to insert samples */
Packit 971217
  if (in_offset > audiorate->next_offset) {
Packit 971217
    GstBuffer *fill;
Packit 971217
    gint fillsize;
Packit 971217
    guint64 fillsamples;
Packit 971217
Packit 971217
    /* We don't want to allocate a single unreasonably huge buffer - it might
Packit 971217
       be hundreds of megabytes. So, limit each output buffer to one second of
Packit 971217
       audio */
Packit 971217
    fillsamples = in_offset - audiorate->next_offset;
Packit 971217
Packit 971217
    while (fillsamples > 0) {
Packit 971217
      guint64 cursamples = MIN (fillsamples, rate);
Packit 971217
      GstMapInfo fillmap;
Packit 971217
Packit 971217
      fillsamples -= cursamples;
Packit 971217
      fillsize = cursamples * bpf;
Packit 971217
Packit 971217
      fill = gst_buffer_new_and_alloc (fillsize);
Packit 971217
Packit 971217
      gst_buffer_map (fill, &fillmap, GST_MAP_WRITE);
Packit 971217
      gst_audio_format_fill_silence (audiorate->info.finfo, fillmap.data,
Packit 971217
          fillmap.size);
Packit 971217
      gst_buffer_unmap (fill, &fillmap);
Packit 971217
Packit 971217
      GST_DEBUG_OBJECT (audiorate, "inserting %" G_GUINT64_FORMAT " samples",
Packit 971217
          cursamples);
Packit 971217
Packit 971217
      GST_BUFFER_OFFSET (fill) = audiorate->next_offset;
Packit 971217
      audiorate->next_offset += cursamples;
Packit 971217
      GST_BUFFER_OFFSET_END (fill) = audiorate->next_offset;
Packit 971217
Packit 971217
      /* Use next timestamp, then calculate following timestamp based on 
Packit 971217
       * offset to get duration. Necessary complexity to get 'perfect' 
Packit 971217
       * streams */
Packit 971217
      GST_BUFFER_TIMESTAMP (fill) = audiorate->next_ts;
Packit 971217
      audiorate->next_ts =
Packit 971217
          gst_util_uint64_scale_int_round (audiorate->next_offset, GST_SECOND,
Packit 971217
          rate);
Packit 971217
      GST_BUFFER_DURATION (fill) =
Packit 971217
          audiorate->next_ts - GST_BUFFER_TIMESTAMP (fill);
Packit 971217
Packit 971217
      /* we created this buffer to fill a gap */
Packit 971217
      GST_BUFFER_FLAG_SET (fill, GST_BUFFER_FLAG_GAP);
Packit 971217
      /* set discont if it's pending, this is mostly done for the first buffer 
Packit 971217
       * and after a flushing seek */
Packit 971217
      if (audiorate->discont) {
Packit 971217
        GST_BUFFER_FLAG_SET (fill, GST_BUFFER_FLAG_DISCONT);
Packit 971217
        audiorate->discont = FALSE;
Packit 971217
      }
Packit 971217
Packit 971217
      fill = gst_audio_buffer_clip (fill, &audiorate->src_segment, rate, bpf);
Packit 971217
      if (fill)
Packit 971217
        ret = gst_pad_push (audiorate->srcpad, fill);
Packit 971217
Packit 971217
      if (ret != GST_FLOW_OK)
Packit 971217
        goto beach;
Packit 971217
      audiorate->out += cursamples;
Packit 971217
      audiorate->add += cursamples;
Packit 971217
Packit 971217
      if (!audiorate->silent)
Packit 971217
        gst_audio_rate_notify_add (audiorate);
Packit 971217
    }
Packit 971217
Packit 971217
  } else if (in_offset < audiorate->next_offset) {
Packit 971217
    /* need to remove samples */
Packit 971217
    if (in_offset_end <= audiorate->next_offset) {
Packit 971217
      guint64 drop = in_size / bpf;
Packit 971217
Packit 971217
      audiorate->drop += drop;
Packit 971217
Packit 971217
      GST_DEBUG_OBJECT (audiorate, "dropping %" G_GUINT64_FORMAT " samples",
Packit 971217
          drop);
Packit 971217
Packit 971217
      /* we can drop the buffer completely */
Packit 971217
      gst_buffer_unref (buf);
Packit 971217
      buf = NULL;
Packit 971217
Packit 971217
      if (!audiorate->silent)
Packit 971217
        gst_audio_rate_notify_drop (audiorate);
Packit 971217
Packit 971217
      goto beach;
Packit 971217
    } else {
Packit 971217
      guint64 truncsamples;
Packit 971217
      guint truncsize, leftsize;
Packit 971217
      GstBuffer *trunc;
Packit 971217
Packit 971217
      /* truncate buffer */
Packit 971217
      truncsamples = audiorate->next_offset - in_offset;
Packit 971217
      truncsize = truncsamples * bpf;
Packit 971217
      leftsize = in_size - truncsize;
Packit 971217
Packit 971217
      trunc =
Packit 971217
          gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, truncsize,
Packit 971217
          leftsize);
Packit 971217
Packit 971217
      gst_buffer_unref (buf);
Packit 971217
      buf = trunc;
Packit 971217
Packit 971217
      audiorate->drop += truncsamples;
Packit 971217
      audiorate->out += (leftsize / bpf);
Packit 971217
      GST_DEBUG_OBJECT (audiorate, "truncating %" G_GUINT64_FORMAT " samples",
Packit 971217
          truncsamples);
Packit 971217
Packit 971217
      if (!audiorate->silent)
Packit 971217
        gst_audio_rate_notify_drop (audiorate);
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
send:
Packit 971217
  if (gst_buffer_get_size (buf) == 0)
Packit 971217
    goto beach;
Packit 971217
Packit 971217
  /* Now calculate parameters for whichever buffer (either the original
Packit 971217
   * or truncated one) we're pushing. */
Packit 971217
  GST_BUFFER_OFFSET (buf) = audiorate->next_offset;
Packit 971217
  GST_BUFFER_OFFSET_END (buf) = in_offset_end;
Packit 971217
Packit 971217
  GST_BUFFER_TIMESTAMP (buf) = audiorate->next_ts;
Packit 971217
  audiorate->next_ts = gst_util_uint64_scale_int_round (in_offset_end,
Packit 971217
      GST_SECOND, rate);
Packit 971217
  GST_BUFFER_DURATION (buf) = audiorate->next_ts - GST_BUFFER_TIMESTAMP (buf);
Packit 971217
Packit 971217
  if (audiorate->discont) {
Packit 971217
    /* we need to output a discont buffer, do so now */
Packit 971217
    GST_DEBUG_OBJECT (audiorate, "marking DISCONT on output buffer");
Packit 971217
    buf = gst_buffer_make_writable (buf);
Packit 971217
    GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
Packit 971217
    audiorate->discont = FALSE;
Packit 971217
  } else if (GST_BUFFER_IS_DISCONT (buf)) {
Packit 971217
    /* else we make everything continuous so we can safely remove the DISCONT
Packit 971217
     * flag from the buffer if there was one */
Packit 971217
    GST_DEBUG_OBJECT (audiorate, "removing DISCONT from buffer");
Packit 971217
    buf = gst_buffer_make_writable (buf);
Packit 971217
    GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
Packit 971217
  }
Packit 971217
Packit 971217
  buf = gst_audio_buffer_clip (buf, &audiorate->src_segment, rate, bpf);
Packit 971217
  if (buf) {
Packit 971217
    /* set last_stop on segment */
Packit 971217
    audiorate->src_segment.position =
Packit 971217
        GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf);
Packit 971217
Packit 971217
    ret = gst_pad_push (audiorate->srcpad, buf);
Packit 971217
  }
Packit 971217
  buf = NULL;
Packit 971217
Packit 971217
  audiorate->next_offset = in_offset_end;
Packit 971217
beach:
Packit 971217
Packit 971217
  if (buf)
Packit 971217
    gst_buffer_unref (buf);
Packit 971217
Packit 971217
  return ret;
Packit 971217
Packit 971217
  /* ERRORS */
Packit 971217
not_negotiated:
Packit 971217
  {
Packit 971217
    gst_buffer_unref (buf);
Packit 971217
Packit 971217
    GST_ELEMENT_ERROR (audiorate, STREAM, FORMAT,
Packit 971217
        (NULL), ("pipeline error, format was not negotiated"));
Packit 971217
    return GST_FLOW_NOT_NEGOTIATED;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_audio_rate_set_property (GObject * object,
Packit 971217
    guint prop_id, const GValue * value, GParamSpec * pspec)
Packit 971217
{
Packit 971217
  GstAudioRate *audiorate = GST_AUDIO_RATE (object);
Packit 971217
Packit 971217
  switch (prop_id) {
Packit 971217
    case PROP_SILENT:
Packit 971217
      audiorate->silent = g_value_get_boolean (value);
Packit 971217
      break;
Packit 971217
    case PROP_TOLERANCE:
Packit 971217
      audiorate->tolerance = g_value_get_uint64 (value);
Packit 971217
      break;
Packit 971217
    case PROP_SKIP_TO_FIRST:
Packit 971217
      audiorate->skip_to_first = g_value_get_boolean (value);
Packit 971217
      break;
Packit 971217
    default:
Packit 971217
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 971217
      break;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_audio_rate_get_property (GObject * object,
Packit 971217
    guint prop_id, GValue * value, GParamSpec * pspec)
Packit 971217
{
Packit 971217
  GstAudioRate *audiorate = GST_AUDIO_RATE (object);
Packit 971217
Packit 971217
  switch (prop_id) {
Packit 971217
    case PROP_IN:
Packit 971217
      g_value_set_uint64 (value, audiorate->in);
Packit 971217
      break;
Packit 971217
    case PROP_OUT:
Packit 971217
      g_value_set_uint64 (value, audiorate->out);
Packit 971217
      break;
Packit 971217
    case PROP_ADD:
Packit 971217
      g_value_set_uint64 (value, audiorate->add);
Packit 971217
      break;
Packit 971217
    case PROP_DROP:
Packit 971217
      g_value_set_uint64 (value, audiorate->drop);
Packit 971217
      break;
Packit 971217
    case PROP_SILENT:
Packit 971217
      g_value_set_boolean (value, audiorate->silent);
Packit 971217
      break;
Packit 971217
    case PROP_TOLERANCE:
Packit 971217
      g_value_set_uint64 (value, audiorate->tolerance);
Packit 971217
      break;
Packit 971217
    case PROP_SKIP_TO_FIRST:
Packit 971217
      g_value_set_boolean (value, audiorate->skip_to_first);
Packit 971217
      break;
Packit 971217
    default:
Packit 971217
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 971217
      break;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static GstStateChangeReturn
Packit 971217
gst_audio_rate_change_state (GstElement * element, GstStateChange transition)
Packit 971217
{
Packit 971217
  GstAudioRate *audiorate = GST_AUDIO_RATE (element);
Packit 971217
Packit 971217
  switch (transition) {
Packit 971217
    case GST_STATE_CHANGE_PAUSED_TO_READY:
Packit 971217
      break;
Packit 971217
    case GST_STATE_CHANGE_READY_TO_PAUSED:
Packit 971217
      audiorate->in = 0;
Packit 971217
      audiorate->out = 0;
Packit 971217
      audiorate->drop = 0;
Packit 971217
      audiorate->add = 0;
Packit 971217
      gst_audio_info_init (&audiorate->info);
Packit 971217
      gst_audio_rate_reset (audiorate);
Packit 971217
      break;
Packit 971217
    default:
Packit 971217
      break;
Packit 971217
  }
Packit 971217
Packit 971217
  return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
plugin_init (GstPlugin * plugin)
Packit 971217
{
Packit 971217
  GST_DEBUG_CATEGORY_INIT (audio_rate_debug, "audiorate", 0,
Packit 971217
      "AudioRate stream fixer");
Packit 971217
Packit 971217
  return gst_element_register (plugin, "audiorate", GST_RANK_NONE,
Packit 971217
      GST_TYPE_AUDIO_RATE);
Packit 971217
}
Packit 971217
Packit 971217
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
Packit 971217
    GST_VERSION_MINOR,
Packit 971217
    audiorate,
Packit 971217
    "Adjusts audio frames",
Packit 971217
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)