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