libgnomevfs2-mythtv/modules/mythtv-method.c
author rosfran
Thu Feb 08 21:56:58 2007 +0000 (2007-02-08)
branchtrunk
changeset 340 84fb21f6941f
parent 338 6d9f4e8468b3
child 341 64ded911540b
permissions -rwxr-xr-x
[svn r342] Put back the do_get_file_info to the LiveTV.
     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*64
    44 
    45 static GnomeVFSResult do_read (GnomeVFSMethod *method,
    46                                GnomeVFSMethodHandle *method_handle,
    47                                gpointer buffer,
    48                                GnomeVFSFileSize num_bytes,
    49                                GnomeVFSFileSize *bytes_read,
    50                                GnomeVFSContext *context);
    51 
    52 typedef struct {
    53     GMythFileTransfer *file_transfer;
    54     GMythLiveTV 			*livetv;
    55     gchar 						*channel_name;
    56     
    57     gint mythtv_version;
    58     gint64 content_size;
    59     guint64 bytes_read;
    60 
    61     GByteArray *buffer;
    62     gsize buffer_remain;
    63     gboolean	is_livetv;
    64     
    65 } MythtvHandle;
    66 
    67 static GnomeVFSResult
    68 do_open (GnomeVFSMethod *method,
    69          GnomeVFSMethodHandle **method_handle,
    70          GnomeVFSURI *uri,
    71          GnomeVFSOpenMode mode,
    72          GnomeVFSContext *context)
    73 {
    74     MythtvHandle *myth_handle;
    75     GMythBackendInfo *backend_info;
    76     GMythURI *gmyth_uri = NULL;
    77     gboolean ret = TRUE;
    78 
    79     _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
    80     _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
    81 
    82     myth_handle = g_new0 (MythtvHandle, 1);
    83     
    84     myth_handle->is_livetv = FALSE;
    85 
    86     if (mode & GNOME_VFS_OPEN_WRITE) {
    87         return GNOME_VFS_ERROR_NOT_PERMITTED;
    88     }
    89 
    90     if (gnome_vfs_uri_get_host_name (uri) == NULL) {
    91     	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
    92     }
    93 
    94     /* Initialize mythtv handler*/
    95     myth_handle->file_transfer = NULL;
    96     myth_handle->livetv = NULL;
    97     myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
    98     myth_handle->bytes_read = 0;
    99     myth_handle->content_size = (GnomeVFSFileSize) - 1;
   100 
   101     /* Creates and fills out the backend info structure */    
   102 	  backend_info = gmyth_backend_info_new_with_uri ( 
   103 	  			gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   104 	  
   105 	  /* creates an instance of  */  
   106 	  gmyth_uri = gmyth_uri_new_with_value( 
   107 	  		gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   108 
   109 	  myth_handle->is_livetv = gmyth_uri_is_livetv( gmyth_uri );
   110 
   111 	  /* Connect to the backend */	  
   112 	  if ( gmyth_uri != NULL && myth_handle->is_livetv == TRUE ) {
   113 	  	
   114 	  	if ( NULL == myth_handle->livetv ) 
   115 	  	{
   116 	    	myth_handle->livetv = gmyth_livetv_new ();
   117 	    
   118 		    myth_handle->channel_name = gmyth_uri_get_channel_name( gmyth_uri );
   119 		    
   120 		    g_debug( "[%s] Channel name = %s\n", __FUNCTION__, myth_handle->channel_name );
   121 		
   122 		    if ( myth_handle->channel_name != NULL ) {
   123 		      if (gmyth_livetv_channel_name_setup (myth_handle->livetv, myth_handle->channel_name,
   124 		              backend_info) == FALSE) {
   125 		        g_object_unref( gmyth_uri );
   126 		        ret = FALSE;
   127 		      }
   128 		    } else {
   129 		      if ( gmyth_livetv_setup (myth_handle->livetv, backend_info) == FALSE ) {
   130 		      	g_object_unref( gmyth_uri );
   131 		        ret = FALSE;
   132 		      }
   133 		    }
   134 	  	}
   135 	  	
   136 	  	if ( NULL == myth_handle->file_transfer )	{
   137 	    	myth_handle->file_transfer = gmyth_livetv_create_file_transfer (myth_handle->livetv);
   138 	
   139 		    if (NULL == myth_handle->file_transfer) {
   140 		      ret = FALSE;
   141 			    g_debug ("MythTV FileTransfer is NULL!\n");
   142 			    return GNOME_VFS_ERROR_NOT_OPEN;
   143 		    }
   144 		    
   145 	  		if ( !gmyth_file_transfer_open( myth_handle->file_transfer, myth_handle->livetv->uri != NULL ? 
   146 									gmyth_uri_get_path(myth_handle->livetv->uri) : 
   147 									myth_handle->livetv->proginfo->pathname->str ) )
   148 				{
   149 					g_debug ("Couldn't open MythTV FileTransfer is NULL!\n");
   150 					g_object_unref( myth_handle->file_transfer );
   151 					myth_handle->file_transfer = NULL;
   152 					ret = FALSE;
   153 				}
   154 	  	} /* if - FileTransfer is NULL, or not */
   155 	    
   156 	  } else {
   157 	  	
   158 	  	if (NULL == myth_handle->file_transfer ) {
   159 	
   160 		    myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
   161 		    
   162 		    /* Verifies if the file exists */
   163 		    if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   164 		        g_object_unref (backend_info);
   165 						ret = FALSE;
   166 		    }
   167 		    
   168 		    /* sets the Playback monitor connection */
   169 		    ret = gmyth_file_transfer_open ( myth_handle->file_transfer, 
   170 		    		gmyth_uri_get_path (gmyth_uri) );
   171 		    		
   172 	  	}
   173 		
   174 	  } /* if - LiveTV or not? */
   175 	  
   176     if (ret == FALSE) {
   177 	    g_debug ("MythTV FileTransfer open error.\n");
   178 	    return GNOME_VFS_ERROR_NOT_OPEN;
   179 	  }
   180 
   181     //g_object_unref (backend_info);
   182     
   183 	  //if ( gmyth_uri != NULL )
   184 	  //	g_object_unref( gmyth_uri );
   185 	  
   186 	  g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
   187 	  
   188 	  if ( /*myth_handle->file_transfer->filesize <= 0 && */myth_handle->is_livetv ) {
   189 	  	myth_handle->content_size = (GnomeVFSFileSize) - 1;
   190 			//myth_handle->content_size = gmyth_recorder_get_file_position( myth_handle->livetv->recorder );
   191 	  } else		
   192     	myth_handle->content_size = myth_handle->file_transfer->filesize;
   193     
   194     myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
   195     myth_handle->buffer_remain = 0;
   196 
   197     *method_handle = (GnomeVFSMethodHandle *) myth_handle;
   198 
   199     return GNOME_VFS_OK;
   200 }
   201 
   202 static GnomeVFSResult
   203 do_read (GnomeVFSMethod *method,
   204          GnomeVFSMethodHandle *method_handle,
   205          gpointer buffer,
   206          GnomeVFSFileSize num_bytes,
   207          GnomeVFSFileSize *bytes_read,
   208          GnomeVFSContext *context)
   209 {
   210   MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   211   GnomeVFSFileSize bytes_to_read;
   212 
   213   *bytes_read = 0;
   214 
   215   if ( !myth_handle->is_livetv && ( myth_handle->bytes_read >= myth_handle->content_size ) )
   216       return GNOME_VFS_ERROR_EOF;
   217 
   218   // fixme: change this to min math function
   219   if (myth_handle->content_size > 0 && num_bytes > ( myth_handle->content_size - myth_handle->bytes_read ))
   220     bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   221   else
   222     bytes_to_read = num_bytes;
   223 
   224   /* Loop sending the Myth File Transfer request:
   225   * Retry whilst authentication fails and we supply it. */
   226   //if (myth_handle->buffer_remain  < MYTHTV_BUFFER_SIZE) {
   227   if ( ( myth_handle->buffer_remain = myth_handle->buffer->len ) < MYTHTV_BUFFER_SIZE ) {
   228   	GByteArray *tmp_buffer = g_byte_array_new();
   229 
   230 		printf ("XXXXXXXXXXXXXX Pedindo %d %d\n", MYTHTV_BUFFER_SIZE, myth_handle->buffer_remain);
   231 
   232     gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
   233           tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
   234 
   235 		if (len < 0) {
   236 	    g_byte_array_free (tmp_buffer, TRUE);
   237 	    g_print ("Fail to read bytes");
   238 	    return GNOME_VFS_ERROR_IO;
   239 	  } /*else if (len == 0) {
   240 		    g_byte_array_free (tmp_buffer, TRUE);
   241 		    g_warning ("End of file probably achieved");
   242 		    return GNOME_VFS_ERROR_EOF;
   243 		}*/
   244 	
   245 	    myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
   246 	    		tmp_buffer->data, len);
   247 	
   248 			myth_handle->buffer_remain += len;
   249 
   250       g_byte_array_free (tmp_buffer, TRUE);
   251     	tmp_buffer = NULL;
   252     }
   253     
   254     bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
   255     /* gets the first buffer_size bytes from the byte array buffer variable */ 
   256 
   257     g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
   258 
   259     myth_handle->bytes_read += bytes_to_read;
   260     myth_handle->buffer_remain -= bytes_to_read;    
   261 
   262   	/* flushs the newly buffer got from byte array */
   263   	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
   264     *bytes_read = bytes_to_read;
   265   
   266     return GNOME_VFS_OK;
   267 }
   268 
   269 static GnomeVFSResult
   270 do_close (GnomeVFSMethod *method,
   271           GnomeVFSMethodHandle *method_handle,
   272           GnomeVFSContext *context)
   273 {
   274 
   275     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   276 
   277     if (myth_handle->file_transfer) {
   278 	    //gmyth_file_transfer_close (myth_handle->file_transfer);
   279       g_object_unref (myth_handle->file_transfer);
   280     	myth_handle->file_transfer = NULL;
   281     }
   282     
   283     if (myth_handle->is_livetv && myth_handle->livetv != NULL) {
   284       g_object_unref (myth_handle->livetv);
   285     	myth_handle->livetv = NULL;
   286     }
   287     
   288     if (myth_handle->buffer) {
   289 		g_byte_array_free (myth_handle->buffer, TRUE);
   290 		myth_handle->buffer = NULL;
   291     }
   292 
   293     g_free (myth_handle);
   294 
   295     return GNOME_VFS_OK;
   296 }
   297 
   298 static GnomeVFSResult
   299 do_get_file_info (GnomeVFSMethod *method,
   300                   GnomeVFSURI *uri,
   301                   GnomeVFSFileInfo *file_info,
   302                   GnomeVFSFileInfoOptions options,
   303                   GnomeVFSContext *context)
   304 {
   305     GMythFileTransfer *file_transfer = NULL;
   306     GMythRecorder			*recorder 		 = NULL;
   307     GMythBackendInfo  *backend_info  = NULL;
   308     GMythURI					*gmyth_uri		 = NULL;
   309 		GMythSocket 			*socket 			 = NULL;		 
   310     gboolean 					is_livetv 		 = FALSE;
   311     gboolean					ret						 = TRUE;
   312     
   313     /* Creates and fills out the backend info structure */    
   314 	  backend_info = gmyth_backend_info_new_with_uri ( 
   315 	  			gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   316 	  
   317 	  /* creates an instance of */  
   318 	  gmyth_uri = gmyth_uri_new_with_value( 
   319 	  		gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   320 
   321 	  is_livetv = gmyth_uri_is_livetv( gmyth_uri );
   322 
   323     file_info->valid_fields = file_info->valid_fields
   324         | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   325         | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   326         | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   327     file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   328     /* fixme: get from file extension? */
   329     file_info->mime_type = g_strdup ("video/x-nuv");
   330     file_info->permissions =
   331         GNOME_VFS_PERM_USER_READ |
   332         GNOME_VFS_PERM_OTHER_READ |
   333         GNOME_VFS_PERM_GROUP_READ;
   334         
   335     g_print( "gnome_vfs_uri == %s | gmyth_uri == %s.\n",
   336     				gnome_vfs_uri_get_path (uri),
   337     				gmyth_uri_get_path (gmyth_uri) );
   338 
   339 	  /* Connect to the backend */
   340 	  if ( gmyth_uri != NULL && is_livetv == TRUE ) {
   341 	  	gboolean res = TRUE;
   342 	  	
   343 	  	/* start to get file info from LiveTV remote encoder */	  	
   344 			socket = gmyth_socket_new ();
   345 			
   346 			/* FIME: Implement this at gmyth_socket */
   347 			res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
   348 					backend_info->port, TRUE);
   349 			if (!res) {
   350 				g_print ("[%s] LiveTV can not connect to backend", __FUNCTION__);	
   351 				res = FALSE;
   352 				goto error;
   353 			}
   354 
   355 			if ( gmyth_remote_util_get_free_recorder_count (socket) <= 0 ) {
   356 				g_print ("No free remote encoder available.");
   357 				res = FALSE;
   358 				goto error;
   359 			}
   360 		
   361 			/* Gets the recorder num */
   362 			recorder = remote_request_next_free_recorder (socket, -1);
   363 			
   364 			if ( socket != NULL )
   365 				g_object_unref (socket);
   366 		
   367 			if ( recorder == NULL ) {
   368 				g_print ("[%s] None remote encoder available", __FUNCTION__);
   369 				res = FALSE;
   370 				goto error;
   371 			}
   372 			
   373 			/* Init remote encoder. Opens its control socket. */
   374 			res = gmyth_recorder_setup(recorder);
   375 			if ( !res ) {
   376 				g_print ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
   377 				res = FALSE;
   378 				goto error;
   379 			}			
   380 			
   381 		  //gchar* channel_name = gmyth_uri_get_channel_name( gmyth_uri );
   382 	
   383 		  /* DEBUG message */
   384 			GMythProgramInfo* prog_info = gmyth_recorder_get_current_program_info( recorder );
   385 			
   386 			if ( prog_info != NULL )
   387 			{
   388 				gmyth_program_info_print( prog_info );
   389   
   390 		    g_print( "path = %s",  prog_info->pathname->str );
   391 		    
   392 		    file_info->name = g_strdup ( g_strrstr( prog_info->pathname->str, "/" ) );
   393 		    
   394 	  		gint64 size = 0;
   395 	  		size = gmyth_recorder_get_file_position( recorder );
   396 	  		if( size < 0 )
   397 	  		{
   398 	  			file_info->size = prog_info->filesize;
   399 	  		}
   400 				else
   401 					file_info->size = size; 
   402 		    
   403 			} else {
   404 				file_info->size = gmyth_recorder_get_file_position( recorder );
   405 			}
   406 			
   407 	    if ( recorder != NULL )
   408 	    	g_object_unref (recorder);
   409 	    
   410 	    if ( prog_info != NULL )
   411 	    	g_object_unref( prog_info );
   412 	    
   413 	    /*
   414 	    file_info->size = (GnomeVFSFileSize) - 1;
   415 	    file_info->name = g_strdup ( "LiveTV.nuv" );
   416 	    */
   417 
   418 	  } else {
   419 	  	
   420 	  	/* start to get file info from remote file encoder */	
   421 	    file_transfer = gmyth_file_transfer_new (backend_info);
   422 	    
   423 	    /* Verifies if the file exists */
   424 	    if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   425 	        g_object_unref (backend_info);
   426 					return GNOME_VFS_ERROR_NOT_FOUND;
   427 	    }
   428 	    
   429 	    /* sets the Playback monitor connection */
   430 	    ret = gmyth_file_transfer_open ( file_transfer, gmyth_uri_get_path (gmyth_uri) );
   431 	    
   432 	    file_info->name = g_strdup ( gnome_vfs_uri_get_path (uri) );
   433 		
   434 	  } /* if - LiveTV or not? */	  
   435 	  
   436     if (ret == FALSE) {
   437 	    g_debug ("MythTV FileTransfer open error\n");
   438 	    return GNOME_VFS_ERROR_NOT_OPEN;
   439 	  }
   440 	  
   441 		if ( ret == TRUE  && file_transfer != NULL ) {
   442     	file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
   443 	    if ( file_transfer )
   444 	    	g_object_unref (file_transfer);
   445    	}
   446 
   447     file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
   448 	  file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
   449 	  
   450 error:
   451     if (backend_info)
   452    		g_object_unref (backend_info);
   453 
   454     return GNOME_VFS_OK;
   455 }
   456 
   457 static gboolean
   458 do_is_local (GnomeVFSMethod *method,
   459              const GnomeVFSURI *uri)
   460 {
   461 	return FALSE;
   462 }
   463 
   464 static GnomeVFSMethod method = {
   465 	sizeof (GnomeVFSMethod),
   466 	do_open,
   467 	NULL,
   468 	do_close,
   469 	do_read,
   470 	NULL,
   471 	NULL,
   472 	NULL,
   473 	NULL,
   474 	NULL,
   475 	NULL,
   476 	NULL,
   477 	do_get_file_info,
   478 	NULL,
   479 	do_is_local,
   480 	NULL,
   481 	NULL,
   482 	NULL,
   483 	NULL,
   484 	NULL,
   485 	NULL,
   486 	NULL,
   487 	NULL,
   488 	NULL,
   489 	NULL,
   490 	NULL,
   491 	NULL,
   492 };
   493 
   494 
   495 GnomeVFSMethod *
   496 vfs_module_init (const char *method_name, const char *args)
   497 {
   498 	return &method;
   499 }
   500 
   501 void
   502 vfs_module_shutdown (GnomeVFSMethod *method)
   503 {
   504 }