/* rygel-media-export-jpeg-writer.c generated by valac 0.24.0.112-bbf5, the Vala compiler
 * generated from rygel-media-export-jpeg-writer.vala, do not modify */

/*
 * Copyright (C) 2010 Jens Georg <mail@jensge.org>.
 *
 * Author: Jens Georg <mail@jensge.org>
 *
 * 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 <gst/gst.h>
#include <gst/app/app.h>
#include <gio/gio.h>


#define RYGEL_MEDIA_EXPORT_TYPE_JPEG_WRITER (rygel_media_export_jpeg_writer_get_type ())
#define RYGEL_MEDIA_EXPORT_JPEG_WRITER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_MEDIA_EXPORT_TYPE_JPEG_WRITER, RygelMediaExportJPEGWriter))
#define RYGEL_MEDIA_EXPORT_JPEG_WRITER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_MEDIA_EXPORT_TYPE_JPEG_WRITER, RygelMediaExportJPEGWriterClass))
#define RYGEL_MEDIA_EXPORT_IS_JPEG_WRITER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_MEDIA_EXPORT_TYPE_JPEG_WRITER))
#define RYGEL_MEDIA_EXPORT_IS_JPEG_WRITER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_MEDIA_EXPORT_TYPE_JPEG_WRITER))
#define RYGEL_MEDIA_EXPORT_JPEG_WRITER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_MEDIA_EXPORT_TYPE_JPEG_WRITER, RygelMediaExportJPEGWriterClass))

typedef struct _RygelMediaExportJPEGWriter RygelMediaExportJPEGWriter;
typedef struct _RygelMediaExportJPEGWriterClass RygelMediaExportJPEGWriterClass;
typedef struct _RygelMediaExportJPEGWriterPrivate RygelMediaExportJPEGWriterPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_unref (var), NULL)))

struct _RygelMediaExportJPEGWriter {
	GObject parent_instance;
	RygelMediaExportJPEGWriterPrivate * priv;
};

struct _RygelMediaExportJPEGWriterClass {
	GObjectClass parent_class;
};

struct _RygelMediaExportJPEGWriterPrivate {
	GstBin* bin;
	GstAppSrc* appsrc;
	GMainLoop* loop;
	GstElement* sink;
};


static gpointer rygel_media_export_jpeg_writer_parent_class = NULL;

GType rygel_media_export_jpeg_writer_get_type (void) G_GNUC_CONST;
#define RYGEL_MEDIA_EXPORT_JPEG_WRITER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RYGEL_MEDIA_EXPORT_TYPE_JPEG_WRITER, RygelMediaExportJPEGWriterPrivate))
enum  {
	RYGEL_MEDIA_EXPORT_JPEG_WRITER_DUMMY_PROPERTY
};
RygelMediaExportJPEGWriter* rygel_media_export_jpeg_writer_new (GError** error);
RygelMediaExportJPEGWriter* rygel_media_export_jpeg_writer_construct (GType object_type, GError** error);
static void __lambda6_ (RygelMediaExportJPEGWriter* self);
static void ___lambda6__gst_bus_message (GstBus* _sender, GstMessage* message, gpointer self);
static void __lambda7_ (RygelMediaExportJPEGWriter* self);
static void ___lambda7__gst_bus_message (GstBus* _sender, GstMessage* message, gpointer self);
void rygel_media_export_jpeg_writer_write (RygelMediaExportJPEGWriter* self, GstBuffer* buffer, GFile* file);
static inline void _dynamic_set_file0 (GstElement* obj, GFile* value);
static void rygel_media_export_jpeg_writer_finalize (GObject* obj);


static void __lambda6_ (RygelMediaExportJPEGWriter* self) {
	GMainLoop* _tmp0_ = NULL;
	_tmp0_ = self->priv->loop;
	g_main_loop_quit (_tmp0_);
}


static void ___lambda6__gst_bus_message (GstBus* _sender, GstMessage* message, gpointer self) {
	__lambda6_ ((RygelMediaExportJPEGWriter*) self);
}


static void __lambda7_ (RygelMediaExportJPEGWriter* self) {
	GMainLoop* _tmp0_ = NULL;
	_tmp0_ = self->priv->loop;
	g_main_loop_quit (_tmp0_);
}


static void ___lambda7__gst_bus_message (GstBus* _sender, GstMessage* message, gpointer self) {
	__lambda7_ ((RygelMediaExportJPEGWriter*) self);
}


RygelMediaExportJPEGWriter* rygel_media_export_jpeg_writer_construct (GType object_type, GError** error) {
	RygelMediaExportJPEGWriter * self = NULL;
	GstElement* _tmp0_ = NULL;
	GstElement* _tmp1_ = NULL;
	GstElement* _tmp2_ = NULL;
	GstBin* _tmp3_ = NULL;
	GstBin* _tmp4_ = NULL;
	GstElement* _tmp5_ = NULL;
	GstAppSrc* _tmp6_ = NULL;
	GstBin* _tmp7_ = NULL;
	GstElement* _tmp8_ = NULL;
	GstBus* bus = NULL;
	GstBin* _tmp9_ = NULL;
	GstBus* _tmp10_ = NULL;
	GstBus* _tmp11_ = NULL;
	GstBus* _tmp12_ = NULL;
	GstBus* _tmp13_ = NULL;
	GMainLoop* _tmp14_ = NULL;
	GError * _inner_error_ = NULL;
	self = (RygelMediaExportJPEGWriter*) g_object_new (object_type, NULL);
	_tmp1_ = gst_parse_launch_full ("appsrc name=src ! decodebin ! " "autovideoconvert ! " "jpegenc ! giosink name=sink", NULL, GST_PARSE_FLAG_NONE, &_inner_error_);
	g_object_ref_sink (_tmp1_);
	_tmp0_ = _tmp1_;
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_propagate_error (error, _inner_error_);
		_g_object_unref0 (self);
		return NULL;
	}
	_tmp2_ = _tmp0_;
	_tmp0_ = NULL;
	g_object_ref_sink (_tmp2_);
	_tmp3_ = G_TYPE_CHECK_INSTANCE_TYPE (_tmp2_, gst_bin_get_type ()) ? ((GstBin*) _tmp2_) : NULL;
	if (_tmp3_ == NULL) {
		_g_object_unref0 (_tmp2_);
	}
	_g_object_unref0 (self->priv->bin);
	self->priv->bin = _tmp3_;
	_tmp4_ = self->priv->bin;
	_tmp5_ = gst_bin_get_by_name (_tmp4_, "src");
	_tmp6_ = G_TYPE_CHECK_INSTANCE_TYPE (_tmp5_, gst_app_src_get_type ()) ? ((GstAppSrc*) _tmp5_) : NULL;
	if (_tmp6_ == NULL) {
		_g_object_unref0 (_tmp5_);
	}
	_g_object_unref0 (self->priv->appsrc);
	self->priv->appsrc = _tmp6_;
	_tmp7_ = self->priv->bin;
	_tmp8_ = gst_bin_get_by_name (_tmp7_, "sink");
	_g_object_unref0 (self->priv->sink);
	self->priv->sink = _tmp8_;
	_tmp9_ = self->priv->bin;
	_tmp10_ = gst_element_get_bus ((GstElement*) _tmp9_);
	bus = _tmp10_;
	_tmp11_ = bus;
	gst_bus_add_signal_watch_full (_tmp11_, G_PRIORITY_DEFAULT);
	_tmp12_ = bus;
	g_signal_connect_object (_tmp12_, "message::eos", (GCallback) ___lambda6__gst_bus_message, self, 0);
	_tmp13_ = bus;
	g_signal_connect_object (_tmp13_, "message::error", (GCallback) ___lambda7__gst_bus_message, self, 0);
	_tmp14_ = g_main_loop_new (NULL, FALSE);
	_g_main_loop_unref0 (self->priv->loop);
	self->priv->loop = _tmp14_;
	_g_object_unref0 (bus);
	_g_object_unref0 (_tmp0_);
	return self;
}


RygelMediaExportJPEGWriter* rygel_media_export_jpeg_writer_new (GError** error) {
	return rygel_media_export_jpeg_writer_construct (RYGEL_MEDIA_EXPORT_TYPE_JPEG_WRITER, error);
}


/**
     * Write a Gst.Buffer as retrieved from the Gst.TagList to disk.
     *
     * @param buffer The Gst.Buffer as obtained from tag list
     * @param file   A GLib.File pointing to the target location
     *
     * FIXME This uses a nested main-loop to block which is ugly.
     */
static inline void _dynamic_set_file0 (GstElement* obj, GFile* value) {
	g_object_set (obj, "file", value, NULL);
}


void rygel_media_export_jpeg_writer_write (RygelMediaExportJPEGWriter* self, GstBuffer* buffer, GFile* file) {
	GstFlowReturn flow = 0;
	GstElement* _tmp0_ = NULL;
	GFile* _tmp1_ = NULL;
	GstAppSrc* _tmp2_ = NULL;
	GstBuffer* _tmp3_ = NULL;
	GstAppSrc* _tmp4_ = NULL;
	GstBin* _tmp5_ = NULL;
	GMainLoop* _tmp6_ = NULL;
	GstBin* _tmp7_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (buffer != NULL);
	g_return_if_fail (file != NULL);
	_tmp0_ = self->priv->sink;
	_tmp1_ = file;
	_dynamic_set_file0 (_tmp0_, _tmp1_);
	_tmp2_ = self->priv->appsrc;
	_tmp3_ = buffer;
	g_signal_emit_by_name (_tmp2_, "push-buffer", _tmp3_, &flow, NULL);
	_tmp4_ = self->priv->appsrc;
	gst_app_src_end_of_stream (_tmp4_);
	_tmp5_ = self->priv->bin;
	gst_element_set_state ((GstElement*) _tmp5_, GST_STATE_PLAYING);
	_tmp6_ = self->priv->loop;
	g_main_loop_run (_tmp6_);
	_tmp7_ = self->priv->bin;
	gst_element_set_state ((GstElement*) _tmp7_, GST_STATE_NULL);
}


static void rygel_media_export_jpeg_writer_class_init (RygelMediaExportJPEGWriterClass * klass) {
	rygel_media_export_jpeg_writer_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (RygelMediaExportJPEGWriterPrivate));
	G_OBJECT_CLASS (klass)->finalize = rygel_media_export_jpeg_writer_finalize;
}


static void rygel_media_export_jpeg_writer_instance_init (RygelMediaExportJPEGWriter * self) {
	self->priv = RYGEL_MEDIA_EXPORT_JPEG_WRITER_GET_PRIVATE (self);
}


static void rygel_media_export_jpeg_writer_finalize (GObject* obj) {
	RygelMediaExportJPEGWriter * self;
	GstBin* _tmp0_ = NULL;
	GstBus* _tmp1_ = NULL;
	GstBus* _tmp2_ = NULL;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_MEDIA_EXPORT_TYPE_JPEG_WRITER, RygelMediaExportJPEGWriter);
	_tmp0_ = self->priv->bin;
	_tmp1_ = gst_element_get_bus ((GstElement*) _tmp0_);
	_tmp2_ = _tmp1_;
	gst_bus_remove_signal_watch (_tmp2_);
	_g_object_unref0 (_tmp2_);
	_g_object_unref0 (self->priv->bin);
	_g_object_unref0 (self->priv->appsrc);
	_g_main_loop_unref0 (self->priv->loop);
	_g_object_unref0 (self->priv->sink);
	G_OBJECT_CLASS (rygel_media_export_jpeg_writer_parent_class)->finalize (obj);
}


/**
 * Utility class to write media-art content to JPEG files
 *
 * This uses a gstreamer pipeline to transcode the image tag as contained in
 * MP3 files. This class is single-shot, use and then throw away.
 */
GType rygel_media_export_jpeg_writer_get_type (void) {
	static volatile gsize rygel_media_export_jpeg_writer_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_media_export_jpeg_writer_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RygelMediaExportJPEGWriterClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_media_export_jpeg_writer_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelMediaExportJPEGWriter), 0, (GInstanceInitFunc) rygel_media_export_jpeg_writer_instance_init, NULL };
		GType rygel_media_export_jpeg_writer_type_id;
		rygel_media_export_jpeg_writer_type_id = g_type_register_static (G_TYPE_OBJECT, "RygelMediaExportJPEGWriter", &g_define_type_info, 0);
		g_once_init_leave (&rygel_media_export_jpeg_writer_type_id__volatile, rygel_media_export_jpeg_writer_type_id);
	}
	return rygel_media_export_jpeg_writer_type_id__volatile;
}



