/* rygel-media-export-jpeg-writer.c generated by valac 0.17.4.16-52707, 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/gstappsrc.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 _gst_object_unref0(var) ((var == NULL) ? NULL : (var = (gst_object_unref (var), NULL)))
#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_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 __lambda3_ (RygelMediaExportJPEGWriter* self);
static void ___lambda3__gst_bus_message (GstBus* _sender, GstMessage* message, gpointer self);
static void __lambda4_ (RygelMediaExportJPEGWriter* self);
static void ___lambda4__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 __lambda3_ (RygelMediaExportJPEGWriter* self) {
	GMainLoop* _tmp0_;
	_tmp0_ = self->priv->loop;
	g_main_loop_quit (_tmp0_);
}


static void ___lambda3__gst_bus_message (GstBus* _sender, GstMessage* message, gpointer self) {
	__lambda3_ (self);
}


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


static void ___lambda4__gst_bus_message (GstBus* _sender, GstMessage* message, gpointer self) {
	__lambda4_ (self);
}


RygelMediaExportJPEGWriter* rygel_media_export_jpeg_writer_construct (GType object_type, GError** error) {
	RygelMediaExportJPEGWriter * self = NULL;
	GstElement* _tmp0_ = NULL;
	GstElement* _tmp1_;
	GstBin* _tmp2_;
	GstElement* _tmp3_ = NULL;
	GstBin* _tmp4_;
	GstElement* _tmp5_ = NULL;
	GstBin* _tmp6_;
	GstBus* _tmp7_ = NULL;
	GstBus* bus;
	GstBus* _tmp8_;
	GstBus* _tmp9_;
	GstBus* _tmp10_;
	GMainLoop* _tmp11_;
	GError * _inner_error_ = NULL;
	self = (RygelMediaExportJPEGWriter*) g_object_new (object_type, NULL);
	_tmp0_ = gst_parse_launch ("appsrc name=src ! decodebin2 ! " "ffmpegcolorspace ! " "jpegenc ! giosink name=sink", &_inner_error_);
	_tmp1_ = _tmp0_;
	if (_inner_error_ != NULL) {
		g_propagate_error (error, _inner_error_);
		_g_object_unref0 (self);
		return NULL;
	}
	_gst_object_unref0 (self->priv->bin);
	self->priv->bin = G_TYPE_CHECK_INSTANCE_TYPE (_tmp1_, GST_TYPE_BIN) ? ((GstBin*) _tmp1_) : NULL;
	_tmp2_ = self->priv->bin;
	_tmp3_ = gst_bin_get_by_name (_tmp2_, "src");
	_gst_object_unref0 (self->priv->appsrc);
	self->priv->appsrc = G_TYPE_CHECK_INSTANCE_TYPE (_tmp3_, GST_TYPE_APP_SRC) ? ((GstAppSrc*) _tmp3_) : NULL;
	_tmp4_ = self->priv->bin;
	_tmp5_ = gst_bin_get_by_name (_tmp4_, "sink");
	_gst_object_unref0 (self->priv->sink);
	self->priv->sink = _tmp5_;
	_tmp6_ = self->priv->bin;
	_tmp7_ = gst_element_get_bus ((GstElement*) _tmp6_);
	bus = _tmp7_;
	_tmp8_ = bus;
	gst_bus_add_signal_watch (_tmp8_);
	_tmp9_ = bus;
	g_signal_connect_object (_tmp9_, "message::eos", (GCallback) ___lambda3__gst_bus_message, self, 0);
	_tmp10_ = bus;
	g_signal_connect_object (_tmp10_, "message::error", (GCallback) ___lambda4__gst_bus_message, self, 0);
	_tmp11_ = g_main_loop_new (NULL, FALSE);
	_g_main_loop_unref0 (self->priv->loop);
	self->priv->loop = _tmp11_;
	_gst_object_unref0 (bus);
	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);
}


static gpointer _gst_buffer_ref0 (gpointer self) {
	return self ? gst_buffer_ref (self) : NULL;
}


void rygel_media_export_jpeg_writer_write (RygelMediaExportJPEGWriter* self, GstBuffer* buffer, GFile* file) {
	GstElement* _tmp0_;
	GFile* _tmp1_;
	GstAppSrc* _tmp2_;
	GstBuffer* _tmp3_;
	GstBuffer* _tmp4_;
	GstAppSrc* _tmp5_;
	GstBin* _tmp6_;
	GMainLoop* _tmp7_;
	GstBin* _tmp8_;
	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;
	_tmp4_ = _gst_buffer_ref0 (_tmp3_);
	gst_app_src_push_buffer (_tmp2_, _tmp4_);
	_tmp5_ = self->priv->appsrc;
	gst_app_src_end_of_stream (_tmp5_);
	_tmp6_ = self->priv->bin;
	gst_element_set_state ((GstElement*) _tmp6_, GST_STATE_PLAYING);
	_tmp7_ = self->priv->loop;
	g_main_loop_run (_tmp7_);
	_tmp8_ = self->priv->bin;
	gst_element_set_state ((GstElement*) _tmp8_, 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;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_MEDIA_EXPORT_TYPE_JPEG_WRITER, RygelMediaExportJPEGWriter);
	_gst_object_unref0 (self->priv->bin);
	_gst_object_unref0 (self->priv->appsrc);
	_g_main_loop_unref0 (self->priv->loop);
	_gst_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;
}



