branches/gmyth-0.1b/src/gmyth_file_transfer.c
branchtrunk
changeset 366 8aa32fa19a8f
parent 365 28c358053693
child 367 839c0fc4f285
     1.1 --- a/branches/gmyth-0.1b/src/gmyth_file_transfer.c	Wed Feb 14 23:06:17 2007 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,705 +0,0 @@
     1.4 -/**
     1.5 - * GMyth Library
     1.6 - *
     1.7 - * @file gmyth/gmyth_file_transfer.c
     1.8 - * 
     1.9 - * @brief <p> GMythFileTransfer deals with the file streaming media remote/local
    1.10 - * transfering to the MythTV frontend.
    1.11 - *
    1.12 - * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
    1.13 - * @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
    1.14 - *
    1.15 - *//*
    1.16 - * 
    1.17 - * This program is free software; you can redistribute it and/or modify
    1.18 - * it under the terms of the GNU Lesser General Public License as published by
    1.19 - * the Free Software Foundation; either version 2 of the License, or
    1.20 - * (at your option) any later version.
    1.21 - *
    1.22 - * This program is distributed in the hope that it will be useful,
    1.23 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.24 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.25 - * GNU General Public License for more details.
    1.26 - *
    1.27 - * You should have received a copy of the GNU Lesser General Public License
    1.28 - * along with this program; if not, write to the Free Software
    1.29 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.30 - *
    1.31 - * GStreamer MythTV plug-in properties:
    1.32 - * - location (backend server hostname/URL) [ex.: myth://192.168.1.73:28722/1000_1092091.nuv]
    1.33 - * - path (qurl - remote file to be opened)
    1.34 - * - port number *   
    1.35 - */
    1.36 - 
    1.37 -#ifdef HAVE_CONFIG_H
    1.38 -#include "config.h"
    1.39 -#endif
    1.40 -
    1.41 -#include "gmyth_file_transfer.h"
    1.42 -#include "gmyth_livetv.h"
    1.43 -#include "gmyth_util.h"
    1.44 -#include "gmyth_socket.h"
    1.45 -#include "gmyth_stringlist.h"
    1.46 -#include "gmyth_debug.h"
    1.47 -#include "gmyth_uri.h"
    1.48 -#include "gmyth_marshal.h"
    1.49 -
    1.50 -#include <unistd.h>
    1.51 -#include <glib.h>
    1.52 -
    1.53 -#include <arpa/inet.h>
    1.54 -#include <sys/types.h>
    1.55 -#include <sys/socket.h>
    1.56 -#include <netdb.h>
    1.57 -#include <errno.h>
    1.58 -#include <stdlib.h>
    1.59 -#include <assert.h>
    1.60 -
    1.61 -#define GMYTHTV_QUERY_HEADER		"QUERY_FILETRANSFER "
    1.62 -
    1.63 -/* default values to the file transfer parameters */
    1.64 -#define GMYTHTV_USER_READ_AHEAD			TRUE
    1.65 -#define GMYTHTV_RETRIES							-1
    1.66 -#define GMYTHTV_FILE_SIZE						0
    1.67 -
    1.68 -#define GMYTHTV_BUFFER_SIZE					64*1024
    1.69 -
    1.70 -#define GMYTHTV_VERSION							30
    1.71 -
    1.72 -#define GMYTHTV_TRANSFER_MAX_WAITS	700
    1.73 -
    1.74 -#ifdef GMYTHTV_ENABLE_DEBUG
    1.75 -#define GMYTHTV_ENABLE_DEBUG				1
    1.76 -#else
    1.77 -#undef GMYTHTV_ENABLE_DEBUG
    1.78 -#endif
    1.79 -
    1.80 -/* this NDEBUG is to maintain compatibility with GMyth library */
    1.81 -#ifndef NDEBUG
    1.82 -#define GMYTHTV_ENABLE_DEBUG				1
    1.83 -#endif
    1.84 -
    1.85 -enum myth_sock_types {
    1.86 -  GMYTH_PLAYBACK_TYPE = 0,
    1.87 -  GMYTH_MONITOR_TYPE,
    1.88 -  GMYTH_FILETRANSFER_TYPE,
    1.89 -  GMYTH_RINGBUFFER_TYPE
    1.90 -};
    1.91 -
    1.92 -#define GMYTH_FILE_TRANSFER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GMYTH_FILE_TRANSFER_TYPE, GMythFileTransferPrivate))
    1.93 -
    1.94 -struct _GMythFileTransferPrivate {
    1.95 -	
    1.96 -	GMythLiveTV *livetv;
    1.97 -	
    1.98 -	gboolean do_next_program_chain;
    1.99 -	
   1.100 -};
   1.101 -
   1.102 -static void gmyth_file_transfer_class_init          (GMythFileTransferClass *klass);
   1.103 -static void gmyth_file_transfer_init                (GMythFileTransfer *object);
   1.104 -
   1.105 -static void gmyth_file_transfer_dispose  (GObject *object);
   1.106 -static void gmyth_file_transfer_finalize (GObject *object);
   1.107 -
   1.108 -static void gmyth_file_transfer_program_info_changed( GMythFileTransfer *transfer, 
   1.109 -										gint msg_code, gpointer livetv_transfer );
   1.110 -
   1.111 -static gboolean gmyth_connect_to_backend (GMythFileTransfer *transfer);
   1.112 -
   1.113 -void gmyth_file_transfer_close( GMythFileTransfer *transfer );
   1.114 -
   1.115 -static gboolean myth_control_acquire_context( gboolean do_wait );
   1.116 -
   1.117 -static gboolean myth_control_release_context( );
   1.118 -
   1.119 -G_DEFINE_TYPE(GMythFileTransfer, gmyth_file_transfer, G_TYPE_OBJECT)
   1.120 -
   1.121 -static void
   1.122 -gmyth_file_transfer_class_init (GMythFileTransferClass *klass)
   1.123 -{
   1.124 -  GObjectClass *gobject_class;
   1.125 -  GMythFileTransferClass *gtransfer_class;
   1.126 -
   1.127 -  gobject_class = (GObjectClass *) klass;
   1.128 -  gtransfer_class = (GMythFileTransferClass *) gobject_class;
   1.129 -
   1.130 -  gobject_class->dispose  = gmyth_file_transfer_dispose;
   1.131 -  gobject_class->finalize = gmyth_file_transfer_finalize;
   1.132 -  
   1.133 -  g_type_class_add_private (gobject_class, sizeof (GMythFileTransferPrivate));
   1.134 -  
   1.135 -	gtransfer_class->program_info_changed_handler = gmyth_file_transfer_program_info_changed;
   1.136 -  
   1.137 -	gtransfer_class->program_info_changed_handler_signal_id = 
   1.138 -		  g_signal_new ( "program-info-changed",
   1.139 -		                 G_TYPE_FROM_CLASS (gtransfer_class),
   1.140 -		                 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
   1.141 -		                 0,
   1.142 -										 NULL,
   1.143 -										 NULL,
   1.144 -										 gmyth_marshal_VOID__INT_POINTER,
   1.145 -										 G_TYPE_NONE,
   1.146 -										 2,
   1.147 -										 G_TYPE_INT,
   1.148 -										 G_TYPE_POINTER );
   1.149 -
   1.150 -}
   1.151 -
   1.152 -static void
   1.153 -gmyth_file_transfer_init (GMythFileTransfer *transfer)
   1.154 -{ 
   1.155 -  g_return_if_fail( transfer != NULL );
   1.156 -
   1.157 -  transfer->readposition = 0;
   1.158 -  transfer->filesize = GMYTHTV_FILE_SIZE;
   1.159 -  
   1.160 -  transfer->priv = GMYTH_FILE_TRANSFER_GET_PRIVATE(transfer);	
   1.161 -  
   1.162 -  transfer->priv->do_next_program_chain = FALSE;
   1.163 -  transfer->priv->livetv = NULL;
   1.164 -
   1.165 -  transfer->control_sock = NULL;
   1.166 -  transfer->sock = NULL;
   1.167 -  
   1.168 -  transfer->mutex = g_mutex_new();
   1.169 -  
   1.170 -	g_signal_connect ( G_OBJECT (transfer), "program-info-changed",
   1.171 -              (GCallback)(GMYTH_FILE_TRANSFER_GET_CLASS(transfer)->program_info_changed_handler),
   1.172 -              NULL );
   1.173 -
   1.174 -}
   1.175 -
   1.176 -static void
   1.177 -gmyth_file_transfer_dispose  (GObject *object)
   1.178 -{
   1.179 -	GMythFileTransfer *transfer = GMYTH_FILE_TRANSFER (object);
   1.180 -	
   1.181 -  if ( transfer->mutex != NULL )  
   1.182 -  {
   1.183 -  	g_mutex_free( transfer->mutex );
   1.184 -  	transfer->mutex = NULL;
   1.185 -  }
   1.186 -  
   1.187 -  if ( transfer->control_sock != NULL )  
   1.188 -  {
   1.189 -  	g_object_unref( transfer->control_sock );
   1.190 -  	transfer->control_sock = NULL;
   1.191 -  }  
   1.192 -  
   1.193 -  if ( transfer->sock != NULL )  
   1.194 -  {
   1.195 -  	g_object_unref( transfer->sock );
   1.196 -  	transfer->sock = NULL;
   1.197 -  }
   1.198 -    
   1.199 -  if ( transfer->filename != NULL )  
   1.200 -  {
   1.201 -  	g_free( transfer->filename );
   1.202 -  	transfer->filename = NULL;
   1.203 -  }
   1.204 -
   1.205 -  G_OBJECT_CLASS (gmyth_file_transfer_parent_class)->dispose (object);
   1.206 -}
   1.207 -
   1.208 -static void
   1.209 -gmyth_file_transfer_finalize (GObject *object)
   1.210 -{
   1.211 -  g_signal_handlers_destroy (object);
   1.212 -
   1.213 -  G_OBJECT_CLASS (gmyth_file_transfer_parent_class)->finalize (object);
   1.214 -}
   1.215 -
   1.216 -// fixme: do we need the card_id????
   1.217 -GMythFileTransfer*
   1.218 -gmyth_file_transfer_new ( const GMythBackendInfo *backend_info)
   1.219 -{
   1.220 -  GMythFileTransfer *transfer = GMYTH_FILE_TRANSFER (g_object_new (GMYTH_FILE_TRANSFER_TYPE, NULL));
   1.221 -  
   1.222 -  transfer->backend_info = (GMythBackendInfo *)backend_info;
   1.223 -  g_object_ref (transfer->backend_info);
   1.224 -
   1.225 -  return transfer;
   1.226 -}
   1.227 -
   1.228 -GMythFileTransfer* 
   1.229 -gmyth_file_transfer_new_with_uri (const gchar* uri_str)
   1.230 -{
   1.231 -  GMythFileTransfer *transfer = GMYTH_FILE_TRANSFER (g_object_new (GMYTH_FILE_TRANSFER_TYPE, NULL));
   1.232 -
   1.233 -  transfer->backend_info = gmyth_backend_info_new_with_uri (uri_str);
   1.234 -
   1.235 -  return transfer;
   1.236 -}
   1.237 -
   1.238 -gboolean
   1.239 -gmyth_file_transfer_open (GMythFileTransfer *transfer, const gchar* filename)
   1.240 -{
   1.241 -  gboolean ret = TRUE;
   1.242 -  
   1.243 -  g_return_val_if_fail (transfer != NULL, FALSE);
   1.244 -  g_return_val_if_fail (filename != NULL, FALSE);
   1.245 -  
   1.246 -  if (transfer->filename != NULL)
   1.247 -  {
   1.248 -    g_free (transfer->filename);
   1.249 -    transfer->filename = NULL;
   1.250 -  }
   1.251 -
   1.252 -  transfer->filename = g_strdup (filename);
   1.253 -  
   1.254 -  /* configure the control socket */
   1.255 -  if (transfer->control_sock == NULL) { 
   1.256 -    if (!gmyth_connect_to_backend (transfer)) {
   1.257 -      gmyth_debug ("Connection to backend failed (Control Socket).\n");
   1.258 -      ret = FALSE;
   1.259 -    }
   1.260 -  } else {
   1.261 -    g_warning("Remote transfer control socket already created.\n");
   1.262 -  }
   1.263 -  
   1.264 -  gmyth_debug ("Got file with size = %lld.\n", transfer->filesize);
   1.265 -  
   1.266 -  return ret;
   1.267 -
   1.268 -}
   1.269 -
   1.270 -static gboolean
   1.271 -gmyth_connect_to_backend (GMythFileTransfer *transfer)
   1.272 -{
   1.273 -  GString *base_str = NULL;
   1.274 -  GString *hostname = NULL;
   1.275 -  GMythStringList *strlist = NULL; 
   1.276 -  gboolean ret = TRUE;
   1.277 -
   1.278 -  g_return_val_if_fail (transfer != NULL, FALSE );
   1.279 -
   1.280 -  base_str = g_string_new ("");
   1.281 -
   1.282 -  /* Creates the control socket */
   1.283 -  if (transfer->control_sock != NULL) {
   1.284 -    g_object_unref (transfer->control_sock);
   1.285 -    transfer->control_sock = NULL;
   1.286 -  }
   1.287 -
   1.288 -  transfer->control_sock = gmyth_socket_new();
   1.289 -
   1.290 -  // Connects the socket, send Mythtv ANN command and verify Mythtv protocol version 
   1.291 -  if (!gmyth_socket_connect_to_backend (transfer->control_sock,
   1.292 -        transfer->backend_info->hostname, transfer->backend_info->port, TRUE)) {
   1.293 -
   1.294 -    g_object_unref (transfer->control_sock);
   1.295 -    transfer->control_sock = NULL;
   1.296 -    return FALSE;
   1.297 -  }
   1.298 -    
   1.299 -  /* Creates the data socket */
   1.300 -  if (transfer->sock != NULL) {
   1.301 -    g_object_unref (transfer->sock);
   1.302 -    transfer->sock = NULL;
   1.303 -  }
   1.304 -
   1.305 -  transfer->sock = gmyth_socket_new ();
   1.306 -  gmyth_socket_connect (transfer->sock, transfer->backend_info->hostname, transfer->backend_info->port);
   1.307 -
   1.308 -  strlist = gmyth_string_list_new();
   1.309 -  hostname = gmyth_socket_get_local_hostname();
   1.310 -  gmyth_debug( "[%s] MythTV version (from backend) = %d.\n", __FUNCTION__, transfer->control_sock->mythtv_version );
   1.311 -  if ( transfer->control_sock->mythtv_version > 26 )
   1.312 -  	g_string_printf( base_str, "ANN FileTransfer %s 1 -1", hostname->str);
   1.313 -  else
   1.314 -  	g_string_printf( base_str, "ANN FileTransfer %s", hostname->str);
   1.315 -
   1.316 -  gmyth_string_list_append_string (strlist, base_str );
   1.317 -  gmyth_string_list_append_char_array (strlist, transfer->filename);
   1.318 -
   1.319 -  gmyth_socket_write_stringlist (transfer->sock, strlist );
   1.320 -  gmyth_socket_read_stringlist (transfer->sock, strlist );
   1.321 -    
   1.322 -  /* file identification used in future file transfer requests to backend */
   1.323 -  transfer->file_id = gmyth_string_list_get_int( strlist, 1 );
   1.324 -
   1.325 -  /* Myth URI stream file size - decoded using two 8-bytes sequences (64 bits/long long types) */
   1.326 -  transfer->filesize = gmyth_util_decode_long_long( strlist, 2 );
   1.327 -
   1.328 -  gmyth_debug ( "[%s] ***** Received: recordernum = %d, filesize = %" G_GUINT64_FORMAT "\n", __FUNCTION__,
   1.329 -          transfer->file_id, transfer->filesize );
   1.330 -
   1.331 -  if (transfer->filesize < 0 ) {
   1.332 -      gmyth_debug ( "[%s] Got filesize equals to %llu is lesser than 0 [invalid stream file]\n", __FUNCTION__, transfer->filesize );
   1.333 -      g_object_unref (transfer->sock);
   1.334 -      transfer->sock = NULL;
   1.335 -      ret = FALSE;
   1.336 -      goto cleanup;
   1.337 -  }
   1.338 -  
   1.339 -cleanup:
   1.340 -
   1.341 -  if ( strlist != NULL )
   1.342 -    g_object_unref( strlist );
   1.343 -
   1.344 -  g_string_free (base_str, TRUE);
   1.345 -  g_string_free (hostname, TRUE);
   1.346 -
   1.347 -  return ret;
   1.348 -}    
   1.349 -
   1.350 -void
   1.351 -gmyth_file_transfer_emit_program_info_changed_signal ( GMythFileTransfer *transfer, gint msg_code,
   1.352 -			gpointer live_tv ) 
   1.353 -{
   1.354 -				/*
   1.355 -	g_signal_emit_by_name ( G_OBJECT(transfer),
   1.356 -         "program-info-changed",
   1.357 -         msg_code, live_tv );
   1.358 -         */
   1.359 -         
   1.360 -  gmyth_debug( "Calling signal handler... [FILE_TRANSFER]" );
   1.361 -
   1.362 -  g_signal_emit ( transfer,
   1.363 -         GMYTH_FILE_TRANSFER_GET_CLASS (transfer)->program_info_changed_handler_signal_id,
   1.364 -         0, /* details */
   1.365 -         msg_code, live_tv );
   1.366 -
   1.367 -}
   1.368 -
   1.369 -gboolean
   1.370 -gmyth_file_transfer_is_open (GMythFileTransfer *transfer)
   1.371 -{
   1.372 -    GMythStringList *strlist;
   1.373 -    GString *query;
   1.374 -
   1.375 -    g_return_val_if_fail (transfer->control_sock != NULL, FALSE);
   1.376 -    g_return_val_if_fail (transfer->sock != NULL, FALSE);
   1.377 -
   1.378 -    strlist = gmyth_string_list_new();
   1.379 -    query = g_string_new (GMYTHTV_QUERY_HEADER);
   1.380 -    g_string_append_printf (query, "%d", transfer->file_id );
   1.381 -
   1.382 -    gmyth_string_list_append_string (strlist, query );
   1.383 -    gmyth_string_list_append_char_array( strlist, "IS_OPEN" );
   1.384 -
   1.385 -    gmyth_socket_write_stringlist( transfer->control_sock, strlist );
   1.386 -    gmyth_socket_read_stringlist( transfer->control_sock, strlist );
   1.387 -
   1.388 -    g_string_free (query, TRUE);
   1.389 -    g_object_unref (strlist);
   1.390 -
   1.391 -    return ( strlist != NULL && gmyth_string_list_get_int( strlist, 0 ) == 1 );
   1.392 -}
   1.393 -
   1.394 -void
   1.395 -gmyth_file_transfer_close( GMythFileTransfer *transfer )
   1.396 -{
   1.397 -  GMythStringList *strlist;
   1.398 -  GString *query;
   1.399 -
   1.400 -  g_return_if_fail (transfer->control_sock != NULL);
   1.401 -
   1.402 -  strlist = gmyth_string_list_new( );
   1.403 -  query = g_string_new (GMYTHTV_QUERY_HEADER);
   1.404 -  g_string_append_printf( query, "%d", transfer->file_id);
   1.405 -
   1.406 -  gmyth_string_list_append_string( strlist, query );
   1.407 -  gmyth_string_list_append_char_array( strlist, "DONE" );
   1.408 -
   1.409 -  if ( gmyth_socket_sendreceive_stringlist(transfer->control_sock, strlist) <= 0 ) {
   1.410 -    // fixme: time out???
   1.411 -    g_printerr( "Remote file timeout.\n" );
   1.412 -  }
   1.413 -
   1.414 -  g_string_free (query, TRUE);
   1.415 -  g_object_unref (strlist);
   1.416 -
   1.417 -  if (transfer->sock) {
   1.418 -    g_object_unref( transfer->sock );
   1.419 -    transfer->sock = NULL;
   1.420 -  }
   1.421 -
   1.422 -  if (transfer->control_sock) {
   1.423 -    g_object_unref( transfer->control_sock );
   1.424 -    transfer->control_sock = NULL;
   1.425 -  } 
   1.426 -  
   1.427 -}
   1.428 -
   1.429 -gint64
   1.430 -gmyth_file_transfer_seek(GMythFileTransfer *transfer, guint64 pos, gint whence)
   1.431 -{
   1.432 -  GMythStringList *strlist = gmyth_string_list_new();
   1.433 -  GString *query;
   1.434 -
   1.435 -  g_return_val_if_fail (transfer->sock != NULL, -1);
   1.436 -  g_return_val_if_fail (transfer->control_sock != NULL, -1);
   1.437 -
   1.438 -  strlist = gmyth_string_list_new();
   1.439 -  query = g_string_new (GMYTHTV_QUERY_HEADER);
   1.440 -  g_string_append_printf (query, "%d", transfer->file_id);
   1.441 -  
   1.442 -  myth_control_acquire_context( TRUE );
   1.443 -
   1.444 -  gmyth_string_list_append_string( strlist, query );
   1.445 -  gmyth_string_list_append_char_array( strlist, "SEEK" );
   1.446 -  gmyth_string_list_append_uint64( strlist, pos );
   1.447 -  
   1.448 -  gmyth_string_list_append_int( strlist, whence );  
   1.449 -
   1.450 -  if (pos > 0 )
   1.451 -    gmyth_string_list_append_uint64( strlist, pos );
   1.452 -  else
   1.453 -    gmyth_string_list_append_uint64( strlist, transfer->readposition );
   1.454 -  
   1.455 -  gmyth_socket_sendreceive_stringlist( transfer->control_sock, strlist );
   1.456 -
   1.457 -  gint64 retval = gmyth_string_list_get_int64(strlist, 0);
   1.458 -  transfer->readposition = retval;
   1.459 -  gmyth_debug ( "[%s] got reading position pointer from the streaming = %lld\n", 
   1.460 -      __FUNCTION__, retval );
   1.461 -
   1.462 -  myth_control_release_context( );
   1.463 -
   1.464 -  return retval;
   1.465 -}
   1.466 -
   1.467 -static gboolean 
   1.468 -myth_control_acquire_context( gboolean do_wait )
   1.469 -{
   1.470 -	gboolean ret = TRUE;	
   1.471 -	//guint max_iter = 50;
   1.472 -	
   1.473 -	//g_mutex_lock( mutex );
   1.474 -	
   1.475 -  //while ( !has_io_access ) 
   1.476 -  //	g_cond_wait( io_watcher_cond, mutex );
   1.477 -  	
   1.478 -  //has_io_access = FALSE;
   1.479 -  
   1.480 -  //myth_control_acquire_context (FALSE);
   1.481 -   
   1.482 -  /* 
   1.483 -  if ( do_wait ) {
   1.484 -  	while ( --max_iter > 0 && !g_main_context_wait( io_watcher_context, io_watcher_cond, mutex ) )
   1.485 -  		ret = FALSE;
   1.486 -  } else if ( !g_main_context_acquire( io_watcher_context ) )
   1.487 -  	ret = FALSE;
   1.488 -  */
   1.489 -  	
   1.490 -  //g_static_mutex_lock( &st_mutex );
   1.491 -  
   1.492 -  return ret;
   1.493 -}
   1.494 -
   1.495 -static gboolean 
   1.496 -myth_control_release_context( ) 
   1.497 -{
   1.498 -	gboolean ret = TRUE;
   1.499 -    
   1.500 -  //g_static_mutex_unlock( &st_mutex );
   1.501 -  
   1.502 -	//g_main_context_release( io_watcher_context );
   1.503 -  
   1.504 -  //g_main_context_wakeup( io_watcher_context );
   1.505 -  
   1.506 -  //has_io_access = TRUE;
   1.507 -
   1.508 -  //g_cond_broadcast( io_watcher_cond );
   1.509 -  
   1.510 -  //g_mutex_unlock( mutex );  
   1.511 - 
   1.512 -  return ret;
   1.513 -}
   1.514 -
   1.515 -gint 
   1.516 -gmyth_file_transfer_read(GMythFileTransfer *transfer, GByteArray *data, gint size, gboolean read_unlimited)
   1.517 -{
   1.518 -  gint bytes_sent = 0;
   1.519 -  gsize bytes_read = 0;
   1.520 -  gsize total_read = 0;
   1.521 -  
   1.522 -  GError *error = NULL;
   1.523 -  
   1.524 -  GIOChannel *io_channel;
   1.525 -  GIOChannel *io_channel_control;
   1.526 -
   1.527 -  GIOCondition io_cond;
   1.528 -  GIOCondition io_cond_control;
   1.529 -  GIOStatus io_status = G_IO_STATUS_NORMAL, io_status_control = G_IO_STATUS_NORMAL;
   1.530 -  
   1.531 -  gboolean ret = TRUE;   
   1.532 -
   1.533 -  GString *query;
   1.534 -  
   1.535 -  g_return_val_if_fail ( data != NULL, -2 );
   1.536 -
   1.537 -  io_channel = transfer->sock->sd_io_ch;
   1.538 -  io_channel_control = transfer->control_sock->sd_io_ch;
   1.539 -
   1.540 -  io_status = g_io_channel_set_encoding( io_channel, NULL, &error );
   1.541 -  if ( io_status == G_IO_STATUS_NORMAL )
   1.542 -    gmyth_debug ( "[%s] Setting encoding to binary data socket).\n", __FUNCTION__ );
   1.543 -
   1.544 -  io_cond = g_io_channel_get_buffer_condition( io_channel );  
   1.545 -
   1.546 -  io_cond_control = g_io_channel_get_buffer_condition( io_channel );
   1.547 -  if ( transfer->sock == NULL || ( io_status == G_IO_STATUS_ERROR ) ) {
   1.548 -    g_printerr( "gmyth_file_transfer_read(): Called with no raw socket.\n" );
   1.549 -    //exit(0); // fixme remove this
   1.550 -    return -1;
   1.551 -  }
   1.552 -
   1.553 -  if ( transfer->control_sock == NULL || ( io_status_control == G_IO_STATUS_ERROR ) ) {
   1.554 -    g_printerr( "gmyth_file_transfer_read(): Called with no control socket.\n" );
   1.555 -    //exit(0); // fixme remove this
   1.556 -    return -1;
   1.557 -  }
   1.558 -
   1.559 -  query = g_string_new (GMYTHTV_QUERY_HEADER);
   1.560 -  g_string_append_printf ( query, "%d", transfer->file_id );
   1.561 -  gmyth_debug ("[%s] Transfer_query = %s\n", __FUNCTION__, query->str );
   1.562 -  
   1.563 -  /* send requests to the maximum number of REQUEST_BLOCK messages */
   1.564 -  guint max_tries = 5;
   1.565 -  
   1.566 -  myth_control_acquire_context( TRUE );
   1.567 -  
   1.568 -  while (total_read == 0 && --max_tries > 0) {
   1.569 -    GMythStringList *strlist = gmyth_string_list_new();
   1.570 -
   1.571 -    gmyth_string_list_append_char_array( strlist, query->str );
   1.572 -    gmyth_string_list_append_char_array( strlist, "REQUEST_BLOCK" );
   1.573 -    gmyth_string_list_append_int( strlist, size );
   1.574 -
   1.575 -    // Request the block to the backend
   1.576 -    gmyth_socket_write_stringlist( transfer->control_sock, strlist );
   1.577 -
   1.578 -    // Receives the backand answer    
   1.579 -    gmyth_socket_read_stringlist( transfer->control_sock, strlist );
   1.580 -
   1.581 -    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) 
   1.582 -    { 
   1.583 -	    bytes_sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error
   1.584 -	    gmyth_debug ( "[%s] got SENT buffer message = %d\n", __FUNCTION__, bytes_sent );
   1.585 -
   1.586 -    	if ( bytes_sent >= 0 ) 
   1.587 -    	{
   1.588 -        gchar *data_buffer = g_new0 ( gchar, bytes_sent );    
   1.589 -   	    while ( 0 != bytes_sent ) 
   1.590 -   	    { 
   1.591 -          io_status = g_io_channel_read_chars( io_channel, data_buffer + total_read, 
   1.592 -	    				(gsize) bytes_sent,	&bytes_read, &error );
   1.593 -
   1.594 -	        total_read += bytes_read;
   1.595 -          bytes_sent -= bytes_read;
   1.596 -      
   1.597 -            /* append new data to the increasing byte array */
   1.598 -	    		data = g_byte_array_append (data, (const guint8*)data_buffer, bytes_read);
   1.599 -	        gmyth_debug ("Total transfer data read: %d\n", total_read);
   1.600 -        }
   1.601 -        g_free (data_buffer);
   1.602 -      } /* if */
   1.603 -    } else if ( !(transfer->priv != NULL && transfer->priv->livetv != NULL &&
   1.604 -					transfer->priv->do_next_program_chain) ) {
   1.605 -    	total_read = GMYTHTV_FILE_TRANSFER_READ_ERROR;
   1.606 -    	g_object_unref (strlist);
   1.607 -    	strlist = NULL;
   1.608 -    	break;
   1.609 -    }
   1.610 -    g_object_unref (strlist);
   1.611 -    strlist = NULL;
   1.612 -  }
   1.613 -  
   1.614 -  if ( bytes_sent == 0 && max_tries == 0 ) 
   1.615 -	{
   1.616 -		gmyth_debug( "Trying to move to the next program chain..." );
   1.617 -		transfer->priv = GMYTH_FILE_TRANSFER_GET_PRIVATE(transfer);
   1.618 -		
   1.619 -		if ( transfer->priv != NULL && transfer->priv->livetv != NULL &&
   1.620 -					transfer->priv->do_next_program_chain )
   1.621 -		{
   1.622 -		
   1.623 -			total_read = GMYTHTV_FILE_TRANSFER_NEXT_PROG_CHAIN;	
   1.624 -		
   1.625 -	  		g_mutex_lock( transfer->mutex );
   1.626 -	  		
   1.627 -	  		ret = gmyth_livetv_next_program_chain( transfer->priv->livetv );
   1.628 -	  		
   1.629 -	  		g_mutex_unlock( transfer->mutex );
   1.630 -  		
   1.631 -			if ( !ret )
   1.632 -				gmyth_debug( "Cannot change to the next program chain!" );
   1.633 -			else
   1.634 -				gmyth_debug( "OK!!! MOVED to the next program chain [%s]!", 
   1.635 -										(gmyth_tvchain_get_id( transfer->priv->livetv->tvchain ))->str );						
   1.636 -		}
   1.637 -
   1.638 -	} /* if */
   1.639 -  
   1.640 -  myth_control_release_context( );
   1.641 -  g_string_free (query, TRUE);
   1.642 -
   1.643 -  if ( error != NULL ) {
   1.644 -    gmyth_debug ("Cleaning-up ERROR: %s [msg = %s, code = %d]\n", __FUNCTION__, error->message, 
   1.645 -	    error->code);
   1.646 -    g_error_free (error);
   1.647 -  }
   1.648 -  
   1.649 -  if ( total_read > 0 )
   1.650 -  	transfer->readposition += total_read;  	
   1.651 -  	
   1.652 -  return total_read;
   1.653 -}
   1.654 -
   1.655 -static void 
   1.656 -gmyth_file_transfer_program_info_changed( GMythFileTransfer *transfer, 
   1.657 -										gint msg_code, gpointer livetv_transfer )
   1.658 -{
   1.659 -	GMythLiveTV *livetv = GMYTH_LIVETV( livetv_transfer );
   1.660 -	
   1.661 -	gmyth_debug( "Program info changed! ( file transfer orig. = %p, ptr. = [%s], user data = [%s] )", transfer, 
   1.662 -		livetv_transfer != NULL ? "[NOT NULL]" : "[NULL]", livetv != NULL ? "[NOT NULL]" : "[NULL]" );
   1.663 -	
   1.664 -	if ( livetv != NULL && transfer == livetv->file_transfer )
   1.665 -	{
   1.666 -		gmyth_debug( "YES, the requested program info movement on the LiveTV transfer is authentical!" );
   1.667 -	}
   1.668 -	
   1.669 -	transfer->priv = GMYTH_FILE_TRANSFER_GET_PRIVATE(transfer);	
   1.670 -	
   1.671 -	transfer->priv->livetv = livetv;	
   1.672 -	transfer->priv->do_next_program_chain = TRUE;
   1.673 -	
   1.674 -	//g_object_unref( transfer );
   1.675 -}
   1.676 -
   1.677 -gboolean 
   1.678 -gmyth_file_transfer_settimeout( GMythFileTransfer *transfer, gboolean fast )
   1.679 -{
   1.680 -
   1.681 -  GMythStringList *strlist = NULL;
   1.682 -
   1.683 -  g_return_val_if_fail (transfer->sock != NULL, FALSE);
   1.684 -  g_return_val_if_fail (transfer->control_sock != NULL, FALSE);
   1.685 -
   1.686 -//  if ( transfer->timeoutisfast == fast )
   1.687 -//    return;
   1.688 -
   1.689 -  strlist = gmyth_string_list_new(); 
   1.690 -  gmyth_string_list_append_char_array( strlist, GMYTHTV_QUERY_HEADER );
   1.691 -  gmyth_string_list_append_char_array( strlist, "SET_TIMEOUT" );
   1.692 -  gmyth_string_list_append_int( strlist, fast ); 
   1.693 -
   1.694 -  gmyth_socket_write_stringlist( transfer->control_sock, strlist );
   1.695 -  gmyth_socket_read_stringlist( transfer->control_sock, strlist );
   1.696 -
   1.697 -//  transfer->timeoutisfast = fast;
   1.698 -
   1.699 -  return TRUE;
   1.700 -}
   1.701 -
   1.702 -
   1.703 -guint64
   1.704 -gmyth_file_transfer_get_filesize (GMythFileTransfer *transfer)
   1.705 -{
   1.706 -    g_return_val_if_fail (transfer != NULL, 0);
   1.707 -    return transfer->filesize;
   1.708 -}