#include <gtk/gtk.h>
#include <gst/gst.h>
#include <gst/gst.h>
#include <gst/base/gstbasesrc.h>

#include <gst/interfaces/tuner.h>

int main(int argc, char **argv)
{
  GstElement *pipeline; 
  GstTuner *tuner = NULL;
  GstTunerChannel *channel = NULL;

  GstElement *video_source;
  GstElement *csp;
  GstElement *video_sink;

  gtk_init(&argc, &argv);
  gst_init (&argc, &argv);

  pipeline = gst_pipeline_new ("pipeline");

  video_source = gst_element_factory_make ("v4l2src", "video_src");

  tuner = GST_TUNER (video_source);


  printf("  v4l2src just created\n");

  if ( tuner == NULL ) {
    printf("    debug 1: tuner is still NULL\n");
  } else {
    channel = gst_tuner_get_channel (tuner);
  }
  if ( channel == NULL ) {
    printf("    debug 1: channel is still NULL\n");
  } else {
    printf("    debug 1: tuner and channel not nil\n");
  }
  

  csp = gst_element_factory_make ("ffmpegcolorspace", "csp");
  video_sink = gst_element_factory_make ("ximagesink", "video_sink");

  gst_bin_add_many (GST_BIN (pipeline), video_source, csp, video_sink, NULL);

  gst_element_link_many (video_source, csp, video_sink, NULL);
 

  printf("  v4l2src just added to pipeline and linked\n");

  if ( tuner == NULL ) {
    tuner = GST_TUNER (video_source);
  }

  if ( tuner == NULL ) {
    printf("    debug 2: tuner is still NULL\n");
  } else {
    channel = gst_tuner_get_channel (tuner);
  }
  if ( channel == NULL ) {
    printf("    debug 2: channel still NULL\n");
  } else {
    printf("    debug 2: tuner and channel not nil\n");
  }
  
  gst_element_set_state (pipeline, GST_STATE_PLAYING);

  printf("  v4l2src just put in playing state\n");

  if ( tuner == NULL ) {
    tuner = GST_TUNER (video_source);
  }

  if ( tuner == NULL ) {
    printf("    debug 3: tuner is still NULL\n");
  } else {
    channel = gst_tuner_get_channel (tuner);
  }
  if ( channel == NULL ) {
    printf("    debug 3: channel stil NULL\n");
  } else {
    printf("    debug 3: tuner and channel not nil\n");
  }


  gtk_main();
  return 0;
}


