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