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