libgnomevfs2-mythtv/modules/mythtv-method.c
author rosfran
Thu Feb 08 20:57:59 2007 +0000 (2007-02-08)
branchtrunk
changeset 338 6d9f4e8468b3
parent 335 aedcbbf818b7
child 340 84fb21f6941f
permissions -rwxr-xr-x
[svn r340] LiveTV is running now.
     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) 0;
   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->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 	  	#if 0
   342 	  	gboolean res = TRUE;
   343 	  	
   344 	  	/* start to get file info from LiveTV remote encoder */	  	
   345 			socket = gmyth_socket_new ();
   346 			
   347 			/* FIME: Implement this at gmyth_socket */
   348 			res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
   349 					backend_info->port, TRUE);
   350 			if (!res) {
   351 				g_print ("[%s] LiveTV can not connect to backend", __FUNCTION__);	
   352 				res = FALSE;
   353 				goto error;
   354 			}
   355 
   356 			if ( gmyth_remote_util_get_free_recorder_count (socket) <= 0 ) {
   357 				g_print ("No free remote encoder available.");
   358 				res = FALSE;
   359 				goto error;
   360 			}
   361 		
   362 			/* Gets the recorder num */
   363 			recorder = remote_request_next_free_recorder (socket, -1);
   364 			
   365 			if ( socket != NULL )
   366 				g_object_unref (socket);
   367 		
   368 			if ( recorder == NULL ) {
   369 				g_print ("[%s] None remote encoder available", __FUNCTION__);
   370 				res = FALSE;
   371 				goto error;
   372 			}
   373 			
   374 			/* Init remote encoder. Opens its control socket. */
   375 			res = gmyth_recorder_setup(recorder);
   376 			if ( !res ) {
   377 				g_print ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
   378 				res = FALSE;
   379 				goto error;
   380 			}			
   381 			
   382 		  //gchar* channel_name = gmyth_uri_get_channel_name( gmyth_uri );
   383 	
   384 		  /* DEBUG message */
   385 			GMythProgramInfo* prog_info = gmyth_recorder_get_current_program_info( recorder );
   386 			
   387 			if ( prog_info != NULL )
   388 			{
   389 				//gmyth_debug( "New ProgramInfo...\n" );
   390 				gmyth_program_info_print( prog_info );
   391   
   392 		    g_print( "path = %s",  prog_info->pathname->str );
   393 		    
   394 		    file_info->name = g_strdup ( g_strrstr( prog_info->pathname->str, "/" ) );
   395 		    
   396 	  		gint64 size = 0;
   397 	  		size = gmyth_recorder_get_file_position( recorder );
   398 	  		if( size < 0 )
   399 	  		{
   400 	  			//GMythProgramInfo *prog_info = gmyth_recorder_get_current_program_info ( recorder );
   401 	  			file_info->size = prog_info->filesize;
   402 	  		}
   403 				else
   404 					file_info->size = size; 
   405 		    
   406 			} else {
   407 				file_info->size = gmyth_recorder_get_file_position( recorder );
   408 			}
   409 			
   410 	    if ( recorder != NULL )
   411 	    	g_object_unref (recorder);
   412 	    
   413 	    if ( prog_info != NULL )
   414 	    	g_object_unref( prog_info );
   415 	    
   416 	    #endif
   417 	    file_info->size = 0;
   418 	    file_info->name = g_strdup ( "LiveTV.nuv" );
   419 
   420 	  } else {
   421 	  	
   422 	  	/* start to get file info from remote file encoder */	
   423 	    file_transfer = gmyth_file_transfer_new (backend_info);
   424 	    
   425 	    /* Verifies if the file exists */
   426 	    if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   427 	        g_object_unref (backend_info);
   428 					return GNOME_VFS_ERROR_NOT_FOUND;
   429 	    }
   430 	    
   431 	    /* sets the Playback monitor connection */
   432 	    ret = gmyth_file_transfer_open ( file_transfer, gmyth_uri_get_path (gmyth_uri) );
   433 	    
   434 	    file_info->name = g_strdup ( gnome_vfs_uri_get_path (uri) );
   435 		
   436 	  } /* if - LiveTV or not? */	  
   437 	  
   438     if (ret == FALSE) {
   439 	    g_debug ("MythTV FileTransfer open error\n");
   440 	    return GNOME_VFS_ERROR_NOT_OPEN;
   441 	  }
   442 	  
   443 		if ( ret == TRUE  && file_transfer != NULL ) {
   444     	file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
   445 	    if ( file_transfer )
   446 	    	g_object_unref (file_transfer);
   447    	}
   448 
   449     file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
   450 	  file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
   451 	  
   452 //error:
   453     if (backend_info)
   454    		g_object_unref (backend_info);
   455 
   456     return GNOME_VFS_OK;
   457 }
   458 
   459 static gboolean
   460 do_is_local (GnomeVFSMethod *method,
   461              const GnomeVFSURI *uri)
   462 {
   463 	return FALSE;
   464 }
   465 
   466 static GnomeVFSMethod method = {
   467 	sizeof (GnomeVFSMethod),
   468 	do_open,
   469 	NULL,
   470 	do_close,
   471 	do_read,
   472 	NULL,
   473 	NULL,
   474 	NULL,
   475 	NULL,
   476 	NULL,
   477 	NULL,
   478 	NULL,
   479 	do_get_file_info,
   480 	NULL,
   481 	do_is_local,
   482 	NULL,
   483 	NULL,
   484 	NULL,
   485 	NULL,
   486 	NULL,
   487 	NULL,
   488 	NULL,
   489 	NULL,
   490 	NULL,
   491 	NULL,
   492 	NULL,
   493 	NULL,
   494 };
   495 
   496 
   497 GnomeVFSMethod *
   498 vfs_module_init (const char *method_name, const char *args)
   499 {
   500 	return &method;
   501 }
   502 
   503 void
   504 vfs_module_shutdown (GnomeVFSMethod *method)
   505 {
   506 }