#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <gst/gst.h>

int main (int argc, char **argv)
{
  gchar *pipe_string;
  GstElement *elem;
  GError *err;
  char c;

  if (! g_thread_supported ())
    g_thread_init (NULL);
  gst_init (&argc, &argv);

  pipe_string = g_strdup_printf ("videotestsrc"
                                 " ! video/x-raw-yuv,width=1024,height=1024,"
                                 "framerate=2/1 ! ffmpegcolorspace ! x264enc ! fakesink sync=true");

  elem = gst_parse_launch (pipe_string, &err);
  if (! elem) {
    fprintf (stderr, "couldn't parse pipeline (%s).\n", err -> message);
    return -1;
  }

  gst_element_set_state (elem, GST_STATE_PLAYING);

  do {
    c = getchar ();
    fprintf (stderr, "got %c\n", c);
    getchar ();

    if (c >= '0' && c <= '9') {
      int t = (c - '0') * 10;
      GstEvent *event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
                                            GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
                                            GST_SEEK_TYPE_SET,
                                            GST_SECOND * t,
                                            GST_SEEK_TYPE_SET,
                                            GST_CLOCK_TIME_NONE);
      gst_event_ref (event);
      gst_element_send_event (elem, event);
      gst_event_unref (event);
    }
  } while (c != 'x');

  return 0;
}
