libgnomevfs2-mythtv/modules/mythtv-method.c
author rosfran
Mon Feb 12 15:22:28 2007 +0000 (2007-02-12)
branchtrunk
changeset 354 ce009d76ffb5
parent 346 4ba962630375
child 355 f7b40eb795c2
permissions -rwxr-xr-x
[svn r356] Solved bug on read method callback.
     1 /*
     2  * @author Hallyson Melo <hallyson.melo@indt.org.br>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU Lesser General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU Lesser General Public License
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  */
    18 
    19 #ifdef HAVE_CONFIG_H
    20 #include <config.h>
    21 #endif
    22 
    23 #include <string.h>
    24 #include <glib.h>
    25 #include <math.h>
    26 
    27 #include <libgnomevfs/gnome-vfs-module.h>
    28 #include <libgnomevfs/gnome-vfs-utils.h>
    29 
    30 #include <gmyth/gmyth_file_transfer.h>
    31 #include <gmyth/gmyth_livetv.h>
    32 #include <gmyth/gmyth_uri.h>
    33 #include <gmyth/gmyth_recorder.h>
    34 #include <gmyth/gmyth_backendinfo.h>
    35 #include <gmyth/gmyth_util.h>
    36 #include <gmyth/gmyth_remote_util.h>
    37 #include <gmyth/gmyth_tvchain.h>
    38 
    39 #define GST_MYTHTV_ID_NUM               1
    40 #define MYTHTV_VERSION_DEFAULT          30
    41 #define MYTHTV_TRANSFER_MAX_WAITS       100
    42 
    43 #define MYTHTV_BUFFER_SIZE							1024*80
    44 #define MYTHTV_MAX_VFS_BUFFER_SIZE			4096
    45 #define MYTHTV_MAX_REQUEST_SIZE					1024*64
    46 
    47 static GnomeVFSResult do_read (GnomeVFSMethod *method,
    48                                GnomeVFSMethodHandle *method_handle,
    49                                gpointer buffer,
    50                                GnomeVFSFileSize num_bytes,
    51                                GnomeVFSFileSize *bytes_read,
    52                                GnomeVFSContext *context);
    53 
    54 typedef struct {
    55     GMythFileTransfer *file_transfer;
    56     GMythLiveTV 			*livetv;
    57     gchar 						*channel_name;
    58     
    59     gint mythtv_version;
    60     gint64 content_size;
    61     guint64 bytes_read;
    62 
    63     GByteArray *buffer;
    64     gsize buffer_remain;
    65     gboolean	is_livetv;
    66     
    67 } MythtvHandle;
    68 
    69 static GnomeVFSResult
    70 do_open (GnomeVFSMethod *method,
    71          GnomeVFSMethodHandle **method_handle,
    72          GnomeVFSURI *uri,
    73          GnomeVFSOpenMode mode,
    74          GnomeVFSContext *context)
    75 {
    76     MythtvHandle *myth_handle;
    77     GMythBackendInfo *backend_info;
    78     GMythURI *gmyth_uri = NULL;
    79     gboolean ret = TRUE;
    80 
    81     _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
    82     _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
    83 
    84     myth_handle = g_new0 (MythtvHandle, 1);
    85     
    86     myth_handle->is_livetv = FALSE;
    87 
    88     if (mode & GNOME_VFS_OPEN_WRITE) {
    89         return GNOME_VFS_ERROR_NOT_PERMITTED;
    90     }
    91 
    92     if (gnome_vfs_uri_get_host_name (uri) == NULL) {
    93     	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
    94     }
    95 
    96     /* Initialize mythtv handler*/
    97     myth_handle->file_transfer = NULL;
    98     myth_handle->livetv = NULL;
    99     myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
   100     myth_handle->bytes_read = 0;
   101     myth_handle->content_size = (GnomeVFSFileSize) - 1;
   102 
   103     /* Creates and fills out the backend info structure */    
   104 	  backend_info = gmyth_backend_info_new_with_uri ( 
   105 	  			gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   106 	  
   107 	  /* creates an instance of  */  
   108 	  gmyth_uri = gmyth_uri_new_with_value( 
   109 	  		gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   110 
   111 	  myth_handle->is_livetv = gmyth_uri_is_livetv( gmyth_uri );
   112 
   113 	  /* Connect to the backend */	  
   114 	  if ( gmyth_uri != NULL && myth_handle->is_livetv == TRUE ) {
   115 	  	
   116 	  	if ( NULL == myth_handle->livetv )
   117 	  	{
   118 	    	myth_handle->livetv = gmyth_livetv_new ();
   119 	    
   120 		    myth_handle->channel_name = gmyth_uri_get_channel_name( gmyth_uri );
   121 		    
   122 		    g_debug( "[%s] Channel name = %s\n", __FUNCTION__, myth_handle->channel_name );
   123 		
   124 		    if ( myth_handle->channel_name != NULL ) {
   125 		      if (gmyth_livetv_channel_name_setup (myth_handle->livetv, myth_handle->channel_name,
   126 		              backend_info) == FALSE) {
   127 		        g_object_unref( gmyth_uri );
   128 		        ret = FALSE;
   129 		      }
   130 		    } else {
   131 		      if ( gmyth_livetv_setup (myth_handle->livetv, backend_info) == FALSE ) {
   132 		      	g_object_unref( gmyth_uri );
   133 		        ret = FALSE;
   134 		      }
   135 		    }
   136 	  	}
   137 	  	
   138 	  	if ( NULL == myth_handle->file_transfer )	{
   139 	    	myth_handle->file_transfer = gmyth_livetv_create_file_transfer (myth_handle->livetv);
   140 	
   141 		    if (NULL == myth_handle->file_transfer) {
   142 		      ret = FALSE;
   143 			    g_debug ("MythTV FileTransfer is NULL!\n");
   144 			    return GNOME_VFS_ERROR_NOT_OPEN;
   145 		    }
   146 		    
   147 	  		if ( !gmyth_file_transfer_open( myth_handle->file_transfer, myth_handle->livetv->uri != NULL ? 
   148 									gmyth_uri_get_path(myth_handle->livetv->uri) : 
   149 									myth_handle->livetv->proginfo->pathname->str ) )
   150 				{
   151 					g_debug ("Couldn't open MythTV FileTransfer is NULL!\n");
   152 					g_object_unref( myth_handle->file_transfer );
   153 					myth_handle->file_transfer = NULL;
   154 					ret = FALSE;
   155 				}
   156 	  	} /* if - FileTransfer is NULL, or not */
   157 	    
   158 	  } else {
   159 	  	
   160 	  	if (NULL == myth_handle->file_transfer ) {
   161 	
   162 		    myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
   163 		    
   164 		    /* Verifies if the file exists */
   165 		    if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   166 		        g_object_unref (backend_info);
   167 						ret = FALSE;
   168 		    }
   169 		    
   170 		    /* sets the Playback monitor connection */
   171 		    ret = gmyth_file_transfer_open ( myth_handle->file_transfer, 
   172 		    		gmyth_uri_get_path (gmyth_uri) );
   173 		    		
   174 	  	}
   175 		
   176 	  } /* if - LiveTV or not? */
   177 	  
   178     if (ret == FALSE) {
   179 	    g_debug ("MythTV FileTransfer open error.\n");
   180 	    return GNOME_VFS_ERROR_NOT_OPEN;
   181 	  }
   182 
   183     //g_object_unref (backend_info);
   184     
   185 	  //if ( gmyth_uri != NULL )
   186 	  //	g_object_unref( gmyth_uri );
   187 	  
   188 	  g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
   189 	  
   190 	  if ( /*myth_handle->file_transfer->filesize <= 0 && */myth_handle->is_livetv ) {
   191 	  	myth_handle->content_size = (GnomeVFSFileSize) - 1;
   192 			//myth_handle->content_size = gmyth_recorder_get_file_position( myth_handle->livetv->recorder );
   193 	  } else		
   194     	myth_handle->content_size = myth_handle->file_transfer->filesize;
   195     
   196     myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
   197     myth_handle->buffer_remain = 0;
   198 
   199     *method_handle = (GnomeVFSMethodHandle *) myth_handle;
   200 
   201     return GNOME_VFS_OK;
   202 }
   203 
   204 static GnomeVFSResult
   205 do_read (GnomeVFSMethod *method,
   206          GnomeVFSMethodHandle *method_handle,
   207          gpointer buffer,
   208          GnomeVFSFileSize num_bytes,
   209          GnomeVFSFileSize *bytes_read,
   210          GnomeVFSContext *context)
   211 {
   212   MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   213   GnomeVFSFileSize bytes_to_read;
   214 
   215   *bytes_read = 0;
   216   
   217   g_debug ("XXXXXXXXXX Requested %llu bytes...\n", (guint64)num_bytes);
   218 
   219   if ( !myth_handle->is_livetv && ( myth_handle->bytes_read >= myth_handle->content_size ) )
   220       return GNOME_VFS_ERROR_EOF;
   221 
   222   // fixme: change this to min math function
   223   if (!myth_handle->is_livetv && ( myth_handle->content_size > 0 ) && 
   224   				( num_bytes > ( myth_handle->content_size - myth_handle->bytes_read ) ) )
   225     bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   226   else
   227     bytes_to_read = num_bytes;
   228 
   229   /* Loop sending the Myth File Transfer request:
   230   * Retry whilst authentication fails and we supply it. */
   231   //if (myth_handle->buffer_remain  < MYTHTV_BUFFER_SIZE) {
   232   if ( ( myth_handle->buffer_remain = myth_handle->buffer->len ) < MYTHTV_MAX_VFS_BUFFER_SIZE ) {
   233   	gint buffer_size = MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain;
   234   	
   235   	/* resize buffer length request to no more than MYTHTV_MAX_REQUEST_SIZE */
   236   	if ( buffer_size > MYTHTV_MAX_REQUEST_SIZE )
   237   		buffer_size = MYTHTV_MAX_REQUEST_SIZE;
   238 
   239   	GByteArray *tmp_buffer = g_byte_array_new();
   240 
   241 		g_debug ("XXXXXXXXXX Pedindo %d (restam apenas %d no buffer)\n", buffer_size, myth_handle->buffer_remain);
   242 
   243     gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
   244           tmp_buffer, buffer_size, TRUE);
   245 
   246 		if (!myth_handle->is_livetv && len < 0) {
   247 	    g_byte_array_free (tmp_buffer, TRUE);
   248 	    g_debug ("Fail to read bytes");
   249 	    return GNOME_VFS_ERROR_IO;
   250 	  } /*else if (len == 0) {
   251 		    g_byte_array_free (tmp_buffer, TRUE);
   252 		    g_warning ("End of file probably achieved");
   253 		    return GNOME_VFS_ERROR_EOF;
   254 		}*/
   255 	
   256 	    myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
   257 	    		tmp_buffer->data, len);
   258 	
   259 			myth_handle->buffer_remain += len;
   260 
   261 			if ( tmp_buffer != NULL )
   262 			{
   263 	      g_byte_array_free (tmp_buffer, TRUE);
   264 	    	tmp_buffer = NULL;
   265 			}
   266     } /* if - got from the network, or not */
   267     
   268     bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
   269     /* gets the first buffer_size bytes from the byte array buffer variable */ 
   270 
   271     g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
   272 
   273     myth_handle->bytes_read += bytes_to_read;
   274     myth_handle->buffer_remain -= bytes_to_read;    
   275 
   276   	/* flushs the newly buffer got from byte array */
   277   	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
   278     *bytes_read = bytes_to_read;
   279   
   280     return GNOME_VFS_OK;
   281 }
   282 
   283 static GnomeVFSResult
   284 do_close (GnomeVFSMethod *method,
   285           GnomeVFSMethodHandle *method_handle,
   286           GnomeVFSContext *context)
   287 {
   288 
   289     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   290 
   291     if (myth_handle->file_transfer != NULL) {
   292       g_object_unref (myth_handle->file_transfer);
   293     	myth_handle->file_transfer = NULL;
   294     }
   295     
   296     if (myth_handle->is_livetv && myth_handle->livetv != NULL) {
   297       g_object_unref (myth_handle->livetv);
   298     	myth_handle->livetv = NULL;
   299     }
   300     
   301     if (myth_handle->buffer) {
   302 		g_byte_array_free (myth_handle->buffer, TRUE);
   303 		myth_handle->buffer = NULL;
   304     }
   305 
   306     g_free (myth_handle);
   307 
   308     return GNOME_VFS_OK;
   309 }
   310 
   311 static GnomeVFSResult
   312 do_get_file_info (GnomeVFSMethod *method,
   313                   GnomeVFSURI *uri,
   314                   GnomeVFSFileInfo *file_info,
   315                   GnomeVFSFileInfoOptions options,
   316                   GnomeVFSContext *context)
   317 {
   318     GMythFileTransfer *file_transfer = NULL;
   319     GMythRecorder			*recorder 		 = NULL;
   320     GMythTVChain			*tvchain	 		 = NULL;
   321     GMythBackendInfo  *backend_info  = NULL;
   322     GMythURI					*gmyth_uri		 = NULL;
   323 		GMythSocket 			*socket 			 = NULL;		 
   324     gboolean 					is_livetv 		 = FALSE;
   325     gboolean					ret						 = TRUE;
   326     gboolean 					res 					 = TRUE;
   327     
   328     /* Creates and fills out the backend info structure */    
   329 	  backend_info = gmyth_backend_info_new_with_uri ( 
   330 	  			gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   331 	  
   332 	  /* creates an instance of */  
   333 	  gmyth_uri = gmyth_uri_new_with_value( 
   334 	  		gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   335 
   336 	  is_livetv = gmyth_uri_is_livetv( gmyth_uri );
   337 
   338     file_info->valid_fields = file_info->valid_fields
   339         | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   340         | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   341         | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   342     file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   343     /* fixme: get from file extension? */
   344     file_info->mime_type = g_strdup ("video/x-nuv");
   345     file_info->permissions =
   346         GNOME_VFS_PERM_USER_READ |
   347         GNOME_VFS_PERM_OTHER_READ |
   348         GNOME_VFS_PERM_GROUP_READ;
   349         
   350     g_print( "gnome_vfs_uri == %s | gmyth_uri == %s.\n",
   351     				gnome_vfs_uri_get_path (uri),
   352     				gmyth_uri_get_path (gmyth_uri) );
   353 
   354 	  /* Connect to the backend */
   355 	  if ( gmyth_uri != NULL && is_livetv == TRUE ) {
   356 	  	
   357 	  	/* start to get file info from LiveTV remote encoder */	  	
   358 			socket = gmyth_socket_new ();
   359 			
   360 			/* FIME: Implement this at gmyth_socket */
   361 			res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
   362 					backend_info->port, TRUE);
   363 			if (!res) {
   364 				g_print ("[%s] LiveTV can not connect to backend", __FUNCTION__);	
   365 				res = FALSE;
   366 				goto error;
   367 			}
   368 
   369 			if ( gmyth_remote_util_get_free_recorder_count (socket) <= 0 ) {
   370 				g_print ("No free remote encoder available.");
   371 				res = FALSE;
   372 				goto error;
   373 			}
   374 		
   375 			/* Gets the recorder num */
   376 			recorder = remote_request_next_free_recorder (socket, -1);
   377 			
   378 			//if ( socket != NULL )
   379 			//	g_object_unref (socket);
   380 		
   381 			if ( recorder == NULL ) {
   382 				g_print ("[%s] None remote encoder available", __FUNCTION__);
   383 				res = FALSE;
   384 				goto error;
   385 			}
   386 			
   387 			/* Init remote encoder. Opens its control socket. */
   388 			res = gmyth_recorder_setup(recorder);
   389 			if ( !res ) {
   390 				g_print ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
   391 				res = FALSE;
   392 				goto error;
   393 			}
   394 			
   395 			/* Creates livetv chain handler */
   396 			tvchain = gmyth_tvchain_new();
   397 			gmyth_tvchain_initialize ( tvchain, backend_info );
   398 		
   399 			if ( tvchain == NULL || tvchain->tvchain_id == NULL ) {
   400 				res = FALSE;
   401 				goto error;
   402 			}
   403 			
   404 			// Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly)
   405 			res = gmyth_recorder_spawntv ( recorder,
   406 					gmyth_tvchain_get_id(tvchain) );
   407 			if (!res) {
   408 				g_warning ("[%s] Fail while spawn tv\n", __FUNCTION__);
   409 				res = FALSE;
   410 				goto error;
   411 			}		
   412 			
   413 		  //gchar* channel_name = gmyth_uri_get_channel_name( gmyth_uri );
   414 	
   415 		  /* DEBUG message */
   416 			GMythProgramInfo* prog_info = gmyth_recorder_get_current_program_info( recorder );
   417 			
   418 			if ( prog_info != NULL )
   419 			{
   420 				gmyth_program_info_print( prog_info );
   421   
   422 		    g_print( "path = %s",  prog_info->pathname->str );
   423 		    
   424 		    file_info->name = g_strdup ( g_strrstr( prog_info->pathname->str, "/" ) );
   425 		    
   426 			} else {
   427 				file_info->name = g_strdup ( "LiveTV.nuv" );
   428 				file_info->size = gmyth_recorder_get_file_position( recorder );
   429 			}
   430 			
   431 	    if ( recorder != NULL )
   432 	    	g_object_unref (recorder);
   433 	    
   434 	    if ( prog_info != NULL )
   435 	    	g_object_unref( prog_info );
   436 
   437 	    if ( tvchain != NULL )
   438 	    	g_object_unref (tvchain);
   439 	    
   440 	    file_info->size = (GnomeVFSFileSize) - 1;
   441 
   442 	  } else {
   443 	  	
   444 	  	/* start to get file info from remote file encoder */	
   445 	    file_transfer = gmyth_file_transfer_new (backend_info);
   446 	    
   447 	    /* Verifies if the file exists */
   448 	    if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   449 	        g_object_unref (backend_info);
   450 					return GNOME_VFS_ERROR_NOT_FOUND;
   451 	    }
   452 	    
   453 	    /* sets the Playback monitor connection */
   454 	    ret = gmyth_file_transfer_open ( file_transfer, gmyth_uri_get_path (gmyth_uri) );
   455 	    
   456 	    file_info->name = g_strdup ( gnome_vfs_uri_get_path (uri) );
   457 		
   458 	  } /* if - LiveTV or not? */	  
   459 	  
   460     if (ret == FALSE) {
   461 	    g_debug ("MythTV FileTransfer open error\n");
   462 	    return GNOME_VFS_ERROR_NOT_OPEN;
   463 	  }
   464 	  
   465 		if ( ret == TRUE  && file_transfer != NULL ) {
   466     	file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
   467 	    if ( file_transfer )
   468 	    	g_object_unref (file_transfer);
   469    	}
   470 
   471     file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
   472 	  file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
   473 	  
   474 error:
   475     if (backend_info)
   476    		g_object_unref (backend_info);
   477    		
   478    	if (!res)
   479    		return GNOME_VFS_ERROR_IO;
   480 
   481     return GNOME_VFS_OK;
   482 }
   483 
   484 static gboolean
   485 do_is_local (GnomeVFSMethod *method,
   486              const GnomeVFSURI *uri)
   487 {
   488 	return FALSE;
   489 }
   490 
   491 static GnomeVFSMethod method = {
   492 	sizeof (GnomeVFSMethod),
   493 	do_open,
   494 	NULL,
   495 	do_close,
   496 	do_read,
   497 	NULL,
   498 	NULL,
   499 	NULL,
   500 	NULL,
   501 	NULL,
   502 	NULL,
   503 	NULL,
   504 	do_get_file_info,
   505 	NULL,
   506 	do_is_local,
   507 	NULL,
   508 	NULL,
   509 	NULL,
   510 	NULL,
   511 	NULL,
   512 	NULL,
   513 	NULL,
   514 	NULL,
   515 	NULL,
   516 	NULL,
   517 	NULL,
   518 	NULL,
   519 };
   520 
   521 
   522 GnomeVFSMethod *
   523 vfs_module_init (const char *method_name, const char *args)
   524 {
   525 	return &method;
   526 }
   527 
   528 void
   529 vfs_module_shutdown (GnomeVFSMethod *method)
   530 {
   531 }