/* ffmpeg-screen-recorder.c generated by valac 0.56.17, the Vala compiler
 * generated from ffmpeg-screen-recorder.vala, do not modify */

/*
Peek Copyright (c) 2015-2018 by Philipp Wolfer <ph.wolfer@gmail.com>

This file is part of Peek.

This software is licensed under the GNU General Public License
(version 3 or later). See the LICENSE file in this distribution.
*/

#include "../../application.h"
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include <stdio.h>
#include <glib-object.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	PEEK_RECORDING_FFMPEG_SCREEN_RECORDER_0_PROPERTY,
	PEEK_RECORDING_FFMPEG_SCREEN_RECORDER_NUM_PROPERTIES
};
static GParamSpec* peek_recording_ffmpeg_screen_recorder_properties[PEEK_RECORDING_FFMPEG_SCREEN_RECORDER_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_array_unref0(var) ((var == NULL) ? NULL : (var = (g_array_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

static gpointer peek_recording_ffmpeg_screen_recorder_parent_class = NULL;

static void peek_recording_ffmpeg_screen_recorder_real_start_recording (PeekRecordingBaseScreenRecorder* base,
                                                                 PeekRecordingRecordingArea* area,
                                                                 GError** error);
static void _g_free0_ (gpointer var);
static void _vala_gchar_free_function_content_of (gpointer data);
static void peek_recording_ffmpeg_screen_recorder_real_stop_recording (PeekRecordingBaseScreenRecorder* base);
static void peek_recording_ffmpeg_screen_recorder_finalize (GObject * obj);
static GType peek_recording_ffmpeg_screen_recorder_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static void
_g_free0_ (gpointer var)
{
	var = (g_free (var), NULL);
}

static void
_vala_gchar_free_function_content_of (gpointer data)
{
	gchar* self;
	self = *((gchar**) data);
	_g_free0_ (self);
}

static void
peek_recording_ffmpeg_screen_recorder_real_start_recording (PeekRecordingBaseScreenRecorder* base,
                                                            PeekRecordingRecordingArea* area,
                                                            GError** error)
{
	PeekRecordingFfmpegScreenRecorder * self;
	GError* _inner_error0_ = NULL;
	self = (PeekRecordingFfmpegScreenRecorder*) base;
	g_return_if_fail (area != NULL);
	{
		gchar* display = NULL;
		const gchar* _tmp0_;
		gchar* _tmp1_;
		const gchar* _tmp2_;
		GArray* args = NULL;
		GArray* _tmp4_;
		GArray* _tmp5_;
		gchar* _tmp6_;
		GArray* _tmp7_;
		gchar* _tmp8_;
		GArray* _tmp9_;
		gchar* _tmp10_;
		GArray* _tmp11_;
		gchar* _tmp12_;
		GArray* _tmp13_;
		gchar* _tmp14_;
		GArray* _tmp15_;
		gchar* _tmp16_;
		GArray* _tmp17_;
		PeekRecordingRecordingConfig* _tmp18_;
		PeekRecordingRecordingConfig* _tmp19_;
		gint _tmp20_;
		gint _tmp21_;
		gchar* _tmp22_;
		GArray* _tmp23_;
		gchar* _tmp24_;
		GArray* _tmp25_;
		PeekRecordingRecordingArea _tmp26_;
		gchar* _tmp27_;
		gchar* _tmp28_;
		gchar* _tmp29_;
		gchar* _tmp30_;
		PeekRecordingRecordingArea _tmp31_;
		gchar* _tmp32_;
		gchar* _tmp33_;
		gchar* _tmp34_;
		PeekRecordingRecordingConfig* _tmp35_;
		PeekRecordingRecordingConfig* _tmp36_;
		gboolean _tmp37_;
		gboolean _tmp38_;
		GArray* _tmp43_;
		gchar* _tmp44_;
		GArray* _tmp45_;
		const gchar* _tmp46_;
		gchar* _tmp47_;
		gchar* _tmp48_;
		PeekRecordingRecordingArea _tmp49_;
		gchar* _tmp50_;
		gchar* _tmp51_;
		gchar* _tmp52_;
		gchar* _tmp53_;
		gchar* _tmp54_;
		gchar* _tmp55_;
		PeekRecordingRecordingArea _tmp56_;
		gchar* _tmp57_;
		gchar* _tmp58_;
		gchar* _tmp59_;
		GArray* _tmp60_;
		gchar* _tmp61_;
		gchar* filter = NULL;
		PeekRecordingRecordingConfig* _tmp62_;
		PeekRecordingRecordingConfig* _tmp63_;
		gint _tmp64_;
		gint _tmp65_;
		gchar* _tmp66_;
		gchar* _tmp67_;
		gchar* _tmp68_;
		gchar* _tmp69_;
		gchar* _tmp70_;
		gchar* _tmp71_;
		PeekRecordingRecordingConfig* _tmp72_;
		PeekRecordingRecordingConfig* _tmp73_;
		PeekOutputFormat _tmp74_;
		PeekOutputFormat _tmp75_;
		GArray* _tmp78_;
		const gchar* _tmp79_;
		gchar* _tmp80_;
		gchar* extension = NULL;
		GArray* _tmp81_;
		PeekRecordingRecordingConfig* _tmp82_;
		PeekRecordingRecordingConfig* _tmp83_;
		gchar* _tmp84_ = NULL;
		gchar* _tmp85_ = NULL;
		gchar* _tmp86_;
		gchar* _tmp87_;
		GArray* _tmp88_;
		gchar* _tmp89_;
		GArray* _tmp90_;
		const gchar* _tmp91_;
		gchar* _tmp92_;
		GArray* _tmp93_;
		gpointer* _tmp94_;
		gint _tmp94__length1;
		_tmp0_ = g_getenv ("DISPLAY");
		_tmp1_ = g_strdup (_tmp0_);
		display = _tmp1_;
		_tmp2_ = display;
		if (_tmp2_ == NULL) {
			gchar* _tmp3_;
			_tmp3_ = g_strdup (":0");
			_g_free0 (display);
			display = _tmp3_;
		}
		_tmp4_ = g_array_new (TRUE, TRUE, sizeof (gchar*));
		g_array_set_clear_func (_tmp4_, (GDestroyNotify) _vala_gchar_free_function_content_of);
		args = _tmp4_;
		_tmp5_ = args;
		_tmp6_ = g_strdup ("ffmpeg");
		g_array_append_val (_tmp5_, _tmp6_);
		_tmp7_ = args;
		_tmp8_ = g_strdup ("-f");
		g_array_append_val (_tmp7_, _tmp8_);
		_tmp9_ = args;
		_tmp10_ = g_strdup ("x11grab");
		g_array_append_val (_tmp9_, _tmp10_);
		_tmp11_ = args;
		_tmp12_ = g_strdup ("-show_region");
		g_array_append_val (_tmp11_, _tmp12_);
		_tmp13_ = args;
		_tmp14_ = g_strdup ("0");
		g_array_append_val (_tmp13_, _tmp14_);
		_tmp15_ = args;
		_tmp16_ = g_strdup ("-framerate");
		g_array_append_val (_tmp15_, _tmp16_);
		_tmp17_ = args;
		_tmp18_ = peek_recording_screen_recorder_get_config ((PeekRecordingScreenRecorder*) self);
		_tmp19_ = _tmp18_;
		_tmp20_ = peek_recording_recording_config_get_framerate (_tmp19_);
		_tmp21_ = _tmp20_;
		_tmp22_ = g_strdup_printf ("%i", MAX (_tmp21_, 6));
		g_array_append_val (_tmp17_, _tmp22_);
		_tmp23_ = args;
		_tmp24_ = g_strdup ("-video_size");
		g_array_append_val (_tmp23_, _tmp24_);
		_tmp25_ = args;
		_tmp26_ = *area;
		_tmp27_ = g_strdup_printf ("%i", _tmp26_.width);
		_tmp28_ = _tmp27_;
		_tmp29_ = g_strconcat (_tmp28_, "x", NULL);
		_tmp30_ = _tmp29_;
		_tmp31_ = *area;
		_tmp32_ = g_strdup_printf ("%i", _tmp31_.height);
		_tmp33_ = _tmp32_;
		_tmp34_ = g_strconcat (_tmp30_, _tmp33_, NULL);
		g_array_append_val (_tmp25_, _tmp34_);
		_g_free0 (_tmp33_);
		_g_free0 (_tmp30_);
		_g_free0 (_tmp28_);
		_tmp35_ = peek_recording_screen_recorder_get_config ((PeekRecordingScreenRecorder*) self);
		_tmp36_ = _tmp35_;
		_tmp37_ = peek_recording_recording_config_get_capture_mouse (_tmp36_);
		_tmp38_ = _tmp37_;
		if (!_tmp38_) {
			GArray* _tmp39_;
			gchar* _tmp40_;
			GArray* _tmp41_;
			gchar* _tmp42_;
			_tmp39_ = args;
			_tmp40_ = g_strdup ("-draw_mouse");
			g_array_append_val (_tmp39_, _tmp40_);
			_tmp41_ = args;
			_tmp42_ = g_strdup ("0");
			g_array_append_val (_tmp41_, _tmp42_);
		}
		_tmp43_ = args;
		_tmp44_ = g_strdup ("-i");
		g_array_append_val (_tmp43_, _tmp44_);
		_tmp45_ = args;
		_tmp46_ = display;
		_tmp47_ = g_strconcat (_tmp46_, "+", NULL);
		_tmp48_ = _tmp47_;
		_tmp49_ = *area;
		_tmp50_ = g_strdup_printf ("%i", _tmp49_.left);
		_tmp51_ = _tmp50_;
		_tmp52_ = g_strconcat (_tmp48_, _tmp51_, NULL);
		_tmp53_ = _tmp52_;
		_tmp54_ = g_strconcat (_tmp53_, ",", NULL);
		_tmp55_ = _tmp54_;
		_tmp56_ = *area;
		_tmp57_ = g_strdup_printf ("%i", _tmp56_.top);
		_tmp58_ = _tmp57_;
		_tmp59_ = g_strconcat (_tmp55_, _tmp58_, NULL);
		g_array_append_val (_tmp45_, _tmp59_);
		_g_free0 (_tmp58_);
		_g_free0 (_tmp55_);
		_g_free0 (_tmp53_);
		_g_free0 (_tmp51_);
		_g_free0 (_tmp48_);
		_tmp60_ = args;
		_tmp61_ = g_strdup ("-filter:v");
		g_array_append_val (_tmp60_, _tmp61_);
		_tmp62_ = peek_recording_screen_recorder_get_config ((PeekRecordingScreenRecorder*) self);
		_tmp63_ = _tmp62_;
		_tmp64_ = peek_recording_recording_config_get_downsample (_tmp63_);
		_tmp65_ = _tmp64_;
		_tmp66_ = g_strdup_printf ("%i", _tmp65_);
		_tmp67_ = _tmp66_;
		_tmp68_ = g_strconcat ("scale=iw/", _tmp67_, NULL);
		_tmp69_ = _tmp68_;
		_tmp70_ = g_strconcat (_tmp69_, ":-1", NULL);
		_tmp71_ = _tmp70_;
		_g_free0 (_tmp69_);
		_g_free0 (_tmp67_);
		filter = _tmp71_;
		_tmp72_ = peek_recording_screen_recorder_get_config ((PeekRecordingScreenRecorder*) self);
		_tmp73_ = _tmp72_;
		_tmp74_ = peek_recording_recording_config_get_output_format (_tmp73_);
		_tmp75_ = _tmp74_;
		if (_tmp75_ == PEEK_OUTPUT_FORMAT_MP4) {
			const gchar* _tmp76_;
			gchar* _tmp77_;
			_tmp76_ = filter;
			_tmp77_ = g_strconcat (_tmp76_, ", crop=iw-mod(iw\\,2):ih-mod(ih\\,2)", NULL);
			_g_free0 (filter);
			filter = _tmp77_;
		}
		_tmp78_ = args;
		_tmp79_ = filter;
		_tmp80_ = g_strdup (_tmp79_);
		g_array_append_val (_tmp78_, _tmp80_);
		_tmp81_ = args;
		_tmp82_ = peek_recording_screen_recorder_get_config ((PeekRecordingScreenRecorder*) self);
		_tmp83_ = _tmp82_;
		peek_recording_ffmpeg_add_output_parameters (_tmp81_, _tmp83_, &_tmp84_);
		_g_free0 (extension);
		extension = _tmp84_;
		_tmp86_ = peek_utils_create_temp_file (extension, &_inner_error0_);
		_tmp85_ = _tmp86_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_free0 (extension);
			_g_free0 (filter);
			_g_array_unref0 (args);
			_g_free0 (display);
			if (_inner_error0_->domain == G_FILE_ERROR) {
				goto __catch0_g_file_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
		_tmp87_ = _tmp85_;
		_tmp85_ = NULL;
		_g_free0 (((PeekRecordingBaseScreenRecorder*) self)->temp_file);
		((PeekRecordingBaseScreenRecorder*) self)->temp_file = _tmp87_;
		_tmp88_ = args;
		_tmp89_ = g_strdup ("-y");
		g_array_append_val (_tmp88_, _tmp89_);
		_tmp90_ = args;
		_tmp91_ = ((PeekRecordingBaseScreenRecorder*) self)->temp_file;
		_tmp92_ = g_strdup (_tmp91_);
		g_array_append_val (_tmp90_, _tmp92_);
		_tmp93_ = args;
		_tmp94_ = _tmp93_->data;
		_tmp94__length1 = _tmp93_->len;
		peek_recording_cli_screen_recorder_spawn_record_command ((PeekRecordingCliScreenRecorder*) self, _tmp94_, (gint) _tmp94__length1, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_free0 (_tmp85_);
			_g_free0 (extension);
			_g_free0 (filter);
			_g_array_unref0 (args);
			_g_free0 (display);
			if (_inner_error0_->domain == G_FILE_ERROR) {
				goto __catch0_g_file_error;
			}
			goto __finally0;
		}
		_g_free0 (_tmp85_);
		_g_free0 (extension);
		_g_free0 (filter);
		_g_array_unref0 (args);
		_g_free0 (display);
	}
	goto __finally0;
	__catch0_g_file_error:
	{
		GError* e = NULL;
		GError* _tmp95_;
		const gchar* _tmp96_;
		GError* _tmp97_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp95_ = e;
		_tmp96_ = _tmp95_->message;
		_tmp97_ = g_error_new_literal (PEEK_RECORDING_ERROR, PEEK_RECORDING_ERROR_INITIALIZING_RECORDING_FAILED, _tmp96_);
		_inner_error0_ = _tmp97_;
		_g_error_free0 (e);
		goto __finally0;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		if (_inner_error0_->domain == PEEK_RECORDING_ERROR) {
			g_propagate_error (error, _inner_error0_);
			return;
		} else {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
	}
}

gboolean
peek_recording_ffmpeg_screen_recorder_is_available (GError** error)
{
	gboolean result;
	result = peek_utils_check_for_executable ("ffmpeg");
	return result;
}

static void
peek_recording_ffmpeg_screen_recorder_real_stop_recording (PeekRecordingBaseScreenRecorder* base)
{
	PeekRecordingFfmpegScreenRecorder * self;
	gboolean _tmp0_ = FALSE;
	GSubprocess* _tmp1_;
	GError* _inner_error0_ = NULL;
	self = (PeekRecordingFfmpegScreenRecorder*) base;
	_tmp1_ = ((PeekRecordingCliScreenRecorder*) self)->subprocess;
	if (_tmp1_ != NULL) {
		GOutputStream* _tmp2_;
		_tmp2_ = ((PeekRecordingCliScreenRecorder*) self)->input;
		_tmp0_ = _tmp2_ != NULL;
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		{
			guint8* command = NULL;
			guint8* _tmp3_;
			gint command_length1;
			gint _command_size_;
			GOutputStream* _tmp4_;
			guint8* _tmp5_;
			gint _tmp5__length1;
			GOutputStream* _tmp6_;
			_tmp3_ = g_new0 (guint8, 1);
			_tmp3_[0] = (guint8) 'q';
			command = _tmp3_;
			command_length1 = 1;
			_command_size_ = command_length1;
			_tmp4_ = ((PeekRecordingCliScreenRecorder*) self)->input;
			_tmp5_ = command;
			_tmp5__length1 = command_length1;
			g_output_stream_write (_tmp4_, _tmp5_, (gsize) _tmp5__length1, NULL, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				command = (g_free (command), NULL);
				goto __catch0_g_error;
			}
			_tmp6_ = ((PeekRecordingCliScreenRecorder*) self)->input;
			g_output_stream_flush (_tmp6_, NULL, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				command = (g_free (command), NULL);
				goto __catch0_g_error;
			}
			command = (g_free (command), NULL);
		}
		goto __finally0;
		__catch0_g_error:
		{
			GError* e = NULL;
			FILE* _tmp7_;
			GError* _tmp8_;
			const gchar* _tmp9_;
			GError* _tmp10_;
			const gchar* _tmp11_;
			GError* _tmp12_;
			GError* _tmp13_;
			e = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp7_ = stderr;
			_tmp8_ = e;
			_tmp9_ = _tmp8_->message;
			fprintf (_tmp7_, "Error: %s\n", _tmp9_);
			_tmp10_ = e;
			_tmp11_ = _tmp10_->message;
			_tmp12_ = g_error_new_literal (PEEK_RECORDING_ERROR, PEEK_RECORDING_ERROR_RECORDING_ABORTED, _tmp11_);
			_tmp13_ = _tmp12_;
			g_signal_emit_by_name ((PeekRecordingScreenRecorder*) self, "recording-aborted", _tmp13_);
			_g_error_free0 (_tmp13_);
			_g_error_free0 (e);
		}
		__finally0:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
	}
}

PeekRecordingFfmpegScreenRecorder*
peek_recording_ffmpeg_screen_recorder_construct (GType object_type)
{
	PeekRecordingFfmpegScreenRecorder * self = NULL;
	self = (PeekRecordingFfmpegScreenRecorder*) peek_recording_cli_screen_recorder_construct (object_type);
	return self;
}

PeekRecordingFfmpegScreenRecorder*
peek_recording_ffmpeg_screen_recorder_new (void)
{
	return peek_recording_ffmpeg_screen_recorder_construct (PEEK_RECORDING_TYPE_FFMPEG_SCREEN_RECORDER);
}

static void
peek_recording_ffmpeg_screen_recorder_class_init (PeekRecordingFfmpegScreenRecorderClass * klass,
                                                  gpointer klass_data)
{
	peek_recording_ffmpeg_screen_recorder_parent_class = g_type_class_peek_parent (klass);
	((PeekRecordingBaseScreenRecorderClass *) klass)->start_recording = (void (*) (PeekRecordingBaseScreenRecorder*, PeekRecordingRecordingArea*, GError**)) peek_recording_ffmpeg_screen_recorder_real_start_recording;
	((PeekRecordingBaseScreenRecorderClass *) klass)->stop_recording = (void (*) (PeekRecordingBaseScreenRecorder*)) peek_recording_ffmpeg_screen_recorder_real_stop_recording;
	G_OBJECT_CLASS (klass)->finalize = peek_recording_ffmpeg_screen_recorder_finalize;
}

static void
peek_recording_ffmpeg_screen_recorder_instance_init (PeekRecordingFfmpegScreenRecorder * self,
                                                     gpointer klass)
{
}

static void
peek_recording_ffmpeg_screen_recorder_finalize (GObject * obj)
{
	PeekRecordingFfmpegScreenRecorder * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PEEK_RECORDING_TYPE_FFMPEG_SCREEN_RECORDER, PeekRecordingFfmpegScreenRecorder);
	peek_recording_screen_recorder_cancel ((PeekRecordingScreenRecorder*) self);
	G_OBJECT_CLASS (peek_recording_ffmpeg_screen_recorder_parent_class)->finalize (obj);
}

static GType
peek_recording_ffmpeg_screen_recorder_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (PeekRecordingFfmpegScreenRecorderClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) peek_recording_ffmpeg_screen_recorder_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (PeekRecordingFfmpegScreenRecorder), 0, (GInstanceInitFunc) peek_recording_ffmpeg_screen_recorder_instance_init, NULL };
	GType peek_recording_ffmpeg_screen_recorder_type_id;
	peek_recording_ffmpeg_screen_recorder_type_id = g_type_register_static (PEEK_RECORDING_TYPE_CLI_SCREEN_RECORDER, "PeekRecordingFfmpegScreenRecorder", &g_define_type_info, 0);
	return peek_recording_ffmpeg_screen_recorder_type_id;
}

GType
peek_recording_ffmpeg_screen_recorder_get_type (void)
{
	static volatile gsize peek_recording_ffmpeg_screen_recorder_type_id__once = 0;
	if (g_once_init_enter (&peek_recording_ffmpeg_screen_recorder_type_id__once)) {
		GType peek_recording_ffmpeg_screen_recorder_type_id;
		peek_recording_ffmpeg_screen_recorder_type_id = peek_recording_ffmpeg_screen_recorder_get_type_once ();
		g_once_init_leave (&peek_recording_ffmpeg_screen_recorder_type_id__once, peek_recording_ffmpeg_screen_recorder_type_id);
	}
	return peek_recording_ffmpeg_screen_recorder_type_id__once;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

