diff -r 265cdb1c59e3 -r 7174e23f7617 gst-plugins-mythtv/src/myth_file_transfer.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gst-plugins-mythtv/src/myth_file_transfer.c Thu Sep 28 16:02:14 2006 +0100 @@ -0,0 +1,960 @@ +/* vim: set sw=2: -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2; c-indent-level: 2-*- */ +/** + * GStreamer 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 + * @author Rosfran Lins Borges + */ + +#include "myth_file_transfer.h" +#include "myth_uri.h" +#include "myth_livetv.h" +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +#define MYTHTV_QUERY_HEADER "QUERY_FILETRANSFER" +#define MYTHTV_RECORDER_HEADER "QUERY_RECORDER" + +/* default values to the file transfer parameters */ +#define MYTHTV_USER_READ_AHEAD FALSE +#define MYTHTV_RETRIES 1 +#define MYTHTV_FILE_SIZE -1 + +#define MYTHTV_BUFFER_SIZE 2048 + +#define MYTHTV_VERSION 30 + +#define MYTHTV_TRANSFER_MAX_WAITS 700 + +#ifdef MYTHTV_ENABLE_DEBUG +#define MYTHTV_ENABLE_DEBUG 1 +#else +#undef MYTHTV_ENABLE_DEBUG +#endif + +/* this NDEBUG is to maintain compatibility with GMyth library */ +#ifndef NDEBUG +#define MYTHTV_ENABLE_DEBUG 1 +#endif + +static guint wait_to_transfer = 0; + +enum myth_sock_types { + MYTH_PLAYBACK_TYPE = 0, + MYTH_MONITOR_TYPE, + MYTH_FILETRANSFER_TYPE, + MYTH_RINGBUFFER_TYPE +}; + +static GStaticMutex mutex = G_STATIC_MUTEX_INIT; + +static void myth_file_transfer_class_init (MythFileTransferClass *klass); +static void myth_file_transfer_init (MythFileTransfer *object); + +static void myth_file_transfer_dispose (GObject *object); +static void myth_file_transfer_finalize (GObject *object); + +static GMythSocket *myth_connect_to_transfer_backend( MythFileTransfer **transfer, guint sock_type ); +static void* myth_init_io_watchers( void *data ); + +void myth_file_transfer_close( MythFileTransfer *transfer ); + +G_DEFINE_TYPE(MythFileTransfer, myth_file_transfer, G_TYPE_OBJECT) + +static guint64 +mmyth_util_decode_long_long( GMythStringList *strlist, guint offset ) +{ + + guint64 ret_value = 0LL; + + g_return_val_if_fail( strlist != NULL, ret_value ); + + if ( offset < gmyth_string_list_length( strlist )) + g_printerr( "[%s] Offset is lower than the GMythStringList (offset = %d)!\n", __FUNCTION__, offset ); + g_return_val_if_fail( offset < gmyth_string_list_length( strlist ), ret_value ); + + gint l1 = gmyth_string_list_get_int( strlist, offset ); + gint l2 = gmyth_string_list_get_int( strlist, offset + 1 ); + + ret_value = ((guint64)(l2) & 0xffffffffLL) | ((guint64)(l1) << 32); + + return ret_value; + +} + +static void +myth_file_transfer_class_init (MythFileTransferClass *klass) +{ + GObjectClass *gobject_class; + + gobject_class = (GObjectClass *) klass; + + gobject_class->dispose = myth_file_transfer_dispose; + gobject_class->finalize = myth_file_transfer_finalize; +} + + static void +myth_file_transfer_init (MythFileTransfer *myth_file_transfer) +{ + g_return_if_fail( myth_file_transfer != NULL ); + myth_file_transfer->mythtv_version = MYTHTV_VERSION; +} + +static void +myth_file_transfer_dispose (GObject *object) +{ + MythFileTransfer *myth_file_transfer = MYTH_FILE_TRANSFER(object); + + myth_file_transfer_close( myth_file_transfer ); + + G_OBJECT_CLASS (myth_file_transfer_parent_class)->dispose (object); +} + + static void +myth_file_transfer_finalize (GObject *object) +{ + g_signal_handlers_destroy (object); + + G_OBJECT_CLASS (myth_file_transfer_parent_class)->finalize (object); +} + + MythFileTransfer* +myth_file_transfer_new (gint num, GString *uri_str, gshort port, gint mythtv_version) +{ + MythFileTransfer *transfer = MYTH_FILE_TRANSFER ( g_object_new ( + MYTH_FILE_TRANSFER_TYPE, FALSE )); + + if ( mythtv_version > 0 ) + transfer->mythtv_version = mythtv_version; + + transfer->card_id = num; + + transfer->rec_id = -1; + + transfer->recordernum = 0; + transfer->uri = myth_uri_new ( uri_str->str ); + + transfer->hostname = g_string_new( myth_uri_gethost(transfer->uri) ); + g_print( "\t--> transfer->hostname = %s\n", transfer->hostname->str ); + + if ( port >= 0 ) + transfer->port = port; + else + transfer->port = myth_uri_getport( transfer->uri ); + + g_print( "\t--> transfer->port = %d\n", transfer->port ); + + transfer->readposition = 0; + transfer->filesize = MYTHTV_FILE_SIZE; + transfer->timeoutisfast = FALSE; + + transfer->userreadahead = MYTHTV_USER_READ_AHEAD; + transfer->retries = MYTHTV_RETRIES; + + transfer->live_tv = FALSE; + + transfer->query = g_string_new( MYTHTV_QUERY_HEADER ); + g_string_append_printf ( transfer->query, " %d", transfer->recordernum ); + g_print( "\t--> transfer->query = %s\n", transfer->query->str ); + + transfer->control_sock = NULL; + transfer->event_sock = NULL; + transfer->sock = NULL; + + return transfer; +} + +gboolean +myth_file_transfer_livetv_setup( MythFileTransfer **transfer, GMythSocket *live_socket ) +{ + (*transfer)->sock = live_socket; + g_object_ref( live_socket ); + + return TRUE; +} + +gboolean +myth_file_transfer_playback_setup( MythFileTransfer **transfer, gboolean live_tv ) +{ + + gboolean ret = TRUE; + + (*transfer)->live_tv = live_tv; + + printf("[%s] Running config to the %s...\n", __FUNCTION__, live_tv ? "LiveTV" : "FileTransfer" ); + + /* configure the control socket */ + if ((*transfer)->control_sock == NULL) { + + if ( myth_connect_to_transfer_backend ( transfer, MYTH_PLAYBACK_TYPE ) == NULL ) { + g_printerr( "Connection to backend failed (Control Socket).\n" ); + ret = FALSE; + } + + } else { + g_warning("Remote transfer control socket already created.\n"); + } + + return ret; + +} + +gboolean +myth_file_transfer_setup( MythFileTransfer **transfer, gboolean live_tv ) +{ + GMythStringList *strlist = NULL; + + gboolean ret = TRUE; + + (*transfer)->live_tv = live_tv; + + printf("[%s] Running config to the %s...\n", __FUNCTION__, live_tv ? "LiveTV" : "FileTransfer" ); + +#if 0 + /* configure the event socket */ + if ((*transfer)->event_sock == NULL) { + + if ( myth_connect_to_transfer_backend ( transfer, MYTH_MONITOR_TYPE ) == NULL ) { + g_printerr( "Connection to backend failed (Event Socket).\n" ); + ret = FALSE; + } + + } else { + g_warning("Remote transfer control socket already created.\n"); + } +#endif + + /* configure the socket */ + if ( (*transfer)->sock == NULL ) { + + //if ( live_tv == FALSE ) { + + if ( myth_connect_to_transfer_backend ( transfer, MYTH_FILETRANSFER_TYPE ) == NULL ) { + g_printerr ("Connection to backend failed (Raw Transfer Socket).\n"); + ret = FALSE; + } + + if ( !(*transfer)->live_tv && (*transfer)->control_sock != NULL) { + strlist = gmyth_string_list_new(); + g_string_printf ( (*transfer)->query, "%s %d", MYTHTV_QUERY_HEADER, (*transfer)->recordernum ); + + gmyth_string_list_append_string( strlist, (*transfer)->query ); + gmyth_string_list_append_char_array( strlist, "IS_OPEN" ); + + gmyth_socket_write_stringlist( (*transfer)->control_sock, strlist ); + gmyth_socket_read_stringlist( (*transfer)->control_sock, strlist ); + + if ( strlist!=NULL && gmyth_string_list_get_int( strlist, 0 ) == 1 ) { + g_print( "[%s] Remote Myth FileTransfer socket is open!\n", __FUNCTION__ ); + } else { + g_print( "[%s] Remote Myth FileTransfer socket is CLOSED! See the MythTV Server Backend for configuration details...\n", __FUNCTION__ ); + ret = FALSE; + } + } + + } else { + g_warning("Remote transfer (raw) socket already created.\n"); + } + + return ret; +} + +static GMythSocket * +myth_connect_to_transfer_backend( MythFileTransfer **transfer, guint sock_type ) +{ + GMythSocket *sock = NULL; + + g_return_val_if_fail( transfer != NULL && *transfer != NULL, NULL ); + g_return_val_if_fail( (*transfer)->uri != NULL, NULL ); + + g_static_mutex_lock (&mutex); + + gchar *path_dir = myth_uri_getpath( (*transfer)->uri ); + //g_print( "\t--> %s: path_dir = %s\n", __FUNCTION__, path_dir ); + + gchar *stype = g_strdup( "" ); + + // if ( (*transfer)->live_tv == FALSE ) { + + sock = gmyth_socket_new(); + + gmyth_socket_connect( &sock, (*transfer)->hostname->str, (*transfer)->port ); + + /* + } else { + sock = (*transfer)->sock; + } + */ +#ifdef MYTHTV_ENABLE_DEBUG + + g_print( "[%s] --> Creating socket... (%s, %d)\n", __FUNCTION__, (*transfer)->hostname->str, (*transfer)->port ); +#endif + + GMythStringList *strlist = NULL; + + GString *hostname = g_string_new( myth_uri_gethost( (*transfer)->uri ) ); + GString *base_str = g_string_new( "" ); + + if ( gmyth_socket_check_protocol_version_number (sock, (*transfer)->mythtv_version) ) { + + if (sock == NULL) { + stype = (sock_type==MYTH_PLAYBACK_TYPE) ? "control socket" : "file data socket"; + g_printerr( "FileTransfer, open_socket(%s): \n" + "\t\t\tCould not connect to server \"%s\" @ port %d\n", stype, + (*transfer)->hostname->str, (*transfer)->port ); + g_object_unref(sock); + g_static_mutex_unlock (&mutex); + return NULL; + } + + hostname = gmyth_socket_get_local_hostname(); + + g_print( "[%s] local hostname = %s\n", __FUNCTION__, hostname->str ); + + if ( sock_type == MYTH_PLAYBACK_TYPE ) + { + (*transfer)->control_sock = sock; + g_string_printf( base_str, "ANN Playback %s %d", hostname->str, FALSE ); + + gmyth_socket_send_command( (*transfer)->control_sock, base_str ); + GString *resp = gmyth_socket_receive_response( (*transfer)->control_sock ); + g_print( "[%s] Got Playback response from %s: %s\n", __FUNCTION__, base_str->str, resp->str ); + } + else if ( sock_type == MYTH_MONITOR_TYPE ) + { + (*transfer)->event_sock = sock; + g_string_printf( base_str, "ANN Monitor %s %d", 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( myth_init_io_watchers, (void*)(*transfer), FALSE, NULL ); + + g_printerr( "[%s] Watch listener function to the IO control channel on thread %p.\n", __FUNCTION__, g_thread_self() ); + + } + else if ( sock_type == MYTH_FILETRANSFER_TYPE ) + { + (*transfer)->sock = sock; + strlist = gmyth_string_list_new(); + //g_string_printf( base_str, "ANN FileTransfer %s %d %d", hostname->str, + // transfer->userreadahead, transfer->retries ); + 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 ); + + gmyth_socket_write_stringlist( (*transfer)->sock, strlist ); + gmyth_socket_read_stringlist( (*transfer)->sock, strlist ); + + /* socket number, where all the stream data comes from - got from the MythTV remote backend */ + (*transfer)->recordernum = gmyth_string_list_get_int( strlist, 1 ); + + /* Myth URI stream file size - decoded using two 8-bytes sequences (64 bits/long long types) */ + (*transfer)->filesize = mmyth_util_decode_long_long( strlist, 2 ); + + printf( "[%s] ***** Received: recordernum = %d, filesize = %" G_GUINT64_FORMAT "\n", __FUNCTION__, + (*transfer)->recordernum, (*transfer)->filesize ); + + if ( (*transfer)->filesize <= 0 ) { + g_print( "[%s] Got filesize equals to %llu is lesser than 0 [invalid stream file]\n", __FUNCTION__, (*transfer)->filesize ); + g_object_unref(sock); + sock = NULL; + } + } + else if ( sock_type == MYTH_RINGBUFFER_TYPE ) + { + (*transfer)->sock = sock; + //myth_file_transfer_spawntv( (*transfer), NULL ); + + strlist = gmyth_string_list_new(); + g_string_printf( base_str, "ANN RingBuffer %s %d", hostname->str, (*transfer)->card_id ); + + gmyth_socket_send_command( (*transfer)->sock, base_str ); + GString *resp = gmyth_socket_receive_response( (*transfer)->sock ); + g_print( "[%s] Got RingBuffer response from %s: %s\n", __FUNCTION__, base_str->str, resp->str ); + + } + + } + + printf("[%s] ANN %s sent: %s\n", (sock_type==MYTH_PLAYBACK_TYPE) ? "Playback" : (sock_type==MYTH_FILETRANSFER_TYPE) ? "FileTransfer" : "Monitor", __FUNCTION__, base_str->str); + + if ( strlist != NULL ) + g_object_unref( strlist ); + + g_static_mutex_unlock (&mutex); + + return sock; +} + +void +myth_file_transfer_spawntv ( MythFileTransfer *file_transfer, + GString *tvchain_id ) +{ + GMythStringList *str_list; + + g_debug ("myth_file_transfer_spawntv.\n"); + + str_list = gmyth_string_list_new (); + + g_string_printf( file_transfer->query, "%s %d", MYTHTV_RECORDER_HEADER, + file_transfer->card_id ); + gmyth_string_list_append_string (str_list, file_transfer->query); + gmyth_string_list_append_string (str_list, g_string_new ("SPAWN_LIVETV")); + if (tvchain_id!=NULL) { + gmyth_string_list_append_string (str_list, tvchain_id); + gmyth_string_list_append_int (str_list, FALSE); // PIP = FALSE (0) + } + + gmyth_socket_sendreceive_stringlist ( file_transfer->sock, str_list ); + + //GString *str = NULL; + + //if (str_list!=NULL && (str = gmyth_string_list_get_string( str_list, 0 )) != NULL && strcasecmp( str->str, "ok" ) != 0 ) { + // g_print( "[%s]\t\tSpawnLiveTV is OK!\n", __FUNCTION__ ); + //} + if (str_list!=NULL) + g_object_unref (str_list); + +} + +gboolean +myth_file_transfer_is_recording ( MythFileTransfer *file_transfer ) +{ + gboolean ret = TRUE; + + GMythStringList *str_list = gmyth_string_list_new (); + + g_debug ( "[%s]\n", __FUNCTION__ ); + g_static_mutex_lock (&mutex); + + g_string_printf( file_transfer->query, "%s %d", MYTHTV_RECORDER_HEADER, + file_transfer->rec_id >= 0 ? file_transfer->rec_id : file_transfer->card_id ); + gmyth_string_list_append_string (str_list, file_transfer->query); + gmyth_string_list_append_string (str_list, g_string_new ("IS_RECORDING")); + + gmyth_socket_sendreceive_stringlist ( file_transfer->control_sock, 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 && strcmp( str->str, "bad" )!= 0 ) { + gint is_rec = gmyth_string_list_get_int( str_list, 0 ); + if ( is_rec != 0 ) + ret = TRUE; + else + ret = FALSE; + } + } + g_print( "[%s] %s, stream is %s being recorded!\n", __FUNCTION__, ret ? "YES" : "NO", ret ? "" : "NOT" ); + g_static_mutex_unlock (&mutex); + + if ( str_list != NULL ) + g_object_unref (str_list); + + return ret; + +} + +guint64 +myth_file_transfer_get_file_position ( MythFileTransfer *file_transfer ) +{ + guint64 pos = 0; + + GMythStringList *str_list = gmyth_string_list_new (); + + g_debug ( "[%s]\n", __FUNCTION__ ); + g_static_mutex_lock (&mutex); + + g_string_printf( file_transfer->query, "%s %d", MYTHTV_RECORDER_HEADER, + file_transfer->rec_id >= 0 ? file_transfer->rec_id : file_transfer->card_id ); + + gmyth_string_list_append_string (str_list, file_transfer->query); + gmyth_string_list_append_string (str_list, g_string_new ("GET_FILE_POSITION")); + + gmyth_socket_sendreceive_stringlist ( file_transfer->control_sock, 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 && strcmp ( str->str, "bad" ) != 0 ) + pos = gmyth_util_decode_long_long( str_list, 0 ); + } + g_static_mutex_unlock (&mutex); + +#ifndef MYTHTV_ENABLE_DEBUG + + g_print( "[%s] Got file position = %llu\n", __FUNCTION__, pos ); +#endif + if (str_list!=NULL) + g_object_unref (str_list); + + return pos; + +} + + glong +myth_file_transfer_get_recordernum( MythFileTransfer *transfer ) +{ + return transfer->recordernum; +} + + glong +myth_file_transfer_get_filesize( MythFileTransfer *transfer ) +{ + return transfer->filesize; +} + + gboolean +myth_file_transfer_isopen( MythFileTransfer *transfer ) +{ + return (transfer->sock != NULL && transfer->control_sock != NULL); +} + + void +myth_file_transfer_close( MythFileTransfer *transfer ) +{ + GMythStringList *strlist; + + if (transfer->control_sock == NULL) + return; + + strlist = gmyth_string_list_new( ); + + g_string_printf( transfer->query, "%s %d", MYTHTV_QUERY_HEADER, + transfer->recordernum ); + gmyth_string_list_append_string( strlist, transfer->query ); + gmyth_string_list_append_char_array( strlist, "DONE" ); + + + if ( gmyth_socket_sendreceive_stringlist(transfer->control_sock, strlist) <= 0 ) + { + g_printerr( "Remote file timeout.\n" ); + } + + if (transfer->sock) + { + g_object_unref( transfer->sock ); + transfer->sock = NULL; + } + + if (transfer->control_sock) + { + g_object_unref( transfer->control_sock ); + transfer->control_sock = NULL; + } + +} + + void +myth_file_transfer_reset_controlsock( MythFileTransfer *transfer ) +{ + if (transfer->control_sock == NULL) + { + g_printerr( "myth_file_transfer_reset_controlsock(): Called with no control socket" ); + return; + } + + GString *str = gmyth_socket_receive_response( transfer->control_sock ); + + g_string_free( str, TRUE ); +} + +void +myth_file_transfer_reset_sock( MythFileTransfer *transfer ) +{ + if ( transfer->sock == NULL ) + { + g_printerr( "myth_file_transfer_reset_sock(): Called with no raw socket" ); + return; + } + + GString *str = gmyth_socket_receive_response( transfer->sock ); + + g_string_free( str, TRUE ); +} + +void +myth_file_transfer_reset( MythFileTransfer *transfer ) +{ + myth_file_transfer_reset_controlsock( transfer ); + myth_file_transfer_reset_sock( transfer ); +} + +guint64 +myth_file_transfer_seek(MythFileTransfer *transfer, guint64 pos, gint whence) +{ + if (transfer->sock == NULL) + { + g_printerr( "[%s] myth_file_transfer_seek(): Called with no socket", __FUNCTION__ ); + return 0; + } + + if (transfer->control_sock == NULL) + return 0; + + // if (!controlSock->isOpen() || controlSock->error()) + // return 0; + + GMythStringList *strlist = gmyth_string_list_new(); + g_string_printf (transfer->query, "%s %d", MYTHTV_QUERY_HEADER, transfer->recordernum); + gmyth_string_list_append_string( strlist, transfer->query ); + gmyth_string_list_append_char_array( strlist, "SEEK" ); + gmyth_string_list_append_uint64( strlist, pos ); + // gmyth_string_list_append_int( strlist, whence ); + + if (pos > 0 ) + gmyth_string_list_append_uint64( strlist, pos ); + else + gmyth_string_list_append_uint64( strlist, transfer->readposition ); + + gmyth_socket_sendreceive_stringlist( transfer->control_sock, strlist ); + + guint64 retval = gmyth_string_list_get_uint64(strlist, 0); + transfer->readposition = retval; + g_print( "[%s] got reading position pointer from the streaming = %llu\n", + __FUNCTION__, retval ); + + //myth_file_transfer_reset( transfer ); + + return retval; +} + +static gboolean +myth_control_sock_listener( GIOChannel *source, GIOCondition condition, gpointer data ) +{ + + GIOStatus ret; + GError *err = NULL; + gchar *msg = g_strdup(""); + + gsize len; + if (condition & G_IO_HUP) + g_error ("Read end of pipe died!\n"); + ret = g_io_channel_read_line ( source, &msg, &len, NULL, &err); + if ( ret == G_IO_STATUS_ERROR ) + g_error ("[%s] Error reading: %s\n", __FUNCTION__, err != NULL ? err->message : "" ); + g_print ("\n\n\n\n\n\n[%s]\t\tEVENT: Read %u bytes: %s\n\n\n\n\n", __FUNCTION__, len, msg != NULL ? msg : "" ); + if ( msg != NULL ) + g_free (msg); + + return TRUE; + +} + +static void* +myth_init_io_watchers( void *data ) +{ + MythFileTransfer *transfer = (MythFileTransfer*)data; + GMainContext *context = g_main_context_new(); + GMainLoop *loop = g_main_loop_new( NULL, FALSE ); + + GSource *source = NULL; + + if ( transfer->event_sock->sd_io_ch != NULL ) + source = g_io_create_watch( transfer->event_sock->sd_io_ch, G_IO_IN | G_IO_HUP ); + + g_source_set_callback ( source, (GSourceFunc)myth_control_sock_listener, NULL, NULL ); + + g_source_attach( source, context ); + + if (source==NULL) + g_printerr( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ ); + + g_main_loop_run( loop ); + + g_source_unref( source ); + + g_main_loop_unref( loop ); + + g_main_context_unref( context ); + + return NULL; +} + + gint +myth_file_transfer_read(MythFileTransfer *transfer, void *data, gint size, gboolean read_unlimited) +{ + gint recv = 0; + gsize bytes_read = 0; + + gint sent = 0; + //guint zerocnt = 0; + gboolean response = FALSE; + + GIOChannel *io_channel; + GIOChannel *io_channel_control; + + GIOCondition io_cond; + GIOCondition io_cond_control; + GIOStatus io_status = G_IO_STATUS_NORMAL, io_status_control = G_IO_STATUS_NORMAL; + + gint buf_len = MYTHTV_BUFFER_SIZE; + + GMythStringList *strlist = NULL; + GError *error = NULL; + + gchar *trash = g_strdup(""); + + g_return_val_if_fail ( data != NULL, -2 ); + + /* gets the size of the entire file, if the size requested is lesser than 0 */ + if ( size <= 0 ) + size = transfer->filesize; + + io_channel = transfer->sock->sd_io_ch; + io_channel_control = transfer->control_sock->sd_io_ch; + + //g_io_channel_set_flags( io_channel, G_IO_FLAG_APPEND | + // G_IO_STATUS_AGAIN | G_IO_FLAG_IS_READABLE | G_IO_FLAG_IS_WRITEABLE | + // G_IO_FLAG_IS_SEEKABLE, NULL ); + + io_status = g_io_channel_set_encoding( io_channel, NULL, &error ); + if ( io_status == G_IO_STATUS_NORMAL ) + g_print( "[%s] Setting encoding to binary data socket).\n", __FUNCTION__ ); + + io_cond = g_io_channel_get_buffer_condition( io_channel ); + + io_cond_control = g_io_channel_get_buffer_condition( io_channel ); + + if ( transfer->sock == NULL || ( io_status == G_IO_STATUS_ERROR ) ) + { + g_printerr( "myth_file_transfer_read(): Called with no raw socket.\n" ); + recv = -1; + goto cleanup; + } + + if ( transfer->control_sock == NULL || ( io_status_control == G_IO_STATUS_ERROR ) ) + { + g_printerr( "myth_file_transfer_read(): Called with no control socket.\n" ); + recv = -1; + goto cleanup; + } + + /* + if (!controlSock->isOpen() || controlSock->error()) + return -1; + */ + + if ( ( io_cond & G_IO_IN ) != 0 ) { + do + { + + io_status = g_io_channel_read_line( io_channel, &trash, &bytes_read, NULL, &error); + + g_print( "[%s] cleaning buffer on IO binary channel... (%s)\n", __FUNCTION__, trash ); + io_cond = g_io_channel_get_buffer_condition( io_channel ); + + } while ( ( io_cond & G_IO_IN ) != 0 && ( io_status != G_IO_STATUS_ERROR ) ); + + if ( trash!= NULL ) + g_free( trash ); + } + + if ( ( io_cond_control & G_IO_IN ) != 0 ) { + GMythStringList *strlist_tmp = gmyth_string_list_new(); + gmyth_socket_read_stringlist( transfer->control_sock, strlist_tmp ); + g_object_unref( strlist_tmp ); + } + + wait_to_transfer = 0; + + while ( transfer->live_tv && ( myth_file_transfer_get_file_position( transfer ) < 4096 ) && + wait_to_transfer++ < MYTHTV_TRANSFER_MAX_WAITS ) + g_usleep( 1000*50 ); /* waits just for 2/10 second */ + + //g_thread_create( myth_init_io_watchers, (void*)transfer, FALSE, NULL ); + //g_printerr( "[%s] Watch listener function to the IO control channel on thread %p.\n", __FUNCTION__, g_thread_self() ); + + g_static_mutex_lock (&mutex); + strlist = gmyth_string_list_new(); + + g_string_printf ( transfer->query, "%s %d", /*transfer->live_tv ? MYTHTV_RECORDER_HEADER :*/ MYTHTV_QUERY_HEADER, + /* transfer->live_tv ? transfer->card_id :*/ transfer->recordernum ); // transfer->recordernum + g_print( "\t[%s] Transfer_query = %s\n", __FUNCTION__, transfer->query->str ); + strlist = gmyth_string_list_new(); + + gmyth_string_list_append_string( strlist, transfer->query ); + gmyth_string_list_append_char_array( strlist, /*transfer->live_tv ? "REQUEST_BLOCK_RINGBUF" :*/ "REQUEST_BLOCK" ); + gmyth_string_list_append_int( strlist, size ); + + gmyth_socket_write_stringlist( transfer->control_sock, strlist ); + sent = size; + + //data = (void*)g_new0( gchar, size ); + + g_io_channel_flush( io_channel_control, NULL ); +// g_io_channel_flush( io_channel, NULL ); + + io_cond = g_io_channel_get_buffer_condition( io_channel ); + + while ( ( recv < sent ) )//&& ( io_cond & G_IO_IN ) != 0 ) + { + do + { + //while ( ( io_cond & G_IO_IN ) == 0 ) { + //usleep(200); + // + //io_cond = g_io_channel_get_buffer_condition( io_channel ); + // + + buf_len = ( sent - recv ) > MYTHTV_BUFFER_SIZE ? MYTHTV_BUFFER_SIZE : ( sent - recv ); + + io_status = g_io_channel_read_chars( io_channel, data + recv, + buf_len, &bytes_read, &error ); + /* + GString *sss = g_string_new(""); + sss = g_string_append_len( sss, (gchar*)data+recv, bytes_read ); + + g_print( "[%s] Reading buffer (length = %d)\n", __FUNCTION__, bytes_read); + */ + if ( bytes_read > 0 ) + { + if ( bytes_read <= buf_len ) + recv += bytes_read; + } + + if ( io_status == G_IO_STATUS_EOF ) { + g_printerr( "[%s] got EOS!", __FUNCTION__ ); + break; + } else if ( io_status == G_IO_STATUS_ERROR ) { + g_printerr( "[%s] myth_file_transfer_read(): socket error.\n", __FUNCTION__ ); + break; + } + + /* increase buffer size, to allow get more data (do not obey to the buffer size) */ + if ( read_unlimited == TRUE ) { + //if ( recv > buf_len ) + // sent += (bytes_read - buf_len) + 1; + } + if ( bytes_read == buf_len ) + break; + + /* verify if the input (read) buffer is ready to receive data */ + io_cond = g_io_channel_get_buffer_condition( io_channel ); + + g_print( "[%s]\t io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, + ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" ); + + } while ( recv < sent && ( ( io_cond & G_IO_IN ) != 0 ) && ( io_status == G_IO_STATUS_NORMAL ) ); + + io_cond_control = g_io_channel_get_buffer_condition( io_channel_control ); + if ( ( io_status == G_IO_STATUS_EOF ) || ( ( io_cond_control & G_IO_IN ) != 0 ) ) + { + gmyth_socket_read_stringlist( transfer->control_sock, strlist ); + sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error + g_print( "[%s]\t sent = %d, io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, + sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" ); + response = TRUE; + } + } + + if ( ( ( error == NULL ) && ( response == FALSE ) ) || + ( io_status == G_IO_STATUS_EOF ) || ( ( io_cond & G_IO_IN ) == 0 ) ) + { + if ( gmyth_socket_read_stringlist( transfer->control_sock, strlist ) > 0 ) + { + if ( strlist != NULL && gmyth_string_list_length(strlist) > 0 ) { + sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error + g_print( "[%s]\t sent = %d, io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, + sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" ); + } + } + else + { + g_printerr ( "myth_file_transfer_read(): No response from control socket."); + sent = -1; + } + + if ( error!=NULL ) { + g_printerr( "[%s] Error occurred: (%d, %s)\n", __FUNCTION__, error->code, error->message ); + g_error_free( error ); + } + } + +cleanup: + + if ( trash != NULL ) + g_free( trash ); + + if ( strlist != NULL ) + g_object_unref( strlist ); + + g_static_mutex_unlock (&mutex); + g_print( "myth_file_transfer_read(): reqd=%d, rcvd=%d, rept=%d, "\ + "(rcvd and rept MUST be the same!)\n", size, + recv, sent ); + + //if ( sent != recv ) { + // recv = -1; + //} + + if ( error != NULL ) + g_printerr( "ERROR: %s [msg = %s, code = %d]\n", __FUNCTION__, error->message, + error->code ); + + return recv; +} + + void +myth_file_transfer_settimeout( MythFileTransfer *transfer, gboolean fast ) +{ + + GMythStringList *strlist = NULL; + + if ( transfer->timeoutisfast == fast ) + return; + + if ( transfer->sock == NULL ) + { + g_printerr( "myth_file_transfer_settimeout(): Called with no socket" ); + return; + } + + if ( transfer->control_sock == NULL ) + return; + + strlist = gmyth_string_list_new(); + gmyth_string_list_append_string( strlist, transfer->query ); + gmyth_string_list_append_char_array( strlist, "SET_TIMEOUT" ); + gmyth_string_list_append_int( strlist, fast ); + + gmyth_socket_write_stringlist( transfer->control_sock, strlist ); + gmyth_socket_read_stringlist( transfer->control_sock, strlist ); + + transfer->timeoutisfast = fast; + +} + +#ifdef DO_TESTING + + int +main( int argc, char *argv[] ) +{ + g_type_init(); + + MythFileTransfer *file_transfer = myth_file_transfer_new( 1, + g_string_new("myth://192.168.1.109:6543/jshks.nuv"), -1, MYTHTV_VERSION ); + myth_file_transfer_setup( &file_transfer ); + gchar *data = g_strdup(""); + + gint num = myth_file_transfer_read( file_transfer, data, -1 ); + + return 0; + +} + +#endif