+ sinkpad = gst_element_get_static_pad(decoder, "sink");
+ gst_pad_link(pad, sinkpad);
+ gst_object_unref(sinkpad);
+}
+
+struct play_file_param
+{
+ GstElement *pipeline;
+ GstElement *source;
+ gint* sval;
+ GCond* cond;
+ GMutex* mutex;
+ GAsyncQueue* queue;
+ const char* media_d;
+};
+
+static gpointer player(gpointer data)
+{
+ struct play_file_param *p = (struct play_file_param*)data;
+ GstElement* pipeline = p->pipeline;
+ GstElement* source = p->source;
+ gint* sval = p->sval;
+ GCond* cond = p->cond;
+ GMutex* mutex = p->mutex;
+ GAsyncQueue* queue = p->queue;
+ const char* media_d = p->media_d;
+ free(p);
+
+ g_printf("Player thread started\n");
+
+ for(;;) {
+ g_mutex_lock(mutex);
+ while((*sval) < 1)
+ g_cond_wait(cond, mutex);
+ *sval = 0;
+ g_mutex_unlock(mutex);
+
+ char* name = (char*)g_async_queue_pop(queue);
+ if(!name)
+ return NULL;
+
+ gst_element_set_state(pipeline, GST_STATE_READY);
+
+ char* path;
+ asprintf(&path, "%s/%s.ogg", media_d, name);
+ free(name);
+ if(!path)
+ return NULL;
+
+ g_print("playing '%s'\n", path);
+ g_object_set(G_OBJECT(source), "location", path, NULL);
+ free(path);
+ gst_element_set_state(pipeline, GST_STATE_PLAYING);
+ }
+
+ return NULL;
+}
+
+GstElement* init_pipeline(GMainLoop *loop, const char* media_d, GAsyncQueue* queue, gint* sval)
+{
+ GCond* cond = g_cond_new();
+ if(!cond) {
+ g_printerr("Condition could not be created.\n");
+ return NULL;
+ }
+ GMutex* mutex = g_mutex_new();
+ if(!cond) {
+ g_printerr("Mutex could not be created.\n");
+ return NULL;
+ }
+
+ GstElement *pipeline = gst_pipeline_new("dart-sounds");
+ GstElement *source = gst_element_factory_make("filesrc", "source");
+ GstElement *demuxer = gst_element_factory_make("oggdemux", "demuxer");
+ GstElement *decoder = gst_element_factory_make("vorbisdec", "decoder");
+ GstElement *conv = gst_element_factory_make("audioconvert", "converter");
+ GstElement *filter = gst_element_factory_make("capsfilter", "filter");
+ GstElement *sink = gst_element_factory_make("autoaudiosink", "sink");
+
+ if (!pipeline || !source || !demuxer || !decoder || !conv || !filter || !sink) {
+ g_printerr("One element could not be created. Exiting.\n");
+ return NULL;
+ }
+
+ GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE (pipeline));
+ struct bus_call_param* datab = malloc(sizeof(struct bus_call_param));
+ if(!datab) {
+ g_printerr("Memory error\n");
+ return NULL;
+ }
+ datab->loop = loop;
+ datab->sval = sval;
+ datab->cond = cond;
+ datab->mutex = mutex;
+ gst_bus_add_watch(bus, bus_call, datab);
+ gst_object_unref(bus);
+
+
+ GstCaps* caps = gst_caps_new_simple("audio/x-raw-int", "channels", G_TYPE_INT, 2, NULL);
+ g_object_set(G_OBJECT(filter), "caps", caps, NULL);
+
+ gst_bin_add_many(GST_BIN(pipeline), source, demuxer, decoder, conv, filter, sink, NULL);
+ gst_element_link(source, demuxer);
+ gst_element_link_many(decoder, conv, filter, sink, NULL);
+ g_signal_connect(demuxer, "pad-added", G_CALLBACK(on_pad_added), decoder);
+
+ struct play_file_param* datap = malloc(sizeof(struct play_file_param));
+ if(datap) {
+ datap->pipeline = pipeline;
+ datap->source = source;
+ datap->sval = sval;
+ datap->cond = cond;
+ datap->mutex = mutex;
+ datap->queue = queue;
+ datap->media_d = media_d;
+ g_thread_create(player, datap, 0, NULL);
+ } else {
+ g_printerr("Memory Error\n");
+ return NULL;
+ }
+
+ return pipeline;