#include <gst/gst.h>
#include <gst/base/gstadapter.h>

#define fail_unless(a) g_assert(a)

#define NB_CHANNELS 1
#define DATA_FORMAT_SIZE sizeof(gint16)
#define RATE 44100
#define SAMPLE 1024
#define BUFFER_SIZE (DATA_FORMAT_SIZE * SAMPLE * NB_CHANNELS)


int main(int argc, char *argv[]) {
  GstAdapter *adapter = NULL;
  GstBuffer *tmp = NULL;
  GstClockTime next_ts = 0;
  GstClockTime duration;

  gint i;
  gint nbuffers = 10;
  guint64 distance = 0;
  gsize offset;
  GstClockTime pts;

  /* Initialize GStreamer */
  gst_init (&argc, &argv);

  adapter = gst_adapter_new ();

  duration = gst_util_uint64_scale_int_round (SAMPLE, GST_SECOND, RATE);

  for (i = 0; i < nbuffers; i++) {
    tmp = gst_buffer_new_allocate (NULL, BUFFER_SIZE, NULL);
    GST_BUFFER_PTS (tmp) = next_ts;
    GST_BUFFER_DURATION (tmp) = duration;
    next_ts += duration;

    gst_adapter_push (adapter, tmp);
  }

  for (i = 0; i < nbuffers; i++) {
    offset = i * BUFFER_SIZE;
    pts = gst_adapter_prev_pts_at_offset (adapter, offset, &distance);
    g_message("offset %" G_GSIZE_FORMAT " PTS=%" GST_TIME_FORMAT
        " distance=%"G_GUINT64_FORMAT, offset, GST_TIME_ARGS (pts), distance);
  }

  gst_object_unref (adapter);
  return 0;
}
