libgnomevfs2-mythtv/modules/mythtv-method.c
author rosfran
Fri Nov 17 18:18:24 2006 +0000 (2006-11-17)
branchtrunk
changeset 94 da398435732b
parent 38 d5f5855e7800
child 111 dfa72795bd32
permissions -rwxr-xr-x
[svn r95] Minor fixes on the GstAdapter (cache buffer).
     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_file_transfer.h"
    31 
    32 #define GST_MYTHTV_ID_NUM               1
    33 #define MYTHTV_VERSION_DEFAULT          30
    34 
    35 static GnomeVFSResult do_read (GnomeVFSMethod *method,
    36                                GnomeVFSMethodHandle *method_handle,
    37                                gpointer buffer,
    38                                GnomeVFSFileSize num_bytes,
    39                                GnomeVFSFileSize *bytes_read,
    40                                GnomeVFSContext *context);
    41 
    42 typedef struct {
    43     GMythFileTransfer *file_transfer;
    44     
    45     gint mythtv_version;
    46     guint64 content_size;
    47     guint64 bytes_read;
    48     guint64 read_offset;
    49     gboolean live_tv;
    50 } MythtvHandle;
    51 
    52 
    53 
    54 static GnomeVFSResult
    55 do_open (GnomeVFSMethod *method,
    56          GnomeVFSMethodHandle **method_handle,
    57          GnomeVFSURI *uri,
    58          GnomeVFSOpenMode mode,
    59          GnomeVFSContext *context)
    60 {
    61     MythtvHandle *myth_handle = g_new0 (MythtvHandle, 1);
    62 
    63     const gchar *user, *password, *host, *transfer_uri;
    64     guint port;
    65     gchar *path;
    66     gboolean ret;
    67 
    68     _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
    69     _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
    70 
    71     if (mode & GNOME_VFS_OPEN_WRITE) {
    72         return GNOME_VFS_ERROR_NOT_PERMITTED;
    73     }
    74 
    75     if ((host = gnome_vfs_uri_get_host_name (uri)) == NULL) {
    76 	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
    77     }
    78 
    79     /* Parse URI */
    80     path = gnome_vfs_unescape_string (uri->text, NULL);
    81     port = gnome_vfs_uri_get_host_port (uri);
    82     user = gnome_vfs_uri_get_user_name (uri);
    83     password = gnome_vfs_uri_get_password (uri);
    84 
    85     transfer_uri = gnome_vfs_uri_to_string (uri, 
    86 		    GNOME_VFS_URI_HIDE_USER_NAME | GNOME_VFS_URI_HIDE_PASSWORD);
    87    
    88     /* Initialize mythtv handler*/
    89     myth_handle->file_transfer = NULL;
    90     myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
    91     myth_handle->bytes_read = 0;
    92     myth_handle->content_size = -1;
    93     myth_handle->read_offset = 0;
    94     myth_handle->live_tv = FALSE;
    95 
    96     /* Connect to the backend */
    97     myth_handle->file_transfer = gmyth_file_transfer_new ( GST_MYTHTV_ID_NUM /*mythtv->live_tv_id*/,
    98         g_string_new( transfer_uri ), -1, myth_handle->mythtv_version );
    99 
   100     if ( myth_handle->file_transfer == NULL ) {
   101 	return GNOME_VFS_ERROR_NOT_OPEN;
   102     }
   103 
   104     /* sets the Playback monitor connection */
   105     ret = gmyth_file_transfer_playback_setup( &(myth_handle->file_transfer), myth_handle->live_tv );
   106     if (ret == FALSE) {
   107         g_printerr ("Mythtv FileTransfer playback setup error\n");
   108 	return GNOME_VFS_ERROR_NOT_OPEN;
   109     }
   110       
   111     /* sets the FileTransfer instance connection (video/audio download) */
   112     ret = gmyth_file_transfer_setup( &(myth_handle->file_transfer), myth_handle->live_tv );
   113 
   114     if ( ret == FALSE ) {
   115       g_printerr ("MythTV FileTransfer request failed when setting up socket connection!\n" );
   116       return GNOME_VFS_ERROR_NOT_OPEN;
   117     }
   118 
   119     // TODO: Verify if file exists in the backend
   120 
   121     myth_handle->content_size = myth_handle->file_transfer->filesize;
   122 
   123     *method_handle = (GnomeVFSMethodHandle *) myth_handle;
   124 
   125     return GNOME_VFS_OK;
   126 }
   127 
   128 static GnomeVFSResult
   129 do_read (GnomeVFSMethod *method,
   130          GnomeVFSMethodHandle *method_handle,
   131          gpointer buffer,
   132          GnomeVFSFileSize num_bytes,
   133          GnomeVFSFileSize *bytes_read,
   134          GnomeVFSContext *context)
   135 {
   136     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   137     guint len = 0, offset = 0;
   138     GnomeVFSFileSize bytes_to_read;
   139 
   140     g_debug ("do_read(): file size: %llu, already read: %llu, requested: %llu", myth_handle->content_size,
   141 		    myth_handle->bytes_read, num_bytes);
   142 
   143     *bytes_read = 0;
   144 
   145     if (myth_handle->bytes_read >= myth_handle->content_size)
   146         return GNOME_VFS_ERROR_EOF;
   147 
   148     // fixme: change this to min math function
   149     if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
   150 	    bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   151     else
   152 	    bytes_to_read = num_bytes;
   153 
   154     /* Loop sending the Myth File Transfer request:
   155     * Retry whilst authentication fails and we supply it. */
   156 
   157     // lock here ???
   158     while ( bytes_to_read > 0 ) {
   159   	len = gmyth_file_transfer_read( myth_handle->file_transfer,
   160 			buffer + offset, bytes_to_read, TRUE );
   161 
   162         if ( len > 0 ) {
   163             offset += len;      
   164             bytes_to_read -= len;
   165         } else {
   166 	    break;
   167         }
   168     
   169         if ( offset == num_bytes )
   170           break;
   171     }
   172   
   173     if (( offset <= 0 ) && !myth_handle->live_tv )
   174         return GNOME_VFS_ERROR_EOF;
   175   
   176     myth_handle->read_offset += offset;
   177     myth_handle->bytes_read += offset;
   178     *bytes_read = offset;
   179   
   180     return GNOME_VFS_OK;
   181 }
   182 
   183 static GnomeVFSResult
   184 do_close (GnomeVFSMethod *method,
   185           GnomeVFSMethodHandle *method_handle,
   186           GnomeVFSContext *context)
   187 {
   188     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   189 
   190     if (myth_handle->file_transfer)
   191         g_object_unref (myth_handle->file_transfer);
   192 
   193     g_free (myth_handle);
   194 
   195     return GNOME_VFS_OK;
   196 }
   197 
   198 static GnomeVFSResult
   199 do_get_file_info (GnomeVFSMethod *method,
   200                   GnomeVFSURI *uri,
   201                   GnomeVFSFileInfo *file_info,
   202                   GnomeVFSFileInfoOptions options,
   203                   GnomeVFSContext *context)
   204 {
   205     GString *name = g_string_new ("hallyson");	
   206     file_info->name = g_string_free (name, FALSE);//"hallyson.txt";//get_base_from_uri (uri);
   207     file_info->valid_fields = file_info->valid_fields
   208         | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   209         | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   210         | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   211     file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   212     file_info->mime_type = "video/x-nuv"; //g_strdup (gnome_vfs_mime_type_from_name (file_info->name));
   213     file_info->permissions =
   214         GNOME_VFS_PERM_USER_READ |
   215         GNOME_VFS_PERM_OTHER_READ |
   216         GNOME_VFS_PERM_GROUP_READ;
   217 
   218     return GNOME_VFS_OK;
   219 }
   220 
   221 static gboolean
   222 do_is_local (GnomeVFSMethod *method,
   223              const GnomeVFSURI *uri)
   224 {
   225 	return FALSE;
   226 }
   227 
   228 static GnomeVFSMethod method = {
   229 	sizeof (GnomeVFSMethod),
   230 	do_open,
   231 	NULL,
   232 	do_close,
   233 	do_read,
   234 	NULL,
   235 	NULL,
   236 	NULL,
   237 	NULL,
   238 	NULL,
   239 	NULL,
   240 	NULL,
   241 	do_get_file_info,
   242 	NULL,
   243 	do_is_local,
   244 	NULL,
   245 	NULL,
   246 	NULL,
   247 	NULL,
   248 	NULL,
   249 	NULL,
   250 	NULL,
   251 	NULL,
   252 	NULL,
   253 	NULL,
   254 	NULL,
   255 	NULL,
   256 };
   257 
   258 
   259 GnomeVFSMethod *
   260 vfs_module_init (const char *method_name, const char *args)
   261 {
   262 	return &method;
   263 }
   264 
   265 void
   266 vfs_module_shutdown (GnomeVFSMethod *method)
   267 {
   268 }