libgnomevfs2-mythtv/modules/mythtv-method.c
author renatofilho
Thu Feb 01 18:31:29 2007 +0000 (2007-02-01)
branchtrunk
changeset 319 69382cca242e
parent 301 c3fdac309aec
child 323 6523ab923c00
permissions -rwxr-xr-x
[svn r320] fixed a lot of bugs
     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 
    37 #define GST_MYTHTV_ID_NUM               1
    38 #define MYTHTV_VERSION_DEFAULT          30
    39 #define MYTHTV_TRANSFER_MAX_WAITS       100
    40 
    41 #define MYTHTV_BUFFER_SIZE		1024*64
    42 
    43 static GnomeVFSResult do_read (GnomeVFSMethod *method,
    44                                GnomeVFSMethodHandle *method_handle,
    45                                gpointer buffer,
    46                                GnomeVFSFileSize num_bytes,
    47                                GnomeVFSFileSize *bytes_read,
    48                                GnomeVFSContext *context);
    49 
    50 typedef struct {
    51     GMythFileTransfer *file_transfer;
    52     GMythLiveTV 			*livetv;
    53     gint 							channel_num;
    54     
    55     gint mythtv_version;
    56     gint64 content_size;
    57     guint64 bytes_read;
    58 
    59     GByteArray *buffer;
    60     gsize buffer_remain;
    61 } MythtvHandle;
    62 
    63 static GnomeVFSResult
    64 do_open (GnomeVFSMethod *method,
    65          GnomeVFSMethodHandle **method_handle,
    66          GnomeVFSURI *uri,
    67          GnomeVFSOpenMode mode,
    68          GnomeVFSContext *context)
    69 {
    70     MythtvHandle *myth_handle;
    71     GMythBackendInfo *backend_info;
    72     GMythURI *gmyth_uri = NULL;
    73     gboolean ret = TRUE;
    74     gboolean is_livetv = FALSE;
    75 
    76     _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
    77     _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
    78 
    79     myth_handle = g_new0 (MythtvHandle, 1);
    80 
    81     if (mode & GNOME_VFS_OPEN_WRITE) {
    82         return GNOME_VFS_ERROR_NOT_PERMITTED;
    83     }
    84 
    85     if (gnome_vfs_uri_get_host_name (uri) == NULL) {
    86     	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
    87     }
    88 
    89     /* Initialize mythtv handler*/
    90     myth_handle->file_transfer = NULL;
    91     myth_handle->livetv = NULL;
    92     myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
    93     myth_handle->bytes_read = 0;
    94     myth_handle->content_size = 0;
    95 
    96     /* Creates and fills out the backend info structure */    
    97 	  backend_info = gmyth_backend_info_new_with_uri ( 
    98 	  			gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
    99 	  
   100 	  /* creates an instance of  */  
   101 	  gmyth_uri = gmyth_uri_new_with_value( 
   102 	  		gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   103 
   104 	  is_livetv = gmyth_uri_is_livetv( gmyth_uri );
   105 
   106 	  /* Connect to the backend */	  
   107 	  if ( gmyth_uri != NULL && is_livetv == TRUE ) {
   108 	    myth_handle->livetv = gmyth_livetv_new ();
   109 	    
   110 	    myth_handle->channel_num = gmyth_uri_get_channel_num( gmyth_uri );
   111 	
   112 	    if ( myth_handle->channel_num != -1 ) {
   113 	      if (gmyth_livetv_channel_setup (myth_handle->livetv, myth_handle->channel_num,
   114 	              backend_info) == FALSE) {
   115 	        g_object_unref( gmyth_uri );
   116 	        ret = FALSE;
   117 	      }
   118 	    } else {
   119 	      if ( gmyth_livetv_setup (myth_handle->livetv, backend_info) == FALSE ) {
   120 	      	g_object_unref( gmyth_uri );
   121 	        ret = FALSE;
   122 	      }
   123 	    }
   124 	
   125 	    myth_handle->file_transfer = gmyth_livetv_create_file_transfer (myth_handle->livetv);
   126 	
   127 	    if (NULL == myth_handle->file_transfer) {
   128 	      ret = FALSE;
   129 	    }
   130 	    
   131 	  } else {
   132 	
   133 	    myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
   134 	    
   135 	    /* Verifies if the file exists */
   136 	    if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   137 	        g_object_unref (backend_info);
   138 					return GNOME_VFS_ERROR_NOT_FOUND;
   139 	    }
   140 	    
   141 	    /* sets the Playback monitor connection */
   142 	    ret = gmyth_file_transfer_open ( myth_handle->file_transfer, gmyth_uri_get_path (gmyth_uri) );
   143 		
   144 	  } /* if - LiveTV or not? */
   145 	  
   146     if (ret == FALSE) {
   147 	    g_warning ("MythTV FileTransfer open error\n");
   148 	    return GNOME_VFS_ERROR_NOT_OPEN;
   149 	  } 
   150 
   151     g_object_unref (backend_info);
   152     
   153 	  //if ( gmyth_uri != NULL )
   154 	  //	g_object_unref( gmyth_uri );
   155 	  
   156 	  g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
   157 
   158     myth_handle->content_size = myth_handle->file_transfer->filesize;
   159     
   160     myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
   161     myth_handle->buffer_remain = 0;
   162 
   163     *method_handle = (GnomeVFSMethodHandle *) myth_handle;
   164 
   165     return GNOME_VFS_OK;
   166 }
   167 
   168 static GnomeVFSResult
   169 do_read (GnomeVFSMethod *method,
   170          GnomeVFSMethodHandle *method_handle,
   171          gpointer buffer,
   172          GnomeVFSFileSize num_bytes,
   173          GnomeVFSFileSize *bytes_read,
   174          GnomeVFSContext *context)
   175 {
   176     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   177     GnomeVFSFileSize bytes_to_read;
   178 
   179     *bytes_read = 0;
   180 
   181     if (myth_handle->bytes_read >= myth_handle->content_size)
   182         return GNOME_VFS_ERROR_EOF;
   183 
   184     // fixme: change this to min math function
   185     if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
   186 	    bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   187     else
   188 	    bytes_to_read = num_bytes;
   189 
   190     /* Loop sending the Myth File Transfer request:
   191     * Retry whilst authentication fails and we supply it. */
   192     //if (myth_handle->buffer_remain  < MYTHTV_BUFFER_SIZE) {
   193     if ( bytes_to_read > myth_handle->buffer_remain ) {
   194         GByteArray *tmp_buffer = g_byte_array_new();
   195 
   196 	printf ("XXXXXXXXXXXXXX Pedindo %d %d\n", MYTHTV_BUFFER_SIZE, myth_handle->buffer_remain);
   197 
   198         gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
   199               tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
   200 
   201 	if (len < 0) {
   202             g_byte_array_free (tmp_buffer, TRUE);
   203             g_warning ("Fail to read bytes");
   204 	    return GNOME_VFS_ERROR_IO;
   205         } /*else if (len == 0) {
   206 	    g_byte_array_free (tmp_buffer, TRUE);
   207 	    g_warning ("End of file probably achieved");
   208 	    return GNOME_VFS_ERROR_EOF;
   209 	}*/
   210 
   211         myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
   212     		tmp_buffer->data, len);
   213 
   214 		myth_handle->buffer_remain += len;
   215 
   216         g_byte_array_free (tmp_buffer, TRUE);
   217     	tmp_buffer = NULL;    	
   218     }
   219     
   220     bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
   221     /* gets the first buffer_size bytes from the byte array buffer variable */ 
   222 
   223     g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
   224 
   225     myth_handle->bytes_read += bytes_to_read;
   226     myth_handle->buffer_remain -= bytes_to_read;    
   227 
   228   	/* flushs the newly buffer got from byte array */
   229   	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
   230     *bytes_read = bytes_to_read;
   231   
   232     return GNOME_VFS_OK;
   233 }
   234 
   235 static GnomeVFSResult
   236 do_close (GnomeVFSMethod *method,
   237           GnomeVFSMethodHandle *method_handle,
   238           GnomeVFSContext *context)
   239 {
   240 
   241     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   242 
   243     if (myth_handle->file_transfer) {
   244 	    gmyth_file_transfer_close (myth_handle->file_transfer);
   245         g_object_unref (myth_handle->file_transfer);
   246     	myth_handle->file_transfer = NULL;
   247     }
   248     
   249     if (myth_handle->livetv) {
   250       g_object_unref (myth_handle->livetv);
   251     	myth_handle->livetv = NULL;
   252     }
   253     
   254     if (myth_handle->buffer) {
   255 		g_byte_array_free (myth_handle->buffer, TRUE);
   256 		myth_handle->buffer = NULL;
   257     }
   258 
   259     g_free (myth_handle);
   260 
   261     return GNOME_VFS_OK;
   262 }
   263 
   264 static GnomeVFSResult
   265 do_get_file_info (GnomeVFSMethod *method,
   266                   GnomeVFSURI *uri,
   267                   GnomeVFSFileInfo *file_info,
   268                   GnomeVFSFileInfoOptions options,
   269                   GnomeVFSContext *context)
   270 {
   271     GMythFileTransfer *file_transfer = NULL;
   272     GMythLiveTV 			*livetv 			 = NULL;
   273     GMythBackendInfo  *backend_info  = NULL;
   274     GMythURI					*gmyth_uri		 = NULL;
   275     gboolean 					is_livetv 		 = FALSE;
   276     gboolean					ret						 = TRUE;
   277     
   278     /* Creates and fills out the backend info structure */    
   279 	  backend_info = gmyth_backend_info_new_with_uri ( 
   280 	  			gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   281 	  
   282 	  /* creates an instance of */  
   283 	  gmyth_uri = gmyth_uri_new_with_value( 
   284 	  		gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   285 
   286 	  is_livetv = gmyth_uri_is_livetv( gmyth_uri );
   287 
   288     file_info->name = g_strdup (gmyth_uri_get_path (gmyth_uri)+1);
   289     file_info->valid_fields = file_info->valid_fields
   290         | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   291         | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   292         | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   293     file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   294     /* fixme: get from file extension? */
   295     file_info->mime_type = g_strdup ("video/x-nuv");
   296     file_info->permissions =
   297         GNOME_VFS_PERM_USER_READ |
   298         GNOME_VFS_PERM_OTHER_READ |
   299         GNOME_VFS_PERM_GROUP_READ;
   300 
   301 	  /* Connect to the backend */
   302 	  if ( gmyth_uri != NULL && is_livetv == TRUE ) {
   303 	    livetv = gmyth_livetv_new ();
   304 	    
   305 	    gint channel_num = gmyth_uri_get_channel_num( gmyth_uri );
   306 	
   307 	    if ( channel_num != -1 ) {
   308 	      if (gmyth_livetv_channel_setup (livetv, channel_num,
   309 	              backend_info) == FALSE) {
   310 	        g_object_unref( gmyth_uri );
   311 	        ret = FALSE;
   312 	      }
   313 	    } else {
   314 	      if ( gmyth_livetv_setup (livetv, backend_info) == FALSE ) {
   315 	      	g_object_unref( gmyth_uri );
   316 	        ret = FALSE;
   317 	      }
   318 	    }
   319 	
   320 	    file_transfer = gmyth_livetv_create_file_transfer (livetv);
   321 	
   322 	    if (NULL == file_transfer) {
   323 	      ret = FALSE;
   324 	    }
   325 	    
   326 	  } else {
   327 	
   328 	    file_transfer = gmyth_file_transfer_new (backend_info);
   329 	    
   330 	    /* Verifies if the file exists */
   331 	    if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   332 	        g_object_unref (backend_info);
   333 					return GNOME_VFS_ERROR_NOT_FOUND;
   334 	    }
   335 	    
   336 	    /* sets the Playback monitor connection */
   337 	    ret = gmyth_file_transfer_open ( file_transfer, gmyth_uri_get_path (gmyth_uri) );
   338 		
   339 	  } /* if - LiveTV or not? */
   340 	  
   341 	  /*	  
   342     if (ret == FALSE) {
   343 	    g_warning ("MythTV FileTransfer open error\n");
   344 	    return GNOME_VFS_ERROR_NOT_OPEN;
   345 	  }
   346 	  */
   347 
   348     file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
   349     file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
   350     file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
   351     
   352     g_object_unref (file_transfer);
   353     g_object_unref (livetv);
   354     g_object_unref (backend_info);
   355 
   356     return GNOME_VFS_OK;
   357 }
   358 
   359 static gboolean
   360 do_is_local (GnomeVFSMethod *method,
   361              const GnomeVFSURI *uri)
   362 {
   363 	return FALSE;
   364 }
   365 
   366 static GnomeVFSMethod method = {
   367 	sizeof (GnomeVFSMethod),
   368 	do_open,
   369 	NULL,
   370 	do_close,
   371 	do_read,
   372 	NULL,
   373 	NULL,
   374 	NULL,
   375 	NULL,
   376 	NULL,
   377 	NULL,
   378 	NULL,
   379 	do_get_file_info,
   380 	NULL,
   381 	do_is_local,
   382 	NULL,
   383 	NULL,
   384 	NULL,
   385 	NULL,
   386 	NULL,
   387 	NULL,
   388 	NULL,
   389 	NULL,
   390 	NULL,
   391 	NULL,
   392 	NULL,
   393 	NULL,
   394 };
   395 
   396 
   397 GnomeVFSMethod *
   398 vfs_module_init (const char *method_name, const char *args)
   399 {
   400 	return &method;
   401 }
   402 
   403 void
   404 vfs_module_shutdown (GnomeVFSMethod *method)
   405 {
   406 }