diff -r 000000000000 -r 06009f72d657 branches/gmyth-0.1b/src/gmyth_monitor_handler.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/branches/gmyth-0.1b/src/gmyth_monitor_handler.c Tue Feb 13 23:17:35 2007 +0000
@@ -0,0 +1,595 @@
+/**
+ * GMyth Library
+ *
+ * @file gmyth/gmyth_monitor_handler.c
+ *
+ * @brief
GMythMonitorHandler deals with the streaming media events remote/local
+ * that are sent to the MythTV frontend.
+ *
+ * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
+ * @author Rosfran Lins Borges
+ *
+ *//*
+ *
+ * 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
+ *
+ * GStreamer MythTV plug-in properties:
+ * - location (backend server hostname/URL) [ex.: myth://192.168.1.73:28722/1000_1092091.nuv]
+ * - path (qurl - remote file to be opened)
+ * - port number *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "gmyth_marshal.h"
+
+#include "gmyth_monitor_handler.h"
+
+#include "gmyth.h"
+
+#define GMYTHTV_QUERY_HEADER "QUERY_FILETRANSFER "
+
+#define GMYTHTV_VERSION 30
+
+#define GMYTHTV_TRANSFER_MAX_WAITS 700
+
+#define GMYTHTV_BUFFER_SIZE 8*1024
+
+#ifdef GMYTHTV_ENABLE_DEBUG
+#define GMYTHTV_ENABLE_DEBUG 1
+#else
+#undef GMYTHTV_ENABLE_DEBUG
+#endif
+
+/* this NDEBUG is to maintain compatibility with GMyth library */
+#ifndef NDEBUG
+#define GMYTHTV_ENABLE_DEBUG 1
+#endif
+
+//GMainContext *io_watcher_context = NULL;
+
+//GThread *monitor_th = NULL;
+
+//static gboolean* myth_control_sock_listener( GIOChannel *io_channel );
+//static gboolean gmyth_monitor_handler_listener( GIOChannel *io_channel,
+// GIOCondition condition, gpointer data );
+
+//gboolean* gmyth_monitor_handler_listener( GMythMonitorHandler *monitor );
+
+void gmyth_monitor_handler_listener( GMythMonitorHandler *monitor, gpointer user_data );
+
+static void gmyth_monitor_handler_default_listener( GMythMonitorHandler *monitor, gint msg_code, gchar* message );
+
+static GMutex* mutex = NULL;
+
+//static GCond* io_watcher_cond = NULL;
+
+static void gmyth_monitor_handler_class_init (GMythMonitorHandlerClass *klass);
+static void gmyth_monitor_handler_init (GMythMonitorHandler *object);
+
+static void gmyth_monitor_handler_dispose (GObject *object);
+static void gmyth_monitor_handler_finalize (GObject *object);
+
+static gboolean gmyth_connect_to_backend_monitor (GMythMonitorHandler *monitor);
+
+void gmyth_monitor_handler_close( GMythMonitorHandler *monitor );
+
+G_DEFINE_TYPE(GMythMonitorHandler, gmyth_monitor_handler, G_TYPE_OBJECT)
+
+static void
+gmyth_monitor_handler_class_init (GMythMonitorHandlerClass *klass)
+{
+ GObjectClass *gobject_class;
+ GMythMonitorHandlerClass *gmonitor_class;
+
+ gobject_class = (GObjectClass *) klass;
+ gmonitor_class = (GMythMonitorHandlerClass *) gobject_class;
+
+ gobject_class->dispose = gmyth_monitor_handler_dispose;
+ gobject_class->finalize = gmyth_monitor_handler_finalize;
+
+ gmonitor_class->backend_events_handler_signal_id =
+ g_signal_new ("backend-events-handler",
+ G_TYPE_FROM_CLASS (gmonitor_class),
+ G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
+ 0,
+ NULL,
+ NULL,
+ gmyth_marshal_VOID__INT_STRING,
+ G_TYPE_NONE,
+ 2,
+ G_TYPE_INT,
+ G_TYPE_STRING);
+
+ gmonitor_class->backend_events_handler = gmyth_monitor_handler_default_listener;
+
+}
+
+static void
+gmyth_monitor_handler_init (GMythMonitorHandler *monitor)
+{
+ g_return_if_fail( monitor != NULL );
+
+ monitor->event_sock = NULL;
+ monitor->hostname = NULL;
+ monitor->port = 0;
+ monitor->actual_index = 0;
+
+ monitor->allow_msgs_listener = TRUE;
+
+ //monitor->backend_msgs = g_hash_table_new( g_int_hash, g_int_equal );
+
+ /* it is used for signalizing the event socket consumer thread */
+ //io_watcher_cond = g_cond_new();
+
+ /* mutex to control access to the event socket consumer thread */
+ //mutex = g_mutex_new();
+
+ monitor->monitor_th = NULL;
+
+ monitor->gmyth_monitor_handler_listener = gmyth_monitor_handler_listener;
+}
+
+static void
+gmyth_monitor_handler_dispose (GObject *object)
+{
+
+ GMythMonitorHandler *monitor = GMYTH_MONITOR_HANDLER (object);
+
+ monitor->allow_msgs_listener = FALSE;
+
+ if ( monitor->monitor_th != NULL )
+ {
+ g_thread_pool_free( monitor->monitor_th, TRUE, FALSE );
+ //g_thread_exit( monitor->monitor_th );
+ if ( monitor->monitor_th != NULL )
+ g_object_unref( monitor->monitor_th );
+ monitor->monitor_th = NULL;
+ }
+
+ if ( monitor->event_sock != NULL )
+ {
+ g_object_unref( monitor->event_sock );
+ monitor->event_sock = NULL;
+ }
+
+ if ( monitor->hostname != NULL )
+ {
+ g_free( monitor->hostname );
+ monitor->hostname = NULL;
+ }
+
+ if ( monitor->backend_msgs != NULL )
+ {
+ g_hash_table_destroy ( monitor->backend_msgs );
+ monitor->backend_msgs = NULL;
+ }
+
+ if ( mutex != NULL )
+ {
+ g_mutex_free( mutex );
+ mutex = NULL;
+ }
+
+ /*
+ if ( io_watcher_cond != NULL )
+ {
+ g_cond_free( io_watcher_cond );
+ io_watcher_cond = NULL;
+ }
+ */
+
+ G_OBJECT_CLASS (gmyth_monitor_handler_parent_class)->dispose (object);
+}
+
+static void
+gmyth_monitor_handler_finalize (GObject *object)
+{
+ g_signal_handlers_destroy (object);
+
+ G_OBJECT_CLASS (gmyth_monitor_handler_parent_class)->finalize (object);
+}
+
+// fixme: do we need the card_id????
+GMythMonitorHandler*
+gmyth_monitor_handler_new ( void )
+{
+ GMythMonitorHandler *monitor = GMYTH_MONITOR_HANDLER ( g_object_new ( GMYTH_MONITOR_HANDLER_TYPE,
+ FALSE ) );
+
+ return monitor;
+}
+
+static gboolean
+myth_control_acquire_context( gboolean do_wait )
+{
+
+ gboolean ret = TRUE;
+ //guint max_iter = 50;
+
+ //g_mutex_lock( mutex );
+
+ //while ( !has_io_access )
+ // g_cond_wait( io_watcher_cond, mutex );
+
+ //has_io_access = FALSE;
+ /*
+ if ( do_wait ) {
+ while ( --max_iter > 0 && !g_main_context_wait( io_watcher_context, io_watcher_cond, mutex ) )
+ ret = FALSE;
+ } else if ( !g_main_context_acquire( io_watcher_context ) )
+ ret = FALSE;
+ */
+
+ //g_static_mutex_lock( &st_mutex );
+
+ return ret;
+
+}
+
+static gboolean
+myth_control_release_context( )
+{
+
+ gboolean ret = TRUE;
+
+ //g_static_mutex_unlock( &st_mutex );
+
+ //g_main_context_release( io_watcher_context );
+
+ //g_main_context_wakeup( io_watcher_context );
+
+ //has_io_access = TRUE;
+
+ //g_cond_broadcast( io_watcher_cond );
+
+ //g_mutex_unlock( mutex );
+
+ return ret;
+
+}
+
+gboolean
+gmyth_monitor_handler_open (GMythMonitorHandler *monitor, const gchar *hostname, gint port)
+{
+ gboolean ret = TRUE;
+
+ g_return_val_if_fail( hostname != NULL, FALSE );
+
+ if (monitor->hostname != NULL) {
+ g_free (monitor->hostname);
+ monitor->hostname = NULL;
+ }
+
+ monitor->hostname = g_strdup( hostname );
+ monitor->port = port;
+
+ gmyth_debug ("Monitor event socket --- hostname: %s, port %d\n", monitor->hostname, monitor->port);
+
+ /* configure the event socket */
+ if ( NULL == monitor->event_sock ) {
+ if (!gmyth_connect_to_backend_monitor (monitor)) {
+ g_printerr( "Connection to backend failed (Event Socket).\n" );
+ ret = FALSE;
+ }
+ } else {
+ g_warning("Remote monitor event socket already created.\n");
+ }
+
+ return ret;
+
+}
+
+static gint
+gmyth_monitor_handler_is_backend_message( GMythMonitorHandler *monitor,
+ GMythStringList* strlist, gchar **back_msg_action )
+{
+ gint msg_type = GMYTH_BACKEND_NO_MESSAGE;
+ GString *back_msg = NULL;
+
+ back_msg = gmyth_string_list_get_string( strlist, 0 );
+ if ( back_msg != NULL && back_msg->str != NULL &&
+ strstr( back_msg->str, "BACKEND" ) != NULL )
+ {
+ gmyth_debug( "MONITOR HANDLER - Received backend message = %s", back_msg->str );
+ *back_msg_action = gmyth_string_list_get_char_array( strlist, 1 );
+
+ if ( back_msg_action != NULL )
+ {
+
+ if ( g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "LIVETV_CHAIN" ) ||
+ g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "RECORDING_LIST_CHANGE" ) ||
+ g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "SCHEDULE_CHANGE" ) ||
+ g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "LIVETV_WATCH" ) )
+ {
+ gmyth_debug( "MONITOR: message type == GMYTH_BACKEND_PROGRAM_INFO_CHANGED, msg = %s", *back_msg_action );
+ msg_type = GMYTH_BACKEND_PROGRAM_INFO_CHANGED;
+ } else if ( g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "DONE_RECORDING" ) ) {
+ gmyth_debug( "MONITOR: message type == GMYTH_BACKEND_DONE_RECORDING, msg = %s", *back_msg_action );
+ msg_type = GMYTH_BACKEND_DONE_RECORDING;
+ }
+
+ //g_hash_table_insert ( monitor->backend_msgs,
+ // &(monitor->actual_index), *back_msg_action );
+
+ } // if
+ }
+
+ if ( back_msg != NULL )
+ {
+ g_string_free( back_msg, TRUE );
+ back_msg = NULL;
+ }
+
+ return msg_type;
+
+}
+
+static void
+gmyth_monitor_handler_default_listener( GMythMonitorHandler *monitor, gint msg_code, gchar* message )
+{
+ //assert( message!= NULL );
+ gmyth_debug( "DEFAULT Signal handler ( msg = %s, code = %d )\n",
+ message, msg_code );
+}
+
+static void
+gmyth_monitor_handler_print( GString *str, gpointer ptr )
+{
+ gmyth_debug( "Backend message event: %s --- ", str->str );
+}
+
+//static void
+//gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, gpointer user_data)
+//static gboolean
+//gmyth_monitor_handler_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data )
+//gboolean* gmyth_monitor_handler_listener( GMythMonitorHandler *monitor )
+void
+gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, gpointer user_data)
+{
+ //GMythMonitorHandler *monitor = (GMythMonitorHandler*)data;
+ GIOStatus io_status;
+ GIOCondition io_cond;
+ guint recv = 0;
+ gboolean *ret = g_new0( gboolean, 1 );
+ *ret = TRUE;
+ //gboolean ret = TRUE;
+ gsize len = 0;
+
+ static guint count = 0;
+
+ GIOChannel *io_channel = monitor->event_sock->sd_io_ch;
+ //GIOCondition condition = g_io_channel_get_buffer_condition( io_channel );
+
+ GMythStringList *strlist = NULL;
+
+ //GMythMonitorHandler *monitor = (GMythMonitorHandler*)data;
+
+ myth_control_acquire_context (TRUE);
+
+ if ( io_channel == NULL ) {
+ g_debug ("Monitor socket is NULL!\n");
+ *ret = FALSE;
+ goto clean_up;
+ }
+
+ while (monitor->allow_msgs_listener) {
+ ++count;
+
+ gmyth_debug ("%d - Listening on Monitor socket...!\n", count);
+
+ do
+ {
+
+ gint bytes_sent = 0;
+
+ strlist = gmyth_string_list_new();
+
+ if ( monitor->event_sock != NULL )
+ {
+
+ len = gmyth_socket_read_stringlist( monitor->event_sock, strlist );
+
+ if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 )
+ {
+ bytes_sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
+
+ gmyth_debug ( "[%s] MONITOR: received data buffer from IO event channel... %d strings gone!\n",
+ __FUNCTION__, len );
+
+ recv += len;
+
+ /* debug purpose: prints out all the string list elements */
+ g_list_foreach( strlist->glist, (GFunc)gmyth_monitor_handler_print, NULL );
+
+ gchar *back_msg_action = g_new0( gchar, 1 );
+ gint msg_type = gmyth_monitor_handler_is_backend_message( monitor, strlist,
+ &back_msg_action );
+
+ g_signal_emit ( monitor,
+ GMYTH_MONITOR_HANDLER_GET_CLASS (monitor)->backend_events_handler_signal_id,
+ 0, /* details */
+ msg_type, back_msg_action );
+
+ if (back_msg_action!= NULL)
+ g_free( back_msg_action );
+
+ }
+
+ }
+
+ if (strlist!=NULL)
+ g_object_unref( strlist );
+
+ io_cond = g_io_channel_get_buffer_condition( io_channel );
+
+ } while ( recv <= 0 && ( io_cond & G_IO_IN ) != 0 );
+
+ gmyth_debug ("[%s]\tMONITOR EVENT: Read %d bytes\n", __FUNCTION__, recv );
+
+ g_usleep( 300 );
+
+ } /* main GThread while */
+
+ myth_control_release_context ();
+
+ if ( io_status == G_IO_STATUS_ERROR ) {
+ //gmyth_debug ("[%s] Error reading: %s\n", __FUNCTION__, error != NULL ? error->message : "" );
+ gmyth_debug ("Error reading MONITOR event socket.\n");
+ *ret = FALSE;
+ goto clean_up;
+ }
+
+clean_up:
+
+ if (strlist!=NULL)
+ g_object_unref( strlist );
+
+ return;
+
+}
+
+static gboolean
+gmyth_connect_to_backend_monitor (GMythMonitorHandler *monitor)
+{
+ gboolean ret = TRUE;
+
+ monitor->event_sock = gmyth_socket_new();
+
+ /* Connects the socket, send Mythtv ANN Monitor and verify Mythtv protocol version */
+ if (!gmyth_socket_connect_to_backend_events ( monitor->event_sock,
+ monitor->hostname, monitor->port, FALSE ) )
+ {
+ g_object_unref (monitor->event_sock);
+ monitor->event_sock = NULL;
+ ret = FALSE;
+ }
+
+ return ret;
+}
+
+static gboolean*
+gmyth_monitor_handler_setup( GMythMonitorHandler *monitor, GIOChannel *channel )
+{
+ gboolean *ret = g_new0( gboolean, 1 );
+ guint src_id = 0;
+
+ *ret = TRUE;
+
+ //io_watcher_context = g_main_context_default();
+ //GMainLoop *loop = g_main_loop_new( io_watcher_context, TRUE );
+
+ //GSource *source;
+
+ if ( channel != NULL ) {
+ //source = g_io_create_watch( channel, G_IO_IN | G_IO_HUP );
+ src_id = g_io_add_watch( channel, G_IO_IN,
+ (GIOFunc)gmyth_monitor_handler_listener, monitor );
+ } else {
+ *ret = FALSE;
+ goto cleanup;
+ }
+
+ //g_source_set_callback ( source, (GSourceFunc)gmyth_monitor_handler_listener, NULL, NULL );
+
+ //g_source_attach( source, io_watcher_context );
+
+ //if (NULL == source){
+ if (src_id < 0){
+ gmyth_debug( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ );
+ *ret = FALSE;
+ goto cleanup;
+ }
+
+ //g_main_loop_run( loop );
+
+cleanup:
+ //if ( source != NULL )
+ // g_source_unref( source );
+
+ //if ( io_watcher_context != NULL )
+ // g_main_context_unref( io_watcher_context );
+
+ //if ( loop != NULL )
+ // g_main_loop_unref( loop );
+
+ return ret;
+
+}
+
+gboolean
+gmyth_monitor_handler_start (GMythMonitorHandler *monitor)
+{
+ gboolean *ret = g_new0( gboolean, 1 );
+ *ret = TRUE;
+
+ /*if (!g_thread_supported () ) g_thread_init (NULL);*/
+ /*
+ monitor->monitor_th = g_thread_pool_new( (GThreadFunc)gmyth_monitor_handler_listener,
+ monitor, TRUE, NULL );
+ */
+ monitor->monitor_th = g_thread_pool_new( (GFunc)gmyth_monitor_handler_listener,
+ monitor, 3, TRUE, NULL );
+ g_thread_pool_push( monitor->monitor_th, monitor, NULL );
+
+ //if ( ( ret = g_thread_join( monitor_th ) ) == FALSE )
+ if ( monitor->monitor_th != NULL )
+ //if ( gmyth_monitor_handler_setup( monitor, monitor->event_sock->sd_io_ch ) )
+ {
+ gmyth_debug ( "\n[%s]\tOK! Starting listener on the MONITOR event socket...[thread location = %p]\n",
+ __FUNCTION__, g_thread_self( ) );
+ *ret = TRUE;
+ } else {
+ gmyth_debug ( "\n[%s]\tERROR! Coudn't start listener on the MONITOR event socket...[thread location = %p]\n",
+ __FUNCTION__, g_thread_self( ) );
+ *ret = FALSE;
+ }
+
+//cleanup:
+
+ gmyth_debug( "[%s] Watch listener function over the IO control channel? %s!!!\n",
+ __FUNCTION__, ( *ret == TRUE ? "YES" : "NO" ) );
+
+ return *ret;
+}
+
+void
+gmyth_monitor_handler_close( GMythMonitorHandler *monitor )
+{
+
+ if (monitor->event_sock) {
+ g_object_unref( monitor->event_sock );
+ monitor->event_sock = NULL;
+ }
+
+ if (monitor->hostname) {
+ g_free( monitor->hostname );
+ monitor->hostname = NULL;
+ }
+
+}
+