/* rygel-gst-sink.c generated by valac 0.30.0.35-2c9e8, the Vala compiler
 * generated from rygel-gst-sink.vala, do not modify */

/*
 * Copyright (C) 2011 Nokia Corporation.
 * Copyright (C) 2012 Intel Corporation.
 *
 * 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.1 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 library; 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/base/base.h>
#include <gio/gio.h>
#include <rygel-server.h>
#include <stdlib.h>
#include <string.h>
#include <gst/gst.h>


#define RYGEL_TYPE_GST_SINK (rygel_gst_sink_get_type ())
#define RYGEL_GST_SINK(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_GST_SINK, RygelGstSink))
#define RYGEL_GST_SINK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_GST_SINK, RygelGstSinkClass))
#define RYGEL_IS_GST_SINK(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_GST_SINK))
#define RYGEL_IS_GST_SINK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_GST_SINK))
#define RYGEL_GST_SINK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_GST_SINK, RygelGstSinkClass))

typedef struct _RygelGstSink RygelGstSink;
typedef struct _RygelGstSinkClass RygelGstSinkClass;
typedef struct _RygelGstSinkPrivate RygelGstSinkPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _gst_buffer_unref0(var) ((var == NULL) ? NULL : (var = (gst_buffer_unref (var), NULL)))
typedef struct _Block2Data Block2Data;
#define _gst_caps_unref0(var) ((var == NULL) ? NULL : (var = (gst_caps_unref (var), NULL)))

struct _RygelGstSink {
	GstBaseSink parent_instance;
	RygelGstSinkPrivate * priv;
	GCancellable* cancellable;
};

struct _RygelGstSinkClass {
	GstBaseSinkClass parent_class;
};

struct _RygelGstSinkPrivate {
	gint priority;
	gint64 bytes_sent;
	gint64 max_bytes;
	GMutex buffer_mutex;
	GCond buffer_condition;
	RygelDataSource* source;
	RygelHTTPSeekRequest* offsets;
	gboolean frozen;
};

struct _Block2Data {
	int _ref_count_;
	RygelGstSink* self;
	GstBuffer* buffer;
};


static gpointer rygel_gst_sink_parent_class = NULL;

GType rygel_gst_sink_get_type (void) G_GNUC_CONST;
#define RYGEL_GST_SINK_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RYGEL_TYPE_GST_SINK, RygelGstSinkPrivate))
enum  {
	RYGEL_GST_SINK_DUMMY_PROPERTY
};
#define RYGEL_GST_SINK_NAME "http-gst-sink"
#define RYGEL_GST_SINK_PAD_NAME "sink"
#define RYGEL_GST_SINK_MAX_BUFFERED_CHUNKS ((guint) 32)
#define RYGEL_GST_SINK_MIN_BUFFERED_CHUNKS ((guint) 4)
RygelGstSink* rygel_gst_sink_new (RygelDataSource* source, RygelHTTPSeekRequest* offsets);
RygelGstSink* rygel_gst_sink_construct (GType object_type, RygelDataSource* source, RygelHTTPSeekRequest* offsets);
static void rygel_gst_sink_on_cancelled (RygelGstSink* self);
static void _rygel_gst_sink_on_cancelled_g_cancellable_cancelled (GCancellable* _sender, gpointer self);
void rygel_gst_sink_freeze (RygelGstSink* self);
void rygel_gst_sink_thaw (RygelGstSink* self);
static GstFlowReturn rygel_gst_sink_real_render (GstBaseSink* base, GstBuffer* buffer);
static Block2Data* block2_data_ref (Block2Data* _data2_);
static void block2_data_unref (void * _userdata_);
static gboolean __lambda4_ (Block2Data* _data2_);
gboolean rygel_gst_sink_push_data (RygelGstSink* self, GstBuffer* buffer);
static gboolean ___lambda4__gsource_func (gpointer self);
static void rygel_gst_sink_finalize (GObject* obj);
static void _vala_clear_GMutex (GMutex * mutex);
static void _vala_clear_GRecMutex (GRecMutex * mutex);
static void _vala_clear_GRWLock (GRWLock * mutex);
static void _vala_clear_GCond (GCond * mutex);


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


static void _rygel_gst_sink_on_cancelled_g_cancellable_cancelled (GCancellable* _sender, gpointer self) {
	rygel_gst_sink_on_cancelled ((RygelGstSink*) self);
}


RygelGstSink* rygel_gst_sink_construct (GType object_type, RygelDataSource* source, RygelHTTPSeekRequest* offsets) {
	RygelGstSink * self = NULL;
	RygelDataSource* _tmp0_ = NULL;
	RygelHTTPSeekRequest* _tmp1_ = NULL;
	RygelHTTPSeekRequest* _tmp2_ = NULL;
	GCancellable* _tmp3_ = NULL;
	RygelHTTPSeekRequest* _tmp4_ = NULL;
	GCancellable* _tmp9_ = NULL;
	g_return_val_if_fail (source != NULL, NULL);
	self = (RygelGstSink*) g_object_new (object_type, NULL);
	self->priv->bytes_sent = (gint64) 0;
	self->priv->max_bytes = G_MAXINT64;
	_tmp0_ = source;
	self->priv->source = _tmp0_;
	_tmp1_ = offsets;
	_tmp2_ = _g_object_ref0 (_tmp1_);
	_g_object_unref0 (self->priv->offsets);
	self->priv->offsets = _tmp2_;
	_tmp3_ = g_cancellable_new ();
	_g_object_unref0 (self->cancellable);
	self->cancellable = _tmp3_;
	gst_base_sink_set_sync ((GstBaseSink*) self, FALSE);
	g_object_set ((GstObject*) self, "name", RYGEL_GST_SINK_NAME, NULL);
	self->priv->frozen = FALSE;
	_tmp4_ = self->priv->offsets;
	if (_tmp4_ != NULL) {
		RygelHTTPSeekRequest* _tmp5_ = NULL;
		_tmp5_ = self->priv->offsets;
		if (G_TYPE_CHECK_INSTANCE_TYPE (_tmp5_, RYGEL_TYPE_HTTP_BYTE_SEEK_REQUEST)) {
			RygelHTTPSeekRequest* _tmp6_ = NULL;
			gint64 _tmp7_ = 0LL;
			gint64 _tmp8_ = 0LL;
			_tmp6_ = self->priv->offsets;
			_tmp7_ = rygel_http_byte_seek_request_get_total_size (G_TYPE_CHECK_INSTANCE_TYPE (_tmp6_, RYGEL_TYPE_HTTP_BYTE_SEEK_REQUEST) ? ((RygelHTTPByteSeekRequest*) _tmp6_) : NULL);
			_tmp8_ = _tmp7_;
			self->priv->max_bytes = _tmp8_;
		}
	}
	_tmp9_ = self->cancellable;
	g_signal_connect_object (_tmp9_, "cancelled", (GCallback) _rygel_gst_sink_on_cancelled_g_cancellable_cancelled, self, 0);
	return self;
}


RygelGstSink* rygel_gst_sink_new (RygelDataSource* source, RygelHTTPSeekRequest* offsets) {
	return rygel_gst_sink_construct (RYGEL_TYPE_GST_SINK, source, offsets);
}


void rygel_gst_sink_freeze (RygelGstSink* self) {
	gboolean _tmp0_ = FALSE;
	g_return_if_fail (self != NULL);
	g_mutex_lock (&self->priv->buffer_mutex);
	_tmp0_ = self->priv->frozen;
	if (!_tmp0_) {
		self->priv->frozen = TRUE;
	}
	g_mutex_unlock (&self->priv->buffer_mutex);
}


void rygel_gst_sink_thaw (RygelGstSink* self) {
	gboolean _tmp0_ = FALSE;
	g_return_if_fail (self != NULL);
	g_mutex_lock (&self->priv->buffer_mutex);
	_tmp0_ = self->priv->frozen;
	if (_tmp0_) {
		self->priv->frozen = FALSE;
		g_cond_broadcast (&self->priv->buffer_condition);
	}
	g_mutex_unlock (&self->priv->buffer_mutex);
}


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


static Block2Data* block2_data_ref (Block2Data* _data2_) {
	g_atomic_int_inc (&_data2_->_ref_count_);
	return _data2_;
}


static void block2_data_unref (void * _userdata_) {
	Block2Data* _data2_;
	_data2_ = (Block2Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data2_->_ref_count_)) {
		RygelGstSink* self;
		self = _data2_->self;
		_gst_buffer_unref0 (_data2_->buffer);
		_g_object_unref0 (self);
		g_slice_free (Block2Data, _data2_);
	}
}


static gboolean __lambda4_ (Block2Data* _data2_) {
	RygelGstSink* self;
	gboolean result = FALSE;
	GstBuffer* _tmp0_ = NULL;
	gboolean _tmp1_ = FALSE;
	self = _data2_->self;
	_tmp0_ = _data2_->buffer;
	_tmp1_ = rygel_gst_sink_push_data (self, _tmp0_);
	result = _tmp1_;
	return result;
}


static gboolean ___lambda4__gsource_func (gpointer self) {
	gboolean result;
	result = __lambda4_ (self);
	return result;
}


static GstFlowReturn rygel_gst_sink_real_render (GstBaseSink* base, GstBuffer* buffer) {
	RygelGstSink * self;
	GstFlowReturn result = 0;
	Block2Data* _data2_;
	GstBuffer* _tmp0_ = NULL;
	GstBuffer* _tmp1_ = NULL;
	GCancellable* _tmp6_ = NULL;
	gboolean _tmp7_ = FALSE;
	gint _tmp8_ = 0;
	self = (RygelGstSink*) base;
	g_return_val_if_fail (buffer != NULL, 0);
	_data2_ = g_slice_new0 (Block2Data);
	_data2_->_ref_count_ = 1;
	_data2_->self = g_object_ref (self);
	_tmp0_ = buffer;
	_tmp1_ = _gst_buffer_ref0 (_tmp0_);
	_gst_buffer_unref0 (_data2_->buffer);
	_data2_->buffer = _tmp1_;
	g_mutex_lock (&self->priv->buffer_mutex);
	while (TRUE) {
		gboolean _tmp2_ = FALSE;
		GCancellable* _tmp3_ = NULL;
		gboolean _tmp4_ = FALSE;
		_tmp3_ = self->cancellable;
		_tmp4_ = g_cancellable_is_cancelled (_tmp3_);
		if (!_tmp4_) {
			gboolean _tmp5_ = FALSE;
			_tmp5_ = self->priv->frozen;
			_tmp2_ = _tmp5_;
		} else {
			_tmp2_ = FALSE;
		}
		if (!_tmp2_) {
			break;
		}
		g_cond_wait (&self->priv->buffer_condition, &self->priv->buffer_mutex);
	}
	g_mutex_unlock (&self->priv->buffer_mutex);
	_tmp6_ = self->cancellable;
	_tmp7_ = g_cancellable_is_cancelled (_tmp6_);
	if (_tmp7_) {
		result = GST_FLOW_OK;
		block2_data_unref (_data2_);
		_data2_ = NULL;
		return result;
	}
	_tmp8_ = self->priv->priority;
	g_idle_add_full (_tmp8_, ___lambda4__gsource_func, block2_data_ref (_data2_), block2_data_unref);
	result = GST_FLOW_OK;
	block2_data_unref (_data2_);
	_data2_ = NULL;
	return result;
}


gboolean rygel_gst_sink_push_data (RygelGstSink* self, GstBuffer* buffer) {
	gboolean result = FALSE;
	gint64 left = 0LL;
	gint64 _tmp0_ = 0LL;
	gint64 _tmp1_ = 0LL;
	gboolean _tmp2_ = FALSE;
	GCancellable* _tmp3_ = NULL;
	gboolean _tmp4_ = FALSE;
	gsize bufsize = 0UL;
	GstBuffer* _tmp6_ = NULL;
	gsize _tmp7_ = 0UL;
	gint64 to_send = 0LL;
	gsize _tmp8_ = 0UL;
	gint64 _tmp9_ = 0LL;
	gint64 _tmp10_ = 0LL;
	GstMapInfo info = {0};
	GstBuffer* _tmp11_ = NULL;
	GstMapInfo _tmp12_ = {0};
	guint8* tmp = NULL;
	GstMapInfo _tmp13_ = {0};
	guint8* _tmp14_ = NULL;
	gint _tmp14__length1 = 0;
	gint64 _tmp15_ = 0LL;
	gint tmp_length1 = 0;
	gint _tmp_size_ = 0;
	RygelDataSource* _tmp16_ = NULL;
	guint8* _tmp17_ = NULL;
	gint _tmp17__length1 = 0;
	gint64 _tmp18_ = 0LL;
	gint64 _tmp19_ = 0LL;
	GstBuffer* _tmp20_ = NULL;
	GstMapInfo _tmp21_ = {0};
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (buffer != NULL, FALSE);
	_tmp0_ = self->priv->max_bytes;
	_tmp1_ = self->priv->bytes_sent;
	left = _tmp0_ - _tmp1_;
	_tmp3_ = self->cancellable;
	_tmp4_ = g_cancellable_is_cancelled (_tmp3_);
	if (_tmp4_) {
		_tmp2_ = TRUE;
	} else {
		gint64 _tmp5_ = 0LL;
		_tmp5_ = left;
		_tmp2_ = _tmp5_ <= ((gint64) 0);
	}
	if (_tmp2_) {
		result = FALSE;
		return result;
	}
	_tmp6_ = buffer;
	_tmp7_ = gst_buffer_get_size (_tmp6_);
	bufsize = _tmp7_;
	_tmp8_ = bufsize;
	_tmp9_ = left;
	_tmp10_ = MIN ((gint64) _tmp8_, _tmp9_);
	to_send = _tmp10_;
	_tmp11_ = buffer;
	gst_buffer_map (_tmp11_, &_tmp12_, GST_MAP_READ);
	info = _tmp12_;
	_tmp13_ = info;
	_tmp14_ = _tmp13_.data;
	_tmp14__length1 = (gint) _tmp13_.size;
	_tmp15_ = to_send;
	tmp = _tmp14_ + 0;
	tmp_length1 = ((gint) _tmp15_) - 0;
	_tmp_size_ = tmp_length1;
	_tmp16_ = self->priv->source;
	_tmp17_ = tmp;
	_tmp17__length1 = tmp_length1;
	g_signal_emit_by_name (_tmp16_, "data-available", _tmp17_, _tmp17__length1);
	_tmp18_ = self->priv->bytes_sent;
	_tmp19_ = to_send;
	self->priv->bytes_sent = _tmp18_ + _tmp19_;
	_tmp20_ = buffer;
	_tmp21_ = info;
	gst_buffer_unmap (_tmp20_, &_tmp21_);
	result = FALSE;
	return result;
}


static void rygel_gst_sink_on_cancelled (RygelGstSink* self) {
	g_return_if_fail (self != NULL);
	g_mutex_lock (&self->priv->buffer_mutex);
	g_cond_broadcast (&self->priv->buffer_condition);
	g_mutex_unlock (&self->priv->buffer_mutex);
}


static void rygel_gst_sink_class_init (RygelGstSinkClass * klass) {
	GstCaps* caps = NULL;
	GstCaps* _tmp0_ = NULL;
	GstPadTemplate* template = NULL;
	GstCaps* _tmp1_ = NULL;
	GstPadTemplate* _tmp2_ = NULL;
	GstPadTemplate* _tmp3_ = NULL;
	GstPadTemplate* _tmp4_ = NULL;
	rygel_gst_sink_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (RygelGstSinkPrivate));
	((GstBaseSinkClass *) klass)->render = rygel_gst_sink_real_render;
	G_OBJECT_CLASS (klass)->finalize = rygel_gst_sink_finalize;
	_tmp0_ = gst_caps_new_any ();
	caps = _tmp0_;
	_tmp1_ = caps;
	_tmp2_ = gst_pad_template_new (RYGEL_GST_SINK_PAD_NAME, GST_PAD_SINK, GST_PAD_ALWAYS, _tmp1_);
	g_object_ref_sink (_tmp2_);
	template = _tmp2_;
	_tmp3_ = template;
	_tmp4_ = _g_object_ref0 (_tmp3_);
	gst_element_class_add_pad_template (GST_ELEMENT_CLASS (klass), _tmp4_);
	_g_object_unref0 (template);
	_gst_caps_unref0 (caps);
}


static void rygel_gst_sink_instance_init (RygelGstSink * self) {
	self->priv = RYGEL_GST_SINK_GET_PRIVATE (self);
	g_mutex_init (&self->priv->buffer_mutex);
	g_cond_init (&self->priv->buffer_condition);
}


static void rygel_gst_sink_finalize (GObject* obj) {
	RygelGstSink * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TYPE_GST_SINK, RygelGstSink);
	_g_object_unref0 (self->cancellable);
	_vala_clear_GMutex (&self->priv->buffer_mutex);
	_vala_clear_GCond (&self->priv->buffer_condition);
	_g_object_unref0 (self->priv->offsets);
	G_OBJECT_CLASS (rygel_gst_sink_parent_class)->finalize (obj);
}


GType rygel_gst_sink_get_type (void) {
	static volatile gsize rygel_gst_sink_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_gst_sink_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RygelGstSinkClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_gst_sink_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelGstSink), 0, (GInstanceInitFunc) rygel_gst_sink_instance_init, NULL };
		GType rygel_gst_sink_type_id;
		rygel_gst_sink_type_id = g_type_register_static (gst_base_sink_get_type (), "RygelGstSink", &g_define_type_info, 0);
		g_once_init_leave (&rygel_gst_sink_type_id__volatile, rygel_gst_sink_type_id);
	}
	return rygel_gst_sink_type_id__volatile;
}


static void _vala_clear_GMutex (GMutex * mutex) {
	GMutex zero_mutex = { 0 };
	if (memcmp (mutex, &zero_mutex, sizeof (GMutex))) {
		g_mutex_clear (mutex);
		memset (mutex, 0, sizeof (GMutex));
	}
}


static void _vala_clear_GRecMutex (GRecMutex * mutex) {
	GRecMutex zero_mutex = { 0 };
	if (memcmp (mutex, &zero_mutex, sizeof (GRecMutex))) {
		g_rec_mutex_clear (mutex);
		memset (mutex, 0, sizeof (GRecMutex));
	}
}


static void _vala_clear_GRWLock (GRWLock * mutex) {
	GRWLock zero_mutex = { 0 };
	if (memcmp (mutex, &zero_mutex, sizeof (GRWLock))) {
		g_rw_lock_clear (mutex);
		memset (mutex, 0, sizeof (GRWLock));
	}
}


static void _vala_clear_GCond (GCond * mutex) {
	GCond zero_mutex = { 0 };
	if (memcmp (mutex, &zero_mutex, sizeof (GCond))) {
		g_cond_clear (mutex);
		memset (mutex, 0, sizeof (GCond));
	}
}



