/* rygel-gst-launch-audio-item.c generated by valac 0.29.2, the Vala compiler
 * generated from rygel-gst-launch-audio-item.vala, do not modify */

/*
 * Copyright (C) 2009 Thijs Vermeir <thijsvermeir@gmail.com>
 * Copyright (C) 2010 Nokia Corporation.
 * Copyright (C) 2012 Intel Corporation.
 *
 * Author: Thijs Vermeir <thijsvermeir@gmail.com>
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *                               <zeeshan.ali@nokia.com>
 *         Jens Georg <jensg@openismus.com>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Rygel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <glib.h>
#include <glib-object.h>
#include <rygel-server.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include <gee.h>


#define RYGEL_GST_LAUNCH_TYPE_AUDIO_ITEM (rygel_gst_launch_audio_item_get_type ())
#define RYGEL_GST_LAUNCH_AUDIO_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_GST_LAUNCH_TYPE_AUDIO_ITEM, RygelGstLaunchAudioItem))
#define RYGEL_GST_LAUNCH_AUDIO_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_GST_LAUNCH_TYPE_AUDIO_ITEM, RygelGstLaunchAudioItemClass))
#define RYGEL_GST_LAUNCH_IS_AUDIO_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_GST_LAUNCH_TYPE_AUDIO_ITEM))
#define RYGEL_GST_LAUNCH_IS_AUDIO_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_GST_LAUNCH_TYPE_AUDIO_ITEM))
#define RYGEL_GST_LAUNCH_AUDIO_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_GST_LAUNCH_TYPE_AUDIO_ITEM, RygelGstLaunchAudioItemClass))

typedef struct _RygelGstLaunchAudioItem RygelGstLaunchAudioItem;
typedef struct _RygelGstLaunchAudioItemClass RygelGstLaunchAudioItemClass;
typedef struct _RygelGstLaunchAudioItemPrivate RygelGstLaunchAudioItemPrivate;
typedef struct _Block1Data Block1Data;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _RygelGstLaunchAudioItem {
	RygelAudioItem parent_instance;
	RygelGstLaunchAudioItemPrivate * priv;
};

struct _RygelGstLaunchAudioItemClass {
	RygelAudioItemClass parent_class;
};

struct _Block1Data {
	int _ref_count_;
	RygelGstLaunchAudioItem* self;
	RygelMediaEngine* media_engine;
};


static gpointer rygel_gst_launch_audio_item_parent_class = NULL;

GType rygel_gst_launch_audio_item_get_type (void) G_GNUC_CONST;
enum  {
	RYGEL_GST_LAUNCH_AUDIO_ITEM_DUMMY_PROPERTY
};
RygelGstLaunchAudioItem* rygel_gst_launch_audio_item_new (const gchar* id, RygelMediaContainer* parent, const gchar* title, const gchar* mime_type, const gchar* launch_line);
RygelGstLaunchAudioItem* rygel_gst_launch_audio_item_construct (GType object_type, const gchar* id, RygelMediaContainer* parent, const gchar* title, const gchar* mime_type, const gchar* launch_line);
static Block1Data* block1_data_ref (Block1Data* _data1_);
static void block1_data_unref (void * _userdata_);
static void __lambda4_ (Block1Data* _data1_, GObject* obj, GAsyncResult* res);
static void ___lambda4__gasync_ready_callback (GObject* source_object, GAsyncResult* res, gpointer self);


static Block1Data* block1_data_ref (Block1Data* _data1_) {
	g_atomic_int_inc (&_data1_->_ref_count_);
	return _data1_;
}


static void block1_data_unref (void * _userdata_) {
	Block1Data* _data1_;
	_data1_ = (Block1Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data1_->_ref_count_)) {
		RygelGstLaunchAudioItem* self;
		self = _data1_->self;
		_g_object_unref0 (_data1_->media_engine);
		_g_object_unref0 (self);
		g_slice_free (Block1Data, _data1_);
	}
}


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


static void __lambda4_ (Block1Data* _data1_, GObject* obj, GAsyncResult* res) {
	RygelGstLaunchAudioItem* self;
	GeeList* added_resources = NULL;
	GAsyncResult* _tmp0_ = NULL;
	GeeList* _tmp1_ = NULL;
	GeeList* _tmp2_ = NULL;
	gint _tmp3_ = 0;
	gint _tmp4_ = 0;
	gchar* _tmp5_ = NULL;
	gchar* _tmp6_ = NULL;
	GeeList* _tmp21_ = NULL;
	GeeList* _tmp22_ = NULL;
	GeeList* _tmp23_ = NULL;
	self = _data1_->self;
	g_return_if_fail (res != NULL);
	_tmp0_ = res;
	_tmp1_ = rygel_media_engine_get_resources_for_item_finish (_data1_->media_engine, _tmp0_);
	added_resources = _tmp1_;
	_tmp2_ = added_resources;
	_tmp3_ = gee_collection_get_size ((GeeCollection*) _tmp2_);
	_tmp4_ = _tmp3_;
	_tmp5_ = rygel_media_object_get_primary_uri ((RygelMediaObject*) self);
	_tmp6_ = _tmp5_;
	g_debug ("rygel-gst-launch-audio-item.vala:48: Adding %d resources to item sourc" \
"e %s", _tmp4_, _tmp6_);
	_g_free0 (_tmp6_);
	{
		GeeList* _resrc_list = NULL;
		GeeList* _tmp7_ = NULL;
		GeeList* _tmp8_ = NULL;
		gint _resrc_size = 0;
		GeeList* _tmp9_ = NULL;
		gint _tmp10_ = 0;
		gint _tmp11_ = 0;
		gint _resrc_index = 0;
		_tmp7_ = added_resources;
		_tmp8_ = _g_object_ref0 (_tmp7_);
		_resrc_list = _tmp8_;
		_tmp9_ = _resrc_list;
		_tmp10_ = gee_collection_get_size ((GeeCollection*) _tmp9_);
		_tmp11_ = _tmp10_;
		_resrc_size = _tmp11_;
		_resrc_index = -1;
		while (TRUE) {
			gint _tmp12_ = 0;
			gint _tmp13_ = 0;
			gint _tmp14_ = 0;
			RygelMediaResource* resrc = NULL;
			GeeList* _tmp15_ = NULL;
			gint _tmp16_ = 0;
			gpointer _tmp17_ = NULL;
			RygelMediaResource* _tmp18_ = NULL;
			gchar* _tmp19_ = NULL;
			gchar* _tmp20_ = NULL;
			_tmp12_ = _resrc_index;
			_resrc_index = _tmp12_ + 1;
			_tmp13_ = _resrc_index;
			_tmp14_ = _resrc_size;
			if (!(_tmp13_ < _tmp14_)) {
				break;
			}
			_tmp15_ = _resrc_list;
			_tmp16_ = _resrc_index;
			_tmp17_ = gee_list_get (_tmp15_, _tmp16_);
			resrc = (RygelMediaResource*) _tmp17_;
			_tmp18_ = resrc;
			_tmp19_ = rygel_media_resource_get_name (_tmp18_);
			_tmp20_ = _tmp19_;
			g_debug ("rygel-gst-launch-audio-item.vala:51: Media-export item media resource " \
"%s", _tmp20_);
			_g_free0 (_tmp20_);
			_g_object_unref0 (resrc);
		}
		_g_object_unref0 (_resrc_list);
	}
	_tmp21_ = rygel_media_object_get_resource_list ((RygelMediaObject*) self);
	_tmp22_ = _tmp21_;
	_tmp23_ = added_resources;
	gee_collection_add_all ((GeeCollection*) _tmp22_, (GeeCollection*) _tmp23_);
	_g_object_unref0 (_tmp22_);
	_g_object_unref0 (added_resources);
}


static void ___lambda4__gasync_ready_callback (GObject* source_object, GAsyncResult* res, gpointer self) {
	__lambda4_ (self, source_object, res);
	block1_data_unref (self);
}


RygelGstLaunchAudioItem* rygel_gst_launch_audio_item_construct (GType object_type, const gchar* id, RygelMediaContainer* parent, const gchar* title, const gchar* mime_type, const gchar* launch_line) {
	RygelGstLaunchAudioItem * self = NULL;
	Block1Data* _data1_;
	const gchar* _tmp0_ = NULL;
	RygelMediaContainer* _tmp1_ = NULL;
	const gchar* _tmp2_ = NULL;
	const gchar* _tmp3_ = NULL;
	const gchar* _tmp4_ = NULL;
	gchar* _tmp5_ = NULL;
	gchar* _tmp6_ = NULL;
	RygelMediaEngine* _tmp7_ = NULL;
	g_return_val_if_fail (id != NULL, NULL);
	g_return_val_if_fail (parent != NULL, NULL);
	g_return_val_if_fail (title != NULL, NULL);
	g_return_val_if_fail (mime_type != NULL, NULL);
	g_return_val_if_fail (launch_line != NULL, NULL);
	_data1_ = g_slice_new0 (Block1Data);
	_data1_->_ref_count_ = 1;
	_tmp0_ = id;
	_tmp1_ = parent;
	_tmp2_ = title;
	self = (RygelGstLaunchAudioItem*) rygel_audio_item_construct (object_type, _tmp0_, _tmp1_, _tmp2_, RYGEL_AUDIO_ITEM_UPNP_CLASS);
	_data1_->self = g_object_ref (self);
	_tmp3_ = mime_type;
	rygel_media_file_item_set_mime_type ((RygelMediaFileItem*) self, _tmp3_);
	_tmp4_ = launch_line;
	_tmp5_ = g_strconcat ("gst-launch://", _tmp4_, NULL);
	_tmp6_ = _tmp5_;
	rygel_media_object_add_uri ((RygelMediaObject*) self, _tmp6_);
	_g_free0 (_tmp6_);
	_tmp7_ = rygel_media_engine_get_default ();
	_data1_->media_engine = _tmp7_;
	rygel_media_engine_get_resources_for_item (_data1_->media_engine, (RygelMediaObject*) self, ___lambda4__gasync_ready_callback, block1_data_ref (_data1_));
	block1_data_unref (_data1_);
	_data1_ = NULL;
	return self;
}


RygelGstLaunchAudioItem* rygel_gst_launch_audio_item_new (const gchar* id, RygelMediaContainer* parent, const gchar* title, const gchar* mime_type, const gchar* launch_line) {
	return rygel_gst_launch_audio_item_construct (RYGEL_GST_LAUNCH_TYPE_AUDIO_ITEM, id, parent, title, mime_type, launch_line);
}


static void rygel_gst_launch_audio_item_class_init (RygelGstLaunchAudioItemClass * klass) {
	rygel_gst_launch_audio_item_parent_class = g_type_class_peek_parent (klass);
}


static void rygel_gst_launch_audio_item_instance_init (RygelGstLaunchAudioItem * self) {
}


/**
 * Audio item that serves data from a gst-launch commandline.
 */
GType rygel_gst_launch_audio_item_get_type (void) {
	static volatile gsize rygel_gst_launch_audio_item_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_gst_launch_audio_item_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RygelGstLaunchAudioItemClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_gst_launch_audio_item_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelGstLaunchAudioItem), 0, (GInstanceInitFunc) rygel_gst_launch_audio_item_instance_init, NULL };
		GType rygel_gst_launch_audio_item_type_id;
		rygel_gst_launch_audio_item_type_id = g_type_register_static (RYGEL_TYPE_AUDIO_ITEM, "RygelGstLaunchAudioItem", &g_define_type_info, 0);
		g_once_init_leave (&rygel_gst_launch_audio_item_type_id__volatile, rygel_gst_launch_audio_item_type_id);
	}
	return rygel_gst_launch_audio_item_type_id__volatile;
}



