/* Eloi Bail : eloi.bail<at>savoirfairelinux.com
 *
 * Simple test showing that reverve playback is not working with videotestsrc
 * - Reproduced on 0.10 SDK and 1.0 HEAD
 * - Working fine with audiotestsrc
 * - audiotestsrc and videotestsrc are both used for gap on Gstreamer Editing Services
*/

#include <gst/gst.h>
#include <gst/app/gstappsink.h>

GstElement *pipeline, *audiotestsrc, *audiosink;
GstElement *appsink, *appsrc;
GstBus *bus;
GMainLoop * main_loop;
GList *samples;
GList *iter = NULL;

void appsrc_needdata (GstElement *appsrc, guint arg, gpointer udata)
{
  GList *iter;
  GstSample *sample;

  g_print ("Pushing a loop of buffers\n");

  for (iter = samples; iter; iter = iter->next) {
    sample = iter->data;
    GST_BUFFER_FLAG_UNSET (gst_sample_get_buffer (sample), GST_BUFFER_FLAG_DISCONT);
    gst_app_src_push_buffer (appsrc, gst_buffer_ref (gst_sample_get_buffer (sample)));
  }
}

int main(int argc, char *argv[]) {
  gint i;
  gst_init (&argc, &argv);

  pipeline = gst_pipeline_new ("test_pipeline");
  audiotestsrc = gst_element_factory_make ("audiotestsrc", "myaudiotestsrc");
  appsink = gst_element_factory_make ("appsink", NULL);
  gst_bin_add_many (GST_BIN(pipeline), audiotestsrc, appsink, NULL);
  gst_element_link_many (audiotestsrc, appsink, NULL);

  g_print("Collecting audio playback data\n");

  /* Get 10 buffers of 10 different streams.
   * all streams will start at 0 */
  for (i = 0; i < 10; i++) {
    gint j;
    gdouble freq;
    GstSample *sample;

    g_object_get (audiotestsrc, "freq", &freq, NULL);
    g_object_set (audiotestsrc, "num-buffers", 10, "freq", freq+200, NULL);
    gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
    g_usleep (1 * 1000000L);

    for (j = 0; j < 10; j++) {
      sample = gst_app_sink_pull_sample (GST_APP_SINK (appsink));
      samples = g_list_append (samples, sample);
    }
    gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
  }
  gst_object_unref (pipeline);

  g_print ("STARTING PLAYBACK\n");

  pipeline = gst_pipeline_new ("test_pipeline");
  appsrc = gst_element_factory_make ("appsrc", NULL);
  audiosink = gst_element_factory_make ("autoaudiosink", NULL);
  gst_bin_add_many (GST_BIN(pipeline), appsrc, audiosink, NULL);
  gst_element_link_many (appsrc, audiosink, NULL);

  /* use appsrc to playback the buffers, they will all play */
  gst_app_src_set_caps (appsrc, gst_sample_get_caps (samples->data));
  gst_app_src_set_emit_signals (appsrc, TRUE);
  //gst_app_src_set_stream_type (appsrc, GST_APP_STREAM_TYPE_STREAM);
  g_object_set (appsrc, "format", 3, NULL);
  g_signal_connect (appsrc, "need-data", G_CALLBACK(appsrc_needdata), NULL);

  gst_element_set_state (pipeline, GST_STATE_READY);
 
  main_loop = g_main_loop_new (NULL, FALSE);
  bus = gst_element_get_bus (GST_ELEMENT (pipeline));
  /* Create a GLib Main Loop and set it to run */

  gst_element_set_state (pipeline, GST_STATE_PLAYING);
  g_main_loop_run (main_loop);
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);
}
