#include <gst/gst.h>
#include <boost/thread.hpp>

#include "CGSTMainloopThread.h"
#include <iostream>

int main(int argc, char* argv[])
{
    boost::scoped_ptr<gst::CGSTMainloopThread> m_mainLoop(new gst::CGSTMainloopThread(argc, argv));
    boost::thread t = boost::thread(boost::ref( *(m_mainLoop.get()) ));

    GstElement* pipeline(gst_pipeline_new("pipeline"));
    GstElement* src(gst_element_factory_make("decklinksrc", "source"));
    GstElement* queue(gst_element_factory_make("queue", "queue0"));
    GstElement* con(gst_element_factory_make("videoconvert", "conv"));
    GstElement* dst(gst_element_factory_make("autovideosink", "sink"));
    GstElement* fake(gst_element_factory_make("fakesink", "fakesink"));

    unsigned int m = 12;
    unsigned int c = 1;
    unsigned int d = 0;
    unsigned int a = 1;
    g_object_set(G_OBJECT(src), "mode", m, NULL);
    g_object_set(G_OBJECT(src), "connection", c, NULL);
    g_object_set(G_OBJECT(src), "device-number", d, NULL);
    g_object_set(G_OBJECT(src), "audio-input", a, NULL);

    g_object_set(G_OBJECT(dst), "sync", false, NULL);
    g_object_set(G_OBJECT(fake), "sync", false, NULL);

    gst_bin_add_many(GST_BIN(pipeline), src, con, dst, fake, queue, NULL);


    gst_element_link(src, fake);

    gst_element_link_many(src, queue, con, dst, NULL);


    int i =0;

    while (1)
    {
        std::cout << "i: " << i << std::endl;
        gst_element_set_state(pipeline, GST_STATE_PLAYING);
        boost::this_thread::sleep_for(boost::chrono::seconds(40));
        gst_element_set_state(pipeline, GST_STATE_NULL);
        boost::this_thread::sleep_for(boost::chrono::seconds(3));
        ++i;
    }
}
