libgnomevfs2-mythtv/modules/mythtv-method.c
author melunko
Fri Jan 19 13:09:21 2007 +0000 (2007-01-19)
branchtrunk
changeset 278 a24bacc8d752
parent 251 9f0346db9a46
child 281 08c4bc759e7a
permissions -rwxr-xr-x
[svn r279] Removed some useless debug messages
     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     guint64 content_size;
    57     guint64 bytes_read;
    58 
    59     GByteArray *buffer;
    60     gsize buffer_remain;
    61 } MythtvHandle;
    62 
    63 
    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     gint wait_to_transfer = 0;
    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     if (mode & GNOME_VFS_OPEN_WRITE) {
    85         return GNOME_VFS_ERROR_NOT_PERMITTED;
    86     }
    87 
    88     if (gnome_vfs_uri_get_host_name (uri) == NULL) {
    89     	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
    90     }
    91 
    92     /* Initialize mythtv handler*/
    93     myth_handle->file_transfer = NULL;
    94     myth_handle->livetv = NULL;
    95     myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
    96     myth_handle->bytes_read = 0;
    97     myth_handle->content_size = -1;
    98 
    99     /* Creates and fills out the backend info structure */    
   100 	  backend_info = gmyth_backend_info_new_with_uri ( 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( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ) );
   104 	  
   105 	  /* Connect to the backend */	  
   106 	  if ( gmyth_uri != NULL && ( is_livetv = gmyth_uri_is_livetv( gmyth_uri ) ) == TRUE ) {
   107 	    myth_handle->livetv = gmyth_livetv_new ();
   108 	    
   109 	    myth_handle->channel_num = gmyth_uri_get_channel_num( gmyth_uri );
   110 	
   111 	    if ( myth_handle->channel_num != -1 ) {
   112 	      if (gmyth_livetv_channel_setup (myth_handle->livetv, myth_handle->channel_num,
   113 	              backend_info) == FALSE) {
   114 	        g_object_unref( gmyth_uri );
   115 	        ret = FALSE;
   116 	      }
   117 	    } else {
   118 	      if ( gmyth_livetv_setup (myth_handle->livetv, backend_info) == FALSE ) {
   119 	      	g_object_unref( gmyth_uri );
   120 	        ret = FALSE;
   121 	      }
   122 	    }
   123 	
   124 	    myth_handle->file_transfer = gmyth_livetv_create_file_transfer (myth_handle->livetv);
   125 	
   126 	    if (NULL == myth_handle->file_transfer) {
   127 	      ret = FALSE;
   128 	    }
   129 	    
   130 	    if ( gmyth_uri != NULL )
   131 	    	g_object_unref( gmyth_uri );
   132 	    	
   133 	  } else {
   134 	
   135 	    myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
   136 	    
   137 	    /* Verifies if the file exists */
   138 	    if (!gmyth_util_file_exists (backend_info, gnome_vfs_uri_get_path (uri))) {
   139 	        g_object_unref (backend_info);
   140 					return GNOME_VFS_ERROR_NOT_FOUND;
   141 	    }
   142 	    
   143 	    /* sets the Playback monitor connection */
   144 	    ret = gmyth_file_transfer_open ( myth_handle->file_transfer, gnome_vfs_uri_get_path (uri) );
   145 		
   146 	  } /* if - LiveTV or not? */
   147 	  
   148     if (ret == FALSE) {
   149 	    g_warning ("MythTV FileTransfer open error\n");
   150 	    return GNOME_VFS_ERROR_NOT_OPEN;
   151 	  } 
   152 	
   153 	  if ( is_livetv == TRUE && ret == TRUE ) {
   154 	    /* loop finished, set the max tries variable to zero again... */
   155 	    wait_to_transfer = 0;
   156 	
   157 	    while ( wait_to_transfer++ < MYTHTV_TRANSFER_MAX_WAITS &&
   158 	        (gmyth_livetv_is_recording (myth_handle->livetv) == FALSE))
   159 	      g_usleep (500);
   160 	
   161 	    /* IS_RECORDING again, just like the MythTV backend does... */
   162 	    gmyth_livetv_is_recording (myth_handle->livetv);
   163 	
   164 	    sleep (4);                  /* FIXME: this is evil (tpm) */
   165 	  }
   166 
   167     g_object_unref (backend_info);
   168 
   169     g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
   170 
   171     myth_handle->content_size = myth_handle->file_transfer->filesize;
   172     
   173     myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
   174     myth_handle->buffer_remain = 0;
   175 
   176     *method_handle = (GnomeVFSMethodHandle *) myth_handle;
   177 
   178     return GNOME_VFS_OK;
   179 }
   180 
   181 static GnomeVFSResult
   182 do_read (GnomeVFSMethod *method,
   183          GnomeVFSMethodHandle *method_handle,
   184          gpointer buffer,
   185          GnomeVFSFileSize num_bytes,
   186          GnomeVFSFileSize *bytes_read,
   187          GnomeVFSContext *context)
   188 {
   189     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   190     GnomeVFSFileSize bytes_to_read;
   191 
   192     *bytes_read = 0;
   193 
   194     if (myth_handle->bytes_read >= myth_handle->content_size)
   195         return GNOME_VFS_ERROR_EOF;
   196 
   197     // fixme: change this to min math function
   198     if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
   199 	    bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   200     else
   201 	    bytes_to_read = num_bytes;
   202 
   203     /* Loop sending the Myth File Transfer request:
   204     * Retry whilst authentication fails and we supply it. */
   205   
   206     if ( bytes_to_read > myth_handle->buffer_remain ) {
   207         GByteArray *tmp_buffer = g_byte_array_new();
   208 
   209         gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
   210               tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
   211 
   212 	if (len < 0) {
   213             g_byte_array_free (tmp_buffer, TRUE);
   214             g_warning ("Fail to read bytes");
   215 	    return GNOME_VFS_ERROR_IO;
   216         } else if (len == 0) {
   217 	    g_byte_array_free (tmp_buffer, TRUE);
   218 	    g_warning ("End of file probably achieved");
   219 	    return GNOME_VFS_ERROR_EOF;
   220 	}
   221 
   222         myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
   223     		tmp_buffer->data, len);
   224 
   225 		myth_handle->buffer_remain += len;
   226 
   227         g_byte_array_free (tmp_buffer, TRUE);
   228     	tmp_buffer = NULL;    	
   229     }
   230     
   231     bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
   232     /* gets the first buffer_size bytes from the byte array buffer variable */ 
   233 
   234     g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
   235 
   236     myth_handle->bytes_read += bytes_to_read;
   237     myth_handle->buffer_remain -= bytes_to_read;    
   238 
   239   	/* flushs the newly buffer got from byte array */
   240   	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
   241     *bytes_read = bytes_to_read;
   242   
   243     return GNOME_VFS_OK;
   244 }
   245 
   246 static GnomeVFSResult
   247 do_close (GnomeVFSMethod *method,
   248           GnomeVFSMethodHandle *method_handle,
   249           GnomeVFSContext *context)
   250 {
   251 
   252     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   253 
   254     if (myth_handle->file_transfer) {
   255 	    gmyth_file_transfer_close (myth_handle->file_transfer);
   256         g_object_unref (myth_handle->file_transfer);
   257     	myth_handle->file_transfer = NULL;
   258     }
   259     
   260     if (myth_handle->livetv) {
   261       g_object_unref (myth_handle->livetv);
   262     	myth_handle->livetv = NULL;
   263     }
   264     
   265     if (myth_handle->buffer) {
   266 		g_byte_array_free (myth_handle->buffer, TRUE);
   267 		myth_handle->buffer = NULL;
   268     }
   269 
   270     g_free (myth_handle);
   271 
   272     return GNOME_VFS_OK;
   273 }
   274 
   275 static GnomeVFSResult
   276 do_get_file_info (GnomeVFSMethod *method,
   277                   GnomeVFSURI *uri,
   278                   GnomeVFSFileInfo *file_info,
   279                   GnomeVFSFileInfoOptions options,
   280                   GnomeVFSContext *context)
   281 {
   282     GMythFileTransfer *file_transfer = NULL;
   283     GMythBackendInfo *backend_info = NULL;
   284 
   285     file_info->name = g_strdup (gnome_vfs_uri_get_path (uri));
   286     file_info->valid_fields = file_info->valid_fields
   287         | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   288         | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   289         | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   290     file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   291     // fixme: get from file extension?
   292     file_info->mime_type = g_strdup ("video/x-nuv");
   293     file_info->permissions =
   294         GNOME_VFS_PERM_USER_READ |
   295         GNOME_VFS_PERM_OTHER_READ |
   296         GNOME_VFS_PERM_GROUP_READ;
   297 
   298     backend_info = gmyth_backend_info_new_full (gnome_vfs_uri_get_host_name (uri),
   299             gnome_vfs_uri_get_user_name (uri),
   300             gnome_vfs_uri_get_password (uri),
   301             NULL,
   302             gnome_vfs_uri_get_host_port (uri));
   303 
   304     file_transfer = gmyth_file_transfer_new (backend_info);
   305     if (gmyth_file_transfer_open (file_transfer, gnome_vfs_uri_get_path (uri)) == TRUE) {
   306         file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
   307         file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
   308         file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
   309     }
   310 
   311     g_object_unref (file_transfer);
   312     g_object_unref (backend_info);
   313     return GNOME_VFS_OK;
   314 }
   315 
   316 static gboolean
   317 do_is_local (GnomeVFSMethod *method,
   318              const GnomeVFSURI *uri)
   319 {
   320 	return FALSE;
   321 }
   322 
   323 static GnomeVFSMethod method = {
   324 	sizeof (GnomeVFSMethod),
   325 	do_open,
   326 	NULL,
   327 	do_close,
   328 	do_read,
   329 	NULL,
   330 	NULL,
   331 	NULL,
   332 	NULL,
   333 	NULL,
   334 	NULL,
   335 	NULL,
   336 	do_get_file_info,
   337 	NULL,
   338 	do_is_local,
   339 	NULL,
   340 	NULL,
   341 	NULL,
   342 	NULL,
   343 	NULL,
   344 	NULL,
   345 	NULL,
   346 	NULL,
   347 	NULL,
   348 	NULL,
   349 	NULL,
   350 	NULL,
   351 };
   352 
   353 
   354 GnomeVFSMethod *
   355 vfs_module_init (const char *method_name, const char *args)
   356 {
   357 	return &method;
   358 }
   359 
   360 void
   361 vfs_module_shutdown (GnomeVFSMethod *method)
   362 {
   363 }