libgnomevfs2-mythtv/modules/mythtv-method.c
branchtrunk
changeset 446 d260ed30f4de
parent 408 7d529c58a88f
child 447 c632a2009bee
     1.1 --- a/libgnomevfs2-mythtv/modules/mythtv-method.c	Thu Mar 08 23:55:27 2007 +0000
     1.2 +++ b/libgnomevfs2-mythtv/modules/mythtv-method.c	Fri Mar 23 22:54:24 2007 +0000
     1.3 @@ -22,6 +22,8 @@
     1.4  
     1.5  #include <string.h>
     1.6  #include <glib.h>
     1.7 +#include <glib/gprintf.h>
     1.8 +#include <glib/gstdio.h>
     1.9  #include <math.h>
    1.10  
    1.11  #include <libgnomevfs/gnome-vfs-module.h>
    1.12 @@ -42,11 +44,11 @@
    1.13  #define MYTHTV_TRANSFER_MAX_WAITS       100
    1.14  
    1.15  /* internal GnomeVFS plug-in buffer size ( 120 Kbytes ) */
    1.16 -#define MYTHTV_BUFFER_SIZE							1024*80
    1.17 +#define MYTHTV_BUFFER_SIZE                          80*1024
    1.18  /* internally sized GnomeVFS plug-in buffer ( 4 Kbytes ) */
    1.19 -#define MYTHTV_MAX_VFS_BUFFER_SIZE			4096
    1.20 +#define MYTHTV_MAX_VFS_BUFFER_SIZE                  4096
    1.21  /* maximum number of bytes to be requested to the MythTV backend ( 64 Kbytes ) */
    1.22 -#define MYTHTV_MAX_REQUEST_SIZE					1024*64
    1.23 +#define MYTHTV_MAX_REQUEST_SIZE                     64*1024
    1.24  
    1.25  static GnomeVFSResult do_read (GnomeVFSMethod * method,
    1.26  			       GnomeVFSMethodHandle * method_handle,
    1.27 @@ -73,6 +75,10 @@
    1.28  } MythtvHandle;
    1.29  
    1.30  //static MythtvHandle *myth_handle = NULL;
    1.31 +#ifdef DEBUG
    1.32 +static FILE *fpout = NULL;
    1.33 +static gboolean first = TRUE;
    1.34 +#endif
    1.35  
    1.36  static GnomeVFSResult
    1.37  do_open (GnomeVFSMethod * method,
    1.38 @@ -96,6 +102,7 @@
    1.39  		return GNOME_VFS_ERROR_INVALID_HOST_NAME;
    1.40  	}
    1.41  
    1.42 +    // FIXME: myth_handle is always NULL here
    1.43  	if ((NULL == myth_handle) || !myth_handle->configured) {
    1.44  		myth_handle = g_new0 (MythtvHandle, 1);
    1.45  
    1.46 @@ -131,7 +138,7 @@
    1.47  		if (gmyth_uri != NULL && myth_handle->is_livetv == TRUE) {
    1.48  
    1.49  			if (NULL == myth_handle->livetv) {
    1.50 -				myth_handle->livetv = gmyth_livetv_new ();
    1.51 +				myth_handle->livetv = gmyth_livetv_new (backend_info);
    1.52  
    1.53  				myth_handle->channel_name =
    1.54  				    gmyth_uri_get_channel_name (gmyth_uri);
    1.55 @@ -141,17 +148,13 @@
    1.56  					 myth_handle->channel_name);
    1.57  
    1.58  				if (myth_handle->channel_name != NULL) {
    1.59 -					if (gmyth_livetv_channel_name_setup
    1.60 -					    (myth_handle->livetv,
    1.61 -					     myth_handle->channel_name,
    1.62 -					     backend_info) == FALSE) {
    1.63 +					if (gmyth_livetv_channel_name_setup (myth_handle->livetv,
    1.64 +    					     myth_handle->channel_name) == FALSE) {
    1.65  						g_object_unref (gmyth_uri);
    1.66  						ret = FALSE;
    1.67  					}
    1.68  				} else {
    1.69 -					if (gmyth_livetv_setup
    1.70 -					    (myth_handle->livetv,
    1.71 -					     backend_info) == FALSE) {
    1.72 +					if (gmyth_livetv_setup (myth_handle->livetv) == FALSE) {
    1.73  						g_object_unref (gmyth_uri);
    1.74  						ret = FALSE;
    1.75  					}
    1.76 @@ -160,8 +163,7 @@
    1.77  
    1.78  			if (NULL == myth_handle->file_transfer) {
    1.79  				myth_handle->file_transfer =
    1.80 -				    gmyth_livetv_create_file_transfer
    1.81 -				    (myth_handle->livetv);
    1.82 +				    gmyth_livetv_create_file_transfer (myth_handle->livetv);
    1.83  
    1.84  				if (NULL == myth_handle->file_transfer) {
    1.85  					ret = FALSE;
    1.86 @@ -186,10 +188,8 @@
    1.87  					myth_handle->file_transfer = NULL;
    1.88  					ret = FALSE;
    1.89  				}
    1.90 -			}
    1.91 -			/* if - FileTransfer is NULL, or not */
    1.92 +			} /* if - FileTransfer is NULL, or not */
    1.93  		} else {
    1.94 -
    1.95  			if (NULL == myth_handle->file_transfer) {
    1.96  
    1.97  				myth_handle->file_transfer =
    1.98 @@ -211,8 +211,7 @@
    1.99  							      (gmyth_uri));
   1.100  
   1.101  			}
   1.102 -
   1.103 -		}		/* if - LiveTV or not? */
   1.104 +		} /* if - LiveTV or not? */
   1.105  
   1.106  		if (ret == FALSE) {
   1.107  			g_debug ("MythTV FileTransfer open error.\n");
   1.108 @@ -236,9 +235,7 @@
   1.109  		if ( /*myth_handle->file_transfer->filesize <= 0 && */
   1.110  		    myth_handle->is_livetv) {
   1.111  			myth_handle->content_size =
   1.112 -			    gmyth_recorder_get_file_position (myth_handle->
   1.113 -							      livetv->
   1.114 -							      recorder);
   1.115 +			    gmyth_recorder_get_file_position (myth_handle->livetv->recorder);
   1.116  		} else {
   1.117  			myth_handle->content_size =
   1.118  			    myth_handle->file_transfer->filesize;
   1.119 @@ -256,7 +253,8 @@
   1.120  	 GnomeVFSMethodHandle * method_handle,
   1.121  	 gpointer buffer,
   1.122  	 GnomeVFSFileSize num_bytes,
   1.123 -	 GnomeVFSFileSize * bytes_read, GnomeVFSContext * context)
   1.124 +	 GnomeVFSFileSize * bytes_read, 
   1.125 +     GnomeVFSContext * context)
   1.126  {
   1.127  	MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   1.128  	GnomeVFSFileSize bytes_to_read = num_bytes;
   1.129 @@ -284,7 +282,6 @@
   1.130  		gint buffer_size;
   1.131  
   1.132  		while (MYTHTV_BUFFER_SIZE != myth_handle->buffer_remain) {
   1.133 -
   1.134  			/* resize buffer length request to no more than MYTHTV_MAX_REQUEST_SIZE */
   1.135  			if ((MYTHTV_BUFFER_SIZE -
   1.136  			     myth_handle->buffer_remain) <=
   1.137 @@ -297,8 +294,7 @@
   1.138  
   1.139  			GByteArray *tmp_buffer = g_byte_array_new ();
   1.140  
   1.141 -			g_debug
   1.142 -			    ("Asking %d bytes (there is %d bytes in the buffer)\n",
   1.143 +			g_debug ("Asking %d bytes (there is %d bytes in the buffer)\n",
   1.144  			     buffer_size, myth_handle->buffer_remain);
   1.145  
   1.146  			gint len =
   1.147 @@ -332,6 +328,7 @@
   1.148  		}		/* while - iterates until fills the internal buffer */
   1.149  
   1.150  	}
   1.151 +        
   1.152  	/* if - got from the network, or not */
   1.153  	bytes_to_read =
   1.154  	    (bytes_to_read >
   1.155 @@ -367,16 +364,16 @@
   1.156  
   1.157  	//if ( NULL == myth_handle || myth_handle->configured ) {
   1.158  
   1.159 +	if (myth_handle->is_livetv && myth_handle->livetv != NULL) {
   1.160 +		g_object_unref (myth_handle->livetv);
   1.161 +		myth_handle->livetv = NULL;
   1.162 +	}
   1.163 +   
   1.164  	if (myth_handle->file_transfer != NULL) {
   1.165  		g_object_unref (myth_handle->file_transfer);
   1.166  		myth_handle->file_transfer = NULL;
   1.167  	}
   1.168  
   1.169 -	if (myth_handle->is_livetv && myth_handle->livetv != NULL) {
   1.170 -		g_object_unref (myth_handle->livetv);
   1.171 -		myth_handle->livetv = NULL;
   1.172 -	}
   1.173 -
   1.174  	if (myth_handle->buffer) {
   1.175  		g_byte_array_free (myth_handle->buffer, TRUE);
   1.176  		myth_handle->buffer = NULL;
   1.177 @@ -384,6 +381,8 @@
   1.178  
   1.179  	myth_handle->configured = FALSE;
   1.180  
   1.181 +	g_free (myth_handle->channel_name);
   1.182 +    
   1.183  	g_free (myth_handle);
   1.184  
   1.185  	myth_handle = NULL;
   1.186 @@ -427,6 +426,7 @@
   1.187  
   1.188  	is_livetv = gmyth_uri_is_livetv (gmyth_uri);
   1.189  
   1.190 +    file_info->valid_fields = 0;
   1.191  	file_info->valid_fields = file_info->valid_fields
   1.192  	    | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   1.193  	    | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   1.194 @@ -451,19 +451,16 @@
   1.195  		/* FIME: Implement this at gmyth_socket */
   1.196  		res =
   1.197  		    gmyth_socket_connect_to_backend (socket,
   1.198 -						     backend_info->
   1.199 -						     hostname,
   1.200 +						     backend_info->hostname,
   1.201  						     backend_info->port,
   1.202  						     TRUE);
   1.203  		if (!res) {
   1.204 -			g_debug ("[%s] LiveTV can not connect to backend",
   1.205 -				 __FUNCTION__);
   1.206 +			g_debug ("[%s] LiveTV can not connect to backend",  __FUNCTION__);
   1.207  			res = FALSE;
   1.208  			goto error;
   1.209  		}
   1.210  
   1.211 -		if (gmyth_remote_util_get_free_recorder_count (socket) <=
   1.212 -		    0) {
   1.213 +		if (gmyth_remote_util_get_free_recorder_count (socket) <= 0) {
   1.214  			g_debug ("No free remote encoder available.");
   1.215  			res = FALSE;
   1.216  			goto error;
   1.217 @@ -472,12 +469,11 @@
   1.218  		/* Gets the recorder num */
   1.219  		recorder = remote_request_next_free_recorder (socket, -1);
   1.220  
   1.221 -		if ( socket != NULL )
   1.222 +		if (socket != NULL)
   1.223  			g_object_unref (socket);
   1.224  
   1.225  		if (recorder == NULL) {
   1.226 -			g_debug ("[%s] None remote encoder available",
   1.227 -				 __FUNCTION__);
   1.228 +			g_debug ("[%s] None remote encoder available", __FUNCTION__);
   1.229  			res = FALSE;
   1.230  			goto error;
   1.231  		}
   1.232 @@ -485,9 +481,7 @@
   1.233  		/* Init remote encoder. Opens its control socket. */
   1.234  		res = gmyth_recorder_setup (recorder);
   1.235  		if (!res) {
   1.236 -			g_debug
   1.237 -			    ("[%s] Fail while setting remote encoder\n",
   1.238 -			     __FUNCTION__);
   1.239 +			g_debug ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
   1.240  			res = FALSE;
   1.241  			goto error;
   1.242  		}
   1.243 @@ -501,9 +495,7 @@
   1.244  			goto error;
   1.245  		}
   1.246  		/* Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly) */
   1.247 -		res = gmyth_recorder_spawntv (recorder,
   1.248 -					      gmyth_tvchain_get_id
   1.249 -					      (tvchain));
   1.250 +		res = gmyth_recorder_spawntv (recorder, gmyth_tvchain_get_id(tvchain));
   1.251  		if (!res) {
   1.252  			g_warning ("[%s] Fail while spawn tv\n",
   1.253  				   __FUNCTION__);
   1.254 @@ -527,8 +519,8 @@
   1.255  
   1.256  		} else {
   1.257  			file_info->name = g_strdup ("LiveTV.nuv");
   1.258 -			file_info->size =
   1.259 -			    gmyth_recorder_get_file_position (recorder);
   1.260 +            /* Size being overrided below ... */
   1.261 +			//file_info->size = gmyth_recorder_get_file_position (recorder);
   1.262  		}
   1.263  
   1.264  		if (recorder != NULL)
   1.265 @@ -540,47 +532,44 @@
   1.266  		if (tvchain != NULL)
   1.267  			g_object_unref (tvchain);
   1.268  
   1.269 -		file_info->size = (GnomeVFSFileSize) - 1;
   1.270 +		//file_info->size = (GnomeVFSFileSize) - 1;
   1.271  
   1.272  	} else {
   1.273  
   1.274 -		/* start to get file info from remote file encoder */
   1.275 -		file_transfer = gmyth_file_transfer_new (backend_info);
   1.276 +        /* start to get file info from remote file encoder */
   1.277 +        file_transfer = gmyth_file_transfer_new (backend_info);
   1.278  
   1.279  		/* Verifies if the file exists */
   1.280 -		if (!gmyth_util_file_exists
   1.281 -		    (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   1.282 -			g_object_unref (backend_info);
   1.283 -			return GNOME_VFS_ERROR_NOT_FOUND;
   1.284 +    	if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   1.285 +            g_object_unref (backend_info);
   1.286 +            return GNOME_VFS_ERROR_NOT_FOUND;
   1.287  		}
   1.288  
   1.289  		/* sets the Playback monitor connection */
   1.290 -		ret =
   1.291 -		    gmyth_file_transfer_open (file_transfer,
   1.292 -					      gmyth_uri_get_path
   1.293 -					      (gmyth_uri));
   1.294 +		ret = gmyth_file_transfer_open (file_transfer,
   1.295 +					      gmyth_uri_get_path (gmyth_uri));
   1.296  
   1.297  		file_info->name = g_strdup (gnome_vfs_uri_get_path (uri));
   1.298  
   1.299 -	}			/* if - LiveTV or not? */
   1.300 +	} /* if - LiveTV or not? */
   1.301  
   1.302  	if (ret == FALSE) {
   1.303  		g_debug ("MythTV FileTransfer open error\n");
   1.304  		return GNOME_VFS_ERROR_NOT_OPEN;
   1.305  	}
   1.306  
   1.307 +    /* Just for recorded content */
   1.308  	if (ret == TRUE && file_transfer != NULL) {
   1.309  		file_info->size =
   1.310  		    gmyth_file_transfer_get_filesize (file_transfer);
   1.311 +
   1.312 +        file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_SIZE;
   1.313 +        
   1.314  		if (file_transfer)
   1.315  			g_object_unref (file_transfer);
   1.316  	}
   1.317  
   1.318 -	file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
   1.319 -	file_info->io_block_size =
   1.320 -	    GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
   1.321 -
   1.322 -      error:
   1.323 +error:
   1.324  	if (backend_info)
   1.325  		g_object_unref (backend_info);
   1.326