/* gcc test.c `pkg-config --cflags --libs gstreamer-1.0` */
#include <gst/gst.h>

#define VIDEO_SINK "glimagesink"
#define VIDEO_FILE "/home/xclaesse/programmation/videos/big_buck_bunny_720p_h264.mp4"
#define JPEG_FILE "/home/xclaesse/Images/xclaesse.jpg"

typedef struct
{
  GstElement *pipeline;
  GstElement *sink;
  GstPad *sink_pad;
  GstPad *src_pad;
} Context;

static GstPadProbeReturn
idle_probe_cb (GstPad *pad,
    GstPadProbeInfo *info,
    gpointer user_data)
{
  Context *ctx = user_data;
  GstElement *src_bin;
  GstPad *src_pad;

  /* Pause pipeline and unlink the source */
  gst_element_set_state (ctx->pipeline, GST_STATE_PAUSED);
  gst_pad_unlink (ctx->src_pad, ctx->sink_pad);

  /* Create a new source and plug it */
  src_bin = gst_parse_bin_from_description ("filesrc location="JPEG_FILE" ! jpegdec", TRUE, NULL);
  src_pad = gst_element_get_static_pad (src_bin, "src");
  gst_bin_add (GST_BIN (ctx->pipeline), src_bin);
  gst_pad_link (src_pad, ctx->sink_pad);
  gst_element_sync_state_with_parent (src_bin);

  /* Leave the old source blocked */
  return GST_PAD_PROBE_OK;
}

int main()
{
  Context ctx;

  gst_init (NULL, NULL);

  ctx.pipeline = gst_parse_launch ("filesrc location="VIDEO_FILE" ! decodebin ! "VIDEO_SINK" name=videosink", NULL);
  ctx.sink = gst_bin_get_by_name (GST_BIN (ctx.pipeline), "videosink");
  ctx.sink_pad = gst_element_get_static_pad (ctx.sink, "sink");

  /* Play 1s */
  gst_element_set_state (ctx.pipeline, GST_STATE_PLAYING);
  g_usleep (G_USEC_PER_SEC);

  /* Block the stream */
  ctx.src_pad = gst_pad_get_peer (ctx.sink_pad);
  gst_pad_add_probe (ctx.src_pad,
      GST_PAD_PROBE_TYPE_IDLE |
      GST_PAD_PROBE_TYPE_BLOCK |
      GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
      idle_probe_cb, &ctx, NULL);

  /* Poor man's main loop */
  while (1);
}
