libgnomevfs2-mythtv/modules/mythtv-method.c
author rosfran
Mon Feb 12 14:52:45 2007 +0000 (2007-02-12)
branchtrunk
changeset 353 f3b8627a51e4
parent 344 21a15c29957b
child 354 ce009d76ffb5
permissions -rwxr-xr-x
[svn r355] Added G_IO_IN event handlers, using g_io_add_watch, to the GMyth's MonitorHandler.
     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->is_livetv && ( myth_handle->content_size > 0 ) && 
   220   				( num_bytes > ( myth_handle->content_size - myth_handle->bytes_read ) ) )
   221     bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   222   else
   223     bytes_to_read = num_bytes;
   224 
   225   /* Loop sending the Myth File Transfer request:
   226   * Retry whilst authentication fails and we supply it. */
   227   //if (myth_handle->buffer_remain  < MYTHTV_BUFFER_SIZE) {
   228   if ( ( myth_handle->buffer_remain = myth_handle->buffer->len ) < MYTHTV_BUFFER_SIZE ) {
   229   	GByteArray *tmp_buffer = g_byte_array_new();
   230 
   231 		printf ("XXXXXXXXXXXXXX Pedindo %d %d\n", MYTHTV_BUFFER_SIZE, myth_handle->buffer_remain);
   232 
   233     gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
   234           tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
   235 
   236 		if (!myth_handle->is_livetv && len < 0) {
   237 	    g_byte_array_free (tmp_buffer, TRUE);
   238 	    g_print ("Fail to read bytes");
   239 	    return GNOME_VFS_ERROR_IO;
   240 	  } /*else if (len == 0) {
   241 		    g_byte_array_free (tmp_buffer, TRUE);
   242 		    g_warning ("End of file probably achieved");
   243 		    return GNOME_VFS_ERROR_EOF;
   244 		}*/
   245 	
   246 	    myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
   247 	    		tmp_buffer->data, len);
   248 	
   249 			myth_handle->buffer_remain += len;
   250 
   251       g_byte_array_free (tmp_buffer, TRUE);
   252     	tmp_buffer = NULL;
   253     }
   254     
   255     bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
   256     /* gets the first buffer_size bytes from the byte array buffer variable */ 
   257 
   258     g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
   259 
   260     myth_handle->bytes_read += bytes_to_read;
   261     myth_handle->buffer_remain -= bytes_to_read;    
   262 
   263   	/* flushs the newly buffer got from byte array */
   264   	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
   265     *bytes_read = bytes_to_read;
   266   
   267     return GNOME_VFS_OK;
   268 }
   269 
   270 static GnomeVFSResult
   271 do_close (GnomeVFSMethod *method,
   272           GnomeVFSMethodHandle *method_handle,
   273           GnomeVFSContext *context)
   274 {
   275 
   276     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   277 
   278     if (myth_handle->file_transfer != NULL) {
   279 	    //gmyth_file_transfer_close (myth_handle->file_transfer);
   280       g_object_unref (myth_handle->file_transfer);
   281     	myth_handle->file_transfer = NULL;
   282     }
   283     
   284     if (myth_handle->is_livetv && myth_handle->livetv != NULL) {
   285       g_object_unref (myth_handle->livetv);
   286     	myth_handle->livetv = NULL;
   287     }
   288     
   289     if (myth_handle->buffer) {
   290 		g_byte_array_free (myth_handle->buffer, TRUE);
   291 		myth_handle->buffer = NULL;
   292     }
   293 
   294     g_free (myth_handle);
   295 
   296     return GNOME_VFS_OK;
   297 }
   298 
   299 static GnomeVFSResult
   300 do_get_file_info (GnomeVFSMethod *method,
   301                   GnomeVFSURI *uri,
   302                   GnomeVFSFileInfo *file_info,
   303                   GnomeVFSFileInfoOptions options,
   304                   GnomeVFSContext *context)
   305 {
   306     GMythFileTransfer *file_transfer = NULL;
   307     GMythRecorder			*recorder 		 = NULL;
   308     GMythTVChain			*tvchain	 		 = NULL;
   309     GMythBackendInfo  *backend_info  = NULL;
   310     GMythURI					*gmyth_uri		 = NULL;
   311 		GMythSocket 			*socket 			 = NULL;		 
   312     gboolean 					is_livetv 		 = FALSE;
   313     gboolean					ret						 = TRUE;
   314     gboolean 					res 					 = TRUE;
   315     
   316     /* Creates and fills out the backend info structure */    
   317 	  backend_info = gmyth_backend_info_new_with_uri ( 
   318 	  			gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   319 	  
   320 	  /* creates an instance of */  
   321 	  gmyth_uri = gmyth_uri_new_with_value( 
   322 	  		gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   323 
   324 	  is_livetv = gmyth_uri_is_livetv( gmyth_uri );
   325 
   326     file_info->valid_fields = file_info->valid_fields
   327         | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   328         | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   329         | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   330     file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   331     /* fixme: get from file extension? */
   332     file_info->mime_type = g_strdup ("video/x-nuv");
   333     file_info->permissions =
   334         GNOME_VFS_PERM_USER_READ |
   335         GNOME_VFS_PERM_OTHER_READ |
   336         GNOME_VFS_PERM_GROUP_READ;
   337         
   338     g_print( "gnome_vfs_uri == %s | gmyth_uri == %s.\n",
   339     				gnome_vfs_uri_get_path (uri),
   340     				gmyth_uri_get_path (gmyth_uri) );
   341 
   342 	  /* Connect to the backend */
   343 	  if ( gmyth_uri != NULL && is_livetv == TRUE ) {
   344 	  	
   345 	  	/* start to get file info from LiveTV remote encoder */	  	
   346 			socket = gmyth_socket_new ();
   347 			
   348 			/* FIME: Implement this at gmyth_socket */
   349 			res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
   350 					backend_info->port, TRUE);
   351 			if (!res) {
   352 				g_print ("[%s] LiveTV can not connect to backend", __FUNCTION__);	
   353 				res = FALSE;
   354 				goto error;
   355 			}
   356 
   357 			if ( gmyth_remote_util_get_free_recorder_count (socket) <= 0 ) {
   358 				g_print ("No free remote encoder available.");
   359 				res = FALSE;
   360 				goto error;
   361 			}
   362 		
   363 			/* Gets the recorder num */
   364 			recorder = remote_request_next_free_recorder (socket, -1);
   365 			
   366 			//if ( socket != NULL )
   367 			//	g_object_unref (socket);
   368 		
   369 			if ( recorder == NULL ) {
   370 				g_print ("[%s] None remote encoder available", __FUNCTION__);
   371 				res = FALSE;
   372 				goto error;
   373 			}
   374 			
   375 			/* Init remote encoder. Opens its control socket. */
   376 			res = gmyth_recorder_setup(recorder);
   377 			if ( !res ) {
   378 				g_print ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
   379 				res = FALSE;
   380 				goto error;
   381 			}
   382 			
   383 			/* Creates livetv chain handler */
   384 			tvchain = gmyth_tvchain_new();
   385 			gmyth_tvchain_initialize ( tvchain, backend_info );
   386 		
   387 			if ( tvchain == NULL || tvchain->tvchain_id == NULL ) {
   388 				res = FALSE;
   389 				goto error;
   390 			}
   391 			
   392 			// Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly)
   393 			res = gmyth_recorder_spawntv ( recorder,
   394 					gmyth_tvchain_get_id(tvchain) );
   395 			if (!res) {
   396 				g_warning ("[%s] Fail while spawn tv\n", __FUNCTION__);
   397 				res = FALSE;
   398 				goto error;
   399 			}		
   400 			
   401 		  //gchar* channel_name = gmyth_uri_get_channel_name( gmyth_uri );
   402 	
   403 		  /* DEBUG message */
   404 			GMythProgramInfo* prog_info = gmyth_recorder_get_current_program_info( recorder );
   405 			
   406 			if ( prog_info != NULL )
   407 			{
   408 				gmyth_program_info_print( prog_info );
   409   
   410 		    g_print( "path = %s",  prog_info->pathname->str );
   411 		    
   412 		    file_info->name = g_strdup ( g_strrstr( prog_info->pathname->str, "/" ) );
   413 		    
   414 			} else {
   415 				file_info->name = g_strdup ( "LiveTV.nuv" );
   416 				file_info->size = gmyth_recorder_get_file_position( recorder );
   417 			}
   418 			
   419 	    if ( recorder != NULL )
   420 	    	g_object_unref (recorder);
   421 	    
   422 	    if ( prog_info != NULL )
   423 	    	g_object_unref( prog_info );
   424 
   425 	    if ( tvchain != NULL )
   426 	    	g_object_unref (tvchain);
   427 	    
   428 	    file_info->size = (GnomeVFSFileSize) - 1;
   429 
   430 	  } else {
   431 	  	
   432 	  	/* start to get file info from remote file encoder */	
   433 	    file_transfer = gmyth_file_transfer_new (backend_info);
   434 	    
   435 	    /* Verifies if the file exists */
   436 	    if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   437 	        g_object_unref (backend_info);
   438 					return GNOME_VFS_ERROR_NOT_FOUND;
   439 	    }
   440 	    
   441 	    /* sets the Playback monitor connection */
   442 	    ret = gmyth_file_transfer_open ( file_transfer, gmyth_uri_get_path (gmyth_uri) );
   443 	    
   444 	    file_info->name = g_strdup ( gnome_vfs_uri_get_path (uri) );
   445 		
   446 	  } /* if - LiveTV or not? */	  
   447 	  
   448     if (ret == FALSE) {
   449 	    g_debug ("MythTV FileTransfer open error\n");
   450 	    return GNOME_VFS_ERROR_NOT_OPEN;
   451 	  }
   452 	  
   453 		if ( ret == TRUE  && file_transfer != NULL ) {
   454     	file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
   455 	    if ( file_transfer )
   456 	    	g_object_unref (file_transfer);
   457    	}
   458 
   459     file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
   460 	  file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
   461 	  
   462 error:
   463     if (backend_info)
   464    		g_object_unref (backend_info);
   465    		
   466    	if (!res)
   467    		return GNOME_VFS_ERROR_IO;
   468 
   469     return GNOME_VFS_OK;
   470 }
   471 
   472 static gboolean
   473 do_is_local (GnomeVFSMethod *method,
   474              const GnomeVFSURI *uri)
   475 {
   476 	return FALSE;
   477 }
   478 
   479 static GnomeVFSMethod method = {
   480 	sizeof (GnomeVFSMethod),
   481 	do_open,
   482 	NULL,
   483 	do_close,
   484 	do_read,
   485 	NULL,
   486 	NULL,
   487 	NULL,
   488 	NULL,
   489 	NULL,
   490 	NULL,
   491 	NULL,
   492 	do_get_file_info,
   493 	NULL,
   494 	do_is_local,
   495 	NULL,
   496 	NULL,
   497 	NULL,
   498 	NULL,
   499 	NULL,
   500 	NULL,
   501 	NULL,
   502 	NULL,
   503 	NULL,
   504 	NULL,
   505 	NULL,
   506 	NULL,
   507 };
   508 
   509 
   510 GnomeVFSMethod *
   511 vfs_module_init (const char *method_name, const char *args)
   512 {
   513 	return &method;
   514 }
   515 
   516 void
   517 vfs_module_shutdown (GnomeVFSMethod *method)
   518 {
   519 }