libgnomevfs2-mythtv/modules/mythtv-method.c
author rosfran
Fri Feb 02 22:44:59 2007 +0000 (2007-02-02)
branchtrunk
changeset 323 6523ab923c00
parent 317 3b575d136efe
child 327 41995e0df2ba
permissions -rwxr-xr-x
[svn r325] Minor changes, separated process of opening LiveTV FileTransfer from the LiveTV setup.
     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   		if ( !gmyth_file_transfer_open( myth_handle->file_transfer, myth_handle->livetv->uri != NULL ? 
   132 								gmyth_uri_get_path(myth_handle->livetv->uri) : 
   133 								myth_handle->livetv->proginfo->pathname->str ) )
   134 			{
   135 				g_object_unref( myth_handle->file_transfer );
   136 				myth_handle->file_transfer = NULL;
   137 				ret = FALSE;
   138 			}
   139 	    
   140 	  } else {
   141 	
   142 	    myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
   143 	    
   144 	    /* Verifies if the file exists */
   145 	    if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   146 	        g_object_unref (backend_info);
   147 					ret = FALSE;
   148 	    }
   149 	    
   150 	    /* sets the Playback monitor connection */
   151 	    ret = gmyth_file_transfer_open ( myth_handle->file_transfer, gmyth_uri_get_path (gmyth_uri) );
   152 		
   153 	  } /* if - LiveTV or not? */
   154 	  
   155     if (ret == FALSE) {
   156 	    g_warning ("MythTV FileTransfer open error.\n");
   157 	    return GNOME_VFS_ERROR_NOT_OPEN;
   158 	  }
   159 
   160     g_object_unref (backend_info);
   161     
   162 	  //if ( gmyth_uri != NULL )
   163 	  //	g_object_unref( gmyth_uri );
   164 	  
   165 	  g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
   166 	  
   167 	  if ( myth_handle->file_transfer->filesize < 0 && is_livetv )
   168 			myth_handle->content_size = gmyth_recorder_get_file_position( myth_handle->livetv->recorder );
   169 		else		
   170     	myth_handle->content_size = myth_handle->file_transfer->filesize;
   171     
   172     myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
   173     myth_handle->buffer_remain = 0;
   174 
   175     *method_handle = (GnomeVFSMethodHandle *) myth_handle;
   176 
   177     return GNOME_VFS_OK;
   178 }
   179 
   180 static GnomeVFSResult
   181 do_read (GnomeVFSMethod *method,
   182          GnomeVFSMethodHandle *method_handle,
   183          gpointer buffer,
   184          GnomeVFSFileSize num_bytes,
   185          GnomeVFSFileSize *bytes_read,
   186          GnomeVFSContext *context)
   187 {
   188     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   189     GnomeVFSFileSize bytes_to_read;
   190 
   191     *bytes_read = 0;
   192 
   193     if (myth_handle->bytes_read >= myth_handle->content_size)
   194         return GNOME_VFS_ERROR_EOF;
   195 
   196     // fixme: change this to min math function
   197     if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
   198 	    bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   199     else
   200 	    bytes_to_read = num_bytes;
   201 
   202     /* Loop sending the Myth File Transfer request:
   203     * Retry whilst authentication fails and we supply it. */
   204     //if (myth_handle->buffer_remain  < MYTHTV_BUFFER_SIZE) {
   205     if ( bytes_to_read > myth_handle->buffer_remain ) {
   206         GByteArray *tmp_buffer = g_byte_array_new();
   207 
   208 	printf ("XXXXXXXXXXXXXX Pedindo %d %d\n", MYTHTV_BUFFER_SIZE, myth_handle->buffer_remain);
   209 
   210         gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
   211               tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
   212 
   213 	if (len < 0) {
   214             g_byte_array_free (tmp_buffer, TRUE);
   215             g_warning ("Fail to read bytes");
   216 	    return GNOME_VFS_ERROR_IO;
   217         } /*else if (len == 0) {
   218 	    g_byte_array_free (tmp_buffer, TRUE);
   219 	    g_warning ("End of file probably achieved");
   220 	    return GNOME_VFS_ERROR_EOF;
   221 	}*/
   222 
   223         myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
   224     		tmp_buffer->data, len);
   225 
   226 		myth_handle->buffer_remain += len;
   227 
   228         g_byte_array_free (tmp_buffer, TRUE);
   229     	tmp_buffer = NULL;    	
   230     }
   231     
   232     bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
   233     /* gets the first buffer_size bytes from the byte array buffer variable */ 
   234 
   235     g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
   236 
   237     myth_handle->bytes_read += bytes_to_read;
   238     myth_handle->buffer_remain -= bytes_to_read;    
   239 
   240   	/* flushs the newly buffer got from byte array */
   241   	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
   242     *bytes_read = bytes_to_read;
   243   
   244     return GNOME_VFS_OK;
   245 }
   246 
   247 static GnomeVFSResult
   248 do_close (GnomeVFSMethod *method,
   249           GnomeVFSMethodHandle *method_handle,
   250           GnomeVFSContext *context)
   251 {
   252 
   253     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   254 
   255     if (myth_handle->file_transfer) {
   256 	    gmyth_file_transfer_close (myth_handle->file_transfer);
   257         g_object_unref (myth_handle->file_transfer);
   258     	myth_handle->file_transfer = NULL;
   259     }
   260     
   261     if (myth_handle->livetv) {
   262       g_object_unref (myth_handle->livetv);
   263     	myth_handle->livetv = NULL;
   264     }
   265     
   266     if (myth_handle->buffer) {
   267 		g_byte_array_free (myth_handle->buffer, TRUE);
   268 		myth_handle->buffer = NULL;
   269     }
   270 
   271     g_free (myth_handle);
   272 
   273     return GNOME_VFS_OK;
   274 }
   275 
   276 static GnomeVFSResult
   277 do_get_file_info (GnomeVFSMethod *method,
   278                   GnomeVFSURI *uri,
   279                   GnomeVFSFileInfo *file_info,
   280                   GnomeVFSFileInfoOptions options,
   281                   GnomeVFSContext *context)
   282 {
   283     GMythFileTransfer *file_transfer = NULL;
   284     GMythLiveTV 			*livetv 			 = NULL;
   285     GMythBackendInfo  *backend_info  = NULL;
   286     GMythURI					*gmyth_uri		 = NULL;
   287     gboolean 					is_livetv 		 = FALSE;
   288     gboolean					ret						 = TRUE;
   289     
   290     /* Creates and fills out the backend info structure */    
   291 	  backend_info = gmyth_backend_info_new_with_uri ( 
   292 	  			gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   293 	  
   294 	  /* creates an instance of */  
   295 	  gmyth_uri = gmyth_uri_new_with_value( 
   296 	  		gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
   297 
   298 	  is_livetv = gmyth_uri_is_livetv( gmyth_uri );
   299 
   300     file_info->name = g_strdup (gmyth_uri_get_path (gmyth_uri)+1);
   301     file_info->valid_fields = file_info->valid_fields
   302         | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   303         | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   304         | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   305     file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   306     /* fixme: get from file extension? */
   307     file_info->mime_type = g_strdup ("video/x-nuv");
   308     file_info->permissions =
   309         GNOME_VFS_PERM_USER_READ |
   310         GNOME_VFS_PERM_OTHER_READ |
   311         GNOME_VFS_PERM_GROUP_READ;
   312 
   313 	  /* Connect to the backend */
   314 	  if ( gmyth_uri != NULL && is_livetv == TRUE ) {
   315 	    livetv = gmyth_livetv_new ();
   316 	    
   317 	    gint channel_num = gmyth_uri_get_channel_num( gmyth_uri );
   318 	
   319 	    if ( channel_num != -1 ) {
   320 	      if (gmyth_livetv_channel_setup (livetv, channel_num,
   321 	              backend_info) == FALSE) {
   322 	        g_object_unref( gmyth_uri );
   323 	        ret = FALSE;
   324 	      }
   325 	    } else {
   326 	      if ( gmyth_livetv_setup (livetv, backend_info) == FALSE ) {
   327 	      	g_object_unref( gmyth_uri );
   328 	        ret = FALSE;
   329 	      }
   330 	    }
   331 	
   332 	    file_transfer = gmyth_livetv_create_file_transfer (livetv);
   333 	
   334 	    if (NULL == file_transfer) {
   335 	      ret = FALSE;
   336 	    }
   337 	    
   338 	  } else {
   339 	
   340 	    file_transfer = gmyth_file_transfer_new (backend_info);
   341 	    
   342 	    /* Verifies if the file exists */
   343 	    if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
   344 	        g_object_unref (backend_info);
   345 					return GNOME_VFS_ERROR_NOT_FOUND;
   346 	    }
   347 	    
   348 	    /* sets the Playback monitor connection */
   349 	    ret = gmyth_file_transfer_open ( file_transfer, gmyth_uri_get_path (gmyth_uri) );
   350 		
   351 	  } /* if - LiveTV or not? */
   352 	  
   353 	  /*	  
   354     if (ret == FALSE) {
   355 	    g_warning ("MythTV FileTransfer open error\n");
   356 	    return GNOME_VFS_ERROR_NOT_OPEN;
   357 	  }
   358 	  */
   359 	  if ( file_transfer->filesize < 0 && is_livetv )
   360 			file_info->size = gmyth_recorder_get_file_position( livetv->recorder );
   361 		else		
   362     	file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
   363     
   364     file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
   365     file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
   366     
   367     g_object_unref (file_transfer);
   368     g_object_unref (livetv);
   369     g_object_unref (backend_info);
   370 
   371     return GNOME_VFS_OK;
   372 }
   373 
   374 static gboolean
   375 do_is_local (GnomeVFSMethod *method,
   376              const GnomeVFSURI *uri)
   377 {
   378 	return FALSE;
   379 }
   380 
   381 static GnomeVFSMethod method = {
   382 	sizeof (GnomeVFSMethod),
   383 	do_open,
   384 	NULL,
   385 	do_close,
   386 	do_read,
   387 	NULL,
   388 	NULL,
   389 	NULL,
   390 	NULL,
   391 	NULL,
   392 	NULL,
   393 	NULL,
   394 	do_get_file_info,
   395 	NULL,
   396 	do_is_local,
   397 	NULL,
   398 	NULL,
   399 	NULL,
   400 	NULL,
   401 	NULL,
   402 	NULL,
   403 	NULL,
   404 	NULL,
   405 	NULL,
   406 	NULL,
   407 	NULL,
   408 	NULL,
   409 };
   410 
   411 
   412 GnomeVFSMethod *
   413 vfs_module_init (const char *method_name, const char *args)
   414 {
   415 	return &method;
   416 }
   417 
   418 void
   419 vfs_module_shutdown (GnomeVFSMethod *method)
   420 {
   421 }