/*
 * test ghostpad resetting
 *
 * gcc `pkg-config --cflags --libs gstreamer-0.10` bug596366.c -o bug596366
 *
 */

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


void test(void)
{
  GstElement *b1, *b2, *src1, *src2, *mix, *fmt, *sink1, *sink2;
  GstPad *sinkpad, *ghostpad;
  GstCaps *caps;

  b1 = gst_element_factory_make ("pipeline", NULL);
  b2 = gst_element_factory_make ("bin", NULL);
  src1 = gst_element_factory_make ("fakesrc", NULL);
  src2 = gst_element_factory_make ("fakesrc", NULL);
  mix = gst_element_factory_make ("adder", NULL);
  fmt = gst_element_factory_make ("capsfilter", NULL);
  sink1 = gst_element_factory_make ("fakesink", NULL);
  sink2 = gst_element_factory_make ("fakesink", NULL);

  gst_bin_add (GST_BIN (b2), sink1);
  gst_bin_add (GST_BIN (b2), sink2);
  gst_bin_add (GST_BIN (b1), src1);
  gst_bin_add (GST_BIN (b1), src2);
  gst_bin_add (GST_BIN (b1), mix);
  gst_bin_add (GST_BIN (b1), fmt);
  gst_bin_add (GST_BIN (b1), b2);

  caps = gst_caps_from_string ("audio/x-raw-int, width=16, channels=1");
  g_object_set (fmt, "caps", caps, NULL);
  gst_caps_unref (caps);

  /* create the ghostpad as a sink-pad for bin 2 */
  ghostpad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
  gst_element_add_pad (b2, ghostpad);

  sinkpad = gst_element_get_static_pad (sink1, "sink");
  gst_ghost_pad_set_target ((GstGhostPad *) ghostpad, sinkpad);
  gst_object_unref (sinkpad);

  gst_element_link_many (src1, mix, fmt, b2, NULL);
  gst_element_link_many (src2, mix, NULL);
  
  /* set different target after ghostpad is linked */
  sinkpad = gst_element_get_static_pad (sink2, "sink");
  gst_ghost_pad_set_target ((GstGhostPad *) ghostpad, sinkpad);
  gst_object_unref (sinkpad);

  /* clean up */
  gst_object_unref (b1);
}

gint main(gint argc, gchar *argv[])
{
  GstElement *b1, *b2, *src, *fmt, *sink1, *sink2;
  GstPad *sinkpad, *ghostpad;

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

  test();

  exit(0);
}
