# HG changeset patch
# User rosfran
# Date 1162839637 0
# Node ID dc4814fe47239244b6ad3bf4bf3d2783cf2478f9
# Parent  f926338c6952e514b466b66bd15b111bf90d8599
[svn r66] Refactoring some remoce_encoder functions, which actually needs to be just recorder; added channel-related recorder functions.

diff -r f926338c6952 -r dc4814fe4723 gmyth/src/Makefile.am
--- a/gmyth/src/Makefile.am	Mon Nov 06 18:58:26 2006 +0000
+++ b/gmyth/src/Makefile.am	Mon Nov 06 19:00:37 2006 +0000
@@ -6,7 +6,7 @@
 	gmyth_common.c				\
 	gmyth_context.c				\
 	gmyth_epg.c 				\
-	gmyth_remote_encoder.c		\
+	gmyth_recorder.c		\
 	gmyth_remote_util.c			\
 	gmyth_settings.c			\
 	gmyth_tvchain.c				\
@@ -46,7 +46,7 @@
 	gmyth_common.h	 			\
 	gmyth_context.h 			\
 	gmyth_epg.h 				\
-	gmyth_remote_encoder.h 		\
+	gmyth_recorder.h 		\
 	gmyth_scheduler.h 			\
 	gmyth_settings.h 			\
 	gmyth_tvchain.h 			\
diff -r f926338c6952 -r dc4814fe4723 gmyth/src/gmyth_file_transfer.c
--- a/gmyth/src/gmyth_file_transfer.c	Mon Nov 06 18:58:26 2006 +0000
+++ b/gmyth/src/gmyth_file_transfer.c	Mon Nov 06 19:00:37 2006 +0000
@@ -845,7 +845,7 @@
 
   //g_io_channel_flush( io_channel, NULL );
 
-  g_static_mutex_lock( &mutex );
+  //g_static_mutex_lock( &mutex );
 
   io_cond = g_io_channel_get_buffer_condition( io_channel );
 
@@ -990,7 +990,7 @@
   }
 
 cleanup:
-  g_static_mutex_unlock (&mutex);
+  //g_static_mutex_unlock (&mutex);
   g_io_channel_flush( io_channel_control, NULL );
 
   if ( trash != NULL )
diff -r f926338c6952 -r dc4814fe4723 gmyth/src/gmyth_livetv.c
--- a/gmyth/src/gmyth_livetv.c	Mon Nov 06 18:58:26 2006 +0000
+++ b/gmyth/src/gmyth_livetv.c	Mon Nov 06 19:00:37 2006 +0000
@@ -34,7 +34,7 @@
 	livetv->backend_port = 0;
 	livetv->local_hostname = NULL;
 
-	livetv->remote_encoder = NULL;
+	livetv->recorder = NULL;
 	livetv->tvchain = NULL;
 	livetv->proginfo = NULL;
 }
@@ -55,9 +55,9 @@
 
 	g_debug ("[%s] Finalizing livetv", __FUNCTION__);
 
-	if ( livetv->remote_encoder != NULL ) {
-		g_object_unref (livetv->remote_encoder);
-		livetv->remote_encoder = NULL;
+	if ( livetv->recorder != NULL ) {
+		g_object_unref (livetv->recorder);
+		livetv->recorder = NULL;
 	}
 
 	if ( livetv->tvchain != NULL ) {
@@ -107,10 +107,10 @@
 		goto error;
 	}
 
-	// Gets the remote encoder num
-	livetv->remote_encoder = remote_request_next_free_recorder (-1);
+	// Gets the recorder num
+	livetv->recorder = remote_request_next_free_recorder (-1);
 
-	if ( livetv->remote_encoder == NULL ) {
+	if ( livetv->recorder == NULL ) {
 		g_warning ("[%s] None remote encoder available", __FUNCTION__);
 		res = FALSE;
 		goto error;
@@ -126,14 +126,14 @@
 	}
 
 	// Init remote encoder. Opens its control socket.
-	res = gmyth_remote_encoder_setup(livetv->remote_encoder);
+	res = gmyth_recorder_setup(livetv->recorder);
 	if ( !res ) {
 		g_warning ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
 		res = FALSE;
 		goto error;
 	}
 	// Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly)
-	res = gmyth_remote_encoder_spawntv ( livetv->remote_encoder,
+	res = gmyth_recorder_spawntv ( livetv->recorder,
 			gmyth_tvchain_get_id(livetv->tvchain) );
 	if (!res) {
 		g_warning ("[%s] Fail while spawn tv\n", __FUNCTION__);
@@ -173,9 +173,9 @@
 		res = FALSE;
 	}
 
-	if ( livetv->remote_encoder != NULL ) {
-		g_object_unref (livetv->remote_encoder);
-		livetv->remote_encoder = NULL;
+	if ( livetv->recorder != NULL ) {
+		g_object_unref (livetv->recorder);
+		livetv->recorder = NULL;
 	}
 
 	if ( livetv->tvchain != NULL ) {
@@ -229,9 +229,9 @@
 		res = FALSE;
 	}
 
-	if ( livetv->remote_encoder != NULL ) {
-		g_object_unref (livetv->remote_encoder);
-		livetv->remote_encoder = NULL;
+	if ( livetv->recorder != NULL ) {
+		g_object_unref (livetv->recorder);
+		livetv->recorder = NULL;
 	}
 
 	if ( livetv->tvchain != NULL ) {
@@ -255,7 +255,7 @@
 	g_debug ("[%s] Stopping the LiveTV...\n", __FUNCTION__);
 
 	if (livetv->is_livetv) {
-		if (!gmyth_remote_encoder_stop_livetv (livetv->remote_encoder)) {
+		if (!gmyth_recorder_stop_livetv (livetv->recorder)) {
 			g_warning ("[%s] Error while stoping remote encoder", __FUNCTION__);	
 		}
 	}
diff -r f926338c6952 -r dc4814fe4723 gmyth/src/gmyth_livetv.h
--- a/gmyth/src/gmyth_livetv.h	Mon Nov 06 18:58:26 2006 +0000
+++ b/gmyth/src/gmyth_livetv.h	Mon Nov 06 19:00:37 2006 +0000
@@ -3,7 +3,7 @@
 
 #include <glib-object.h>
 
-#include "gmyth_remote_encoder.h"
+#include "gmyth_recorder.h"
 #include "gmyth_tvchain.h"
 #include "gmyth_common.h"
 
@@ -37,7 +37,7 @@
 	gint backend_port;
 	GString *local_hostname;
 
-	GMythRemoteEncoder *remote_encoder;
+	GMythRecorder *recorder;
 	GMythTVChain *tvchain;
 	GMythProgramInfo *proginfo;
 
diff -r f926338c6952 -r dc4814fe4723 gmyth/src/gmyth_recorder.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gmyth/src/gmyth_recorder.c	Mon Nov 06 19:00:37 2006 +0000
@@ -0,0 +1,297 @@
+/**
+ * GMyth Library
+ *
+ * @file gmyth/gmyth_remote_encoder.c
+ * 
+ * @brief <p> GMythRecorder class defines functions for playing live tv.
+ *
+ * The remote encoder is used by gmyth_tvplayer to setup livetv. 
+ *
+ * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
+ * @author Hallyson Luiz de Morais Melo <hallyson.melo@indt.org.br>
+ *
+ *//*
+ * 
+ * This program 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.
+ *
+ * This program 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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "gmyth_recorder.h"
+
+#include <assert.h>
+
+#include "gmyth_stringlist.h"
+
+static void gmyth_recorder_class_init          (GMythRecorderClass *klass);
+static void gmyth_recorder_init                (GMythRecorder *object);
+
+static void gmyth_recorder_dispose  (GObject *object);
+static void gmyth_recorder_finalize (GObject *object);
+
+G_DEFINE_TYPE(GMythRecorder, gmyth_recorder, G_TYPE_OBJECT)
+    
+static void
+gmyth_recorder_class_init (GMythRecorderClass *klass)
+{
+    GObjectClass *gobject_class;
+
+    gobject_class = (GObjectClass *) klass;
+	
+    gobject_class->dispose  = gmyth_recorder_dispose;
+    gobject_class->finalize = gmyth_recorder_finalize;	
+}
+
+static void
+gmyth_recorder_init (GMythRecorder *gmyth_remote_encoder)
+{
+}
+
+static void
+gmyth_recorder_dispose  (GObject *object)
+{
+    // GMythRecorder *gmyth_remote_encoder = GMYTH_RECORDER(object);
+    
+	G_OBJECT_CLASS (gmyth_recorder_parent_class)->dispose (object);
+}
+
+
+static void
+gmyth_recorder_finalize (GObject *object)
+{
+	g_signal_handlers_destroy (object);
+
+    GMythRecorder *recorder = GMYTH_RECORDER(object);
+
+	g_debug ("[%s] Closing control socket", __FUNCTION__);
+	gmyth_socket_close_connection(recorder->myth_socket);
+	g_object_unref (recorder->myth_socket);
+    
+    G_OBJECT_CLASS (gmyth_recorder_parent_class)->finalize (object);
+}
+
+/** Creates a new instance of GMythRecorder.
+ * 
+ * @return a new instance of GMythRecorder.
+ */
+GMythRecorder*
+gmyth_recorder_new (int num, GString *hostname, gshort port)
+{
+	GMythRecorder *encoder = GMYTH_RECORDER ( g_object_new (
+			GMYTH_RECORDER_TYPE, FALSE ));
+			
+	encoder->recorder_num = num;
+	encoder->hostname = g_string_new (hostname->str);
+	encoder->port = port;
+	
+	return encoder;
+}
+
+/** Configures the remote encoder instance connecting it to Mythtv backend.
+ * 
+ * @param recorder the GMythRecorder instance.
+ * @return TRUE if successfull, FALSE if any error happens.
+ */
+gboolean
+gmyth_recorder_setup (GMythRecorder *recorder)
+{
+	assert (recorder);
+	g_debug ("[%s] Creating socket and connecting to backend", __FUNCTION__);
+
+	if (recorder->myth_socket == NULL) {
+		
+		recorder->myth_socket = gmyth_socket_new ();
+		
+		if (!gmyth_socket_connect_to_backend (recorder->myth_socket, recorder->hostname->str, 
+					recorder->port, TRUE) ) {
+			g_warning ("GMythRemoteEncoder: Connection to backend failed");	
+			return FALSE;
+		}
+
+	} else {
+		g_warning("Remote encoder socket already created\n");
+	}
+
+	return TRUE;
+}
+
+/** Sends the SPAWN_LIVETV command through Mythtv protocol. This command
+ * requests the backend to start capturing TV content.
+ * 
+ * @param recorder The GMythRecorder instance.
+ * @param tvchain_id The tvchain unique id.
+ * @return true if success, false if any error happens.
+ */
+gboolean
+gmyth_recorder_spawntv (GMythRecorder *recorder, GString *tvchain_id)
+{
+	GMythStringList *str_list;
+	GString *tmp_str;
+	
+	g_debug ("[%s] Spawntv with tvchain_id = %s", __FUNCTION__, tvchain_id->str);
+	
+	str_list = gmyth_string_list_new ();
+	
+	tmp_str = g_string_new ("QUERY_RECORDER ");
+	g_string_append_printf (tmp_str, "%d", recorder->recorder_num); 
+	
+	gmyth_string_list_append_string (str_list, tmp_str);
+	gmyth_string_list_append_string (str_list, g_string_new ("SPAWN_LIVETV"));
+	gmyth_string_list_append_string (str_list, tvchain_id);
+	gmyth_string_list_append_int (str_list, 0); // PIP = FALSE (0)
+
+	gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
+	
+    g_string_free (tmp_str, TRUE);
+    
+    tmp_str = gmyth_string_list_get_string (str_list, 0);
+    if (tmp_str == NULL) {
+    	g_warning ("[%s] Spawntv request returned %s", __FUNCTION__, tmp_str->str);
+    	return FALSE;
+	}
+	
+    if (g_ascii_strncasecmp (tmp_str->str, "ok", 2)) {
+    	g_warning ("[%s] Spawntv request returned %s", __FUNCTION__, tmp_str->str);
+	    g_object_unref (str_list);
+    	return FALSE;
+    }
+
+    g_object_unref (str_list);
+    return TRUE;
+
+}
+
+/** Sends the command STOP_LIVETV to Mythtv backend.
+ * 
+ * @param recorder the GMythRecorder instance.
+ * @return true if success, false if any error happens.
+ */
+gboolean
+gmyth_recorder_stop_livetv (GMythRecorder *recorder)
+{
+	GMythStringList *str_list;
+	GString *tmp_str;
+
+	g_debug ("[%s]", __FUNCTION__);
+
+	str_list = gmyth_string_list_new ();
+
+	tmp_str = g_string_new ("QUERY_RECORDER ");
+	g_string_append_printf (tmp_str, "%d", recorder->recorder_num); 
+	gmyth_string_list_append_string (str_list, g_string_new ("STOP_LIVETV"));
+
+	gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
+
+	g_string_free (tmp_str, TRUE);
+
+	tmp_str = gmyth_string_list_get_string (str_list, 0);
+	if (g_ascii_strncasecmp (tmp_str->str, "ok", 2)) {
+		g_warning ("[%s] Stop livetv request returned %s", __FUNCTION__, tmp_str->str);
+		g_object_unref (str_list);
+		return FALSE;
+	}
+
+	g_object_unref (str_list);
+	return TRUE;
+
+}
+
+/** Send a CHECK_CHANNEL command request to the backend, in order to find if a 
+ * certain channel actually exists.
+ * 
+ * @param recorder The GMythRecorder instance.
+ * @param channel	 The new channel to be checked.
+ * @return true if success, false if any error happens.
+ */
+gboolean
+gmyth_recorder_check_channel (GMythRecorder *recorder, gint channel)
+{
+	GMythStringList *str_list;
+	GString *tmp_str;
+	
+	g_debug ("[%s] SET_CHANNEL with channel = %d", __FUNCTION__, channel);
+	
+	str_list = gmyth_string_list_new ();
+	
+	tmp_str = g_string_new ("QUERY_RECORDER ");
+	g_string_append_printf (tmp_str, "%d", recorder->recorder_num); 
+	
+	gmyth_string_list_append_string (str_list, tmp_str);
+	gmyth_string_list_append_string (str_list, g_string_new ("CHECK_CHANNEL"));
+	gmyth_string_list_append_int (str_list, channel);
+
+	gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
+	
+    g_string_free (tmp_str, TRUE);
+    
+    tmp_str = gmyth_string_list_get_string (str_list, 0);
+    if (tmp_str == NULL) {
+    	g_warning ("[%s] CHECK_CHANNEL request returned %s", __FUNCTION__, tmp_str->str);
+    	return FALSE;
+	}
+	
+    if (g_ascii_strncasecmp (tmp_str->str, "ok", 2) == 0 || g_ascii_strncasecmp (tmp_str->str, "0", 1) == 0 ) {
+    	g_warning ("[%s] CHECK_CHANNEL request returned %s", __FUNCTION__, tmp_str->str);
+	    g_object_unref (str_list);
+    	return FALSE;
+    }
+
+    g_object_unref (str_list);
+    return TRUE;
+
+}
+
+/** Send a SET_CHANNEL command request to the backend, to start streaming on another 
+ * TV content channel.
+ * 
+ * @param recorder The GMythRecorder instance.
+ * @param channel	 The new channel to be loaded.
+ * @return true if success, false if any error happens.
+ */
+gboolean
+gmyth_recorder_set_channel (GMythRecorder *recorder, gint channel)
+{
+	GMythStringList *str_list;
+	GString *tmp_str;
+	
+	g_debug ("[%s] SET_CHANNEL with channel = %d", __FUNCTION__, channel);
+	
+	str_list = gmyth_string_list_new ();
+	
+	tmp_str = g_string_new ("QUERY_RECORDER ");
+	g_string_append_printf (tmp_str, "%d", recorder->recorder_num); 
+	
+	gmyth_string_list_append_string (str_list, tmp_str);
+	gmyth_string_list_append_string (str_list, g_string_new ("SET_CHANNEL"));
+	gmyth_string_list_append_int (str_list, channel);
+
+	gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
+	
+    g_string_free (tmp_str, TRUE);
+    
+    tmp_str = gmyth_string_list_get_string (str_list, 0);
+    if (tmp_str == NULL) {
+    	g_warning ("[%s] SET_CHANNEL request returned %s", __FUNCTION__, tmp_str->str);
+    	return FALSE;
+	}
+	
+    if (g_ascii_strncasecmp (tmp_str->str, "ok", 2)) {
+    	g_warning ("[%s] SET_CHANNEL request returned %s", __FUNCTION__, tmp_str->str);
+	    g_object_unref (str_list);
+    	return FALSE;
+    }
+
+    g_object_unref (str_list);
+    return TRUE;
+
+}
diff -r f926338c6952 -r dc4814fe4723 gmyth/src/gmyth_recorder.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gmyth/src/gmyth_recorder.h	Mon Nov 06 19:00:37 2006 +0000
@@ -0,0 +1,94 @@
+/**
+ * GMyth Library
+ *
+ * @file gmyth/gmyth_recorder.h
+ * 
+ * @brief <p> GMythRecorder class defines functions for playing live tv.
+ *
+ * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
+ * @author Hallyson Luiz de Morais Melo <hallyson.melo@indt.org.br>
+ *
+ *//*
+ * 
+ * This program 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.
+ *
+ * This program 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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef __GMYTH_RECORDER_H__
+#define __GMYTH_RECORDER_H__
+
+#include <glib-object.h>
+
+#include "gmyth_socket.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <netdb.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+G_BEGIN_DECLS
+
+#define GMYTH_RECORDER_TYPE               (gmyth_recorder_get_type ())
+#define GMYTH_RECORDER(obj)               (G_TYPE_CHECK_INSTANCE_CAST ((obj), GMYTH_RECORDER_TYPE, GMythRecorder))
+#define GMYTH_RECORDER_CLASS(klass)       (G_TYPE_CHECK_CLASS_CAST ((klass), GMYTH_RECORDER_TYPE, GMythRecorderClass))
+#define IS_GMYTH_RECORDER(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GMYTH_RECORDER_TYPE))
+#define IS_GMYTH_RECORDER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GMYTH_RECORDER_TYPE))
+#define GMYTH_RECORDER_GET_CLASS(obj)     (G_TYPE_INSTANCE_GET_CLASS ((obj), GMYTH_RECORDER_TYPE, GMythRecorderClass))
+
+
+typedef struct _GMythRecorder         GMythRecorder;
+typedef struct _GMythRecorderClass    GMythRecorderClass;
+
+struct _GMythRecorderClass
+{
+  GObjectClass parent_class;
+
+  /* callbacks */
+  /* no one for now */
+};
+
+struct _GMythRecorder
+{
+    GObject parent;
+
+    /* socket descriptor */
+    GMythSocket *myth_socket;
+    
+    int recorder_num;
+    GString *hostname;
+    int port;
+};
+
+
+GType   gmyth_recorder_get_type   (void);
+
+GMythRecorder* gmyth_recorder_new    (int num,
+	                                     GString *hostname,
+	                                     gshort port);
+
+gboolean gmyth_recorder_setup     (GMythRecorder *recorder);
+gboolean gmyth_recorder_spawntv   (GMythRecorder *recorder,
+                                         GString *tvchain_id);
+gboolean gmyth_recorder_stop_livetv (GMythRecorder *recorder);
+
+gboolean gmyth_recorder_check_channel (GMythRecorder *recorder, gint channel);
+
+gboolean gmyth_recorder_set_channel   (GMythRecorder *recorder,
+                                         gint channel);
+
+G_END_DECLS
+
+#endif /* __GMYTH_REMOTE_ENCODER_H__ */
diff -r f926338c6952 -r dc4814fe4723 gmyth/src/gmyth_remote_util.c
--- a/gmyth/src/gmyth_remote_util.c	Mon Nov 06 18:58:26 2006 +0000
+++ b/gmyth/src/gmyth_remote_util.c	Mon Nov 06 19:00:37 2006 +0000
@@ -28,7 +28,7 @@
 #include "gmyth_remote_util.h"
  
 #include "gmyth_context.h"
-#include "gmyth_remote_encoder.h"
+#include "gmyth_recorder.h"
 #include "gmyth_stringlist.h"
 
 /** Requests the Mythtv backend for a free remote recorder.
@@ -36,10 +36,10 @@
  * @param curr The recorder index, or -1 to consider the first one.
  * @return the remote encoder instance available, or NULL if any error happens.
  */
-GMythRemoteEncoder*
+GMythRecorder*
 remote_request_next_free_recorder (int curr)
 {
-	GMythRemoteEncoder *encoder;
+	GMythRecorder *recorder = NULL;
 	GString *hostname;
 	int num, port;
 	
@@ -58,13 +58,18 @@
     num = gmyth_string_list_get_int (strlist, 0);
     hostname = gmyth_string_list_get_string (strlist, 1);
     port = gmyth_string_list_get_int (strlist, 2);
+    
+    if ( num < 0 || port < 0 )
+    	goto clean_up;
 
 	g_debug ("[%s] Free recorder info received: num: %d, hostname: %s, port: %d", 
 				__FUNCTION__, num, hostname->str, port);
 	
-	encoder = gmyth_remote_encoder_new (num, hostname, port);
+	recorder = gmyth_recorder_new (num, hostname, port);
+
+clean_up:
 	
 	g_object_unref (strlist);
 	
-	return encoder;
+	return recorder;
 }
diff -r f926338c6952 -r dc4814fe4723 gmyth/src/gmyth_remote_util.h
--- a/gmyth/src/gmyth_remote_util.h	Mon Nov 06 18:58:26 2006 +0000
+++ b/gmyth/src/gmyth_remote_util.h	Mon Nov 06 19:00:37 2006 +0000
@@ -29,11 +29,11 @@
 #define __REMOTE_UTIL_H__
 
 #include <glib.h>
-#include "gmyth_remote_encoder.h"
+#include "gmyth_recorder.h"
 
 G_BEGIN_DECLS
 
-GMythRemoteEncoder* remote_request_next_free_recorder (int curr);
+GMythRecorder* remote_request_next_free_recorder (int curr);
 
 G_END_DECLS