libgnomevfs2-mythtv/modules/mythtv-method.c
author rosfran
Fri Jan 19 00:51:49 2007 +0000 (2007-01-19)
branchtrunk
changeset 277 890b023c759f
parent 251 9f0346db9a46
child 281 08c4bc759e7a
permissions -rwxr-xr-x
[svn r278] Added LiveTV streaming to the gnomevfs mythtv plug-in.
melunko@38
     1
/*
melunko@38
     2
 * @author Hallyson Melo <hallyson.melo@indt.org.br>
melunko@38
     3
 *
melunko@38
     4
 * This program is free software; you can redistribute it and/or modify
melunko@38
     5
 * it under the terms of the GNU Lesser General Public License as published by
melunko@38
     6
 * the Free Software Foundation; either version 2 of the License, or
melunko@38
     7
 * (at your option) any later version.
melunko@38
     8
 *
melunko@38
     9
 * This program is distributed in the hope that it will be useful,
melunko@38
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
melunko@38
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
melunko@38
    12
 * GNU General Public License for more details.
melunko@38
    13
 *
melunko@38
    14
 * You should have received a copy of the GNU Lesser General Public License
melunko@38
    15
 * along with this program; if not, write to the Free Software
melunko@38
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
melunko@38
    17
 */
melunko@38
    18
melunko@38
    19
#ifdef HAVE_CONFIG_H
melunko@38
    20
#include <config.h>
melunko@38
    21
#endif
melunko@38
    22
melunko@38
    23
#include <string.h>
melunko@38
    24
#include <glib.h>
melunko@38
    25
#include <math.h>
melunko@38
    26
melunko@38
    27
#include <libgnomevfs/gnome-vfs-module.h>
melunko@38
    28
#include <libgnomevfs/gnome-vfs-utils.h>
melunko@38
    29
rosfran@277
    30
#include <gmyth/gmyth_file_transfer.h>
rosfran@277
    31
#include <gmyth/gmyth_livetv.h>
rosfran@277
    32
#include <gmyth/gmyth_uri.h>
rosfran@277
    33
#include <gmyth/gmyth_recorder.h>
rosfran@277
    34
#include <gmyth/gmyth_backendinfo.h>
rosfran@277
    35
#include <gmyth/gmyth_util.h>
melunko@38
    36
melunko@38
    37
#define GST_MYTHTV_ID_NUM               1
melunko@38
    38
#define MYTHTV_VERSION_DEFAULT          30
rosfran@277
    39
#define MYTHTV_TRANSFER_MAX_WAITS       100
melunko@38
    40
melunko@111
    41
#define MYTHTV_BUFFER_SIZE		1024*64
melunko@111
    42
melunko@38
    43
static GnomeVFSResult do_read (GnomeVFSMethod *method,
melunko@38
    44
                               GnomeVFSMethodHandle *method_handle,
melunko@38
    45
                               gpointer buffer,
melunko@38
    46
                               GnomeVFSFileSize num_bytes,
melunko@38
    47
                               GnomeVFSFileSize *bytes_read,
melunko@38
    48
                               GnomeVFSContext *context);
melunko@38
    49
melunko@38
    50
typedef struct {
melunko@48
    51
    GMythFileTransfer *file_transfer;
rosfran@277
    52
    GMythLiveTV 			*livetv;
rosfran@277
    53
    gint 							channel_num;
melunko@38
    54
    
melunko@38
    55
    gint mythtv_version;
melunko@38
    56
    guint64 content_size;
melunko@38
    57
    guint64 bytes_read;
melunko@111
    58
rosfran@116
    59
    GByteArray *buffer;
melunko@111
    60
    gsize buffer_remain;
melunko@38
    61
} MythtvHandle;
melunko@38
    62
melunko@38
    63
melunko@38
    64
melunko@38
    65
static GnomeVFSResult
melunko@38
    66
do_open (GnomeVFSMethod *method,
melunko@38
    67
         GnomeVFSMethodHandle **method_handle,
melunko@38
    68
         GnomeVFSURI *uri,
melunko@38
    69
         GnomeVFSOpenMode mode,
melunko@38
    70
         GnomeVFSContext *context)
melunko@38
    71
{
melunko@111
    72
    MythtvHandle *myth_handle;
melunko@160
    73
    GMythBackendInfo *backend_info;
rosfran@277
    74
    GMythURI *gmyth_uri = NULL;
rosfran@277
    75
    gboolean ret = TRUE;
rosfran@277
    76
    gboolean is_livetv = FALSE;
rosfran@277
    77
    gint wait_to_transfer = 0;
melunko@38
    78
melunko@38
    79
    _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
melunko@38
    80
    _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
melunko@38
    81
melunko@111
    82
    myth_handle = g_new0 (MythtvHandle, 1);
melunko@111
    83
melunko@38
    84
    if (mode & GNOME_VFS_OPEN_WRITE) {
melunko@38
    85
        return GNOME_VFS_ERROR_NOT_PERMITTED;
melunko@38
    86
    }
melunko@38
    87
renatofilho@149
    88
    if (gnome_vfs_uri_get_host_name (uri) == NULL) {
renatofilho@149
    89
    	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
melunko@38
    90
    }
melunko@38
    91
melunko@38
    92
    /* Initialize mythtv handler*/
melunko@38
    93
    myth_handle->file_transfer = NULL;
rosfran@277
    94
    myth_handle->livetv = NULL;
melunko@38
    95
    myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
melunko@38
    96
    myth_handle->bytes_read = 0;
melunko@38
    97
    myth_handle->content_size = -1;
melunko@38
    98
rosfran@277
    99
    /* Creates and fills out the backend info structure */    
rosfran@277
   100
	  backend_info = gmyth_backend_info_new_with_uri ( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ) );
rosfran@277
   101
	  
rosfran@277
   102
	  /* creates an instance of  */  
rosfran@277
   103
	  gmyth_uri = gmyth_uri_new_with_value( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ) );
rosfran@277
   104
	  
rosfran@277
   105
	  /* Connect to the backend */	  
rosfran@277
   106
	  if ( gmyth_uri != NULL && ( is_livetv = gmyth_uri_is_livetv( gmyth_uri ) ) == TRUE ) {
rosfran@277
   107
	    myth_handle->livetv = gmyth_livetv_new ();
rosfran@180
   108
	    
rosfran@277
   109
	    myth_handle->channel_num = gmyth_uri_get_channel_num( gmyth_uri );
rosfran@277
   110
	
rosfran@277
   111
	    if ( myth_handle->channel_num != -1 ) {
rosfran@277
   112
	      if (gmyth_livetv_channel_setup (myth_handle->livetv, myth_handle->channel_num,
rosfran@277
   113
	              backend_info) == FALSE) {
rosfran@277
   114
	        g_object_unref( gmyth_uri );
rosfran@277
   115
	        ret = FALSE;
rosfran@277
   116
	      }
rosfran@277
   117
	    } else {
rosfran@277
   118
	      if ( gmyth_livetv_setup (myth_handle->livetv, backend_info) == FALSE ) {
rosfran@277
   119
	      	g_object_unref( gmyth_uri );
rosfran@277
   120
	        ret = FALSE;
rosfran@277
   121
	      }
rosfran@277
   122
	    }
rosfran@277
   123
	
rosfran@277
   124
	    myth_handle->file_transfer = gmyth_livetv_create_file_transfer (myth_handle->livetv);
rosfran@277
   125
	
rosfran@277
   126
	    if (NULL == myth_handle->file_transfer) {
rosfran@277
   127
	      ret = FALSE;
rosfran@277
   128
	    }
rosfran@277
   129
	    
rosfran@277
   130
	    if ( gmyth_uri != NULL )
rosfran@277
   131
	    	g_object_unref( gmyth_uri );
rosfran@277
   132
	    	
rosfran@277
   133
	  } else {
rosfran@277
   134
	
rosfran@277
   135
	    myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
rosfran@277
   136
	    
rosfran@277
   137
	    /* Verifies if the file exists */
rosfran@277
   138
	    if (!gmyth_util_file_exists (backend_info, gnome_vfs_uri_get_path (uri))) {
rosfran@277
   139
	        g_object_unref (backend_info);
rosfran@277
   140
					return GNOME_VFS_ERROR_NOT_FOUND;
rosfran@277
   141
	    }
rosfran@277
   142
	    
rosfran@277
   143
	    /* sets the Playback monitor connection */
rosfran@277
   144
	    ret = gmyth_file_transfer_open ( myth_handle->file_transfer, gnome_vfs_uri_get_path (uri) );
rosfran@277
   145
		
rosfran@277
   146
	  } /* if - LiveTV or not? */
rosfran@277
   147
	  
rosfran@277
   148
    if (ret == FALSE) {
rosfran@277
   149
	    g_warning ("MythTV FileTransfer open error\n");
rosfran@277
   150
	    return GNOME_VFS_ERROR_NOT_OPEN;
rosfran@277
   151
	  } 
rosfran@277
   152
	
rosfran@277
   153
	  if ( is_livetv == TRUE && ret == TRUE ) {
rosfran@277
   154
	    /* loop finished, set the max tries variable to zero again... */
rosfran@277
   155
	    wait_to_transfer = 0;
rosfran@277
   156
	
rosfran@277
   157
	    while ( wait_to_transfer++ < MYTHTV_TRANSFER_MAX_WAITS &&
rosfran@277
   158
	        (gmyth_livetv_is_recording (myth_handle->livetv) == FALSE))
rosfran@277
   159
	      g_usleep (500);
rosfran@277
   160
	
rosfran@277
   161
	    /* IS_RECORDING again, just like the MythTV backend does... */
rosfran@277
   162
	    gmyth_livetv_is_recording (myth_handle->livetv);
rosfran@277
   163
	
rosfran@277
   164
	    sleep (4);                  /* FIXME: this is evil (tpm) */
rosfran@277
   165
	  }
melunko@251
   166
renatofilho@188
   167
    g_object_unref (backend_info);
melunko@38
   168
melunko@111
   169
    g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
melunko@38
   170
melunko@38
   171
    myth_handle->content_size = myth_handle->file_transfer->filesize;
melunko@111
   172
    
rosfran@116
   173
    myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
melunko@111
   174
    myth_handle->buffer_remain = 0;
melunko@38
   175
melunko@38
   176
    *method_handle = (GnomeVFSMethodHandle *) myth_handle;
melunko@38
   177
melunko@38
   178
    return GNOME_VFS_OK;
melunko@38
   179
}
melunko@38
   180
melunko@38
   181
static GnomeVFSResult
melunko@38
   182
do_read (GnomeVFSMethod *method,
melunko@38
   183
         GnomeVFSMethodHandle *method_handle,
melunko@38
   184
         gpointer buffer,
melunko@38
   185
         GnomeVFSFileSize num_bytes,
melunko@38
   186
         GnomeVFSFileSize *bytes_read,
melunko@38
   187
         GnomeVFSContext *context)
melunko@38
   188
{
melunko@38
   189
    MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
melunko@38
   190
    GnomeVFSFileSize bytes_to_read;
melunko@38
   191
melunko@38
   192
    *bytes_read = 0;
melunko@38
   193
melunko@38
   194
    if (myth_handle->bytes_read >= myth_handle->content_size)
melunko@38
   195
        return GNOME_VFS_ERROR_EOF;
melunko@38
   196
melunko@38
   197
    // fixme: change this to min math function
melunko@38
   198
    if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
melunko@38
   199
	    bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
melunko@38
   200
    else
melunko@38
   201
	    bytes_to_read = num_bytes;
melunko@38
   202
melunko@38
   203
    /* Loop sending the Myth File Transfer request:
melunko@38
   204
    * Retry whilst authentication fails and we supply it. */
rosfran@116
   205
  
renatofilho@149
   206
    if ( bytes_to_read > myth_handle->buffer_remain ) {
renatofilho@149
   207
        GByteArray *tmp_buffer = g_byte_array_new();
melunko@38
   208
renatofilho@149
   209
        gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
renatofilho@149
   210
              tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
melunko@38
   211
melunko@251
   212
	if (len < 0) {
renatofilho@149
   213
            g_byte_array_free (tmp_buffer, TRUE);
renatofilho@164
   214
            g_warning ("Fail to read bytes");
melunko@251
   215
	    return GNOME_VFS_ERROR_IO;
melunko@251
   216
        } else if (len == 0) {
melunko@251
   217
	    g_byte_array_free (tmp_buffer, TRUE);
melunko@251
   218
	    g_warning ("End of file probably achieved");
melunko@251
   219
	    return GNOME_VFS_ERROR_EOF;
melunko@251
   220
	}
melunko@111
   221
renatofilho@149
   222
        myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
renatofilho@149
   223
    		tmp_buffer->data, len);
rosfran@116
   224
rosfran@116
   225
		myth_handle->buffer_remain += len;
rosfran@116
   226
renatofilho@169
   227
        g_byte_array_free (tmp_buffer, TRUE);
rosfran@116
   228
    	tmp_buffer = NULL;    	
rosfran@116
   229
    }
melunko@38
   230
    
melunko@111
   231
    bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
renatofilho@164
   232
    /* gets the first buffer_size bytes from the byte array buffer variable */ 
rosfran@116
   233
renatofilho@149
   234
    g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
rosfran@116
   235
melunko@111
   236
    myth_handle->bytes_read += bytes_to_read;
rosfran@116
   237
    myth_handle->buffer_remain -= bytes_to_read;    
rosfran@116
   238
rosfran@116
   239
  	/* flushs the newly buffer got from byte array */
renatofilho@149
   240
  	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
melunko@111
   241
    *bytes_read = bytes_to_read;
melunko@38
   242
  
melunko@38
   243
    return GNOME_VFS_OK;
melunko@38
   244
}
melunko@38
   245
melunko@38
   246
static GnomeVFSResult
melunko@38
   247
do_close (GnomeVFSMethod *method,
melunko@38
   248
          GnomeVFSMethodHandle *method_handle,
melunko@38
   249
          GnomeVFSContext *context)
melunko@38
   250
{
melunko@111
   251
melunko@38
   252
    MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
melunko@38
   253
melunko@111
   254
    if (myth_handle->file_transfer) {
renatofilho@164
   255
	    gmyth_file_transfer_close (myth_handle->file_transfer);
melunko@38
   256
        g_object_unref (myth_handle->file_transfer);
renatofilho@164
   257
    	myth_handle->file_transfer = NULL;
melunko@111
   258
    }
rosfran@127
   259
    
rosfran@277
   260
    if (myth_handle->livetv) {
rosfran@277
   261
      g_object_unref (myth_handle->livetv);
rosfran@277
   262
    	myth_handle->livetv = NULL;
rosfran@277
   263
    }
rosfran@277
   264
    
rosfran@127
   265
    if (myth_handle->buffer) {
renatofilho@164
   266
		g_byte_array_free (myth_handle->buffer, TRUE);
renatofilho@164
   267
		myth_handle->buffer = NULL;
rosfran@127
   268
    }
melunko@38
   269
melunko@38
   270
    g_free (myth_handle);
melunko@38
   271
melunko@38
   272
    return GNOME_VFS_OK;
melunko@38
   273
}
melunko@38
   274
melunko@38
   275
static GnomeVFSResult
melunko@38
   276
do_get_file_info (GnomeVFSMethod *method,
melunko@38
   277
                  GnomeVFSURI *uri,
melunko@38
   278
                  GnomeVFSFileInfo *file_info,
melunko@38
   279
                  GnomeVFSFileInfoOptions options,
melunko@38
   280
                  GnomeVFSContext *context)
melunko@38
   281
{
renatofilho@188
   282
    GMythFileTransfer *file_transfer = NULL;
renatofilho@188
   283
    GMythBackendInfo *backend_info = NULL;
renatofilho@188
   284
renatofilho@188
   285
    file_info->name = g_strdup (gnome_vfs_uri_get_path (uri));
melunko@38
   286
    file_info->valid_fields = file_info->valid_fields
melunko@38
   287
        | GNOME_VFS_FILE_INFO_FIELDS_TYPE
melunko@38
   288
        | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
melunko@38
   289
        | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
melunko@38
   290
    file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
melunko@111
   291
    // fixme: get from file extension?
melunko@111
   292
    file_info->mime_type = g_strdup ("video/x-nuv");
melunko@38
   293
    file_info->permissions =
melunko@38
   294
        GNOME_VFS_PERM_USER_READ |
melunko@38
   295
        GNOME_VFS_PERM_OTHER_READ |
melunko@38
   296
        GNOME_VFS_PERM_GROUP_READ;
melunko@38
   297
renatofilho@188
   298
    backend_info = gmyth_backend_info_new_full (gnome_vfs_uri_get_host_name (uri),
renatofilho@188
   299
            gnome_vfs_uri_get_user_name (uri),
renatofilho@188
   300
            gnome_vfs_uri_get_password (uri),
renatofilho@188
   301
            NULL,
renatofilho@188
   302
            gnome_vfs_uri_get_host_port (uri));
renatofilho@188
   303
renatofilho@188
   304
    file_transfer = gmyth_file_transfer_new (backend_info);
renatofilho@188
   305
    if (gmyth_file_transfer_open (file_transfer, gnome_vfs_uri_get_path (uri)) == TRUE) {
renatofilho@188
   306
        file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
renatofilho@188
   307
        file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
renatofilho@188
   308
        file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
renatofilho@188
   309
    }
renatofilho@188
   310
renatofilho@188
   311
    g_object_unref (file_transfer);
renatofilho@188
   312
    g_object_unref (backend_info);
melunko@38
   313
    return GNOME_VFS_OK;
melunko@38
   314
}
melunko@38
   315
melunko@38
   316
static gboolean
melunko@38
   317
do_is_local (GnomeVFSMethod *method,
melunko@38
   318
             const GnomeVFSURI *uri)
melunko@38
   319
{
melunko@38
   320
	return FALSE;
melunko@38
   321
}
melunko@38
   322
melunko@38
   323
static GnomeVFSMethod method = {
melunko@38
   324
	sizeof (GnomeVFSMethod),
melunko@38
   325
	do_open,
melunko@38
   326
	NULL,
melunko@38
   327
	do_close,
melunko@38
   328
	do_read,
melunko@38
   329
	NULL,
melunko@38
   330
	NULL,
melunko@38
   331
	NULL,
melunko@38
   332
	NULL,
melunko@38
   333
	NULL,
melunko@38
   334
	NULL,
melunko@38
   335
	NULL,
melunko@38
   336
	do_get_file_info,
melunko@38
   337
	NULL,
melunko@38
   338
	do_is_local,
melunko@38
   339
	NULL,
melunko@38
   340
	NULL,
melunko@38
   341
	NULL,
melunko@38
   342
	NULL,
melunko@38
   343
	NULL,
melunko@38
   344
	NULL,
melunko@38
   345
	NULL,
melunko@38
   346
	NULL,
melunko@38
   347
	NULL,
melunko@38
   348
	NULL,
melunko@38
   349
	NULL,
melunko@38
   350
	NULL,
melunko@38
   351
};
melunko@38
   352
melunko@38
   353
melunko@38
   354
GnomeVFSMethod *
melunko@38
   355
vfs_module_init (const char *method_name, const char *args)
melunko@38
   356
{
melunko@38
   357
	return &method;
melunko@38
   358
}
melunko@38
   359
melunko@38
   360
void
melunko@38
   361
vfs_module_shutdown (GnomeVFSMethod *method)
melunko@38
   362
{
melunko@38
   363
}