/* rygel-media-export-jpeg-writer.c generated by valac 0.20.1.2-ca5c2, 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/gstappsink.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 _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 __lambda4_ (RygelMediaExportJPEGWriter* self);
static void ___lambda4__gst_bus_message (GstBus* _sender, GstMessage* message, gpointer self);
static void __lambda5_ (RygelMediaExportJPEGWriter* self);
static void ___lambda5__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 __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);
}


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


static void ___lambda5__gst_bus_message (GstBus* _sender, GstMessage* message, gpointer self) {
	__lambda5_ (self);
}


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


void rygel_media_export_jpeg_writer_write (RygelMediaExportJPEGWriter* self, GstBuffer* buffer, GFile* file) {
	GstFlowReturn flow = 0;
	GstElement* _tmp0_;
	GFile* _tmp1_;
	GstAppSrc* _tmp2_;
	GstBuffer* _tmp3_;
	GstAppSrc* _tmp4_;
	GstBin* _tmp5_;
	GMainLoop* _tmp6_;
	GstBin* _tmp7_;
	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;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_MEDIA_EXPORT_TYPE_JPEG_WRITER, RygelMediaExportJPEGWriter);
	_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;
}



