# HG changeset patch # User rosfran # Date 1164655535 0 # Node ID 9f3c698e34a7c70ed5d31ffcf193fe27269b6342 # Parent 04cead6ae39bc95be2268154fff0ec59d5af8350 [svn r116] A lot of API changes. diff -r 04cead6ae39b -r 9f3c698e34a7 gmyth/src/gmyth_context.c --- a/gmyth/src/gmyth_context.c Mon Nov 27 19:24:43 2006 +0000 +++ b/gmyth/src/gmyth_context.c Mon Nov 27 19:25:35 2006 +0000 @@ -176,7 +176,7 @@ int server_port = gmyth_settings_get_backend_port(gcontext->gmyth_settings); gcontext->server_socket = gmyth_socket_new (); - if (!gmyth_socket_connect_to_backend (gcontext->server_socket, + if (!gmyth_socket_connect_to_backend ( gcontext->server_socket, server_hostname->str, server_port, FALSE)) { g_warning ("[%s] Socket connection to backend error!", __FUNCTION__); g_object_unref (gcontext->server_socket); diff -r 04cead6ae39b -r 9f3c698e34a7 gmyth/src/gmyth_file_transfer.c --- a/gmyth/src/gmyth_file_transfer.c Mon Nov 27 19:24:43 2006 +0000 +++ b/gmyth/src/gmyth_file_transfer.c Mon Nov 27 19:25:35 2006 +0000 @@ -74,7 +74,7 @@ #define GMYTHTV_ENABLE_DEBUG 1 #endif -static gboolean has_io_access = TRUE; +//static gboolean has_io_access = TRUE; enum myth_sock_types { GMYTH_PLAYBACK_TYPE = 0, @@ -85,6 +85,9 @@ //static GStaticMutex mutex = G_STATIC_MUTEX_INIT; +//static gboolean* myth_control_sock_listener( GIOChannel *io_channel ); +static gboolean myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data ); + static GMutex* mutex = NULL; static GCond* io_watcher_cond = NULL; @@ -98,7 +101,7 @@ static void gmyth_file_transfer_finalize (GObject *object); static gboolean gmyth_connect_to_backend (GMythFileTransfer *transfer); -static gboolean myth_init_io_watchers( GMythFileTransfer *transfer ); +//static gboolean myth_init_io_watchers( GMythFileTransfer *transfer ); void gmyth_file_transfer_close( GMythFileTransfer *transfer ); @@ -196,6 +199,135 @@ } static gboolean +myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data ) +//myth_control_sock_listener( GIOChannel *io_channel ) +{ + + GIOStatus io_status; + GError *error = NULL; + GIOCondition io_cond; + //GIOCondition condition; + gchar *trash = g_new0( gchar, GMYTHTV_BUFFER_SIZE*10 ); + guint recv = 0; + //gboolean* ret = g_new0( gboolean, 1 ); + gboolean ret = TRUE; + + //*ret = TRUE; + + if ( io_channel == NULL ) { + ret = FALSE; + goto clean_up; + } + g_print ("Listening on Monitor socket...!\n"); + + //condition = g_io_channel_get_buffer_condition( io_channel ); + + myth_control_acquire_context (TRUE); + + //while ( !has_io_access ) + // g_cond_wait( io_watcher_cond, mutex ); + + //myth_control_acquire_context (TRUE); + + gsize len = 0; + if (condition & G_IO_HUP) { + g_print ("Read end of pipe died!\n"); + ret = FALSE; + goto clean_up; + } + + if ( ( condition & G_IO_IN ) != 0 ) { + io_status = g_io_channel_set_encoding( io_channel, NULL, &error ); + do + { + //trash = g_new0( gchar, GMYTHTV_BUFFER_SIZE ); + + io_status = g_io_channel_read_chars( io_channel, trash + recv, + GMYTHTV_BUFFER_SIZE, &len, &error); + + g_print( "[%s] Received data buffer from IO binary channel... %d bytes gone!\n", + __FUNCTION__, len ); + + recv += len; + + //msg = g_strconcat( msg, g_strdup(trash), NULL ); + + //if ( trash != NULL ) + // g_free( trash ); + + io_cond = g_io_channel_get_buffer_condition( io_channel ); + + } while ( ( io_cond & G_IO_IN ) != 0 && ( io_status != G_IO_STATUS_ERROR ) ); + } + //ret = g_io_channel_read_chars ( source, &msg, &len, NULL, &err); + if ( io_status == G_IO_STATUS_ERROR ) { + g_print ("[%s] Error reading: %s\n", __FUNCTION__, error != NULL ? error->message : "" ); + ret = FALSE; + } + g_print ("\n[%s]\tEVENT: Read %d bytes: %s\n\n", __FUNCTION__, len, trash != NULL ? trash : "[no event data]" ); + + //g_cond_signal( io_watcher_cond ); + + //myth_control_release_context( ); +clean_up: + return ret; + +} + +static gboolean +gmyth_connect_to_backend_monitor (GMythFileTransfer *transfer) +{ + gboolean ret = TRUE; + GString *base_str = g_string_new( "" ); + guint source_id = 0; + + transfer->event_sock = gmyth_socket_new(); + + gmyth_socket_connect ( transfer->event_sock, transfer->hostname->str, transfer->port); + g_string_printf( base_str, "ANN Monitor %s %d", transfer->hostname->str, TRUE ); + + gmyth_socket_send_command( transfer->event_sock, base_str ); + GString *resp = gmyth_socket_receive_response( transfer->event_sock ); + g_print( "[%s] Got Monitor response from %s: %s\n", __FUNCTION__, base_str->str, resp->str ); + //g_thread_create( (GThreadFunc)myth_control_sock_listener, transfer->event_sock->sd_io_ch, TRUE, NULL ); + //io_watcher_context = g_main_context_default(); + GMainLoop *loop = g_main_loop_new( NULL, TRUE ); + + //GSource *source; + + if ( transfer->event_sock->sd_io_ch != NULL ) + source_id = g_io_add_watch( transfer->event_sock->sd_io_ch, G_IO_IN , (GIOFunc)myth_control_sock_listener, NULL ); + else { + ret = FALSE; + goto cleanup; + } + + //g_source_set_callback ( source, (GSourceFunc)myth_control_sock_listener, NULL, NULL ); + + //g_source_attach( source, io_watcher_context ); + +/* + if (source==NULL) { + g_printerr( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ ); + goto cleanup; + } +*/ + g_print( "[%s]\tOK! Starting listener on the MONITOR event socket...\n", __FUNCTION__ ); + + //g_main_loop_run( loop ); + +cleanup: + //if ( source != NULL ) + // g_source_unref( source ); + + g_debug( "[%s] Watch listener function over the IO control channel? %s!!!\n", + __FUNCTION__, ( ret == TRUE ? "YES" : "NO" ) ); + + return ret; + +} + +static gboolean gmyth_connect_to_backend (GMythFileTransfer *transfer) { GString *base_str = g_string_new( "" ); @@ -227,7 +359,11 @@ strlist = gmyth_string_list_new(); hostname = gmyth_socket_get_local_hostname(); - g_string_printf( base_str, "ANN FileTransfer %s 1 -1", hostname->str); + g_debug( "[%s] MythTV version (from backend) = %d.\n", __FUNCTION__, transfer->control_sock->mythtv_version ); + if ( transfer->control_sock->mythtv_version > 26 ) + g_string_printf( base_str, "ANN FileTransfer %s 1 -1", hostname->str); + else + g_string_printf( base_str, "ANN FileTransfer %s", hostname->str); gmyth_string_list_append_string (strlist, base_str ); gmyth_string_list_append_char_array (strlist, path_dir ); @@ -251,6 +387,7 @@ return FALSE; } + gmyth_connect_to_backend_monitor( transfer ); if ( strlist != NULL ) g_object_unref( strlist ); @@ -410,7 +547,7 @@ } gint -gmyth_file_transfer_read(GMythFileTransfer *transfer, void *data, gint size, gboolean read_unlimited) +gmyth_file_transfer_read(GMythFileTransfer *transfer, GByteArray *data, gint size, gboolean read_unlimited) { gsize bytes_sent = 0; gsize bytes_read = 0; @@ -427,6 +564,8 @@ GMythStringList *strlist = NULL; GString *query; + + gchar *data_buffer = NULL; g_return_val_if_fail ( data != NULL, -2 ); @@ -475,13 +614,20 @@ g_print( "[%s] got SENT buffer message = %d\n", __FUNCTION__, bytes_sent ); if ( bytes_sent != 0 ) { + + data_buffer = g_new0( gchar, bytes_sent ); + while (total_read != bytes_sent) { - io_status = g_io_channel_read_chars( io_channel, data + total_read, - (gsize) bytes_sent, // buffer_len - &bytes_read, &error ); + io_status = g_io_channel_read_chars( io_channel, data_buffer + total_read, + (gsize) bytes_sent, // buffer_len + &bytes_read, &error ); total_read += bytes_read; + + /* append new data to the increasing byte array */ + data = g_byte_array_append ( data, g_memdup( data_buffer, bytes_read ), bytes_read ); + printf ("Total file transfer data read: %d\n", total_read); } } diff -r 04cead6ae39b -r 9f3c698e34a7 gmyth/src/gmyth_file_transfer.h --- a/gmyth/src/gmyth_file_transfer.h Mon Nov 27 19:24:43 2006 +0000 +++ b/gmyth/src/gmyth_file_transfer.h Mon Nov 27 19:25:35 2006 +0000 @@ -78,6 +78,7 @@ /* socket descriptors */ GMythSocket *control_sock; + GMythSocket *event_sock; GMythSocket *sock; gint64 readposition; @@ -106,11 +107,10 @@ void gmyth_file_transfer_close (GMythFileTransfer *transfer); gboolean gmyth_file_transfer_is_open (GMythFileTransfer *transfer); -gint gmyth_file_transfer_read(GMythFileTransfer *transfer, void *data, gint size, gboolean read_unlimited); +gint gmyth_file_transfer_read(GMythFileTransfer *transfer, GByteArray *data, gint size, gboolean read_unlimited); gint64 gmyth_file_transfer_seek(GMythFileTransfer *transfer, guint64 pos, gint whence); gboolean gmyth_file_transfer_settimeout( GMythFileTransfer *transfer, gboolean fast); - //gboolean gmyth_file_transfer_playback_setup( GMythFileTransfer **transfer, gboolean live_tv ); //gboolean gmyth_file_transfer_setup( GMythFileTransfer **transfer, gboolean live_tv ); //gboolean gmyth_file_transfer_livetv_setup( GMythFileTransfer **transfer, GMythSocket *live_sock ); diff -r 04cead6ae39b -r 9f3c698e34a7 gmyth/src/gmyth_recorder.c --- a/gmyth/src/gmyth_recorder.c Mon Nov 27 19:24:43 2006 +0000 +++ b/gmyth/src/gmyth_recorder.c Mon Nov 27 19:25:35 2006 +0000 @@ -32,6 +32,9 @@ #include #include "gmyth_stringlist.h" +#include "gmyth_util.h" + +#define GMYTHTV_RECORDER_HEADER "QUERY_RECORDER" static void gmyth_recorder_class_init (GMythRecorderClass *klass); static void gmyth_recorder_init (GMythRecorder *object); @@ -112,8 +115,8 @@ recorder->myth_socket = gmyth_socket_new (); - if (!gmyth_socket_connect_to_backend (recorder->myth_socket, recorder->hostname->str, - recorder->port, TRUE) ) { + if (!gmyth_socket_connect_to_backend ( recorder->myth_socket, recorder->hostname->str, + recorder->port, TRUE ) ) { g_warning ("GMythRemoteEncoder: Connection to backend failed"); return FALSE; } @@ -136,14 +139,13 @@ gmyth_recorder_spawntv (GMythRecorder *recorder, GString *tvchain_id) { GMythStringList *str_list; - GString *tmp_str; + GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER ); 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); + 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")); @@ -180,15 +182,14 @@ gmyth_recorder_stop_livetv (GMythRecorder *recorder) { GMythStringList *str_list; - GString *tmp_str; + GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER ); 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")); + g_string_append_printf ( tmp_str, " %d", recorder->recorder_num ); + gmyth_string_list_append_char_array( str_list, "STOP_LIVETV" ); gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list); @@ -216,14 +217,13 @@ gmyth_recorder_send_frontend_ready_command (GMythRecorder *recorder) { GMythStringList *str_list; - GString *tmp_str; + GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER ); - g_debug ( "[%s] FRONTEND_READY with recorder id = %s", __FUNCTION__, recorder->recorder_num ); + g_debug ( "[%s] FRONTEND_READY with recorder id = %d\n", __FUNCTION__, recorder->recorder_num ); str_list = gmyth_string_list_new (); - tmp_str = g_string_new ("QUERY_RECORDER "); - g_string_append_printf (tmp_str, "%d", recorder->recorder_num); + 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 ("FRONTEND_READY")); @@ -260,14 +260,13 @@ gmyth_recorder_check_channel (GMythRecorder *recorder, gint channel) { GMythStringList *str_list; - GString *tmp_str; + GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER ); 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); + 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")); @@ -305,14 +304,13 @@ gmyth_recorder_set_channel (GMythRecorder *recorder, gint channel) { GMythStringList *str_list; - GString *tmp_str; + GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER ); 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); + 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")); @@ -338,3 +336,36 @@ return TRUE; } + +gint64 +gmyth_recorder_get_file_position ( GMythRecorder *recorder ) +{ + gint64 pos = 0; + GString *query = g_string_new( GMYTHTV_RECORDER_HEADER ); + + GMythStringList *str_list = gmyth_string_list_new (); + + g_string_append_printf( query, " %d", recorder->recorder_num ); + + gmyth_string_list_append_string (str_list, query); + gmyth_string_list_append_char_array( str_list, "GET_FILE_POSITION" ); + + gmyth_socket_sendreceive_stringlist ( recorder->myth_socket, str_list ); + + if ( str_list != NULL && gmyth_string_list_length(str_list) > 0 ) + { + GString *str = NULL; + if ( ( str = gmyth_string_list_get_string( str_list, 0 ) ) != NULL && strstr ( str->str, "bad" ) == NULL ) + pos = gmyth_util_decode_long_long( str_list, 0 ); + } + +#ifndef GMYTHTV_ENABLE_DEBUG + g_print( "[%s] Got file position = %lld\n", __FUNCTION__, pos ); +#endif + if (str_list!=NULL) + g_object_unref (str_list); + + return pos; + +} + diff -r 04cead6ae39b -r 9f3c698e34a7 gmyth/src/gmyth_recorder.h --- a/gmyth/src/gmyth_recorder.h Mon Nov 27 19:24:43 2006 +0000 +++ b/gmyth/src/gmyth_recorder.h Mon Nov 27 19:25:35 2006 +0000 @@ -90,6 +90,8 @@ gboolean gmyth_recorder_set_channel (GMythRecorder *recorder, gint channel); + +gint64 gmyth_recorder_get_file_position ( GMythRecorder *recorder ); G_END_DECLS diff -r 04cead6ae39b -r 9f3c698e34a7 gmyth/src/gmyth_socket.c --- a/gmyth/src/gmyth_socket.c Mon Nov 27 19:24:43 2006 +0000 +++ b/gmyth/src/gmyth_socket.c Mon Nov 27 19:25:35 2006 +0000 @@ -148,8 +148,8 @@ GList *local_addrs = NULL; - struct ifaddrs *ifaddr; - struct ifaddrs *i; + struct ifaddrs *ifaddr = g_new0( struct ifaddrs, 1 ); + struct ifaddrs *i = g_new0( struct ifaddrs, 1 ); gchar addr[NI_MAXHOST+1]; gchar *ifname;