#include <stdio.h>
#include <gst/gst.h>
#include <stdlib.h>

int main(int argc, char* argv[] ) {
	if ( argc != 2 ) {
		g_print("usage: vaapi_test n_pipelines\n");
        return -1;
    }

	gst_init (&argc, &argv);

	GError *error = NULL;
	GstElement **pipelines;
	GMainLoop *g_main_loop = NULL;

	int count = strtol(argv[1],NULL,10);
	pipelines = (GstElement **)malloc(count * sizeof(GstElement*));

	for(int i=0; i<count; i++ ) {
		pipelines[i] = gst_parse_launch("videotestsrc is-live=true ! video/x-raw,width=1280,height=1024,framerate=20/1 ! vaapih264enc ! testsink", &error);
		if(!pipelines[i]) {
			g_print("Parse error: %s\n",error->message);
			return -1;
		}
		gst_element_set_state(pipelines[i] , GST_STATE_PLAYING);
	}

	g_main_loop = g_main_loop_new (NULL, FALSE);
	g_main_loop_run (g_main_loop);

	for(int i=0; i<count; i++ ) {
		gst_element_set_state(pipelines[i] , GST_STATE_NULL);
		gst_object_unref(pipelines[i]);
	}
	g_main_loop_unref (g_main_loop);

	return 0;
}

